BSD 2 development
authorMichael Toy <toy@ucbvax.Berkeley.EDU>
Wed, 9 May 1979 05:04:35 +0000 (21:04 -0800)
committerMichael Toy <toy@ucbvax.Berkeley.EDU>
Wed, 9 May 1979 05:04:35 +0000 (21:04 -0800)
Work on file src/termlib/tgoto.c
Work on file src/termlib/tputs.c

Synthesized-from: 2bsd

src/termlib/tgoto.c [new file with mode: 0644]
src/termlib/tputs.c [new file with mode: 0644]

diff --git a/src/termlib/tgoto.c b/src/termlib/tgoto.c
new file mode 100644 (file)
index 0000000..8ea3381
--- /dev/null
@@ -0,0 +1,124 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#define        CTRL(c) ('c' & 037)
+
+char   *UP;
+char   *BC;
+
+/*
+ * Routine to perform cursor addressing.
+ * CM is a string containing printf type escapes to allow
+ * cursor addressing.  We start out ready to print the destination
+ * line, and switch each time we print row or column.
+ * The following escapes are defined for substituting row/column:
+ *
+ *     %d      as in printf
+ *     %2      like %2d
+ *     %3      like %3d
+ *     %.      gives %c hacking special case characters
+ *     %+x     like %c but adding x first
+ *     %<xy    if value < x add y, else just use valueindexing)
+ *     %r      reverses row/column
+ *     %i      increments row/column (for one origin indexing)
+ *     %%      gives %
+ *
+ * all other characters are ``self-inserting''.
+ */
+char *
+tgoto(CM, destcol, destline)
+       char *CM;
+       int destcol, destline;
+{
+       static char result[16];
+       static char added[10];
+       char *cp = CM;
+       register char *dp = result;
+       register int c;
+       int oncol = 0;
+       register int which;
+
+       if (cp == 0) {
+toohard:
+               /*
+                * ``We don't do that under BOZO's big top''
+                */
+               strcpy(result, "OOPS");
+               return (result);
+       }
+       added[0] = 0;
+       while (c = *cp++) {
+               if (c != '%') {
+                       *dp++ = c;
+                       continue;
+               }
+               which = oncol ? destcol : destline;
+               switch (c = *cp++) {
+
+               case 'n':
+                       destcol ^= 0140;
+                       destline ^= 0140;
+                       continue;
+
+               case 'd':
+                       if (which < 10)
+                               goto one;
+                       if (which < 100)
+                               goto two;
+                       /* fall into... */
+
+               case '3':
+                       *dp++ = (which / 100) | '0';
+                       which %= 100;
+                       /* fall into... */
+
+               case '2':
+two:   
+                       *dp++ = which / 10 | '0';
+one:
+                       *dp++ = which % 10 | '0';
+                       oncol = 1 - oncol;
+                       continue;
+
+               case '<':
+                       if (which < *dp++)
+                               which += *dp++;
+                       else
+                               dp++;
+                       goto casedot;
+
+               case '+':
+                       which += *cp++;
+                       /* fall into... */
+
+               case '.':
+casedot:
+                       if (!UP)
+                               goto toohard;
+                       if (which == 0 || which == CTRL(d) || which == '\t' || which == '\n') {
+                               do {
+                                       strcat(added, oncol ? (BC ? BC : "\b") : UP);
+                                       which++;
+                               } while (which == '\n');
+                       }
+                       *dp++ = which;
+                       /* fall into... */
+
+               case 'r':
+                       oncol = 1 - oncol;
+                       continue;
+
+               case 'i':
+                       destcol++;
+                       destline++;
+                       continue;
+
+               case '%':
+                       *dp++ = c;
+                       continue;
+
+               default:
+                       goto toohard;
+               }
+       }
+       strcpy(dp, added);
+       return (result);
+}
diff --git a/src/termlib/tputs.c b/src/termlib/tputs.c
new file mode 100644 (file)
index 0000000..6cace8b
--- /dev/null
@@ -0,0 +1,87 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include <sgtty.h>
+#include <ctype.h>
+
+/*
+ * The following array gives the number of tens of milliseconds per
+ * character for each speed as returned by gtty.  Thus since 300
+ * baud returns a 7, there are 33.3 milliseconds per char at 300 baud.
+ */
+static
+short  tmspc10[] = {
+       0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10
+};
+
+short  ospeed;
+char   PC;
+
+/*
+ * Put the character string cp out, with padding.
+ * The number of affected lines is affcnt, and the routine
+ * used to output one character is outc.
+ */
+tputs(cp, affcnt, outc)
+       register char *cp;
+       int affcnt;
+       int (*outc)();
+{
+       register int i = 0;
+       register int mspc10;
+
+       if (cp == 0)
+               return;
+
+       /*
+        * Convert the number representing the delay.
+        */
+       if (isdigit(*cp)) {
+               do
+                       i = i * 10 + *cp++ - '0';
+               while (isdigit(*cp));
+       }
+       i *= 10;
+       if (*cp == '.') {
+               cp++;
+               if (isdigit(*cp))
+                       i += *cp - '0';
+               /*
+                * Only one digit to the right of the decimal point.
+                */
+               while (isdigit(*cp))
+                       cp++;
+       }
+
+       /*
+        * If the delay is followed by a `*', then
+        * multiply by the affected lines count.
+        */
+       if (*cp == '*')
+               cp++, i *= affcnt;
+
+       /*
+        * The guts of the string.
+        */
+       while (*cp)
+               (*outc)(*cp++);
+
+       /*
+        * If no delay needed, or output speed is
+        * not comprehensible, then don't try to delay.
+        */
+       if (i == 0)
+               return;
+       if (ospeed <= 0 || ospeed >= (sizeof tmspc10 / sizeof tmspc10[0]))
+               return;
+
+       /*
+        * Round up by a half a character frame,
+        * and then do the delay.
+        * Too bad there are no user program accessible programmed delays.
+        * Transmitting pad characters slows many
+        * terminals down and also loads the system.
+        */
+       mspc10 = tmspc10[ospeed];
+       i += mspc10 / 2;
+       for (i /= mspc10; i > 0; i--)
+               (*outc)(PC);
+}