123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include "xregex.h"
- #if 0
- # define dbg(...) bb_error_msg(__VA_ARGS__)
- #else
- # define dbg(...) ((void)0)
- #endif
- enum {
- OPT_in_place = 1 << 0,
- };
- typedef struct sed_cmd_s {
-
- struct sed_cmd_s *next;
-
- regex_t *beg_match;
- regex_t *end_match;
- regex_t *sub_match;
- int beg_line;
- int beg_line_orig;
- int end_line;
- int end_line_orig;
- FILE *sw_file;
- char *string;
- unsigned which_match;
-
- unsigned invert:1;
- unsigned in_match:1;
- unsigned sub_p:1;
- char sw_last_char;
-
- char cmd;
- } sed_cmd_t;
- static const char semicolon_whitespace[] ALIGN1 = "; \n\r\t\v";
- struct globals {
-
- int be_quiet, regex_type;
- FILE *nonstdout;
- char *outname, *hold_space;
- smallint exitcode;
-
- int current_input_file, last_input_file;
- char **input_file_list;
- FILE *current_fp;
- regmatch_t regmatch[10];
- regex_t *previous_regex_ptr;
-
- sed_cmd_t *sed_cmd_head, **sed_cmd_tail;
-
- llist_t *append_head;
- char *add_cmd_line;
- struct pipeline {
- char *buf;
- int idx;
- int len;
- } pipeline;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
- G.sed_cmd_tail = &G.sed_cmd_head; \
- } while (0)
- #if ENABLE_FEATURE_CLEAN_UP
- static void sed_free_and_close_stuff(void)
- {
- sed_cmd_t *sed_cmd = G.sed_cmd_head;
- llist_free(G.append_head, free);
- while (sed_cmd) {
- sed_cmd_t *sed_cmd_next = sed_cmd->next;
- if (sed_cmd->sw_file)
- fclose(sed_cmd->sw_file);
- if (sed_cmd->beg_match) {
- regfree(sed_cmd->beg_match);
- free(sed_cmd->beg_match);
- }
- if (sed_cmd->end_match) {
- regfree(sed_cmd->end_match);
- free(sed_cmd->end_match);
- }
- if (sed_cmd->sub_match) {
- regfree(sed_cmd->sub_match);
- free(sed_cmd->sub_match);
- }
- free(sed_cmd->string);
- free(sed_cmd);
- sed_cmd = sed_cmd_next;
- }
- free(G.hold_space);
- if (G.current_fp)
- fclose(G.current_fp);
- }
- #else
- void sed_free_and_close_stuff(void);
- #endif
- static void cleanup_outname(void)
- {
- if (G.outname) unlink(G.outname);
- }
- static unsigned parse_escapes(char *dest, const char *string, int len, char from, char to)
- {
- char *d = dest;
- int i = 0;
- if (len == -1)
- len = strlen(string);
- while (i < len) {
- if (string[i] == '\\') {
- if (!to || string[i+1] == from) {
- if ((*d = to ? to : string[i+1]) == '\0')
- return d - dest;
- i += 2;
- d++;
- continue;
- }
- i++;
- *d++ = '\\';
-
- }
- if ((*d = string[i++]) == '\0')
- return d - dest;
- d++;
- }
- *d = '\0';
- return d - dest;
- }
- static char *copy_parsing_escapes(const char *string, int len)
- {
- const char *s;
- char *dest = xmalloc(len + 1);
-
-
- for (s = "\nn\tt\rr"; *s; s += 2) {
- len = parse_escapes(dest, string, len, s[1], s[0]);
- string = dest;
- }
- return dest;
- }
- static int index_of_next_unescaped_regexp_delim(int delimiter, const char *str)
- {
- int bracket = -1;
- int escaped = 0;
- int idx = 0;
- char ch;
- if (delimiter < 0) {
- bracket--;
- delimiter = -delimiter;
- }
- for (; (ch = str[idx]) != '\0'; idx++) {
- if (bracket >= 0) {
- if (ch == ']'
- && !(bracket == idx - 1 || (bracket == idx - 2 && str[idx - 1] == '^'))
- ) {
- bracket = -1;
- }
- } else if (escaped)
- escaped = 0;
- else if (ch == '\\')
- escaped = 1;
- else if (bracket == -1 && ch == '[')
- bracket = idx;
- else if (ch == delimiter)
- return idx;
- }
-
- bb_error_msg_and_die("unmatched '%c'", delimiter);
- }
- static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
- {
- const char *cmdstr_ptr = cmdstr;
- unsigned char delimiter;
- int idx = 0;
-
- if (*cmdstr == '\0')
- bb_error_msg_and_die("bad format in substitution expression");
- delimiter = *cmdstr_ptr++;
-
- idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
- *match = copy_parsing_escapes(cmdstr_ptr, idx);
-
- cmdstr_ptr += idx + 1;
- idx = index_of_next_unescaped_regexp_delim(- (int)delimiter, cmdstr_ptr);
- *replace = copy_parsing_escapes(cmdstr_ptr, idx);
- return ((cmdstr_ptr - cmdstr) + idx);
- }
- static int get_address(const char *my_str, int *linenum, regex_t ** regex)
- {
- const char *pos = my_str;
- if (isdigit(*my_str)) {
- *linenum = strtol(my_str, (char**)&pos, 10);
-
- } else if (*my_str == '$') {
- *linenum = -1;
- pos++;
- } else if (*my_str == '/' || *my_str == '\\') {
- int next;
- char delimiter;
- char *temp;
- delimiter = '/';
- if (*my_str == '\\')
- delimiter = *++pos;
- next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
- if (next != 0) {
- temp = copy_parsing_escapes(pos, next);
- G.previous_regex_ptr = *regex = xzalloc(sizeof(regex_t));
- xregcomp(*regex, temp, G.regex_type);
- free(temp);
- } else {
- *regex = G.previous_regex_ptr;
- if (!G.previous_regex_ptr)
- bb_error_msg_and_die("no previous regexp");
- }
-
- pos += (next+1);
- }
- return pos - my_str;
- }
- static int parse_file_cmd( const char *filecmdstr, char **retval)
- {
- int start = 0, idx, hack = 0;
-
- while (isspace(filecmdstr[start]))
- start++;
- idx = start;
- while (filecmdstr[idx] && filecmdstr[idx] != '\n')
- idx++;
-
- if (filecmdstr[idx] == '\n')
- hack = 1;
- if (idx == start)
- bb_error_msg_and_die("empty filename");
- *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
- if (hack)
- (*retval)[idx] = '\\';
- return idx;
- }
- static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
- {
- int cflags = G.regex_type;
- char *match;
- int idx;
-
- idx = parse_regex_delim(substr, &match, &sed_cmd->string);
-
-
-
- sed_cmd->which_match = 1;
- dbg("s flags:'%s'", substr + idx + 1);
- while (substr[++idx]) {
- dbg("s flag:'%c'", substr[idx]);
-
- if (isdigit(substr[idx])) {
- if (match[0] != '^') {
-
- const char *pos = substr + idx;
- sed_cmd->which_match = (unsigned)strtol(substr+idx, (char**) &pos, 10);
- idx = pos - substr - 1;
- }
- continue;
- }
-
- if (isspace(substr[idx]))
- continue;
- switch (substr[idx]) {
-
- case 'g':
- if (match[0] != '^')
- sed_cmd->which_match = 0;
- break;
-
- case 'p':
- sed_cmd->sub_p = 1;
- break;
-
- case 'w':
- {
- char *fname;
- idx += parse_file_cmd( substr+idx+1, &fname);
- sed_cmd->sw_file = xfopen_for_write(fname);
- sed_cmd->sw_last_char = '\n';
- free(fname);
- break;
- }
-
- case 'i':
- case 'I':
- cflags |= REG_ICASE;
- break;
-
- case '#':
-
- idx += strlen(substr + idx);
-
-
- case ';':
- case '}':
- goto out;
- default:
- dbg("s bad flags:'%s'", substr + idx);
- bb_error_msg_and_die("bad option in substitution expression");
- }
- }
- out:
-
- if (*match != '\0') {
-
- sed_cmd->sub_match = xzalloc(sizeof(regex_t));
- dbg("xregcomp('%s',%x)", match, cflags);
- xregcomp(sed_cmd->sub_match, match, cflags);
- dbg("regcomp ok");
- }
- free(match);
- return idx;
- }
- static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
- {
- static const char cmd_letters[] ALIGN1 = "saicrw:btTydDgGhHlnNpPqx={}";
- enum {
- IDX_s = 0,
- IDX_a,
- IDX_i,
- IDX_c,
- IDX_r,
- IDX_w,
- IDX_colon,
- IDX_b,
- IDX_t,
- IDX_T,
- IDX_y,
- IDX_d,
- IDX_D,
- IDX_g,
- IDX_G,
- IDX_h,
- IDX_H,
- IDX_l,
- IDX_n,
- IDX_N,
- IDX_p,
- IDX_P,
- IDX_q,
- IDX_x,
- IDX_equal,
- IDX_lbrace,
- IDX_rbrace,
- IDX_nul
- };
- unsigned idx;
- BUILD_BUG_ON(sizeof(cmd_letters)-1 != IDX_nul);
- idx = strchrnul(cmd_letters, sed_cmd->cmd) - cmd_letters;
-
- if (idx == IDX_s) {
- cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
- }
-
- else if (idx <= IDX_c) {
- unsigned len;
- if (idx < IDX_c) {
- if (sed_cmd->end_line || sed_cmd->end_match)
- bb_error_msg_and_die("command '%c' uses only one address", sed_cmd->cmd);
- }
- for (;;) {
- if (*cmdstr == '\n' || *cmdstr == '\\') {
- cmdstr++;
- break;
- }
- if (!isspace(*cmdstr))
- break;
- cmdstr++;
- }
- len = strlen(cmdstr);
- sed_cmd->string = copy_parsing_escapes(cmdstr, len);
- cmdstr += len;
-
- parse_escapes(sed_cmd->string, sed_cmd->string, -1, '\0', '\0');
- }
-
- else if (idx <= IDX_w) {
- if (idx < IDX_w) {
- if (sed_cmd->end_line || sed_cmd->end_match)
- bb_error_msg_and_die("command '%c' uses only one address", sed_cmd->cmd);
- }
- cmdstr += parse_file_cmd( cmdstr, &sed_cmd->string);
- if (sed_cmd->cmd == 'w') {
- sed_cmd->sw_file = xfopen_for_write(sed_cmd->string);
- sed_cmd->sw_last_char = '\n';
- }
- }
-
- else if (idx <= IDX_T) {
- int length;
- cmdstr = skip_whitespace(cmdstr);
- length = strcspn(cmdstr, semicolon_whitespace);
- if (length) {
- sed_cmd->string = xstrndup(cmdstr, length);
- cmdstr += length;
- }
- }
-
- else if (idx == IDX_y) {
- char *match, *replace;
- int i = cmdstr[0];
- cmdstr += parse_regex_delim(cmdstr, &match, &replace)+1;
-
- parse_escapes(match, match, -1, i, i);
- parse_escapes(replace, replace, -1, i, i);
- sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
- for (i = 0; match[i] && replace[i]; i++) {
- sed_cmd->string[i*2] = match[i];
- sed_cmd->string[i*2+1] = replace[i];
- }
- free(match);
- free(replace);
- }
-
- else if (idx >= IDX_nul) {
- bb_error_msg_and_die("unsupported command %c", sed_cmd->cmd);
- }
-
- return cmdstr;
- }
- static void add_cmd(const char *cmdstr)
- {
- sed_cmd_t *sed_cmd;
- unsigned len, n;
-
- if (G.add_cmd_line) {
- char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr);
- free(G.add_cmd_line);
- cmdstr = G.add_cmd_line = tp;
- }
-
- n = len = strlen(cmdstr);
- while (n && cmdstr[n-1] == '\\')
- n--;
- if ((len - n) & 1) {
- if (!G.add_cmd_line)
- G.add_cmd_line = xstrdup(cmdstr);
- G.add_cmd_line[len-1] = '\0';
- return;
- }
-
- while (*cmdstr) {
-
- cmdstr += strspn(cmdstr, semicolon_whitespace);
-
- if (!*cmdstr) break;
-
- if (*cmdstr == '#') {
-
- if (cmdstr[1] == 'n')
- G.be_quiet++;
- cmdstr = strpbrk(cmdstr, "\n\r");
- if (!cmdstr) break;
- continue;
- }
-
- sed_cmd = xzalloc(sizeof(sed_cmd_t));
-
- cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
- sed_cmd->beg_line_orig = sed_cmd->beg_line;
-
- if (*cmdstr == ',') {
- int idx;
- cmdstr++;
- if (*cmdstr == '+' && isdigit(cmdstr[1])) {
-
- char *end;
-
- idx = strtol(cmdstr+1, &end, 10);
- sed_cmd->end_line = -2 - idx;
- cmdstr = end;
- } else {
- idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
- cmdstr += idx;
- idx--;
- }
- if (idx < 0)
- bb_error_msg_and_die("no address after comma");
- sed_cmd->end_line_orig = sed_cmd->end_line;
- }
-
- cmdstr = skip_whitespace(cmdstr);
-
- if (*cmdstr == '!') {
- sed_cmd->invert = 1;
- cmdstr++;
-
- cmdstr = skip_whitespace(cmdstr);
- }
-
- if (!*cmdstr)
- bb_error_msg_and_die("missing command");
- sed_cmd->cmd = *cmdstr++;
- cmdstr = parse_cmd_args(sed_cmd, cmdstr);
-
-
- *G.sed_cmd_tail = sed_cmd;
- G.sed_cmd_tail = &sed_cmd->next;
- }
-
- free(G.add_cmd_line);
- G.add_cmd_line = NULL;
- }
- #define PIPE_GROW 64
- static void pipe_putc(char c)
- {
- if (G.pipeline.idx == G.pipeline.len) {
- G.pipeline.buf = xrealloc(G.pipeline.buf,
- G.pipeline.len + PIPE_GROW);
- G.pipeline.len += PIPE_GROW;
- }
- G.pipeline.buf[G.pipeline.idx++] = c;
- }
- static void do_subst_w_backrefs(char *line, char *replace)
- {
- int i, j;
-
- for (i = 0; replace[i]; i++) {
-
- if (replace[i] == '\\') {
- unsigned backref = replace[++i] - '0';
- if (backref <= 9) {
-
- if (G.regmatch[backref].rm_so != -1) {
- j = G.regmatch[backref].rm_so;
- while (j < G.regmatch[backref].rm_eo)
- pipe_putc(line[j++]);
- }
- continue;
- }
-
-
- pipe_putc(replace[i]);
- continue;
- }
-
- if (replace[i] == '&') {
- j = G.regmatch[0].rm_so;
- while (j < G.regmatch[0].rm_eo)
- pipe_putc(line[j++]);
- continue;
- }
-
- pipe_putc(replace[i]);
- }
- }
- static int do_subst_command(sed_cmd_t *sed_cmd, char **line_p)
- {
- char *line = *line_p;
- unsigned match_count = 0;
- bool altered = 0;
- bool prev_match_empty = 1;
- bool tried_at_eol = 0;
- regex_t *current_regex;
- current_regex = sed_cmd->sub_match;
-
- if (!current_regex) {
- current_regex = G.previous_regex_ptr;
- if (!current_regex)
- bb_error_msg_and_die("no previous regexp");
- }
- G.previous_regex_ptr = current_regex;
-
- dbg("matching '%s'", line);
- if (REG_NOMATCH == regexec(current_regex, line, 10, G.regmatch, 0)) {
- dbg("no match");
- return 0;
- }
- dbg("match");
-
- G.pipeline.buf = xmalloc(PIPE_GROW);
- G.pipeline.len = PIPE_GROW;
- G.pipeline.idx = 0;
-
- do {
- int start = G.regmatch[0].rm_so;
- int end = G.regmatch[0].rm_eo;
- int i;
- match_count++;
-
- if (sed_cmd->which_match
- && (sed_cmd->which_match != match_count)
- ) {
- for (i = 0; i < end; i++)
- pipe_putc(*line++);
-
- if (start == end && *line)
- pipe_putc(*line++);
- goto next;
- }
-
- for (i = 0; i < start; i++)
- pipe_putc(line[i]);
-
- if (prev_match_empty || start != 0 || start != end) {
-
- dbg("inserting replacement at %d in '%s'", start, line);
- do_subst_w_backrefs(line, sed_cmd->string);
-
- altered = 1;
- } else {
- dbg("NOT inserting replacement at %d in '%s'", start, line);
- }
-
- prev_match_empty = (start == end);
- if (prev_match_empty) {
- if (!line[end]) {
- tried_at_eol = 1;
- } else {
- pipe_putc(line[end]);
- end++;
- }
- }
-
- dbg("line += %d", end);
- line += end;
-
- if (sed_cmd->which_match != 0)
- break;
- next:
-
- if (*line == '\0') {
- if (tried_at_eol)
- break;
- tried_at_eol = 1;
- }
- } while (regexec(current_regex, line, 10, G.regmatch, REG_NOTBOL) != REG_NOMATCH);
-
- while (1) {
- char c = *line++;
- pipe_putc(c);
- if (c == '\0')
- break;
- }
- free(*line_p);
- *line_p = G.pipeline.buf;
- return altered;
- }
- static sed_cmd_t *branch_to(char *label)
- {
- sed_cmd_t *sed_cmd;
- for (sed_cmd = G.sed_cmd_head; sed_cmd; sed_cmd = sed_cmd->next) {
- if (sed_cmd->cmd == ':'
- && sed_cmd->string
- && strcmp(sed_cmd->string, label) == 0
- ) {
- return sed_cmd;
- }
- }
- bb_error_msg_and_die("can't find label for jump to '%s'", label);
- }
- static void append(char *s)
- {
- llist_add_to_end(&G.append_head, s);
- }
- enum {
- NO_EOL_CHAR = 1,
- LAST_IS_NUL = 2,
- };
- static void puts_maybe_newline(char *s, FILE *file, char *last_puts_char, char last_gets_char)
- {
- char lpc = *last_puts_char;
-
- if (lpc != '\n' && lpc != '\0') {
- fputc('\n', file);
- lpc = '\n';
- }
- fputs(s, file);
-
- if (s[0])
- lpc = 'x';
-
- if (last_gets_char == LAST_IS_NUL) {
- fputc('\0', file);
- lpc = 'x';
- } else
-
- if (last_gets_char != NO_EOL_CHAR) {
- fputc(last_gets_char, file);
- lpc = last_gets_char;
- }
- if (ferror(file)) {
- xfunc_error_retval = 4;
- bb_error_msg_and_die(bb_msg_write_error);
- }
- *last_puts_char = lpc;
- }
- static void flush_append(char *last_puts_char)
- {
- char *data;
-
- while ((data = (char *)llist_pop(&G.append_head)) != NULL) {
-
- puts_maybe_newline(data, G.nonstdout, last_puts_char, '\n');
- free(data);
- }
- }
- static char *get_next_line(char *gets_char, char *last_puts_char)
- {
- char *temp = NULL;
- int len;
- char gc;
- flush_append(last_puts_char);
-
- gc = NO_EOL_CHAR;
- for (; G.current_input_file <= G.last_input_file; G.current_input_file++) {
- FILE *fp = G.current_fp;
- if (!fp) {
- const char *path = G.input_file_list[G.current_input_file];
- fp = stdin;
- if (path != bb_msg_standard_input) {
- fp = fopen_or_warn(path, "r");
- if (!fp) {
- G.exitcode = EXIT_FAILURE;
- continue;
- }
- }
- G.current_fp = fp;
- }
-
- temp = bb_get_chunk_from_file(fp, &len);
- if (temp) {
-
- char c = temp[len-1];
- if (c == '\n' || c == '\0') {
- temp[len-1] = '\0';
- gc = c;
- if (c == '\0') {
- int ch = fgetc(fp);
- if (ch != EOF)
- ungetc(ch, fp);
- else
- gc = LAST_IS_NUL;
- }
- }
-
- break;
-
- }
-
- fclose_if_not_stdin(fp);
- G.current_fp = NULL;
- }
- *gets_char = gc;
- return temp;
- }
- #define sed_puts(s, n) (puts_maybe_newline(s, G.nonstdout, &last_puts_char, n))
- static int beg_match(sed_cmd_t *sed_cmd, const char *pattern_space)
- {
- int retval = sed_cmd->beg_match && !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0);
- if (retval)
- G.previous_regex_ptr = sed_cmd->beg_match;
- return retval;
- }
- static void process_files(void)
- {
- char *pattern_space, *next_line;
- int linenum = 0;
- char last_puts_char = '\n';
- char last_gets_char, next_gets_char;
- sed_cmd_t *sed_cmd;
- int substituted;
-
- next_line = get_next_line(&next_gets_char, &last_puts_char);
-
- again:
- substituted = 0;
-
- pattern_space = next_line;
- if (!pattern_space)
- return;
- last_gets_char = next_gets_char;
-
- next_line = get_next_line(&next_gets_char, &last_puts_char);
- linenum++;
-
- restart:
- for (sed_cmd = G.sed_cmd_head; sed_cmd; sed_cmd = sed_cmd->next) {
- int old_matched, matched;
- old_matched = sed_cmd->in_match;
-
- dbg("match1:%d", sed_cmd->in_match);
- dbg("match2:%d", (!sed_cmd->beg_line && !sed_cmd->end_line
- && !sed_cmd->beg_match && !sed_cmd->end_match));
- dbg("match3:%d", (sed_cmd->beg_line > 0
- && (sed_cmd->end_line || sed_cmd->end_match
- ? (sed_cmd->beg_line <= linenum)
- : (sed_cmd->beg_line == linenum)
- )
- ));
- dbg("match4:%d", (beg_match(sed_cmd, pattern_space)));
- dbg("match5:%d", (sed_cmd->beg_line == -1 && next_line == NULL));
-
- sed_cmd->in_match = sed_cmd->in_match
-
- || (!sed_cmd->beg_line && !sed_cmd->end_line
- && !sed_cmd->beg_match && !sed_cmd->end_match)
-
- || (sed_cmd->beg_line > 0
- && (sed_cmd->end_line || sed_cmd->end_match
-
- ? (sed_cmd->beg_line <= linenum)
- : (sed_cmd->beg_line == linenum)
- )
- )
-
- || (beg_match(sed_cmd, pattern_space))
-
- || (sed_cmd->beg_line == -1 && next_line == NULL);
-
- matched = sed_cmd->in_match;
- dbg("cmd:'%c' matched:%d beg_line:%d end_line:%d linenum:%d",
- sed_cmd->cmd, matched, sed_cmd->beg_line, sed_cmd->end_line, linenum);
-
- if (matched) {
- if (sed_cmd->end_line <= -2) {
-
- sed_cmd->end_line = linenum + (-sed_cmd->end_line - 2);
- }
-
- if (sed_cmd->beg_line > 0) {
- sed_cmd->beg_line = -2;
- }
- dbg("end1:%d", sed_cmd->end_line ? sed_cmd->end_line == -1
- ? !next_line : (sed_cmd->end_line <= linenum)
- : !sed_cmd->end_match);
- dbg("end2:%d", sed_cmd->end_match && old_matched
- && !regexec(sed_cmd->end_match,pattern_space, 0, NULL, 0));
- sed_cmd->in_match = !(
-
- (sed_cmd->end_line
- ? sed_cmd->end_line == -1
- ? !next_line
- : (sed_cmd->end_line <= linenum)
- : !sed_cmd->end_match
- )
-
- || (sed_cmd->end_match && old_matched
- && (regexec(sed_cmd->end_match,
- pattern_space, 0, NULL, 0) == 0)
- )
- );
- }
-
- if (sed_cmd->cmd == '{') {
- if (sed_cmd->invert ? matched : !matched) {
- unsigned nest_cnt = 0;
- while (1) {
- if (sed_cmd->cmd == '{')
- nest_cnt++;
- if (sed_cmd->cmd == '}') {
- nest_cnt--;
- if (nest_cnt == 0)
- break;
- }
- sed_cmd = sed_cmd->next;
- if (!sed_cmd)
- bb_error_msg_and_die("unterminated {");
- }
- }
- continue;
- }
-
- if (sed_cmd->invert ? matched : !matched)
- continue;
-
- if (sed_cmd->beg_match) {
- G.previous_regex_ptr = sed_cmd->beg_match;
- }
-
- dbg("pattern_space:'%s' next_line:'%s' cmd:%c",
- pattern_space, next_line, sed_cmd->cmd);
- switch (sed_cmd->cmd) {
-
- case '=':
- fprintf(G.nonstdout, "%d\n", linenum);
- break;
-
- case 'P':
- {
- char *tmp = strchr(pattern_space, '\n');
- if (tmp) {
- *tmp = '\0';
-
- sed_puts(pattern_space, '\n');
- *tmp = '\n';
- break;
- }
-
- }
-
- case 'p':
-
- sed_puts(pattern_space, '\n');
- break;
-
- case 'D':
- {
- char *tmp = strchr(pattern_space, '\n');
- if (tmp) {
- overlapping_strcpy(pattern_space, tmp + 1);
- goto restart;
- }
- }
-
- case 'd':
- goto discard_line;
-
- case 's':
- if (!do_subst_command(sed_cmd, &pattern_space))
- break;
- dbg("do_subst_command succeeded:'%s'", pattern_space);
- substituted |= 1;
-
- if (sed_cmd->sub_p)
- sed_puts(pattern_space, last_gets_char);
-
- if (sed_cmd->sw_file)
- puts_maybe_newline(
- pattern_space, sed_cmd->sw_file,
- &sed_cmd->sw_last_char, last_gets_char);
- break;
-
- case 'a':
- append(xstrdup(sed_cmd->string));
- break;
-
- case 'i':
- sed_puts(sed_cmd->string, '\n');
- break;
-
- case 'c':
-
- if (!sed_cmd->in_match)
- sed_puts(sed_cmd->string, '\n');
- goto discard_line;
-
- case 'r':
- {
- FILE *rfile;
- rfile = fopen_for_read(sed_cmd->string);
- if (rfile) {
- char *line;
- while ((line = xmalloc_fgetline(rfile))
- != NULL)
- append(line);
- fclose(rfile);
- }
- break;
- }
-
- case 'w':
- puts_maybe_newline(
- pattern_space, sed_cmd->sw_file,
- &sed_cmd->sw_last_char, last_gets_char);
- break;
-
- case 'n':
- if (!G.be_quiet)
- sed_puts(pattern_space, last_gets_char);
- if (next_line == NULL) {
-
- goto discard_line;
- }
- free(pattern_space);
- pattern_space = next_line;
- last_gets_char = next_gets_char;
- next_line = get_next_line(&next_gets_char, &last_puts_char);
- substituted = 0;
- linenum++;
- break;
-
- case 'q':
-
- free(next_line);
- next_line = NULL;
- goto discard_commands;
-
- case 'N':
- {
- int len;
-
-
- if (next_line == NULL) {
-
- goto discard_commands;
- }
-
- len = strlen(pattern_space);
- pattern_space = xrealloc(pattern_space, len + strlen(next_line) + 2);
- pattern_space[len] = '\n';
- strcpy(pattern_space + len+1, next_line);
- last_gets_char = next_gets_char;
- next_line = get_next_line(&next_gets_char, &last_puts_char);
- linenum++;
- break;
- }
-
- case 't':
- if (!substituted) break;
- substituted = 0;
-
-
- case 'T':
- if (substituted) break;
-
-
- case 'b':
- if (!sed_cmd->string) goto discard_commands;
- else sed_cmd = branch_to(sed_cmd->string);
- break;
-
- case 'y':
- {
- int i, j;
- for (i = 0; pattern_space[i]; i++) {
- for (j = 0; sed_cmd->string[j]; j += 2) {
- if (pattern_space[i] == sed_cmd->string[j]) {
- pattern_space[i] = sed_cmd->string[j + 1];
- break;
- }
- }
- }
- break;
- }
- case 'g':
- free(pattern_space);
- pattern_space = xstrdup(G.hold_space ? G.hold_space : "");
- break;
- case 'G':
- {
- int pattern_space_size = 2;
- int hold_space_size = 0;
- if (pattern_space)
- pattern_space_size += strlen(pattern_space);
- if (G.hold_space)
- hold_space_size = strlen(G.hold_space);
- pattern_space = xrealloc(pattern_space,
- pattern_space_size + hold_space_size);
- if (pattern_space_size == 2)
- pattern_space[0] = 0;
- strcat(pattern_space, "\n");
- if (G.hold_space)
- strcat(pattern_space, G.hold_space);
- last_gets_char = '\n';
- break;
- }
- case 'h':
- free(G.hold_space);
- G.hold_space = xstrdup(pattern_space);
- break;
- case 'H':
- {
- int hold_space_size = 2;
- int pattern_space_size = 0;
- if (G.hold_space)
- hold_space_size += strlen(G.hold_space);
- if (pattern_space)
- pattern_space_size = strlen(pattern_space);
- G.hold_space = xrealloc(G.hold_space,
- hold_space_size + pattern_space_size);
- if (hold_space_size == 2)
- *G.hold_space = 0;
- strcat(G.hold_space, "\n");
- if (pattern_space)
- strcat(G.hold_space, pattern_space);
- break;
- }
- case 'x':
- {
- char *tmp = pattern_space;
- pattern_space = G.hold_space ? G.hold_space : xzalloc(1);
- last_gets_char = '\n';
- G.hold_space = tmp;
- break;
- }
- }
- }
-
- discard_commands:
-
- if (!G.be_quiet)
- sed_puts(pattern_space, last_gets_char);
-
- discard_line:
- flush_append(&last_puts_char );
- free(pattern_space);
- goto again;
- }
- static void add_cmd_block(char *cmdstr)
- {
- char *sv, *eol;
- cmdstr = sv = xstrdup(cmdstr);
- do {
- eol = strchr(cmdstr, '\n');
- if (eol)
- *eol = '\0';
- add_cmd(cmdstr);
- cmdstr = eol + 1;
- } while (eol);
- free(sv);
- }
- int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int sed_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned opt;
- llist_t *opt_e, *opt_f;
- char *opt_i;
- #if ENABLE_LONG_OPTS
- static const char sed_longopts[] ALIGN1 =
-
- "in-place\0" Optional_argument "i"
- "regexp-extended\0" No_argument "r"
- "quiet\0" No_argument "n"
- "silent\0" No_argument "n"
- "expression\0" Required_argument "e"
- "file\0" Required_argument "f";
- #endif
- INIT_G();
-
- if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
-
- if (argv[1] && strcmp(argv[1], "--version") == 0) {
- puts("This is not GNU sed version 4.0");
- return 0;
- }
-
- opt_e = opt_f = NULL;
- opt_i = NULL;
-
-
- opt = getopt32long(argv, "^"
- "i::rEne:*f:*"
- "\0" "nn",
- sed_longopts,
- &opt_i, &opt_e, &opt_f,
- &G.be_quiet);
-
- argv += optind;
- if (opt & OPT_in_place) {
- die_func = cleanup_outname;
- }
- if (opt & (2|4))
- G.regex_type |= REG_EXTENDED;
-
-
- while (opt_e) {
- add_cmd_block(llist_pop(&opt_e));
- }
- while (opt_f) {
- char *line;
- FILE *cmdfile;
- cmdfile = xfopen_stdin(llist_pop(&opt_f));
- while ((line = xmalloc_fgetline(cmdfile)) != NULL) {
- add_cmd(line);
- free(line);
- }
- fclose_if_not_stdin(cmdfile);
- }
-
- if (!(opt & 0x30)) {
- if (!*argv)
- bb_show_usage();
- add_cmd_block(*argv++);
- }
-
- add_cmd("");
-
- G.nonstdout = stdout;
-
- G.input_file_list = argv;
- if (!argv[0]) {
- if (opt & OPT_in_place)
- bb_error_msg_and_die(bb_msg_requires_arg, "-i");
- argv[0] = (char*)bb_msg_standard_input;
-
- } else {
- goto start;
- for (; *argv; argv++) {
- struct stat statbuf;
- int nonstdoutfd;
- sed_cmd_t *sed_cmd;
- G.last_input_file++;
- start:
- if (!(opt & OPT_in_place)) {
- if (LONE_DASH(*argv)) {
- *argv = (char*)bb_msg_standard_input;
- process_files();
- }
- continue;
- }
-
- if (stat(*argv, &statbuf) != 0) {
- bb_simple_perror_msg(*argv);
- G.exitcode = EXIT_FAILURE;
- G.current_input_file++;
- continue;
- }
- G.outname = xasprintf("%sXXXXXX", *argv);
- nonstdoutfd = xmkstemp(G.outname);
- G.nonstdout = xfdopen_for_write(nonstdoutfd);
-
-
- fchmod(nonstdoutfd, statbuf.st_mode);
- fchown(nonstdoutfd, statbuf.st_uid, statbuf.st_gid);
- process_files();
- fclose(G.nonstdout);
- G.nonstdout = stdout;
- if (opt_i) {
- char *backupname = xasprintf("%s%s", *argv, opt_i);
- xrename(*argv, backupname);
- free(backupname);
- }
-
- xrename(G.outname, *argv);
- free(G.outname);
- G.outname = NULL;
-
- for (sed_cmd = G.sed_cmd_head; sed_cmd; sed_cmd = sed_cmd->next) {
- sed_cmd->beg_line = sed_cmd->beg_line_orig;
- sed_cmd->end_line = sed_cmd->end_line_orig;
- }
- }
-
- }
- process_files();
- return G.exitcode;
- }
|