home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / MCOMM530.ZIP / SMALTERM.C < prev    next >
C/C++ Source or Header  |  1990-06-22  |  37KB  |  1,074 lines

  1.  
  2. /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  3. *      S M A L T E R M  --  D e m o   D r i v e r   f o r   M C o m m 5     *
  4. *                          A s y n c   R o u t i n e s                      *
  5. *                                                                           *
  6. *              Mike Dumdei, 6 Holly Lane, Texarkana TX 75503                *
  7. *                   Split Up the Middle BBS, 214 838-6713                   *
  8. *                                                                           *
  9. *               (Link with comm_s.lib for external functions)               *
  10. *               cl smalterm.c /link comm_s          (Microsoft C)           *
  11. *               tcc smalterm.c comm_s.lib           (Turbo C)               *
  12. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #if !defined(__TURBOC__)
  16.     #include <sys\types.h>
  17. #endif
  18. #include <sys\stat.h>
  19. #include <io.h>
  20. #include <stdlib.h>
  21. #include <conio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <process.h>
  25. #include <stdarg.h>
  26. #include <dos.h>
  27. #include <bios.h>
  28.  
  29. #include "comm.h"                            /* header for async functions */
  30. #include "ansidrv.h"                   /* header for ANSI display routines */
  31. #define KEY_INT
  32. #include "keys.h"                                 /* key definition header */
  33. #if defined(RED)
  34.     #undef RED
  35. #endif
  36. #include "colors.h"                  /* vid mode & color definition header */
  37. #include "extra.h"                               /* extra functions header */
  38.  
  39. #if defined (__TURBOC__)                                        /* Turbo C */
  40.   #define   NO_FILES(x)     findfirst((x), &findbuf, 0)
  41.   #define   find_t          ffblk
  42.   #define   KBHIT           bioskey(1)
  43.   #define   KBREAD          bioskey(0)
  44.   #include  <dir.h>
  45. #else                                                       /* Microsoft C */
  46.   #include  <direct.h>
  47.   #define   NO_FILES(x)     _dos_findfirst((x), _A_NORMAL, &findbuf)
  48.   #define   KBHIT           _bios_keybrd(_KEYBRD_READY)
  49.   #define   KBREAD          _bios_keybrd(_KEYBRD_READ)
  50. #endif
  51.  
  52. #define POP         0
  53. #define PUSH        1              /* used in screen save/restore function */
  54. #define RXBUFSZ     4096                            /* rx ring buffer size */
  55. #define TXBUFSZ     1050                            /* tx ring buffer size */
  56. #define LOST_CARRIER -1
  57. #define NO_INPUT     -1
  58. #define ESC_PRESSED  -2
  59. #define TIMED_OUT    -3
  60.  
  61. #define TRUE          1
  62. #define FALSE         0
  63. #define REG     register
  64.  
  65. typedef unsigned char uchar;
  66. typedef unsigned long ulong;
  67. typedef unsigned int uint;
  68.  
  69.  
  70. /*      f u n c t i o n   d e c l a r a t i o n s     */
  71. extern int main(int argc,char * *argv);
  72. static int proc_keypress(int ch);
  73. static int proc_rxch(int ch);
  74. static int proc_fkey(int keycd);
  75. static int watch_cd(void );
  76. static int rcls(void);
  77. static int toggle_log(void );
  78. static int toggle_echo(void );
  79. static int toggle_lfs(void );
  80. static int toggle_dtr(void );
  81. static int dial_nbr(void );
  82. static int shell_to_dos(void );
  83. static int host_mode(void );
  84. static int shell_remote(void );
  85. static int run_batch(int keycd);
  86. static int exit_term(void );
  87. static int do_help(void );
  88. static int async_tx_str(char *str);
  89. static int async_tx_echo(char *str);
  90. static int screen_pushpop(int flag);
  91. static int chg_parms(void );
  92. static int rx_timeout(int Tenths);
  93. static int waitfor(char *str, int ticks);
  94. static int waitforOK(int ticks );
  95. static int prompt(char *kbuf,char *promptmsg,int maxchars);
  96. static int comprompt(char *kbuf,char *promptmsg,int maxchars);
  97. static int hang_up(void );
  98.  
  99. /*      g l o b a l   v a r i a b l e s     */
  100.  
  101. ASYNC   *port;                               /* pointer to async structure */
  102.  /* default to COM1 */
  103. char    com_str[5] = "COM1";           /* text for port ("COM1" or "COM2") */
  104. int     IOadrs = 0x3f8;                      /* comm port base I/O address */
  105. char    irqm = IRQ4;                         /* mask for selected IRQ line */
  106. char    vctrn = VCTR4;                        /* comm interrupt vector nbr */
  107.  
  108. FILE    *logfil = NULL;                                 /* log file handle */
  109. char    dialstr[40];                           /* dial string storage area */
  110. int     ChkgKbd = 1;              /* watch for ESC during rcv with timeout */
  111. int     ChkgCarrier = 0;        /* monitor carrier during rcv with timeout */
  112. char    buf[200];                                    /* gen purpose buffer */
  113. int     lfs = 0, echo = 0, dtr = 1, autoans = 0;          /* various flags */
  114. struct find_t findbuf;                        /* struct for NO_FILES macro */
  115. int     cyn, hred, grn, hgrn, wht, hblu;           /* text color variables */
  116. int     hostcnt = 0;                        /* toggle to host mode counter */
  117.  
  118. /*
  119.             * * * *    M A I N    * * * *
  120. */
  121. main(int argc, char **argv)
  122. {
  123.     static char params[10] = "1200N81";              /* default parameters */
  124.     char        buf[100];
  125.     int         got_port = FALSE;
  126.     unsigned    i, ch;
  127.  
  128.     initvid();                                /* initialize video routines */
  129.     if (v_mode == CO80)
  130.     {
  131.         cyn = CYN, hred = H_RED, grn = GRN, hgrn = H_GRN,
  132.         wht = WHT, hblu = H_BLU;
  133.     }
  134.     else
  135.     {
  136.         cyn = WHT, hred = H_WHT, grn = WHT, hgrn = H_WHT,
  137.         wht = WHT, hblu = H_WHT;
  138.     }
  139.     v_color = cyn;
  140.     cls();                                             /* clear the screen */
  141.  
  142.     /*  D I S P L A Y   S I G N O N  */
  143.     v_color = hred;
  144.     d_str("SMALTERM - Sample driver for MCOMM5 comm routines\n");
  145.     v_color = grn;
  146.     d_str("    Mike Dumdei, 6 Holly Lane, Texarkana TX 75503\n");
  147.     d_str("    Split Up the Middle BBS     214 838-6713\n");
  148.     v_color = hred;
  149.     d_str("Usage: smalterm {COM1 | COM2} {param str EX: 2400N81}\n\n");
  150.     v_color = cyn;
  151.  
  152.     /*  P R O C E S S   C O M M A N D   L I N E   A R G S  */
  153.     for (i = 1; i < 3; i++)        /* do it twice so args can be any order */
  154.     {                              /*  or have one arg and not the other   */
  155.         if (argc > i)
  156.         {
  157.             if (!got_port && !stricmp(argv[i], "COM1"))
  158.                 got_port = TRUE;
  159.             else if (!got_port && !stricmp(argv[i], "COM2"))
  160.             {
  161.                 got_port = TRUE, strcpy(com_str, "COM2");
  162.                 IOadrs = 0x2f8, irqm = IRQ3, vctrn = VCTR3;
  163.             }
  164.             else if (strlen(argv[i]) < 9)   /* assume a valid param string */
  165.                 strcpy(params, argv[i]);
  166.         }
  167.     }
  168.  
  169.     /*  O P E N   T H E   C O M M   P O R T  */
  170.     port = malloc(sizeof(ASYNC));
  171.     AllocRingBuffer(port, RXBUFSZ, TXBUFSZ, 1);    /* alloc FAR ring bufrs */
  172.     if ((ch = async_open(port, IOadrs, irqm, vctrn, params)) != R_OK)
  173.     {
  174.         /* couldn't open the port code */
  175.         strsum(buf, "SMALTERM: Couldn't open ", com_str, " using ",
  176.               params, " for parameters -- ABORTING\n\n", NULL);
  177.         d_str(buf);
  178.         strsum(buf, "Error code = ", itoa(ch, &buf[50], 10), "\n\n", NULL);
  179.         d_str(buf);
  180.         exit(ch);
  181.     }
  182.  
  183.     /* opened OK code */
  184.  
  185.     d_str(strsum(buf, "SMALTERM: ", com_str, " -- PORT OPENED\n\n", NULL));
  186.     d_str("Press F1 for HELP screen\n");           /* tell how to get help */
  187.     /* display status line */
  188.     SETWND(0, 0, 23, 79);                           /* set the window size */
  189.     d_strnat(24, 0, '═', cyn, 80);                          /* paint a bar */
  190.     d_msgat(24, 60, hgrn, strupr(com_str));                /* display port */
  191.     d_msgat(24, 65, hgrn, strupr(params));               /* display params */
  192.     d_msgat(24, 10, hgrn, "DTR");                           /* DTR is high */
  193.     *dialstr = '\0';                              /* clear out dial string */
  194.     tickhookset(1);        /* enable 'ticker' variable for timer functions */
  195.     if (!async_carrier(port))
  196.     {
  197.         async_tx_str("ATE1V1\r");                       /* reset the modem */
  198.         waitforOK(9);
  199.     }
  200.     /*
  201.         T H I S   I S   T H E   M A I N   I N G R E D I E N T
  202.     */
  203.     while (1)
  204.     {
  205.         /* turn off auto answer if enabled by Host mode */
  206.         if (autoans && !async_carrier(port))
  207.         {
  208.             DELAY_TENTHS(2);
  209.             async_tx_str("ATS0=0\r");
  210.             waitforOK(9);
  211.             autoans = 0;
  212.         }
  213.          /* check the keyboard */
  214.         if (KBHIT)
  215.             proc_keypress(KBREAD);
  216.          /* check the serial port */
  217.         if (async_rxcnt(port))
  218.             proc_rxch(async_rx(port));
  219.  
  220.     }
  221. }
  222.  
  223. int proc_rxch(int ch)
  224. {
  225.     REG int ch2;
  226.  
  227.     ch2 = ch & 0xff;
  228.  
  229.     if (ch2 == '\r' && lfs)
  230.         ch2 = '\n';              /* translate to nl if supplying lin feeds */
  231.     if (ch2 != '~')
  232.         hostcnt = 0;
  233.     else
  234.     {
  235.         if (++hostcnt >= 10)               /* ten ~'s puts it in host mode */
  236.         {
  237.             host_mode();
  238.             hostcnt = 0;
  239.             return (ch);
  240.         }
  241.     }
  242.     d_ch((char)ch2);     /* dsply if got one -- 'd_ch' supports ANSI codes */
  243.     if (logfil && !v_ansiseq)
  244.         fputc(ch2, logfil);               /* write char to logfile if open */
  245.     return (ch);
  246. }
  247.  
  248. /*
  249.         p r o c e s s   k e y p r e s s
  250. */
  251. int proc_keypress(REG int ch)
  252. {
  253.     REG int ch2;
  254.  
  255.     if (!(ch2 = ch & 0xff))
  256.         proc_fkey(ch);                  /* process if extended key pressed */
  257.     else
  258.     {
  259.     async_tx(port, (char)ch2);                          /* send ch to port */
  260.         if (echo && ch != X_ESC)
  261.         {
  262.             if (ch2 == '\r' && lfs)
  263.                 ch2 = '\n';                  /* make it a nl if adding lfs */
  264.         d_ch((char)ch2);                               /* display the char */
  265.             if (logfil && !v_ansiseq)
  266.                 fputc(ch2, logfil);            /* write to logfile if open */
  267.         }
  268.     }
  269.     return (ch);
  270. }
  271.  
  272. /*
  273.         p r o c e s s   e x t e n d e d   k e y s
  274. */
  275. int     proc_fkey(REG int keycd)
  276. {
  277.     static struct
  278.     {
  279.     int jval;
  280.     int (*fun)();
  281.     } *f, funtbl[] = {
  282.         { ALT_C, rcls },            { ALT_E, toggle_echo },
  283.         { ALT_X, exit_term },       { ALT_S, shell_to_dos },
  284.         { ALT_D, dial_nbr },        { PGUP, run_batch },
  285.         { PGDN, run_batch },        { ALT_H, hang_up },
  286.         { ALT_L, toggle_lfs},       { F2, toggle_log },
  287.         { F10, host_mode },         { ALT_P, chg_parms },
  288.         { F9, watch_cd },    { 0, NULL }             };
  289.  
  290.     if (keycd == F1)
  291.         keycd = do_help();         /* do a help screen first if F1 pressed */
  292.     for (f = funtbl; f->jval != keycd && f->jval; f++)
  293.         ;                                       /* lookup function for key */
  294.     if (f->jval)
  295.         return ((f->fun)(keycd));/* execute it if found & return exit code */
  296.     else
  297.         return (0);                      /* else do nothing and return a 0 */
  298. }
  299.  
  300. /*
  301.         w a t c h   c a r r i e r  d e t e c t
  302. */
  303. int     watch_cd(void)
  304. {
  305.     return(watchdogset(1, port->ComBase));
  306. }
  307.  
  308. /*
  309.         r e s e t   c o l o r,   c l e a r   s c r e e n
  310. */
  311. int     rcls(void)
  312. {
  313.     v_color = cyn;
  314.     cls();
  315.     return 0;
  316. }
  317.  
  318. /*
  319.         t o g g l e   l o g   f i l e
  320. */
  321. int     toggle_log(void)
  322. {
  323.     if (logfil == NULL)
  324.     {
  325.         if (NULL != (logfil = fopen("smalterm.log", "ab")))
  326.             d_msgat(24, 25, hgrn, "LOG");              /* LOG file is open */
  327.     }
  328.     else
  329.     {
  330.         fclose(logfil);
  331.         logfil = NULL;
  332.         d_msgat(24, 25, cyn, "═══");                   /* LOG file is open */
  333.     }
  334.     return 0;
  335. }
  336.  
  337. /*
  338.         t o g g l e   e c h o    f u n c t i o n
  339. */
  340. int     toggle_echo(void)
  341. {
  342.     if (echo ^= 1)                                          /* toggle flag */
  343.         d_msgat(24, 15, hgrn, "ECHO");                       /* ECHO is on */
  344.     else
  345.         d_msgat(24, 15, cyn, "════");                       /* ECHO is off */
  346.     return 0;
  347. }
  348.  
  349. /*
  350.         t o g g l e   l f ' s    f u n c t i o n
  351. */
  352. int     toggle_lfs(void)
  353. {
  354.     if (lfs ^= 1)                                           /* toggle flag */
  355.         d_msgat(24, 21, hgrn , "LF");                       /* LF's are on */
  356.     else
  357.         d_msgat(24, 21, cyn, "══");                        /* LF's are off */
  358.     return 0;
  359. }
  360.  
  361. /*
  362.         t o g g l e   d t r    f u n c t i o n
  363. */
  364. int     toggle_dtr(void)
  365. {
  366.     async_dtr(port, (dtr ^= 1));                             /* toggle DTR */
  367.     if (dtr)
  368.         d_msgat(24, 10, hgrn, "DTR");                       /* DTR is high */
  369.     else
  370.         d_msgat(24, 10, (hred | BLNK), "DTR");               /* DTR is low */
  371.     return 0;
  372. }
  373.  
  374. /*
  375.         d i a l   a   n u m b e r
  376. */
  377. int     dial_nbr(void)
  378. {
  379.     char    lbuf[25];
  380.     static char dmsg[] = "Enter number (proceed with P if pulse dial) ->";
  381.  
  382.     /* input a phone number */
  383.     if (prompt(lbuf, dmsg, 24))                        /* input the number */
  384.         return 0;
  385.     if (*lbuf == 'p' || *lbuf == 'P')
  386.         strsum(dialstr, "ATDP", &lbuf[1], "\r", NULL);
  387.     else                                          /* build the dial string */
  388.         strsum(dialstr, "ATDT", lbuf, "\r", NULL);
  389.     async_tx_str(dialstr);                              /* dial the number */
  390.     return 0;
  391. }
  392.  
  393. /*
  394.         s h e l l   t o   D O S   f u n c t i o n
  395. */
  396. int     shell_to_dos(void)
  397. {
  398.     char    savdcolr, lbuf[2];
  399.     char    cmdcom[60];
  400.  
  401.     if (screen_pushpop(PUSH))                       /* save current screen */
  402.     {
  403.         cls();
  404.         savdcolr = v_color, v_color = hred;
  405.         *cmdcom = '\0';
  406.         strcpy(cmdcom, getenv("COMSPEC"));
  407.         if (!*cmdcom)
  408.             strcpy(cmdcom, "COMMAND.COM");
  409.         d_str("Type EXIT to return to SMALTERM."); /* tell how to get back */
  410.         v_color = savdcolr;
  411.         spawnlp(P_WAIT, cmdcom, cmdcom, NULL);        /* spawn COMMAND.COM */
  412.         screen_pushpop(POP);     /* fix up screen & ANSI sequence variable */
  413.     }
  414.     else
  415.         prompt(lbuf, "Not enough memory, Press ENTER to continue\a", 0);
  416.     screen_pushpop(POP);         /* fix up screen & ANSI sequence variable */
  417.     return 0;
  418. }
  419.  
  420. /*
  421.         h o s t   m o d e
  422. */
  423. int     host_mode(void)
  424. {
  425.     long    rto;
  426.     char    i, lbuf[62], ch = '\0';
  427.     int     savdcolr;
  428.  
  429.     static char menu[] =
  430. "\r\n\r\nU)pload    D)ownload    S)hell to DOS    P)age operator    H)ang up\
  431. \r\n      Enter selection ---> ";
  432.  
  433.     static char zmrcvmsg[] =
  434. "u\r\n\r\nReady to receive files using Zmodem, start your upload\r\n";
  435.  
  436.     static char zmsndmsg1[] =
  437. "d\r\n\r\nEnter the filename(s) you wish to download at the prompt, one file\r\n\
  438. per prompt.  Wild cards are accepted.  Press ENTER when all files have\r\n\
  439. been entered or press ESC to abort the transfer.\r\n";
  440.  
  441.     static char zmsndmsg2[] =
  442. "\r\nReady to transmit files using Zmodem, start your download\r\n";
  443.  
  444.     static char pagemsg[] =
  445. "p\r\n\r\nPaging remote, press ESC if no answer\r\n";
  446.     
  447.     d_str("\nHost mode activated, Press ESC to exit Host,\n");
  448.     d_str("Press the space bar to chat\n");
  449.     async_txflush(port);
  450.     async_rxflush(port);                             /* clean out the port */
  451.     if (lfs)
  452.         toggle_lfs();
  453.     if (echo)
  454.         toggle_echo();
  455.     while (ch != ESC && ch != ' ')
  456.     {
  457.         if (!async_carrier(port))
  458.         {
  459.             DELAY_TENTHS(2);
  460.             async_tx_str("ATS0=1\r");         /* set modem for auto answer */
  461.             waitforOK(9);                             /* throw away the OK */
  462.             autoans = 1;       /* set flag showing AA enabled by Host mode */
  463.             d_str("\r\nAwaiting caller\r\n");
  464.             while (1)         /* loop till carrier detected or ESC pressed */
  465.             {
  466.                 if (KBHIT && ((ch = KBREAD) == ESC))
  467.                     break;    
  468.                 if (async_carrier(port))
  469.                 {
  470.                     DELAY_TENTHS(5);/* half sec delay to let things settle */
  471.                     async_rxflush(port);             /* clean out the port */
  472.                     async_tx_echo("Welcome to Smalterm Host\r\n");
  473.                     break;
  474.                 }
  475.             }
  476.         }
  477.         if (async_carrier(port))
  478.             async_tx_echo(menu);
  479.         while (async_carrier(port) && ch != ESC && ch != ' ')
  480.         {
  481.             if (KBHIT)
  482.             {
  483.                 switch (ch = KBREAD)
  484.                 {
  485.                   case ESC:
  486.                     hang_up();                        /* hang up on caller */
  487.                     d_str("\r\nCaller aborted");
  488.                     continue;                         /* break out of loop */
  489.                   case ' ':
  490.                     async_tx_echo("\r\nEntering chat mode\r\nHello\r\n");
  491.                     toggle_lfs();
  492.                     toggle_echo();
  493.                     continue;
  494.                 }
  495.             }
  496.             switch (async_rx(port) & 0xff)
  497.             {
  498.               case 'u':
  499.               case 'U':
  500.                 async_tx_echo(zmrcvmsg);
  501.                 while (!async_stat(port, B_TXEMPTY | B_CD))
  502.                     ;
  503.                 DELAY_TENTHS(2);
  504.                 if (!async_carrier(port))
  505.                     break;
  506.                 if (screen_pushpop(PUSH))           /* save current screen */
  507.                 {
  508.                     cls();
  509.                     savdcolr = v_color, v_color = hred;
  510.                     d_str("Remote is uploading ...");
  511.                     v_color = savdcolr;
  512.                     system("RCV.BAT");                /* receive the files */
  513.                 }
  514.                 screen_pushpop(POP);                 /* restore the screen */
  515.                 DELAY_TENTHS(2);
  516.                 break;
  517.               case 'd':
  518.               case 'D':
  519.                 async_tx_echo(zmsndmsg1);
  520.                 while (!async_stat(port, B_TXEMPTY | B_CD))
  521.                     ;
  522.                 strcpy(buf, "SND.BAT");
  523.                 i = '0';
  524.                 while(++i < '9' && async_carrier(port))
  525.                 {
  526.                     strcpy(lbuf, "\r\nEnter filename #  : ");
  527.                     lbuf[18] = i;
  528.                     comprompt(lbuf, lbuf, 60);
  529.                     if (!*lbuf || *lbuf == ESC)
  530.                         break;
  531.                     if (NO_FILES(lbuf))
  532.                     {
  533.                         async_tx_echo("\r\nFile not found");
  534.                         --i;
  535.                         continue;
  536.                     }
  537.                     if (strlen(buf) + strlen(lbuf) > 125)
  538.                     {
  539.                         async_tx_echo("\r\nFile buffer full");
  540.                         continue;
  541.                     }
  542.                     strcat(buf, " ");
  543.                     strcat(buf, lbuf);
  544.                 }
  545.                 if (*lbuf == ESC || !async_carrier(port))
  546.                     break;
  547.                 async_tx_echo(zmsndmsg2);
  548.                 while (!async_stat(port, B_TXEMPTY | B_CD))
  549.                     ;
  550.                 DELAY_TENTHS(2);
  551.                 if (screen_pushpop(PUSH))           /* save current screen */
  552.                 {
  553.                     cls();
  554.                     savdcolr = v_color, v_color = hred;
  555.                     d_str("Remote is downloading ...");
  556.                     v_color = savdcolr;
  557.                     system(buf);                         /* send the files */
  558.                 }
  559.                 screen_pushpop(POP);                 /* restore the screen */
  560.                 break;
  561.               case 's':
  562.               case 'S':
  563.                 async_tx_echo("s\r\n");
  564.                 watchdogset(1, port->ComBase);          /* enable watchdog */
  565.                 shell_remote();                         /* do remote shell */
  566.                 watchdogset(0, 0);                     /* disable watchdog */
  567.                 break;
  568.               case 'p':
  569.               case 'P':
  570.                 ch = '\0';
  571.                 async_tx_echo(pagemsg);
  572.                 d_str("\a\a\a");
  573.                 SET_TO_SECS(rto, 15);
  574.                 while (async_carrier(port) && async_rx(port) != ESC
  575.                   && (!KBHIT || (ch = KBREAD) != ' '))
  576.                 {
  577.                     if (timed_out(&rto))
  578.                     {
  579.                         d_str("\a\a\a");
  580.                         SET_TO_SECS(rto, 15);
  581.                     }
  582.                 }
  583.                 if (ch == ' ')
  584.                 {
  585.                     async_tx_echo("\r\nEntering chat mode\r\nHello\r\n");
  586.                     toggle_lfs();
  587.                     toggle_echo();
  588.                     continue;
  589.                 }
  590.                 break;
  591.               case 'h':
  592.               case 'H':
  593.                 async_tx_echo(
  594.                   "h\r\nSmalterm Host going offline ...\r\n");
  595.                 while (!async_stat(port, B_TXEMPTY))
  596.                     ;
  597.                 hang_up();
  598.                 d_str("\r\nCaller offline");
  599.                 continue;                             /* break out of loop */
  600.               default:
  601.                 continue;                                /* skips the menu */
  602.             }
  603.             async_tx_echo(menu);
  604.         }
  605.     }
  606.     return 0;
  607. }
  608.  
  609.  
  610. /*
  611.         r e m o t e   s h e l l   t o   D O S   f u n c t i o n
  612. */
  613. int     shell_remote(void)
  614. {
  615.     char    savdcolr, lbuf[2];
  616.     char    cmdcom[60];
  617.     int      x, inhdl, outhdl, errhdl;
  618.  
  619.     if (screen_pushpop(PUSH))                       /* save current screen */
  620.     {
  621.         cls();
  622.         savdcolr = v_color, v_color = hred;
  623.         *cmdcom = '\0';
  624.         strcpy(cmdcom, getenv("COMSPEC"));
  625.         if (!*cmdcom)
  626.             strcpy(cmdcom, "COMMAND.COM");
  627.         d_str("Executing remote shell to DOS ...");
  628.         v_color = savdcolr;
  629.         async_txflush(port);
  630.         async_tx_str("\r\nShelling to DOS, Type EXIT when finished.\r\n");
  631.         while (!async_stat(port, B_TXEMPTY))
  632.             ;                             /* wait for message to get there */
  633.         DELAY_TENTHS(1);                         /* wait a little bit more */
  634.         async_stop(port);                       /* let go of the comm port */
  635.  
  636.         /* make stdin, stdout, & stderr refer to com port */
  637.         x = open(com_str, O_RDWR | O_BINARY);
  638.         inhdl = dup(0);
  639.         outhdl = dup(1);
  640.         errhdl = dup(2);
  641.         dup2(x, 0);
  642.         dup2(x, 1);
  643.         dup2(x, 2);
  644.  
  645.         spawnlp(P_WAIT, cmdcom, cmdcom, NULL);        /* spawn COMMAND.COM */
  646.  
  647.         /* restore stdin, stdout, & stderr */
  648.         close(x);
  649.         dup2(inhdl, 0);
  650.         close(inhdl);
  651.         dup2(outhdl, 1);
  652.         close(outhdl);
  653.         dup2(errhdl, 2);
  654.         close(errhdl);
  655.  
  656.         async_restart(port);                      /* re-init the comm port */
  657.     }
  658.     else
  659.         prompt(lbuf, "Not enough memory, Press ENTER to continue\a", 0);
  660.     screen_pushpop(POP);         /* fix up screen & ANSI sequence variable */
  661.     return 0;
  662. }
  663.  
  664. /*
  665.         r u n   b a t c h   f i l e
  666. */
  667. int     run_batch(int keycd)
  668. {
  669.     REG     char *p1;
  670.     char    savdcolr;
  671.     char    b_buf[60];
  672.  
  673.     /* get batch name and parameters */
  674.     p1 = b_buf;
  675.     strcpy (p1, (keycd == PGUP) ? "SND.BAT " : "RCV.BAT ");
  676.     p1 += strlen(p1);
  677.     if (prompt(p1, "Enter batch parameters ->", 50) == ESC_PRESSED)
  678.         return 0;
  679.     if (screen_pushpop(PUSH))                       /* save current screen */
  680.     {
  681.         cls();
  682.         savdcolr = v_color, v_color = hred;
  683.         d_str(strsum(buf, "Executing: ", b_buf, "\n\n", NULL));
  684.         v_color = savdcolr;
  685.         system(b_buf);                               /* run the batch file */
  686.     }
  687.     else
  688.         prompt(b_buf, "Not enough memory, Press ENTER to continue\a", 0);
  689.     screen_pushpop(POP);         /* fix up screen & ANSI sequence variable */
  690.     d_str("\a\a\a\a");                                  /* signal finished */
  691.     return 0;
  692. }
  693.  
  694. /*
  695.         e x i t   f o r   g o o d   f u n c t i o n
  696. */
  697. int     exit_term(void)
  698. {
  699.     char    lbuf[2];
  700.  
  701.     switch (prompt(lbuf, "Exit SmalTerm (Y, n)?", 1))
  702.     {
  703.       case ESC_PRESSED:
  704.         return 0;
  705.       case NO_INPUT:
  706.         break;
  707.       default:
  708.         if (*lbuf != 'Y' && *lbuf != 'y')
  709.             return 0;
  710.     }
  711.     tickhookset(0);                    /* disable the TIMER interrupt hook */
  712.     async_close(port);                                   /* close the port */
  713.     SETWND(0, 0, 24, 79);
  714.     cls();                                             /* clear the screen */
  715.     exit (0);                                               /* back to DOS */
  716. }
  717.  
  718.  
  719. /*
  720.         h e l p    f u n c t i o n
  721. */
  722. int     do_help()
  723. {
  724.     /* not the best way to do video but shows ANSI  */
  725.     /*  capabilities of ANSI8MSC display functions  */
  726.     static char *help_scrn[] =
  727.     {
  728. "\33[1;1H\33[0;1;37;40m┌──   \33[31mSmalTerm HELP  [F1]   \33[37m─────────────────────────────────┐\n",
  729. "│\33[60C│\n",
  730. "│  \33[0;32mALT-C:  \33[36mClear the screen\33[8C\33[32mPGUP :  \33[36mExecute SND.BAT   \33[1;37m│\n",
  731. "│  \33[0;32mALT-D:  \33[36mDial a number\33[11C\33[32mPGDN :  \33[36mExecute RCV.BAT   \33[1;37m│\n",
  732. "│  \33[0;32mALT-E:  \33[36mToggle echo\33[15C\33[32mF2 :  \33[36mToggle log file   \33[1;37m│\n",
  733. "│  \33[0;32mALT-H:  \33[36mHang Up\33[19C\33[32mF9 :  \33[36mEnable watchdog   \33[1;37m│\n",
  734. "│  \33[0;32mALT-L:  \33[36mToggle line feeds\33[8C\33[32mF10 :  \33[36mEnable host mode  \33[1;37m│\n",
  735. "│  \33[0;32mALT-P:  \33[36mChange parameters\33[33C\33[1;37m│\n",
  736. "│  \33[0;32mALT-S:  \33[36mShell to DOS\33[12C\33[32mALT-X:  \33[36mExit to DOS\33[7C\33[1;37m│\n",
  737. "├────────────────────────────────────────────────────────────┤\n",
  738. "│  \33[0;37mDemo comm program written using MComm5 async routines,    \33[1;37m│\n",
  739. "│  \33[0;37mANSI_xxC video routines, and EXTRA functions.\33[13C\33[1;37m│\n",
  740. "│   \33[0;37mMike Dumdei, 6 Holly Lane, Texarkana TX 75503\33[12C\33[1;37m│\n",
  741. "│   \33[0;37mSplit Up the Middle BBS  ----  (214) 838-6713\33[12C\33[1;37m│\n",
  742. "└────────────────────────────────────────────────────────────┘\33[0;36m",
  743. NULL
  744.     };
  745.     register char **p1;
  746.     char    *lbuf = 0;
  747.     int     rval = 0, x;
  748.  
  749.     x = v_ansiseq, v_ansiseq = 0;    /* in case an ANSI seq is in progress */
  750.     if ((lbuf = malloc(SCRBUF(15, 62))) != NULL)/* malloc buf to save scrn */
  751.     {
  752.         pu_scrnd(3, 9, 15, 62, lbuf);            /* push area used by HELP */
  753.         SETWND(3, 9, 17, 70);
  754.         cls();
  755.         v_scrlm = 0;   /* don't want to scroll when at the btm of the wind */
  756.     }
  757.     for (p1 = help_scrn; *p1; p1++)  /* display the ANSI style help screen */
  758.         d_str(*p1);
  759.     rval = KBREAD;                                   /* return key pressed */
  760.     if (lbuf)
  761.     {
  762.         SETWND(0, 0, 23, 79);                     /* reset the window size */
  763.         v_scrlm = 1;                              /* reset the scroll mode */
  764.         po_scrnd(lbuf);                                  /* restore screen */
  765.         free(lbuf);                                      /* release memory */
  766.     }
  767.     v_ansiseq = x;                                  /* restore ANSI status */
  768.     return (rval);               /* return extended key if pressed, else 0 */
  769. }
  770.  
  771. /*
  772.         a s y n c _ t x _ s t r   f u n c t i o n
  773. */
  774. int     async_tx_str(char *str)
  775. {
  776.     return(async_txblk(port, str, strlen(str)));
  777. }
  778.  
  779. /*
  780.         a s y n c _ t x _ e c h o   f u n c t i o n
  781. */
  782. int     async_tx_echo(register char *str)
  783. {
  784.  
  785.     while (*str && async_carrier(port))
  786.     {
  787.         d_ch(*str);
  788.         async_tx(port, *str++);
  789.         while (!async_stat(port, B_TXEMPTY | B_CD))
  790.             ;
  791.     }
  792.     return 0;
  793. }
  794.  
  795.  
  796. /*
  797.         s c r e e n   s a v e / r e s t o r e   f u n c t i o n
  798. */
  799. int     screen_pushpop(int flag)
  800. {
  801.     static char *savscrn = NULL;
  802.     static ulong   savdwndsz;
  803.     static int     x;
  804.  
  805.     if (flag == PUSH)
  806.     {
  807.         x = v_ansiseq, v_ansiseq = 0;/* in case an ANSI seq is in progress */
  808.     if ((savscrn = malloc(SCRBUF(25, 80))) != NULL)
  809.         {
  810.             savdwndsz = v_wndsz;
  811.             SETWND(0, 0, 24, 79);         /* reset window to entire screen */
  812.             pu_scrnd(0, 0, 25, 80, savscrn);/* save current screen in bufr */
  813.             return (1);                                  /* return success */
  814.         }
  815.         else
  816.             return (0);                                   /* return failed */
  817.     }
  818.     else
  819.     {
  820.         v_ansiseq = x;                              /* restore ANSI status */
  821.         if (savscrn != NULL)
  822.         {
  823.             po_scrnd(savscrn);                /* restore the pushed screen */
  824.             free(savscrn);                 /* release screen buffer memory */
  825.             savscrn = NULL;
  826.             v_wndsz = savdwndsz;                  /* reset the screen size */
  827.             return (1);                                  /* return success */
  828.         }
  829.         else
  830.             return (0);                 /* return failed if nothing pushed */
  831.     }
  832. }
  833.  
  834. /*
  835.         c h a n g e   p a r a m e t e r s
  836. */
  837. int     chg_parms(void)
  838. {
  839.     char    lbuf[12];
  840.  
  841.     /* get new parmameters */
  842.     if (prompt(lbuf, "Enter parameters (Ex: 1200N81) ->", 10))
  843.         return 0;
  844.     if (async_setbpds(port, lbuf) == R_OK)            /* change parameters */
  845.     {
  846.         d_nchat(24, 65, '═', cyn, 10, HORZ);
  847.         d_msgat(24, 65, hgrn, strupr(lbuf));             /* display params */
  848.     }
  849.     else
  850.         prompt(lbuf, "Invalid parameters, Press ENTER to continue\a", 0);
  851.     return 0;
  852. }
  853.  
  854. /*
  855.         r e c e i v e   w i t h   t i m e o u t
  856.             Gets a char from the async port 'port'.  Times out after so many
  857.             tenths of a second.
  858. */
  859. int     rx_timeout(int Tenths)
  860. {
  861.     uint StatChar;
  862.     long rto;
  863.  
  864.     /* if char is in buffer return it with no kbd or timeout chks */
  865.     if (!(B_RXEMPTY & (StatChar = async_rx(port))))
  866.         return (StatChar & 0xff);
  867.  
  868.     /* if no char in rxbufr, set up timeout val & wait for char or timeout */
  869.     SET_TO_TENTHS(rto, Tenths);
  870.     while (1)
  871.     {
  872.         if (!(B_RXEMPTY & (StatChar = async_rx(port))))
  873.             return (StatChar & 0xff);     /* return with char if one ready */
  874.         if (ChkgCarrier && (B_CD & StatChar))
  875.             return (LOST_CARRIER);               /* ret if carrier dropped */
  876.         if (ChkgKbd && KBHIT && (char)KBREAD == ESC)
  877.             return (ESC_PRESSED);     /* ret if watching Kbd & ESC pressed */
  878.         if (timed_out(&rto))
  879.             return (TIMED_OUT);                  /* ret if ran out of time */
  880.     }
  881. }
  882.  
  883. /*
  884.         w a i t   f o r   m o d e m   t o   s e n d   O K
  885. */
  886. int     waitforOK(int ticks)
  887. {
  888.     char lbuf[2];
  889.  
  890.     if (waitfor("OK", ticks))
  891.         prompt(lbuf, "Modem does not respond,  Press ENTER to continue\a", 0);
  892.     return 0;
  893. }
  894.  
  895. /*
  896.         w a i t f o r   f u n c t i o n
  897. */
  898. int     waitfor(char *str, int ticks)
  899. {
  900.     REG char *p1, *lbuf;
  901.     long    to;
  902.     char    ch, *end;
  903.     int     matchlen;
  904.  
  905.     if (!(matchlen = strlen(str)))
  906.         return (0);
  907.     set_timeout(&to, ticks);
  908.     lbuf = calloc(matchlen + 1, 1);
  909.     p1 = lbuf - 1;
  910.     end = p1 + matchlen;
  911.     while (1)
  912.     {
  913.         if (timed_out(&to))
  914.         {
  915.             free(lbuf);
  916.             return (TIMED_OUT);
  917.         }
  918.         if (KBHIT && proc_keypress(KBREAD) == X_ESC)
  919.         {
  920.             free(lbuf);
  921.             return (ESC_PRESSED);
  922.         }
  923.         if (!async_rxcnt(port))
  924.             continue;
  925.         ch = (char)proc_rxch(async_rx(port));
  926.         if (p1 != end)
  927.         {
  928.             *++p1 = ch;
  929.             if (p1 != end)
  930.                 continue;
  931.         }
  932.         else
  933.         {
  934.             memmove(lbuf, &lbuf[1], matchlen);
  935.             *p1 = ch;
  936.         }
  937.         if (*lbuf == *str && !memicmp(lbuf, str, matchlen))
  938.         {
  939.             free(lbuf);
  940.             return (0);
  941.         }
  942.     }
  943. }
  944.  
  945. /*
  946.         p r o m p t   f o r   k e y b o a r d   i n p u t
  947. */
  948. int     prompt(char *kbuf, char *promptmsg, int maxchars)
  949. {
  950.     REG     char *p1;
  951.     char    ch = 0, *scrnbuf, *endkbuf;
  952.     int     i, x, y, boxtop, boxlft, boxlen;
  953.  
  954.     x = v_ansiseq, v_ansiseq = 0;                /* save ANSI seq variable */
  955.     y = v_color, v_color = hblu;                   /* set the color to use */
  956.     boxlen = strlen(promptmsg) + maxchars + 6;
  957.     boxlft = (80 - boxlen) / 2;
  958.     boxtop = (v_btm - 5) / 2;                  /* calculate box dimensions */
  959.     scrnbuf = malloc(SCRBUF(5, boxlen));
  960.     pu_scrnd(boxtop, boxlft, 5, boxlen, scrnbuf);  /* save the screen area */
  961.     for (i = 1; i < 4; i++)
  962.         d_nchat(boxtop + i, boxlft + 1, ' ', wht, boxlen - 2, HORZ);
  963.     d_chat(boxtop, boxlft, '╔');
  964.     d_nchat(boxtop, boxlft + 1, '═', hblu, boxlen - 2, HORZ);
  965.     d_chat(boxtop, boxlft + boxlen - 1, '╗');
  966.     d_nchat(boxtop + 1, boxlft, '║', hblu, 3, VERT);
  967.     d_chat(boxtop + 4, boxlft, '╚');
  968.     d_nchat(boxtop + 4, boxlft + 1, '═', hblu, boxlen - 2, HORZ);
  969.     d_chat(boxtop + 4, boxlft + boxlen - 1, '╝');            /* draw a box */
  970.     d_nchat(boxtop + 1, boxlft + boxlen - 1, '║', hblu, 3, VERT);
  971.     d_msgat(boxtop + 2, boxlft + 3, hred, promptmsg);
  972.     loc(boxtop + 2, boxlft + strlen(promptmsg) + 4); /* display the prompt */
  973.     v_color = wht;
  974.     for (p1 = kbuf, endkbuf = p1 + maxchars; ch != '\r' && ch != ESC;)
  975.     {
  976.         ch = KBREAD & 0xff;                                  /* get a char */
  977.         if (ch != '\r' && ch != ESC)
  978.         {
  979.             if (ch == '\b')
  980.             {
  981.                 if (p1 > kbuf)                 /* backspace key processing */
  982.                     --p1, d_ch(ch);
  983.                 continue;
  984.             }
  985.             else if (p1 != endkbuf && isprint(ch))
  986.             {
  987.                 d_ch(ch), *p1++ = ch;                /* put char in buffer */
  988.                 continue;
  989.             }
  990.             d_ch('\a');               /* beep if max chars already entered */
  991.         }
  992.         *p1 = '\0';                                /* terminate the buffer */
  993.     }
  994.     po_scrnd(scrnbuf);
  995.     free(scrnbuf);
  996.     v_ansiseq = x, v_color = y;              /* restore screen & variables */
  997.     if (ch == ESC)
  998.         return (ESC_PRESSED);                /* return this if ESCaped out */
  999.     if (p1 == kbuf)
  1000.         return (NO_INPUT);                     /* return val if ENTER only */
  1001.     return (0);                            /* return val if got some input */
  1002. }
  1003.  
  1004. /*
  1005.         r e m o t e   p r o m p t   f u n c t i o n
  1006. */
  1007. int     comprompt(char *kbuf, char *promptmsg, int maxchars)
  1008. {
  1009.     REG     char *p1;
  1010.     char    ch = 0, *endkbuf;
  1011.  
  1012.     async_tx_echo(promptmsg);
  1013.     for (p1 = kbuf, endkbuf = p1 + maxchars; ch != '\r' && ch != ESC;)
  1014.     {
  1015.         if (KBHIT && (KBREAD == X_ESC) || !async_carrier(port))
  1016.             ch = ESC;                       /* ck for local operator abort */
  1017.         else
  1018.             ch = async_rx(port);
  1019.         if (!ch)                             /* get a char from the remote */
  1020.             continue;
  1021.         if (ch != '\r' && ch != ESC)
  1022.         {
  1023.             if (ch == '\b')
  1024.             {
  1025.                 if (p1 > kbuf)                 /* backspace key processing */
  1026.                     --p1, d_ch(ch), async_tx(port, ch);
  1027.                 continue;
  1028.             }
  1029.             else if (p1 != endkbuf && isprint(ch))
  1030.             {
  1031.                 d_ch(ch), async_tx(port, ch), *p1++ = ch; /* put ch in bfr */
  1032.                 continue;
  1033.             }
  1034.             async_tx(port, '\a');     /* beep if max chars already entered */
  1035.         }
  1036.         *p1 = '\0';                                /* terminate the buffer */
  1037.     }
  1038.     if (ch == ESC)
  1039.         return (*kbuf = ESC);
  1040.     if (p1 == kbuf)
  1041.         return (*kbuf = '\0');                 /* return val if ENTER only */
  1042.     return (p1 - kbuf);
  1043. }
  1044.  
  1045. /*
  1046.         h a n g   u p   t h e   p h o n e
  1047. */
  1048. int     hang_up(void)
  1049. {
  1050.     long rto;
  1051.  
  1052.     if (dtr)
  1053.         toggle_dtr();
  1054.     async_txflush(port);
  1055.     async_rxflush(port);
  1056.     SET_TO_TENTHS(rto, 3);
  1057.     while (!timed_out(&rto))
  1058.     {
  1059.         if (!async_carrier(port))
  1060.         {
  1061.             toggle_dtr();
  1062.             return 0;
  1063.         }
  1064.     }
  1065.     toggle_dtr();
  1066.     DELAY_TENTHS(11);
  1067.     async_tx_str("+++");
  1068.     DELAY_TENTHS(13);
  1069.     async_tx_str("ATH0\r");
  1070.     waitforOK(24);
  1071.     return 0;
  1072. }
  1073.  
  1074.