sed: deal with peculiar behavior of '2d;2,1p' in GNU sed
[oweals/busybox.git] / editors / sed.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * sed.c - very minimalist version of sed
4  *
5  * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
6  * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
7  * Copyright (C) 2002  Matt Kraai
8  * Copyright (C) 2003 by Glenn McGrath
9  * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net>
10  *
11  * MAINTAINER: Rob Landley <rob@landley.net>
12  *
13  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
14  */
15
16 /* Code overview.
17
18   Files are laid out to avoid unnecessary function declarations.  So for
19   example, every function add_cmd calls occurs before add_cmd in this file.
20
21   add_cmd() is called on each line of sed command text (from a file or from
22   the command line).  It calls get_address() and parse_cmd_args().  The
23   resulting sed_cmd_t structures are appended to a linked list
24   (G.sed_cmd_head/G.sed_cmd_tail).
25
26   add_input_file() adds a FILE* to the list of input files.  We need to
27   know all input sources ahead of time to find the last line for the $ match.
28
29   process_files() does actual sedding, reading data lines from each input FILE *
30   (which could be stdin) and applying the sed command list (sed_cmd_head) to
31   each of the resulting lines.
32
33   sed_main() is where external code calls into this, with a command line.
34 */
35
36
37 /*
38         Supported features and commands in this version of sed:
39
40          - comments ('#')
41          - address matching: num|/matchstr/[,num|/matchstr/|$]command
42          - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
43          - edit commands: (a)ppend, (i)nsert, (c)hange
44          - file commands: (r)ead
45          - backreferences in substitution expressions (\0, \1, \2...\9)
46          - grouped commands: {cmd1;cmd2}
47          - transliteration (y/source-chars/dest-chars/)
48          - pattern space hold space storing / swapping (g, h, x)
49          - labels / branching (: label, b, t, T)
50
51          (Note: Specifying an address (range) to match is *optional*; commands
52          default to the whole pattern space if no specific address match was
53          requested.)
54
55         Todo:
56          - Create a wrapper around regex to make libc's regex conform with sed
57
58         Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
59 */
60
61 #include "libbb.h"
62 #include "xregex.h"
63
64 /* Each sed command turns into one of these structures. */
65 typedef struct sed_cmd_s {
66         /* Ordered by alignment requirements: currently 36 bytes on x86 */
67         struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
68
69         /* address storage */
70         regex_t *beg_match;     /* sed -e '/match/cmd' */
71         regex_t *end_match;     /* sed -e '/match/,/end_match/cmd' */
72         regex_t *sub_match;     /* For 's/sub_match/string/' */
73         int beg_line;           /* 'sed 1p'   0 == apply commands to all lines */
74         int end_line;           /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
75
76         FILE *sw_file;          /* File (sw) command writes to, -1 for none. */
77         char *string;           /* Data string for (saicytb) commands. */
78
79         unsigned which_match;   /* (s) Which match to replace (0 for all) */
80
81         /* Bitfields (gcc won't group them if we don't) */
82         unsigned invert:1;      /* the '!' after the address */
83         unsigned in_match:1;    /* Next line also included in match? */
84         unsigned sub_p:1;       /* (s) print option */
85
86         char sw_last_char;      /* Last line written by (sw) had no '\n' */
87
88         /* GENERAL FIELDS */
89         char cmd;               /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
90 } sed_cmd_t;
91
92 static const char semicolon_whitespace[] ALIGN1 = "; \n\r\t\v";
93
94 struct globals {
95         /* options */
96         int be_quiet, regex_type;
97         FILE *nonstdout;
98         char *outname, *hold_space;
99
100         /* List of input files */
101         int input_file_count, current_input_file;
102         FILE **input_file_list;
103
104         regmatch_t regmatch[10];
105         regex_t *previous_regex_ptr;
106
107         /* linked list of sed commands */
108         sed_cmd_t sed_cmd_head, *sed_cmd_tail;
109
110         /* Linked list of append lines */
111         llist_t *append_head;
112
113         char *add_cmd_line;
114
115         struct pipeline {
116                 char *buf;      /* Space to hold string */
117                 int idx;        /* Space used */
118                 int len;        /* Space allocated */
119         } pipeline;
120 };
121 #define G (*(struct globals*)&bb_common_bufsiz1)
122 void BUG_sed_globals_too_big(void);
123 #define INIT_G() do { \
124         if (sizeof(struct globals) > COMMON_BUFSIZE) \
125                 BUG_sed_globals_too_big(); \
126         G.sed_cmd_tail = &G.sed_cmd_head; \
127 } while (0)
128
129
130 #if ENABLE_FEATURE_CLEAN_UP
131 static void sed_free_and_close_stuff(void)
132 {
133         sed_cmd_t *sed_cmd = G.sed_cmd_head.next;
134
135         llist_free(G.append_head, free);
136
137         while (sed_cmd) {
138                 sed_cmd_t *sed_cmd_next = sed_cmd->next;
139
140                 if (sed_cmd->sw_file)
141                         xprint_and_close_file(sed_cmd->sw_file);
142
143                 if (sed_cmd->beg_match) {
144                         regfree(sed_cmd->beg_match);
145                         free(sed_cmd->beg_match);
146                 }
147                 if (sed_cmd->end_match) {
148                         regfree(sed_cmd->end_match);
149                         free(sed_cmd->end_match);
150                 }
151                 if (sed_cmd->sub_match) {
152                         regfree(sed_cmd->sub_match);
153                         free(sed_cmd->sub_match);
154                 }
155                 free(sed_cmd->string);
156                 free(sed_cmd);
157                 sed_cmd = sed_cmd_next;
158         }
159
160         free(G.hold_space);
161
162         while (G.current_input_file < G.input_file_count)
163                 fclose(G.input_file_list[G.current_input_file++]);
164 }
165 #else
166 void sed_free_and_close_stuff(void);
167 #endif
168
169 /* If something bad happens during -i operation, delete temp file */
170
171 static void cleanup_outname(void)
172 {
173         if (G.outname) unlink(G.outname);
174 }
175
176 /* strcpy, replacing "\from" with 'to'. If to is NUL, replacing "\any" with 'any' */
177
178 static void parse_escapes(char *dest, const char *string, int len, char from, char to)
179 {
180         int i = 0;
181
182         while (i < len) {
183                 if (string[i] == '\\') {
184                         if (!to || string[i+1] == from) {
185                                 *dest++ = to ? to : string[i+1];
186                                 i += 2;
187                                 continue;
188                         }
189                         *dest++ = string[i++];
190                 }
191                 /* TODO: is it safe wrt a string with trailing '\\' ? */
192                 *dest++ = string[i++];
193         }
194         *dest = '\0';
195 }
196
197 static char *copy_parsing_escapes(const char *string, int len)
198 {
199         char *dest = xmalloc(len + 1);
200
201         parse_escapes(dest, string, len, 'n', '\n');
202         /* GNU sed also recognizes \t */
203         parse_escapes(dest, dest, strlen(dest), 't', '\t');
204         return dest;
205 }
206
207
208 /*
209  * index_of_next_unescaped_regexp_delim - walks left to right through a string
210  * beginning at a specified index and returns the index of the next regular
211  * expression delimiter (typically a forward slash ('/')) not preceded by
212  * a backslash ('\').  A negative delimiter disables square bracket checking.
213  */
214 static int index_of_next_unescaped_regexp_delim(int delimiter, const char *str)
215 {
216         int bracket = -1;
217         int escaped = 0;
218         int idx = 0;
219         char ch;
220
221         if (delimiter < 0) {
222                 bracket--;
223                 delimiter = -delimiter;
224         }
225
226         for (; (ch = str[idx]); idx++) {
227                 if (bracket >= 0) {
228                         if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
229                                         && str[idx - 1] == '^')))
230                                 bracket = -1;
231                 } else if (escaped)
232                         escaped = 0;
233                 else if (ch == '\\')
234                         escaped = 1;
235                 else if (bracket == -1 && ch == '[')
236                         bracket = idx;
237                 else if (ch == delimiter)
238                         return idx;
239         }
240
241         /* if we make it to here, we've hit the end of the string */
242         bb_error_msg_and_die("unmatched '%c'", delimiter);
243 }
244
245 /*
246  *  Returns the index of the third delimiter
247  */
248 static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
249 {
250         const char *cmdstr_ptr = cmdstr;
251         char delimiter;
252         int idx = 0;
253
254         /* verify that the 's' or 'y' is followed by something.  That something
255          * (typically a 'slash') is now our regexp delimiter... */
256         if (*cmdstr == '\0')
257                 bb_error_msg_and_die("bad format in substitution expression");
258         delimiter = *cmdstr_ptr++;
259
260         /* save the match string */
261         idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
262         *match = copy_parsing_escapes(cmdstr_ptr, idx);
263
264         /* save the replacement string */
265         cmdstr_ptr += idx + 1;
266         idx = index_of_next_unescaped_regexp_delim(-delimiter, cmdstr_ptr);
267         *replace = copy_parsing_escapes(cmdstr_ptr, idx);
268
269         return ((cmdstr_ptr - cmdstr) + idx);
270 }
271
272 /*
273  * returns the index in the string just past where the address ends.
274  */
275 static int get_address(const char *my_str, int *linenum, regex_t ** regex)
276 {
277         const char *pos = my_str;
278
279         if (isdigit(*my_str)) {
280                 *linenum = strtol(my_str, (char**)&pos, 10);
281                 /* endstr shouldnt ever equal NULL */
282         } else if (*my_str == '$') {
283                 *linenum = -1;
284                 pos++;
285         } else if (*my_str == '/' || *my_str == '\\') {
286                 int next;
287                 char delimiter;
288                 char *temp;
289
290                 delimiter = '/';
291                 if (*my_str == '\\') delimiter = *++pos;
292                 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
293                 temp = copy_parsing_escapes(pos, next);
294                 *regex = xmalloc(sizeof(regex_t));
295                 xregcomp(*regex, temp, G.regex_type|REG_NEWLINE);
296                 free(temp);
297                 /* Move position to next character after last delimiter */
298                 pos += (next+1);
299         }
300         return pos - my_str;
301 }
302
303 /* Grab a filename.  Whitespace at start is skipped, then goes to EOL. */
304 static int parse_file_cmd(/*sed_cmd_t *sed_cmd,*/ const char *filecmdstr, char **retval)
305 {
306         int start = 0, idx, hack = 0;
307
308         /* Skip whitespace, then grab filename to end of line */
309         while (isspace(filecmdstr[start]))
310                 start++;
311         idx = start;
312         while (filecmdstr[idx] && filecmdstr[idx] != '\n')
313                 idx++;
314
315         /* If lines glued together, put backslash back. */
316         if (filecmdstr[idx] == '\n')
317                 hack = 1;
318         if (idx == start)
319                 bb_error_msg_and_die("empty filename");
320         *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
321         if (hack)
322                 (*retval)[idx] = '\\';
323
324         return idx;
325 }
326
327 static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
328 {
329         int cflags = G.regex_type;
330         char *match;
331         int idx;
332
333         /*
334          * A substitution command should look something like this:
335          *    s/match/replace/ #gIpw
336          *    ||     |        |||
337          *    mandatory       optional
338          */
339         idx = parse_regex_delim(substr, &match, &sed_cmd->string);
340
341         /* determine the number of back references in the match string */
342         /* Note: we compute this here rather than in the do_subst_command()
343          * function to save processor time, at the expense of a little more memory
344          * (4 bits) per sed_cmd */
345
346         /* process the flags */
347
348         sed_cmd->which_match = 1;
349         while (substr[++idx]) {
350                 /* Parse match number */
351                 if (isdigit(substr[idx])) {
352                         if (match[0] != '^') {
353                                 /* Match 0 treated as all, multiple matches we take the last one. */
354                                 const char *pos = substr + idx;
355 /* FIXME: error check? */
356                                 sed_cmd->which_match = (unsigned)strtol(substr+idx, (char**) &pos, 10);
357                                 idx = pos - substr;
358                         }
359                         continue;
360                 }
361                 /* Skip spaces */
362                 if (isspace(substr[idx])) continue;
363
364                 switch (substr[idx]) {
365                 /* Replace all occurrences */
366                 case 'g':
367                         if (match[0] != '^')
368                                 sed_cmd->which_match = 0;
369                         break;
370                 /* Print pattern space */
371                 case 'p':
372                         sed_cmd->sub_p = 1;
373                         break;
374                 /* Write to file */
375                 case 'w':
376                 {
377                         char *temp;
378                         idx += parse_file_cmd(/*sed_cmd,*/ substr+idx, &temp);
379                         break;
380                 }
381                 /* Ignore case (gnu exension) */
382                 case 'I':
383                         cflags |= REG_ICASE;
384                         break;
385                 /* Comment */
386                 case '#':
387                         while (substr[++idx]) /*skip all*/;
388                         /* Fall through */
389                 /* End of command */
390                 case ';':
391                 case '}':
392                         goto out;
393                 default:
394                         bb_error_msg_and_die("bad option in substitution expression");
395                 }
396         }
397 out:
398         /* compile the match string into a regex */
399         if (*match != '\0') {
400                 /* If match is empty, we use last regex used at runtime */
401                 sed_cmd->sub_match = xmalloc(sizeof(regex_t));
402                 xregcomp(sed_cmd->sub_match, match, cflags);
403         }
404         free(match);
405
406         return idx;
407 }
408
409 /*
410  *  Process the commands arguments
411  */
412 static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
413 {
414         /* handle (s)ubstitution command */
415         if (sed_cmd->cmd == 's')
416                 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
417         /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
418         else if (strchr("aic", sed_cmd->cmd)) {
419                 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
420                         bb_error_msg_and_die
421                                 ("only a beginning address can be specified for edit commands");
422                 for (;;) {
423                         if (*cmdstr == '\n' || *cmdstr == '\\') {
424                                 cmdstr++;
425                                 break;
426                         } else if (isspace(*cmdstr))
427                                 cmdstr++;
428                         else
429                                 break;
430                 }
431                 sed_cmd->string = xstrdup(cmdstr);
432                 /* "\anychar" -> "anychar" */
433                 parse_escapes(sed_cmd->string, sed_cmd->string, strlen(cmdstr), '\0', '\0');
434                 cmdstr += strlen(cmdstr);
435         /* handle file cmds: (r)ead */
436         } else if (strchr("rw", sed_cmd->cmd)) {
437                 if (sed_cmd->end_line || sed_cmd->end_match)
438                         bb_error_msg_and_die("command only uses one address");
439                 cmdstr += parse_file_cmd(/*sed_cmd,*/ cmdstr, &sed_cmd->string);
440                 if (sed_cmd->cmd == 'w') {
441                         sed_cmd->sw_file = xfopen_for_write(sed_cmd->string);
442                         sed_cmd->sw_last_char = '\n';
443                 }
444         /* handle branch commands */
445         } else if (strchr(":btT", sed_cmd->cmd)) {
446                 int length;
447
448                 cmdstr = skip_whitespace(cmdstr);
449                 length = strcspn(cmdstr, semicolon_whitespace);
450                 if (length) {
451                         sed_cmd->string = xstrndup(cmdstr, length);
452                         cmdstr += length;
453                 }
454         }
455         /* translation command */
456         else if (sed_cmd->cmd == 'y') {
457                 char *match, *replace;
458                 int i = cmdstr[0];
459
460                 cmdstr += parse_regex_delim(cmdstr, &match, &replace)+1;
461                 /* \n already parsed, but \delimiter needs unescaping. */
462                 parse_escapes(match, match, strlen(match), i, i);
463                 parse_escapes(replace, replace, strlen(replace), i, i);
464
465                 sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
466                 for (i = 0; match[i] && replace[i]; i++) {
467                         sed_cmd->string[i*2] = match[i];
468                         sed_cmd->string[i*2+1] = replace[i];
469                 }
470                 free(match);
471                 free(replace);
472         }
473         /* if it wasnt a single-letter command that takes no arguments
474          * then it must be an invalid command.
475          */
476         else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
477                 bb_error_msg_and_die("unsupported command %c", sed_cmd->cmd);
478         }
479
480         /* give back whatever's left over */
481         return cmdstr;
482 }
483
484
485 /* Parse address+command sets, skipping comment lines. */
486
487 static void add_cmd(const char *cmdstr)
488 {
489         sed_cmd_t *sed_cmd;
490         int temp;
491
492         /* Append this line to any unfinished line from last time. */
493         if (G.add_cmd_line) {
494                 char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr);
495                 free(G.add_cmd_line);
496                 cmdstr = G.add_cmd_line = tp;
497         }
498
499         /* If this line ends with backslash, request next line. */
500         temp = strlen(cmdstr);
501         if (temp && cmdstr[--temp] == '\\') {
502                 if (!G.add_cmd_line)
503                         G.add_cmd_line = xstrdup(cmdstr);
504                 G.add_cmd_line[temp] = '\0';
505                 return;
506         }
507
508         /* Loop parsing all commands in this line. */
509         while (*cmdstr) {
510                 /* Skip leading whitespace and semicolons */
511                 cmdstr += strspn(cmdstr, semicolon_whitespace);
512
513                 /* If no more commands, exit. */
514                 if (!*cmdstr) break;
515
516                 /* if this is a comment, jump past it and keep going */
517                 if (*cmdstr == '#') {
518                         /* "#n" is the same as using -n on the command line */
519                         if (cmdstr[1] == 'n')
520                                 G.be_quiet++;
521                         cmdstr = strpbrk(cmdstr, "\n\r");
522                         if (!cmdstr) break;
523                         continue;
524                 }
525
526                 /* parse the command
527                  * format is: [addr][,addr][!]cmd
528                  *            |----||-----||-|
529                  *            part1 part2  part3
530                  */
531
532                 sed_cmd = xzalloc(sizeof(sed_cmd_t));
533
534                 /* first part (if present) is an address: either a '$', a number or a /regex/ */
535                 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
536
537                 /* second part (if present) will begin with a comma */
538                 if (*cmdstr == ',') {
539                         int idx;
540
541                         cmdstr++;
542                         idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
543                         if (!idx)
544                                 bb_error_msg_and_die("no address after comma");
545                         cmdstr += idx;
546                 }
547
548                 /* skip whitespace before the command */
549                 cmdstr = skip_whitespace(cmdstr);
550
551                 /* Check for inversion flag */
552                 if (*cmdstr == '!') {
553                         sed_cmd->invert = 1;
554                         cmdstr++;
555
556                         /* skip whitespace before the command */
557                         cmdstr = skip_whitespace(cmdstr);
558                 }
559
560                 /* last part (mandatory) will be a command */
561                 if (!*cmdstr)
562                         bb_error_msg_and_die("missing command");
563                 sed_cmd->cmd = *(cmdstr++);
564                 cmdstr = parse_cmd_args(sed_cmd, cmdstr);
565
566                 /* Add the command to the command array */
567                 G.sed_cmd_tail->next = sed_cmd;
568                 G.sed_cmd_tail = G.sed_cmd_tail->next;
569         }
570
571         /* If we glued multiple lines together, free the memory. */
572         free(G.add_cmd_line);
573         G.add_cmd_line = NULL;
574 }
575
576 /* Append to a string, reallocating memory as necessary. */
577
578 #define PIPE_GROW 64
579
580 static void pipe_putc(char c)
581 {
582         if (G.pipeline.idx == G.pipeline.len) {
583                 G.pipeline.buf = xrealloc(G.pipeline.buf,
584                                 G.pipeline.len + PIPE_GROW);
585                 G.pipeline.len += PIPE_GROW;
586         }
587         G.pipeline.buf[G.pipeline.idx++] = c;
588 }
589
590 static void do_subst_w_backrefs(char *line, char *replace)
591 {
592         int i,j;
593
594         /* go through the replacement string */
595         for (i = 0; replace[i]; i++) {
596                 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
597                 if (replace[i] == '\\') {
598                         unsigned backref = replace[++i] - '0';
599                         if (backref <= 9) {
600                                 /* print out the text held in G.regmatch[backref] */
601                                 if (G.regmatch[backref].rm_so != -1) {
602                                         j = G.regmatch[backref].rm_so;
603                                         while (j < G.regmatch[backref].rm_eo)
604                                                 pipe_putc(line[j++]);
605                                 }
606                                 continue;
607                         }
608                         /* I _think_ it is impossible to get '\' to be
609                          * the last char in replace string. Thus we dont check
610                          * for replace[i] == NUL. (counterexample anyone?) */
611                         /* if we find a backslash escaped character, print the character */
612                         pipe_putc(replace[i]);
613                         continue;
614                 }
615                 /* if we find an unescaped '&' print out the whole matched text. */
616                 if (replace[i] == '&') {
617                         j = G.regmatch[0].rm_so;
618                         while (j < G.regmatch[0].rm_eo)
619                                 pipe_putc(line[j++]);
620                         continue;
621                 }
622                 /* Otherwise just output the character. */
623                 pipe_putc(replace[i]);
624         }
625 }
626
627 static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
628 {
629         char *oldline = *line;
630         int altered = 0;
631         unsigned match_count = 0;
632         regex_t *current_regex;
633
634         /* Handle empty regex. */
635         if (sed_cmd->sub_match == NULL) {
636                 current_regex = G.previous_regex_ptr;
637                 if (!current_regex)
638                         bb_error_msg_and_die("no previous regexp");
639         } else
640                 G.previous_regex_ptr = current_regex = sed_cmd->sub_match;
641
642         /* Find the first match */
643         if (REG_NOMATCH == regexec(current_regex, oldline, 10, G.regmatch, 0))
644                 return 0;
645
646         /* Initialize temporary output buffer. */
647         G.pipeline.buf = xmalloc(PIPE_GROW);
648         G.pipeline.len = PIPE_GROW;
649         G.pipeline.idx = 0;
650
651         /* Now loop through, substituting for matches */
652         do {
653                 int i;
654
655                 /* Work around bug in glibc regexec, demonstrated by:
656                    echo " a.b" | busybox sed 's [^ .]* x g'
657                    The match_count check is so not to break
658                    echo "hi" | busybox sed 's/^/!/g' */
659                 if (!G.regmatch[0].rm_so && !G.regmatch[0].rm_eo && match_count) {
660                         pipe_putc(*oldline++);
661                         continue;
662                 }
663
664                 match_count++;
665
666                 /* If we aren't interested in this match, output old line to
667                    end of match and continue */
668                 if (sed_cmd->which_match
669                  && (sed_cmd->which_match != match_count)
670                 ) {
671                         for (i = 0; i < G.regmatch[0].rm_eo; i++)
672                                 pipe_putc(*oldline++);
673                         continue;
674                 }
675
676                 /* print everything before the match */
677                 for (i = 0; i < G.regmatch[0].rm_so; i++)
678                         pipe_putc(oldline[i]);
679
680                 /* then print the substitution string */
681                 do_subst_w_backrefs(oldline, sed_cmd->string);
682
683                 /* advance past the match */
684                 oldline += G.regmatch[0].rm_eo;
685                 /* flag that something has changed */
686                 altered++;
687
688                 /* if we're not doing this globally, get out now */
689                 if (sed_cmd->which_match)
690                         break;
691         } while (*oldline && (regexec(current_regex, oldline, 10, G.regmatch, 0) != REG_NOMATCH));
692
693         /* Copy rest of string into output pipeline */
694
695         while (*oldline)
696                 pipe_putc(*oldline++);
697         pipe_putc(0);
698
699         free(*line);
700         *line = G.pipeline.buf;
701         return altered;
702 }
703
704 /* Set command pointer to point to this label.  (Does not handle null label.) */
705 static sed_cmd_t *branch_to(char *label)
706 {
707         sed_cmd_t *sed_cmd;
708
709         for (sed_cmd = G.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
710                 if (sed_cmd->cmd == ':' && sed_cmd->string && !strcmp(sed_cmd->string, label)) {
711                         return sed_cmd;
712                 }
713         }
714         bb_error_msg_and_die("can't find label for jump to '%s'", label);
715 }
716
717 static void append(char *s)
718 {
719         llist_add_to_end(&G.append_head, xstrdup(s));
720 }
721
722 static void flush_append(void)
723 {
724         char *data;
725
726         /* Output appended lines. */
727         while ((data = (char *)llist_pop(&G.append_head))) {
728                 fprintf(G.nonstdout, "%s\n", data);
729                 free(data);
730         }
731 }
732
733 static void add_input_file(FILE *file)
734 {
735         G.input_file_list = xrealloc_vector(G.input_file_list, 2, G.input_file_count);
736         G.input_file_list[G.input_file_count++] = file;
737 }
738
739 /* Get next line of input from G.input_file_list, flushing append buffer and
740  * noting if we ran out of files without a newline on the last line we read.
741  */
742 enum {
743         NO_EOL_CHAR = 1,
744         LAST_IS_NUL = 2,
745 };
746 static char *get_next_line(char *gets_char)
747 {
748         char *temp = NULL;
749         int len;
750         char gc;
751
752         flush_append();
753
754         /* will be returned if last line in the file
755          * doesn't end with either '\n' or '\0' */
756         gc = NO_EOL_CHAR;
757         while (G.current_input_file < G.input_file_count) {
758                 FILE *fp = G.input_file_list[G.current_input_file];
759                 /* Read line up to a newline or NUL byte, inclusive,
760                  * return malloc'ed char[]. length of the chunk read
761                  * is stored in len. NULL if EOF/error */
762                 temp = bb_get_chunk_from_file(fp, &len);
763                 if (temp) {
764                         /* len > 0 here, it's ok to do temp[len-1] */
765                         char c = temp[len-1];
766                         if (c == '\n' || c == '\0') {
767                                 temp[len-1] = '\0';
768                                 gc = c;
769                                 if (c == '\0') {
770                                         int ch = fgetc(fp);
771                                         if (ch != EOF)
772                                                 ungetc(ch, fp);
773                                         else
774                                                 gc = LAST_IS_NUL;
775                                 }
776                         }
777                         /* else we put NO_EOL_CHAR into *gets_char */
778                         break;
779
780                 /* NB: I had the idea of peeking next file(s) and returning
781                  * NO_EOL_CHAR only if it is the *last* non-empty
782                  * input file. But there is a case where this won't work:
783                  * file1: "a woo\nb woo"
784                  * file2: "c no\nd no"
785                  * sed -ne 's/woo/bang/p' input1 input2 => "a bang\nb bang"
786                  * (note: *no* newline after "b bang"!) */
787                 }
788                 /* Close this file and advance to next one */
789                 fclose(fp);
790                 G.current_input_file++;
791         }
792         *gets_char = gc;
793         return temp;
794 }
795
796 /* Output line of text. */
797 /* Note:
798  * The tricks with NO_EOL_CHAR and last_puts_char are there to emulate gnu sed.
799  * Without them, we had this:
800  * echo -n thingy >z1
801  * echo -n again >z2
802  * >znull
803  * sed "s/i/z/" z1 z2 znull | hexdump -vC
804  * output:
805  * gnu sed 4.1.5:
806  * 00000000  74 68 7a 6e 67 79 0a 61  67 61 7a 6e              |thzngy.agazn|
807  * bbox:
808  * 00000000  74 68 7a 6e 67 79 61 67  61 7a 6e                 |thzngyagazn|
809  */
810 static void puts_maybe_newline(char *s, FILE *file, char *last_puts_char, char last_gets_char)
811 {
812         char lpc = *last_puts_char;
813
814         /* Need to insert a '\n' between two files because first file's
815          * last line wasn't terminated? */
816         if (lpc != '\n' && lpc != '\0') {
817                 fputc('\n', file);
818                 lpc = '\n';
819         }
820         fputs(s, file);
821
822         /* 'x' - just something which is not '\n', '\0' or NO_EOL_CHAR */
823         if (s[0])
824                 lpc = 'x';
825
826         /* had trailing '\0' and it was last char of file? */
827         if (last_gets_char == LAST_IS_NUL) {
828                 fputc('\0', file);
829                 lpc = 'x'; /* */
830         } else
831         /* had trailing '\n' or '\0'? */
832         if (last_gets_char != NO_EOL_CHAR) {
833                 fputc(last_gets_char, file);
834                 lpc = last_gets_char;
835         }
836
837         if (ferror(file)) {
838                 xfunc_error_retval = 4;  /* It's what gnu sed exits with... */
839                 bb_error_msg_and_die(bb_msg_write_error);
840         }
841         *last_puts_char = lpc;
842 }
843
844 #define sed_puts(s, n) (puts_maybe_newline(s, G.nonstdout, &last_puts_char, n))
845
846 static int beg_match(sed_cmd_t *sed_cmd, const char *pattern_space)
847 {
848         int retval = sed_cmd->beg_match && !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0);
849         if (retval)
850                 G.previous_regex_ptr = sed_cmd->beg_match;
851         return retval;
852 }
853
854 /* Process all the lines in all the files */
855
856 static void process_files(void)
857 {
858         char *pattern_space, *next_line;
859         int linenum = 0;
860         char last_puts_char = '\n';
861         char last_gets_char, next_gets_char;
862         sed_cmd_t *sed_cmd;
863         int substituted;
864
865         /* Prime the pump */
866         next_line = get_next_line(&next_gets_char);
867
868         /* Go through every line in each file */
869  again:
870         substituted = 0;
871
872         /* Advance to next line.  Stop if out of lines. */
873         pattern_space = next_line;
874         if (!pattern_space)
875                 return;
876         last_gets_char = next_gets_char;
877
878         /* Read one line in advance so we can act on the last line,
879          * the '$' address */
880         next_line = get_next_line(&next_gets_char);
881         linenum++;
882
883         /* For every line, go through all the commands */
884  restart:
885         for (sed_cmd = G.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
886                 int old_matched, matched;
887
888                 old_matched = sed_cmd->in_match;
889
890                 /* Determine if this command matches this line: */
891                         /* Are we continuing a previous multi-line match? */
892                 sed_cmd->in_match = sed_cmd->in_match
893                         /* Or is no range necessary? */
894                         || (!sed_cmd->beg_line && !sed_cmd->end_line
895                                 && !sed_cmd->beg_match && !sed_cmd->end_match)
896                         /* Or did we match the start of a numerical range? */
897                         || (sed_cmd->beg_line > 0
898                             && (sed_cmd->beg_line == linenum
899                                /* GNU sed compat:
900                                 * "shadowed beginning" case: "1d;1,ENDp" - p still matches at line 2
901                                 * even though 1d skipped line 1 which is a start line for p */
902                                || (sed_cmd->beg_line < linenum && (sed_cmd->end_line > 0 || sed_cmd->end_match))
903                                )
904                         )
905                         /* Or does this line match our begin address regex? */
906                         || (beg_match(sed_cmd, pattern_space))
907                         /* Or did we match last line of input? */
908                         || (sed_cmd->beg_line == -1 && next_line == NULL);
909
910                 matched = sed_cmd->in_match;
911
912                 //bb_error_msg("cmd:'%c' matched:%d beg_line:%d end_line:%d linenum:%d",
913                 //sed_cmd->cmd, matched, sed_cmd->beg_line, sed_cmd->end_line, linenum);
914
915                 /* Is this line the end of the current match? */
916                 if (matched) {
917                         int n = (
918                                 /* has the ending line come, or is this a single address command? */
919                                 sed_cmd->end_line ?
920                                         sed_cmd->end_line == -1 ?
921                                                 !next_line
922                                                 : (sed_cmd->end_line <= linenum)
923                                         : !sed_cmd->end_match
924                                 );
925                         if (!n) {
926                                 /* or does this line matches our last address regex */
927                                 n = (sed_cmd->end_match
928                                      && old_matched
929                                      && (regexec(sed_cmd->end_match,
930                                                  pattern_space, 0, NULL, 0) == 0)
931                                 );
932                         }
933                         if (n && sed_cmd->beg_line > 0) {
934                                 /* once matched, "n,xxx" range is dead, disabling it */
935                                 sed_cmd->beg_line = -2;
936                         }
937                         sed_cmd->in_match = !n;
938                 }
939
940                 /* Skip blocks of commands we didn't match */
941                 if (sed_cmd->cmd == '{') {
942                         if (sed_cmd->invert ? matched : !matched) {
943                                 while (sed_cmd->cmd != '}') {
944                                         sed_cmd = sed_cmd->next;
945                                         if (!sed_cmd)
946                                                 bb_error_msg_and_die("unterminated {");
947                                 }
948                         }
949                         continue;
950                 }
951
952                 /* Okay, so did this line match? */
953                 if (sed_cmd->invert ? matched : !matched)
954                         continue; /* no */
955
956                 /* Update last used regex in case a blank substitute BRE is found */
957                 if (sed_cmd->beg_match) {
958                         G.previous_regex_ptr = sed_cmd->beg_match;
959                 }
960
961                 /* actual sedding */
962                 switch (sed_cmd->cmd) {
963
964                 /* Print line number */
965                 case '=':
966                         fprintf(G.nonstdout, "%d\n", linenum);
967                         break;
968
969                 /* Write the current pattern space up to the first newline */
970                 case 'P':
971                 {
972                         char *tmp = strchr(pattern_space, '\n');
973
974                         if (tmp) {
975                                 *tmp = '\0';
976                                 /* TODO: explain why '\n' below */
977                                 sed_puts(pattern_space, '\n');
978                                 *tmp = '\n';
979                                 break;
980                         }
981                         /* Fall Through */
982                 }
983
984                 /* Write the current pattern space to output */
985                 case 'p':
986                         /* NB: we print this _before_ the last line
987                          * (of current file) is printed. Even if
988                          * that line is nonterminated, we print
989                          * '\n' here (gnu sed does the same) */
990                         sed_puts(pattern_space, '\n');
991                         break;
992                 /* Delete up through first newline */
993                 case 'D':
994                 {
995                         char *tmp = strchr(pattern_space, '\n');
996
997                         if (tmp) {
998                                 tmp = xstrdup(tmp+1);
999                                 free(pattern_space);
1000                                 pattern_space = tmp;
1001                                 goto restart;
1002                         }
1003                 }
1004                 /* discard this line. */
1005                 case 'd':
1006                         goto discard_line;
1007
1008                 /* Substitute with regex */
1009                 case 's':
1010                         if (!do_subst_command(sed_cmd, &pattern_space))
1011                                 break;
1012                         substituted |= 1;
1013
1014                         /* handle p option */
1015                         if (sed_cmd->sub_p)
1016                                 sed_puts(pattern_space, last_gets_char);
1017                         /* handle w option */
1018                         if (sed_cmd->sw_file)
1019                                 puts_maybe_newline(
1020                                         pattern_space, sed_cmd->sw_file,
1021                                         &sed_cmd->sw_last_char, last_gets_char);
1022                         break;
1023
1024                 /* Append line to linked list to be printed later */
1025                 case 'a':
1026                         append(sed_cmd->string);
1027                         break;
1028
1029                 /* Insert text before this line */
1030                 case 'i':
1031                         sed_puts(sed_cmd->string, '\n');
1032                         break;
1033
1034                 /* Cut and paste text (replace) */
1035                 case 'c':
1036                         /* Only triggers on last line of a matching range. */
1037                         if (!sed_cmd->in_match)
1038                                 sed_puts(sed_cmd->string, NO_EOL_CHAR);
1039                         goto discard_line;
1040
1041                 /* Read file, append contents to output */
1042                 case 'r':
1043                 {
1044                         FILE *rfile;
1045
1046                         rfile = fopen_for_read(sed_cmd->string);
1047                         if (rfile) {
1048                                 char *line;
1049
1050                                 while ((line = xmalloc_fgetline(rfile))
1051                                                 != NULL)
1052                                         append(line);
1053                                 xprint_and_close_file(rfile);
1054                         }
1055
1056                         break;
1057                 }
1058
1059                 /* Write pattern space to file. */
1060                 case 'w':
1061                         puts_maybe_newline(
1062                                 pattern_space, sed_cmd->sw_file,
1063                                 &sed_cmd->sw_last_char, last_gets_char);
1064                         break;
1065
1066                 /* Read next line from input */
1067                 case 'n':
1068                         if (!G.be_quiet)
1069                                 sed_puts(pattern_space, last_gets_char);
1070                         if (next_line) {
1071                                 free(pattern_space);
1072                                 pattern_space = next_line;
1073                                 last_gets_char = next_gets_char;
1074                                 next_line = get_next_line(&next_gets_char);
1075                                 substituted = 0;
1076                                 linenum++;
1077                                 break;
1078                         }
1079                         /* fall through */
1080
1081                 /* Quit.  End of script, end of input. */
1082                 case 'q':
1083                         /* Exit the outer while loop */
1084                         free(next_line);
1085                         next_line = NULL;
1086                         goto discard_commands;
1087
1088                 /* Append the next line to the current line */
1089                 case 'N':
1090                 {
1091                         int len;
1092                         /* If no next line, jump to end of script and exit. */
1093                         if (next_line == NULL) {
1094                                 /* Jump to end of script and exit */
1095                                 free(next_line);
1096                                 next_line = NULL;
1097                                 goto discard_line;
1098                         /* append next_line, read new next_line. */
1099                         }
1100                         len = strlen(pattern_space);
1101                         pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
1102                         pattern_space[len] = '\n';
1103                         strcpy(pattern_space + len+1, next_line);
1104                         last_gets_char = next_gets_char;
1105                         next_line = get_next_line(&next_gets_char);
1106                         linenum++;
1107                         break;
1108                 }
1109
1110                 /* Test/branch if substitution occurred */
1111                 case 't':
1112                         if (!substituted) break;
1113                         substituted = 0;
1114                         /* Fall through */
1115                 /* Test/branch if substitution didn't occur */
1116                 case 'T':
1117                         if (substituted) break;
1118                         /* Fall through */
1119                 /* Branch to label */
1120                 case 'b':
1121                         if (!sed_cmd->string) goto discard_commands;
1122                         else sed_cmd = branch_to(sed_cmd->string);
1123                         break;
1124                 /* Transliterate characters */
1125                 case 'y':
1126                 {
1127                         int i, j;
1128
1129                         for (i = 0; pattern_space[i]; i++) {
1130                                 for (j = 0; sed_cmd->string[j]; j += 2) {
1131                                         if (pattern_space[i] == sed_cmd->string[j]) {
1132                                                 pattern_space[i] = sed_cmd->string[j + 1];
1133                                                 break;
1134                                         }
1135                                 }
1136                         }
1137
1138                         break;
1139                 }
1140                 case 'g':       /* Replace pattern space with hold space */
1141                         free(pattern_space);
1142                         pattern_space = xstrdup(G.hold_space ? G.hold_space : "");
1143                         break;
1144                 case 'G':       /* Append newline and hold space to pattern space */
1145                 {
1146                         int pattern_space_size = 2;
1147                         int hold_space_size = 0;
1148
1149                         if (pattern_space)
1150                                 pattern_space_size += strlen(pattern_space);
1151                         if (G.hold_space)
1152                                 hold_space_size = strlen(G.hold_space);
1153                         pattern_space = xrealloc(pattern_space,
1154                                         pattern_space_size + hold_space_size);
1155                         if (pattern_space_size == 2)
1156                                 pattern_space[0] = 0;
1157                         strcat(pattern_space, "\n");
1158                         if (G.hold_space)
1159                                 strcat(pattern_space, G.hold_space);
1160                         last_gets_char = '\n';
1161
1162                         break;
1163                 }
1164                 case 'h':       /* Replace hold space with pattern space */
1165                         free(G.hold_space);
1166                         G.hold_space = xstrdup(pattern_space);
1167                         break;
1168                 case 'H':       /* Append newline and pattern space to hold space */
1169                 {
1170                         int hold_space_size = 2;
1171                         int pattern_space_size = 0;
1172
1173                         if (G.hold_space)
1174                                 hold_space_size += strlen(G.hold_space);
1175                         if (pattern_space)
1176                                 pattern_space_size = strlen(pattern_space);
1177                         G.hold_space = xrealloc(G.hold_space,
1178                                         hold_space_size + pattern_space_size);
1179
1180                         if (hold_space_size == 2)
1181                                 *G.hold_space = 0;
1182                         strcat(G.hold_space, "\n");
1183                         if (pattern_space)
1184                                 strcat(G.hold_space, pattern_space);
1185
1186                         break;
1187                 }
1188                 case 'x': /* Exchange hold and pattern space */
1189                 {
1190                         char *tmp = pattern_space;
1191                         pattern_space = G.hold_space ? : xzalloc(1);
1192                         last_gets_char = '\n';
1193                         G.hold_space = tmp;
1194                         break;
1195                 }
1196                 } /* switch */
1197         } /* for each cmd */
1198
1199         /*
1200          * Exit point from sedding...
1201          */
1202  discard_commands:
1203         /* we will print the line unless we were told to be quiet ('-n')
1204            or if the line was suppressed (ala 'd'elete) */
1205         if (!G.be_quiet)
1206                 sed_puts(pattern_space, last_gets_char);
1207
1208         /* Delete and such jump here. */
1209  discard_line:
1210         flush_append();
1211         free(pattern_space);
1212
1213         goto again;
1214 }
1215
1216 /* It is possible to have a command line argument with embedded
1217  * newlines.  This counts as multiple command lines.
1218  * However, newline can be escaped: 's/e/z\<newline>z/'
1219  * We check for this.
1220  */
1221
1222 static void add_cmd_block(char *cmdstr)
1223 {
1224         char *sv, *eol;
1225
1226         cmdstr = sv = xstrdup(cmdstr);
1227         do {
1228                 eol = strchr(cmdstr, '\n');
1229  next:
1230                 if (eol) {
1231                         /* Count preceding slashes */
1232                         int slashes = 0;
1233                         char *sl = eol;
1234
1235                         while (sl != cmdstr && *--sl == '\\')
1236                                 slashes++;
1237                         /* Odd number of preceding slashes - newline is escaped */
1238                         if (slashes & 1) {
1239                                 overlapping_strcpy(eol - 1, eol);
1240                                 eol = strchr(eol, '\n');
1241                                 goto next;
1242                         }
1243                         *eol = '\0';
1244                 }
1245                 add_cmd(cmdstr);
1246                 cmdstr = eol + 1;
1247         } while (eol);
1248         free(sv);
1249 }
1250
1251 int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1252 int sed_main(int argc UNUSED_PARAM, char **argv)
1253 {
1254         enum {
1255                 OPT_in_place = 1 << 0,
1256         };
1257         unsigned opt;
1258         llist_t *opt_e, *opt_f;
1259         int status = EXIT_SUCCESS;
1260
1261         INIT_G();
1262
1263         /* destroy command strings on exit */
1264         if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
1265
1266         /* Lie to autoconf when it starts asking stupid questions. */
1267         if (argv[1] && !strcmp(argv[1], "--version")) {
1268                 puts("This is not GNU sed version 4.0");
1269                 return 0;
1270         }
1271
1272         /* do normal option parsing */
1273         opt_e = opt_f = NULL;
1274         opt_complementary = "e::f::" /* can occur multiple times */
1275                             "nn"; /* count -n */
1276         opt = getopt32(argv, "irne:f:", &opt_e, &opt_f,
1277                             &G.be_quiet); /* counter for -n */
1278         //argc -= optind;
1279         argv += optind;
1280         if (opt & OPT_in_place) { // -i
1281                 atexit(cleanup_outname);
1282         }
1283         if (opt & 0x2) G.regex_type |= REG_EXTENDED; // -r
1284         //if (opt & 0x4) G.be_quiet++; // -n
1285         while (opt_e) { // -e
1286                 add_cmd_block(llist_pop(&opt_e));
1287         }
1288         while (opt_f) { // -f
1289                 char *line;
1290                 FILE *cmdfile;
1291                 cmdfile = xfopen_for_read(llist_pop(&opt_f));
1292                 while ((line = xmalloc_fgetline(cmdfile)) != NULL) {
1293                         add_cmd(line);
1294                         free(line);
1295                 }
1296                 fclose(cmdfile);
1297         }
1298         /* if we didn't get a pattern from -e or -f, use argv[0] */
1299         if (!(opt & 0x18)) {
1300                 if (!*argv)
1301                         bb_show_usage();
1302                 add_cmd_block(*argv++);
1303         }
1304         /* Flush any unfinished commands. */
1305         add_cmd("");
1306
1307         /* By default, we write to stdout */
1308         G.nonstdout = stdout;
1309
1310         /* argv[0..(argc-1)] should be names of file to process. If no
1311          * files were specified or '-' was specified, take input from stdin.
1312          * Otherwise, we process all the files specified. */
1313         if (argv[0] == NULL) {
1314                 if (opt & OPT_in_place)
1315                         bb_error_msg_and_die(bb_msg_requires_arg, "-i");
1316                 add_input_file(stdin);
1317                 process_files();
1318         } else {
1319                 int i;
1320                 FILE *file;
1321
1322                 for (i = 0; argv[i]; i++) {
1323                         struct stat statbuf;
1324                         int nonstdoutfd;
1325
1326                         if (LONE_DASH(argv[i]) && !(opt & OPT_in_place)) {
1327                                 add_input_file(stdin);
1328                                 process_files();
1329                                 continue;
1330                         }
1331                         file = fopen_or_warn(argv[i], "r");
1332                         if (!file) {
1333                                 status = EXIT_FAILURE;
1334                                 continue;
1335                         }
1336                         if (!(opt & OPT_in_place)) {
1337                                 add_input_file(file);
1338                                 continue;
1339                         }
1340
1341                         G.outname = xasprintf("%sXXXXXX", argv[i]);
1342                         nonstdoutfd = mkstemp(G.outname);
1343                         if (-1 == nonstdoutfd)
1344                                 bb_perror_msg_and_die("cannot create temp file %s", G.outname);
1345                         G.nonstdout = fdopen(nonstdoutfd, "w");
1346
1347                         /* Set permissions of output file */
1348
1349                         fstat(fileno(file), &statbuf);
1350                         fchmod(nonstdoutfd, statbuf.st_mode);
1351                         add_input_file(file);
1352                         process_files();
1353                         fclose(G.nonstdout);
1354
1355                         G.nonstdout = stdout;
1356                         /* unlink(argv[i]); */
1357                         xrename(G.outname, argv[i]);
1358                         free(G.outname);
1359                         G.outname = NULL;
1360                 }
1361                 if (G.input_file_count > G.current_input_file)
1362                         process_files();
1363         }
1364
1365         return status;
1366 }