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 / HEATH / HMSRC20.LBR / HM5.CZ / HM5.C
Text File  |  2000-06-30  |  7KB  |  327 lines

  1. /***************************** Module 5 ************************************/
  2.  
  3. #define  C80
  4. #include "hmodem80.h"
  5.  
  6. dohost()
  7. {
  8.    static int i, c, d, valid, n;
  9.    static int result;
  10.  
  11.    flush();
  12.    mstrout(Host.autoanswer,FALSE);
  13.    cls();
  14.    hlabel();
  15.    printf("Waiting for call...");
  16.    while (!connect()) {                 /* wait for connect */
  17.       if (QuitFlag)
  18.          goto cleanup;
  19.    }
  20.    wait(3);
  21.    autobaud();
  22.    if (QuitFlag)
  23.       goto cleanup;
  24.    mstrout("\n\nHModem ",TRUE);
  25.    mstrout(Version,TRUE);
  26.    mstrout("\n\n",TRUE);
  27.    mstrout(Host.welcome,TRUE);
  28.    purgeline();
  29.    do {
  30.       mstrout("\nUser ID:  ",TRUE);
  31.       purgeline();
  32.    } while (!(i=mgetline(Host.user,20,TRUE)));
  33.    if (i == TIMEOUT)
  34.       goto cleanup;
  35.    for (i=0; i<3; i++) {
  36.       valid = getpassword();
  37.       if (valid)
  38.          break;
  39.    }
  40.    if (!valid || (valid == TIMEOUT))
  41.       goto cleanup;
  42.    c = 0;
  43.    while ((c != 'G') && connect()) {
  44.       hostmenu();
  45.       hlabel();
  46.       for (i=0; (i < 60) && connect(); i++) {
  47.          c = mgetchar(1);
  48.          if (c != TIMEOUT)
  49.             break;
  50.       }
  51.       if (c != TIMEOUT) {
  52.          c = toupper(c);
  53.          mcharout(c);
  54.          mstrout("\n\n",FALSE);
  55.       }
  56.       switch (c) {
  57.       
  58.       case TIMEOUT:
  59.          goto cleanup;
  60.          break;
  61.  
  62.       case 'C':
  63.          chat();
  64.          break;
  65.  
  66.       case 'G':
  67.          mstrout("\nThank you for calling...\n",TRUE);
  68.          break;
  69.  
  70.       case 'D':
  71.          result = dotransfer(SEND,NULL);
  72.          if (result == TIMEOUT)
  73.             goto cleanup;
  74.          QuitFlag = FALSE;
  75.          break;
  76.  
  77.       case 'F':
  78.          directory();
  79.          break;
  80.  
  81.       case 'U':
  82.          result = dotransfer(RECEIVE,NULL);
  83.          if (result == TIMEOUT)
  84.             goto cleanup;
  85.          QuitFlag = FALSE;
  86.          break;
  87.  
  88.       case 'M':
  89.          if (getmessage() == TIMEOUT)
  90.             goto cleanup;
  91.          break;
  92.  
  93.       case 'R':
  94.          if ((mgetchar(1) == 'z') && ((mgetchar(1)&0xff) == CR)) {
  95.             result = dotransfer(RECEIVE,'Z');
  96.             if (result == TIMEOUT)
  97.                goto cleanup;
  98.             QuitFlag = FALSE;
  99.             break;
  100.          }
  101.  
  102.       case 'S':      /* backdoor to set default drive */
  103.          if (mgetchar(1) == 'i')
  104.             reset(mgetchar(1));
  105.          break;
  106.  
  107.       default:
  108.          break;
  109.       }
  110.    }
  111. cleanup:
  112.    printf("\n\nExiting Host Mode...");
  113.    if (connect())
  114.       hangup();
  115.    initializemodem();
  116.    wait(3);
  117. }
  118.  
  119. dotransfer(which,prot)
  120. int which, prot;
  121. {
  122.    static int result;
  123.  
  124.    if (which == RECEIVE)
  125.       result = bringin(prot);
  126.    else if (which == SEND)
  127.       result = sendout(prot);
  128.    switch (result) {
  129.       case NERROR:
  130.          mstrout("\007\nTRANSFER ABORTED\n",FALSE);
  131.          break;
  132.       case OK:
  133.          mstrout("\007\nTransfer Successful\n",FALSE);
  134.          break;
  135.       default:
  136.          break;
  137.    }
  138.    return result;
  139. }
  140.  
  141. getpassword()
  142. {
  143.    static char *password;
  144.  
  145.    password = Pathname;
  146.    mstrout("\nPassword:  ",TRUE);
  147.    if (mgetline(password,20,FALSE) == TIMEOUT)
  148.       return TIMEOUT;
  149.    if (strcmp(password,Host.password))
  150.       return FALSE;
  151.    else
  152.       return TRUE;
  153. }
  154.  
  155. getmessage()
  156. {
  157.    static char *buffer;
  158.    static int fm, i;
  159.    static char *p;
  160.  
  161.    buffer = Pathname;
  162.    mstrout("\nPlease enter date and time:  ",TRUE);
  163.    if ((i=mgetline(buffer,40,TRUE)) == TIMEOUT)
  164.       return TIMEOUT;
  165.    if (!i)
  166.       return 0;
  167.    addatadrive(Msgfile);
  168.    if (!(fm = fopen(Msgfile,"u")) && !(fm = fopen(Msgfile,"w"))) {
  169.       openerror(fm,Msgfile);
  170.       return TIMEOUT;
  171.    }
  172.    seek(fm,0,2);
  173.    fprintf(fm,"From:  %s\n",Host.user);
  174.    fprintf(fm,"Date:  %s\n",buffer);
  175.    mstrout(">",TRUE);
  176.    p = buffer;
  177.    while (((i = mgetline(buffer,80,TRUE)) != TIMEOUT) && i) {
  178.       while (*p)
  179.          putc(*p++,fm);
  180.       putc('\n',fm);
  181.       mstrout(">",TRUE);
  182.       p = buffer;
  183.    }
  184.    putc('\n',fm);
  185.    putc('\n',fm);
  186.    fclose(fm);
  187.    return 0;
  188. }      
  189.  
  190. /* mgetline - get a string from the modem.  Returns length of the string,
  191.     0 terminated, without the newline at the end. */
  192.  
  193. mgetline(s,lim,echo)
  194. char *s;
  195. int lim, echo;
  196. {    
  197.    static char *t;
  198.    static int i;
  199.  
  200.     for (t=s,*t='\0' ; --lim > 0 ; ++t) {
  201.       for (i=0; i<60; i++) {
  202.          if (!connect())
  203.             return TIMEOUT;
  204.          if ((*t = mgetchar(1)) != TIMEOUT)
  205.             break;
  206.       }
  207.       if (!connect())
  208.          return TIMEOUT;
  209.       if (*t == '\r')
  210.          break;
  211.       if (*t == TIMEOUT)
  212.          return TIMEOUT;
  213.       if ((*t == BS) && (t == s)) {
  214.          --t;
  215.          ++lim;
  216.          continue;
  217.       }
  218.       else {
  219.          if (*t == BS) {
  220.             mstrout("\b \b",TRUE);
  221.             t -= 2;
  222.             lim += 2;
  223.          }
  224.          else {
  225.             putchar(*t);
  226.             if (echo)
  227.                mcharout(*t);
  228.             else
  229.                mcharout('*');
  230.          }
  231.       }
  232.    }
  233.     *t = '\0';
  234.    mstrout("\n",TRUE);
  235.     return (t - s);
  236. }
  237.  
  238. hostmenu()
  239. {
  240.    purgeline();
  241.    flush();
  242.    mstrout("\n\n(C)hat, (D)ownload, (F)iles, (U)pload, (M)essage, or (G)oodbye?  ",TRUE);
  243. }
  244.  
  245. connect()
  246. {
  247.    opabort();                 /* sets QuitFlag if esc hit */
  248.    if (Host.modemconnection)
  249.       return (inp(Mdmstat) & CONNECTSTAT) && !QuitFlag;
  250.    else
  251.       return !QuitFlag;
  252. }
  253.  
  254. autobaud() 
  255. {
  256.    static int baudindex[] = { 3, 6, 7 };
  257.    static int c, i;
  258.  
  259.    printf("\nAutomatic baud rate adjust...");
  260.    purgeline();
  261.    Current.cparity = 'N';
  262.    Current.cdatabits = 8;
  263.    Current.cstopbits = 1;
  264.    for (i=0; mdmerror() || ((c=(mgetchar(1) & 0xff)) != CR); i %= 3) {
  265. #ifdef   DEBUG
  266.       printf("\nc = %x",c);
  267. #endif
  268.       if (!connect())
  269.          break;
  270.       Current.cbaudindex = baudindex[i++];
  271.       updateace();
  272.       purgeline();
  273.    }
  274.    purgeline();
  275. }
  276.  
  277. chat()
  278. {
  279.    static int c;
  280.  
  281.    cls();
  282.    c = 0;
  283.    chlabel();
  284.    mstrout("\nSignaling sysop...",TRUE);
  285.    printf("\007 \007 \007");
  286.    while (c != ESC) {
  287.       if (c=getch()) {
  288.          putchar(c);
  289.          mcharout(c);
  290.          if (c == CR) {
  291.             putchar(LF);
  292.             mcharout(LF);            
  293.          }
  294.          else if (c == BS)
  295.             mstrout(" \b",TRUE);
  296.       }
  297.       if (minprdy()) {
  298.          putchar(c=mcharinp());
  299.          mcharout(c);
  300.          if (c == CR) {
  301.             putchar(LF);
  302.             mcharout(LF);
  303.          }
  304.          else if (c == BS)
  305.             mstrout(" \b",TRUE);
  306.       }
  307.    }
  308.    mstrout("\007\n\nReturning to Host mode...\n\n",TRUE);
  309.    hlabel();
  310. }
  311.  
  312. mgetchar(seconds)         /* allows input from modem or operator */
  313. int seconds;
  314. {
  315.    static int c, tenths;
  316.  
  317.    Lastkey = 0;
  318.    tenths = seconds * 10;
  319.    if ((c=readline(tenths)) != TIMEOUT)
  320.       return (c & 0xff);
  321.    else if (Lastkey)
  322.       return Lastkey;
  323.    return TIMEOUT;
  324. }
  325.  
  326. /********************** end of module 5 ***********************************/
  327.