home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part05 / s_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  4.7 KB  |  202 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 "misc.h"
  9. #include "modem.h"
  10. #include "status.h"
  11.  
  12. int
  13. tty_setup()
  14. {
  15.     WINDOW *tt_win, *newwin();
  16.     char *strdup(), buf[80];
  17.     int num, i, j, ret_code;
  18.     void disp_tty(), create_modem(), delete_modem(), error_win();
  19.     void delete_tty();
  20.     extern char *null_ptr;
  21.  
  22.     tt_win = newwin(23, 80, 0, 0);
  23.  
  24.     waddstr(tt_win, "----------------------------------- ");
  25.     wattrstr(tt_win, A_BOLD, "TTY Setup");
  26.     waddstr(tt_win, " ----------------------------------");
  27.     mvwaddstr(tt_win, 2, 22, "TTY name");
  28.     mvwaddstr(tt_win, 2, 37, "Modem name");
  29.     mvwaddstr(tt_win, 2, 51, "Maximum baud");
  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.     mvwprintw(tt_win, 19, 0, "--------------------------------------------------------------------------------");
  36.     mvwattrstr(tt_win, 20, 0, A_BOLD, "OPTION ==> ");
  37.     mvwaddstr(tt_win, 20, 60, "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.             delete_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(buf, "'%s'", status->m_path);
  61.                 error_win(0, "No empty TTY slots in", buf);
  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.                     wrefresh(tt_win);
  84.                     continue;
  85.                 }
  86.                 delete_tty(num-1);
  87.                 delete_modem();
  88.  
  89.                     /* show the new list */
  90.                 disp_tty(tt_win);
  91.                 ret_code = 1;
  92.             }
  93.  
  94.         }
  95.         if (i == 0 || i>NUM_TTY+2)
  96.             beep();
  97.         mvwaddstr(tt_win, 20, 12, "  ");
  98.         clear_line(tt_win, 21, 0, 0);
  99.         clear_line(tt_win, 22, 0, 0);
  100.         wmove(tt_win, 20, 12);
  101.         wrefresh(tt_win);
  102.     }
  103.     delwin(tt_win);
  104.     return(ret_code);
  105. }
  106.  
  107. /*
  108.  * Display the current tty list.  No scrolling yet, so if your NUM_TTY is
  109.  * greater than ten, this routine will need some work.
  110.  */
  111.  
  112. void
  113. disp_tty(win)
  114. WINDOW *win;
  115. {
  116.     int i;
  117.  
  118.     for (i=0; i<NUM_TTY; i++)
  119.         mvwprintw(win, i+4, 20, "%2d) %-14.14s %-14.14s  %d\n",
  120.          i+1, modem->tty[i], modem->tname[i], modem->mbaud[i]);
  121.     return;
  122. }
  123.  
  124. /*
  125.  * Prompt the user for the TTY database info.  A return code of 1 means a
  126.  * user abort.  The second argument is the zero based index.
  127.  */
  128.  
  129. tty_prompt(win, i)
  130. WINDOW *win;
  131. int i;
  132. {
  133.     char *ans, *temp_tty, *temp_tname, *str_prompt(), *menu_prompt();
  134.     extern char *v_baud[];
  135.     void free_ptr();
  136.                     /* get temp tty */
  137.     if ((ans = str_prompt(win, i+4, 24, "TTY name", NULL)) == NULL)
  138.         return(1);
  139.  
  140.     temp_tty = strdup(ans);
  141.     clear_line(win, 21, 0, 0);
  142.  
  143.                     /* get temp tname */
  144.     if ((ans = str_prompt(win, i+4, 39, "Modem name", NULL)) == NULL)
  145.         return(1);
  146.  
  147.     temp_tname = strdup(ans);
  148.     clear_line(win, 21, 0, 0);
  149.     
  150.                     /* get maximum baud */
  151.     if ((ans = menu_prompt(win, i+4, 55, "Maximum baud", v_baud)) == NULL)
  152.         return(1);
  153.  
  154.     wrefresh(win);
  155.                     /* store 'em for real */
  156.     free_ptr(modem->tty[i]);
  157.     free_ptr(modem->tname[i]);
  158.  
  159.     modem->tty[i] = strdup(temp_tty);
  160.     modem->tname[i] = strdup(temp_tname);
  161.     modem->mbaud[i] = atoi(ans);
  162.  
  163.     free_ptr(temp_tty);
  164.     free_ptr(temp_tname);
  165.     return(0);
  166. }
  167.  
  168. /*
  169.  * Delete a tty entry.  Since the list must be contiguous, we collapse the
  170.  * list to cover the hole we made.
  171.  */
  172.  
  173. void
  174. delete_tty(i)
  175. int i;
  176. {
  177.     int j;
  178.     char *strdup();
  179.     void free_ptr();
  180.     extern char *null_ptr;
  181.                     /* collapse the list */
  182.     for (j=i; j<modem->t_entries-1; j++) {
  183.         free_ptr(modem->tty[j]);
  184.         free_ptr(modem->tname[j]);
  185.         modem->tty[j] = strdup(modem->tty[j+1]);
  186.         modem->tname[j] = strdup(modem->tname[j+1]);
  187.         modem->mbaud[j] = modem->mbaud[j+1];
  188.     }
  189.     j = modem->t_entries-1;
  190.                     /* zap the entry */
  191.     free_ptr(modem->tty[j]);
  192.     free_ptr(modem->tname[j]);
  193.     modem->tty[j] = null_ptr;
  194.     modem->tname[j] = null_ptr;
  195.     modem->mbaud[j] = 0;
  196.                     /* update the count */
  197.     modem->t_entries--;
  198.     if (modem->t_cur >= modem->t_entries)
  199.         modem->t_cur = -1;
  200.     return;
  201. }
  202.