summaryrefslogtreecommitdiff
path: root/net/rdist6/files/patch-src_gram.y
blob: dd323eaa7b62288ac04412898188af7f40710c8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
--- src/gram.y.orig	1998-11-10 04:10:42 UTC
+++ src/gram.y
@@ -61,6 +61,13 @@ static char copyright[] =
 
 #include "defs.h"
 
+#define	yylex	rdist_yylex
+
+static char * xappend(char *str, size_t *len, char c);
+void yyerror(const char *message);
+static char *yytext;
+static size_t yytextlen;
+
 static struct namelist *addnl(), *subnl(), *andnl();
 struct	cmd *cmds = NULL;
 struct	cmd *last_cmd;
@@ -240,12 +247,14 @@ opt_namelist:	  /* VOID */ = {
 int	yylineno = 1;
 extern	FILE *fin;
 
+static int
 yylex()
 {
-	static char yytext[INMAX];
 	register int c;
-	register char *cp1, *cp2;
 	static char quotechars[] = "[]{}*?$";
+
+	yytext = NULL;
+	yytextlen = 0;
 	
 again:
 	switch (c = getc(fin)) {
@@ -285,19 +294,13 @@ again:
 		break;
 
 	case '"':  /* STRING */
-		cp1 = yytext;
-		cp2 = &yytext[INMAX - 1];
 		for (;;) {
-			if (cp1 >= cp2) {
-				yyerror("command string too long\n");
-				break;
-			}
 			c = getc(fin);
 			if (c == EOF || c == '"')
 				break;
 			if (c == '\\') {
 				if ((c = getc(fin)) == EOF) {
-					*cp1++ = '\\';
+					yytext = xappend(yytext, &yytextlen, '\\');
 					break;
 				}
 			}
@@ -305,12 +308,11 @@ again:
 				yylineno++;
 				c = ' '; /* can't send '\n' */
 			}
-			*cp1++ = c;
+			yytext = xappend(yytext, &yytextlen, c);
 		}
 		if (c != '"')
 			yyerror("missing closing '\"'\n");
-		*cp1 = '\0';
-		yylval.string = makestr(yytext);
+		yylval.string = xappend(yytext, &yytextlen, '\0');
 		return(STRING);
 
 	case ':':  /* : or :: */
@@ -319,35 +321,34 @@ again:
 		(void) ungetc(c, fin);
 		return(COLON);
 	}
-	cp1 = yytext;
-	cp2 = &yytext[INMAX - 1];
 	for (;;) {
-		if (cp1 >= cp2) {
-			yyerror("input line too long\n");
-			break;
-		}
 		if (c == '\\') {
 			if ((c = getc(fin)) != EOF) {
 				if (any(c, quotechars))
-					*cp1++ = QUOTECHAR;
+					yytext = xappend(yytext, &yytextlen,
+					    QUOTECHAR);
 			} else {
-				*cp1++ = '\\';
+				yytext = xappend(yytext, &yytextlen, '\\');
 				break;
 			}
 		}
-		*cp1++ = c;
+		yytext = xappend(yytext, &yytextlen, c);
 		c = getc(fin);
 		if (c == EOF || any(c, " \"'\t()=;:\n")) {
 			(void) ungetc(c, fin);
 			break;
 		}
 	}
-	*cp1 = '\0';
-	if (yytext[0] == '-' && yytext[1] == CNULL) 
+	if (yytext == NULL) {
+		yylval.string = NULL;
+		return(NAME);
+	}
+	yytext = xappend(yytext, &yytextlen, '\0');
+	if (yytextlen == 2 && yytext[0] == '-') 
 		return '-';
 	if (yytext[0] == '-' && parendepth <= 0) {
 		opt_t opt = 0;
-		static char ebuf[BUFSIZ];
+		char ebuf[BUFSIZ];
 
 		switch (yytext[1]) {
 		case 'o':
@@ -397,7 +398,7 @@ again:
 	else if (!strcmp(yytext, "cmdspecial"))
 		c = CMDSPECIAL;
 	else {
-		yylval.string = makestr(yytext);
+		yylval.string = yytext;
 		return(NAME);
 	}
 	yylval.subcmd = makesubcmd(c);
@@ -421,6 +422,7 @@ extern int any(c, str)
 /*
  * Insert or append ARROW command to list of hosts to be updated.
  */
+void
 insert(label, files, hosts, subcmds)
 	char *label;
 	struct namelist *files, *hosts;
@@ -475,6 +477,7 @@ insert(label, files, hosts, subcmds)
  * Append DCOLON command to the end of the command list since these are always
  * executed in the order they appear in the distfile.
  */
+static void
 append(label, files, stamp, subcmds)
 	char *label;
 	struct namelist *files;
@@ -501,24 +504,30 @@ append(label, files, stamp, subcmds)
 /*
  * Error printing routine in parser.
  */
+void
 yyerror(s)
-	char *s;
+	const char *s;
 {
-	error("Error in distfile: line %d: %s", yylineno, s);
+	error("Error in distfile: line %d: %s (offending text: %.*s%s)",
+	    yylineno, s, 11, yytextlen > 0 ? yytext : "<empty>",
+	    yytextlen > 11 ? " ..." : "");
 }
 
 /*
- * Return a copy of the string.
+ * Append character to a string of given length. Return
+ * the new string, which -- depending on realloc -- may
+ * or may not have the same address now.
  */
-char *
-makestr(str)
-	char *str;
+static char *
+xappend(char *str, size_t *len, char c)
 {
 	char *cp;
 
-	cp = strdup(str);
+	cp = realloc(str, *len + 1);
 	if (cp == NULL)
 		fatalerr("ran out of memory");
+	cp[*len] = c;
+	(*len)++;
 
 	return(cp);
 }