home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / LITECOMM / QBTTL.C < prev    next >
Text File  |  1993-12-01  |  24KB  |  1,090 lines

  1. /*
  2. **  QBTTL is a complete, if somewhat limited, terminal emulation
  3. **  program  designed to demonstrate the use of the LiteComm(tm)
  4. **  ToolBox.  The executable version is included so that you can
  5. **  try it out while viewing the code.  To successfully create a
  6. **  new version of QBTTL, you must have the XMODEM and WXMODEM engines
  7. **  which are provided as part of your registration package.  The
  8. **  QUICKB functionality is derived from public domain code, and adapted
  9. **  by Information Technology for use with LiteComm.
  10. **  Also note that the windowing parts of the program are based upon
  11. **  Vitamin-C by Creative Programming.  You must have this product
  12. **  to successfully compile a new version of QBTTL.
  13. **  While non-registered users cannot create a new version of QBTTL,
  14. **  you may still compile the QBTTL program.  All required header files
  15. **  are included in the shareware archive
  16. **
  17. **  QBTTL functions as a vidtex terminal when used in conjuction with
  18. **  CompuServe.  In addition, it will run on any communications port from
  19. **  1 thru 4, defaulting to port 2 (COM2).  To execute QBTTL on other than
  20. **  COM2, specify 
  21. **    QBTTL n
  22. **  where n is a number from 1 to 4.
  23. **
  24. **  Please note also that, to send a Ctrl-C, you must use the Alt-C keys
  25. **
  26. **  Information Technology, Ltd.
  27. */
  28.  
  29. #include "litecomm.h"
  30. #include "litexm.h"
  31. #include <vcstdio.h>
  32.  
  33. #ifdef __TURBOC__
  34. #include <stdlib.h>
  35. #include <fcntl.h>
  36. #include <stat.h>
  37. #include <io.h>
  38. #include <conio.h>
  39. #include <mem.h>
  40. #endif
  41.  
  42. #ifdef M_I86
  43. #include <signal.h>
  44. #include <conio.h>
  45. #include <ctype.h>
  46. #include <fcntl.h>
  47. #include <stdio.h>
  48. #include <sys\types.h>
  49. #include <sys\stat.h>
  50.  
  51. #endif
  52.  
  53. #define CTRLX 0x18
  54.  
  55. #define ESC 0x1b
  56. #define ENQ 0x05
  57.  
  58. int nocbrk();
  59. void moveleft();
  60. void moveright();
  61. void moveup();
  62. void movedn();
  63. void show_modem();
  64. void simtab();
  65. void setcursorpos();
  66. void procesc();
  67. void clreos();
  68. void send(void);
  69. void recv(void);
  70. void xupload(void);
  71. void xdnload(void);
  72. void wupload(void);
  73. void wdnload(void);
  74.  
  75. /*
  76. **  Comm parameters - human-readable form
  77. */
  78. int baud = 2400;
  79. char parity = 'N';
  80. int data = 8;
  81. int stop = 1;
  82.  
  83. /*
  84. **  Comm parameters - Litecomm form
  85. */
  86. unsigned pbaud = 2400;
  87. unsigned pparity = NPARITY;
  88. unsigned pbits = BIT8;
  89. unsigned pstop = STOP1;
  90.  
  91. unsigned port = 2;
  92.  
  93. int xmode = 0;                          /* xmodem type */
  94. int yxmode = 0;                            /* use small blocks */
  95. int halfd = 0;                          /* half-duplex */
  96. int hostm = 0;                          /* host mode */
  97. int ctlc_hit = FALSE;
  98.  
  99. COUNT mwin;                             /* for Vitamin-C */
  100. COUNT swin;
  101.  
  102. COUNT mbd;
  103. COUNT mbg;
  104. COUNT msay;
  105. COUNT mact;
  106. COUNT mnact;
  107. COUNT mtitle;
  108. COUNT sbd;
  109. COUNT sbg;
  110. COUNT ssay;
  111. COUNT sact;
  112. COUNT snact;
  113. COUNT stitle;
  114. char    strbuf[80];                   /* for sprintf usage */
  115.  
  116. void main(argc, argv)
  117. int    argc;
  118. char *argv[];
  119. {
  120.     COUNT    opt;
  121.  
  122. #ifdef M_I86
  123.     signal(SIGINT, nocbrk);               /* set Ctrl-Break handler */
  124. #endif
  125.  
  126. #ifdef __TURBOC__
  127.     ctrlbrk(nocbrk);
  128. #endif
  129.  
  130. /*
  131. **  check for a port parameter
  132. */
  133.     if (argc > 1)
  134.     {
  135.         port = atoi(argv[1]);
  136.         if ((port < 1) || (port > 4))
  137.         {
  138.             puts("Invalid Port Specified\n");
  139.             exit(4);
  140.         }
  141.     }
  142.  
  143. /*
  144. ** establish windowing environment
  145. */
  146.     vcstart(SAVESCRN);
  147.     VC_VIO = 1;
  148.     if ((mwin=wxopen(0,0,23,79,NULL,
  149.          ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0)) == -1)
  150.         terror("mwin:Not Enough Memory");
  151.     if ((swin=wxopen(24,0,24,79,NULL,
  152.          ACTIVE+COOKED+NOADJ, 0, 0)) == -1)
  153.         terror("swin:Not Enough Memory");
  154.     wselect(mwin);
  155.  
  156. /*
  157. ** get display attributes
  158. */
  159.     wattr(mwin,&mbd, &mbg, &msay, &mact, &mnact, &mtitle, GET);
  160.     wattr(swin,&sbd, &sbg, &ssay, &sact, &snact, &stitle, GET);
  161.  
  162. /*
  163. ** set-up the comm port
  164. */
  165.     if (comm_opn(port,2400,NPARITY,BIT8,STOP1,2048,2048) == -1)
  166.     {
  167.         urgentmsg("ERROR", "Can't open port");
  168.         abort();
  169.     }
  170.     lc_setmdm(port, (DTR | RTS));
  171.  
  172.     while (1)
  173.     {
  174.         erase();
  175.         at(0,0);
  176.         vcputs("-- MAIN MENU --\r", msay);
  177.         vcputs("T - enter Terminal mode\r", msay);
  178.         vcputs("    Alt-X leaves terminal mode\r", msay);
  179.         sprintf(strbuf, "H - toggle Host mode (now %s)\r",hostm ? "ON":"OFF");
  180.         vcputs(strbuf, msay);
  181.         sprintf(strbuf, "G - toGgle half-duplex mode (now %s)\r",halfd ? "ON":"OFF");
  182.         vcputs(strbuf, msay);
  183.         sprintf(strbuf, "C - change Comm parameters (now %d,%c,%d,%d)\r",
  184.                 baud,parity,data,stop);
  185.         vcputs(strbuf, msay);
  186.         sprintf(strbuf, "X - change Xmodem mode (now %s)\r",xmode ? "WIN":"NOR");
  187.         vcputs(strbuf, msay);
  188.         sprintf("Y - toggle Ymodem mode (now %s)\r",yxmode ? "ON":"OFF");
  189.         vcputs(strbuf, msay);
  190.         vcputs("S - Send a file\r", msay);
  191.         vcputs("R - Receive a file\r", msay);
  192.         vcputs("Q - Quit\r\r", msay);
  193.  
  194.         opt = getone();
  195.         opt = toupper(opt);
  196.  
  197.         switch (opt)
  198.         {
  199.             case 'T':   terminal();
  200.                         break;
  201.             case 'H':   if (hostm)
  202.                             hostm = 0;
  203.                         else
  204.                         {
  205.                             hostm = 1;
  206.                             halfd = 0;
  207.                         }
  208.                         break;
  209.             case 'G':   if (halfd)
  210.                             halfd = 0;
  211.                         else
  212.                         {
  213.                             halfd = 1;
  214.                             hostm = 0;
  215.                         }
  216.                         break;
  217.             case 'X':   if (xmode)
  218.                             xmode = 0;
  219.                         else
  220.                             xmode = 1;
  221.                         break;
  222.             case 'Y':   if (yxmode)
  223.                         {
  224.                             ymodem = FALSE;
  225.                             yxmode = 0;
  226.                         }
  227.                         else
  228.                         {
  229.                             yxmode = 1;
  230.                             ymodem = TRUE;
  231.                         }
  232.                         break;
  233.             case 'S':   send();
  234.                         break;
  235.             case 'R':   recv();
  236.                         break;
  237.             case 'C':   chgcomm();
  238.                         break;
  239.         }
  240.         if (opt == 'Q')
  241.             break;                      /* shut down time */
  242.     }                                   /* while (1) */
  243.  
  244.     comm_close(port);
  245.     vcend(CLOSE);
  246.     exit(0);
  247. }                                       /* main */
  248.  
  249. int nocbrk()
  250. {
  251. #ifdef M_I86
  252.     signal(SIGINT, SIG_IGN);             /* set Ctrl-Break handler */
  253. #endif
  254.     ctlc_hit = TRUE;
  255.     _abort_flag = TRUE;
  256. #ifdef M_I86
  257.     signal(SIGINT, nocbrk);             /* set Ctrl-Break handler */
  258. #endif
  259.     return(TRUE);
  260. }
  261.  
  262. terminal()
  263. {
  264.     COUNT ch;
  265.     COUNT row, col;
  266.     COUNT twin;
  267.  
  268.     erase();
  269.  
  270.     while (1)
  271.     {
  272.         if ((ch = lc_get(port)) != -1)
  273.             switch (ch & 0x7f)
  274.             {
  275.                 case 0x08:             /* BS */
  276.                     moveleft();
  277.                     vcputc(' ', msay);
  278.                     moveleft();
  279.                     break;
  280.                 case '\t':                /* HT */
  281.                     simtab();
  282.                     break;
  283.                 case '\r':              /* CR */
  284.                     wcurspos(mwin, &row, &col);
  285.                     at(row,0);
  286.                     break;
  287.                 case DLE:
  288.                     twin=wxopen(6,20,13,60,"TRANSFER A FILE",
  289.                         BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  290.                     wselect(twin);
  291.                     bp_DLE();
  292.                     wclose(twin);
  293.                     wselect(mwin);
  294.                     break;
  295.                 case ENQ:
  296.                     bp_ENQ();
  297.                     break;
  298.                 case ESC:
  299.                     procesc();
  300.                     break;
  301.                 default:
  302.                     if (pparity != NPARITY)
  303.                         ch &= 0x7f;                /* strip parity */
  304.                     vcputc(ch, msay);
  305.                     if (hostm)
  306.                     {
  307.                         lc_put(port,ch);            /* echo back */
  308.                         if (ch == '\r')             /* was it return */
  309.                         {
  310.                             lc_put(port,'\n');      /* echo lf as well */
  311.                             vcputc('\n', msay);
  312.                         }
  313.                     }
  314.             }
  315.         show_modem (lc_mstat(port));
  316.  
  317.         if (keyrdy())                       /* anything typed ? */
  318.         {
  319.             ch = getone();                  /* get input */
  320.             if (ch == ALT_X)
  321.                 return(0);
  322.             if (ch == ALT_C)
  323.                 ch = 0x03;
  324.             lc_put(port,ch);                /* xmit the char */
  325.             if (hostm || halfd)             /* local echo needed ? */
  326.             {
  327.                 vcputc(ch, msay);
  328.                 if (ch == '\r')             /* was it CR */
  329.                 {
  330.                     vcputc('\n', msay);     /* add LF */
  331.                     if (hostm)
  332.                         lc_put(port, '\n'); /* and send LF in host mode */
  333.                 }
  334.             }
  335.         }
  336.     }                                       /* while */
  337. }
  338.  
  339. void procesc()
  340. {
  341.     int ch;
  342.  
  343.  
  344.     ch = lc_getw(port);                    /* wait for actual command */
  345.  
  346.     switch (ch & 0x7f)
  347.     {
  348.         case 'A':
  349.             moveup();
  350.             break;
  351.         case 'B':
  352.             movedn();
  353.             break;
  354.         case 'C':
  355.             moveright();
  356.             break;
  357.         case 'D':
  358.             moveleft();
  359.             break;
  360.         case 'H':
  361.             at(0,0);
  362.             break;
  363.         case 'I':
  364.             bp_ESC_I();
  365.             break;
  366.         case 'J':
  367.             clreos();
  368.             break;
  369.         case 'K':
  370.             xeraeol(vc.dflt);
  371.             break;
  372.         case 'j':
  373.             erase();
  374.             break;
  375.         case 'Y':
  376.             setcursorpos();
  377.             break;
  378.     }
  379. }                                        /* procesc */
  380.  
  381. void show_modem(mdmstat)
  382. int mdmstat;
  383. {
  384.     COUNT lwin;
  385.  
  386.     if ((mdmstat & 0x0f))            /* any status change ? */
  387.     {
  388.         lwin = wselect(swin);
  389.         erase();
  390.         at(0,2);
  391.         if (mdmstat & CTS)
  392.             vcputs("CTS", ssay);
  393.         at(0,8);
  394.         if (mdmstat & DSR)
  395.             vcputs("DSR", ssay);
  396.         at(0,14);
  397.         if (mdmstat & DCD)
  398.             vcputs("DCD", ssay);
  399.         at(0,20);
  400.         if (mdmstat & RI)
  401.             vcputs("RI", ssay);
  402.         wselect(lwin);
  403.     }
  404. }                                       /* show_modem */
  405.  
  406. void clreos()
  407. {
  408.     xeraeos(vc.dflt);
  409. }                                        /* clreos */
  410.  
  411. void setcursorpos()
  412. {
  413.     int    ch;
  414.     int    x;
  415.     int    y;
  416.  
  417.     while((ch = lc_get(port)) == -1)
  418.     ;
  419.     y = (ch & 0x7f) - 32;
  420.  
  421.     while((ch = lc_get(port)) == -1)
  422.     ;
  423.     x = (ch & 0x7f) - 32;
  424.     at(y, x);
  425. }
  426.  
  427. struct text_info
  428. {
  429.     COUNT   wintop,
  430.             winbottom,
  431.             winleft,
  432.             winright,
  433.             curx,
  434.             cury;
  435. };
  436.  
  437. void gettextinfo(r)
  438. struct text_info *r;
  439. {
  440.     wcoord(mwin, &(r->wintop), &(r->winleft), &(r->winbottom), &(r->winright));
  441.     wcurspos(mwin, &(r->cury), &(r->curx));
  442. }
  443.  
  444. void moveleft()
  445. {
  446.     int scurx,
  447.         scury;
  448.     struct text_info r;
  449.  
  450.     gettextinfo(&r);
  451.     scurx = r.curx;
  452.     scury = r.cury;
  453.  
  454.     scurx--;                             /* decrement x position */
  455.     if (scurx < r.winleft)
  456.     {
  457.         scury--;
  458.         scurx = r.winright;
  459.         if (scury < r.wintop);
  460.             scury = r.winbottom;
  461.     }
  462.     at(scury, scurx);
  463. }                                        /* moveleft */
  464.  
  465. void moveright()
  466. {
  467.     int scurx,
  468.         scury;
  469.     struct text_info r;
  470.  
  471.     gettextinfo(&r);
  472.     scurx = r.curx;
  473.     scury = r.cury;
  474.  
  475.     scurx++;                             /* decrement x position */
  476.     if (scurx > r.winright)
  477.     {
  478.         scury++;
  479.         scurx = r.winleft;
  480.         if (scury > r.winbottom);
  481.             scury = r.wintop;
  482.     }
  483.     at(scury, scurx);
  484. }                                        /* moveright */
  485.  
  486. void moveup()
  487. {
  488.     int scury;
  489.  
  490.     struct text_info r;
  491.  
  492.     gettextinfo(&r);
  493.     scury = r.cury;
  494.  
  495.     scury = r.cury - 1;
  496.     if (scury < r.wintop)
  497.         scury = r.winbottom;
  498.     at(scury, r.curx);
  499. }                                        /* moveup */
  500.  
  501. void movedn()
  502. {
  503.     int scury;
  504.  
  505.     struct text_info r;
  506.  
  507.     gettextinfo(&r);
  508.     scury = r.cury;
  509.  
  510.     scury = r.cury + 1;
  511.     if (scury > r.winbottom)
  512.         scury = r.wintop;
  513.     at(scury, r.curx);
  514. }
  515.  
  516. void simtab()
  517. {
  518.     int scurx,
  519.         scury;
  520.     struct text_info r;
  521.  
  522.     gettextinfo(&r);
  523.     scurx = r.curx;
  524.     scury = r.cury;
  525.  
  526.     do
  527.         scurx++;                         /* bump x position */
  528.     while (scurx % 9);
  529.  
  530.     if (scurx > r.winright)
  531.     {
  532.         scury++;
  533.         scurx = r.winleft;
  534.         if (scury > r.winbottom);
  535.         {
  536.             at(r.winbottom, r.winright);
  537.             vcputc('\n', msay);
  538.             return;
  539.         }
  540.     }
  541.     at(scury, scurx);
  542. }                                        /* simtab */
  543.  
  544.  
  545. chgcomm()
  546. {
  547.     COUNT opt;
  548.     unsigned sbaud;
  549.     unsigned sparity;
  550.     unsigned sbits;
  551.     unsigned sstop;
  552.     int hbaud;
  553.     char hparity;
  554.     int hdata;
  555.     int hstop;
  556.  
  557. /*
  558. ** get current default settings
  559. */
  560.     sbaud = pbaud;
  561.     sparity = pparity;
  562.     sbits = pbits;
  563.     sstop = pstop;
  564.     hbaud = baud;
  565.     hparity = parity;
  566.     hdata = data;
  567.     hstop = stop;
  568.  
  569.  
  570.     while (1)
  571.     {
  572.         erase();
  573.         vcputs("-- COMM PARAMETERS --\r", msay);
  574.         sprintf(strbuf,"  (Presently %d,%c,%d,%d)\r",
  575.                 hbaud,hparity,hdata,hstop);
  576.         vcputs(strbuf, msay);
  577.         vcputs("B - change Baud Rate\r", msay);
  578.         vcputs("P - change Parity\r", msay);
  579.         vcputs("D - change Data bits\r", msay);
  580.         vcputs("S - change Stop bits\r\r", msay);
  581.         vcputs("A - Abandon Changes\r", msay);
  582.         vcputs("Q - Quit to Main Menu\r\r", msay);
  583.  
  584.         opt = getone();
  585.         opt = toupper(opt);
  586.  
  587.         switch (opt)
  588.         {
  589.             case 'A':
  590.                 return;
  591.             case 'Q':
  592.                 pbaud = sbaud;          /* reset the globals */
  593.                 pparity = sparity;
  594.                 pbits = sbits;
  595.                 pstop = sstop;
  596.                 baud = hbaud;
  597.                 parity = hparity;
  598.                 data = hdata;
  599.                 stop = hstop;
  600.                 comm_setup(port,pbaud,pparity,pbits,pstop);
  601.                 return;
  602.             case 'B':
  603.                 vcputs("1 - 110, 2 - 300, 3 - 600, 4 - 1200, 5 - 2400\r", msay);
  604.                 vcputs("6 - 4800, 7 - 9600, 8 - 19200\r", msay);
  605.                 opt = getone();
  606.                 switch (opt)
  607.                 {
  608.                     case '1': hbaud = 110;
  609.                               sbaud = 110;
  610.                               break;
  611.                     case '2': hbaud = 300;
  612.                               sbaud = 300;
  613.                               break;
  614.                     case '3': hbaud = 600;
  615.                               sbaud = 600;
  616.                               break;
  617.                     case '4': hbaud = 1200;
  618.                               sbaud = 1200;
  619.                               break;
  620.                     case '5': hbaud = 2400;
  621.                               sbaud = 2400;
  622.                               break;
  623.                     case '6': hbaud = 4800;
  624.                               sbaud = 4800;
  625.                               break;
  626.                     case '7': hbaud = 9600;
  627.                               sbaud = 9600;
  628.                               break;
  629.                     case '8': hbaud = 19200;
  630.                               sbaud = 19200;
  631.                               break;
  632.                 }
  633.                 break;
  634.             case 'P':
  635.                 vcputs("1 - NONE, 2 - EVEN, 3 - ODD, 4 - MARK, 5 - SPACE\r", msay);
  636.                 opt = getone();
  637.                 switch (opt)
  638.                 {
  639.                     case '1': hparity = 'N';
  640.                               sparity = NPARITY;
  641.                               break;
  642.                     case '2': hparity = 'E';
  643.                               sparity = EPARITY;
  644.                               break;
  645.                     case '3': hparity = 'O';
  646.                               sparity = OPARITY;
  647.                               break;
  648.                     case '4': hparity = 'M';
  649.                               sparity = MPARITY;
  650.                               break;
  651.                     case '5': hparity = 'S';
  652.                               sparity = SPARITY;
  653.                               break;
  654.                 }
  655.                 break;
  656.             case 'D':
  657.                 vcputs("Number of Data Bits (5, 6, 7, 8)\r", msay);
  658.                 opt = getone();
  659.                 switch (opt)
  660.                 {
  661.                     case '5': hdata = 5;
  662.                               sbits = BIT5;
  663.                               break;
  664.                     case '6': hdata = 6;
  665.                               sbits = BIT6;
  666.                               break;
  667.                     case '7': hdata = 7;
  668.                               sbits = BIT7;
  669.                               break;
  670.                     case '8': hdata = 8;
  671.                               sbits = BIT8;
  672.                               break;
  673.                 }
  674.                 break;
  675.             case 'S':
  676.                 vcputs("Number of Stop Bits (1, 2)\r", msay);
  677.                 opt = getone();
  678.                 switch (opt)
  679.                 {
  680.                     case '1': hstop = 1;
  681.                               sstop = STOP1;
  682.                               break;
  683.                     case '2': hstop = 2;
  684.                               sstop = STOP2;
  685.                               break;
  686.                 }
  687.                 break;
  688.         }
  689.     }
  690. }
  691.  
  692.  
  693. void send()
  694. {
  695.     COUNT twin;
  696.  
  697.     twin=wxopen(6,25,10,50,"SEND A FILE",
  698.          BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  699.     wselect(twin);
  700.     if (xmode)
  701.         wupload();
  702.     else
  703.         xupload();
  704.     wclose(twin);
  705.     wselect(mwin);
  706. }
  707.  
  708. void recv()
  709. {
  710.     COUNT twin;
  711.  
  712.     twin=wxopen(6,25,10,50,"RECEIVE A FILE",
  713.          BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  714.     wselect(twin);
  715.     if (xmode)
  716.         wdnload();
  717.     else
  718.         xdnload();
  719.     wclose(twin);
  720.     wselect(mwin);
  721. }
  722.  
  723. void getfname(fname)
  724. char *fname;
  725. {
  726.     erase();
  727.     atsay(0,0,"File Name: ");
  728.     empty(fname, 13);
  729.     get(fname,NULL);
  730.     readgets();
  731.     cputs("\r");
  732. }
  733.  
  734. void xupload(void)
  735. {
  736.     char fname[13];
  737.     int  fd;
  738.     unsigned char buf[1024];
  739.     unsigned char *bpos;
  740.     int toread;
  741.     int tosend;
  742.     unsigned char hdshk;
  743.     int hmode;
  744.     int result;
  745.  
  746.     getfname(fname);
  747.  
  748.     ctlc_hit = FALSE;
  749.     if (ctlc_hit == TRUE)
  750.     {
  751.         ctlc_hit = FALSE;
  752.         return;
  753.     }
  754.  
  755.     if (isblank(fname))
  756.         return;
  757.     trim(fname);
  758.  
  759.     if ((fd = open(fname, (O_RDONLY|O_BINARY))) == -1)
  760.     {
  761.         urgentmsg("WARNING", "Unable to open the file");
  762.         return;
  763.     }
  764. /*
  765. ** the file has been opened successfully...now switch the
  766. ** comport mode to 8 bits for xmodem protocol
  767. */
  768.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  769.     {
  770.         urgentmsg("ERROR","Unable to switch line mode");
  771.         return;
  772.     }
  773. /*
  774. ** begin file transmission to receiver -
  775. ** in receiver's specified mode
  776. */
  777.     if (yxmode)
  778.     {
  779.         ymodem = TRUE;
  780.         toread = 1024;
  781.     }
  782.     else
  783.     {
  784.         ymodem = FALSE;
  785.         toread = 128;
  786.     }
  787.  
  788.     while (TRUE)
  789.     {
  790.         memset(buf, 0x1a, sizeof(buf));     /* clear buff, short rec */
  791.         if ((tosend = read(fd, buf, sizeof(buf))) < 1)  /* EOF or Error */
  792.         {
  793.             lcxteot(port);            /* send end of file */
  794.             urgentmsg("SUCCESS","End of Transmission");
  795.             break;
  796.         }
  797.         bpos = buf;
  798.         while (TRUE)
  799.         {
  800.             if (tosend <= 0)
  801.                 break;                        /* block sent completely */
  802.               if (yxmode)
  803.                 if (tosend != toread)    /* short block */
  804.                 {
  805.                       toread = 128;       /* sending short */
  806.                       ymodem = FALSE;
  807.                 }
  808.             result = lcxtrec(port, bpos);
  809.             switch(result)           /* what action to take ? */
  810.             {
  811.                 case SUCCESS:
  812.                     sprintf(strbuf,"Sent record: %d",(rec-1));
  813.                     atsay(1,1,strbuf);
  814.                     eraeol();
  815.                     tosend -= toread;
  816.                     bpos += toread;
  817.                     break;
  818.                 case CAN:
  819.                     urgentmsg("WARNING","Cancel Received");
  820.                     break;
  821.                 case RETRIES:
  822.                     sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  823.                     urgentmsg("ERROR", strbuf);
  824.                     break;
  825.                 default:
  826.                     urgentmsg("ERROR", "Fatal transmission error");
  827.                     break;
  828.             }
  829.             if (result != SUCCESS)
  830.                 break;
  831.         }                                    /* inner while */
  832.         if (result != SUCCESS)
  833.             break;
  834.     }                                          /* while TRUE */
  835.     comm_setup(port,pbaud,pparity,pbits,pstop);
  836.     close(fd);                                 /* close down the file */
  837. }
  838.  
  839. void xdnload(void)
  840. {
  841.     char fname[13];
  842.     int  fd;
  843.     unsigned char buf[1024];
  844.     unsigned char hdshk;
  845.     int bsize;
  846.     int hmode;
  847.     int result;
  848.  
  849.     ctlc_hit = FALSE;
  850.  
  851.     getfname(fname);
  852.     if (ctlc_hit == TRUE)
  853.     {
  854.         ctlc_hit = FALSE;
  855.         return;
  856.     }
  857.     if (isblank(fname))
  858.         return;
  859.     trim(fname);
  860.  
  861.     if ((fd = open(fname,
  862.          (O_WRONLY|O_CREAT|O_TRUNC|O_BINARY),(S_IREAD|S_IWRITE))) == -1)
  863.     {
  864.         urgentmsg("WARNING", "Unable to open the file");
  865.         return;
  866.     }
  867. /*
  868. ** the file has been opened successfully...now switch the
  869. ** comport mode to 8 bits for xmodem protocol
  870. */
  871.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  872.     {
  873.         urgentmsg("ERROR", "Unable to switch line mode");
  874.         return;
  875.     }
  876. /*
  877. ** setup conditions for XMODEM receive
  878. */
  879.     hdshk = CRC;                 /* use crc method */
  880.     hmode = RELAXED;                /* normal time delay used */
  881.     while (TRUE)
  882.     {
  883.         while ((result = lcxrrec(port, buf, &bsize, hmode, &hdshk)) == SUCCESS)
  884.         {
  885.             write(fd, buf, bsize);  /* dump the block */
  886.             sprintf(strbuf,"Received record: %d",(rec-1));
  887.             atsay(1,1,strbuf);
  888.             eraeol();
  889.         }
  890.         switch(result)           /* unusual conditions */
  891.         {
  892.             case DUPSEQ:
  893.                 atsay(1,1,"Duplicate record seq");
  894.                 eraeol();
  895.                 break;
  896.             case CAN:
  897.                 urgentmsg("WARNING", "Cancelled by host");
  898.                 break;
  899.             case EOT:
  900.                 urgentmsg("SUCCESS", "Normal Termination");
  901.                 break;
  902.             case TOUT:
  903.                 urgentmsg("ERROR", "SOH timeout");
  904.                 break;
  905.             case RETRIES:
  906.                 sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  907.                 urgentmsg("ERROR", strbuf);
  908.                 break;
  909.             default:
  910.                 urgentmsg("ERROR", "Fatal transmission error");
  911.                 break;
  912.         }
  913.         if ((result != SUCCESS) && (result != DUPSEQ))
  914.             break;
  915.     }                                          /* while TRUE */
  916.  
  917.     comm_setup(port,pbaud,pparity,pbits,pstop);
  918.     close(fd);                                 /* close down the file */
  919. }
  920.  
  921. void wupload(void)
  922. {
  923.     char fname[13];
  924.     int  fd;
  925.     unsigned char buf[128];
  926.     unsigned char hdshk;
  927.     int hmode;
  928.     int result;
  929.     int nrec;                           /* returned record */
  930.  
  931.     ctlc_hit = FALSE;
  932.     getfname(fname);
  933.     if (ctlc_hit == TRUE)
  934.     {
  935.         ctlc_hit = FALSE;
  936.         return;
  937.     }
  938.     if (isblank(fname))
  939.         return;
  940.     trim(fname);
  941.  
  942.     if ((fd = open(fname,(O_RDONLY|O_BINARY))) == -1)
  943.     {
  944.         urgentmsg("ERROR", "Unable to open the file");
  945.         return;
  946.     }
  947. /*
  948. ** the file has been opened successfully...now switch the
  949. ** comport mode to 8 bits for xmodem protocol
  950. */
  951.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  952.     {
  953.         urgentmsg("ERROR", "Unable to switch line mode");
  954.         return;
  955.     }
  956.     lc_xoff(port,TRUE);                    /* turn on auto xon-xoff */
  957.  
  958. /*
  959. ** begin file transmission to receiver -
  960. ** in receiver's specified mode
  961. */
  962.     while (TRUE)
  963.     {
  964.         memset(buf, 0x1a, 128);     /* clear buff, short rec */
  965.         if (read(fd, buf, sizeof(buf)) < 1)  /* EOF or Error */
  966.             nrec = -1;              /* defines End of File */
  967.         else
  968.             nrec = 0;
  969.  
  970.         result = lwxtrec(port, buf, &nrec);
  971.  
  972.         switch(result)           /* what action to take ? */
  973.         {
  974.             case SUCCESS:
  975.                 if (nrec == -1)  /* EOF confirmed */
  976.                     urgentmsg("SUCCESS","End of Transmission");
  977.                 else
  978.                 {
  979.                     sprintf(strbuf, "Sent record: %d",rec);
  980.                     atsay(1,1,strbuf);
  981.                     eraeol();
  982.                 }
  983.                 break;
  984.             case CAN:
  985.                 urgentmsg("ERROR", "Cancel Received");
  986.                 break;
  987.             case RETRIES:
  988.                 sprintf(strbuf, "Too Many tries...Rec %d",rec);
  989.                 urgentmsg("ERROR", strbuf);
  990.                 break;
  991.             case RESEND:                /* must reset file position */
  992.                 lseek(fd, (long)(nrec * sizeof(buf)), SEEK_SET);
  993.                 break;
  994.             default:
  995.                 urgentmsg("ERROR", "Fatal transmission error");
  996.                 break;
  997.         }
  998.         if (result == RESEND)
  999.             continue;
  1000.         if (result == SUCCESS)
  1001.             if (nrec == -1)             /* was EOF signalled */
  1002.                 break;
  1003.             else
  1004.                 continue;
  1005.         break;                          /* some other error */
  1006.     }                                          /* while TRUE */
  1007.     lc_xoff(port, FALSE);                         /* turn off auto xon/xoff */
  1008.     comm_setup(port,pbaud,pparity,pbits,pstop);
  1009.     close(fd);                                 /* close down the file */
  1010. }
  1011.  
  1012. void wdnload(void)
  1013. {
  1014.     char fname[13];
  1015.     int  fd;
  1016.     unsigned char buf[128];
  1017.     unsigned char hdshk;
  1018.     int hmode;
  1019.     int result;
  1020.  
  1021.     ctlc_hit = FALSE;
  1022.  
  1023.     getfname(fname);
  1024.     if (ctlc_hit == TRUE)
  1025.     {
  1026.         ctlc_hit = FALSE;
  1027.         return;
  1028.     }
  1029.     if (isblank(fname))
  1030.         return;
  1031.     trim(fname);
  1032.  
  1033.     if ((fd = open(fname,
  1034.          (O_WRONLY|O_CREAT|O_TRUNC|O_BINARY),(S_IREAD|S_IWRITE))) == -1)
  1035.     {
  1036.         urgentmsg("ERROR", "Unable to open the file");
  1037.         return;
  1038.     }
  1039. /*
  1040. ** the file has been opened successfully...now switch the
  1041. ** comport mode to 8 bits for xmodem protocol
  1042. */
  1043.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  1044.     {
  1045.         urgentmsg("ERROR", "Unable to switch line mode");
  1046.         return;
  1047.     }
  1048. /*
  1049. ** setup conditions for XMODEM receive
  1050. */
  1051.     while (TRUE)
  1052.     {
  1053.         while ((result = lwxrrec(port, buf)) == SUCCESS)
  1054.         {
  1055.             write(fd, buf, sizeof(buf));  /* dump the block */
  1056.             sprintf(strbuf, "Received record: %d", rec-1);
  1057.             atsay(1,1,strbuf);
  1058.             eraeol();
  1059.         }
  1060.         switch(result)           /* unusual conditions */
  1061.         {
  1062.             case DUPSEQ:
  1063.                 atsay(1,1,"Duplicate record seq");
  1064.                 eraeol();
  1065.                 break;
  1066.             case CAN:
  1067.                 urgentmsg("ERROR", "Cancelled by host");
  1068.                 break;
  1069.             case EOT:
  1070.                 urgentmsg("SUCCESS","Normal Termination");
  1071.                 break;
  1072.             case TOUT:
  1073.                 urgentmsg("ERROR", "SOH timeout");
  1074.                 break;
  1075.             case RETRIES:
  1076.                 sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  1077.                 urgentmsg("ERROR", strbuf);
  1078.                 break;
  1079.             default:
  1080.                 urgentmsg("ERROR", "Fatal transmission error");
  1081.                 break;
  1082.         }
  1083.         if ((result != SUCCESS) && (result != DUPSEQ))
  1084.             break;
  1085.     }                                          /* while TRUE */
  1086.  
  1087.     comm_setup(port,pbaud,pparity,pbits,pstop);
  1088.     close(fd);                                 /* close down the file */
  1089. }
  1090.