update from Rodney Ruddock (rodney@snowhite.cis.uoguelph.ca)
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 12 May 1993 03:08:37 +0000 (19:08 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 12 May 1993 03:08:37 +0000 (19:08 -0800)
SCCS-vsn: contrib/ed/c.c 5.5
SCCS-vsn: contrib/ed/e.c 5.7
SCCS-vsn: contrib/ed/g.c 5.10
SCCS-vsn: contrib/ed/filename.c 5.7
SCCS-vsn: contrib/ed/main.c 5.12
SCCS-vsn: contrib/ed/r.c 5.8
SCCS-vsn: contrib/ed/re.c 5.6
SCCS-vsn: contrib/ed/u.c 5.5
SCCS-vsn: contrib/ed/w.c 5.8
SCCS-vsn: contrib/ed/sub.c 5.8
SCCS-vsn: contrib/ed/ed.1 5.9

usr/src/contrib/ed/c.c
usr/src/contrib/ed/e.c
usr/src/contrib/ed/ed.1
usr/src/contrib/ed/filename.c
usr/src/contrib/ed/g.c
usr/src/contrib/ed/main.c
usr/src/contrib/ed/r.c
usr/src/contrib/ed/re.c
usr/src/contrib/ed/sub.c
usr/src/contrib/ed/u.c
usr/src/contrib/ed/w.c

index 1cda903..6c3bcbd 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)c.c        5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)c.c        5.5 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -35,6 +35,8 @@ c(inputt, errnum)
        FILE *inputt;
        int *errnum;
 {
+       int l_flag=1;
+
        if (Start_default && End_default)
                Start = End = current;
        else
@@ -45,6 +47,8 @@ c(inputt, errnum)
                return;
        }
        Start_default = End_default = 0;
+       if (End == bottom)
+               l_flag = 0;
 
        /* first delete the lines */
        d(inputt, errnum);
@@ -52,7 +56,8 @@ c(inputt, errnum)
                return;
        *errnum = 0;
 
-       if ((current != NULL) && (current != bottom))
+       /*if ((current != NULL) && (current != bottom)) RMSR */
+       if ((current != NULL) && l_flag)
                current = current->above;
        add_flag = 1;
        Start_default = End_default = 1;
index cd754d3..291df11 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)e.c        5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)e.c        5.7 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -179,4 +179,5 @@ e2(inputt, errnum)
                }
        }
        change_flag = 0;
+       *errnum = 1;
 }
index ef3b60e..4a9cb6e 100644 (file)
@@ -6,7 +6,7 @@
 .\"
 .\" %sccs.include.redist.roff%
 .\"
-.\"    @(#)ed.1        5.8 (Berkeley) %G%
+.\"    @(#)ed.1        5.9 (Berkeley) %G%
 .\"
 .TH ED 1 ""
 .SH NAME
@@ -149,7 +149,7 @@ the buffer to the top of the buffer if need be.
 RE's are, outside of this document, now refered to as
 .IR "basic regular expressions" .
 Basic regular expressions (BRE's), traditionally described in \fIed(1)\fR are
-now fully described in regex(1). BRE's are, for the most part, the same as
+now fully described in regex(7). BRE's are, for the most part, the same as
 the old RE's - the name has changed and the expressions extended to meet
 POSIX 1003.2 specifications. (See the search command for more details.)
 .TP
@@ -472,7 +472,7 @@ command except (\fIe\fR, \fIE\fR, \fIf\fR, \fIq\fR, \fIQ\fR, \fIr\fR, \fIw\fR, \
 The move command moves the addressed lines in the buffer
 to after the address
 .BR a .
-Line 0 is valid for this command.
+Line 0 is valid as the address \fBa\fR for this command.
 .B Current
 is the location in the
 .B buffer
@@ -648,7 +648,7 @@ the
 .B buffer
 to after the address
 .BR a .
-Address 0 is valid for this command.
+Address 0 is valid as the address \fBa\fR for this command.
 .B Current
 is the last line transcribed.
 .TP 5
@@ -859,7 +859,7 @@ B. W. Kernighan,
 .I
 Advanced editing on UNIX
 .br
-regex(7), sed(1), learn(1), ex(1), POSIX 1003.2 (4.20)
+ex(1), learn(1), regex(3), regex(7), sed(1), vi(1), POSIX 1003.2 (4.20)
 .SH "AUTHOR"
 Rodney Ruddock
 .SH DIAGNOSTICS
@@ -888,6 +888,13 @@ RE's. The changes to the RE's are extensions for internationalization
 under POSIX 1003.2. Old scripts with RE's should work without
 modification.
 .PP
+Regular expression logic is very tight. If you believe a command with a
+regular expression in it has performed erroneously then a close reading
+of regex(7) is likely required.
+.PP
+Address `0' is legal only for those commands which explicitly state that
+it may be used; its use is illegal for all other commands.
+.PP
 The special form of substitute has been maintained for backward
 compatability and should not be used in scripts if they are to
 portable.
index 752dbc9..275c8f4 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)filename.c 5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)filename.c 5.7 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -79,7 +79,7 @@ filename(inputt, errnum)
                        l_fname[l_cnt] = '\0';
                        break;
                } else
-                       if ((ss == '!') && (l_esc == 0))
+                       if ((ss == '!') && (l_esc == 0) && (l_cnt < 2))
                                l_bang_flag = 1;
                        else
                                if ((ss != ' ') || (l_bang_flag))
index 1eaf62e..b34219c 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)g.c        5.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)g.c        5.10 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -143,7 +143,7 @@ g(inputt, errnum)
        int *errnum;
 {
        static char *l_template_g;
-       char *l_patt, l_ohm[130];
+       char *l_patt;
        static int l_template_flag = 0;
        int l_re_success, l_flag_v = 0, l_err, l_num;
        register l_gut_cnt, a;
@@ -243,7 +243,6 @@ g(inputt, errnum)
        l_posix_cur = current;
 #endif
        current = Start;
-       strcpy(l_ohm, help_msg);
 
        sigspecial++;
 
@@ -340,7 +339,6 @@ g(inputt, errnum)
        }
 
 point:
-       strcpy(help_msg, l_ohm);
        if (GV_flag == 0) {
                fclose(l_fp);
                unlink(l_template_g);
index 54b7148..fdfa09c 100644 (file)
@@ -15,7 +15,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     5.12 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -95,7 +95,7 @@ main(argc, argv)
        int argc;
        char *argv[];
 {
-       int l_num, errnum = 0, l_err = 0;
+       int l_num, errnum=0, l_err=0;
        char *l_fnametmp, *l_col, buf[2];
        struct winsize win;
 
@@ -228,6 +228,8 @@ cmd_loop(inputt, errnum)
                        start_up_flag = 0;
                        /* simulate the 'e' at startup */
                        e2(inputt, errnum);
+                       if (*errnum == 0)
+                               goto errmsg2;
                }
        }
        for (;;) {
@@ -250,6 +252,8 @@ cmd_loop(inputt, errnum)
                        case 'e':
                        case 'E':
                                e(inputt, errnum);
+                               if (*errnum == 0)
+                                       goto errmsg2;
                                break;
                        case 'f':
                                f(inputt, errnum);
@@ -354,10 +358,14 @@ cmd_loop(inputt, errnum)
                                /*ss = 'q';*/
                        case 'q':
                        case 'Q':
+                               if ((!isatty(STDIN_FILENO)) && (ss == 'q'))
+                                       ss = 'Q';
                                q(inputt, errnum);
                                break;
                        case 'r':
                                r(inputt, errnum);
+                               if (*errnum == 0)
+                                       goto errmsg2;
                                break;
                        case 's':
                                s(inputt, errnum);
@@ -500,12 +508,10 @@ cmd_loop(inputt, errnum)
                        else if (*errnum < 0) {
 errmsg:                                while (((ss = getc(inputt)) != '\n') &&
                                    (ss != EOF));
+                               (void)printf("?\n");
+errmsg2:                       if (help_flag)
+                                       (void)printf("%s\n", help_msg);
                                exit_code = 4;
-                               if (g_flag == 0) {
-                                       (void)printf("?\n");
-                                       if (help_flag)
-                                               (void)printf("%s\n", help_msg);
-                               }
 /* for people wanting scripts to carry on after a cmd error, then
  * define NOENDONSCRIPT on the compile line.
  */
index 0946227..6ba93d9 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)r.c        5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)r.c        5.8 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -53,6 +53,10 @@ r(inputt, errnum)
                if (sigint_flag && (!sigspecial))
                        SIGINT_ACTION;
        } else {
+               if (End_default) {
+                       End = bottom;
+                       End_default = 0;
+               }
                l_temp = filename(inputt, errnum);
                if (*errnum == 1)
                        l_filename_read = l_temp;
@@ -136,7 +140,7 @@ r(inputt, errnum)
                pclose(l_fp);
        else
                fclose(l_fp);
-       change_flag = 1;
+       /*change_flag = 1; done in input_lines() already */
        if (sigint_flag)
                SIGINT_ACTION;
        *errnum = 1;
index 89cc483..ce9b039 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)re.c       5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)re.c       5.6 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -53,7 +53,7 @@ regexec_n(reprecomp, strg, num_subexp, reprematch, flags, n, offset, pass)
 {
        int l_cnt, l_flag=0;
 #ifndef REG_STARTEND
-       char *l_offset = strg;
+       char *l_offset=strg, *l_end;
 #endif
 
        if (n <= 0)
@@ -63,12 +63,9 @@ regexec_n(reprecomp, strg, num_subexp, reprematch, flags, n, offset, pass)
        if (pass)
                reprematch[0].rm_so = 0;
        reprematch[0].rm_eo = len;
-       if (!reprematch[0].rm_so)
-               l_flag = 1;
 #else
        strg = &strg[offset];
-       if (!offset)
-               l_flag = 1;
+       l_end = &strg[strlen(strg)];
 #endif
        for (l_cnt = 0;;) {
                if (regexec(reprecomp,
@@ -76,22 +73,20 @@ regexec_n(reprecomp, strg, num_subexp, reprematch, flags, n, offset, pass)
                        l_cnt++;
                else
                        return (REG_NOMATCH);
-               /* to skip over null RE matchings */
-               if (l_flag)
-                       l_flag = 0;
-               else
-                       if (reprematch[0].rm_so == reprematch[0].rm_eo) {
-                               l_cnt--;
-                               if ((++reprematch[0].rm_eo) > len)
-                                       return (REG_NOMATCH);
-                       }
+
                if (l_cnt >= n)
                        break;
 #ifdef REG_STARTEND
+               if (reprematch[0].rm_so == reprematch[0].rm_eo)
+                       reprematch[0].rm_eo++;
                reprematch[0].rm_so = reprematch[0].rm_eo;
+               if (reprematch[0].rm_so == len)
+                       return (REG_NOMATCH);
                reprematch[0].rm_eo = len;
 #else
                strg = &strg[reprematch[0].rm_eo];
+               if (strg == l_end)
+                        return (REG_NOMATCH);
 #endif
                /* if a "^" started the current RE we only loop once */
                if (RE_sol)
@@ -146,7 +141,7 @@ re_replace(line, num_subexp, repmatch, replacer, offset)
         * l_slen[0] == len of what is to be replaced.
         * l_slen[1-9] == len of each backref.
         */
-       if ((*replacer == '%') && (replacer[1] == 1)) {
+       if ((*replacer == '%') && (replacer[1] == '\0')) {
                l_string = calloc(l_len_whole - l_slen[0] +
                    (strlen(l_prev_r)) + 2, sizeof(char));
                if (l_string == NULL) {
@@ -165,11 +160,13 @@ re_replace(line, num_subexp, repmatch, replacer, offset)
                l_string[l_len_before + offset] = '\0';
 #endif
                strcat(l_string, l_prev_r);
+               l_new_rm_eo = strlen(l_string);
 #ifdef REG_STARTEND
                strcat(l_string, &line[repmatch[0].rm_eo]);
 #else
                strcat(l_string, &line[repmatch[0].rm_eo + offset]);
 #endif
+               repmatch[0].rm_eo = l_new_rm_eo;
                return (l_string);
        }
 
index 3086276..827d469 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)sub.c      5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)sub.c      5.8 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -38,9 +38,9 @@ s(inputt, errnum)
        static int l_count2 = 1, l_global = 0, l_print = 0;
        static int l_first_pass_flag = 0;
        static char *l_match = NULL, *l_repl = NULL;
-       LINE *l_s_ret, *l_temp_line, *l_temp_line2, *l_kval, *l_last;
+       LINE *l_s_ret, *l_temp_line, *l_temp_line2, *l_kval, *l_last, *l_cur;
        int l_s_flag, l_count, l_matched, l_nflag, l_cnt, yy, l_sr_flag = 0;
-       int l_err, l_sl, l_rep_flag, l_u_reuse_flag=0, l_local_len;
+       int l_err, l_sl, l_rep_flag, l_u_reuse_flag=0, l_local_len, l_nudge=0;
        char *l_match2 = NULL, *l_local = NULL, *l_local_temp = NULL;
 #ifndef REG_STARTEND
        size_t l_offset = 0;
@@ -175,6 +175,7 @@ bcg1:
        if (sigint_flag && (!sigspecial))
                SIGINT_ACTION;
 bcg2:
+       l_cur = current;
        current = Start;
        l_s_flag = 0;
        do {
@@ -192,6 +193,9 @@ bcg2:
 #endif
                do {
                        RE_match[0].rm_so = RE_match[0].rm_eo;
+                       if (l_nudge)
+                               RE_match[0].rm_so++;
+                       l_nudge = 0;
 #ifdef REG_STARTEND
                        l_matched = regexec_n(&RE_comp, l_local,
                            (size_t)RE_SEC, RE_match, 0, l_count,
@@ -202,8 +206,11 @@ bcg2:
                            &l_offset, 0);
 #endif
                        if (l_matched == 0) {
-                               if ((l_s_flag == 0) && (g_flag == 0))
+                               if ((l_s_flag == 0) && (g_flag == 0)) {
+                                       current = l_cur;
                                        u_clr_stk();
+                                       current = Start;
+                               }
                                l_count = l_s_flag = 1;
                                l_rep_flag = 0;
                                /*
@@ -224,6 +231,8 @@ bcg2:
                                 * position of that character has changed
                                 * because of the replacement.
                                 */
+                               if (RE_match[0].rm_so == RE_match[0].rm_eo)
+                                       l_nudge = 1;
 #ifdef REG_STARTEND
                                l_local = re_replace(l_local,
                                    (size_t)(RE_SEC - 1), RE_match, &l_repl[1]);
@@ -315,9 +324,13 @@ next:
 
        if (l_s_flag == 0) {
                current = Start;
-               strcpy(help_msg, "no matches found for substitution");
-               *errnum = -1;
-               ungetc('\n', inputt);
+               if (!g_flag) {
+                       strcpy(help_msg, "no matches found for substitution");
+                       *errnum = -1;
+                       ungetc('\n', inputt);
+               }
+               else
+                       *errnum = 0;
                return;
        }
        change_flag = 1;
index 26d8163..74aacce 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)u.c        5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)u.c        5.5 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -27,6 +27,9 @@ static char sccsid[] = "@(#)u.c       5.4 (Berkeley) %G%";
 #include "ed.h"
 #include "extern.h"
 
+struct d_layer *old_d_stk=NULL;
+
+
 /*
  * This restores the buffer to the state it was in just before the
  * last buffer modifying command - the global commands (with command
@@ -56,6 +59,7 @@ undo()
 {
        LINE *l_current, *l_bottom, *l_top;
        struct u_layer *l_old_u_stk, *l_temp;
+       struct d_layer *l_d_temp;
 
        sigspecial++;
        /* This is done because undo can be undone. */
@@ -67,6 +71,10 @@ undo()
        u_top = top;
        u_bottom = bottom;
 
+       l_d_temp = old_d_stk;
+       old_d_stk = d_stk;
+       d_stk = l_d_temp;
+
        l_old_u_stk = u_stk;
        u_stk = NULL;
 
@@ -115,6 +123,7 @@ u_clr_stk()
                free(l_temp);
        }
        u_stk = NULL;           /* Just to sure. */
+       old_d_stk = NULL;       /* so something in use isn't freed! */
        sigspecial--;
        if (sigint_flag && (!sigspecial))
                SIGINT_ACTION;
index fc5cdd3..af60652 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)w.c        5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)w.c        5.8 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -51,11 +51,6 @@ w(inputt, errnum)
        } else
                if (Start_default)
                        Start = End;
-       if (Start == NULL) {
-               strcpy(help_msg, "buffer empty");
-               *errnum = -1;
-               return;
-       }
        Start_default = End_default = 0;
 
        l_sl = ss;
@@ -124,7 +119,10 @@ w(inputt, errnum)
                goto point;
 
        /* Write it out and get a report on the number of bytes written. */
-       l_ttl = edwrite(l_fp, Start, End);
+       if (Start == NULL)
+               l_ttl = 0;
+       else
+               l_ttl = edwrite(l_fp, Start, End);
        if (explain_flag > 0)           /* For -s option. */
                printf("%d\n", l_ttl);