home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / conf / part03 / confprnt.c next >
Encoding:
C/C++ Source or Header  |  1988-03-13  |  6.4 KB  |  422 lines

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