994
|
1 /* subst.c */
|
|
2 #include <stdio.h>
|
|
3 #include "tools.h"
|
|
4 #include "ed.h"
|
|
5
|
|
6 int subst(pat, sub, gflg, pflag)
|
|
7 TOKEN *pat;
|
|
8 char *sub;
|
|
9 int gflg, pflag;
|
|
10 {
|
|
11 int lin, chngd, nchngd;
|
|
12 char *txtptr, *txt;
|
|
13 char *lastm, *m, *new, buf[MAXLINE];
|
|
14
|
|
15 if (line1 <= 0) return(ERR);
|
|
16 nchngd = 0; /* reset count of lines changed */
|
|
17 for (lin = line1; lin <= line2; lin++) {
|
|
18 txt = txtptr = gettxt(lin);
|
|
19 new = buf;
|
|
20 chngd = 0;
|
|
21 lastm = NULL;
|
|
22 while (*txtptr) {
|
|
23 if (gflg || !chngd)
|
|
24 m = amatch(txtptr, pat, txt);
|
|
25 else
|
|
26 m = NULL;
|
|
27 if (m != NULL && lastm != m) {
|
|
28 chngd++;
|
|
29 new = catsub(txtptr, m, sub, new,
|
|
30 buf + MAXLINE);
|
|
31 lastm = m;
|
|
32 }
|
|
33 if (m == NULL || m == txtptr) {
|
|
34 *new++ = *txtptr++;
|
|
35 } else {
|
|
36 txtptr = m;
|
|
37 }
|
|
38 }
|
|
39 if (chngd) {
|
|
40 if (new >= buf + MAXLINE) return(ERR);
|
|
41 *new++ = EOS;
|
|
42 del(lin, lin);
|
|
43 ins(buf);
|
|
44 nchngd++;
|
|
45 if (pflag) doprnt(curln, curln);
|
|
46 }
|
|
47 }
|
|
48 if (nchngd == 0 && !gflg) {
|
|
49 return(ERR);
|
|
50 }
|
|
51 return(nchngd);
|
|
52 }
|
|
53
|