home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc072.zip / ckudia.c < prev    next >
C/C++ Source or Header  |  1989-02-08  |  30KB  |  970 lines

  1. char *dialv = "Dial Command, V2.0(010) 15 Jan 89";
  2.  
  3. /*  C K U D I A     --  Dialing program for connection to remote system */
  4.  
  5. /*
  6.  Original author: Herm Fischer (HFISCHER@USC-ECLB).
  7.  Contributed to Columbia University for inclusion in C-Kermit.
  8.  Copyright (C) 1985, Herman Fischer, 16400 Ventura Blvd, Encino CA 91436.
  9.  Permission is granted to any individual or institution to use, copy, or
  10.  redistribute this software so long as it is not sold for profit, provided this
  11.  copyright notice is retained.
  12.  
  13.  ------
  14.  
  15.  This module should work under all versions of Unix.  It calls externally
  16.  defined system-depended functions for i/o, but depends upon the existence
  17.  of various modem control functions.
  18.  
  19.  This module, and the supporting routines in the ckutio.c module, assume
  20.  that the computer and modem properly utilize the following data communi-
  21.  cations signals (that means one should prepare the modem to use, not
  22.  circumvent, these signals):
  23.  
  24.      Data Terminal Ready:  This signal is asserted by the computer
  25.      when Kermit is about to ask the modem to dial a call, and is
  26.      removed when Kermit wishes to have the modem hang up a call.
  27.      The signal is asserted both while Kermit is asking the modem
  28.      to dial a specific number, and after connection, while Kermit
  29.      is in a data exchange mode.
  30.  
  31.      Carrier detect:  This signal must be asserted by the modem when
  32.      a carrier is detected from a remote modem on a communications
  33.      circuit.  It must be removed by the modem when the circuit
  34.      disconnects or is hung up.     (Carrier detect is ignored while
  35.      Kermit is asking the modem to dial the call, because there is
  36.      no consistant usage of this signal during the dialing phase
  37.      among different modem manufacturers.)
  38.  
  39. */
  40.  
  41. /*
  42.  * Modifications:
  43.  *
  44.  *    21-Jul-85    Fixed failure returns hanging on no carrier signal
  45.  *            Requires tthang change too (ckutio.c revision)
  46.  *                            -- Herm Fischer
  47.  *
  48.  *    28-Jun-85    Fixed bug with defaulting the modem-failure message
  49.  *            in lbuf.
  50.  *                            -- Dan Schullman
  51.  *
  52.  *    27-Jun-85    Merged in code from Joe Orost at Berkeley for
  53.  *            supporting the US Robotics modem, which included
  54.  *            changing the single characters in MDMINF into
  55.  *            multi-character strings and modifying waitFor.
  56.  *                            -- Dan Schullman
  57.  *
  58.  *    26-Jun-85    Allow interrupts to be used to abort dialing,
  59.  *            and ring the bell when a connection is made.
  60.  *            Reorganized some of the failure paths to use the
  61.  *            same code, and now close the line on failures.
  62.  *            Allow use of stored numbers with the DF100 and
  63.  *            DF200 modems.  Handlers now declared after the
  64.  *            call to setjmp.
  65.  *                            -- Dan Schullman
  66.  *
  67.  *    24-May-85    DF03, DF100-series, DF200-series, and "unknown" modem
  68.  *            support added.    Also restructured the various data
  69.  *            tables, fixed some bugs related to missing data and
  70.  *            missing case labels, and modified the failure message
  71.  *            to display the "reason" given by the modem.
  72.  *                            -- Dan Schullman
  73.  *    16-Mar-87    Support for the ATT7300 UNIX PC internal modem was
  74.  *            added.
  75.  *                            -- Richard E. Hill
  76.  *
  77.  *    14-Mar-88    Rewrite code for ATT7300 (here and in ckutio.c)
  78.  *            Avoids dial(3c) with it's LCK files, hangs up line
  79.  *            correctly, enables user interrupts and timeouts,
  80.  *            turns on/off the system getty() login procedure.
  81.  *            Correct Hayes command sequence at little.
  82.  *            Procedures: attdial, atthang, ongetty, offgetty.
  83.  *            Parts adapted from work of Richard E. Hill and
  84.  *            Kevin O'Gorman.
  85.  *                            -- Joe R. Doupnik
  86.  *
  87.  * 13-Jan-89 Add IBM/Siemens/Rolm CBX dialing support.  - F. da Cruz
  88.  *
  89.  */
  90.  
  91. /*
  92.  * To add support for another modem, do the following:
  93.  *
  94.  *    Define a modem number symbol (n_XXX) for it, keeping the list
  95.  *    in alphabetical and numerical order, and renumbering the values
  96.  *    as necessary.
  97.  *
  98.  *    Create a MDMINF structure for it, again keeping the list alphabetical
  99.  *    for sanity's sake.
  100.  *
  101.  *    Add the address of the MDMINF structure to the ptrtab array, again
  102.  *    in alphabetical and numerical order.
  103.  *
  104.  *    Add the "user visible" modem name and corresponding modem number to
  105.  *    the mdmtab array, again in alphabetical order.
  106.  *
  107.  *    Read through the code and add modem-specific sections as necessary.
  108.  */
  109.  
  110. /*
  111.  * The intent of the "unknown" modem is hopefully to allow KERMIT to support
  112.  * unknown modems by having the user type the entire autodial sequence
  113.  * (possibly including control characters, etc.) as the "phone number".
  114.  * The only reason that the CONNECT command cannot be used to do this is
  115.  * that a remote line cannot normally be opened unless carrier is present.
  116.  *
  117.  * The protocol and other characteristics of this modem are unknown, with
  118.  * some "reasonable" values being chosen for some of them.  The only way to
  119.  * detect if a connection is made is to look for carrier present.
  120.  *
  121.  * SUPPORT IS CURRENTLY ONLY PARTIALLY SKETCHED OUT FOR THIS.  ALSO, IT
  122.  * SHOULD PERHAPS BE HANDLED MUCH EARLIER, SIMPLY READING USER INPUT AND
  123.  * SENDING IT TO THE MODEM AND ECHOING MODEM RESPONSES BACK TO THE USER,
  124.  * ALL THE TIME LOOKING FOR CARRIER.  OF COURSE, THE PROBLEM THEN BECOMES
  125.  * ONE OF ALLOWING THE USER TO CANCEL THE DIALING.  WE COULD CHOOSE SOME
  126.  * PHRASE THAT WOULD PRESUMABLY NEVER BE A PART OF A VALID AUTODIAL SEQUENCE
  127.  * (E.G., "QUIT" and "quit"). -- DS
  128.  */
  129. #include "ckcdeb.h"
  130. #include <stdio.h>
  131. #include <ctype.h>
  132. #include <signal.h>
  133. #include "ckcker.h"
  134. #include "ckucmd.h"
  135.  
  136. #ifndef ZILOG
  137. #include <setjmp.h>            /* Longjumps */
  138. #else
  139. #include <setret.h>
  140. #endif
  141.  
  142. extern int flow, local, mdmtyp, quiet, speed, parity, seslog, ttyfd;
  143. extern char ttname[], sesfil[];
  144.  
  145. #define MDMINF    struct mdminf
  146.  
  147. MDMINF        /* structure for modem-specific information */
  148.     {
  149.     int        dial_time;    /* time modem allows for dialing (secs) */
  150.     char    *pause_chars;    /* character(s) to tell modem to pause */
  151.     int        pause_time;    /* time associated with pause chars (secs) */
  152.     char    *wake_str;    /* string to wakeup modem & put in cmd mode */
  153.     int        wake_rate;    /* delay between wake_str characters (msecs) */
  154.     char    *wake_prompt;    /* string prompt after wake_str */
  155.     char    *dmode_str;    /* string to put modem in dialing mode */
  156.     char    *dmode_prompt;    /* string prompt for dialing mode */
  157.     char    *dial_str;    /* dialing string, with "%s" for number */
  158.     int        dial_rate;    /* delay between dialing characters (msecs) */
  159.     };
  160.  
  161. /*
  162.  * Define symbolic modem numbers.
  163.  *
  164.  * The numbers MUST correspond to the ordering of entries
  165.  * within the ptrtab array, and start at one (1).
  166.  *
  167.  * It is assumed that there are relatively few of these
  168.  * values, and that the high(er) bytes of the value may
  169.  * be used for modem-specific mode information.
  170.  *
  171.  * REMEMBER that only the first eight characters of these
  172.  * names are guaranteed to be unique.
  173.  */
  174.  
  175. #define        n_CERMETEK     1
  176. #define        n_DF03         2
  177. #define        n_DF100         3
  178. #define        n_DF200         4
  179. #define        n_GDC         5
  180. #define        n_HAYES         6
  181. #define        n_PENRIL     7
  182. #define        n_RACAL         8
  183. #define        n_UNKNOWN     9
  184. #define        n_USROBOT    10
  185. #define        n_VENTEL    11
  186. #define        n_CONCORD    12
  187. #define        n_ATTUPC    13    /* aka Unix PC and ATT7300 */
  188. #define        n_ROLM          14      /* Rolm CBX */
  189.  
  190. /*
  191.  * Declare modem "variant" numbers for any of the above for which it is
  192.  * necessary to note various operational modes, using the second byte
  193.  * of a modem number.
  194.  *
  195.  * It is assumed that such modem modes share the same modem-specific
  196.  * information (see MDMINF structure) but may differ in some of the actions
  197.  * that are performed.
  198.  */
  199. #define        n_HAYESNV    ( n_HAYES + ( 1<<8 ) )
  200.  
  201. /*
  202.  * Declare structures containing modem-specific information.
  203.  *
  204.  * REMEMBER that only the first SEVEN characters of these
  205.  * names are guaranteed to be unique.
  206.  */
  207.  
  208. static
  209. MDMINF CERMETEK =    /* information for "Cermetek Info-Mate 212 A" modem */
  210.     {
  211.     20,            /* dial_time */
  212.     "BbPpTt",        /* pause_chars */
  213.     0,            /* pause_time */    /** unknown -- DS **/
  214.     "  XY\016R\r",    /* wake_str */
  215.     200,        /* wake_rate */
  216.     "",            /* wake_prompt */
  217.     "",            /* dmode_str */
  218.     "",            /* dmode_prompt */
  219.     "\016D '%s'\r",    /* dial_str */
  220.     200            /* dial_rate */
  221.     };
  222.  
  223. static
  224. MDMINF DF03 =        /* information for "DEC DF03-AC" modem */
  225.     {
  226.     27,            /* dial_time */
  227.     "=",        /* pause_chars */    /* wait for second dial tone */
  228.     15,            /* pause_time */
  229.     "\001\002",        /* wake_str */
  230.     0,            /* wake_rate */
  231.     "",            /* wake_prompt */
  232.     "",            /* dmode_str */
  233.     "",            /* dmode_prompt */
  234.     "%s",        /* dial_str */
  235.     0            /* dial_rate */
  236.     };
  237.  
  238. static
  239. MDMINF DF100 =        /* information for "DEC DF100-series" modem */
  240.             /*
  241.              * The telephone "number" can include "P"s and/or "T"s
  242.              * within it to indicate that subsequent digits are
  243.              * to be dialed using pulse or tone dialing.  The
  244.              * modem defaults to pulse dialing.  You may modify
  245.              * the dial string below to explicitly default all
  246.              * dialing to pulse or tone, but doing so prevents
  247.              * the use of phone numbers that you may have stored
  248.              * in the modem's memory.
  249.              */
  250.     {
  251.     30,            /* dial_time */
  252.     "=",        /* pause_chars */    /* wait for second dial tone */
  253.     15,            /* pause_time */
  254.     "\001",        /* wake_str */
  255.     0,            /* wake_rate */
  256.     "",            /* wake_prompt */
  257.     "",            /* dmode_str */
  258.     "",            /* dmode_prompt */
  259.     "%s#",        /* dial_str */
  260.     0            /* dial_rate */
  261.     };
  262.  
  263. static
  264. MDMINF DF200 =        /* information for "DEC DF200-series" modem */
  265.             /*
  266.              * The telephone "number" can include "P"s and/or "T"s
  267.              * within it to indicate that subsequent digits are
  268.              * to be dialed using pulse or tone dialing.  The
  269.              * modem defaults to pulse dialing.  You may modify
  270.              * the dial string below to explicitly default all
  271.              * dialing to pulse or tone, but doing so prevents
  272.              * the use of phone numbers that you may have stored
  273.              * in the modem's memory.
  274.              */
  275.     {
  276.     30,            /* dial_time */
  277.     "=W",        /* pause_chars */    /* =: second tone; W: 5 secs */
  278.     15,            /* pause_time */    /* worst case */
  279.     "\002",        /* wake_str */        /* allow stored number usage */
  280.     0,            /* wake_rate */
  281.     "",            /* wake_prompt */
  282.     "",            /* dmode_str */
  283.     "",            /* dmode_prompt */
  284.     "%s!",        /* dial_str */
  285.     0            /* dial_rate */
  286.     };
  287.  
  288. static
  289. MDMINF GDC =        /* information for "GeneralDataComm 212A/ED" modem */
  290.     {
  291.     32,            /* dial_time */
  292.     "%",        /* pause_chars */
  293.     3,            /* pause_time */
  294.     "\r\r",        /* wake_str */
  295.     500,        /* wake_rate */
  296.     "$",        /* wake_prompt */
  297.     "D\r",        /* dmode_str */
  298.     ":",        /* dmode_prompt */
  299.     "T%s\r",        /* dial_str */
  300.     0            /* dial_rate */
  301.     };
  302.  
  303. static
  304. MDMINF HAYES =        /* information for "Hayes" modem */
  305.     {
  306.     35,            /* dial_time */
  307.     ",",        /* pause_chars */
  308.     2,            /* pause_time */
  309.     "AT\r",        /* wake_str */
  310.     0,            /* wake_rate */
  311.     "",            /* wake_prompt */
  312.     "",            /* dmode_str */
  313.     "",            /* dmode_prompt */
  314.     "ATD%s\r",        /* dial_str, note: user can supply D or T */
  315.     0            /* dial_rate */
  316.     };
  317.  
  318. static
  319. MDMINF PENRIL =        /* information for "Penril" modem */
  320.     {
  321.     50,            /* dial_time */
  322.     "",            /* pause_chars */    /** unknown -- HF **/
  323.     0,            /* pause_time */
  324.     "\r\r",        /* wake_str */
  325.     300,        /* wake_rate */
  326.     ">",        /* wake_prompt */
  327.     "k\r",        /* dmode_str */
  328.     ":",        /* dmode_prompt */
  329.     "%s\r",        /* dial_str */
  330.     0            /* dial_rate */
  331.     };
  332.  
  333. static
  334. MDMINF RACAL =        /* information for "Racal Vadic" modem */
  335.     {
  336.     35,            /* dial_time */
  337.     "Kk",        /* pause_chars */
  338.     5,            /* pause_time */
  339.     "\005\r",        /* wake_str */
  340.     50,            /* wake_rate */
  341.     "*",        /* wake_prompt */
  342.     "D\r",        /* dmode_str */
  343.     "?",        /* dmode_prompt */
  344.     "%s\r",        /* dial_str */
  345.     0            /* dial_rate */
  346.     };
  347.  
  348. static
  349. MDMINF UNKNOWN =    /* information for "Unknown" modem */
  350.     {
  351.     30,            /* dial_time */
  352.     "",            /* pause_chars */
  353.     0,            /* pause_time */
  354.     "",            /* wake_str */
  355.     0,            /* wake_rate */
  356.     "",            /* wake_prompt */
  357.     "",            /* dmode_str */
  358.     "",            /* dmode_prompt */
  359.     "%s\r",        /* dial_str */
  360.     0            /* dial_rate */
  361.     };
  362.  
  363. static
  364. MDMINF USROBOT =    /* information for "US Robotics 212A" modem */
  365.     {
  366.     30,            /* dial_time */
  367.     ",",        /* pause_chars */
  368.     2,            /* pause_time */
  369.     "ATS2=01\r",    /* wake_str */
  370.     0,            /* wake_rate */
  371.     "OK\r",        /* wake_prompt */
  372.     "",            /* dmode_str */
  373.     "",            /* dmode_prompt */
  374.     "ATTD%s\r",        /* dial_str */
  375.     0            /* dial_rate */
  376.     };
  377.  
  378. static
  379. MDMINF VENTEL =        /* information for "Ventel" modem */
  380.     {
  381.     20,            /* dial_time */
  382.     "%",        /* pause_chars */
  383.     5,            /* pause_time */
  384.     "\r\r\r",        /* wake_str */
  385.     300,        /* wake_rate */
  386.     "$",        /* wake_prompt */
  387.     "",            /* dmode_str */
  388.     "",            /* dmode_prompt */
  389.     "<K%s\r>",        /* dial_str */
  390.     0            /* dial_rate */
  391.     };
  392.  
  393. static
  394. MDMINF CONCORD =    /* Info for Condor CDS 220 2400b modem */
  395.     {
  396.     35,            /* dial_time */
  397.     ",",        /* pause_chars */
  398.     2,            /* pause_time */
  399.     "\r\r",        /* wake_str */
  400.     20,            /* wake_rate */
  401.     "CDS >",        /* wake_prompt */
  402.     "",            /* dmode_str */
  403.     "",            /* dmode_prompt */
  404.     "<D M%s\r>",    /* dial_str */
  405.     0            /* dial_rate */
  406.     };
  407.  
  408. static
  409. MDMINF ATTUPC = /* dummy information for "ATT7300/Unix PC" internal modem */
  410.     {
  411.     20,            /* dial_time */
  412.     "",            /* pause_chars */
  413.     0,            /* pause_time */
  414.     "",            /* wake_str */
  415.     0,            /* wake_rate */
  416.     "",            /* wake_prompt */
  417.     "",            /* dmode_str */
  418.     "",            /* dmode_prompt */
  419.     "%s\r",        /* dial_str */
  420.     0            /* dial_rate */
  421.     };
  422.  
  423. static
  424. MDMINF ROLM =        /* IBM (Siemens) / Rolm CBX */
  425.     {
  426.     60,            /* dial_time */
  427.     "",            /* pause_chars */
  428.     0,            /* pause_time */
  429.     "\r\r",        /* wake_str */
  430.     5,            /* wake_rate */
  431.     "MODIFY?",            /* wake_prompt */
  432.     "",            /* dmode_str */
  433.     "",            /* dmode_prompt */
  434.     "CALL %s\r",    /* dial_str */
  435.     0            /* dial_rate */
  436.     };
  437.  
  438. /*
  439.  * Declare table for converting modem numbers to information pointers.
  440.  *
  441.  * The entries MUST be in ascending order by modem number, without any
  442.  * "gaps" in the numbers, and starting from one (1).
  443.  *
  444.  * This table should NOT include entries for the "variant" modem numbers,
  445.  * since it is assumed that they share the same information as the normal
  446.  * value.
  447.  */
  448. static
  449. MDMINF *ptrtab[] =
  450.     {
  451.     &CERMETEK,
  452.     &DF03,
  453.     &DF100,
  454.     &DF200,
  455.     &GDC,
  456.     &HAYES,
  457.     &PENRIL,
  458.     &RACAL,
  459.     &UNKNOWN,
  460.     &USROBOT,
  461.     &VENTEL,
  462.     &CONCORD,
  463.     &ATTUPC,            /* ATT7300 internal modem, jrd*/
  464.     &ROLM            /* Rolm CBX, fdc */
  465.     };
  466.  
  467. /*
  468.  * Declare modem names and associated numbers for command parsing,
  469.  * and also for doing number-to-name translation.
  470.  *
  471.  * The entries MUST be in alphabetical order by modem name.
  472.  */
  473. struct keytab mdmtab[] =
  474.     {
  475.     "att7300",        n_ATTUPC,    0,
  476.     "cermetek",        n_CERMETEK,    0,
  477.     "concord",        n_CONCORD,    0,
  478.     "df03-ac",        n_DF03,        0,
  479.     "df100-series",    n_DF100,    0,
  480.     "df200-series",    n_DF200,    0,
  481.     "direct",        0,        0,
  482.     "gendatacomm",    n_GDC,        0,
  483.     "hayes",        n_HAYES,    0,
  484.     "penril",        n_PENRIL,    0,
  485.     "racalvadic",    n_RACAL,    0,
  486.     "rolm",        n_ROLM,        0,
  487.     "unknown",        n_UNKNOWN,    0,
  488.     "usrobotics-212a",    n_USROBOT,    0,
  489.     "ventel",        n_VENTEL,    0
  490.     };
  491.  
  492. int nmdm = (sizeof(mdmtab) / sizeof(struct keytab));    /* number of modems */
  493.  
  494. #define DIALING 4        /* for ttpkt parameter */
  495. #define CONNECT 5
  496.  
  497. #define CONNECTED 1        /* for completion status */
  498. #define FAILED      2
  499.  
  500. /*
  501.  * Failure reasons for use with the 'longjmp' exit.
  502.  */
  503. #define F_time        1    /* timeout */
  504. #define F_int        2    /* interrupt */
  505. #define F_modem        3    /* modem-detected failure */
  506. #define F_minit        4    /* cannot initialize modem */
  507.  
  508. static
  509. char *F_reason[5] = {        /* failure reasons for message */
  510.     "Unknown",    "Timeout", "Interrupt", "Modem", "Initialize" };
  511.  
  512. static int tries = 0;
  513.  
  514. #define LBUFL 100
  515. static char lbuf[LBUFL];
  516.  
  517. static jmp_buf sjbuf;
  518.  
  519. static SIGTYP (*savAlrm)();    /* for saving alarm handler */
  520. static SIGTYP (*savInt)();    /* for saving interrupt handler */
  521.  
  522. dialtime() {            /* timer interrupt handler */
  523.     longjmp( sjbuf, F_time );
  524. }
  525.  
  526. dialint()            /* user-interrupt handler */
  527.     {
  528.     longjmp( sjbuf, F_int );
  529.     }
  530.  
  531. static
  532. ttolSlow(s,millisec) char *s; int millisec; {  /* output s-l-o-w-l-y */
  533.     for (; *s; s++) {
  534.     ttoc(*s);
  535.     msleep(millisec);
  536.     }
  537.     }
  538.  
  539. /*
  540.  * Wait for a string of characters.
  541.  *
  542.  * The characters are waited for individually, and other characters may
  543.  * be received "in between".  This merely guarantees that the characters
  544.  * ARE received, and in the order specified.
  545.  */
  546. static
  547. waitFor(s) char *s;
  548.     {
  549.     CHAR c;
  550.     while ( c = *s++ )            /* while more characters remain... */
  551.     while ( ( ttinc(0) & 0177 ) != c ) ;    /* wait for the character */
  552.     }
  553.  
  554. static
  555. didWeGet(s,r) char *s, *r; {    /* Looks in string s for response r */
  556.     int lr = strlen(r);        /*  0 means not found, 1 means found it */
  557.     int i;
  558.     debug(F110,"didWeGet",r,0);
  559.     debug(F110," in",s,0);
  560.     for (i = strlen(s)-lr; i >= 0; i--)
  561.     if ( s[i] == r[0] ) if ( !strncmp(s+i,r,lr) ) return( 1 );
  562.     return( 0 );
  563. }
  564.  
  565.  
  566. /* R E S E T -- Reset alarms, etc. on exit. */
  567.  
  568. static
  569. reset ()
  570.     {
  571.     alarm(0);
  572.     signal(SIGALRM,savAlrm);        /* restore alarm handler */
  573.     signal(SIGINT,savInt);        /* restore interrupt handler */
  574.     }
  575.  
  576. /*  C K D I A L     --  Dial up the remote system */
  577.  
  578. ckdial(telnbr) char *telnbr; {
  579.  
  580.     char c;
  581.     char *i, *j;
  582.     int waitct, status;
  583.     char errmsg[50], *erp;
  584.     MDMINF *pmdminf;    /* pointer to modem-specific info */
  585.     int augmdmtyp;    /* "augmented" modem type, to handle modem modes */
  586.     int mdmEcho = 0;    /* assume modem does not echo */
  587.     int n, n1;
  588.     char *pc;        /* pointer to a character */
  589.  
  590.     if (!mdmtyp) {
  591.         printf("Sorry, you must 'set modem' first\n");
  592.         return(-2);
  593.     }
  594.     if (!local) {
  595.         printf("Sorry, you must 'set line' first\n");
  596.         return(-2);
  597.     }
  598.     if (speed < 0) {
  599.         printf("Sorry, you must 'set speed' first\n");
  600.         return(-2);
  601.     }
  602.         debug(F110,"dial",telnbr,0);
  603.  
  604.     /* Carrier no-wait can be invalidated by ckutio fun and games, jrd */
  605.     if (ttopen(ttname,&local,mdmtyp) < 0) { /* Open, no carrier wait */
  606.         erp = errmsg;
  607.         sprintf(erp,"Sorry, can't open %s",ttname);
  608.         perror(errmsg);
  609.         return(-2);
  610.     }
  611.     pmdminf = ptrtab[mdmtyp-1];    /* set pointer to modem info */
  612.     augmdmtyp = mdmtyp;        /* initialize "augmented" modem type */
  613.  
  614. /* cont'd... */
  615.  
  616.                        /* interdigit waits for tone dial */
  617. /* ...dial, cont'd */
  618.  
  619.  
  620.     waitct = 1*strlen(telnbr) ;    /* compute time to dial worst case */
  621.     waitct += pmdminf->dial_time;    /* dialtone + completion wait times */
  622.     for (i=telnbr; *i; i++)        /* add in pause characters time */
  623.         for (j=pmdminf->pause_chars; *j; j++)
  624.         if (*i == *j) {
  625.             waitct += pmdminf->pause_time;
  626.             break;
  627.             }
  628.  
  629.        printf("Dialing thru %s, speed %d, number %s.\r\n",ttname,speed,telnbr);
  630.        printf("The timeout for completing the call is %d seconds.\r\n",waitct);
  631.        printf("Type the interrupt character (^C) to cancel the dialing.\r\n");
  632.        debug(F101,ttname,"",speed);
  633.        debug(F101,"timeout","",waitct);
  634.  
  635. /* Hang up the modem (in case it wasn't "on hook") */
  636.  
  637.     if ( tthang() < 0 ) {
  638.         printf("Sorry, Can't hang up tty line\n");
  639.         return(-2);
  640.         }
  641.  
  642. /* Condition console terminal and communication line */
  643.                 /* place line into "clocal" dialing state */
  644.     if ( ttpkt(speed,DIALING,parity) < 0 )    {
  645.         printf("Sorry, Can't condition communication line\n");
  646.         return(-2);
  647.     }
  648.  
  649. /*
  650.  * Establish jump vector, or handle "failure" jumps.
  651.  */
  652.  
  653.     if ( n = setjmp(sjbuf) )        /* if a "failure jump" was taken... */
  654.     {
  655.     alarm ( 0 );            /* disable timeouts */
  656.     if ( n1 = setjmp(sjbuf) )    /* failure while handling failure */
  657.         {
  658.         printf ( "%s failure while handling failure.\r\n", F_reason[n1] );
  659.         }
  660.     else                /* first (i.e., non-nested) failure */
  661.         {
  662.         signal ( SIGALRM, dialtime );    /* be sure to catch signals */
  663.         if ( signal ( SIGINT, SIG_IGN ) != SIG_IGN )
  664.         signal ( SIGINT, dialint );
  665.         alarm ( 10 );        /* be sure to exit this section */
  666.         ttclos ();            /* hangup and close the line */
  667.         }
  668.     switch ( n )            /* type of failure */
  669.         {
  670.         case F_time:        /* timed out */
  671.         {
  672.         printf ( "No connection made within the allotted time.\r\n" );
  673.         debug(F110,"dial","timeout",0);
  674.         break;
  675.         }
  676.         case F_int:            /* dialing interrupted */
  677.         {
  678.         printf ( "Dialing interrupted.\r\n" );
  679.         debug(F110,"dial","interrupted",0);
  680.         break;
  681.         }
  682.         case F_modem:        /* modem detected a failure */
  683.         {
  684.         printf ( "Failed (\"" );
  685.         for ( pc=lbuf; *pc; pc++ )
  686.             if ( isprint(*pc) )
  687.             putchar(*pc);    /* display printable reason */
  688.         printf ( "\").\r\n" );
  689.         debug(F110,"dial",lbuf,0);
  690.         break;
  691.         }
  692.         case F_minit:        /* cannot initialize modem */
  693.         {
  694.         printf ( "Cannot initialize modem.\r\n" );
  695.         debug(F110,"dial","modem init",0);
  696.         break;
  697.         }
  698.         }
  699.     reset();            /* reset alarms, etc. */
  700.     return ( -2 );            /* exit with failure code */
  701.     }
  702.  
  703. /*
  704.  * Set timer and interrupt handlers.
  705.  */
  706.     ttflui();            /* flush input buffer if any */
  707.  
  708.     savAlrm = signal(SIGALRM,dialtime); /* set alarm handler */
  709.     if ( ( savInt = signal ( SIGINT, SIG_IGN ) ) != SIG_IGN )
  710.     signal ( SIGINT, dialint );    /* set int handler if not ignored */
  711.     alarm(10);            /* give modem 10 seconds to wake up */
  712.  
  713. /*
  714.  * Put modem in command mode.
  715.  */
  716.  
  717. #define OKAY 1            /* modem attention attempt status */
  718. #define IGNORE 2
  719. #define GOT_O -2
  720. #define GOT_A -3
  721.  
  722. switch (augmdmtyp) {
  723.  
  724. #ifdef ATT7300
  725.     case n_ATTUPC:
  726. /*
  727.   For ATT7300/Unix PC's with their special internal modem. Timeout
  728.   and user interrupts are enabled during dialing.  attdial() is in
  729.   file ckutio.c.  - jrd
  730. */
  731.     {
  732.     alarm(waitct);                /* do alarm properly */
  733.     if (attdial(ttname,speed,telnbr))    /* dial internal modem */
  734.         {
  735.         reset();            /* reset alarms, etc. */
  736.         if ( ! quiet ) printf("Call did not complete.");
  737.         ttclos();            /* close line if open */
  738.         return (-2);            /* return failure */
  739.         }
  740.     reset();                /* reset alarms, etc. */
  741.     ttpkt(speed,CONNECT,parity);        /* cancel dialing ioctl */
  742.     if ( ! quiet ) printf ("Call completed.\07\r\n");
  743.     return(0);     /* no conversation with modem to complete dialing */
  744.     }
  745. #endif /* att7300 */
  746.  
  747.     case n_HAYES:
  748.     case n_HAYESNV:
  749.     while(tries++ < 4) {
  750.         ttol( HAYES.wake_str, strlen(HAYES.wake_str) );    /* wakeup */
  751.         status = 0;
  752.         while ( status <= 0 ) {
  753.         switch (ttinc(0) & 0177) {
  754.             case 'A':            /* echoing, ignore */
  755.             status = GOT_A;
  756.             break;
  757.             case 'T':
  758.             if (status == GOT_A) {
  759.                 mdmEcho = 1;    /* expect echoing later */
  760.                 status = 0;
  761.                 break;
  762.             }
  763.             status = IGNORE;
  764.             break;
  765.             case '\n':
  766.             case '\r':
  767.             status = 0;
  768.             break;
  769.             case '0':            /* numeric result code */
  770.             augmdmtyp = n_HAYESNV;    /* nonverbal result codes */
  771.             status = OKAY;
  772.             break;
  773.             case 'O':            /* maybe English result code*/
  774.             status = GOT_O;
  775.             break;
  776.             case 'K':
  777.             if (status == GOT_O) {
  778.                 augmdmtyp = n_HAYES;
  779.                 status = OKAY;
  780.                 break;
  781.             }            /* else its default anyway */
  782.             default:
  783.             status = IGNORE;
  784.             break;
  785.             }
  786.         }
  787.         if (status == OKAY) break;
  788.         if (status == IGNORE) ttflui();
  789.         sleep(1);        /* wait before retrying */
  790.     }
  791.     if (status != 0) break;
  792.     longjmp( sjbuf, F_minit );    /* modem-initialization failure */
  793.  
  794. /* cont'd... */
  795.  
  796.                     /* interdigit waits for tone dial */
  797. /* ...dial, cont'd */
  798.  
  799.     default:            /* place modem into command mode */
  800.     ttolSlow(pmdminf->wake_str, pmdminf->wake_rate);
  801.     waitFor(pmdminf->wake_prompt);
  802.     break;
  803.     }
  804.     alarm(0);            /* turn off alarm */
  805.     msleep(500);        /* give things settling time */
  806.     alarm(10);            /* alarm on dialing prompts */
  807.  
  808.  
  809. /* Dial the number */
  810.                 /* put modem into dialing mode */
  811.     ttolSlow(pmdminf->dmode_str, pmdminf->dial_rate);
  812.     if (pmdminf->dmode_prompt) {    /* wait for prompt, if any expected */
  813.     waitFor(pmdminf->dmode_prompt);
  814.     msleep(300);
  815.     }
  816.  
  817.     alarm(0);            /* turn off alarm on dialing prompts */
  818.     alarm(waitct);        /* time to allow for connecting */
  819.     ttflui();            /* clear out stuff from waking modem up */
  820.     sprintf(lbuf, pmdminf->dial_str, telnbr); /* form dialing string */
  821.     debug(F110,"dialing",lbuf);
  822.     ttolSlow(lbuf,pmdminf->dial_rate);    /* send dialing string */
  823.  
  824.     if (augmdmtyp == n_RACAL) { /* acknowledge printout of dialing string */
  825.     sleep(3);
  826.     ttflui();
  827.     ttoc('\r');
  828.     }
  829.  
  830. /* cont'd... */
  831.  
  832.                     /* interdigit waits for tone dial */
  833. /* ...dial, cont'd */
  834.  
  835.  
  836. /* Check for connection */
  837.  
  838. /*
  839.  * I believe we also need to look for carrier in order to determine if a
  840.  * connection has been made.  In fact, for many we may only want to look for
  841.  * the "failure" responses in order to short-circuit the timeout, and let
  842.  * carrier be the determination of whether a connection has been made. -- DS
  843.  */
  844.  
  845.     status = 0;
  846.     strcpy(lbuf,"No Connection");    /* default failure reason */
  847.     while (status == 0) {
  848.       switch (augmdmtyp) {
  849.     default:
  850.         for (n=0; n < LBUFL; n++) { /* accumulate response */
  851.         lbuf[n] = (ttinc(0) & 0177);
  852.         if ( lbuf[n] == '\r' || lbuf[n] == '\n' ) break;
  853.         }
  854.         lbuf[n] = '\0';        /* terminate response from modem */
  855.         debug(F110,"dial modem response",lbuf,0);
  856.         if (n) {            /* if one or more characters present */
  857.         switch (augmdmtyp) {
  858.           case n_CERMETEK:
  859.             if (didWeGet(lbuf,"\016A")) {
  860.             status = CONNECTED;
  861.             ttolSlow("\016U 1\r",200);    /* make transparent*/
  862.             }
  863.             break;
  864.           case n_DF100:         /* DF100 won't generate some of these */
  865.           case n_DF200:
  866.             if (didWeGet(lbuf,"Attached")) status = CONNECTED;
  867.             /*
  868.              * The DF100 will respond with "Attached" even if DTR
  869.              * and/or carrier are not present.    Another reason to
  870.              * (also) wait for carrier?
  871.              */
  872.             if (didWeGet(lbuf,"Busy")) status = FAILED;
  873.             if (didWeGet(lbuf,"Disconnected")) status = FAILED;
  874.             if (didWeGet(lbuf,"Error")) status = FAILED;
  875.             if (didWeGet(lbuf,"No answer")) status = FAILED;
  876.             if (didWeGet(lbuf,"No dial tone")) status = FAILED;
  877.             if (didWeGet(lbuf,"Speed:")) status = FAILED;
  878.             /*
  879.              * It appears that the "Speed:..." response comes after an
  880.              * "Attached" response, so this is never seen.  HOWEVER,
  881.              * it would be very handy to detect this and temporarily
  882.              * reset the speed, since it's a nuiscance otherwise.
  883.              * If we wait for some more input from the modem, how do
  884.              * we know if it's from the remote host or the modem?
  885.              * Carrier reportedly doesn't get set until after the
  886.              * "Speed:..." response (if any) is sent.  Another reason
  887.              * to (also) wait for carrier.
  888.              */
  889.             break;
  890.           case n_GDC:
  891.             if (didWeGet(lbuf,"ON LINE")) status = CONNECTED;
  892.             if (didWeGet(lbuf,"NO CONNECT")) status = FAILED;
  893.             break;
  894.           case n_HAYES:
  895.           case n_USROBOT:
  896.             if (didWeGet(lbuf,"CONNECT")) status = CONNECTED;
  897.             if (didWeGet(lbuf,"NO CARRIER")) status = FAILED;
  898.             break;
  899.           case n_PENRIL:
  900.             if (didWeGet(lbuf,"OK")) status = CONNECTED;
  901.             if (didWeGet(lbuf,"BUSY")) status = FAILED;
  902.             if (didWeGet(lbuf,"NO RING")) status = FAILED;
  903.             break;
  904.           case n_RACAL:
  905.             if (didWeGet(lbuf,"ON LINE")) status = CONNECTED;
  906.             if (didWeGet(lbuf,"FAILED CALL")) status = FAILED;
  907.             break;
  908.           case n_ROLM:
  909.             if (didWeGet(lbuf,"CALLING")) status = 0;
  910.             if (didWeGet(lbuf,"COMPLETE")) status = CONNECTED;
  911.             if (didWeGet(lbuf,"FAILED")) status = FAILED;
  912.             if (didWeGet(lbuf,"NOT AVAILABLE")) status = FAILED;
  913.             if (didWeGet(lbuf,"LACKS PERMISSION")) status = FAILED;
  914.             /*
  915.               The Rolm CBX does not give a CALL COMPLETE indication
  916.               when dialing an outpool number, but it does seem to
  917.               return a long string of DELs at that point.
  918.             */
  919.             if (didWeGet(lbuf,"\177\177\177")) status = CONNECTED;
  920.             break;        
  921.           case n_VENTEL:
  922.             if (didWeGet(lbuf,"ONLINE!")) status = CONNECTED;
  923.             if (didWeGet(lbuf,"BUSY")) status = FAILED;
  924.             if (didWeGet(lbuf,"DEAD PHONE")) status = FAILED;
  925.             break;
  926.           case n_CONCORD:
  927.             if (didWeGet(lbuf,"INITIATING")) status = CONNECTED;
  928.             if (didWeGet(lbuf,"BUSY")) status = FAILED;
  929.             if (didWeGet(lbuf,"CALL FAILED")) status = FAILED;
  930.             break;
  931.         }
  932.         }
  933.         break;
  934.  
  935.     case n_DF03:            /* because response lacks CR or NL */
  936.         c = ttinc(0) & 0177;
  937.         if ( c == 'A' ) status = CONNECTED;
  938.         if ( c == 'B' ) status = FAILED;
  939.         break;
  940.  
  941.     case n_HAYESNV:
  942.         c = ttinc(0) & 0177;
  943.         if (mdmEcho) {        /* sponge up dialing string */
  944.         mdmEcho = c!='\r';    /* until return is echoed */
  945.         break;
  946.         }
  947.         if (c == '1') status = CONNECTED;
  948.         if (c == '3') status = FAILED;
  949.         if (c == '5') status = CONNECTED;
  950.         break;
  951.  
  952.     case n_UNKNOWN:
  953.         /** SHOULD WAIT FOR CARRIER OR TIMEOUT -- DS **/
  954.         break;
  955.     }                /* switch (augmdmtyp) */
  956.     }                    /* while status == 0 */
  957.     alarm(0);                /* turn off alarm on connecting */
  958.     if ( status != CONNECTED )        /* modem-detected failure */
  959.     longjmp( sjbuf, F_modem );    /* exit (with reason in lbuf) */
  960.     msleep(500);            /* allow some time...  */
  961.     alarm(3);                /* precaution in case of trouble */
  962.     debug(F110,"dial","succeeded",0);
  963.     if (augmdmtyp != n_ROLM)        /* Rolm has wierd modem signaling */
  964.       ttpkt(speed,CONNECT,parity);    /* cancel dialing state ioctl */
  965.     reset ();                /* reset alarms, etc. */
  966.     if ( ! quiet )
  967.     printf ( "Call completed.\07\r\n" );
  968.     return ( 0 );            /* return, and presumably connect */
  969. }
  970.