home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / MODEMS / ZMODEM / ZMP-SRC.LBR / ZMTERM.CZ / ZMTERM.C
Text File  |  2000-06-30  |  12KB  |  555 lines

  1. /*            TERM module File #1                */
  2.  
  3. #include "zmp.h"
  4.  
  5. #ifdef   AZTEC_C
  6. #include "libc.h"
  7. #else
  8. #include <stdio.h>
  9. #endif
  10.  
  11. #define autolen 6        /* length of ZRQINIT required for auto rx */
  12.  
  13. char *alloc(), *index();
  14. static short Chpos;
  15.  
  16. ovmain()
  17. {
  18.  
  19.     static short i, mdmdata, dolabel, keycount, keypoint;
  20.     static short lastkey = '\0', fkey, autopos;
  21.     static char keybuf[25], autoseq[] = { '*','*',CTRLX,'B','0','0' };
  22.     static char kbdata;
  23.  
  24.     if (FirsTerm) {
  25.         locate(0,0);        /* print header if first time*/
  26.         prompt(FALSE);        /* but don't clear screen */
  27.         locate(15,0);        /* then put cursor near bottom */
  28.         printf("Ready.     \n");    /* overwrite 'wait' */
  29.         Chpos = 0;        /* clear character position */
  30.         FirsTerm = FALSE;    /* don't come here again */
  31.     }
  32.     else
  33.         prompt(TRUE);
  34.     startcapture();
  35.     autopos = keycount = 0;    /* no remote xfer, no keys in buffer */
  36.     Zmodem = FALSE;            /* ensure we don't auto zmodem */
  37.     purgeline();            /* get rid of any junk */
  38.  
  39. /* Main loop: */
  40.  
  41.     while (TRUE) {
  42.         if (keycount) {        /* get any buffered keys */
  43.             kbdata = keybuf[keypoint++];
  44.             keycount--;
  45.             if (kbdata == RET) {    /* Translate ! to CR/LF */
  46.                 kbdata = CR;
  47.                 keybuf[--keypoint] = LF;
  48.                 keycount++;    /* get LF next time */
  49.             }
  50.             if (kbdata == WAITASEC) {    /* handle pause */
  51.                 wait(1);
  52.                 kbdata = '\0';    /* that's it for this loop */
  53.             }
  54.         }
  55.         else
  56.             kbdata = getch();    /* if none, any at keyboard */
  57.         if (kbdata)  {
  58.             if (lastkey == ESC) {
  59.                 fkey = toupper(kbdata);
  60.                     /* allow ESC ESC without complications */
  61.                 lastkey = (kbdata == ESC) ? '\0' : kbdata;
  62.                 dolabel = TRUE;
  63.                 flush();
  64.                 switch (fkey) {
  65.  
  66.                     case RECEIVE:
  67.                     case SEND:
  68.                     case CONFIG:
  69.                         keep(Lastlog,FALSE);
  70.                         return fkey;
  71.                         break;
  72.  
  73.                     case USER:
  74.                         printf("\nEnter overlay number: ");
  75.                         scanf("%d",&Userid);
  76.                         return fkey;
  77.                         break;
  78.  
  79.                     case CAPTURE:
  80.                         capturetog(Logfile);
  81.                         dolabel = FALSE;
  82.                         Chpos = 0;
  83.                         break;
  84.  
  85.                     case DIR:
  86.                         killlabel();
  87.                         keep(Lastlog,FALSE);
  88.                         directory();
  89.                         startcapture();
  90.                         Chpos = 0;
  91.                         dolabel = FALSE;
  92.                         break;
  93.  
  94.                     case PRTSCRN:
  95.                         screenprint();
  96.                         dolabel = FALSE;
  97.                         Chpos = 0;
  98.                         break;
  99.  
  100.                     case HANGUP:
  101.                         hangup();
  102.                         dolabel = FALSE;
  103.                         Chpos = 0;
  104.                         break;
  105.  
  106.                     case COMMAND:
  107.                         docmd();
  108.                         break;
  109.  
  110.                     case DIAL:
  111.                         keep(Lastlog,FALSE);
  112.                         dial();
  113.                         dolabel = FALSE;
  114.                         Chpos = 0;
  115.                         purgeline();
  116.                         startcapture();
  117.                         break;
  118.  
  119. #ifdef HOSTON
  120.                     case HOST:
  121.                         keep(Lastlog,TRUE);
  122.                         QuitFlag = FALSE;
  123.                         Inhost = TRUE;
  124.                         while (!QuitFlag)
  125.                             dohost();
  126.                         Inhost = FALSE;
  127.                         flush();
  128.                         cls();
  129.                         startcapture();
  130.                         break;
  131. #endif
  132.  
  133.                     case TOGPRT:
  134.                         toggleprt();
  135.                         dolabel = FALSE;
  136.                         Chpos = 0;
  137.                         break;
  138.  
  139.                     case DISK:
  140.                         diskstuff();
  141.                         Chpos = 0;
  142.                         break;
  143.  
  144.                     case HELP:
  145.                         if (keybuf[0] = help()) {
  146.                             lastkey = ESC;
  147.                             keycount = 1;
  148.                             keypoint = 0;
  149.                             dolabel = FALSE;
  150.                         }
  151.                         Chpos = 0;
  152.                         break;
  153.  
  154.                     case QUIT:
  155.                         doexit();
  156.                         break;
  157.  
  158.                     case CLRSCR:
  159.                         cls();
  160.                         break;
  161.  
  162.                     case BRK:
  163.                         sendbrk();
  164.                         printf("\nBreak sent.\n");
  165.                         dolabel = FALSE;
  166.                         Chpos = 0;
  167.                         break;
  168.  
  169.                     case DISPKEYS:
  170.                         keydisp();
  171.                         dolabel = FALSE;
  172.                         break;
  173.  
  174.                     default:
  175.                         dolabel = FALSE;
  176.                         i = fkey - '0';
  177.                         if ((i >= 0) && (i <= 9))  {
  178.                             strcpy(keybuf,KbMacro[i]);
  179.                             keycount = strlen(keybuf);
  180.                             keypoint = 0;
  181.                         }
  182.                         else
  183.                         mcharout(kbdata); /* send it if not anything else */
  184.                         break;
  185.  
  186.                 }        /* end of switch*/
  187.  
  188.                 if (dolabel)
  189.                     prompt(TRUE);    /* print header */
  190.  
  191.             }            /* end of if lastkey == ESC */
  192.  
  193.             else if ((lastkey = kbdata) != ESC) {
  194.                 mcharout(kbdata);    /* Not a function key */
  195. ilfloop:
  196.                 if (!FDx) {
  197.                     prtchr(kbdata);
  198.                     if (kbdata == CR) {
  199.                         kbdata = LF;
  200.                         if (RemEcho)
  201.                             mcharout(kbdata);
  202.                         goto ilfloop;
  203.                     }
  204.                 }
  205.             }
  206.         }                    /*  end of if char at kbd  */
  207.  
  208.         if (minprdy()) {
  209.             mdmdata = mcharinp();    /* Character at modem */
  210.             if (mdmdata == autoseq[autopos++]) {    /* ZRQINIT? */
  211.                 if (autopos == autolen) {
  212.                     printf("\nZmodem receive.\n");
  213.                     Zmodem = TRUE;    /* yes, do auto.. */
  214.                     return RECEIVE;    /* ..zmodem receive */
  215.                 }
  216.             }
  217.             else    /* no, reset ZRQINIT sequence test */
  218.                 autopos = (mdmdata == '*') ? 1 : 0;
  219.             if (ParityMask)            /* if flag on, */
  220.                 mdmdata &= 0x7f;    /* remove parity */
  221.             if (Filter && (mdmdata > '\r') && (mdmdata < ' '))
  222.                 goto endloop;    /* filter control chars */
  223. olfloop:
  224.             prtchr(mdmdata);    /* print the character */
  225.             tobuffer(mdmdata);
  226.             toprinter(mdmdata);
  227.             if (RemEcho) {
  228.                 mcharout(mdmdata);
  229.                 if (mdmdata == CR) {
  230.                     mdmdata = LF;
  231.                     goto olfloop;
  232.                 }
  233.             }
  234.         }
  235. endloop:
  236.         prtservice();     /* service printer at the end of each loop */
  237.     }    /* end of while */
  238. }/* end of main */
  239.  
  240.  
  241. /* print character, handling tabs (can't use bdos 2 as it reacts to ctl-s) */
  242. prtchr(c)
  243. char c;
  244. {
  245.     if (c == '\t') {            /* process tabs */
  246.         bdos(DIRCTIO,' ');        /* do at least one */
  247.         while (++Chpos % 8)
  248.             bdos (DIRCTIO,' ');     /* pad with space */
  249.     }
  250.     else {
  251.         bdos(DIRCTIO,c);        /* just print it */
  252.         if (c >= ' ')            /* if printable, */
  253.             Chpos++;    /* bump character position */
  254.         else
  255.             if (c == '\r')        /* cr resets  it */
  256.                 Chpos = 0;
  257.     }
  258. }
  259.  
  260. tobuffer(c)
  261. int c;
  262. {
  263.     if (BFlag) {
  264.         MainBuffer[TxtPtr++] = (char)c;
  265.         if (TxtPtr > Buftop) {
  266.             keep(Lastlog,TRUE);    /* must be true since remote */
  267.             startcapture();        /* is probably still going */
  268.         }
  269.     }
  270. }
  271.  
  272. /* Print message at top of page. Clear screen first if clear set */
  273. prompt(clear)
  274. short clear;
  275. {
  276.     if (clear)
  277.         cls();
  278.     printf("\rTerminal Mode: ESC H for help.\t\t");
  279.     printf("Drive %c%d:   %u baud\n",Currdrive,Curruser,
  280.         Baudtable[Current.cbaudindex]);
  281.     Chpos = 0;            /* reset character position */
  282. }
  283.  
  284. toprinter(i)
  285. int i;
  286. {
  287.     char c;
  288.  
  289.     c = (char) i;
  290.     if (PFlag && (c != '\f')) {    /* don't print form feeds */
  291.         *Prthead++ = c;
  292.         adjustprthead();
  293.     }
  294. }
  295.  
  296. toggleprt()
  297. {
  298.     PFlag = !PFlag;
  299.     if (PFlag) {
  300.         if (getprtbuf() != OK)
  301.             PFlag = FALSE;
  302.         else printf("\nPrinter ON\n");
  303.     }
  304.     else {
  305.         while (prtservice())
  306.             ;    /* Empty the buffer */
  307.         bdos(5,'\r');    /* do final cr/lf */
  308.         bdos(5,'\n');
  309.         free(Prtbuf);
  310.         printf("\nPrinter OFF\n");
  311.     }
  312. }
  313.  
  314. getprtbuf()
  315. {
  316.     keep(Lastlog,TRUE);    /* need to steal some of the buffer */
  317.     Prtbuf = alloc(Pbufsiz);
  318.     if (allocerror(Prtbuf))
  319.         return NERROR;
  320.     Prthead = Prttail = Prtbottom = Prtbuf;
  321.     Prttop = Prtbuf + Pbufsiz - 1;
  322.     startcapture();
  323.  
  324. #ifdef DEBUG
  325.     printf("\nPrtbuf = %x\n",Prtbuf);
  326. #endif
  327.  
  328.     return OK;
  329. }
  330.  
  331. /* Quit. */
  332. doexit()
  333. {
  334.     static char c;
  335.  
  336.     killlabel();
  337.     putlabel("Are you SURE you want to exit ZMP? (Y/N) <Y>");
  338.     if (c = toupper(dio()) == 'N')
  339.         return;
  340.     keep(Lastlog,FALSE);
  341.     reset(Invokdrive,Invokuser);
  342.     deinitvector();     /*restore interrupt vector*/
  343.     cls();
  344.     userout();    /* user-defined exit routine */
  345.     exit(0);        /* and quit */
  346.  
  347. }
  348.  
  349. prtservice()    /*printer service routine*/
  350. {
  351.  
  352.     if (PFlag) {
  353.         if (pready()) {
  354.             if (Prthead != Prttail) {
  355.                 bdos(5,*Prttail++);        /* write list byte */
  356.                 if (Prttail > Prttop)
  357.                     Prttail = Prtbottom;
  358.             }
  359.             return (Prthead != Prttail);    /* Return true if buffer full */
  360.         }
  361.     }
  362. }
  363.  
  364. pready()   /*get printer status using bios call*/
  365. {
  366.     return (bios(14 + 1));
  367.  
  368. }
  369.  
  370. adjustprthead()
  371. {
  372.     if (Prthead > Prttop)
  373.         Prthead = Prtbottom;
  374. }
  375.  
  376. setace(n)   /* for a particular phone call */
  377. int n;
  378. {
  379.     Current.cbaudindex = Book[n].pbaudindex;
  380.     Current.cparity = Book[n].pparity;
  381.     Current.cdatabits = Book[n].pdatabits;
  382.     Current.cstopbits = Book[n].pstopbits;
  383.     updateace();
  384. }
  385.  
  386. dial()
  387. {
  388.     static char *number;      /* buffer for number to be sent to modem */
  389.     static char *result;      /* buffer for responses from modem */
  390.     static char *instr;       /* buffer for numbers entered at keyboard */
  391.     static int connect;
  392.     static int status, i, j, n, nocnt, action, c;
  393.     static char *p;
  394.  
  395.     if (allocerror(number = alloc(128)))
  396.         return;
  397.     if (allocerror(result = alloc(128)))
  398.         return;
  399.     if (allocerror(instr = alloc(128)))
  400.         return;
  401.     status = shownos();
  402.     printf("\nEnter letters and/or numbers, separated by commas..\n: ");
  403.     QuitFlag = connect = FALSE;
  404.     Dialing = TRUE;
  405.     if (j=getline(instr,80)) {
  406.         putlabel("Automatic Redial:  Press ESC to stop");
  407.         for (i=0,nocnt=1; instr[i]; i++)
  408.             if  (instr[i] == ',') {
  409.                 instr[i] = 0;
  410.                 nocnt++;
  411.             }
  412.         i = nocnt;
  413.         while (TRUE) {
  414.             p = instr;
  415.             nocnt = i;
  416.             while (nocnt--) {
  417.                 n = -1;
  418.                 strcpy(number,Modem.dialcmd);
  419.                 if (*p == '+') {
  420.                     strcat(number,Sprint);
  421.                     p++;
  422.                 }
  423.                 else if (*p == '-') {
  424.                     strcat(number,Mci);
  425.                     p++;
  426.                 }
  427.                 if ((status == OK) && (j=strlen(p))==1) {
  428.                     if (isalpha(n = *p)) {
  429.                         n = toupper(n) - 'A';
  430.                         setace(n);
  431.                         strcat(number,Book[n].number);
  432.                         strcat(number,Modem.dialsuffix);
  433.                         mstrout(number,FALSE);
  434.                         printf("\nDialing %s...",
  435.                             Book[n].name);
  436.                     }
  437.                     else {
  438.                         printf("\nInvalid Number\n");
  439.                         goto abort;
  440.                     }
  441.                 }     
  442.                 else {
  443.                     strcat(number,p);
  444.                     strcat(number,Modem.dialsuffix);
  445.                     mstrout(number,FALSE);
  446.                     printf("\nDialing %s...",p);
  447.                 }
  448.                     /*flush modem input*/
  449.                 while (readline(10) != TIMEOUT);
  450.                 do {
  451.                     action = readstr(result,Modem.timeout);
  452.                     if (action == TIMEOUT)
  453.                         goto abort;
  454.                     printf("%s\n",result);
  455.                 } while (!(c=isin(result,Modem.connect))
  456.                     && !isin(result,Modem.busy1)
  457.                     && !isin(result,Modem.busy2)
  458.                     && !isin(result,Modem.busy3)
  459.                     && !isin(result,Modem.busy4));
  460.  
  461.                 if (c) {      /* got connect string */
  462.                     printf("\007\nOn Line to %s\n",
  463.                         n >= 0 ? Book[n].name : p);
  464.                     if (n >= 0)
  465.                         FDx = !Book[n].echo;
  466.                     connect = TRUE;
  467.                     goto done;
  468.                 }
  469.                 mcharout(CR);
  470.                     /* wait for modem */
  471.                 while (readline(10) != TIMEOUT);
  472.                 p += j+1;
  473.             }
  474.             if (kbwait(Modem.pause))
  475.                 goto abort;
  476.         }
  477.     }
  478.  
  479. abort:
  480.     printf("Call Aborted.\n");
  481.     mcharout(CR);
  482.     readstr(result,1);      /*gobble last result*/
  483.     resetace();
  484.  
  485. done:
  486.     flush();
  487.     if (Book != (struct phonebook *) MEMORY_FULL)
  488.         free(Book);
  489.     free(instr);
  490.     free(result);
  491.     free(number);
  492.     Dialing = FALSE;
  493.     return connect;
  494. }
  495.  
  496. shownos()
  497. {
  498.     static int i, j, status;
  499.  
  500.     cls();
  501.     if ((status=loadnos()) == OK) {
  502.         stndout();
  503.         printf("         NAME                NUMBER          B   P D S E");
  504.         stndend();
  505.         for (i=0,j=1; i<20; i++,j++) {
  506.             LOCATE(i+1,0);
  507.             printf("%c - %s",i+'A',Book[i].name);
  508.             LOCATE(i+1,41-strlen(Book[i].number));
  509.             printf(Book[i].number);
  510.             LOCATE(i+1,44);
  511.             printf("%4d %c",Baudtable[Book[i].pbaudindex],
  512.                 Book[i].pparity);
  513.             printf(" %d %d %c\n",Book[i].pdatabits,
  514.                 Book[i].pstopbits,Book[i].echo?'H':'F');
  515.         }
  516.     }
  517.     return status;
  518. }
  519.  
  520. loadnos()
  521. {
  522.     static unsigned amount;
  523.     char dummy;
  524.     int i,result;
  525.     FILE *fd;
  526.  
  527.     result = NERROR;
  528.     amount = 21 * sizeof(struct phonebook);
  529.     Book = (struct phonebook *) alloc(amount);
  530.     if (!allocerror(Book)) {
  531.         strcpy(Pathname,Phonefile);
  532.         addu(Pathname,Overdrive,Overuser);
  533.         fd = fopen(Pathname,"r");
  534.         if (fd) {
  535.             for (i = 0; i < 20; i++) {
  536.                 fgets(Book[i].name,17,fd);
  537.                 fscanf(fd,"%c %s %d %c",
  538.                     &dummy,
  539.                     Book[i].number,
  540.                     &Book[i].pbaudindex,
  541.                     &Book[i].pparity);
  542.                 fscanf(fd,"%d %d %d",
  543.                     &Book[i].pdatabits,
  544.                     &Book[i].pstopbits,
  545.                     &Book[i].echo);
  546.                 fgetc(fd);        /* remove LF */
  547.             }
  548.             fclose(fd);
  549.             result = OK;
  550.         }
  551.     }
  552.     return result;
  553. }      
  554. /*            End of TERM module File 1            */
  555.