home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / WWIV420S.ZIP / COM.C < prev    next >
C/C++ Source or Header  |  1991-04-23  |  22KB  |  1,072 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1991 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "vars.h"
  18.  
  19. #pragma hdrstop
  20.  
  21. #include <stdarg.h>
  22. #include <math.h>
  23.  
  24.  
  25.  
  26. #define frequency 500
  27.  
  28.  
  29. int check_comport(int pn)
  30. {
  31.   unsigned char new_iir, old_iir;
  32.  
  33.   old_iir = inportb(syscfg.com_base[pn]+2);
  34.   outportb(syscfg.com_base[pn]+2,0xc1);
  35.   new_iir = inportb(syscfg.com_base[pn]+2);
  36.   outportb(syscfg.com_base[pn]+2,old_iir);
  37.  
  38.   if (new_iir & 0x38) {
  39.     /* no com port */
  40.     return(0);
  41.   }
  42.  
  43.   switch ((new_iir >> 6) & 0x03) {
  44.     case 0: /* no 16550 */
  45.     case 1: /* huh? */
  46.       return(1);
  47.  
  48.     case 2: /* 16550 */
  49.     case 3: /* 16550A */
  50.       return(2);
  51.   }
  52.  
  53.   return(1);
  54. }
  55.  
  56.  
  57. void savel(char *cl, char *atr, char *xl, char *cc)
  58. {
  59.   int i, i1;
  60.  
  61.   *cc = curatr;
  62.   strcpy(xl, endofline);
  63.   i = ((wherey() + topline) * 80) * 2;
  64.   for (i1 = 0; i1 < wherex(); i1++) {
  65.     cl[i1]  = scrn[i + (i1 * 2)];
  66.     atr[i1] = scrn[i + (i1 * 2) + 1];
  67.   }
  68.   cl[wherex()]  = 0;
  69.   atr[wherex()] = 0;
  70. }
  71.  
  72.  
  73. void restorel(char *cl, char *atr, char *xl, char *cc)
  74. {
  75.   int i;
  76.  
  77.   if (wherex())
  78.     nl();
  79.   for (i = 0; cl[i] != 0; i++) {
  80.     setc(atr[i]);
  81.     outchr(cl[i]);
  82.   }
  83.   setc(*cc);
  84.   strcpy(endofline, xl);
  85. }
  86.  
  87.  
  88. void ptime()
  89. {
  90.   char xl[81], cl[81], atr[81], cc, s[81];
  91.   long l;
  92.  
  93.   savel(cl, atr, xl, &cc);
  94.  
  95.   ansic(0);
  96.   nl();
  97.   nl();
  98.   time(&l);
  99.   strcpy(s, ctime(&l));
  100.   s[strlen(s) - 1] = 0;
  101.   pl(s);
  102.   if (useron) {
  103.     npr("Time on   = %s\r\n", ctim(timer() - timeon));
  104.     npr("Time left = %s\r\n", ctim(nsl()));
  105.   }
  106.   nl();
  107.  
  108.   restorel(cl, atr, xl, &cc);
  109. }
  110.  
  111.  
  112. void reprint()
  113. {
  114.   char xl[81], cl[81], atr[81], cc, ansistr_1[81];
  115.   int ansiptr_1;
  116.  
  117.   ansiptr_1=ansiptr;
  118.   ansiptr=0;
  119.   ansistr[ansiptr_1]=0;
  120.   strcpy(ansistr_1,ansistr);
  121.  
  122.   savel(cl, atr, xl, &cc);
  123.   nl();
  124.   restorel(cl, atr, xl, &cc);
  125.  
  126.   strcpy(ansistr,ansistr_1);
  127.   ansiptr=ansiptr_1;
  128. }
  129.  
  130.  
  131. void print_help(int n)
  132. {
  133.   char xl[81], cl[81], atr[81], cc, s[81];
  134.   int next;
  135.  
  136.   savel(cl, atr, xl, &cc);
  137.   ansic(0);
  138.   outstr("\x0c");
  139.  
  140.   sprintf(s,"%sHELP.MSG",syscfg.gfilesdir);
  141.   next = 0;
  142.   if (helps[n].stored_as)
  143.     read_message1(&helps[n], 0, 0, &next, s);
  144.  
  145.   restorel(cl, atr, xl, &cc);
  146. }
  147.  
  148.  
  149. void setbeep(int i)
  150. {
  151.   int i1,i2;
  152.  
  153.   if (i) {
  154.     i1 = 0x34DD / frequency;
  155.     i2 = inportb(0x61);
  156.     if (!(i2 & 0x03)) {
  157.       outportb(0x61, i2 | 0x03);
  158.       outportb(0x43, 0xB6);
  159.     }
  160.     outportb(0x42, i1 & 0x0F);
  161.     outportb(0x42, i1 >> 4);
  162.   } else
  163.     outportb(0x61, inportb(0x61) & 0xFC);
  164. }
  165.  
  166. void far interrupt async_isr ()
  167. /* This function is called every time a char is received on the com port.
  168.  * The character is stored in the buffer[] array, and the head pointer is
  169.  * updated.
  170.  */
  171. {
  172.   buffer[head++] = inportb(base);
  173.   if (head == max_buf)
  174.     head = 0;
  175.   outportb(0x20, 0x20);
  176. }
  177.  
  178.  
  179.  
  180. void outcomch(char ch)
  181. /* This function outputs one character to the com port */
  182. {
  183.   while (!(inportb(base + 5) & 0x20))
  184.     ;
  185.   if (flow_control)
  186.     while (!(inportb(base + 6) & 0x10))
  187.       ;
  188.   outportb(base, ch);
  189. }
  190.  
  191.  
  192.  
  193. char peek1c()
  194. {
  195.   if (head!=tail) {
  196.     return(buffer[tail]);
  197.   } else
  198.     return(0);
  199. }
  200.  
  201. char get1c()
  202. /* This function returns one character from the com port, or a zero if
  203.  * no character is waiting
  204.  */
  205. {
  206.   char c1;
  207.  
  208.   if (head != tail) {
  209.     disable();
  210.     c1 = buffer[tail++];
  211.     if (tail == max_buf)
  212.       tail = 0;
  213.     enable();
  214.     return(c1);
  215.   } else
  216.     return(0);
  217. }
  218.  
  219.  
  220.  
  221. int comhit()
  222. /* This returns a value telling if there is a character waiting in the com
  223.  * buffer.
  224.  */
  225. {
  226.   return(head != tail);
  227. }
  228.  
  229.  
  230.  
  231. void dump()
  232. /* This function clears the com buffer */
  233. {
  234.   disable();
  235.   head = tail = 0;
  236.   enable();
  237. }
  238.  
  239.  
  240.  
  241. void set_baud(unsigned int rate)
  242. /* This function sets the com speed to that passed */
  243. {
  244.   float rl;
  245.  
  246.   if ((rate > 49) && (rate < 57601)) {
  247.     rl   = 115200.0 / ((float) rate);
  248.     rate = (int) rl;
  249.     outportb(base + 3, inportb(base + 3) | 0x80);
  250.     outportb(base,     (rate & 0x00FF));
  251.     outportb(base + 1, ((rate >> 8) & 0x00FF));
  252.     outportb(base + 3, inportb(base + 3) & 0x7F);
  253.   }
  254. }
  255.  
  256.  
  257. void initport(int port_num)
  258. /* This function initializes the com buffer, setting up the interrupt,
  259.  * and com parameters
  260.  */
  261. {
  262.   int temp;
  263.  
  264.   base = syscfg.com_base[port_num];
  265.   async_irq = syscfg.com_ISR[port_num];
  266.   setvect(8 + async_irq, async_isr);
  267.   head = tail = 0;
  268.   outportb(base + 3, 0x03);
  269.   disable();
  270.   temp = inportb(base + 5);
  271.   temp = inportb(base);
  272.   temp = inportb(0x21);
  273.   temp = temp & ((1 << async_irq) ^ 0x00FF);
  274.   outportb(0x21, temp);
  275.   outportb(base + 1, 0x01);
  276.   temp=inportb(base + 4);
  277.   outportb(base + 4, temp | 0x0A);
  278.   outportb(base+2,0xc1);
  279.   enable();
  280.   dtr(1);
  281. }
  282.  
  283.  
  284.  
  285. void closeport()
  286. /* This function closes out the com port, removing the interrupt routine,
  287.  * etc.
  288.  */
  289. {
  290.   int temp;
  291.  
  292.   disable();
  293.   temp = inportb(0x21);
  294.   temp = temp | ((1 << async_irq));
  295.   outportb(0x21, temp | 0x10);
  296.   outportb(base + 2, 0);
  297.   outportb(base + 4, 3);
  298.   enable();
  299. }
  300.  
  301. void dtr(int i)
  302. /* This function sets the DTR pin to the status given */
  303. {
  304.   int i1;
  305.  
  306.   i1 = inportb(base + 4) & 0x00FE;
  307.   outportb(base + 4, (i || no_hangup) ? (i1 + 1) : i1);
  308. }
  309.  
  310.  
  311. void rts(int i)
  312. /* This function sets the RTS pin to the status given */
  313. {
  314.   int i1;
  315.  
  316.   i1 = inportb(base + 4) & 0x00FD;
  317.   outportb(base + 4, (i) ? (i1 + 2) : i1);
  318. }
  319.  
  320.  
  321. int cdet()
  322. /* This returns the status of the carrier detect lead from the modem */
  323. {
  324.   return((inportb(base + 6) & 0x80) ? 1 : 0);
  325. }
  326.  
  327.  
  328.  
  329. void checkhangup()
  330. /* This function checks to see if the user logged on to the com port has
  331.  * hung up.  Obviously, if no user is logged on remotely, this does nothing.
  332.  * If carrier detect is detected to be low, it is checked 100 times
  333.  * sequentially to make sure it stays down, and is not just a quirk.
  334.  */
  335. {
  336.   int i, ok;
  337.  
  338.   if (!hangup && using_modem && !cdet()) {
  339.     ok = 0;
  340.     for (i = 0; (i < 500) && !ok; i++)
  341.       if (cdet())
  342.         ok = 1;
  343.     if (!ok) {
  344.       hangup = hungup = 1;
  345.       if (useron && !in_extern)
  346.         sysoplog("Hung Up.");
  347.     }
  348.   }
  349. }
  350.  
  351.  
  352.  
  353.  
  354. void addto(char *s, int i)
  355. {
  356.   char temp[20];
  357.  
  358.   if (s[0])
  359.     strcat(s, ";");
  360.   else
  361.     strcpy(s, "\x1B[");
  362.   itoa(i, temp, 10);
  363.   strcat(s, temp);
  364. }
  365.  
  366.  
  367. void makeansi(unsigned char attr, char *s, int forceit)
  368. /* Passed to this function is a one-byte attribute as defined for IBM type
  369.  * screens.  Returned is a string which, when printed, will change the
  370.  * display to the color desired, from the current function.
  371.  */
  372. {
  373.   unsigned char catr;
  374.   char *temp = "04261537";
  375.  
  376.   catr = curatr;
  377.   s[0] = 0;
  378.   if (attr != catr) {
  379.     if ((catr & 0x88) ^ (attr & 0x88)) {
  380.       addto(s, 0);
  381.       addto(s, 30 + temp[attr & 0x07] - '0');
  382.       addto(s, 40 + temp[(attr & 0x70) >> 4] - '0');
  383.       catr = attr & 0x77;
  384.     }
  385.     if ((catr & 0x07) != (attr & 0x07))
  386.       addto(s, 30 + temp[attr & 0x07] - '0');
  387.     if ((catr & 0x70) != (attr & 0x70))
  388.       addto(s, 40 + temp[(attr & 0x70) >> 4] - '0');
  389.     if ((catr & 0x08) ^ (attr & 0x08))
  390.       addto(s, 1);
  391.     if ((catr & 0x80) ^ (attr & 0x80)) {
  392.       if (checkcomp("Mac"))     /*This is the code for Mac's underline*/
  393.         addto(s, 4);            /*They don't have Blinking or Italics*/
  394.       else {
  395.         if (checkcomp("Ami"))   /*Some Amiga terminals use 3 instead of*/
  396.           addto(s, 3);          /*5 for italics.  Using both won't hurt*/
  397.         addto(s, 5);            /*anything, only italics will be generated*/
  398.       }
  399.     }
  400.   }
  401.   if (s[0])
  402.     strcat(s, "m");
  403.   if (!okansi() && !forceit)
  404.     s[0]=0;
  405. }
  406.  
  407.  
  408.  
  409. void setfgc(int i)
  410. /* This sets the foreground color to that passed.  It is called only from
  411.  * execute_ansi
  412.  */
  413. {
  414.   curatr = (curatr & 0xf8) | i;
  415. }
  416.  
  417.  
  418.  
  419. void setbgc(int i)
  420. /* This sets the background color to that passed.  It is called only from
  421.  * execute_ansi
  422.  */
  423. {
  424.   curatr = (curatr & 0x8f) | (i << 4);
  425. }
  426.  
  427.  
  428. void execute_ansi()
  429. /* This function executes an ANSI string to change color, position the
  430.  * cursor, etc.
  431.  */
  432. {
  433.   int args[11], argptr, count, ptr, tempptr, ox, oy;
  434.   char cmd, temp[11], teol[81], *clrlst = "04261537";
  435.  
  436.   if (ansistr[1] != '[') {
  437.  
  438.     /* do nothing if invalid ANSI string. */
  439.  
  440.   } else {
  441.     argptr = tempptr = 0;
  442.     ptr = 2;
  443.     for (count = 0; count < 10; count++)
  444.       args[count] = temp[count] = 0;
  445.     cmd = ansistr[ansiptr - 1];
  446.     ansistr[ansiptr - 1] = 0;
  447.     while ((ansistr[ptr]) && (argptr<10) && (tempptr<10)) {
  448.       if (ansistr[ptr] == ';') {
  449.         temp[tempptr] = 0;
  450.         tempptr = 0;
  451.         args[argptr++] = atoi(temp);
  452.       } else
  453.         temp[tempptr++] = ansistr[ptr];
  454.       ++ptr;
  455.     }
  456.     if (tempptr && (argptr<10)) {
  457.       temp[tempptr]  = 0;
  458.       args[argptr++] = atoi(temp);
  459.     }
  460.     if ((cmd >= 'A') && (cmd <= 'D') && !args[0])
  461.       args[0] = 1;
  462.     switch (cmd) {
  463.         case 'f':
  464.         case 'H':
  465.           movecsr(args[1] - 1, args[0] - 1);
  466.           break;
  467.         case 'A':
  468.           movecsr(wherex(), wherey() - args[0]);
  469.           break;
  470.         case 'B':
  471.           movecsr(wherex(), wherey() + args[0]);
  472.           break;
  473.         case 'C':
  474.           movecsr(wherex() + args[0], wherey());
  475.           break;
  476.         case 'D':
  477.           movecsr(wherex() - args[0], wherey());
  478.           break;
  479.         case 's':
  480.           oldx = wherex();
  481.           oldy = wherey();
  482.           break;
  483.         case 'u':
  484.           movecsr(oldx, oldy);
  485.           break;
  486.         case 'J':
  487.           if (args[0] == 2)
  488.             clrscrb();
  489.           break;
  490.         case 'k':
  491.         case 'K':
  492.           ox = wherex();
  493.           oy = wherey();
  494.           _CX = 80 - ox;
  495.           _AH = 0x09;
  496.           _BH = 0x00;
  497.           _AL = 32;
  498.           _BL = curatr;
  499.           my_video_int();
  500.           movecsr(ox, oy);
  501.           break;
  502.         case 'm':
  503.           if (!argptr) {
  504.             argptr = 1;
  505.             args[0] = 0;
  506.           }
  507.           for (count = 0; count < argptr; count++)
  508.             switch (args[count]) {
  509.               case 0: curatr = 0x07; break;
  510.               case 1: curatr = curatr | 0x08; break;
  511.               case 4: break;
  512.               case 5: curatr = curatr | 0x80; break;
  513.               case 7:
  514.                 ptr = curatr & 0x77;
  515.                 curatr = (curatr & 0x88) | (ptr << 4) | (ptr >> 4);
  516.                 break;
  517.               case 8: curatr = 0; break;
  518.               default:
  519.                 if ((args[count] >= 30) && (args[count] <= 37))
  520.                   setfgc(clrlst[args[count] - 30] - '0');
  521.                 else if ((args[count] >= 40) && (args[count] <= 47))
  522.                   setbgc(clrlst[args[count] - 40] - '0');
  523.             }
  524.           break;
  525.       }
  526.     }
  527.   ansiptr = 0;
  528. }
  529.  
  530.  
  531.  
  532. void outchr(char c)
  533. /* This function outputs one character to the screen, and if output to the
  534.  * com port is enabled, the character is output there too.  ANSI graphics
  535.  * are also trapped here, and the ansi function is called to execute the
  536.  * ANSI codes
  537.  */
  538. {
  539.   int i, i1;
  540.  
  541.   if (change_color) {
  542.     change_color = 0;
  543.     if ((c >= '0') && (c <= '7'))
  544.       ansic(c - '0');
  545.     return;
  546.   }
  547.   if (c == 3) {
  548.     change_color = 1;
  549.     return;
  550.   }
  551.   if (chatcall && !(syscfg.sysconfig & sysconfig_no_beep))
  552.     setbeep(1);
  553.   if ((c == 10) && endofline[0]) {
  554.     if (!in_extern)
  555.       outstr(endofline);
  556.     endofline[0] = 0;
  557.   }
  558.  
  559.   if (global_handle)
  560.     if (echo)
  561.       write(global_handle,&c,1);
  562.  
  563.   if (outcom && (c != 9))
  564.     outcomch(echo ? c : 'X');
  565.   if (ansiptr) {
  566.     ansistr[ansiptr++] = c;
  567.     ansistr[ansiptr]   = 0;
  568.     if ((((c < '0') || (c > '9')) && (c!='[') && (c!=';')) ||
  569.         (ansistr[1] != '[') || (ansiptr>75))
  570.       execute_ansi();
  571.   } else if (c == 27) {
  572.     ansistr[0] = 27;
  573.     ansiptr = 1;
  574.     ansistr[ansiptr]=0;
  575.   } else if (c == 9) {
  576.     i1 = wherex();
  577.     for (i = i1; i< (((i1 / 8) + 1) * 8); i++)
  578.       outchr(32);
  579.   } else if (echo || lecho) {
  580.     out1ch(c);
  581.     if (c == 10) {
  582.       ++lines_listed;
  583.       if ((sysstatus_pause_on_page & thisuser.sysstatus) &&
  584.           (lines_listed >= screenlinest - 1)) {
  585.         pausescr();
  586.         lines_listed = 0;
  587.       }
  588.     }
  589.   } else
  590.     out1ch('X');
  591.   if (chatcall)
  592.     setbeep(0);
  593. }
  594.  
  595.  
  596.  
  597. void outstr(char *s)
  598. /* This function outputs a string of characters to the screen (and remotely
  599.  * if applicable).  The com port is also checked first to see if a remote
  600.  * user has hung up
  601.  */
  602. {
  603.   int i=0;
  604.  
  605.   checkhangup();
  606.   if (!hangup)
  607.     while (s[i])
  608.       outchr(s[i++]);
  609. }
  610.  
  611.  
  612.  
  613. void nl()
  614. /* This function performs a CR/LF sequence to move the cursor to the next
  615.  * line.  If any end-of-line ANSI codes are set (such as changing back to
  616.  * the default color) are specified, those are executed first.
  617.  */
  618. {
  619.   if (endofline[0]) {
  620.     outstr(endofline);
  621.     endofline[0] = 0;
  622.   }
  623.   outstr("\r\n");
  624. }
  625.  
  626.  
  627.  
  628. void backspace()
  629. /* This function executes a backspace, space, backspace sequence. */
  630. {
  631.   int i;
  632.  
  633.   i = echo;
  634.   echo = 1;
  635.   outstr("\b \b");
  636.   echo = i;
  637. }
  638.  
  639.  
  640.  
  641. void setc(unsigned char ch)
  642. /* This sets the current color (both locally and remotely) to that
  643.  * specified (in IBM format).
  644.  */
  645. {
  646.   char s[30];
  647.  
  648.   makeansi(ch, s, 0);
  649.   outstr(s);
  650. }
  651.  
  652.  
  653.  
  654. void pausescr()
  655. /* This will pause output, displaying the [PAUSE] message, and wait for
  656.  * a key to be hit.
  657.  */
  658. {
  659.   int i;
  660.  
  661.   if (okansi()) {
  662.     i = curatr;
  663.     setc((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[3] :
  664.           thisuser.bwcolors[3]);
  665.     outstr("[PAUSE]\x1b[7D");
  666.     setc(i);
  667.     getkey();
  668.     outstr("       \x1b[7D");
  669.   } else {
  670.     outstr("[PAUSE]");
  671.     getkey();
  672.     for (i = 0; i < 7; i++)
  673.       backspace();
  674.   }
  675. }
  676.  
  677.  
  678. void npr(char *fmt, ...)
  679. /* just like printf, only out to the com port */
  680. {
  681.   va_list ap;
  682.   char s[512];
  683.  
  684.   va_start(ap, fmt);
  685.   vsprintf(s, fmt, ap);
  686.   va_end(ap);
  687.   outstr(s);
  688. }
  689.  
  690.  
  691. void pl(char *s)
  692. {
  693.   outstr(s);
  694.   nl();
  695. }
  696.  
  697.  
  698. int kbhitb()
  699. {
  700.   union REGS r;
  701.  
  702.   r.h.ah = 1;
  703.   int86(0x16, &r, &r);
  704.   return((r.x.flags & 64) == 0);
  705. }
  706.  
  707.  
  708. int empty()
  709. {
  710.   if (kbhitb() || (incom && (head != tail)) ||
  711.       (charbufferpointer && charbuffer[charbufferpointer]) ||
  712.       (in_extern == 2))
  713.     return(0);
  714.   return(1);
  715. }
  716.  
  717.  
  718.  
  719. void skey1(char *ch)
  720. {
  721.   char c;
  722.  
  723.   c = *ch;
  724.   if (c == 127)
  725.     c = 8;
  726.   if (okskey)
  727.     switch(c) {
  728.       case 1:
  729.       case 4:
  730.       case 6:
  731.         if (okmacro && !charbufferpointer) {
  732.           if (c == 1)
  733.             c = 2;
  734.           else if (c == 4)
  735.             c = 0;
  736.           else if (c == 6)
  737.             c = 1;
  738.           strcpy(charbuffer, &(thisuser.macros[c][0]));
  739.           c = charbuffer[0];
  740.           if (c)
  741.             charbufferpointer = 1;
  742.         }
  743.         break;
  744.       case 15:
  745.         if (helpl && !ihelp && !chatting && echo) {
  746.           ihelp = 1;
  747.           print_help(helpl);
  748.           ihelp = 0;
  749.         }
  750.         break;
  751.       case 20:
  752.         if (echo)
  753.           ptime();
  754.         break;
  755.       case 18:
  756.         if (echo)
  757.           reprint();
  758.         break;
  759.     }
  760.   *ch = c;
  761. }
  762.  
  763. char getchd()
  764. {
  765.   union REGS r;
  766.  
  767.   r.h.ah = 0x07;
  768.   int86(INT_SAVE_21, &r, &r);
  769.   return(r.h.al);
  770. }
  771.  
  772.  
  773. char getchd1()
  774. {
  775.   union REGS r;
  776.  
  777.   r.h.ah = 0x06;
  778.   r.h.dl = 0xFF;
  779.   int86(INT_SAVE_21, &r, &r);
  780.   return((r.x.flags & 0x40) ? 255 : r.h.al);
  781. }
  782.  
  783.  
  784. char inkey()
  785. /* This function checks both the local keyboard, and the remote terminal
  786.  * (if any) for input.  If there is input, the key is returned.  If there
  787.  * is no input, a zero is returned.  Function keys hit are interpreted as
  788.  * such within the routine and not returned.
  789.  */
  790. {
  791.   char ch=0;
  792.  
  793.   if (charbufferpointer) {
  794.     if (!charbuffer[charbufferpointer])
  795.       charbufferpointer = charbuffer[0] = 0;
  796.     else
  797.       return(charbuffer[charbufferpointer++]);
  798.   }
  799.   if (kbhitb() || (in_extern == 2)) {
  800.     ch = getchd1();
  801.     lastcon = 1;
  802.     if (!ch) {
  803.       if (in_extern)
  804.         in_extern = 2;
  805.       else {
  806.         ch = getchd1();
  807.         skey(ch);
  808.         ch = (((ch == 68) || (ch==103)) ? 2 : 0);
  809.       }
  810.     } else if (in_extern)
  811.       in_extern = 1;
  812.     timelastchar1=timer1();
  813.   } else if (incom && comhit()) {
  814.     ch = (get1c() & andwith);
  815.     lastcon = 0;
  816.   }
  817.   skey1(&ch);
  818.   return(ch);
  819. }
  820.  
  821.  
  822.  
  823. void mpl(int i)
  824. /* This will make a reverse-video prompt line i characters long, repositioning
  825.  * the cursor at the beginning of the input prompt area.  Of course, if the
  826.  * user does not want ansi, this routine does nothing.
  827.  */
  828. {
  829.   int i1;
  830.   char s[81];
  831.  
  832.   if (okansi()) {
  833.     ansic(4);
  834.     for (i1 = 0; i1 < i; i1++)
  835.       outchr(' ');
  836.     outstr("\x1b[");
  837.     itoa(i,s,10);
  838.     outstr(s);
  839.     outstr("D");
  840.   }
  841. }
  842.  
  843.  
  844.  
  845. char upcase(char ch)
  846. /* This converts a character to uppercase */
  847. {
  848.   if ((ch > '`') && (ch < '{'))
  849.     ch = ch - 32;
  850.   return(ch);
  851. }
  852.  
  853.  
  854. unsigned char getkey()
  855. /* This function returns one character from either the local keyboard or
  856.  * remote com port (if applicable).  After 1.5 minutes of inactivity, a
  857.  * beep is sounded.  After 3 minutes of inactivity, the user is hung up.
  858.  */
  859. {
  860.   unsigned char ch;
  861.   int beepyet;
  862.   long dd,tv,tv1;
  863.  
  864.   beepyet = 0;
  865.   timelastchar1=timer1();
  866.  
  867.   if (so())
  868.     tv=10920L;
  869.   else
  870.     tv=3276L;
  871.  
  872.   tv1=tv/2;
  873.  
  874.   lines_listed = 0;
  875.   do {
  876.     while (empty() && !hangup) {
  877.       dd = timer1();
  878.       if (labs(dd - timelastchar1) > 65536L)
  879.         timelastchar1 -= 1572480L;
  880.       if (((dd - timelastchar1) > tv1) && (!beepyet)) {
  881.         beepyet = 1;
  882.         outchr(7);
  883.       }
  884.       if (labs(dd - timelastchar1) > tv) {
  885.         nl();
  886.         outstr("Call back later when you are there.");
  887.         nl();
  888.         hangup = 1;
  889.       }
  890.       checkhangup();
  891.     }
  892.     ch = inkey();
  893.   } while (!ch && !in_extern && !hangup);
  894.   if (checkit && (ch > 127)) {
  895.     checkit = 0;
  896.     ch = ch & (andwith = 0x7F);
  897.   }
  898.   return(ch);
  899. }
  900.  
  901.  
  902.  
  903. void input1(char *s, int maxlen, int lc, int crend)
  904. /* This will input a line of data, maximum maxlen characters long, terminated
  905.  * by a C/R.  if (lc) is non-zero, lowercase is allowed, otherwise all
  906.  * characters are converted to uppercase.
  907.  */
  908. {
  909.   int curpos=0, done=0, in_ansi=0;
  910.   unsigned char ch;
  911.  
  912.   while (!done && !hangup) {
  913.     ch = getkey();
  914.     if (in_ansi) {
  915.       if ((in_ansi==1) && (ch!='['))
  916.         in_ansi=0;
  917.       else {
  918.         if (in_ansi==1)
  919.           in_ansi=2;
  920.         else if (((ch<'0') || (ch>'9')) && (ch!=';'))
  921.           in_ansi=3;
  922.         else
  923.           in_ansi=2;
  924.       }
  925.     }
  926.     if (!in_ansi) {
  927.       if (ch > 31) {
  928.         if (curpos < maxlen) {
  929.           if (!lc)
  930.             ch = upcase(ch);
  931.           s[curpos++] = ch;
  932.           outchr(ch);
  933.         }
  934.       } else
  935.         switch(ch) {
  936.           case 14:
  937.           case 13:
  938.             s[curpos] = 0;
  939.             done = echo = 1;
  940.             if (crend)
  941.               nl();
  942.             break;
  943.           case 23: /* Ctrl-W */
  944.             if (curpos) {
  945.               do {
  946.                 curpos--;
  947.                 backspace();
  948.                 if (s[curpos]==26)
  949.                   backspace();
  950.               } while ((curpos) && (s[curpos-1]!=32));
  951.             }
  952.             break;
  953.           case 26:
  954.             if (input_extern) {
  955.               s[curpos++] = 26;
  956.               outstr("^Z");
  957.             }
  958.             break;
  959.           case 8:
  960.             if (curpos) {
  961.               curpos--;
  962.               backspace();
  963.               if (s[curpos] == 26)
  964.                 backspace();
  965.             }
  966.             break;
  967.           case 21:
  968.           case 24:
  969.             while (curpos) {
  970.               curpos--;
  971.               backspace();
  972.               if (s[curpos] == 26)
  973.                 backspace();
  974.             }
  975.             break;
  976.           case 27:
  977.             in_ansi=1;
  978.             break;
  979.         }
  980.     }
  981.     if (in_ansi==3)
  982.       in_ansi=0;
  983.   }
  984.   if (hangup)
  985.     s[0] = 0;
  986. }
  987.  
  988.  
  989.  
  990. void input(char *s, int len)
  991. /* This will input an upper-case string */
  992. {
  993.   input1(s, len, 0, 1);
  994. }
  995.  
  996.  
  997.  
  998. void inputl(char *s, int len)
  999. /* This will input an upper or lowercase string of characters */
  1000. {
  1001.   input1(s, len, 1, 1);
  1002. }
  1003.  
  1004.  
  1005.  
  1006. int yn()
  1007. /* The keyboard is checked for either a Y, N, or C/R to be hit.  C/R is
  1008.  * assumed to be the same as a N.  Yes or No is output, and yn is set to
  1009.  * zero if No was returned, and yn() is non-zero if Y was hit.
  1010.  */
  1011. {
  1012.   char ch=0;
  1013.  
  1014.   ansic(1);
  1015.   while (!hangup && ((ch = upcase(getkey())) != 'Y') && (ch != 'N') && (ch != 13))
  1016.     ;
  1017.   outstr((ch == 'Y') ? "Yes" : "No");
  1018.   nl();
  1019.   return(ch == 'Y');
  1020. }
  1021.  
  1022.  
  1023.  
  1024. int ny()
  1025. /* This is the same as yn(), except C/R is assumed to be "Y" */
  1026. {
  1027.   char ch=0;
  1028.  
  1029.   ansic(1);
  1030.   while (!hangup && ((ch = upcase(getkey())) != 'Y') && (ch != 'N') && (ch != 13))
  1031.     ;
  1032.   outstr((ch == 'N') ? "No" : "Yes");
  1033.   nl();
  1034.   return((ch == 'Y') || (ch==13));
  1035. }
  1036.  
  1037.  
  1038. void ansic(int n)
  1039. {
  1040.   char c;
  1041.  
  1042.   c = ((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[n] :
  1043.         thisuser.bwcolors[n]);
  1044.   if (c == curatr)
  1045.     return;
  1046.   setc(c);
  1047.   makeansi((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[0] :
  1048.         thisuser.bwcolors[0],endofline, 0);
  1049. }
  1050.  
  1051.  
  1052. char onek(char *s)
  1053. {
  1054.   char ch;
  1055.  
  1056.   while (!strchr(s, ch = upcase(getkey())) && !hangup)
  1057.     ;
  1058.   if (hangup)
  1059.     ch = s[0];
  1060.   outchr(ch);
  1061.   nl();
  1062.   return(ch);
  1063. }
  1064.  
  1065.  
  1066. void prt(int i, char *s)
  1067. {
  1068.   ansic(i);
  1069.   outstr(s);
  1070.   ansic(0);
  1071. }
  1072.