/* search_and_replace: for each line in a range, change text matching a pattern according to a substitution template; return status */ int search_and_replace(pattern_t *pat, int gflag, int kth) { undo_t *up; const char *txt; const char *eot; long lc; long xa = current_addr; int nsubs = 0; line_t *lp; int len; current_addr = first_addr - 1; for (lc = 0; lc <= second_addr - first_addr; lc++) { lp = get_addressed_line_node(++current_addr); if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0) return ERR; else if (len) { up = NULL; if (delete_lines(current_addr, current_addr) < 0) return ERR; txt = rbuf; eot = rbuf + len; SPL1(); do { if ((txt = put_sbuf_line(txt)) == NULL) { SPL0(); return ERR; } else if (up) up->t = get_addressed_line_node(current_addr); else if ((up = push_undo_stack(UADD, current_addr, current_addr)) == NULL) { SPL0(); return ERR; } } while (txt != eot); SPL0(); nsubs++; xa = current_addr; } } current_addr = xa; if (nsubs == 0 && !(gflag & GLB)) { errmsg = "no match"; return ERR; } else if ((gflag & (GPR | GLS | GNP)) && display_lines(current_addr, current_addr, gflag) < 0) return ERR; return 0; }
/* read_stream: read a stream into the editor buffer; return status */ int read_stream(FILE *fp, int n) { line_t *lp = get_addressed_line_node(n); undo_t *up = NULL; unsigned int size = 0; int o_newline_added = newline_added; int o_isbinary = isbinary; int appended = (n == addr_last); int len; isbinary = newline_added = 0; #ifdef DES if (des) init_des_cipher(); #endif for (current_addr = n; (len = get_stream_line(fp)) > 0; size += len) { SPL1(); if (put_sbuf_line(sbuf) == NULL) { SPL0(); return ERR; } lp = lp->q_forw; if (up) up->t = lp; else if ((up = push_undo_stack(UADD, current_addr, current_addr)) == NULL) { SPL0(); return ERR; } SPL0(); } if (len < 0) return ERR; if (appended && size && o_isbinary && o_newline_added) fputs("newline inserted\n", stderr); else if (newline_added && (!appended || (!isbinary && !o_isbinary))) fputs("newline appended\n", stderr); if (isbinary && newline_added && !appended) size += 1; if (!size) newline_added = 1; newline_added = appended ? newline_added : o_newline_added; isbinary = isbinary | o_isbinary; if (des) size += 8 - size % 8; /* adjust DES size */ return size; }