compile.c revision 1.44 1 1.44 asau /* $NetBSD: compile.c,v 1.44 2015/02/28 21:56:53 asau Exp $ */
2 1.15 tls
3 1.1 alm /*-
4 1.41 christos * Copyright (c) 1992 Diomidis Spinellis.
5 1.9 cgd * Copyright (c) 1992, 1993
6 1.9 cgd * The Regents of the University of California. All rights reserved.
7 1.1 alm *
8 1.1 alm * This code is derived from software contributed to Berkeley by
9 1.1 alm * Diomidis Spinellis of Imperial College, University of London.
10 1.1 alm *
11 1.1 alm * Redistribution and use in source and binary forms, with or without
12 1.1 alm * modification, are permitted provided that the following conditions
13 1.1 alm * are met:
14 1.1 alm * 1. Redistributions of source code must retain the above copyright
15 1.1 alm * notice, this list of conditions and the following disclaimer.
16 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 alm * notice, this list of conditions and the following disclaimer in the
18 1.1 alm * documentation and/or other materials provided with the distribution.
19 1.25 agc * 3. Neither the name of the University nor the names of its contributors
20 1.25 agc * may be used to endorse or promote products derived from this software
21 1.25 agc * without specific prior written permission.
22 1.25 agc *
23 1.25 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.25 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.25 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.25 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.25 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.25 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.25 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.25 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.25 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.25 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.25 agc * SUCH DAMAGE.
34 1.25 agc */
35 1.25 agc
36 1.34 gdamore #if HAVE_NBTOOL_CONFIG_H
37 1.34 gdamore #include "nbtool_config.h"
38 1.34 gdamore #endif
39 1.34 gdamore
40 1.17 lukem #include <sys/cdefs.h>
41 1.44 asau __RCSID("$NetBSD: compile.c,v 1.44 2015/02/28 21:56:53 asau Exp $");
42 1.41 christos #ifdef __FBSDID
43 1.41 christos __FBSDID("$FreeBSD: head/usr.bin/sed/compile.c 259132 2013-12-09 18:57:20Z eadler $");
44 1.41 christos #endif
45 1.41 christos
46 1.43 christos #if 0
47 1.43 christos static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
48 1.43 christos #endif
49 1.43 christos
50 1.1 alm #include <sys/types.h>
51 1.1 alm #include <sys/stat.h>
52 1.1 alm
53 1.1 alm #include <ctype.h>
54 1.41 christos #include <err.h>
55 1.1 alm #include <errno.h>
56 1.1 alm #include <fcntl.h>
57 1.1 alm #include <limits.h>
58 1.1 alm #include <regex.h>
59 1.1 alm #include <stdio.h>
60 1.1 alm #include <stdlib.h>
61 1.1 alm #include <string.h>
62 1.41 christos #include <wchar.h>
63 1.1 alm
64 1.1 alm #include "defs.h"
65 1.1 alm #include "extern.h"
66 1.1 alm
67 1.9 cgd #define LHSZ 128
68 1.9 cgd #define LHMASK (LHSZ - 1)
69 1.9 cgd static struct labhash {
70 1.9 cgd struct labhash *lh_next;
71 1.9 cgd u_int lh_hash;
72 1.9 cgd struct s_command *lh_cmd;
73 1.9 cgd int lh_ref;
74 1.9 cgd } *labels[LHSZ];
75 1.9 cgd
76 1.44 asau static char *cu_fgets(char *, int, int *);
77 1.44 asau
78 1.24 wiz static char *compile_addr(char *, struct s_addr *);
79 1.24 wiz static char *compile_ccl(char **, char *);
80 1.41 christos static char *compile_delimited(char *, char *, int);
81 1.24 wiz static char *compile_flags(char *, struct s_subst *);
82 1.41 christos static regex_t *compile_re(char *, int);
83 1.24 wiz static char *compile_subst(char *, struct s_subst *);
84 1.24 wiz static char *compile_text(void);
85 1.41 christos static char *compile_tr(char *, struct s_tr **);
86 1.1 alm static struct s_command
87 1.24 wiz **compile_stream(struct s_command **);
88 1.36 lukem static char *duptoeol(char *, const char *);
89 1.24 wiz static void enterlabel(struct s_command *);
90 1.1 alm static struct s_command
91 1.24 wiz *findlabel(char *);
92 1.24 wiz static void fixuplabel(struct s_command *, struct s_command *);
93 1.24 wiz static void uselabel(void);
94 1.1 alm
95 1.1 alm /*
96 1.44 asau * Current file and line number; line numbers restart across compilation
97 1.44 asau * units, but span across input files. The latter is optional if editing
98 1.44 asau * in place.
99 1.44 asau */
100 1.44 asau static const char *fname; /* File name. */
101 1.44 asau static u_long linenum;
102 1.44 asau
103 1.44 asau /*
104 1.1 alm * Command specification. This is used to drive the command parser.
105 1.1 alm */
106 1.1 alm struct s_format {
107 1.1 alm char code; /* Command code */
108 1.1 alm int naddr; /* Number of address args */
109 1.1 alm enum e_args args; /* Argument type */
110 1.1 alm };
111 1.1 alm
112 1.1 alm static struct s_format cmd_fmts[] = {
113 1.1 alm {'{', 2, GROUP},
114 1.14 mycroft {'}', 0, ENDGROUP},
115 1.1 alm {'a', 1, TEXT},
116 1.1 alm {'b', 2, BRANCH},
117 1.1 alm {'c', 2, TEXT},
118 1.1 alm {'d', 2, EMPTY},
119 1.1 alm {'D', 2, EMPTY},
120 1.1 alm {'g', 2, EMPTY},
121 1.1 alm {'G', 2, EMPTY},
122 1.1 alm {'h', 2, EMPTY},
123 1.1 alm {'H', 2, EMPTY},
124 1.1 alm {'i', 1, TEXT},
125 1.1 alm {'l', 2, EMPTY},
126 1.1 alm {'n', 2, EMPTY},
127 1.1 alm {'N', 2, EMPTY},
128 1.1 alm {'p', 2, EMPTY},
129 1.1 alm {'P', 2, EMPTY},
130 1.1 alm {'q', 1, EMPTY},
131 1.1 alm {'r', 1, RFILE},
132 1.1 alm {'s', 2, SUBST},
133 1.1 alm {'t', 2, BRANCH},
134 1.1 alm {'w', 2, WFILE},
135 1.1 alm {'x', 2, EMPTY},
136 1.1 alm {'y', 2, TR},
137 1.1 alm {'!', 2, NONSEL},
138 1.1 alm {':', 0, LABEL},
139 1.1 alm {'#', 0, COMMENT},
140 1.1 alm {'=', 1, EMPTY},
141 1.1 alm {'\0', 0, COMMENT},
142 1.1 alm };
143 1.1 alm
144 1.1 alm /* The compiled program. */
145 1.1 alm struct s_command *prog;
146 1.1 alm
147 1.1 alm /*
148 1.1 alm * Compile the program into prog.
149 1.1 alm * Initialise appends.
150 1.1 alm */
151 1.1 alm void
152 1.24 wiz compile(void)
153 1.1 alm {
154 1.14 mycroft *compile_stream(&prog) = NULL;
155 1.9 cgd fixuplabel(prog, NULL);
156 1.9 cgd uselabel();
157 1.18 drochner if (appendnum > 0)
158 1.18 drochner appends = xmalloc(sizeof(struct s_appends) * appendnum);
159 1.1 alm match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
160 1.1 alm }
161 1.1 alm
162 1.41 christos #define EATSPACE() do { \
163 1.41 christos if (p) \
164 1.41 christos while (*p && isspace((unsigned char)*p)) \
165 1.41 christos p++; \
166 1.41 christos } while (0)
167 1.1 alm
168 1.1 alm static struct s_command **
169 1.24 wiz compile_stream(struct s_command **link)
170 1.14 mycroft {
171 1.17 lukem char *p;
172 1.41 christos static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
173 1.14 mycroft struct s_command *cmd, *cmd2, *stack;
174 1.1 alm struct s_format *fp;
175 1.41 christos char re[_POSIX2_LINE_MAX + 1];
176 1.1 alm int naddr; /* Number of addresses */
177 1.1 alm
178 1.14 mycroft stack = 0;
179 1.1 alm for (;;) {
180 1.41 christos if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
181 1.14 mycroft if (stack != 0)
182 1.41 christos errx(1, "%lu: %s: unexpected EOF (pending }'s)",
183 1.41 christos linenum, fname);
184 1.1 alm return (link);
185 1.1 alm }
186 1.1 alm
187 1.1 alm semicolon: EATSPACE();
188 1.41 christos if (p) {
189 1.41 christos if (*p == '#' || *p == '\0')
190 1.41 christos continue;
191 1.41 christos else if (*p == ';') {
192 1.41 christos p++;
193 1.41 christos goto semicolon;
194 1.41 christos }
195 1.20 kleink }
196 1.1 alm *link = cmd = xmalloc(sizeof(struct s_command));
197 1.1 alm link = &cmd->next;
198 1.41 christos cmd->startline = cmd->nonsel = 0;
199 1.1 alm /* First parse the addresses */
200 1.1 alm naddr = 0;
201 1.1 alm
202 1.1 alm /* Valid characters to start an address */
203 1.1 alm #define addrchar(c) (strchr("0123456789/\\$", (c)))
204 1.1 alm if (addrchar(*p)) {
205 1.1 alm naddr++;
206 1.1 alm cmd->a1 = xmalloc(sizeof(struct s_addr));
207 1.1 alm p = compile_addr(p, cmd->a1);
208 1.1 alm EATSPACE(); /* EXTENSION */
209 1.1 alm if (*p == ',') {
210 1.1 alm p++;
211 1.1 alm EATSPACE(); /* EXTENSION */
212 1.13 mycroft naddr++;
213 1.1 alm cmd->a2 = xmalloc(sizeof(struct s_addr));
214 1.1 alm p = compile_addr(p, cmd->a2);
215 1.13 mycroft EATSPACE();
216 1.12 mycroft } else
217 1.12 mycroft cmd->a2 = 0;
218 1.12 mycroft } else
219 1.13 mycroft cmd->a1 = cmd->a2 = 0;
220 1.1 alm
221 1.1 alm nonsel: /* Now parse the command */
222 1.1 alm if (!*p)
223 1.41 christos errx(1, "%lu: %s: command expected", linenum, fname);
224 1.1 alm cmd->code = *p;
225 1.1 alm for (fp = cmd_fmts; fp->code; fp++)
226 1.1 alm if (fp->code == *p)
227 1.1 alm break;
228 1.1 alm if (!fp->code)
229 1.41 christos errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
230 1.1 alm if (naddr > fp->naddr)
231 1.41 christos errx(1,
232 1.41 christos "%lu: %s: command %c expects up to %d address(es), found %d",
233 1.41 christos linenum, fname, *p, fp->naddr, naddr);
234 1.1 alm switch (fp->args) {
235 1.1 alm case NONSEL: /* ! */
236 1.13 mycroft p++;
237 1.13 mycroft EATSPACE();
238 1.1 alm cmd->nonsel = ! cmd->nonsel;
239 1.1 alm goto nonsel;
240 1.1 alm case GROUP: /* { */
241 1.1 alm p++;
242 1.1 alm EATSPACE();
243 1.14 mycroft cmd->next = stack;
244 1.14 mycroft stack = cmd;
245 1.14 mycroft link = &cmd->u.c;
246 1.14 mycroft if (*p)
247 1.14 mycroft goto semicolon;
248 1.14 mycroft break;
249 1.14 mycroft case ENDGROUP:
250 1.12 mycroft /*
251 1.12 mycroft * Short-circuit command processing, since end of
252 1.12 mycroft * group is really just a noop.
253 1.12 mycroft */
254 1.14 mycroft cmd->nonsel = 1;
255 1.14 mycroft if (stack == 0)
256 1.41 christos errx(1, "%lu: %s: unexpected }", linenum, fname);
257 1.14 mycroft cmd2 = stack;
258 1.14 mycroft stack = cmd2->next;
259 1.14 mycroft cmd2->next = cmd;
260 1.14 mycroft /*FALLTHROUGH*/
261 1.1 alm case EMPTY: /* d D g G h H l n N p P q x = \0 */
262 1.1 alm p++;
263 1.1 alm EATSPACE();
264 1.40 christos switch (*p) {
265 1.40 christos case ';':
266 1.1 alm p++;
267 1.1 alm link = &cmd->next;
268 1.1 alm goto semicolon;
269 1.40 christos case '}':
270 1.40 christos goto semicolon;
271 1.40 christos case '\0':
272 1.40 christos break;
273 1.40 christos default:
274 1.41 christos errx(1, "%lu: %s: extra characters at the end of %c command",
275 1.41 christos linenum, fname, cmd->code);
276 1.40 christos }
277 1.1 alm break;
278 1.1 alm case TEXT: /* a c i */
279 1.1 alm p++;
280 1.1 alm EATSPACE();
281 1.1 alm if (*p != '\\')
282 1.41 christos errx(1,
283 1.41 christos "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
284 1.1 alm p++;
285 1.1 alm EATSPACE();
286 1.1 alm if (*p)
287 1.41 christos errx(1,
288 1.41 christos "%lu: %s: extra characters after \\ at the end of %c command",
289 1.41 christos linenum, fname, cmd->code);
290 1.1 alm cmd->t = compile_text();
291 1.1 alm break;
292 1.1 alm case COMMENT: /* \0 # */
293 1.1 alm break;
294 1.1 alm case WFILE: /* w */
295 1.1 alm p++;
296 1.1 alm EATSPACE();
297 1.1 alm if (*p == '\0')
298 1.41 christos errx(1, "%lu: %s: filename expected", linenum, fname);
299 1.9 cgd cmd->t = duptoeol(p, "w command");
300 1.1 alm if (aflag)
301 1.1 alm cmd->u.fd = -1;
302 1.41 christos else if ((cmd->u.fd = open(p,
303 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
304 1.1 alm DEFFILEMODE)) == -1)
305 1.41 christos err(1, "%s", p);
306 1.1 alm break;
307 1.1 alm case RFILE: /* r */
308 1.1 alm p++;
309 1.1 alm EATSPACE();
310 1.1 alm if (*p == '\0')
311 1.41 christos errx(1, "%lu: %s: filename expected", linenum, fname);
312 1.1 alm else
313 1.9 cgd cmd->t = duptoeol(p, "read command");
314 1.1 alm break;
315 1.1 alm case BRANCH: /* b t */
316 1.1 alm p++;
317 1.1 alm EATSPACE();
318 1.1 alm if (*p == '\0')
319 1.1 alm cmd->t = NULL;
320 1.1 alm else
321 1.9 cgd cmd->t = duptoeol(p, "branch");
322 1.1 alm break;
323 1.1 alm case LABEL: /* : */
324 1.1 alm p++;
325 1.1 alm EATSPACE();
326 1.9 cgd cmd->t = duptoeol(p, "label");
327 1.1 alm if (strlen(p) == 0)
328 1.41 christos errx(1, "%lu: %s: empty label", linenum, fname);
329 1.9 cgd enterlabel(cmd);
330 1.1 alm break;
331 1.1 alm case SUBST: /* s */
332 1.1 alm p++;
333 1.1 alm if (*p == '\0' || *p == '\\')
334 1.41 christos errx(1,
335 1.41 christos "%lu: %s: substitute pattern can not be delimited by newline or backslash",
336 1.41 christos linenum, fname);
337 1.41 christos cmd->u.s = xcalloc(1, sizeof(struct s_subst));
338 1.41 christos p = compile_delimited(p, re, 0);
339 1.1 alm if (p == NULL)
340 1.41 christos errx(1,
341 1.41 christos "%lu: %s: unterminated substitute pattern", linenum, fname);
342 1.41 christos
343 1.41 christos /* Compile RE with no case sensitivity temporarily */
344 1.41 christos if (*re == '\0')
345 1.41 christos cmd->u.s->re = NULL;
346 1.41 christos else
347 1.41 christos cmd->u.s->re = compile_re(re, 0);
348 1.1 alm --p;
349 1.1 alm p = compile_subst(p, cmd->u.s);
350 1.1 alm p = compile_flags(p, cmd->u.s);
351 1.41 christos
352 1.41 christos /* Recompile RE with case sensitivity from "I" flag if any */
353 1.41 christos if (*re == '\0')
354 1.41 christos cmd->u.s->re = NULL;
355 1.41 christos else
356 1.41 christos cmd->u.s->re = compile_re(re, cmd->u.s->icase);
357 1.1 alm EATSPACE();
358 1.1 alm if (*p == ';') {
359 1.1 alm p++;
360 1.1 alm link = &cmd->next;
361 1.1 alm goto semicolon;
362 1.1 alm }
363 1.1 alm break;
364 1.1 alm case TR: /* y */
365 1.1 alm p++;
366 1.41 christos p = compile_tr(p, &cmd->u.y);
367 1.1 alm EATSPACE();
368 1.40 christos switch (*p) {
369 1.40 christos case ';':
370 1.1 alm p++;
371 1.1 alm link = &cmd->next;
372 1.1 alm goto semicolon;
373 1.40 christos case '}':
374 1.40 christos goto semicolon;
375 1.40 christos case '\0':
376 1.40 christos break;
377 1.40 christos default:
378 1.41 christos errx(1,
379 1.41 christos "%lu: %s: extra text at the end of a transform command", linenum, fname);
380 1.40 christos }
381 1.41 christos if (*p)
382 1.1 alm break;
383 1.1 alm }
384 1.1 alm }
385 1.1 alm }
386 1.1 alm
387 1.1 alm /*
388 1.41 christos * Get a delimited string. P points to the delimeter of the string; d points
389 1.1 alm * to a buffer area. Newline and delimiter escapes are processed; other
390 1.1 alm * escapes are ignored.
391 1.1 alm *
392 1.1 alm * Returns a pointer to the first character after the final delimiter or NULL
393 1.1 alm * in the case of a non-terminated string. The character array d is filled
394 1.1 alm * with the processed string.
395 1.1 alm */
396 1.1 alm static char *
397 1.41 christos compile_delimited(char *p, char *d, int is_tr)
398 1.1 alm {
399 1.1 alm char c;
400 1.1 alm
401 1.1 alm c = *p++;
402 1.1 alm if (c == '\0')
403 1.1 alm return (NULL);
404 1.1 alm else if (c == '\\')
405 1.41 christos errx(1, "%lu: %s: \\ can not be used as a string delimiter",
406 1.41 christos linenum, fname);
407 1.1 alm else if (c == '\n')
408 1.41 christos errx(1, "%lu: %s: newline can not be used as a string delimiter",
409 1.41 christos linenum, fname);
410 1.1 alm while (*p) {
411 1.41 christos if (*p == '[' && *p != c) {
412 1.11 alm if ((d = compile_ccl(&p, d)) == NULL)
413 1.41 christos errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
414 1.11 alm continue;
415 1.11 alm } else if (*p == '\\' && p[1] == '[') {
416 1.11 alm *d++ = *p++;
417 1.11 alm } else if (*p == '\\' && p[1] == c)
418 1.1 alm p++;
419 1.1 alm else if (*p == '\\' && p[1] == 'n') {
420 1.1 alm *d++ = '\n';
421 1.1 alm p += 2;
422 1.1 alm continue;
423 1.41 christos } else if (*p == '\\' && p[1] == '\\') {
424 1.41 christos if (is_tr)
425 1.41 christos p++;
426 1.41 christos else
427 1.41 christos *d++ = *p++;
428 1.41 christos } else if (*p == c) {
429 1.1 alm *d = '\0';
430 1.1 alm return (p + 1);
431 1.1 alm }
432 1.1 alm *d++ = *p++;
433 1.1 alm }
434 1.1 alm return (NULL);
435 1.11 alm }
436 1.11 alm
437 1.11 alm
438 1.11 alm /* compile_ccl: expand a POSIX character class */
439 1.11 alm static char *
440 1.24 wiz compile_ccl(char **sp, char *t)
441 1.11 alm {
442 1.11 alm int c, d;
443 1.11 alm char *s = *sp;
444 1.11 alm
445 1.11 alm *t++ = *s++;
446 1.11 alm if (*s == '^')
447 1.11 alm *t++ = *s++;
448 1.11 alm if (*s == ']')
449 1.11 alm *t++ = *s++;
450 1.11 alm for (; *s && (*t = *s) != ']'; s++, t++)
451 1.11 alm if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
452 1.11 alm *++t = *++s, t++, s++;
453 1.11 alm for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
454 1.11 alm if ((c = *s) == '\0')
455 1.11 alm return NULL;
456 1.41 christos }
457 1.11 alm return (*s == ']') ? *sp = ++s, ++t : NULL;
458 1.2 alm }
459 1.2 alm
460 1.1 alm /*
461 1.41 christos * Compiles the regular expression in RE and returns a pointer to the compiled
462 1.41 christos * regular expression.
463 1.1 alm * Cflags are passed to regcomp.
464 1.1 alm */
465 1.41 christos static regex_t *
466 1.41 christos compile_re(char *re, int case_insensitive)
467 1.1 alm {
468 1.41 christos regex_t *rep;
469 1.41 christos int eval, flags;
470 1.41 christos
471 1.1 alm
472 1.41 christos flags = rflags;
473 1.41 christos if (case_insensitive)
474 1.41 christos flags |= REG_ICASE;
475 1.41 christos rep = xmalloc(sizeof(regex_t));
476 1.41 christos if ((eval = regcomp(rep, re, flags)) != 0)
477 1.41 christos errx(1, "%lu: %s: RE error: %s",
478 1.41 christos linenum, fname, strregerror(eval, rep));
479 1.41 christos if (maxnsub < rep->re_nsub)
480 1.41 christos maxnsub = rep->re_nsub;
481 1.41 christos return (rep);
482 1.1 alm }
483 1.1 alm
484 1.1 alm /*
485 1.1 alm * Compile the substitution string of a regular expression and set res to
486 1.1 alm * point to a saved copy of it. Nsub is the number of parenthesized regular
487 1.1 alm * expressions.
488 1.1 alm */
489 1.1 alm static char *
490 1.24 wiz compile_subst(char *p, struct s_subst *s)
491 1.1 alm {
492 1.41 christos static char lbuf[_POSIX2_LINE_MAX + 1];
493 1.41 christos size_t asize, size;
494 1.41 christos u_char ref;
495 1.1 alm char c, *text, *op, *sp;
496 1.41 christos int more = 1, sawesc = 0;
497 1.1 alm
498 1.1 alm c = *p++; /* Terminator character */
499 1.1 alm if (c == '\0')
500 1.1 alm return (NULL);
501 1.1 alm
502 1.1 alm s->maxbref = 0;
503 1.1 alm s->linenum = linenum;
504 1.41 christos asize = 2 * _POSIX2_LINE_MAX + 1;
505 1.41 christos text = xmalloc(asize);
506 1.41 christos size = 0;
507 1.1 alm do {
508 1.1 alm op = sp = text + size;
509 1.1 alm for (; *p; p++) {
510 1.26 minskim if (*p == '\\' || sawesc) {
511 1.26 minskim /*
512 1.26 minskim * If this is a continuation from the last
513 1.26 minskim * buffer, we won't have a character to
514 1.26 minskim * skip over.
515 1.26 minskim */
516 1.26 minskim if (sawesc)
517 1.26 minskim sawesc = 0;
518 1.26 minskim else
519 1.26 minskim p++;
520 1.26 minskim
521 1.26 minskim if (*p == '\0') {
522 1.26 minskim /*
523 1.26 minskim * This escaped character is continued
524 1.26 minskim * in the next part of the line. Note
525 1.26 minskim * this fact, then cause the loop to
526 1.26 minskim * exit w/ normal EOL case and reenter
527 1.26 minskim * above with the new buffer.
528 1.26 minskim */
529 1.26 minskim sawesc = 1;
530 1.26 minskim p--;
531 1.26 minskim continue;
532 1.26 minskim } else if (strchr("123456789", *p) != NULL) {
533 1.1 alm *sp++ = '\\';
534 1.41 christos ref = (u_char)(*p - '0');
535 1.1 alm if (s->re != NULL &&
536 1.41 christos ref > s->re->re_nsub)
537 1.41 christos errx(1, "%lu: %s: \\%c not defined in the RE",
538 1.41 christos linenum, fname, *p);
539 1.1 alm if (s->maxbref < ref)
540 1.1 alm s->maxbref = ref;
541 1.1 alm } else if (*p == '&' || *p == '\\')
542 1.1 alm *sp++ = '\\';
543 1.1 alm } else if (*p == c) {
544 1.41 christos if (*++p == '\0' && more) {
545 1.41 christos if (cu_fgets(lbuf, sizeof(lbuf), &more))
546 1.41 christos p = lbuf;
547 1.41 christos }
548 1.1 alm *sp++ = '\0';
549 1.41 christos size += (size_t)(sp - op);
550 1.1 alm s->new = xrealloc(text, size);
551 1.1 alm return (p);
552 1.1 alm } else if (*p == '\n') {
553 1.41 christos errx(1,
554 1.41 christos "%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
555 1.1 alm /* NOTREACHED */
556 1.1 alm }
557 1.1 alm *sp++ = *p;
558 1.1 alm }
559 1.41 christos size += (size_t)(sp - op);
560 1.41 christos if (asize - size < _POSIX2_LINE_MAX + 1) {
561 1.41 christos asize *= 2;
562 1.41 christos text = xrealloc(text, asize);
563 1.41 christos }
564 1.41 christos } while (cu_fgets(p = lbuf, sizeof(lbuf), &more));
565 1.41 christos errx(1, "%lu: %s: unterminated substitute in regular expression",
566 1.41 christos linenum, fname);
567 1.1 alm /* NOTREACHED */
568 1.1 alm }
569 1.1 alm
570 1.1 alm /*
571 1.1 alm * Compile the flags of the s command
572 1.1 alm */
573 1.1 alm static char *
574 1.24 wiz compile_flags(char *p, struct s_subst *s)
575 1.1 alm {
576 1.1 alm int gn; /* True if we have seen g or n */
577 1.41 christos unsigned long nval;
578 1.41 christos char wfile[_POSIX2_LINE_MAX + 1], *q;
579 1.1 alm
580 1.1 alm s->n = 1; /* Default */
581 1.1 alm s->p = 0;
582 1.1 alm s->wfile = NULL;
583 1.1 alm s->wfd = -1;
584 1.41 christos s->icase = 0;
585 1.1 alm for (gn = 0;;) {
586 1.1 alm EATSPACE(); /* EXTENSION */
587 1.1 alm switch (*p) {
588 1.1 alm case 'g':
589 1.1 alm if (gn)
590 1.41 christos errx(1,
591 1.41 christos "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
592 1.1 alm gn = 1;
593 1.1 alm s->n = 0;
594 1.1 alm break;
595 1.1 alm case '\0':
596 1.1 alm case '\n':
597 1.1 alm case ';':
598 1.1 alm return (p);
599 1.1 alm case 'p':
600 1.1 alm s->p = 1;
601 1.1 alm break;
602 1.41 christos case 'i':
603 1.41 christos case 'I':
604 1.41 christos s->icase = 1;
605 1.41 christos break;
606 1.1 alm case '1': case '2': case '3':
607 1.1 alm case '4': case '5': case '6':
608 1.1 alm case '7': case '8': case '9':
609 1.1 alm if (gn)
610 1.41 christos errx(1,
611 1.41 christos "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
612 1.1 alm gn = 1;
613 1.41 christos errno = 0;
614 1.41 christos nval = strtoul(p, &p, 10);
615 1.41 christos if (errno == ERANGE || nval > INT_MAX)
616 1.41 christos errx(1,
617 1.41 christos "%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
618 1.41 christos s->n = (int)nval;
619 1.27 grant p--;
620 1.1 alm break;
621 1.1 alm case 'w':
622 1.1 alm p++;
623 1.9 cgd #ifdef HISTORIC_PRACTICE
624 1.1 alm if (*p != ' ') {
625 1.41 christos warnx("%lu: %s: space missing before w wfile", linenum, fname);
626 1.1 alm return (p);
627 1.1 alm }
628 1.1 alm #endif
629 1.1 alm EATSPACE();
630 1.1 alm q = wfile;
631 1.1 alm while (*p) {
632 1.1 alm if (*p == '\n')
633 1.1 alm break;
634 1.1 alm *q++ = *p++;
635 1.1 alm }
636 1.1 alm *q = '\0';
637 1.1 alm if (q == wfile)
638 1.41 christos errx(1, "%lu: %s: no wfile specified", linenum, fname);
639 1.1 alm s->wfile = strdup(wfile);
640 1.1 alm if (!aflag && (s->wfd = open(wfile,
641 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
642 1.1 alm DEFFILEMODE)) == -1)
643 1.41 christos err(1, "%s", wfile);
644 1.1 alm return (p);
645 1.1 alm default:
646 1.41 christos errx(1, "%lu: %s: bad flag in substitute command: '%c'",
647 1.41 christos linenum, fname, *p);
648 1.1 alm break;
649 1.1 alm }
650 1.1 alm p++;
651 1.1 alm }
652 1.1 alm }
653 1.1 alm
654 1.1 alm /*
655 1.1 alm * Compile a translation set of strings into a lookup table.
656 1.1 alm */
657 1.1 alm static char *
658 1.41 christos compile_tr(char *p, struct s_tr **py)
659 1.1 alm {
660 1.41 christos struct s_tr *y;
661 1.41 christos size_t i;
662 1.41 christos const char *op, *np;
663 1.41 christos char old[_POSIX2_LINE_MAX + 1];
664 1.41 christos char new[_POSIX2_LINE_MAX + 1];
665 1.41 christos size_t oclen, oldlen, nclen, newlen;
666 1.41 christos mbstate_t mbs1, mbs2;
667 1.41 christos
668 1.41 christos *py = y = xmalloc(sizeof(*y));
669 1.41 christos y->multis = NULL;
670 1.41 christos y->nmultis = 0;
671 1.1 alm
672 1.1 alm if (*p == '\0' || *p == '\\')
673 1.41 christos errx(1,
674 1.41 christos "%lu: %s: transform pattern can not be delimited by newline or backslash",
675 1.41 christos linenum, fname);
676 1.41 christos p = compile_delimited(p, old, 1);
677 1.41 christos if (p == NULL)
678 1.41 christos errx(1, "%lu: %s: unterminated transform source string",
679 1.41 christos linenum, fname);
680 1.41 christos p = compile_delimited(p - 1, new, 1);
681 1.41 christos if (p == NULL)
682 1.41 christos errx(1, "%lu: %s: unterminated transform target string",
683 1.41 christos linenum, fname);
684 1.1 alm EATSPACE();
685 1.41 christos op = old;
686 1.41 christos oldlen = mbsrtowcs(NULL, &op, 0, NULL);
687 1.41 christos if (oldlen == (size_t)-1)
688 1.41 christos err(1, NULL);
689 1.41 christos np = new;
690 1.41 christos newlen = mbsrtowcs(NULL, &np, 0, NULL);
691 1.41 christos if (newlen == (size_t)-1)
692 1.41 christos err(1, NULL);
693 1.41 christos if (newlen != oldlen)
694 1.41 christos errx(1, "%lu: %s: transform strings are not the same length",
695 1.41 christos linenum, fname);
696 1.41 christos if (MB_CUR_MAX == 1) {
697 1.41 christos /*
698 1.41 christos * The single-byte encoding case is easy: generate a
699 1.41 christos * lookup table.
700 1.41 christos */
701 1.41 christos for (i = 0; i <= UCHAR_MAX; i++)
702 1.41 christos y->bytetab[i] = (u_char)i;
703 1.41 christos for (; *op; op++, np++)
704 1.41 christos y->bytetab[(u_char)*op] = (u_char)*np;
705 1.41 christos } else {
706 1.41 christos /*
707 1.41 christos * Multi-byte encoding case: generate a lookup table as
708 1.41 christos * above, but only for single-byte characters. The first
709 1.41 christos * bytes of multi-byte characters have their lookup table
710 1.41 christos * entries set to 0, which causes do_tr() to search through
711 1.41 christos * an auxiliary vector of multi-byte mappings.
712 1.41 christos */
713 1.41 christos memset(&mbs1, 0, sizeof(mbs1));
714 1.41 christos memset(&mbs2, 0, sizeof(mbs2));
715 1.41 christos for (i = 0; i <= UCHAR_MAX; i++)
716 1.41 christos y->bytetab[i] = (u_char)((btowc((int)i) != WEOF) ? i : 0);
717 1.41 christos while (*op != '\0') {
718 1.41 christos oclen = mbrlen(op, MB_LEN_MAX, &mbs1);
719 1.41 christos if (oclen == (size_t)-1 || oclen == (size_t)-2)
720 1.41 christos errc(1, EILSEQ, NULL);
721 1.41 christos nclen = mbrlen(np, MB_LEN_MAX, &mbs2);
722 1.41 christos if (nclen == (size_t)-1 || nclen == (size_t)-2)
723 1.41 christos errc(1, EILSEQ, NULL);
724 1.41 christos if (oclen == 1 && nclen == 1)
725 1.41 christos y->bytetab[(u_char)*op] = (u_char)*np;
726 1.41 christos else {
727 1.41 christos y->bytetab[(u_char)*op] = 0;
728 1.41 christos y->multis = xrealloc(y->multis,
729 1.41 christos (y->nmultis + 1) * sizeof(*y->multis));
730 1.41 christos i = y->nmultis++;
731 1.41 christos y->multis[i].fromlen = oclen;
732 1.41 christos memcpy(y->multis[i].from, op, oclen);
733 1.41 christos y->multis[i].tolen = nclen;
734 1.41 christos memcpy(y->multis[i].to, np, nclen);
735 1.41 christos }
736 1.41 christos op += oclen;
737 1.41 christos np += nclen;
738 1.41 christos }
739 1.1 alm }
740 1.1 alm return (p);
741 1.1 alm }
742 1.1 alm
743 1.1 alm /*
744 1.41 christos * Compile the text following an a or i command.
745 1.1 alm */
746 1.1 alm static char *
747 1.24 wiz compile_text(void)
748 1.1 alm {
749 1.41 christos size_t asize, size;
750 1.41 christos int esc_nl;
751 1.41 christos char *text, *p, *op, *s;
752 1.41 christos char lbuf[_POSIX2_LINE_MAX + 1];
753 1.41 christos
754 1.41 christos asize = 2 * _POSIX2_LINE_MAX + 1;
755 1.41 christos text = xmalloc(asize);
756 1.41 christos size = 0;
757 1.41 christos while (cu_fgets(lbuf, sizeof(lbuf), NULL)) {
758 1.1 alm op = s = text + size;
759 1.41 christos p = lbuf;
760 1.41 christos EATSPACE();
761 1.41 christos for (esc_nl = 0; *p != '\0'; p++) {
762 1.41 christos if (*p == '\\' && p[1] != '\0' && *++p == '\n')
763 1.41 christos esc_nl = 1;
764 1.1 alm *s++ = *p;
765 1.1 alm }
766 1.41 christos size += (size_t)(s - op);
767 1.41 christos if (!esc_nl) {
768 1.1 alm *s = '\0';
769 1.1 alm break;
770 1.1 alm }
771 1.41 christos if (asize - size < _POSIX2_LINE_MAX + 1) {
772 1.41 christos asize *= 2;
773 1.41 christos text = xrealloc(text, asize);
774 1.41 christos }
775 1.1 alm }
776 1.41 christos text[size] = '\0';
777 1.41 christos p = xrealloc(text, size + 1);
778 1.41 christos return (p);
779 1.1 alm }
780 1.1 alm
781 1.1 alm /*
782 1.1 alm * Get an address and return a pointer to the first character after
783 1.1 alm * it. Fill the structure pointed to according to the address.
784 1.1 alm */
785 1.1 alm static char *
786 1.24 wiz compile_addr(char *p, struct s_addr *a)
787 1.1 alm {
788 1.41 christos char *end, re[_POSIX2_LINE_MAX + 1];
789 1.41 christos int icase;
790 1.1 alm
791 1.41 christos icase = 0;
792 1.41 christos
793 1.41 christos a->type = 0;
794 1.1 alm switch (*p) {
795 1.1 alm case '\\': /* Context address */
796 1.1 alm ++p;
797 1.1 alm /* FALLTHROUGH */
798 1.1 alm case '/': /* Context address */
799 1.41 christos p = compile_delimited(p, re, 0);
800 1.1 alm if (p == NULL)
801 1.41 christos errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
802 1.41 christos /* Check for case insensitive regexp flag */
803 1.41 christos if (*p == 'I') {
804 1.41 christos icase = 1;
805 1.41 christos p++;
806 1.41 christos }
807 1.41 christos if (*re == '\0')
808 1.41 christos a->u.r = NULL;
809 1.41 christos else
810 1.41 christos a->u.r = compile_re(re, icase);
811 1.1 alm a->type = AT_RE;
812 1.1 alm return (p);
813 1.1 alm
814 1.1 alm case '$': /* Last line */
815 1.1 alm a->type = AT_LAST;
816 1.1 alm return (p + 1);
817 1.41 christos
818 1.41 christos case '+': /* Relative line number */
819 1.41 christos a->type = AT_RELLINE;
820 1.41 christos p++;
821 1.41 christos /* FALLTHROUGH */
822 1.1 alm /* Line number */
823 1.41 christos case '0': case '1': case '2': case '3': case '4':
824 1.1 alm case '5': case '6': case '7': case '8': case '9':
825 1.41 christos if (a->type == 0)
826 1.41 christos a->type = AT_LINE;
827 1.41 christos a->u.l = strtoul(p, &end, 10);
828 1.1 alm return (end);
829 1.1 alm default:
830 1.41 christos errx(1, "%lu: %s: expected context address", linenum, fname);
831 1.1 alm return (NULL);
832 1.1 alm }
833 1.1 alm }
834 1.1 alm
835 1.1 alm /*
836 1.9 cgd * duptoeol --
837 1.9 cgd * Return a copy of all the characters up to \n or \0.
838 1.1 alm */
839 1.1 alm static char *
840 1.36 lukem duptoeol(char *s, const char *ctype)
841 1.1 alm {
842 1.1 alm size_t len;
843 1.9 cgd int ws;
844 1.41 christos char *p, *start;
845 1.1 alm
846 1.9 cgd ws = 0;
847 1.9 cgd for (start = s; *s != '\0' && *s != '\n'; ++s)
848 1.19 christos ws = isspace((unsigned char)*s);
849 1.1 alm *s = '\0';
850 1.9 cgd if (ws)
851 1.41 christos warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
852 1.41 christos len = (size_t)(s - start + 1);
853 1.41 christos p = xmalloc(len);
854 1.41 christos return (memmove(p, start, len));
855 1.1 alm }
856 1.1 alm
857 1.1 alm /*
858 1.9 cgd * Convert goto label names to addresses, and count a and r commands, in
859 1.9 cgd * the given subset of the script. Free the memory used by labels in b
860 1.9 cgd * and t commands (but not by :).
861 1.9 cgd *
862 1.1 alm * TODO: Remove } nodes
863 1.1 alm */
864 1.1 alm static void
865 1.24 wiz fixuplabel(struct s_command *cp, struct s_command *end)
866 1.1 alm {
867 1.1 alm
868 1.1 alm for (; cp != end; cp = cp->next)
869 1.1 alm switch (cp->code) {
870 1.1 alm case 'a':
871 1.1 alm case 'r':
872 1.1 alm appendnum++;
873 1.1 alm break;
874 1.1 alm case 'b':
875 1.1 alm case 't':
876 1.9 cgd /* Resolve branch target. */
877 1.1 alm if (cp->t == NULL) {
878 1.1 alm cp->u.c = NULL;
879 1.1 alm break;
880 1.1 alm }
881 1.9 cgd if ((cp->u.c = findlabel(cp->t)) == NULL)
882 1.41 christos errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
883 1.1 alm free(cp->t);
884 1.1 alm break;
885 1.1 alm case '{':
886 1.9 cgd /* Do interior commands. */
887 1.9 cgd fixuplabel(cp->u.c, cp->next);
888 1.1 alm break;
889 1.1 alm }
890 1.9 cgd }
891 1.9 cgd
892 1.9 cgd /*
893 1.9 cgd * Associate the given command label for later lookup.
894 1.9 cgd */
895 1.9 cgd static void
896 1.24 wiz enterlabel(struct s_command *cp)
897 1.9 cgd {
898 1.17 lukem struct labhash **lhp, *lh;
899 1.17 lukem u_char *p;
900 1.17 lukem u_int h, c;
901 1.9 cgd
902 1.9 cgd for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
903 1.9 cgd h = (h << 5) + h + c;
904 1.9 cgd lhp = &labels[h & LHMASK];
905 1.9 cgd for (lh = *lhp; lh != NULL; lh = lh->lh_next)
906 1.9 cgd if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
907 1.41 christos errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
908 1.9 cgd lh = xmalloc(sizeof *lh);
909 1.9 cgd lh->lh_next = *lhp;
910 1.9 cgd lh->lh_hash = h;
911 1.9 cgd lh->lh_cmd = cp;
912 1.9 cgd lh->lh_ref = 0;
913 1.9 cgd *lhp = lh;
914 1.9 cgd }
915 1.9 cgd
916 1.9 cgd /*
917 1.9 cgd * Find the label contained in the command l in the command linked
918 1.9 cgd * list cp. L is excluded from the search. Return NULL if not found.
919 1.9 cgd */
920 1.9 cgd static struct s_command *
921 1.24 wiz findlabel(char *name)
922 1.9 cgd {
923 1.17 lukem struct labhash *lh;
924 1.17 lukem u_char *p;
925 1.17 lukem u_int h, c;
926 1.9 cgd
927 1.9 cgd for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
928 1.9 cgd h = (h << 5) + h + c;
929 1.9 cgd for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
930 1.9 cgd if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
931 1.9 cgd lh->lh_ref = 1;
932 1.9 cgd return (lh->lh_cmd);
933 1.9 cgd }
934 1.9 cgd }
935 1.9 cgd return (NULL);
936 1.9 cgd }
937 1.9 cgd
938 1.41 christos /*
939 1.9 cgd * Warn about any unused labels. As a side effect, release the label hash
940 1.9 cgd * table space.
941 1.9 cgd */
942 1.9 cgd static void
943 1.24 wiz uselabel(void)
944 1.9 cgd {
945 1.17 lukem struct labhash *lh, *next;
946 1.17 lukem int i;
947 1.9 cgd
948 1.9 cgd for (i = 0; i < LHSZ; i++) {
949 1.9 cgd for (lh = labels[i]; lh != NULL; lh = next) {
950 1.9 cgd next = lh->lh_next;
951 1.9 cgd if (!lh->lh_ref)
952 1.41 christos warnx("%lu: %s: unused label '%s'",
953 1.41 christos linenum, fname, lh->lh_cmd->t);
954 1.9 cgd free(lh);
955 1.9 cgd }
956 1.9 cgd }
957 1.1 alm }
958 1.44 asau
959 1.44 asau /*
960 1.44 asau * Like fgets, but go through the chain of compilation units chaining them
961 1.44 asau * together. Empty strings and files are ignored.
962 1.44 asau */
963 1.44 asau char *
964 1.44 asau cu_fgets(char *buf, int n, int *more)
965 1.44 asau {
966 1.44 asau static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
967 1.44 asau static FILE *f; /* Current open file */
968 1.44 asau static char *s; /* Current pointer inside string */
969 1.44 asau static char string_ident[30];
970 1.44 asau char *p;
971 1.44 asau
972 1.44 asau again:
973 1.44 asau switch (state) {
974 1.44 asau case ST_EOF:
975 1.44 asau if (script == NULL) {
976 1.44 asau if (more != NULL)
977 1.44 asau *more = 0;
978 1.44 asau return (NULL);
979 1.44 asau }
980 1.44 asau linenum = 0;
981 1.44 asau switch (script->type) {
982 1.44 asau case CU_FILE:
983 1.44 asau if ((f = fopen(script->s, "r")) == NULL)
984 1.44 asau err(1, "%s", script->s);
985 1.44 asau fname = script->s;
986 1.44 asau state = ST_FILE;
987 1.44 asau goto again;
988 1.44 asau case CU_STRING:
989 1.44 asau if (((size_t)snprintf(string_ident,
990 1.44 asau sizeof(string_ident), "\"%s\"", script->s)) >=
991 1.44 asau sizeof(string_ident) - 1)
992 1.44 asau (void)strcpy(string_ident +
993 1.44 asau sizeof(string_ident) - 6, " ...\"");
994 1.44 asau fname = string_ident;
995 1.44 asau s = script->s;
996 1.44 asau state = ST_STRING;
997 1.44 asau goto again;
998 1.44 asau }
999 1.44 asau case ST_FILE:
1000 1.44 asau if ((p = fgets(buf, n, f)) != NULL) {
1001 1.44 asau linenum++;
1002 1.44 asau if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
1003 1.44 asau nflag = 1;
1004 1.44 asau if (more != NULL)
1005 1.44 asau *more = !feof(f);
1006 1.44 asau return (p);
1007 1.44 asau }
1008 1.44 asau script = script->next;
1009 1.44 asau (void)fclose(f);
1010 1.44 asau state = ST_EOF;
1011 1.44 asau goto again;
1012 1.44 asau case ST_STRING:
1013 1.44 asau if (linenum == 0 && s[0] == '#' && s[1] == 'n')
1014 1.44 asau nflag = 1;
1015 1.44 asau p = buf;
1016 1.44 asau for (;;) {
1017 1.44 asau if (n-- <= 1) {
1018 1.44 asau *p = '\0';
1019 1.44 asau linenum++;
1020 1.44 asau if (more != NULL)
1021 1.44 asau *more = 1;
1022 1.44 asau return (buf);
1023 1.44 asau }
1024 1.44 asau switch (*s) {
1025 1.44 asau case '\0':
1026 1.44 asau state = ST_EOF;
1027 1.44 asau if (s == script->s) {
1028 1.44 asau script = script->next;
1029 1.44 asau goto again;
1030 1.44 asau } else {
1031 1.44 asau script = script->next;
1032 1.44 asau *p = '\0';
1033 1.44 asau linenum++;
1034 1.44 asau if (more != NULL)
1035 1.44 asau *more = 0;
1036 1.44 asau return (buf);
1037 1.44 asau }
1038 1.44 asau case '\n':
1039 1.44 asau *p++ = '\n';
1040 1.44 asau *p = '\0';
1041 1.44 asau s++;
1042 1.44 asau linenum++;
1043 1.44 asau if (more != NULL)
1044 1.44 asau *more = 0;
1045 1.44 asau return (buf);
1046 1.44 asau default:
1047 1.44 asau *p++ = *s++;
1048 1.44 asau }
1049 1.44 asau }
1050 1.44 asau }
1051 1.44 asau /* NOTREACHED */
1052 1.44 asau return (NULL);
1053 1.44 asau }
1054