home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part03 / di_win.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  9.0 KB  |  383 lines

  1. /*
  2.  * The dialing window routines.
  3.  */
  4.  
  5. #define MAX_PASS 25
  6.  
  7. #include <stdio.h>
  8. #include <curses.h>
  9. #include <signal.h>
  10. #include <setjmp.h>
  11. #include "dial_dir.h"
  12. #include "misc.h"
  13. #include "modem.h"
  14. #include "param.h"
  15. #include "status.h"
  16.  
  17. /*
  18.  * The dialing window.  Its job is to kill the input routine, get a port,
  19.  * cycle thru the entries in the queue, while interpreting both the
  20.  * user's requests and the modem's responses.  A return code of 1 means
  21.  * we're ready to fire up the input mode.
  22.  */
  23.  
  24. int
  25. dial_win()
  26. {
  27.     WINDOW *di_win, *newwin();
  28.     int i, j, ans, want_out, pass, tic, baud, modem_sync;
  29.     long now, time();
  30.     char *tbuf, *ctime(), *str, cr=13, *monitor_dial();
  31.     void disp_queue(), send_str(), dial_it(), delay_times(), input_off();
  32.     void error_win(), status_line(), line_set(), hang_up();
  33.     extern int rc_index;
  34.     unsigned int sleep();
  35.                     /* are we already talking? */
  36.     input_off();
  37.     hang_up(1);
  38.  
  39.     if (get_port())
  40.         return(0);
  41.     /*
  42.      * If the phone number is a NULL, then either we are on a
  43.      * direct line, or you want to do the dialing yourself.
  44.      */
  45.     if (*dir->number[dir->q_num[0]] == NULL) {
  46.                     /* check LD permission */
  47.         if (limit_ld(0))
  48.             return(0);
  49.                     /* can't talk directly to OBM */
  50.         if (!strcmp(modem->mname[modem->m_cur], "OBM")) {
  51.             error_win(0, "Can't access the line directly.", "Use the automatic dialing feature");
  52.             return(0);
  53.         }
  54.         unlink(status->vs_path);
  55.         touchwin(stdscr);
  56.         clear();
  57.         printw("Connected to %s at %d baud...\n", modem->tty[modem->t_cur], dir->baud[dir->d_cur]);
  58.         refresh();
  59.         return(1);
  60.     }
  61.  
  62.     di_win = newwin(17, 70, 3, 5);
  63.                     /* the basic window */
  64.     mvwattrstr(di_win, 1, 20, A_BOLD, "D I A L I N G       W I N D O W");
  65.     mvwaddstr(di_win, 2, 0, "---------------------------------------------------------------------");
  66.     mvwaddstr(di_win, 4, 23, "System name:");
  67.     mvwaddstr(di_win, 5, 23, "Pass number:");
  68.     mvwaddstr(di_win, 6, 14, "Elapse time this try:");
  69.     mvwaddstr(di_win, 7, 13, "Time at start of dial:");
  70.     mvwaddstr(di_win, 8, 9, "Time at start of this try:");
  71.     mvwaddstr(di_win, 9, 16, "Connect delay time:");
  72.     mvwaddstr(di_win, 10, 15, "Pause between tries:");
  73.     mvwaddstr(di_win, 11, 25, "Index/tty:");
  74.     mvwaddstr(di_win, 12, 16, "Result of last try:");
  75.  
  76.     mvwaddstr(di_win, 14, 3, "SPACE: Recycle");
  77.     mvwaddstr(di_win, 14, 21, "DEL: Remove from queue");
  78.     mvwaddstr(di_win, 14, 46, "E: Change delay times");
  79.  
  80.                     /* the start time */
  81.     time(&now);
  82.     tbuf = ctime(&now);
  83.     tbuf[19] = NULL;
  84.     mvwaddstr(di_win, 7, 36, &tbuf[11]);
  85.  
  86.     mvwprintw(di_win, 9, 36, "%-4d", param->cdelay);
  87.     mvwprintw(di_win, 10, 36, "%-4d", param->pause);
  88.  
  89.     box(di_win, '|', '-');
  90.     mvwaddstr(di_win, 16, 25, " Press ESC to abort ");
  91.  
  92.     pass = 0;
  93.     i = 0;
  94.     want_out = 0;
  95.     while (!want_out && pass <= MAX_PASS) {
  96.         ans = -1;
  97.         pass++;
  98.                     /* update the d_cur variable */
  99.         dir->d_cur = dir->q_num[i];
  100.                     /* check LD permission */
  101.         if (limit_ld(i))
  102.             return(0);
  103.                     /* get a port */
  104.         if (get_port())
  105.             return(0);
  106.                     /* can the modem sync baud rates ? */
  107.         modem_sync = can_sync();
  108.                     /* fill in the window */
  109.         disp_queue(di_win, dir->d_cur, pass);
  110.  
  111.         /*
  112.          * The actual dial routine. The 'i' is the index into the
  113.          * queue, not the entry number.  Returns immediately without
  114.          * waiting for a carrier.
  115.          */
  116.         dial_it(i);
  117.         ioctl(status->fd, TCFLSH, 0);
  118.  
  119.         /*
  120.          * Here we do a time-slice between reading the result codes
  121.          * from the modem and reading the keyboard.  The one second
  122.          * granularity won't be too accurate, but who cares?
  123.          */
  124.         tic = 0;
  125.         rc_index = 0;
  126.         while (tic < param->cdelay) {
  127.             if (!(str = monitor_dial())) {
  128.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  129.                 wrefresh(di_win);
  130.             }
  131.             else {
  132.                 /*
  133.                  * A return code that converts to an number
  134.                  * that is less than 300 is probably an error
  135.                  * message.
  136.                  */
  137.                 baud = atoi(str);
  138.                 if (baud < 300) {
  139.                     mvwprintw(di_win, 12, 36, "%-20.20s", str);
  140.                     wmove(di_win, 12, 36);
  141.                     wrefresh(di_win);
  142.                     break;
  143.                 }
  144.                     /* we're connected */
  145.                 beep();
  146.                 clear_line(di_win, 12, 36, 1);
  147.                 wattrstr(di_win, A_BLINK, "CONNECTED");
  148.                 wmove(di_win, 12, 36);
  149.                 wrefresh(di_win);
  150.                 wait_key(di_win, 2);
  151.                 delwin(di_win);
  152.  
  153.                 /*
  154.                  * Did the modem sync at a different baud
  155.                  * rate than what we expected?
  156.                  */
  157.                 if (modem_sync) {
  158.                     if (dir->baud[dir->d_cur] != baud) {
  159.                         dir->baud[dir->d_cur] = baud;
  160.                         line_set();
  161.                     }
  162.                 }
  163.  
  164.                 unlink(status->vs_path);
  165.                 touchwin(stdscr);
  166.                 clear();
  167.                 printw("Connected to %s at %d baud...\n",
  168.                  dir->name[dir->d_cur], dir->baud[dir->d_cur]);
  169.                 refresh();
  170.  
  171.                     /* log the call */
  172.                 log_calls(i);
  173.                 return(1);
  174.             }
  175.             if (tic == param->cdelay)
  176.                 break;
  177.                     /* ok... try the keyboard */
  178.             if ((ans = wait_key(di_win, 1)) != -1)
  179.                 break;
  180.  
  181.             mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  182.             wrefresh(di_win);
  183.         }
  184.         /*
  185.          * If the modem did not return a code, then we need to
  186.          * stop it.  Sending a CR will stop most modems cold,
  187.          * except of course for the OBM...
  188.          */
  189.         if (str == NULL) {
  190.             if (!strcmp(modem->mname[modem->m_cur], "OBM"))
  191.                 hang_up(0);
  192.             else
  193.                 write(status->fd, &cr, 1);
  194.             sleep(1);
  195.         }
  196.                     /* if we get here, no key was pressed */
  197.         if (ans == -1) {
  198.             clear_line(di_win, 6, 14, 1);
  199.             mvwaddstr(di_win, 6, 27, "Pausing:");
  200.                     /* no return code ? */
  201.             if (str == NULL) {
  202.                 clear_line(di_win, 12, 36, 1);
  203.                 waddstr(di_win, "TIMED OUT");
  204.                 wmove(di_win, 12, 36);
  205.             }
  206.                     /* do the pause */
  207.             tic = 0;
  208.             while (tic < param->pause) {
  209.                 if ((ans = wait_key(di_win, 1)) != -1)
  210.                     break;
  211.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  212.                 wrefresh(di_win);
  213.             }
  214.             clear_line(di_win, 6, 14, 1);
  215.             waddstr(di_win, "Elapse time this try:");
  216.         }
  217.                     /* Process the keystroke */
  218.         switch(ans) {
  219.             case ' ':    /* next in the queue */
  220.                 clear_line(di_win, 12, 36, 1);
  221.                 waddstr(di_win, "RECYCLED");
  222.                 wmove(di_win, 12, 36);
  223.                 wrefresh(di_win);
  224.                 /* fall thru... */
  225.             case -1:    /* no key was pressed */
  226.                 i++;
  227.                 if (i > NUM_QUEUE)
  228.                     i = 0;
  229.                 if (dir->q_num[i] == -1)
  230.                     i = 0;
  231.                 break;
  232.             case 127:    /* DEL key, remove from queue */
  233.                 if (dir->q_num[1] == -1) {
  234.                     beep();
  235.                     clear_line(di_win, 12, 36, 1);
  236.                     waddstr(di_win, "NO MORE ENTRIES");
  237.                     wmove(di_win, 12, 36);
  238.                     wrefresh(di_win);
  239.                     wait_key(di_win, 3);
  240.                     break;
  241.                 }
  242.                 clear_line(di_win, 12, 36, 1);
  243.                 waddstr(di_win, "ENTRY DELETED");
  244.                 wmove(di_win, 12, 36);
  245.                 wrefresh(di_win);
  246.                 wait_key(di_win, 3);
  247.  
  248.                     /* compact the queue */
  249.                 for (j=i; j<NUM_QUEUE-1; j++)
  250.                     dir->q_num[j] = dir->q_num[j+1];
  251.                 dir->q_num[NUM_QUEUE-1] = -1;
  252.                 break;
  253.             case 'e':
  254.             case 'E':    /* change delay time */
  255.                 delay_times();
  256.                 touchwin(di_win);
  257.                 mvwprintw(di_win, 9, 36, "%-4d", param->cdelay);
  258.                 mvwprintw(di_win, 10, 36, "%-4d", param->pause);
  259.                 break;
  260.             case 27:    /* ESC key */
  261.                 beep();
  262.                 clear_line(di_win, 12, 36, 1);
  263.                 wattrstr(di_win, A_BLINK, "DIAL ABORTED");
  264.                 wmove(di_win, 12, 36);
  265.                 wrefresh(di_win);
  266.                 wait_key(di_win, 3);
  267.                 want_out++;
  268.                 break;
  269.             default:
  270.                 beep();
  271.                 break;
  272.         }
  273.     }
  274.                     /* clean up and go home */
  275.     werase(di_win);
  276.     wrefresh(di_win);
  277.     delwin(di_win);
  278.     if (!want_out)
  279.         error_win(0, "Exceeded the maximum number number of dialing attempts", NULL);
  280.     return(0);
  281. }
  282.  
  283. /*
  284.  * Display what info we know at this time.
  285.  */
  286.  
  287. void
  288. disp_queue(win, entry, pass)
  289. WINDOW *win;
  290. int entry, pass;
  291. {
  292.     long now, time();
  293.     char *tbuf, *ctime();
  294.     void status_line();
  295.                     /* redo the status line */
  296.     status_line(NULL);
  297.                     /* system name */
  298.     clear_line(win, 4, 36, 1);
  299.     waddstr(win, dir->name[entry]);
  300.                     /* pass number */
  301.     mvwprintw(win, 5, 36, "%-4d", pass);
  302.                     /* time of this call */
  303.     time(&now);
  304.     tbuf = ctime(&now);
  305.     tbuf[19] = NULL;
  306.     mvwaddstr(win, 8, 36, &tbuf[11]);
  307.                     /* the index field */    
  308.     clear_line(win, 11, 36, 1);
  309.     waddstr(win, dir->index[entry]);
  310.  
  311.     wmove(win, 12, 36);
  312.     wrefresh(win);
  313.     return;
  314. }
  315.  
  316. /*
  317.  * Monitor the progress of the dialing, but returns a NULL after one
  318.  * second of inactivity.
  319.  */
  320.  
  321. jmp_buf md_jmp;
  322.  
  323. char *
  324. monitor_dial()
  325. {
  326.     int force_md();
  327.     char *ans, *read_codes();
  328.     unsigned int alarm();
  329.  
  330.     if (setjmp(md_jmp))
  331.         return(NULL);
  332.     signal(SIGALRM, force_md);
  333.     alarm(1);
  334.     ans = read_codes();
  335.     alarm(0);
  336.     return(ans);
  337. }
  338. int
  339. force_md(dummy)
  340. int dummy;
  341. {
  342.     void longjmp();
  343.  
  344.     longjmp(md_jmp, 1);
  345. }
  346.  
  347. /*
  348.  * Determine if the modem can detect the synchronization of the baud
  349.  * rate if it's different that what it expects.  To do this, we check
  350.  * to see if any two connect strings are the same.  A return code of
  351.  * 1 means the modem can sync.
  352.  */
  353.  
  354. int
  355. can_sync()
  356. {
  357.     int i;
  358.  
  359.     i = modem->m_cur;
  360.                     /* not both null (sneaky trick) */
  361.     if (*modem->con_3[i] + *modem->con_12[i]) {
  362.         if (!strcmp(modem->con_3[i], modem->con_12[i]))
  363.             return(0);
  364.     }
  365.     if (*modem->con_12[i] + *modem->con_24[i]) {
  366.         if (!strcmp(modem->con_12[i], modem->con_24[i]))
  367.             return(0);
  368.     }
  369.     if (*modem->con_24[i] + *modem->con_48[i]) {
  370.         if (!strcmp(modem->con_24[i], modem->con_48[i]))
  371.             return(0);
  372.     }
  373.     if (*modem->con_48[i] + *modem->con_96[i]) {
  374.         if (!strcmp(modem->con_48[i], modem->con_96[i]))
  375.             return(0);
  376.     }
  377.     if (*modem->con_96[i] + *modem->con_192[i]) {
  378.         if (!strcmp(modem->con_96[i], modem->con_192[i]))
  379.             return(0);
  380.     }
  381.     return(1);
  382. }
  383.