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