annotate 3rdparty/packages/ed/subst.c @ 1734:b992196e2ac9

Dragon updates by Phill
author boisy
date Sat, 27 Nov 2004 15:24:02 +0000
parents bef1844de0dc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
994
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
1 /* subst.c */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
2 #include <stdio.h>
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
3 #include "tools.h"
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
4 #include "ed.h"
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
5
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
6 int subst(pat, sub, gflg, pflag)
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
7 TOKEN *pat;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
8 char *sub;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
9 int gflg, pflag;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
10 {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
11 int lin, chngd, nchngd;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
12 char *txtptr, *txt;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
13 char *lastm, *m, *new, buf[MAXLINE];
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
14
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
15 if (line1 <= 0) return(ERR);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
16 nchngd = 0; /* reset count of lines changed */
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
17 for (lin = line1; lin <= line2; lin++) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
18 txt = txtptr = gettxt(lin);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
19 new = buf;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
20 chngd = 0;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
21 lastm = NULL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
22 while (*txtptr) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
23 if (gflg || !chngd)
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
24 m = amatch(txtptr, pat, txt);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
25 else
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
26 m = NULL;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
27 if (m != NULL && lastm != m) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
28 chngd++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
29 new = catsub(txtptr, m, sub, new,
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
30 buf + MAXLINE);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
31 lastm = m;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
32 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
33 if (m == NULL || m == txtptr) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
34 *new++ = *txtptr++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
35 } else {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
36 txtptr = m;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
37 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
38 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
39 if (chngd) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
40 if (new >= buf + MAXLINE) return(ERR);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
41 *new++ = EOS;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
42 del(lin, lin);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
43 ins(buf);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
44 nchngd++;
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
45 if (pflag) doprnt(curln, curln);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
46 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
47 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
48 if (nchngd == 0 && !gflg) {
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
49 return(ERR);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
50 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
51 return(nchngd);
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
52 }
bef1844de0dc The ED editor ported from Minix
roug
parents:
diff changeset
53