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