Sat Dec 4 23:47:10 2021 UTC ()
make: merge duplicate code in ParseDirective

No functional change.


(rillig)
diff -r1.568 -r1.569 src/usr.bin/make/parse.c

cvs diff -r1.568 -r1.569 src/usr.bin/make/parse.c (expand / switch to unified diff)

--- src/usr.bin/make/parse.c 2021/12/03 23:37:30 1.568
+++ src/usr.bin/make/parse.c 2021/12/04 23:47:09 1.569
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.568 2021/12/03 23:37:30 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.569 2021/12/04 23:47:09 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -99,27 +99,27 @@ @@ -99,27 +99,27 @@
99 99
100#include <sys/types.h> 100#include <sys/types.h>
101#include <sys/stat.h> 101#include <sys/stat.h>
102#include <errno.h> 102#include <errno.h>
103#include <stdarg.h> 103#include <stdarg.h>
104#include <stdint.h> 104#include <stdint.h>
105 105
106#include "make.h" 106#include "make.h"
107#include "dir.h" 107#include "dir.h"
108#include "job.h" 108#include "job.h"
109#include "pathnames.h" 109#include "pathnames.h"
110 110
111/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */ 111/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
112MAKE_RCSID("$NetBSD: parse.c,v 1.568 2021/12/03 23:37:30 rillig Exp $"); 112MAKE_RCSID("$NetBSD: parse.c,v 1.569 2021/12/04 23:47:09 rillig Exp $");
113 113
114/* types and constants */ 114/* types and constants */
115 115
116/* 116/*
117 * Structure for a file being read ("included file") 117 * Structure for a file being read ("included file")
118 */ 118 */
119typedef struct IFile { 119typedef struct IFile {
120 char *fname; /* name of file (relative? absolute?) */ 120 char *fname; /* name of file (relative? absolute?) */
121 bool fromForLoop; /* simulated .include by the .for loop */ 121 bool fromForLoop; /* simulated .include by the .for loop */
122 int lineno; /* current line number in file */ 122 int lineno; /* current line number in file */
123 int first_lineno; /* line number of start of text */ 123 int first_lineno; /* line number of start of text */
124 unsigned int cond_depth; /* 'if' nesting when file opened */ 124 unsigned int cond_depth; /* 'if' nesting when file opened */
125 bool depending; /* state of doing_depend on EOF */ 125 bool depending; /* state of doing_depend on EOF */
@@ -2987,85 +2987,79 @@ ParseLine_ShellCommand(const char *p) @@ -2987,85 +2987,79 @@ ParseLine_ShellCommand(const char *p)
2987 char *cmd = bmake_strdup(p); 2987 char *cmd = bmake_strdup(p);
2988 GNodeListNode *ln; 2988 GNodeListNode *ln;
2989 2989
2990 for (ln = targets->first; ln != NULL; ln = ln->next) { 2990 for (ln = targets->first; ln != NULL; ln = ln->next) {
2991 GNode *gn = ln->datum; 2991 GNode *gn = ln->datum;
2992 ParseAddCmd(gn, cmd); 2992 ParseAddCmd(gn, cmd);
2993 } 2993 }
2994#ifdef CLEANUP 2994#ifdef CLEANUP
2995 Lst_Append(&targCmds, cmd); 2995 Lst_Append(&targCmds, cmd);
2996#endif 2996#endif
2997 } 2997 }
2998} 2998}
2999 2999
3000MAKE_INLINE bool 
3001IsDirective(const char *dir, size_t dirlen, const char *name) 
3002{ 
3003 return dirlen == strlen(name) && memcmp(dir, name, dirlen) == 0; 
3004} 
3005 
3006/* 3000/*
3007 * See if the line starts with one of the known directives, and if so, handle 3001 * See if the line starts with one of the known directives, and if so, handle
3008 * the directive. 3002 * the directive.
3009 */ 3003 */
3010static bool 3004static bool
3011ParseDirective(char *line) 3005ParseDirective(char *line)
3012{ 3006{
3013 char *cp = line + 1; 3007 char *cp = line + 1;
3014 const char *dir, *arg; 3008 const char *arg;
3015 size_t dirlen; 3009 Substring dir;
3016 3010
3017 pp_skip_whitespace(&cp); 3011 pp_skip_whitespace(&cp);
3018 if (IsInclude(cp, false)) { 3012 if (IsInclude(cp, false)) {
3019 ParseInclude(cp); 3013 ParseInclude(cp);
3020 return true; 3014 return true;
3021 } 3015 }
3022 3016
3023 dir = cp; 3017 dir.start = cp;
3024 while (ch_isalpha(*cp) || *cp == '-') 3018 while (ch_isalpha(*cp) || *cp == '-')
3025 cp++; 3019 cp++;
3026 dirlen = (size_t)(cp - dir); 3020 dir.end = cp;
3027 3021
3028 if (*cp != '\0' && !ch_isspace(*cp)) 3022 if (*cp != '\0' && !ch_isspace(*cp))
3029 return false; 3023 return false;
3030 3024
3031 pp_skip_whitespace(&cp); 3025 pp_skip_whitespace(&cp);
3032 arg = cp; 3026 arg = cp;
3033 3027
3034 if (IsDirective(dir, dirlen, "undef")) { 3028 if (Substring_Equals(dir, "undef")) {
3035 Var_Undef(cp); 3029 Var_Undef(arg);
3036 return true; 3030 return true;
3037 } else if (IsDirective(dir, dirlen, "export")) { 3031 } else if (Substring_Equals(dir, "export")) {
3038 Var_Export(VEM_PLAIN, arg); 3032 Var_Export(VEM_PLAIN, arg);
3039 return true; 3033 return true;
3040 } else if (IsDirective(dir, dirlen, "export-env")) { 3034 } else if (Substring_Equals(dir, "export-env")) {
3041 Var_Export(VEM_ENV, arg); 3035 Var_Export(VEM_ENV, arg);
3042 return true; 3036 return true;
3043 } else if (IsDirective(dir, dirlen, "export-literal")) { 3037 } else if (Substring_Equals(dir, "export-literal")) {
3044 Var_Export(VEM_LITERAL, arg); 3038 Var_Export(VEM_LITERAL, arg);
3045 return true; 3039 return true;
3046 } else if (IsDirective(dir, dirlen, "unexport")) { 3040 } else if (Substring_Equals(dir, "unexport")) {
3047 Var_UnExport(false, arg); 3041 Var_UnExport(false, arg);
3048 return true; 3042 return true;
3049 } else if (IsDirective(dir, dirlen, "unexport-env")) { 3043 } else if (Substring_Equals(dir, "unexport-env")) {
3050 Var_UnExport(true, arg); 3044 Var_UnExport(true, arg);
3051 return true; 3045 return true;
3052 } else if (IsDirective(dir, dirlen, "info")) { 3046 } else if (Substring_Equals(dir, "info")) {
3053 ParseMessage(PARSE_INFO, "info", arg); 3047 ParseMessage(PARSE_INFO, "info", arg);
3054 return true; 3048 return true;
3055 } else if (IsDirective(dir, dirlen, "warning")) { 3049 } else if (Substring_Equals(dir, "warning")) {
3056 ParseMessage(PARSE_WARNING, "warning", arg); 3050 ParseMessage(PARSE_WARNING, "warning", arg);
3057 return true; 3051 return true;
3058 } else if (IsDirective(dir, dirlen, "error")) { 3052 } else if (Substring_Equals(dir, "error")) {
3059 ParseMessage(PARSE_FATAL, "error", arg); 3053 ParseMessage(PARSE_FATAL, "error", arg);
3060 return true; 3054 return true;
3061 } 3055 }
3062 return false; 3056 return false;
3063} 3057}
3064 3058
3065static bool 3059static bool
3066ParseVarassign(const char *line) 3060ParseVarassign(const char *line)
3067{ 3061{
3068 VarAssign var; 3062 VarAssign var;
3069 3063
3070 if (!Parse_IsVar(line, &var)) 3064 if (!Parse_IsVar(line, &var))
3071 return false; 3065 return false;