home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / pcmail / part04 / connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  5.6 KB  |  216 lines

  1. /*++
  2. /* NAME
  3. /*      connect 3
  4. /* SUMMARY
  5. /*      pre- and post protocol host access
  6. /* PROJECT
  7. /*      pc-mail
  8. /* PACKAGE
  9. /*      cico
  10. /* SYNOPSIS
  11. /*      int connect()
  12. /*
  13. /*      int disconnect()
  14. /* DESCRIPTION
  15. /*      connect() tries to make a connection to the remote host
  16. /*      and to log on, using the dial-up script and login-name 
  17. /*    entries in the communications parameter file, and the password
  18. /*    provided as command-line parameter to the cico program. 
  19. /*    A simple send-expect script facility is used (no retries 
  20. /*    or delays). After the dial-up script has completed the program 
  21. /*    proceeds by expecting the login: prompt and does the final login.
  22. /*
  23. /*      disconnect() tries to break a connection, using the disconnect
  24. /*      entry in the communications parameter file. Unlike connect() 
  25. /*    this function is not driven by a send-expect script.
  26. /*
  27. /*    The following escape sequences are recognized in send or expect
  28. /*    strings:
  29. /*
  30. /* .nf
  31. /*    \\b    backspace
  32. /*    \\r    carriage return
  33. /*    \\n    newline
  34. /*    \\t    tab
  35. /*    \\s    space
  36. /*    \\f    form feed
  37. /*    \\nnn    octal character value
  38. /*    \\\\    a real backslash
  39. /* FUNCTIONS AND MACROS
  40. /*      xwrite(), xgetc(), trap(), debug(4)(), log()
  41. /* FILES
  42. /*      $MAILDIR/s00000        communications parameter file
  43. /*    $MAILDIR/LOGFILE    system logfile
  44. /* SEE ALSO
  45. /*      params(5)       communications parameter file entries
  46. /* DIAGNOSTICS
  47. /*      connect() returns a status E_BADSETUP if the systems parameter
  48. /*    file contains bad data, and E_NOLINE if the login script fails.
  49. /* AUTHOR(S)
  50. /*      W.Z. Venema
  51. /*      Eindhoven University of Technology
  52. /*      Department of Mathematics and Computer Science
  53. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  54. /* CREATION DATE
  55. /*      Fri Mar 27 17:11:12 GMT+1:00 1987
  56. /* LAST MODIFICATION
  57. /*    Wed Apr  6 00:19:26 MET 1988
  58. /* VERSION/RELEASE
  59. /*    1.4
  60. /*--*/
  61.  
  62. #include <setjmp.h>
  63. #include <ctype.h>
  64. #include "defs.h"
  65. #include "params.h"
  66. #include "status.h"
  67. #include "comm.h"
  68. #include "logs.h"
  69. #include "sysdep.h"
  70.  
  71. hidden char *blnk = " \t";            /* send/expect separators */
  72.  
  73. /* forward declarations */
  74.  
  75. hidden void conn_send();
  76. hidden void conn_xpct();
  77. hidden char *escape();
  78.  
  79. /* connect - connect to remote system; simple script processing (no retries) */
  80.  
  81. public int connect()
  82. {
  83.     int *savetrap = systrap;                    /* save exception handler */
  84.     jmp_buf mytrap;                /* our exception handler */
  85.     int retval;                    /* completion code */
  86.     register char *cp;
  87.  
  88.     /* set up exception handler */
  89.  
  90.     if (retval = setjmp(systrap = mytrap)) {    /* get here if expect fails */
  91.     systrap = savetrap;            /* it just happened */
  92.     return(retval);
  93.     }
  94.  
  95.     /* optional dial-up sequence */
  96.  
  97.     for (cp = strtok(DIAL_SEQUENCE,blnk); cp; cp = strtok((char *)0,blnk)) {
  98.     conn_send(escape(cp));
  99.     if (cp = strtok((char *)0,blnk))
  100.         conn_xpct(escape(cp));
  101.     }
  102.  
  103.     /* mandatory login sequence */
  104.  
  105.     conn_xpct("ogin:");
  106.     conn_send(strcons("%s\r",LOGIN_NAME));
  107.     conn_xpct("ssword:");
  108.     conn_send(strcons("%s\r",password));
  109.  
  110.     /* restore exception handler */
  111.  
  112.     systrap = savetrap;                         /* get here if expect wins */
  113.     return(0);                    /* say no problems... */
  114. }
  115.  
  116. /* disconnect - disconnect line */
  117.  
  118. public int disconnect()
  119. {
  120.     conn_send(escape(DISC_SEQUENCE));        /* send disconnect sequence */
  121.     return(0);                    /* always succeeds... */
  122. }
  123.  
  124. /* conn_send - quick-and-dirty output function */
  125.  
  126. hidden void conn_send(s)
  127. register char *s;
  128. {
  129.     sleep(1);
  130.  
  131.     if (*s) {
  132.     debug(4)("Sending: %S\n",s);
  133.     while (*s) {
  134.         delay();
  135.         xwrite(ttfd,s++,1);
  136.     }
  137.     }
  138. }
  139.  
  140. /* conn_xpct - primitive pattern matching without meta characters */
  141.  
  142. hidden void conn_xpct(s)
  143. char *s;
  144. {
  145.     register int c,i;
  146.     register int n = strlen(s);
  147.  
  148.     /*
  149.     * Keep listening until we time out or until we receive the 
  150.     * expected string. Make sure that we do not overrun our buffer.
  151.     * Parity bits are ignored.
  152.     */
  153.  
  154.     if (n) {
  155.     debug(4)("Expecting: %S\nReceiving: ",s);
  156.  
  157.     if (n > MSGBUF) n = MSGBUF;
  158.  
  159.     for (i = 0; (c = xgetc()) != EOF; ) {
  160.         msgin[i++] = (c &= 0177);
  161.         debug(4)("%C",c);
  162.         if (i >= n && strncmp(s,&msgin[i-n],n) == 0) {
  163.         debug(4)(" ok!\n");
  164.         return;
  165.         } else if (i >= MSGBUF) {
  166.         strncpy(msgin,&msgin[i-(n-1)],n-1);
  167.         i = n-1;
  168.         }
  169.     }
  170.     debug(4)(" failed!\n");
  171.     trap(E_NOLINE,"LOGIN FAILED (at \"%S\")",s);
  172.     }
  173. }
  174.  
  175. /* escape - interpret backslash sequences (this function is too big) */
  176.  
  177. hidden char *escape(s)
  178. register char *s;
  179. {
  180.     static char buf[BUFSIZ];
  181.     register char *cp = buf;
  182.  
  183.     while (*s && cp < buf+BUFSIZ-1) {           /* don't overflow the buffer */
  184.     register char ch;
  185.     if (*s != '\\') {                       /* ordinary character */
  186.         *cp++ = *s++;
  187.     } else if (isdigit(*++s) && *s < '8') { /* \nnn octal code */
  188.         int c, i;
  189.         sscanf(s,"%3o",&c);
  190.         *cp++ = c;
  191.         i = 1;
  192.             s++;
  193.         while (i++ < 3 && isdigit(*s) && *s <'8')
  194.         s++;
  195.     } else if ((ch = *s++) == 0) {          /* at string terminator */
  196.         break;
  197.     } else if (ch == 'b') {                 /* \b becomes backspace */
  198.         *cp++ = '\b';
  199.     } else if (ch == 'f') {                 /* \f becomes formfeed */
  200.         *cp++ = '\f';
  201.     } else if (ch == 'n') {                 /* \n becomes newline */
  202.         *cp++ = '\n';
  203.     } else if (ch == 'r') {                 /* \r becomes carriage ret */
  204.         *cp++ = '\r';
  205.     } else if (ch == 's') {                 /* \s becomes blank */
  206.         *cp++ = ' ';
  207.     } else if (ch == 't') {                 /* \t becomes tab */
  208.         *cp++ = '\t';
  209.     } else {                                /* \any becomes any */
  210.         *cp++ = ch;
  211.     }
  212.     }
  213.     *cp = '\0';                                 /* terminate the result */
  214.     return(buf);
  215. }
  216.