home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / RETURN.C < prev    next >
C/C++ Source or Header  |  1992-12-03  |  47KB  |  2,151 lines

  1. /*****************************************************************************
  2.  
  3.                                 WWIV Version 4
  4.                     Copyright (C) 1988-1993 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 <process.h>
  18. #include <math.h>
  19. #include <dir.h>
  20. #include <stdio.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys\stat.h>
  26. #include <dos.h>
  27. #include <alloc.h>
  28. #include <time.h>
  29.  
  30.  
  31. #include "vardec.h"
  32.  
  33.  
  34. void far *funcs[25];
  35. /****************************************************************************/
  36. void my_video_int(void);
  37. int okansi(void);
  38. void wait1(long l);
  39. void setbeep(int i);
  40. void pla(char *s, int *abort);
  41. void checka(int *abort, int *next);
  42. void inli(char *s, char *rollover, int maxlen, int crend);
  43. long timer1(void);
  44. void movecsr(int x,int y);
  45. int wherex(void);
  46. int wherey(void);
  47. void lf(void);
  48. void cr(void);
  49. void clrscrb(void);
  50. void bs(void);
  51. void out1chx(unsigned char ch);
  52. void out1ch(unsigned char ch);
  53. void far interrupt async_isr(void);
  54. void outcomch(char ch);
  55. char get1c(void);
  56. int comhit(void);
  57. void dump(void);
  58. void set_baud(unsigned int rate);
  59. void initport(int portnum);
  60. void closeport(void);
  61. int cdet(void);
  62. void checkhangup(void);
  63. void addto(char *s, int i);
  64. void makeansi(unsigned char attr, char *s, int forceit);
  65. void setfgc(int i);
  66. void setbgc(int i);
  67. void execute_ansi(void);
  68. void outchr(char c);
  69. void outstr(char *s);
  70. void nl(void);
  71. void backspace(void);
  72. void setc(unsigned char ch);
  73. void pausescr(void);
  74. void pl(char *s);
  75. int kbhitb(void);
  76. int empty(void);
  77. void skey1(char *ch);
  78. char getchd(void);
  79. char getchd1(void);
  80. char inkey(void);
  81. void mpl(int i);
  82. char upcase(char ch);
  83. unsigned char getkey(void);
  84. void input1(char *s, int maxlen, int lc, int crend);
  85. void input(char *s, int len);
  86. void inputl(char *s, int len);
  87. int yn(void);
  88. int ny(void);
  89. void ansic(int n);
  90. char onek(char *s);
  91. void prt(int i, char *s);
  92. unsigned char getkeyext(void);
  93. int do_it(char *cl);
  94. int do_remote(char *s, int ccc);
  95. void checka2(void);
  96. void outdosstr(char *s);
  97. int full_external(char *s, int ccc);
  98. int init_r(void);
  99. void get_dir(char *s, int be);
  100. void cd_to(char *s);
  101. void setup_stuff(void);
  102. void main(int argc, char *argv[]);
  103.  
  104. /****************************************************************************/
  105. char *xenviron[50],newprompt[161];
  106. char ver_no1[51], ver_no2[51];
  107.  
  108.  
  109. int topline=0,screenbottom=24,tempio,curatr=0x07,lines_listed=0,outcom=0;
  110. int hangup=0,hungup=0,echo=1,lecho=1,input_extern=0,ctc=0,ccc=0;
  111. int defscreenbottom=24;
  112.  
  113. char charbuffer[161]="",endofline[81]="";
  114. int charbufferpointer=0;
  115. int ansiptr=0;
  116.  
  117. int oldx,oldy,flow_control,save_dos;
  118. char ansistr[81];
  119. int change_color;
  120. userrec thisuser;
  121.  
  122. unsigned int baud_rate;
  123.  
  124. long timelastchar1,hanguptime1;
  125.  
  126. unsigned char andwith=255;
  127.  
  128. char cdir[81];
  129.  
  130.  
  131. volatile int head,tail;
  132. volatile char buffer[max_buf];
  133. int base,async_irq,useron,in_extern,ok_modem_stuff;
  134. int outcom,incom,using_modem,chatting,screenlinest;
  135.  
  136. extern char *wwiv_version;
  137.  
  138. /****************************************************************************/
  139.  
  140. void my_video_int(void)
  141. {
  142. #if __TURBOC__ >= 0x0200
  143.   /* TC 2.0 or TC++ here */
  144.   static unsigned short sav_bp;
  145.  
  146.   __emit__(0x56, 0x57); /* push si, push di */
  147.   sav_bp = _BP;
  148.   geninterrupt(0x10);
  149.   _BP = sav_bp;
  150.   __emit__(0x5f, 0x5e); /* pop di, pop si */
  151. #else
  152.   /* TC 1.5 here */
  153.   _VideoInt();
  154. #endif
  155. }
  156.  
  157.  
  158. /****************************************************************************/
  159.  
  160. int okansi(void)
  161. /* This function checks the status of the current user's record to see if
  162.  * the user has specified that he wants ANSI graphics displayed.
  163.  */
  164. {
  165.   if (thisuser.sysstatus & sysstatus_ansi)
  166.     return(1);
  167.   else
  168.     return(0);
  169. }
  170.  
  171.  
  172. void wait1(long l)
  173. {
  174.   long l1;
  175.  
  176.   l1 = timer1()+l;
  177.  
  178.   enable();
  179.   while (timer1()<l1)
  180.     ;
  181. }
  182.  
  183.  
  184.  
  185.  
  186. #define frequency 500
  187.  
  188. void setbeep(int i)
  189. {
  190.   int i1,i2;
  191.  
  192.   if (i) {
  193.     i1 = 0x34DD / frequency;
  194.     i2 = inportb(0x61);
  195.     if (!(i2 & 0x03)) {
  196.       outportb(0x61, i2 | 0x03);
  197.       outportb(0x43, 0xB6);
  198.     }
  199.     outportb(0x42, i1 & 0x0F);
  200.     outportb(0x42, i1 >> 4);
  201.   } else
  202.     outportb(0x61, inportb(0x61) & 0xFC);
  203. }
  204.  
  205. void pla(char *s, int *abort)
  206. {
  207.   int i,next;
  208.  
  209.   i=0;
  210.   checkhangup();
  211.   if (hangup)
  212.     *abort=1;
  213.   checka(abort,&next);
  214.   while ((s[i]) && (!(*abort))) {
  215.     outchr(s[i++]);
  216.     checka(abort,&next);
  217.   }
  218.   if (!(*abort))
  219.     nl();
  220. }
  221.  
  222.  
  223. void checka(int *abort, int *next)
  224. {
  225.   char ch;
  226.  
  227.   while ((!empty()) && (!(*abort)) && (!hangup)) {
  228.     checkhangup();
  229.     ch=inkey();
  230.     switch(ch) {
  231.       case 14:
  232.         *next=1;
  233.       case 3:
  234.       case 32:
  235.       case 24:
  236.         *abort=1;
  237.         break;
  238.       case 'P':
  239.       case 'p':
  240.       case 19:
  241.         ch=getkey();
  242.         break;
  243.     }
  244.   }
  245. }
  246.  
  247.  
  248.  
  249. void inli(char *s, char *rollover, int maxlen, int crend)
  250. {
  251.   int cp,i,i1,done,cm,begx;
  252.   char s1[255];
  253.   unsigned char ch;
  254.  
  255.   cm=chatting;
  256.  
  257.   begx=wherex();
  258.   if (rollover[0]!=0) {
  259.     if (charbufferpointer) {
  260.       strcpy(s1,rollover);
  261.       strcat(s1,&charbuffer[charbufferpointer]);
  262.       strcpy(&charbuffer[1],s1);
  263.       charbufferpointer=1;
  264.     } else {
  265.       strcpy(&charbuffer[1],rollover);
  266.       charbufferpointer=1;
  267.     }
  268.     rollover[0]=0;
  269.   }
  270.   cp=0;
  271.   done=0;
  272.   do {
  273.     ch=getkey();
  274.     if (cm)
  275.       if (chatting==0)
  276.         ch=13;
  277.     if ((ch>=32)) {
  278.       if ((wherex()<(thisuser.screenchars-1)) && (cp<maxlen)) {
  279.         s[cp++]=ch;
  280.         outchr(ch);
  281.         if (wherex()==(thisuser.screenchars-1))
  282.           done=1;
  283.       } else {
  284.         if (wherex()>=(thisuser.screenchars-1))
  285.           done=1;
  286.       }
  287.     } else
  288.         switch(ch) {
  289.           case 7:
  290.             if ((chatting) && (outcom))
  291.               outcomch(7);
  292.             break;
  293.           case 13: /* C/R */
  294.             s[cp]=0;
  295.             done=1;
  296.             break;
  297.           case 8:  /* Backspace */
  298.             if (cp) {
  299.               if (s[cp-2]==3) {
  300.                 cp-=2;
  301.                 ansic(0);
  302.               } else
  303.                 if (s[cp-1]==8) {
  304.                   cp--;
  305.                   outchr(32);
  306.                 } else {
  307.                   cp--;
  308.                   backspace();
  309.                 }
  310.             }
  311.             break;
  312.           case 24: /* Ctrl-X */
  313.             while (wherex()>begx) {
  314.               backspace();
  315.               cp=0;
  316.             }
  317.             ansic(0);
  318.             break;
  319.           case 23: /* Ctrl-W */
  320.             if (cp) {
  321.               do {
  322.                 if (s[cp-2]==3) {
  323.                   cp-=2;
  324.                   ansic(0);
  325.                 } else
  326.                   if (s[cp-1]==8) {
  327.                     cp--;
  328.                     outchr(32);
  329.                   } else {
  330.                     cp--;
  331.                     backspace();
  332.                   }
  333.               } while ((cp) && (s[cp-1]!=32) && (s[cp-1]!=8) && (s[cp-2]!=3));
  334.             }
  335.             break;
  336.           case 14: /* Ctrl-N */
  337.             if ((wherex()) && (cp<maxlen)) {
  338.               outchr(8);
  339.               s[cp++]=8;
  340.             }
  341.             break;
  342.           case 16: /* Ctrl-P */
  343.             if (cp<maxlen-1) {
  344.               ch=getkey();
  345.               if ((ch>='0') && (ch<='7')) {
  346.                 s[cp++]=3;
  347.                 s[cp++]=ch;
  348.                 ansic(ch-'0');
  349.               }
  350.             }
  351.             break;
  352.           case 9:  /* Tab */
  353.             i=5-(cp % 5);
  354.             if (((cp+i)<maxlen) && ((wherex()+i)<thisuser.screenchars)) {
  355.               i=5-((wherex()+1) % 5);
  356.               for (i1=0; i1<i; i1++) {
  357.                 s[cp++]=32;
  358.                 outchr(32);
  359.               }
  360.             }
  361.             break;
  362.         }
  363.   } while ((done==0) && (hangup==0));
  364.   if (ch!=13) {
  365.     i=cp-1;
  366.     while ((i>0) && (s[i]!=32) && (s[i]!=8) || (s[i-1]==3))
  367.       i--;
  368.     if ((i>(wherex()/2)) && (i!=(cp-1))) {
  369.       i1=cp-i-1;
  370.       for (i=0; i<i1; i++)
  371.         outchr(8);
  372.       for (i=0; i<i1; i++)
  373.         outchr(32);
  374.       for (i=0; i<i1; i++)
  375.         rollover[i]=s[cp-i1+i];
  376.       rollover[i1]=0;
  377.       cp -= i1;
  378.     }
  379.     s[cp++]=1;
  380.     s[cp]=0;
  381.   }
  382.   if (crend)
  383.     nl();
  384.  
  385. }
  386.  
  387.  
  388.  
  389.  
  390. long timer1(void)
  391. /* This function returns the time, in seconds since midnight. */
  392. {
  393.   unsigned short h,m;
  394.   long l;
  395.  
  396.   m=peek(0x0040,0x006c);
  397.   h=peek(0x0040,0x006e);
  398.   l=((long)h)*65536 + ((long)m);
  399.   return(l);
  400. }
  401.  
  402. #define SCROLL_UP(t,b,l) \
  403.   _CH=t;\
  404.   _DH=b;\
  405.   _BH=curatr;\
  406.   _AL=l;\
  407.   _CL=0;\
  408.   _DL=79;\
  409.   _AH=6;\
  410.   my_video_int();
  411.  
  412. void movecsr(int x,int y)
  413. /* This, obviously, moves the cursor to the location specified, offset from
  414.  * the protected dispaly at the top of the screen
  415.  */
  416. {
  417.   if (x<0)
  418.     x=0;
  419.   if (x>79)
  420.     x=79;
  421.   if (y<0)
  422.     y=0;
  423.   y+=topline;
  424.   if (y>screenbottom)
  425.     y=screenbottom;
  426.  
  427.   _BH=0x00;
  428.   _DH=y;
  429.   _DL=x;
  430.   _AH=0x02;
  431.   my_video_int();
  432. }
  433.  
  434.  
  435.  
  436. int wherex(void)
  437. /* This function returns the current X cursor position, as the number of
  438.  * characters from the left hand side of the screen.  An X position of zero
  439.  * means the cursor is at the left-most position
  440.  */
  441. {
  442.   _BH=0x00;
  443.   _AH=0x03;
  444.   my_video_int();
  445.   tempio=_DL;
  446.   return(tempio);
  447. }
  448.  
  449.  
  450.  
  451. int wherey(void)
  452. /* This function returns the Y cursor position, as the line number from
  453.  * the top of the logical window.  The offset due to the protected top
  454.  * of the screen display is taken into account.  A wherey() of zero means
  455.  * the cursor is at the top-most position it can be at.
  456.  */
  457. {
  458.   _BH=0x00;
  459.   _AH=0x03;
  460.   my_video_int();
  461.   tempio=_DH;
  462.   return(tempio-topline);
  463. }
  464.  
  465.  
  466.  
  467. void lf(void)
  468. /* This function performs a linefeed to the screen (but not remotely) by
  469.  * either moving the cursor down one line, or scrolling the logical screen
  470.  * up one line.
  471.  */
  472. {
  473.   _BH=0x00;
  474.   _AH=0x03;
  475.   my_video_int();
  476.   tempio=_DL;
  477.   if (_DH==screenbottom) {
  478.     SCROLL_UP(topline,screenbottom,1);
  479.     _DL=tempio;
  480.     _DH=screenbottom;
  481.     _BH=0;
  482.     _AH=0x02;
  483.     my_video_int();
  484.   } else {
  485.     tempio=_DH+1;
  486.     _DH=tempio;
  487.     _AH=0x02;
  488.     my_video_int();
  489.   }
  490. }
  491.  
  492.  
  493.  
  494. void cr(void)
  495. /* This short function returns the local cursor to the left-most position
  496.  * on the screen.
  497.  */
  498. {
  499.   _BH=0x00;
  500.   _AH=0x03;
  501.   my_video_int();
  502.   _DL=0x00;
  503.   _AH=2;
  504.   my_video_int();
  505. }
  506.  
  507. void clrscrb(void)
  508. /* This clears the local logical screen */
  509. {
  510.   SCROLL_UP(topline,screenbottom,0);
  511.   movecsr(0,0);
  512.   lines_listed=0;
  513. }
  514.  
  515.  
  516.  
  517. void bs(void)
  518. /* This function moves the cursor one position to the left, or if the cursor
  519.  * is currently at its left-most position, the cursor is moved to the end of
  520.  * the previous line, except if it is on the top line, in which case nothing
  521.  * happens.
  522.  */
  523. {
  524.   _BH=0;
  525.   _AH=3;
  526.   my_video_int();
  527.   if (_DL==0) {
  528.     if (_DH != topline) {
  529.       _DL=79;
  530.       tempio=_DH-1;
  531.       _DH=tempio;
  532.       _AH=2;
  533.       my_video_int();
  534.     }
  535.   } else {
  536.     _DL--;
  537.     _AH=2;
  538.     my_video_int();
  539.   }
  540. }
  541.  
  542.  
  543.  
  544. void out1chx(unsigned char ch)
  545. /* This function outputs one character to the screen, then updates the
  546.  * cursor position accordingly, scolling the screen if necessary.  Not that
  547.  * this function performs no commands such as a C/R or L/F.  If a value of
  548.  * 8, 7, 13, 10, 12 (backspace, beep, C/R, L/F, TOF), or any other command-
  549.  * type characters are passed, the appropriate corresponding "graphics"
  550.  * symbol will be output to the screen as a normal character.
  551.  */
  552. {
  553.   _BL=curatr;
  554.   _BH=0x00;
  555.   _CX=0x01;
  556.   _AL=ch;
  557.   _AH=0x09;
  558.   my_video_int();
  559.   _BH=0x00;
  560.   _AH=0x03;
  561.   my_video_int();
  562.   ++_DL;
  563.   if (_DL==80) {
  564.     _DL=0;
  565.     if (_DH==screenbottom) {
  566.       SCROLL_UP(topline,screenbottom,1);
  567.       _DH=screenbottom;
  568.       _DL=0;
  569.       _BH=0;
  570.       _AH=0x02;
  571.       my_video_int();
  572.     } else {
  573.       tempio=_DH+1;
  574.       _DH=tempio;
  575.       _AH=0x02;
  576.       my_video_int();
  577.     }
  578.   } else {
  579.     _AH=0x02;
  580.     my_video_int();
  581.   }
  582. }
  583.  
  584.  
  585.  
  586.  
  587. void out1ch(unsigned char ch)
  588. /* This function outputs one character to the local screen.  C/R, L/F, TOF,
  589.  * BS, and BELL are interpreted as commands instead of characters.
  590.  */
  591. {
  592.   if (ch>31)
  593.     out1chx(ch);
  594.   else
  595.     if (ch==13)
  596.       cr();
  597.     else
  598.       if (ch==10)
  599.         lf();
  600.       else
  601.         if (ch==12)
  602.           clrscrb();
  603.         else
  604.           if (ch==8)
  605.             bs();
  606.           else
  607.             if (ch==7)
  608.               if (outcom==0) {
  609.                 setbeep(1);
  610.                 wait1(4);
  611.                 setbeep(0);
  612.               }
  613. }
  614.  
  615.  
  616.  
  617. /****************************************************************************/
  618. void far interrupt async_isr(void)
  619. /* This function is called every time a char is received on the com port.
  620.  * The character is stored in the buffer[] array, and the head pointer is
  621.  * updated.
  622.  */
  623. {
  624.   buffer[head++] = inportb(base);
  625.   if (head == max_buf)
  626.     head = 0;
  627.   outportb(0x20, 0x20);
  628. }
  629.  
  630.  
  631.  
  632. void outcomch(char ch)
  633. /* This function outputs one character to the com port */
  634. {
  635.   while (!(inportb(base + 5) & 0x20))
  636.     ;
  637.   if (flow_control)
  638.     while (!(inportb(base + 6) & 0x10))
  639.       ;
  640.   outportb(base, ch);
  641. }
  642.  
  643.  
  644.  
  645.  
  646. char get1c(void)
  647. /* This function returns one character from the com port, or a zero if
  648.  * no character is waiting
  649.  */
  650. {
  651.   char c1;
  652.  
  653.   if (head != tail) {
  654.     disable();
  655.     c1 = buffer[tail++];
  656.     if (tail == max_buf)
  657.       tail = 0;
  658.     enable();
  659.     return(c1);
  660.   } else
  661.     return(0);
  662. }
  663.  
  664.  
  665.  
  666. int comhit(void)
  667. /* This returns a value telling if there is a character waiting in the com
  668.  * buffer.
  669.  */
  670. {
  671.   return(head != tail);
  672. }
  673.  
  674.  
  675.  
  676. void dump(void)
  677. /* This function clears the com buffer */
  678. {
  679.   disable();
  680.   head = tail = 0;
  681.   enable();
  682. }
  683.  
  684.  
  685.  
  686. void set_baud(unsigned int rate)
  687. /* This function sets the com speed to that passed */
  688. {
  689.   float rl;
  690.  
  691.   if ((rate > 49) && (rate < 57601)) {
  692.     rl   = 115200.0 / ((float) rate);
  693.     rate = (int) rl;
  694.     outportb(base + 3, inportb(base + 3) | 0x80);
  695.     outportb(base,     (rate & 0x00FF));
  696.     outportb(base + 1, ((rate >> 8) & 0x00FF));
  697.     outportb(base + 3, inportb(base + 3) & 0x7F);
  698.   }
  699. }
  700.  
  701.  
  702. void initport(int portnum)
  703. /* This function initializes the com buffer, setting up the interrupt,
  704.  * and com parameters
  705.  */
  706. {
  707.   int temp;
  708.  
  709.   temp=portnum;
  710.  
  711.   setvect(8 + async_irq, async_isr);
  712.   head = tail = 0;
  713.   outportb(base + 3, 0x03);
  714.   disable();
  715.   temp = inportb(base + 5);
  716.   temp = inportb(base);
  717.   temp = inportb(0x21);
  718.   temp = temp & ((1 << async_irq) ^ 0x00FF);
  719.   outportb(0x21, temp);
  720.   outportb(base + 1, 0x01);
  721.   temp=inportb(base + 4);
  722.   outportb(base + 4, temp | 0x0B);
  723.   enable();
  724.   set_baud(baud_rate);
  725. }
  726.  
  727.  
  728.  
  729. void closeport(void)
  730. /* This function closes out the com port, removing the interrupt routine,
  731.  * etc.
  732.  */
  733. {
  734.   int temp;
  735.  
  736.   disable();
  737.   temp = inportb(0x21);
  738.   temp = temp | ((1 << async_irq));
  739.   outportb(0x21, temp);
  740.   outportb(base + 2, 0);
  741.   outportb(base + 4, 3);
  742.   setvect(async_irq+8,getvect(8)); /* for desqview */
  743.   enable();
  744. }
  745.  
  746.  
  747. int cdet(void)
  748. /* This returns the status of the carrier detect lead from the modem */
  749. {
  750.   return((inportb(base + 6) & 0x80) ? 1 : 0);
  751. }
  752.  
  753.  
  754.  
  755. void checkhangup(void)
  756. /* This function checks to see if the user logged on to the com port has
  757.  * hung up.  Obviously, if no user is logged on remotely, this does nothing.
  758.  * If carrier detect is detected to be low, it is checked 100 times
  759.  * sequentially to make sure it stays down, and is not just a quirk.
  760.  */
  761. {
  762.   int i, ok;
  763.  
  764.   if (!hangup && using_modem && !cdet()) {
  765.     ok = 0;
  766.     for (i = 0; (i < 500) && !ok; i++)
  767.       if (cdet())
  768.         ok = 1;
  769.     if (!ok) {
  770.       hangup = hungup = 1;
  771.     }
  772.   }
  773. }
  774.  
  775.  
  776.  
  777.  
  778. void addto(char *s, int i)
  779. {
  780.   char temp[20];
  781.  
  782.   if (s[0])
  783.     strcat(s, ";");
  784.   else
  785.     strcpy(s, "\x1B[");
  786.   itoa(i, temp, 10);
  787.   strcat(s, temp);
  788. }
  789.  
  790.  
  791.  
  792. void makeansi(unsigned char attr, char *s, int forceit)
  793. /* Passed to this function is a one-byte attribute as defined for IBM type
  794.  * screens.  Returned is a string which, when printed, will change the
  795.  * display to the color desired, from the current function.
  796.  */
  797. {
  798.   unsigned char catr;
  799.   char *temp = "04261537";
  800.  
  801.   catr = curatr;
  802.   s[0] = 0;
  803.   if (attr != catr) {
  804.     if ((catr & 0x88) ^ (attr & 0x88)) {
  805.       addto(s, 0);
  806.       addto(s, 30 + temp[attr & 0x07] - '0');
  807.       addto(s, 40 + temp[(attr & 0x70) >> 4] - '0');
  808.       catr = attr & 0x77;
  809.     }
  810.     if ((catr & 0x07) != (attr & 0x07))
  811.       addto(s, 30 + temp[attr & 0x07] - '0');
  812.     if ((catr & 0x70) != (attr & 0x70))
  813.       addto(s, 40 + temp[(attr & 0x70) >> 4] - '0');
  814.     if ((catr & 0x08) ^ (attr & 0x08))
  815.       addto(s, 1);
  816.     if ((catr & 0x80) ^ (attr & 0x80))
  817.       addto(s, 5);
  818.   }
  819.   if (s[0])
  820.     strcat(s, "m");
  821.   if (!okansi() && !forceit)
  822.     s[0]=0;
  823. }
  824.  
  825.  
  826.  
  827. void setfgc(int i)
  828. /* This sets the foreground color to that passed.  It is called only from
  829.  * execute_ansi
  830.  */
  831. {
  832.   curatr = (curatr & 0xf8) | i;
  833. }
  834.  
  835.  
  836.  
  837. void setbgc(int i)
  838. /* This sets the background color to that passed.  It is called only from
  839.  * execute_ansi
  840.  */
  841. {
  842.   curatr = (curatr & 0x8f) | (i << 4);
  843. }
  844.  
  845.  
  846. void execute_ansi(void)
  847. /* This function executes an ANSI string to change color, position the
  848.  * cursor, etc.
  849.  */
  850. {
  851.   int args[10], argptr, count, ptr, tempptr, ox, oy;
  852.   char cmd, temp[10], teol[81], *clrlst = "04261537";
  853.  
  854.   if (ansistr[1] != '[') {
  855.  
  856.     /* do nothing if invalid ANSI string. */
  857.  
  858.   } else {
  859.     argptr = tempptr = 0;
  860.     ptr = 2;
  861.     for (count = 0; count < 10; count++)
  862.       args[count] = temp[count] = 0;
  863.     cmd = ansistr[ansiptr - 1];
  864.     ansistr[ansiptr - 1] = 0;
  865.     while (ansistr[ptr]) {
  866.       if (ansistr[ptr] == ';') {
  867.         temp[tempptr] = 0;
  868.         tempptr = 0;
  869.         args[argptr++] = atoi(temp);
  870.       } else
  871.         temp[tempptr++] = ansistr[ptr];
  872.       ++ptr;
  873.     }
  874.     if (tempptr) {
  875.       temp[tempptr]  = 0;
  876.       args[argptr++] = atoi(temp);
  877.     }
  878.     if ((cmd >= 'A') && (cmd <= 'D') && !args[0])
  879.       args[0] = 1;
  880.     switch (cmd) {
  881.         case 'f':
  882.         case 'H':
  883.           movecsr(args[1] - 1, args[0] - 1);
  884.           break;
  885.         case 'A':
  886.           movecsr(wherex(), wherey() - args[0]);
  887.           break;
  888.         case 'B':
  889.           movecsr(wherex(), wherey() + args[0]);
  890.           break;
  891.         case 'C':
  892.           movecsr(wherex() + args[0], wherey());
  893.           break;
  894.         case 'D':
  895.           movecsr(wherex() - args[0], wherey());
  896.           break;
  897.         case 's':
  898.           oldx = wherex();
  899.           oldy = wherey();
  900.           break;
  901.         case 'u':
  902.           movecsr(oldx, oldy);
  903.           break;
  904.         case 'J':
  905.           if (args[0] == 2)
  906.             clrscrb();
  907.           break;
  908.         case 'k':
  909.         case 'K':
  910.           ox = wherex();
  911.           oy = wherey();
  912.           _CX = 80 - ox;
  913.           _AH = 0x09;
  914.           _BH = 0x00;
  915.           _AL = 32;
  916.           _BL = curatr;
  917.           my_video_int();
  918.           movecsr(ox, oy);
  919.           break;
  920.         case 'm':
  921.           if (!argptr) {
  922.             argptr = 1;
  923.             args[0] = 0;
  924.           }
  925.           for (count = 0; count < argptr; count++)
  926.             switch (args[count]) {
  927.               case 0: curatr = 0x07; break;
  928.               case 1: curatr = curatr | 0x08; break;
  929.               case 4: break;
  930.               case 5: curatr = curatr | 0x80; break;
  931.               case 7:
  932.                 ptr = curatr & 0x77;
  933.                 curatr = (curatr & 0x88) | (ptr << 4) | (ptr >> 4);
  934.                 break;
  935.               case 8: curatr = 0; break;
  936.               default:
  937.                 if ((args[count] >= 30) && (args[count] <= 37))
  938.                   setfgc(clrlst[args[count] - 30] - '0');
  939.                 else if ((args[count] >= 40) && (args[count] <= 47))
  940.                   setbgc(clrlst[args[count] - 40] - '0');
  941.             }
  942.           break;
  943.       }
  944.     }
  945.   ansiptr = 0;
  946. }
  947.  
  948.  
  949.  
  950. void outchr(char c)
  951. /* This function outputs one character to the screen, and if output to the
  952.  * com port is enabled, the character is output there too.  ANSI graphics
  953.  * are also trapped here, and the ansi function is called to execute the
  954.  * ANSI codes
  955.  */
  956. {
  957.   int i, i1;
  958.  
  959.   if (change_color) {
  960.     change_color = 0;
  961.     if ((c >= '0') && (c <= '7'))
  962.       ansic(c - '0');
  963.     return;
  964.   }
  965.   if (c == 3) {
  966.     change_color = 1;
  967.     return;
  968.   }
  969.   if ((c == 10) && endofline[0]) {
  970.     if (!in_extern)
  971.       outstr(endofline);
  972.     endofline[0] = 0;
  973.   }
  974.   if (outcom && (c != 9))
  975.     outcomch(echo ? c : 'X');
  976.   if (ansiptr) {
  977.     ansistr[ansiptr++] = c;
  978.     ansistr[ansiptr]   = 0;
  979.     if (((c > '@') && (c != '[')) || (ansistr[1] != '['))
  980.       execute_ansi();
  981.   } else if (c == 27) {
  982.     ansistr[0] = 27;
  983.     ansiptr = 1;
  984.   } else if (c == 9) {
  985.     i1 = wherex();
  986.     for (i = i1; i< (((i1 / 8) + 1) * 8); i++)
  987.       outchr(32);
  988.   } else if (echo || lecho) {
  989.     out1ch(c);
  990.     if (c == 10) {
  991.       ++lines_listed;
  992.       if ((sysstatus_pause_on_page & thisuser.sysstatus) &&
  993.           (lines_listed >= screenlinest - 1)) {
  994.         pausescr();
  995.         lines_listed = 0;
  996.       }
  997.     }
  998.   } else
  999.     out1ch('X');
  1000. }
  1001.  
  1002.  
  1003.  
  1004. void outstr(char *s)
  1005. /* This function outputs a string of characters to the screen (and remotely
  1006.  * if applicable).  The com port is also checked first to see if a remote
  1007.  * user has hung up
  1008.  */
  1009. {
  1010.   int i=0;
  1011.  
  1012.   checkhangup();
  1013.   if (!hangup)
  1014.     while (s[i])
  1015.       outchr(s[i++]);
  1016. }
  1017.  
  1018.  
  1019.  
  1020. void nl(void)
  1021. /* This function performs a CR/LF sequence to move the cursor to the next
  1022.  * line.  If any end-of-line ANSI codes are set (such as changing back to
  1023.  * the default color) are specified, those are executed first.
  1024.  */
  1025. {
  1026.   if (endofline[0]) {
  1027.     outstr(endofline);
  1028.     endofline[0] = 0;
  1029.   }
  1030.   outstr("\r\n");
  1031. }
  1032.  
  1033.  
  1034.  
  1035. void backspace(void)
  1036. /* This function executes a backspace, space, backspace sequence. */
  1037. {
  1038.   int i;
  1039.  
  1040.   i = echo;
  1041.   echo = 1;
  1042.   outstr("\b \b");
  1043.   echo = i;
  1044. }
  1045.  
  1046.  
  1047.  
  1048. void setc(unsigned char ch)
  1049. /* This sets the current color (both locally and remotely) to that
  1050.  * specified (in IBM format).
  1051.  */
  1052. {
  1053.   char s[30];
  1054.  
  1055.   makeansi(ch, s, 0);
  1056.   outstr(s);
  1057. }
  1058.  
  1059.  
  1060.  
  1061. void pausescr(void)
  1062. /* This will pause output, displaying the [PAUSE] message, and wait for
  1063.  * a key to be hit.
  1064.  */
  1065. {
  1066.   int i;
  1067.  
  1068.   if (okansi()) {
  1069.     i = curatr;
  1070.     setc((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[3] :
  1071.           thisuser.bwcolors[3]);
  1072.     outstr("[PAUSE]\x1b[7D");
  1073.     setc(i);
  1074.     getkey();
  1075.     outstr("       \x1b[7D");
  1076.   } else {
  1077.     outstr("[PAUSE]");
  1078.     getkey();
  1079.     for (i = 0; i < 7; i++)
  1080.       backspace();
  1081.   }
  1082. }
  1083.  
  1084.  
  1085.  
  1086. void pl(char *s)
  1087. {
  1088.   outstr(s);
  1089.   nl();
  1090. }
  1091.  
  1092.  
  1093. int kbhitb(void)
  1094. {
  1095.   union REGS r;
  1096.  
  1097.   r.h.ah = 1;
  1098.   int86(0x16, &r, &r);
  1099.   return((r.x.flags & 64) == 0);
  1100. }
  1101.  
  1102.  
  1103. int empty(void)
  1104. {
  1105.   if (kbhitb() || (incom && (head != tail)) ||
  1106.       (charbufferpointer && charbuffer[charbufferpointer]) ||
  1107.       (in_extern == 2))
  1108.     return(0);
  1109.   return(1);
  1110. }
  1111.  
  1112.  
  1113.  
  1114. void skey1(char *ch)
  1115. {
  1116.   char c;
  1117.  
  1118.   c = *ch;
  1119.   if (c == 127)
  1120.     c = 8;
  1121.     switch(c) {
  1122.       case 1:
  1123.       case 4:
  1124.       case 6:
  1125.         if (!charbufferpointer) {
  1126.           if (c == 1)
  1127.             c = 2;
  1128.           else if (c == 4)
  1129.             c = 0;
  1130.           else if (c == 6)
  1131.             c = 1;
  1132.           strcpy(charbuffer, &(thisuser.macros[c][0]));
  1133.           c = charbuffer[0];
  1134.           if (c)
  1135.             charbufferpointer = 1;
  1136.         }
  1137.         break;
  1138.     }
  1139.   *ch = c;
  1140. }
  1141.  
  1142. char getchd(void)
  1143. {
  1144.   union REGS r;
  1145.  
  1146.   r.h.ah = 0x07;
  1147.   int86(save_dos, &r, &r);
  1148.   return(r.h.al);
  1149. }
  1150.  
  1151.  
  1152. char getchd1(void)
  1153. {
  1154.   union REGS r;
  1155.  
  1156.   r.h.ah = 0x06;
  1157.   r.h.dl = 0xFF;
  1158.   int86(save_dos, &r, &r);
  1159.   return((r.x.flags & 0x40) ? 255 : r.h.al);
  1160. }
  1161.  
  1162.  
  1163. char inkey(void)
  1164. /* This function checks both the local keyboard, and the remote terminal
  1165.  * (if any) for input.  If there is input, the key is returned.  If there
  1166.  * is no input, a zero is returned.  Function keys hit are interpreted as
  1167.  * such within the routine and not returned.
  1168.  */
  1169. {
  1170.   char ch=0;
  1171.  
  1172.   if (charbufferpointer) {
  1173.     if (!charbuffer[charbufferpointer])
  1174.       charbufferpointer = charbuffer[0] = 0;
  1175.     else
  1176.       return(charbuffer[charbufferpointer++]);
  1177.   }
  1178.   if (kbhitb() || (in_extern == 2)) {
  1179.     ch = getchd1();
  1180.     if (!ch) {
  1181.       if (in_extern)
  1182.         in_extern = 2;
  1183.       else {
  1184.         ch = getchd1();
  1185.         ch=0;
  1186.       }
  1187.     } else if (in_extern)
  1188.       in_extern = 1;
  1189.     timelastchar1=timer1();
  1190.   } else if (incom && comhit()) {
  1191.     ch = (get1c() & andwith);
  1192.   }
  1193.   skey1(&ch);
  1194.   return(ch);
  1195. }
  1196.  
  1197.  
  1198.  
  1199. void mpl(int i)
  1200. /* This will make a reverse-video prompt line i characters long, repositioning
  1201.  * the cursor at the beginning of the input prompt area.  Of course, if the
  1202.  * user does not want ansi, this routine does nothing.
  1203.  */
  1204. {
  1205.   int i1;
  1206.   char s[81];
  1207.  
  1208.   if (okansi()) {
  1209.     ansic(4);
  1210.     for (i1 = 0; i1 < i; i1++)
  1211.       outchr(' ');
  1212.     outstr("\x1b[");
  1213.     itoa(i,s,10);
  1214.     outstr(s);
  1215.     outstr("D");
  1216.   }
  1217. }
  1218.  
  1219.  
  1220.  
  1221. char upcase(char ch)
  1222. /* This converts a character to uppercase */
  1223. {
  1224.   if ((ch > '`') && (ch < '{'))
  1225.     ch = ch - 32;
  1226.   return(ch);
  1227. }
  1228.  
  1229.  
  1230. unsigned char getkey(void)
  1231. /* This function returns one character from either the local keyboard or
  1232.  * remote com port (if applicable).  After 1.5 minutes of inactivity, a
  1233.  * beep is sounded.  After 3 minutes of inactivity, the user is hung up.
  1234.  */
  1235. {
  1236.   unsigned char ch;
  1237.   int beepyet;
  1238.   long dd;
  1239.  
  1240.   beepyet = 0;
  1241.   timelastchar1=timer1();
  1242.  
  1243.   lines_listed = 0;
  1244.   do {
  1245.     while (empty() && !hangup) {
  1246.       dd = timer1();
  1247.       if (labs(dd - timelastchar1) > 65536L)
  1248.         timelastchar1 -= 1572480L;
  1249.       if (((dd - timelastchar1) > 1638L) && (!beepyet)) {
  1250.         beepyet = 1;
  1251.         outchr(7);
  1252.       }
  1253.       if (labs(dd - timelastchar1) > 3276L) {
  1254.         nl();
  1255.         outstr("Call back later when you are there.");
  1256.         nl();
  1257.         hangup = 1;
  1258.       }
  1259.       checkhangup();
  1260.     }
  1261.     ch = inkey();
  1262.   } while (!ch && !in_extern && !hangup);
  1263.   return(ch);
  1264. }
  1265.  
  1266.  
  1267.  
  1268. void input1(char *s, int maxlen, int lc, int crend)
  1269. /* This will input a line of data, maximum maxlen characters long, terminated
  1270.  * by a C/R.  if (lc) is non-zero, lowercase is allowed, otherwise all
  1271.  * characters are converted to uppercase.
  1272.  */
  1273. {
  1274.   int curpos=0, done=0;
  1275.   unsigned char ch;
  1276.  
  1277.   while (!done && !hangup) {
  1278.     ch = getkey();
  1279.     if (ch > 31) {
  1280.       if (curpos < maxlen) {
  1281.         if (!lc)
  1282.           ch = upcase(ch);
  1283.         s[curpos++] = ch;
  1284.         outchr(ch);
  1285.       }
  1286.     } else
  1287.       switch(ch) {
  1288.         case 14:
  1289.         case 13:
  1290.           s[curpos] = 0;
  1291.           done = echo = 1;
  1292.           if (crend)
  1293.             nl();
  1294.           break;
  1295.         case 26:
  1296.           if (input_extern) {
  1297.             s[curpos++] = 26;
  1298.             outstr("^Z");
  1299.           }
  1300.           break;
  1301.         case 8:
  1302.           if (curpos) {
  1303.             curpos--;
  1304.             backspace();
  1305.             if (s[curpos] == 26)
  1306.               backspace();
  1307.           }
  1308.           break;
  1309.         case 24:
  1310.           while (curpos) {
  1311.             curpos--;
  1312.             backspace();
  1313.             if (s[curpos] == 26)
  1314.               backspace();
  1315.           }
  1316.           break;
  1317.       }
  1318.   }
  1319.   if (hangup)
  1320.     s[0] = 0;
  1321. }
  1322.  
  1323.  
  1324.  
  1325. void input(char *s, int len)
  1326. /* This will input an upper-case string */
  1327. {
  1328.   input1(s, len, 0, 1);
  1329. }
  1330.  
  1331.  
  1332.  
  1333. void inputl(char *s, int len)
  1334. /* This will input an upper or lowercase string of characters */
  1335. {
  1336.   input1(s, len, 1, 1);
  1337. }
  1338.  
  1339.  
  1340.  
  1341. int yn(void)
  1342. /* The keyboard is checked for either a Y, N, or C/R to be hit.  C/R is
  1343.  * assumed to be the same as a N.  Yes or No is output, and yn is set to
  1344.  * zero if No was returned, and yn() is non-zero if Y was hit.
  1345.  */
  1346. {
  1347.   char ch=0;
  1348.  
  1349.   ansic(1);
  1350.   while (!hangup && ((ch = upcase(getkey())) != 'Y') && (ch != 'N') && (ch != 13))
  1351.     ;
  1352.   outstr((ch == 'Y') ? "Yes" : "No");
  1353.   nl();
  1354.   return(ch == 'Y');
  1355. }
  1356.  
  1357.  
  1358.  
  1359. int ny(void)
  1360. /* This is the same as yn(), except C/R is assumed to be "Y" */
  1361. {
  1362.   char ch=0;
  1363.  
  1364.   ansic(1);
  1365.   while (!hangup && ((ch = upcase(getkey())) != 'Y') && (ch != 'N') && (ch != 13))
  1366.     ;
  1367.   outstr((ch == 'N') ? "No" : "Yes");
  1368.   nl();
  1369.   return((ch == 'Y') || (ch==13));
  1370. }
  1371.  
  1372.  
  1373. void ansic(int n)
  1374. {
  1375.   char s[81], c;
  1376.  
  1377.   c = ((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[n] :
  1378.         thisuser.bwcolors[n]);
  1379.   if (c == curatr)
  1380.     return;
  1381.   setc(c);
  1382.   makeansi((thisuser.sysstatus & sysstatus_color) ? thisuser.colors[0] :
  1383.         thisuser.bwcolors[0],endofline, 0);
  1384. }
  1385.  
  1386.  
  1387. char onek(char *s)
  1388. {
  1389.   char ch;
  1390.  
  1391.   while (!strchr(s, ch = upcase(getkey())) && !hangup)
  1392.     ;
  1393.   if (hangup)
  1394.     ch = s[0];
  1395.   outchr(ch);
  1396.   nl();
  1397.   return(ch);
  1398. }
  1399.  
  1400.  
  1401. void prt(int i, char *s)
  1402. {
  1403.   ansic(i);
  1404.   outstr(s);
  1405.   ansic(0);
  1406. }
  1407.  
  1408. /****************************************************************************/
  1409. #pragma warn -par
  1410.  
  1411. void far interrupt inlii(unsigned bp, unsigned di, unsigned si,
  1412.                            unsigned ds, unsigned es, unsigned dx,
  1413.                            unsigned cx, unsigned bx, unsigned ax,
  1414.                            unsigned ip, unsigned cs, unsigned flags,
  1415.                            char *s1, char *s2, int i1, int i2)
  1416. {
  1417.   inli(s1,s2,i1,i2);
  1418. }
  1419.  
  1420. void far interrupt checkai(unsigned bp, unsigned di, unsigned si,
  1421.                            unsigned ds, unsigned es, unsigned dx,
  1422.                            unsigned cx, unsigned bx, unsigned ax,
  1423.                            unsigned ip, unsigned cs, unsigned flags,
  1424.                            int *i1, int *i2)
  1425. {
  1426.   checka(i1,i2);
  1427. }
  1428.  
  1429.  
  1430. void far interrupt plai(unsigned bp, unsigned di, unsigned si,
  1431.                            unsigned ds, unsigned es, unsigned dx,
  1432.                            unsigned cx, unsigned bx, unsigned ax,
  1433.                            unsigned ip, unsigned cs, unsigned flags,
  1434.                            char *s1, int *i1)
  1435. {
  1436.   pla(s1,i1);
  1437. }
  1438.  
  1439.  
  1440. void far interrupt outchri(unsigned bp, unsigned di, unsigned si,
  1441.                            unsigned ds, unsigned es, unsigned dx,
  1442.                            unsigned cx, unsigned bx, unsigned ax,
  1443.                            unsigned ip, unsigned cs, unsigned flags,
  1444.                            char ch)
  1445. {
  1446.   outchr(ch);
  1447. }
  1448.  
  1449.  
  1450. void far interrupt outstri(unsigned bp, unsigned di, unsigned si,
  1451.                            unsigned ds, unsigned es, unsigned dx,
  1452.                            unsigned cx, unsigned bx, unsigned ax,
  1453.                            unsigned ip, unsigned cs, unsigned flags,
  1454.                            char *s1)
  1455. {
  1456.   outstr(s1);
  1457. }
  1458.  
  1459.  
  1460. void far interrupt nli(unsigned bp, unsigned di, unsigned si,
  1461.                            unsigned ds, unsigned es, unsigned dx,
  1462.                            unsigned cx, unsigned bx, unsigned ax,
  1463.                            unsigned ip, unsigned cs, unsigned flags)
  1464. {
  1465.   nl();
  1466. }
  1467.  
  1468.  
  1469. void far interrupt pli(unsigned bp, unsigned di, unsigned si,
  1470.                            unsigned ds, unsigned es, unsigned dx,
  1471.                            unsigned cx, unsigned bx, unsigned ax,
  1472.                            unsigned ip, unsigned cs, unsigned flags,
  1473.                            char *s1)
  1474. {
  1475.   pl(s1);
  1476. }
  1477.  
  1478.  
  1479. void far interrupt emptyi(unsigned bp, unsigned di, unsigned si,
  1480.                            unsigned ds, unsigned es, unsigned dx,
  1481.                            unsigned cx, unsigned bx, unsigned ax,
  1482.                            unsigned ip, unsigned cs, unsigned flags)
  1483. {
  1484.   ax=empty();
  1485. }
  1486.  
  1487.  
  1488. void far interrupt inkeyi(unsigned bp, unsigned di, unsigned si,
  1489.                            unsigned ds, unsigned es, unsigned dx,
  1490.                            unsigned cx, unsigned bx, unsigned ax,
  1491.                            unsigned ip, unsigned cs, unsigned flags)
  1492. {
  1493.   ax=(unsigned) empty();
  1494. }
  1495.  
  1496.  
  1497. void far interrupt getkeyi(unsigned bp, unsigned di, unsigned si,
  1498.                            unsigned ds, unsigned es, unsigned dx,
  1499.                            unsigned cx, unsigned bx, unsigned ax,
  1500.                            unsigned ip, unsigned cs, unsigned flags)
  1501. {
  1502.   ax=(unsigned) getkey();
  1503. }
  1504.  
  1505.  
  1506. void far interrupt inputi(unsigned bp, unsigned di, unsigned si,
  1507.                            unsigned ds, unsigned es, unsigned dx,
  1508.                            unsigned cx, unsigned bx, unsigned ax,
  1509.                            unsigned ip, unsigned cs, unsigned flags,
  1510.                            char *s1, int i)
  1511. {
  1512.   input(s1,i);
  1513. }
  1514.  
  1515.  
  1516. void far interrupt inputli(unsigned bp, unsigned di, unsigned si,
  1517.                            unsigned ds, unsigned es, unsigned dx,
  1518.                            unsigned cx, unsigned bx, unsigned ax,
  1519.                            unsigned ip, unsigned cs, unsigned flags,
  1520.                            char *s1, int i)
  1521. {
  1522.   inputl(s1,i);
  1523. }
  1524.  
  1525.  
  1526. void far interrupt yni(unsigned bp, unsigned di, unsigned si,
  1527.                            unsigned ds, unsigned es, unsigned dx,
  1528.                            unsigned cx, unsigned bx, unsigned ax,
  1529.                            unsigned ip, unsigned cs, unsigned flags)
  1530. {
  1531.   ax=yn();
  1532. }
  1533.  
  1534.  
  1535.  
  1536. void far interrupt nyi(unsigned bp, unsigned di, unsigned si,
  1537.                            unsigned ds, unsigned es, unsigned dx,
  1538.                            unsigned cx, unsigned bx, unsigned ax,
  1539.                            unsigned ip, unsigned cs, unsigned flags)
  1540. {
  1541.   ax=ny();
  1542. }
  1543.  
  1544.  
  1545. void far interrupt ansici(unsigned bp, unsigned di, unsigned si,
  1546.                            unsigned ds, unsigned es, unsigned dx,
  1547.                            unsigned cx, unsigned bx, unsigned ax,
  1548.                            unsigned ip, unsigned cs, unsigned flags,
  1549.                            int i1)
  1550. {
  1551.   ansic(i1);
  1552. }
  1553.  
  1554.  
  1555. void far interrupt oneki(unsigned bp, unsigned di, unsigned si,
  1556.                            unsigned ds, unsigned es, unsigned dx,
  1557.                            unsigned cx, unsigned bx, unsigned ax,
  1558.                            unsigned ip, unsigned cs, unsigned flags,
  1559.                            char *s1)
  1560. {
  1561.   ax=(unsigned) onek(s1);
  1562. }
  1563.  
  1564.  
  1565. void far interrupt prti(unsigned bp, unsigned di, unsigned si,
  1566.                            unsigned ds, unsigned es, unsigned dx,
  1567.                            unsigned cx, unsigned bx, unsigned ax,
  1568.                            unsigned ip, unsigned cs, unsigned flags,
  1569.                            int i1, char *s1)
  1570. {
  1571.   prt(i1,s1);
  1572. }
  1573.  
  1574.  
  1575.  
  1576. void far interrupt mpli(unsigned bp, unsigned di, unsigned si,
  1577.                            unsigned ds, unsigned es, unsigned dx,
  1578.                            unsigned cx, unsigned bx, unsigned ax,
  1579.                            unsigned ip, unsigned cs, unsigned flags,
  1580.                            int i1)
  1581. {
  1582.   mpl(i1);
  1583. }
  1584.  
  1585.  
  1586. #pragma warn +par
  1587.  
  1588. /****************************************************************************/
  1589.  
  1590. unsigned char getkeyext(void)
  1591. {
  1592.   unsigned char ch;
  1593.   static int holding=0;
  1594.   static char held=0;
  1595.  
  1596.   if (holding) {
  1597.     holding=0;
  1598.     return(held);
  1599.   }
  1600.   ch=getkey();
  1601.   if (charbufferpointer==0) {
  1602.     if (ch==16) {
  1603.       ch=getkey();
  1604.       if ((ch==1) && (charbufferpointer==0)) {
  1605.         strcpy(charbuffer,&(thisuser.macros[2][0]));
  1606.         ch=charbuffer[0];
  1607.         if (ch) {
  1608.           charbufferpointer=1;
  1609.           return(ch);
  1610.         } else
  1611.           return(getkeyext());
  1612.       } else
  1613.         if ((ch==4) && (charbufferpointer==0)) {
  1614.           strcpy(charbuffer,&(thisuser.macros[0][0]));
  1615.           ch=charbuffer[0];
  1616.           if (ch) {
  1617.             charbufferpointer=1;
  1618.             return(ch);
  1619.           } else
  1620.             return(getkeyext());
  1621.         } else
  1622.           if ((ch==6) && (charbufferpointer==0)) {
  1623.             strcpy(charbuffer,&(thisuser.macros[1][0]));
  1624.             ch=charbuffer[0];
  1625.             if (ch) {
  1626.               charbufferpointer=1;
  1627.               return(ch);
  1628.             } else
  1629.               return(getkeyext());
  1630.           } else {
  1631.             holding=1;
  1632.             held=ch;
  1633.             return(16);
  1634.           }
  1635.     }
  1636.   }
  1637.   return(ch);
  1638. }
  1639.  
  1640.  
  1641. int do_it(char *cl)
  1642. {
  1643.   int i,i1,l;
  1644.   char s[160];
  1645.   char *ss[30];
  1646.  
  1647.   strcpy(s,cl);
  1648.   ss[0]=s;
  1649.   i=1;
  1650.   l=strlen(s);
  1651.   for (i1=1; i1<l; i1++)
  1652.     if (s[i1]==32) {
  1653.       s[i1]=0;
  1654.       ss[i++]=&(s[i1+1]);
  1655.     }
  1656.   ss[i]=NULL;
  1657.   funcs[20]=NULL;
  1658.   i=(spawnvpe(P_WAIT,ss[0],ss,xenviron) & 0x00ff);
  1659.   funcs[20]=NULL;
  1660. /*   spawnvp(P_WAIT,ss[0],ss); */
  1661.   return(i);
  1662. }
  1663.  
  1664.  
  1665.  
  1666. int do_remote(char *s, int ccc)
  1667. {
  1668.   int rc,xx;
  1669.   char x[161];
  1670.  
  1671.   checkhangup();
  1672.   if (hangup)
  1673.     return(32767);
  1674.   strcpy(x,getenv("COMSPEC"));
  1675.   strcat(x," /C ");
  1676.   strcat(x,s);
  1677.   if (ccc)
  1678.     rc=do_it(x);
  1679.   else
  1680.     rc=do_it(s);
  1681.   chdir(cdir);
  1682.   setdisk(cdir[0]-'A');
  1683.   return(rc);
  1684. }
  1685.  
  1686.  
  1687. union REGS ca_r;
  1688. int ca_pause,ca_ctrl_c;
  1689. long ca_d1;
  1690.  
  1691.  
  1692. void checka2(void)
  1693. {
  1694.   ca_pause=0;
  1695.   ca_ctrl_c=0;
  1696.   ca_r.h.ah=1;
  1697.   int86(0x16,&ca_r,&ca_r);
  1698.   if ((ca_r.x.flags & 64)==0) {
  1699.     if (ca_r.x.ax==11779)
  1700.       ca_ctrl_c=1;
  1701.     if (ca_r.x.ax==7955)
  1702.       ca_pause=1;
  1703.   }
  1704.   if (head!=tail) {
  1705.     if (buffer[tail]==3)
  1706.       ca_ctrl_c=1;
  1707.     if (buffer[tail]==19)
  1708.       ca_pause=1;
  1709.   }
  1710.   if (ca_pause) {
  1711.     while (inkey()!=0)
  1712.       ;
  1713.     ca_d1=timer1();
  1714.     while ((inkey()==0) && (labs(timer1()-ca_d1)<3276L) && (!hangup))
  1715.       checkhangup();
  1716.     lines_listed=0;
  1717.   }
  1718.   if ((ca_ctrl_c) && (ctc)) {
  1719.     while (inkey()!=0)
  1720.       ;
  1721.     pl("^C");
  1722.     ca_r.x.ax=0x4c00;
  1723.     int86(save_dos,&ca_r,&ca_r);
  1724.   }
  1725. }
  1726.  
  1727.  
  1728.  
  1729.  
  1730. void outdosstr(char *s)
  1731. /* This function outputs a string of characters to the screen (and remotely
  1732.  * if applicable).  The com port is also checked first to see if a remote
  1733.  * user has hung up
  1734.  */
  1735. {
  1736.   int i;
  1737.  
  1738.   checkhangup();
  1739.   if (hangup==0) {
  1740.     i=0;
  1741.     while ((s[i] !='$') && (i<1024)) {
  1742.       checka2();
  1743.       outchr(s[i++]);
  1744.     }
  1745.   }
  1746. }
  1747.  
  1748.  
  1749.  
  1750. union REGS ni_r;
  1751. struct SREGS ni_s;
  1752. unsigned ni_n;
  1753. char ni_ch,ni_ch1,ni_ss[10],ni_ch2;
  1754. unsigned char *ni_st;
  1755.  
  1756. #define ST_SIZE 500
  1757. static unsigned short ni_stack[ST_SIZE];
  1758.  
  1759.  
  1760. #pragma warn -par
  1761.  
  1762. void far interrupt newintr1(unsigned bp, unsigned di, unsigned si,
  1763.                            unsigned ds, unsigned es, unsigned dx,
  1764.                            unsigned cx, unsigned bx, unsigned ax,
  1765.                            unsigned ip, unsigned cs, unsigned flags)
  1766. {
  1767.  
  1768.   unsigned short ni_SS, ni_SP;
  1769. #define NEW_STK() { _BX=FP_OFF(&ni_stack[ST_SIZE-2]); _SS=_DS; _SP=_BX; }
  1770. #define OLD_STK() { _AX=ni_SS; _BX=ni_SP; _SS=_AX; _SP=_BX; }
  1771.  
  1772.  
  1773.  
  1774.   ni_r.x.ax=ax;
  1775.   ni_r.x.bx=bx;
  1776.   ni_r.x.cx=cx;
  1777.   ni_r.x.dx=dx;
  1778.   ni_r.x.si=si;
  1779.   ni_r.x.di=di;
  1780.   ni_r.x.flags=flags;
  1781.   ni_s.ds=ds;
  1782.   ni_s.es=es;
  1783.  
  1784.   ni_SS=_SS;
  1785.   ni_SP=_SP;
  1786.  
  1787.   ni_ch=ni_r.h.ah;
  1788.   ni_ch1=0;
  1789.  
  1790.   switch(ni_ch) {
  1791.     case 0x01:
  1792.       NEW_STK();
  1793.       ni_ch=getkeyext();
  1794.       outchr(ni_ch);
  1795.       if (hangup)
  1796.         ni_ch=3;
  1797.       ni_r.h.al=ni_ch;
  1798.       ni_ch1=1;
  1799.       OLD_STK();
  1800.       break;
  1801.     case 0x02:
  1802.       NEW_STK();
  1803.       outchr(ni_r.h.dl);
  1804.       ni_ch1=1;
  1805.       checka2();
  1806.       OLD_STK();
  1807.       break;
  1808.     case 0x06:
  1809.       NEW_STK();
  1810.       if (ni_r.h.dl!=0xff) {
  1811.         outchr(ni_r.h.dl);
  1812.         ni_ch1=1;
  1813.       } else {
  1814.         if (empty()) {
  1815.           ni_r.x.flags |= 64;
  1816.         } else {
  1817.           ni_r.x.flags &= (0xffff ^ 64);
  1818.           ni_r.h.al=getkeyext();
  1819.         }
  1820.       }
  1821.       OLD_STK();
  1822.       break;
  1823.     case 0x07:
  1824.       NEW_STK();
  1825.       ni_ch1=1;
  1826.       ni_r.h.al=getkeyext();
  1827.       OLD_STK();
  1828.       break;
  1829.     case 0x08:
  1830.       NEW_STK();
  1831.       ni_ch1=1;
  1832.       ni_r.h.al=getkeyext();
  1833.       OLD_STK();
  1834.       break;
  1835.     case 0x09:
  1836.       NEW_STK();
  1837.       outdosstr((char *) MK_FP(ni_s.ds, ni_r.x.dx));
  1838.       ni_ch1=1;
  1839.       OLD_STK();
  1840.       break;
  1841.     case 0x0a:
  1842.       NEW_STK();
  1843.       ni_st=(char *) MK_FP(ni_s.ds,ni_r.x.dx);
  1844.       ni_n=(unsigned int)(ni_st[0]);
  1845.       if (in_extern==2)
  1846.         getkeyext();
  1847.       in_extern=0;
  1848.       input_extern=1;
  1849.       input1(&(ni_st[2]),ni_n-3,1,0);
  1850.       input_extern=0;
  1851.       in_extern=1;
  1852.       ni_st[1]=strlen(&(ni_st[2]));
  1853.       strcat(&(ni_st[2]),"\r");
  1854.       if ((hangup)) {
  1855.         strcpy(&(ni_st[2]),"EXIT\r");
  1856.         ni_st[1]=4;
  1857.         outstr("Exiting...");
  1858.       }
  1859.       ni_ch1=1;
  1860.       OLD_STK();
  1861.       break;
  1862.     case 0x0b:
  1863.       NEW_STK();
  1864.       if (empty())
  1865.         ni_r.h.al=0x00;
  1866.       else
  1867.         ni_r.h.al=0xff;
  1868.       ni_ch1=1;
  1869.       OLD_STK();
  1870.       break;
  1871.     case 0x0c:
  1872.       ni_r.h.ah=ni_r.h.al;
  1873.       int86x(INT_REAL_DOS,&ni_r,&ni_r,&ni_s);
  1874.       ni_ch1=1;
  1875.       break;
  1876.     case 0x3f:
  1877.       if (ni_r.x.bx==0x0000) {
  1878.         NEW_STK();
  1879.         ni_st=(char *)MK_FP(ni_s.ds,ni_r.x.dx);
  1880.         inputl(ni_st,ni_r.x.cx);
  1881.         strcat(ni_st,"\r\n");
  1882.         ni_r.x.ax=strlen(ni_st);
  1883.         if (hangup)
  1884.           ni_r.x.ax=0;
  1885.         ni_r.x.flags &=(0xffff ^ 1);
  1886.         ni_ch1=1;
  1887.         OLD_STK();
  1888.       } else
  1889.         int86x(save_dos,&ni_r,&ni_r,&ni_s);
  1890.       break;
  1891.     case 0x40:
  1892.       if ((ni_r.x.bx==0x0001) || (ni_r.x.bx==0x0002)) {
  1893.         NEW_STK();
  1894.         ni_st=(char *)MK_FP(ni_s.ds,ni_r.x.dx);
  1895.         for (ni_n=0; ni_n<ni_r.x.cx; ni_n++) {
  1896.           outchr(ni_st[ni_n]);
  1897.           checka2();
  1898.         }
  1899.         ni_r.x.ax=ni_r.x.cx;
  1900.         ni_r.x.flags &=(0xffff ^ 1);
  1901.         ni_ch1=1;
  1902.         OLD_STK();
  1903.       } else
  1904.         int86x(save_dos,&ni_r,&ni_r,&ni_s);
  1905.       break;
  1906.     default:
  1907.       int86x(save_dos,&ni_r,&ni_r,&ni_s);
  1908.       break;
  1909.   }
  1910.  
  1911.   if (ni_ch1) {
  1912.     checkhangup();
  1913.     if (hangup) {
  1914.       if (hanguptime1<0L) {
  1915.         hanguptime1=timer1();
  1916.         if (funcs[20]) {
  1917.           outstr("Terminating...\r\n");
  1918.           ip=FP_OFF(funcs[20]);
  1919.           cs=FP_SEG(funcs[20]);
  1920.           funcs[20]=(void far *)36;
  1921.         } else {
  1922.           funcs[20]=(void far *)36;
  1923.           outstr("Aborting...\r\n");
  1924.           ni_r.x.ax=0x4c00;
  1925.           int86x(save_dos,&ni_r,&ni_r,&ni_s);
  1926.         }
  1927.       } else {
  1928.         if (labs(timer1()-hanguptime1)>36L) {
  1929.           hanguptime1=timer1();
  1930.           outstr("Aborting...\r\n");
  1931.           ni_r.x.ax=0x4c00;
  1932.           int86x(save_dos,&ni_r,&ni_r,&ni_s);
  1933.         }
  1934.       }
  1935.     }
  1936.   }
  1937.  
  1938.   ax=ni_r.x.ax;
  1939.   bx=ni_r.x.bx;
  1940.   cx=ni_r.x.cx;
  1941.   dx=ni_r.x.dx;
  1942.   si=ni_r.x.si;
  1943.   di=ni_r.x.di;
  1944.   flags=ni_r.x.flags;
  1945.   ds=ni_s.ds;
  1946.   es=ni_s.es;
  1947. }
  1948.  
  1949.  
  1950.  
  1951. int full_external(char *s, int ccc)
  1952. {
  1953.   unsigned short sav;
  1954.   int xx,cy,cx,xxx;
  1955.  
  1956.   checkhangup();
  1957.   if (hangup)
  1958.     return(0);
  1959.   in_extern=1;
  1960.   hanguptime1=-1L;
  1961.   setvect(save_dos,getvect(INT_REAL_DOS));
  1962.  
  1963.   setvect(INT_REAL_DOS,newintr1);
  1964.  
  1965.   if ((screenlinest<=defscreenbottom) && (screenlinest>20)) {
  1966.     screenbottom=screenlinest-1;
  1967.     cy=wherey();
  1968.     cx=wherex();
  1969.     xxx=cy-screenbottom+topline;
  1970.     if (xxx>0) {
  1971.       SCROLL_UP(topline,defscreenbottom,xxx);
  1972.       movecsr(cx,screenbottom);
  1973.     }
  1974.   }
  1975.  
  1976.   do_remote(s,ccc);
  1977.  
  1978.   setvect(INT_REAL_DOS,getvect(save_dos));
  1979.  
  1980.   if (in_extern==2)
  1981.     getkey();
  1982.   in_extern=0;
  1983.   return(0);
  1984. }
  1985.  
  1986.  
  1987. /****************************************************************************/
  1988.  
  1989. #define READ(x) read(i,&(x),sizeof(x))
  1990.  
  1991. int init_r(void)
  1992. {
  1993.   int i;
  1994.  
  1995.   for (i=0; i<25; i++)
  1996.     funcs[i]=NULL;
  1997.   funcs[0]=(void far *)inlii;
  1998.   funcs[1]=(void far *)checkai;
  1999.   funcs[2]=(void far *)plai;
  2000.   funcs[3]=(void far *)outchri;
  2001.   funcs[4]=(void far *)outstri;
  2002.   funcs[5]=(void far *)nli;
  2003.   funcs[8]=(void far *)pli;
  2004.   funcs[9]=(void far *)emptyi;
  2005.   funcs[10]=(void far *)inkeyi;
  2006.   funcs[11]=(void far *)getkeyi;
  2007.   funcs[12]=(void far *)inputi;
  2008.   funcs[13]=(void far *)inputli;
  2009.   funcs[14]=(void far *)yni;
  2010.   funcs[15]=(void far *)nyi;
  2011.   funcs[16]=(void far *)ansici;
  2012.   funcs[17]=(void far *)oneki;
  2013.   funcs[18]=(void far *)prti;
  2014.   funcs[19]=(void far *)mpli;
  2015.  
  2016.   i=open("stat.wwv",O_RDONLY | O_BINARY);
  2017.   if (i<0)
  2018.     return(1);
  2019.  
  2020.   READ(incom);
  2021.   READ(outcom);
  2022.   using_modem=incom || outcom;
  2023.   READ(thisuser);
  2024.   READ(flow_control);
  2025.   READ(async_irq);
  2026.   READ(baud_rate);
  2027.   READ(base);
  2028.   READ(andwith);
  2029.   READ(ctc);
  2030.   READ(defscreenbottom);
  2031.   READ(ok_modem_stuff);
  2032.   READ(save_dos);
  2033.  
  2034.   close(i);
  2035.  
  2036.   if (ok_modem_stuff)
  2037.     initport(0);
  2038.   return(0);
  2039. }
  2040.  
  2041. void get_dir(char *s, int be)
  2042. {
  2043.   strcpy(s,"X:\\");
  2044.   s[0]='A'+getdisk();
  2045.   getcurdir(0,&(s[3]));
  2046.   if (be) {
  2047.     if (s[strlen(s)-1]!='\\')
  2048.       strcat(s,"\\");
  2049.   }
  2050. }
  2051.  
  2052.  
  2053. void cd_to(char *s)
  2054. {
  2055.   char s1[81];
  2056.   int i,db;
  2057.  
  2058.   strcpy(s1,s);
  2059.   i=strlen(s1)-1;
  2060.   db=(s1[i]=='\\');
  2061.   if (i==0)
  2062.     db=0;
  2063.   if ((i==2) && (s1[1]==':'))
  2064.     db=0;
  2065.   if (db)
  2066.     s1[i]=0;
  2067.   chdir(s1);
  2068.   if (s[1]==':')
  2069.     setdisk(s[0]-'A');
  2070. }
  2071.  
  2072. void setup_stuff(void)
  2073. {
  2074.   char s[161];
  2075.   int i,i1;
  2076.  
  2077.   strcpy(ver_no1,"BBS=");
  2078.   strcat(ver_no1,wwiv_version);
  2079.  
  2080.  
  2081.   strcpy(s,getenv("PROMPT"));
  2082.   strcpy(newprompt,"PROMPT=WWIV: ");
  2083.   if (s[0])
  2084.     strcat(newprompt,s);
  2085.   else
  2086.     strcat(newprompt,"$P$G");
  2087.  
  2088.   sprintf(ver_no2,"WWIV_FP=%04.4X:%04.4X",FP_SEG(funcs), FP_OFF(funcs));
  2089.  
  2090.  
  2091.   i=i1=0;
  2092.   while (environ[i]!=NULL) {
  2093.     if (strncmp(environ[i],"PROMPT=",7)==0)
  2094.       xenviron[i1++]=newprompt;
  2095.     else {
  2096.       if (strncmp(environ[i],"BBS=",4) && (strncmp(environ[i],"WWIV_FP=",8)))
  2097.         xenviron[i1++]=environ[i];
  2098.       }
  2099.     ++i;
  2100.   }
  2101.   if (!getenv("PROMPT"))
  2102.     xenviron[i1++]=newprompt;
  2103.   xenviron[i1++]=ver_no1;
  2104.   xenviron[i1++]=ver_no2;
  2105.   xenviron[i1]=NULL;
  2106.  
  2107. }
  2108.  
  2109. /****************************************************************************/
  2110.  
  2111. void main(int argc, char *argv[])
  2112. {
  2113.   int i,i1;
  2114.   char s[130];
  2115.  
  2116.  
  2117.   i=atoi(argv[1]);
  2118.   ccc=atoi(argv[2]);
  2119.  
  2120.  
  2121.   get_dir(cdir,0);
  2122.  
  2123.   setup_stuff();
  2124.   if (argc>=3) {
  2125.     s[0]=0;
  2126.     for (i1=3; i1<argc; i1++) {
  2127.       strcat(s,argv[i1]);
  2128.       strcat(s," ");
  2129.     }
  2130.     if (i) {
  2131.       if (!init_r()) {
  2132.         if (incom || outcom)
  2133.           screenbottom=thisuser.screenlines-1;
  2134.         else
  2135.           screenbottom=defscreenbottom;
  2136.         screenlinest=screenbottom+1;
  2137.         full_external(s,ccc);
  2138.         pl("Returning...");
  2139.         if (ok_modem_stuff)
  2140.           closeport();
  2141.       } else
  2142.         pl("Couldn't find data");
  2143.     } else
  2144.       do_remote(s,ccc);
  2145.   }
  2146.   cd_to(cdir);
  2147.   if (save_dos)
  2148.     setvect(save_dos, NULL);
  2149.   exit(0);
  2150. }         
  2151.