home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / pcomm2 / part06 / s_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  4.7 KB  |  205 lines

  1. /*
  2.  * Display the TTY setup, query for changes.  A return code of 1
  3.  * means something was changed.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <curses.h>
  8. #include "config.h"
  9. #include "misc.h"
  10. #include "modem.h"
  11.  
  12. int
  13. tty_setup()
  14. {
  15.     extern char *null_ptr;
  16.     WINDOW *tt_win, *newwin();
  17.     char *strdup(), message[80];
  18.     int num, i, j, ret_code;
  19.     void disp_tty(), create_modem(), del_modem(), error_win();
  20.     void del_tty();
  21.  
  22.     tt_win = newwin(23, 80, 0, 0);
  23.  
  24.     horizontal(tt_win, 0, 0, 34);
  25.     mvwattrstr(tt_win, 0, 35, A_BOLD, "TTY Setup");
  26.     horizontal(tt_win, 0, 45, 34);
  27.     mvwaddstr(tt_win, 2, 22, "TTY name");
  28.     mvwaddstr(tt_win, 2, 37, "Modem name");
  29.     mvwaddstr(tt_win, 2, 51, "Init speed");
  30.                     /* display the current TTY list */
  31.     disp_tty(tt_win);
  32.                     /* prompt for options */
  33.     mvwprintw(tt_win, 15, 20, "%d) Add a TTY entry", NUM_TTY+1);
  34.     mvwprintw(tt_win, 16, 20, "%d) Delete a TTY entry", NUM_TTY+2);
  35.     horizontal(tt_win, 19, 0, 80);
  36.     mvwattrstr(tt_win, 20, 0, A_BOLD, "OPTION ==> ");
  37.     mvwaddstr(tt_win, 20, 58, "Press <ESC> to return");
  38.     wmove(tt_win, 20, 12);
  39.     touchwin(tt_win);
  40.     wrefresh(tt_win);
  41.                     /* get the option number */
  42.     ret_code = 0;
  43.     while ((i = get_num(tt_win, 2)) != -1) {
  44.                     /* if beyond t_entries, fake it */
  45.         if (i > modem->t_entries && i <= NUM_TTY)
  46.             i=999;
  47.                     /* change an entry  */
  48.         if (i >= 1 && i <= NUM_TTY) {
  49.             if (tty_prompt(tt_win, i-1))
  50.                 break;
  51.                     /* requires modem update? */
  52.             create_modem(modem->tname[i-1]);
  53.             del_modem();
  54.  
  55.             ret_code++;
  56.         }
  57.                     /* add a entry */
  58.         if (i == NUM_TTY+1) {
  59.             if (modem->t_entries == NUM_TTY) {
  60.                 sprintf(message, "'%s'", modem->m_path);
  61.                 error_win(0, "No empty TTY slots in modem/TTY database", message);
  62.                 continue;
  63.             }
  64.                     /* prompt for info */
  65.             j = modem->t_entries;
  66.             if (tty_prompt(tt_win, j))
  67.                 break;
  68.                     /* add modem entry? */
  69.             modem->t_entries++;
  70.             create_modem(modem->tname[j]);
  71.  
  72.             ret_code++;
  73.         }
  74.                     /* delete an entry */
  75.         if (i == NUM_TTY+2) {
  76.             mvwaddstr(tt_win, 21, 0, "Entry number to delete: ");
  77.             wrefresh(tt_win);
  78.             while ((num = get_num(tt_win, 4)) != -1) {
  79.                     /* valid range */
  80.                 if (!num || num>modem->t_entries) {
  81.                     beep();
  82.                     mvwaddstr(tt_win, 21, 24, "   ");
  83.                     wmove(tt_win, 21, 24);
  84.                     wrefresh(tt_win);
  85.                     continue;
  86.                 }
  87.                 del_tty(num-1);
  88.                 del_modem();
  89.  
  90.                     /* show the new list */
  91.                 disp_tty(tt_win);
  92.                 ret_code++;
  93.                 break;
  94.             }
  95.         }
  96.         if (i == 0 || i>NUM_TTY+2)
  97.             beep();
  98.         mvwaddstr(tt_win, 20, 12, "  ");
  99.         clear_line(tt_win, 21, 0, 0);
  100.         clear_line(tt_win, 22, 0, 0);
  101.         wmove(tt_win, 20, 12);
  102.         wrefresh(tt_win);
  103.     }
  104.     delwin(tt_win);
  105.     return(ret_code);
  106. }
  107.  
  108. /*
  109.  * Display the current TTY list.  No scrolling yet, so if your NUM_TTY is
  110.  * greater than ten, this routine will need some work.
  111.  */
  112.  
  113. static void
  114. disp_tty(win)
  115. WINDOW *win;
  116. {
  117.     int i;
  118.  
  119.     for (i=0; i<NUM_TTY; i++)
  120.         mvwprintw(win, i+4, 20, "%2d) %-14.14s %-14.14s  %d\n",
  121.          i+1, modem->tty[i], modem->tname[i], modem->init_sp[i]);
  122.     return;
  123. }
  124.  
  125. /*
  126.  * Prompt the user for the TTY database info.  A return code of 1 means a
  127.  * user abort.  The second argument is the zero based index.
  128.  */
  129.  
  130. static int
  131. tty_prompt(win, i)
  132. WINDOW *win;
  133. int i;
  134. {
  135.     char *ans, *temp_tty, *temp_tname, *str_prompt(), *menu_prompt();
  136.     void free_ptr();
  137.     static char *v_baud[8] = {"0", "300", "1200", "2400", "4800", "9600",
  138.      "19200", NULL};
  139.                     /* get temp TTY */
  140.     if ((ans = str_prompt(win, i+4, 24, "TTY name", "")) == NULL)
  141.         return(1);
  142.  
  143.     temp_tty = strdup(ans);
  144.     clear_line(win, 21, 0, 0);
  145.  
  146.                     /* get temp tname */
  147.     if ((ans = str_prompt(win, i+4, 39, "Modem name", "")) == NULL)
  148.         return(1);
  149.  
  150.     temp_tname = strdup(ans);
  151.     clear_line(win, 21, 0, 0);
  152.  
  153.                     /* get maximum baud */
  154.     if ((ans = menu_prompt(win, i+4, 55, "Init speed", v_baud)) == NULL)
  155.         return(1);
  156.  
  157.     wrefresh(win);
  158.                     /* store 'em for real */
  159.     free_ptr(modem->tty[i]);
  160.     free_ptr(modem->tname[i]);
  161.  
  162.     modem->tty[i] = strdup(temp_tty);
  163.     modem->tname[i] = strdup(temp_tname);
  164.     modem->init_sp[i] = atoi(ans);
  165.  
  166.     free_ptr(temp_tty);
  167.     free_ptr(temp_tname);
  168.     return(0);
  169. }
  170.  
  171. /*
  172.  * Delete a TTY entry.  Since the list must be contiguous, we collapse the
  173.  * list to cover the hole we made.
  174.  */
  175.  
  176. static void
  177. del_tty(i)
  178. int i;
  179. {
  180.     extern char *null_ptr;
  181.     int j;
  182.     char *strdup();
  183.     void free_ptr();
  184.                     /* collapse the list */
  185.     for (j=i; j<modem->t_entries-1; j++) {
  186.         free_ptr(modem->tty[j]);
  187.         free_ptr(modem->tname[j]);
  188.         modem->tty[j] = strdup(modem->tty[j+1]);
  189.         modem->tname[j] = strdup(modem->tname[j+1]);
  190.         modem->init_sp[j] = modem->init_sp[j+1];
  191.     }
  192.     j = modem->t_entries-1;
  193.                     /* zap the entry */
  194.     free_ptr(modem->tty[j]);
  195.     free_ptr(modem->tname[j]);
  196.     modem->tty[j] = null_ptr;
  197.     modem->tname[j] = null_ptr;
  198.     modem->init_sp[j] = 0;
  199.                     /* update the count */
  200.     modem->t_entries--;
  201.     if (modem->t_cur >= modem->t_entries)
  202.         modem->t_cur = -1;
  203.     return;
  204. }
  205.