home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / conf2 / part03 / confprnt.c next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  5.8 KB  |  402 lines

  1. #include "conf.h"
  2.  
  3. jmp_buf glenv, cvenv;
  4.  
  5. #define    BACKCHAR() {if (expand8bit && !isascii(*(lp-1)))         \
  6.             (void)fputs("\b \b", stdout);              \
  7.             if (expandctrl && iscntrl(toascii(*(lp-1)))) \
  8.             (void)fputs("\b \b", stdout);              \
  9.             (void)fputs("\b \b", stdout); --lp;}
  10.  
  11. unsigned linelen, col = 0;
  12.  
  13. my_intr(sig)
  14. int sig;
  15. {
  16.     longjmp(glenv, sig);
  17. }
  18.  
  19. save_intr(sig)
  20. int sig;
  21. {
  22.     longjmp(cvenv, sig);
  23. }
  24.  
  25. fputchar(c)
  26. int c;
  27. {
  28.     (void)putchar(c);
  29. }
  30.  
  31. printmess(stream, constr, usr, tty, mess, length)
  32. char *constr, *usr, *tty, *mess;
  33. unsigned length;
  34. FILE *stream;
  35. {
  36.     register char *ptr = constr;
  37.     register int c;
  38.  
  39.     while (c = *ptr++)
  40.     {
  41.     switch(c)
  42.     {
  43.         case '%':
  44.         switch(c = *ptr++)
  45.         {
  46.             case 'T':
  47.             visprnt(cuser.cu_tty, stream);
  48.             break;
  49.  
  50.             case 't':
  51.             visprnt(tty, stream);
  52.             break;
  53.  
  54.             case 'N':
  55.             visprnt(cuser.cu_cname, stream);
  56.             break;
  57.  
  58.             case 'n':
  59.             visprnt(usr, stream);
  60.             break;
  61.  
  62.             case 'm':
  63.             messptr(mess, stream, length);
  64.             break;
  65.  
  66.             case '\0':
  67.             (void)putc('%', stream);
  68.             --ptr;
  69.             break;
  70.  
  71.             default:
  72.             (void)putc('%', stream);
  73.             case '%':
  74.             (void)putc(c, stream);
  75.             break;
  76.         }
  77.         break;
  78.  
  79.         default:
  80.         (void)putc(c, stream);
  81.         break;
  82.     }
  83.     }
  84.  
  85.     (void)fflush(stream);
  86. }
  87.  
  88. messptr(mess, stream, length)
  89. char *mess;
  90. unsigned length;
  91. FILE *stream;
  92. {
  93.     register char *ptr = mess;
  94.  
  95.     while (length--)
  96.     dispchar(*ptr++, stream, NOVIS);
  97.  
  98.     (void)fflush(stream);
  99. }
  100.  
  101. visprnt(mess, stream)
  102. char *mess;
  103. FILE *stream;
  104. {
  105.     register char *ptr = mess;
  106.  
  107.     while (*ptr)
  108.     dispchar(*ptr++, stream, VIS);
  109.  
  110.     (void)fflush(stream);
  111. }
  112.  
  113. vislen(mess)
  114. register char *mess;
  115. {
  116.     register int length = 0;
  117.  
  118.     while (*mess)
  119.     {
  120.     if (!isascii(*mess) && expand8bit)
  121.         length += 2;
  122.     if (iscntrl(*mess) && expandctrl)
  123.         ++length;
  124.     ++length;
  125.     ++mess;
  126.     }
  127.  
  128.     return length;
  129. }
  130.  
  131. dispchar(c, stream, flag)
  132. int c;
  133. FILE *stream;
  134. int flag;
  135. {
  136.     int wasmeta = FALSE;
  137.  
  138.     if (!isascii(c) && !expand8bit)
  139.     {
  140.     (void)putc(c, stream);
  141.     return;
  142.     }
  143.  
  144.     if (!isascii(c))
  145.     {
  146.     (void)putc('~', stream);
  147.     c = toascii(c);
  148.     wasmeta = TRUE;
  149.     }
  150.  
  151.     if (iscntrl(c) && expandctrl)
  152.     {
  153.     switch (c)
  154.     {
  155.         case DEL:
  156.         (void)fputs("^?", stream);
  157.         break;
  158.  
  159.         case '\n':
  160.         case TAB:
  161.         if (!wasmeta && !(flag&VIS))
  162.         {
  163.             (void)putc(c, stream);
  164.             break;
  165.         }
  166.  
  167.         default:
  168.         (void)putc('^', stream);
  169.         (void)putc(c|'@', stream);
  170.         break;
  171.     }
  172.     }
  173.     else
  174.     (void)putc(c, stream);
  175. }
  176.  
  177. char *
  178. getline()
  179. {
  180.     int c;
  181.     char *line = mymalloc(PAGESIZ);
  182.     int tmplen, len = PAGESIZ;
  183.     char *lp = line;
  184.  
  185.     if (c = setjmp(glenv))
  186.     {
  187.     (void)alarm(0);
  188.     (void)signal(SIGALRM, my_intr);
  189.     (void)signal(SIGINT, SIG_IGN);
  190.  
  191.     switch(c)
  192.     {
  193.         case SIGINT:
  194.         dispchar(ichar, stdout, NOVIS);
  195.         (void)putchar('\n');
  196.         break;
  197.     }
  198.  
  199.     free(line);
  200.     return NULL;
  201.     }
  202.  
  203.     (void)signal(SIGALRM, my_intr);
  204.     (void)signal(SIGINT, my_intr);
  205.  
  206.     (void)alarm(1);
  207.     read(0, lp, 1);
  208.     (void)alarm(0);
  209.  
  210.     do
  211.     {
  212.     if (lineinput) switch(*lp)
  213.     {
  214.         case CR:
  215.         case LF:
  216.         if (lp == line)
  217.         {
  218.             free(line);
  219.             return NULL;
  220.         }
  221.         *lp = '\0';
  222.         linelen = lp - line;
  223.         return line;
  224.  
  225.         default:
  226.         lp++;
  227.             break;
  228.     }
  229.     else switch(*lp)
  230.     {
  231.         case BS:
  232.         case DEL:
  233.         if (lp > line)
  234.             {BACKCHAR();}
  235.         else
  236.             (void)putchar(BELL);
  237.  
  238.         (void)fflush(stdout);
  239.  
  240.         if (lp == line)
  241.         {
  242.             free(line);
  243.             return NULL;
  244.             }
  245.         break;
  246.  
  247.         case CR:
  248.         case LF:
  249.         (void)putchar('\n');
  250.  
  251.         if (lp == line)
  252.         {
  253.             free(line);
  254.             return NULL;
  255.         }
  256.         *lp = '\0';
  257.         linelen = lp - line;
  258.         return line;
  259.  
  260.         case CTRL('L'):
  261.         if (cls == NULL)
  262.             puts("^L\n");
  263.         else
  264.             tputs(cls, lines, fputchar);
  265.  
  266.         messptr(line, stdout, (unsigned)(lp-line));
  267.  
  268.         if (lp == line)
  269.         {
  270.             free(line);
  271.             return NULL;
  272.         }
  273.         break;
  274.  
  275.         case CTRL('R'):
  276.         *lp = '\0';
  277.         (void)puts("^R");
  278.  
  279.         messptr(line, stdout, (unsigned)(lp-line));
  280.  
  281.         if (lp == line)
  282.         {
  283.             free(line);
  284.             return NULL;
  285.         }
  286.         break;
  287.  
  288.         case CTRL('U'):
  289.         while (lp > line)
  290.             BACKCHAR();
  291.  
  292.         (void)fflush(stdout);
  293.         free(line);
  294.         return NULL;
  295.  
  296.         case CTRL('V'):
  297.         if (c = setjmp(cvenv))
  298.         {
  299.             switch(c)
  300.             {
  301.             case SIGINT:
  302.                 *lp = ichar;
  303.                 break;
  304.  
  305.             case SIGQUIT:
  306.                 *lp = qchar;
  307.                 break;
  308.             }
  309.         }
  310.         else
  311.         {
  312.             (void)signal(SIGINT, save_intr);
  313.             (void)signal(SIGQUIT, save_intr);
  314.  
  315.             if (read(0, lp, 1) != 1)
  316.             {
  317.             free(line);        /* some sort of read error */
  318.             return NULL;
  319.             }
  320.         }
  321.  
  322.         (void)signal(SIGINT, my_intr);
  323.         (void)signal(SIGQUIT, fatal);
  324.         dispchar(*lp++, stdout, NOVIS);
  325.         (void)fflush(stdout);
  326.         break;
  327.  
  328.         case CTRL('W'):
  329.         while ((lp > line) && isspace(*(lp-1)))
  330.             BACKCHAR();   /* ditch the post word white space */
  331.  
  332.         while ((lp > line) && !isspace(*(lp-1)))
  333.             BACKCHAR(); /* someday a cool worderizer */
  334.  
  335.         (void)fflush(stdout);
  336.  
  337.         if (lp == line)
  338.         {
  339.             free(line);
  340.             return NULL;
  341.         }
  342.         break;
  343.  
  344.         case CTRL('D'):    /* default must follow this case */
  345.         if (lp == line)
  346.         {
  347.             (void)puts(":quit");
  348.             nice_exit(0);
  349.         }
  350.                 /* if not first character, do default: */
  351.         default:
  352.         dispchar(*lp++, stdout, NOVIS);
  353.         (void) fflush(stdout);
  354.             break;
  355.     }
  356.  
  357.     if ((tmplen = lp - line) >= len )
  358.     {
  359.         line = myrealloc(line, (unsigned)(len += PAGESIZ));
  360.         lp = line + tmplen;
  361.     }
  362.  
  363.     } while(read(0, lp, 1));
  364.  
  365.     free(line);
  366.     return NULL;    /* error while reading -- punt */
  367. }
  368.  
  369. colprnt(word, pad)
  370. char *word;
  371. int pad;
  372. {
  373.     if (col+pad > columns-1)
  374.     {
  375.     if (!columns)
  376.     {
  377.         visprnt(word, stdout);
  378.         (void)putchar('\n');
  379.     }
  380.     else
  381.     {
  382.         col = pad;
  383.         (void)putchar('\n');
  384.         visprnt(word, stdout);
  385.         (void)printf("%-*s", pad-vislen(word), " ");
  386.     }
  387.     }
  388.     else
  389.     {
  390.     col += pad;
  391.     visprnt(word, stdout);
  392.     (void)printf("%-*s", pad-vislen(word), " ");
  393.     }
  394. }
  395.  
  396. terpri()
  397. {
  398.     if (col)
  399.     (void)putchar('\n');
  400.     col = 0;
  401. }
  402.