BSD 4 release
[unix-history] / usr / src / cmd / ex / ex_subr.c
index 1e17f4c..74b81bb 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (c) 1979 Regents of the University of California */
+/* Copyright (c) 1980 Regents of the University of California */
+static char *sccsid = "@(#)ex_subr.c   6.2 11/6/80";
 #include "ex.h"
 #include "ex_re.h"
 #include "ex_tty.h"
 #include "ex.h"
 #include "ex_re.h"
 #include "ex_tty.h"
@@ -56,6 +57,21 @@ column(cp)
        return (qcolumn(cp, (char *) 0));
 }
 
        return (qcolumn(cp, (char *) 0));
 }
 
+/*
+ * Ignore a comment to the end of the line.
+ * This routine eats the trailing newline so don't call newline().
+ */
+comment()
+{
+       register int c;
+
+       do {
+               c = getchar();
+       } while (c != '\n' && c != EOF);
+       if (c == EOF)
+               ungetchar(c);
+}
+
 Copy(to, from, size)
        register char *from, *to;
        register int size;
 Copy(to, from, size)
        register char *from, *to;
        register int size;
@@ -179,6 +195,8 @@ ignnEOF()
 
        if (c == EOF)
                ungetchar(c);
 
        if (c == EOF)
                ungetchar(c);
+       else if (c=='"')
+               comment();
 }
 
 iswhite(c)
 }
 
 iswhite(c)
@@ -437,13 +455,15 @@ putmk1(addr, n)
        int n;
 {
        register line *markp;
        int n;
 {
        register line *markp;
+       register oldglobmk;
 
 
+       oldglobmk = *addr & 1;
        *addr &= ~1;
        for (markp = (anymarks ? names : &names['z'-'a'+1]);
          markp <= &names['z'-'a'+1]; markp++)
                if (*markp == *addr)
                        *markp = n;
        *addr &= ~1;
        for (markp = (anymarks ? names : &names['z'-'a'+1]);
          markp <= &names['z'-'a'+1]; markp++)
                if (*markp == *addr)
                        *markp = n;
-       *addr = n;
+       *addr = n | oldglobmk;
 }
 
 char *
 }
 
 char *
@@ -510,12 +530,18 @@ save(a1, a2)
 {
        register int more;
 
 {
        register int more;
 
+       if (!FIXUNDO)
+               return;
+#ifdef TRACE
+       if (trace)
+               vudump("before save");
+#endif
        undkind = UNDNONE;
        undadot = dot;
        more = (a2 - a1 + 1) - (unddol - dol);
        while (more > (endcore - truedol))
                if (morelines() < 0)
        undkind = UNDNONE;
        undadot = dot;
        more = (a2 - a1 + 1) - (unddol - dol);
        while (more > (endcore - truedol))
                if (morelines() < 0)
-                       error("Out of memory@saving lines for undo - try using ed or re");
+                       error("Out of memory@saving lines for undo - try using ed");
        if (more)
                (*(more > 0 ? copywR : copyw))(unddol + more + 1, unddol + 1,
                    (truedol - unddol));
        if (more)
                (*(more > 0 ? copywR : copyw))(unddol + more + 1, unddol + 1,
                    (truedol - unddol));
@@ -526,6 +552,10 @@ save(a1, a2)
        unddel = a1 - 1;
        undap1 = a1;
        undap2 = a2 + 1;
        unddel = a1 - 1;
        undap1 = a1;
        undap2 = a2 + 1;
+#ifdef TRACE
+       if (trace)
+               vudump("after save");
+#endif
 }
 
 save12()
 }
 
 save12()
@@ -596,11 +626,11 @@ smerror(seekpt, cp)
 #ifdef lint
 char   *std_errlist[] = {
 #else
 #ifdef lint
 char   *std_errlist[] = {
 #else
-#ifdef VMUNIX
+# ifdef VMUNIX
 char   *std_errlist[] = {
 char   *std_errlist[] = {
-#else
+# else
 short  std_errlist[] = {
 short  std_errlist[] = {
-#endif
+# endif
 #endif
        error("Error 0"),
        error("Not super-user"),
 #endif
        error("Error 0"),
        error("Not super-user"),
@@ -634,13 +664,12 @@ short     std_errlist[] = {
        error("Illegal seek"),
        error("Read-only file system"),
        error("Too many links"),
        error("Illegal seek"),
        error("Read-only file system"),
        error("Too many links"),
-       error("Broken pipe")
-#ifndef QUOTA
-       , error("Math argument")
-       , error("Result too large")
-#else
-       , error("Quota exceeded")
+       error("Broken pipe"),
+#ifndef V6
+       error("Math argument"),
+       error("Result too large"),
 #endif
 #endif
+       error("Quota exceeded")         /* Berkeley quota systems only */
 };
 
 #undef error
 };
 
 #undef error
@@ -668,12 +697,32 @@ syserror()
 
        dirtcnt = 0;
        putchar(' ');
 
        dirtcnt = 0;
        putchar(' ');
+       edited = 0;     /* for temp file errors, for example */
        if (e >= 0 && errno <= std_nerrs)
                error(std_errlist[e]);
        else
                error("System error %d", e);
 }
 
        if (e >= 0 && errno <= std_nerrs)
                error(std_errlist[e]);
        else
                error("System error %d", e);
 }
 
+/*
+ * Return the column number that results from being in column col and
+ * hitting a tab, where tabs are set every ts columns.  Work right for
+ * the case where col > COLUMNS, even if ts does not divide COLUMNS.
+ */
+tabcol(col, ts)
+int col, ts;
+{
+       int offset, result;
+
+       if (col >= COLUMNS) {
+               offset = COLUMNS * (col/COLUMNS);
+               col -= offset;
+       } else
+               offset = 0;
+       result = col + ts - (col % ts) + offset;
+       return (result);
+}
+
 char *
 vfindcol(i)
        int i;
 char *
 vfindcol(i)
        int i;
@@ -758,3 +807,23 @@ markit(addr)
                markDOT();
 }
 
                markDOT();
 }
 
+/*
+ * The following code is defensive programming against a bug in the
+ * pdp-11 overlay implementation.  Sometimes it goes nuts and asks
+ * for an overlay with some garbage number, which generates an emt
+ * trap.  This is a less than elegant solution, but it is somewhat
+ * better than core dumping and losing your work, leaving your tty
+ * in a weird state, etc.
+ */
+int _ovno;
+onemt()
+{
+       int oovno;
+
+       signal(SIGEMT, onemt);
+       oovno = _ovno;
+       /* 2 and 3 are valid on 11/40 type vi, so */
+       if (_ovno < 0 || _ovno > 3)
+               _ovno = 0;
+       error("emt trap, _ovno is %d @ - try again");
+}