home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / termlib / tgoto.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  2KB  |  125 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #define    CTRL(c)    ('c' & 037)
  3.  
  4. char    *UP;
  5. char    *BC;
  6.  
  7. /*
  8.  * Routine to perform cursor addressing.
  9.  * CM is a string containing printf type escapes to allow
  10.  * cursor addressing.  We start out ready to print the destination
  11.  * line, and switch each time we print row or column.
  12.  * The following escapes are defined for substituting row/column:
  13.  *
  14.  *    %d    as in printf
  15.  *    %2    like %2d
  16.  *    %3    like %3d
  17.  *    %.    gives %c hacking special case characters
  18.  *    %+x    like %c but adding x first
  19.  *    %<xy    if value < x add y, else just use valueindexing)
  20.  *    %r    reverses row/column
  21.  *    %i    increments row/column (for one origin indexing)
  22.  *    %%    gives %
  23.  *
  24.  * all other characters are ``self-inserting''.
  25.  */
  26. char *
  27. tgoto(CM, destcol, destline)
  28.     char *CM;
  29.     int destcol, destline;
  30. {
  31.     static char result[16];
  32.     static char added[10];
  33.     char *cp = CM;
  34.     register char *dp = result;
  35.     register int c;
  36.     int oncol = 0;
  37.     register int which;
  38.  
  39.     if (cp == 0) {
  40. toohard:
  41.         /*
  42.          * ``We don't do that under BOZO's big top''
  43.          */
  44.         strcpy(result, "OOPS");
  45.         return (result);
  46.     }
  47.     added[0] = 0;
  48.     while (c = *cp++) {
  49.         if (c != '%') {
  50.             *dp++ = c;
  51.             continue;
  52.         }
  53.         which = oncol ? destcol : destline;
  54.         switch (c = *cp++) {
  55.  
  56.         case 'n':
  57.             destcol ^= 0140;
  58.             destline ^= 0140;
  59.             continue;
  60.  
  61.         case 'd':
  62.             if (which < 10)
  63.                 goto one;
  64.             if (which < 100)
  65.                 goto two;
  66.             /* fall into... */
  67.  
  68.         case '3':
  69.             *dp++ = (which / 100) | '0';
  70.             which %= 100;
  71.             /* fall into... */
  72.  
  73.         case '2':
  74. two:    
  75.             *dp++ = which / 10 | '0';
  76. one:
  77.             *dp++ = which % 10 | '0';
  78.             oncol = 1 - oncol;
  79.             continue;
  80.  
  81.         case '<':
  82.             if (which < *dp++)
  83.                 which += *dp++;
  84.             else
  85.                 dp++;
  86.             goto casedot;
  87.  
  88.         case '+':
  89.             which += *cp++;
  90.             /* fall into... */
  91.  
  92.         case '.':
  93. casedot:
  94.             if (!UP)
  95.                 goto toohard;
  96.             if (which == 0 || which == CTRL(d) || which == '\t' || which == '\n') {
  97.                 do {
  98.                     strcat(added, oncol ? (BC ? BC : "\b") : UP);
  99.                     which++;
  100.                 } while (which == '\n');
  101.             }
  102.             *dp++ = which;
  103.             /* fall into... */
  104.  
  105.         case 'r':
  106.             oncol = 1 - oncol;
  107.             continue;
  108.  
  109.         case 'i':
  110.             destcol++;
  111.             destline++;
  112.             continue;
  113.  
  114.         case '%':
  115.             *dp++ = c;
  116.             continue;
  117.  
  118.         default:
  119.             goto toohard;
  120.         }
  121.     }
  122.     strcpy(dp, added);
  123.     return (result);
  124. }
  125.