home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part02 / s_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  4.8 KB  |  214 lines

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