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

  1. /*
  2.  * The revise option of the dialing directory.  A return code of 1 means
  3.  * that something was updated.  Prompts for saving changes to disk.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <curses.h>
  8. #include "config.h"
  9. #include "dial_dir.h"
  10. #include "misc.h"
  11. #include "param.h"
  12.  
  13. int
  14. revise()
  15. {
  16.     WINDOW *r_win, *newwin();
  17.     int count, dir_flag, param_flag, num, x, y, save;
  18.     char ans, buf[40], *ld, *ld_prompt(), *strdup();
  19.     void free_ptr();
  20.  
  21.     r_win = newwin(7, 77, 7, 2);
  22.  
  23.     mvwaddstr(r_win, 3, 6, "Entry to revise?");
  24.     mvwaddstr(r_win, 3, 35, "(Entry Number, +, -, @, #)");
  25.     box(r_win, VERT, HORZ);
  26.     wmove(r_win, 3, 23);
  27.     wrefresh(r_win);
  28.  
  29.     dir_flag = 0;
  30.     param_flag = 0;
  31.     count = 0;
  32.  
  33.     /*
  34.      * Can't use my home-grown get_str() and get_num() functions
  35.      * here, because we are prompting for an entry number or a
  36.      * long distance code.  This routine echoes numbers only.
  37.      */
  38.     while ((ans = wgetch(r_win)) != ESC) {
  39.         if (ans >= '0' && ans <= '9') {
  40.             if (count == 3) {
  41.                 beep();
  42.                 continue;
  43.             }
  44.             buf[count] = ans;
  45.             waddch(r_win, (chtype) ans);
  46.             wrefresh(r_win);
  47.             count++;
  48.             continue;
  49.         }
  50.                     /* terminating CR */
  51.         if (ans == '\r') {
  52.             if (!count) {
  53.                 beep();
  54.                 continue;
  55.             }
  56.             buf[count] = NULL;
  57.             num = atoi(buf);
  58.                     /* valid range of numbers? */
  59.             if (num == 0 || num > NUM_DIR) {
  60.                 beep();
  61.                 mvwaddstr(r_win, 3, 23, "   ");
  62.                 wmove(r_win, 3, 23);
  63.                 wrefresh(r_win);
  64.                 count = 0;
  65.                 continue;
  66.             }
  67.                     /* prompt for that entry */
  68.             if (prompt_lib(r_win, num)) {
  69.                 dir_flag++;
  70.                 break;
  71.             }
  72.             delwin(r_win);
  73.             return(0);
  74.         }
  75.                     /* do our own backspace */
  76.         if (ans == BS) {
  77.             if (!count) {
  78.                 beep();
  79.                 continue;
  80.             }
  81.             count--;
  82.             buf[count] = NULL;
  83.             getyx(r_win, y, x);
  84.             x--;
  85.             wmove(r_win, y, x);
  86.             waddch(r_win, (chtype) ' ');
  87.             wmove(r_win, y, x);
  88.             wrefresh(r_win);
  89.             continue;
  90.         }
  91.                     /* non-number after number is error */
  92.         if (count) {
  93.             beep();
  94.             continue;
  95.         }
  96.                     /* prompt for LD codes */
  97.         switch (ans) {
  98.             case '+':
  99.                 if ((ld = ld_prompt(r_win, param->ld_plus, ans)) != NULL) {
  100.                     free_ptr(param->ld_plus);
  101.                     param->ld_plus = strdup(ld);
  102.                     param_flag++;
  103.                 }
  104.                 break;
  105.             case '-':
  106.                 if ((ld = ld_prompt(r_win, param->ld_minus, ans)) != NULL) {
  107.                     free_ptr(param->ld_minus);
  108.                     param->ld_minus = strdup(ld);
  109.                     param_flag++;
  110.                 }
  111.                 break;
  112.             case '@':
  113.                 if ((ld = ld_prompt(r_win, param->ld_at, ans)) != NULL) {
  114.                     free_ptr(param->ld_at);
  115.                     param->ld_at = strdup(ld);
  116.                     param_flag++;
  117.                 }
  118.                 break;
  119.             case '#':
  120.                 if ((ld = ld_prompt(r_win, param->ld_pound, ans)) != NULL) {
  121.                     free_ptr(param->ld_pound);
  122.                     param->ld_pound = strdup(ld);
  123.                     param_flag++;
  124.                 }
  125.                 break;
  126.             default:
  127.                 beep();
  128.                 continue;
  129.         }
  130.         break;
  131.     }
  132.                     /* if nothing changed */
  133.     if (!param_flag && !dir_flag) {
  134.         delwin(r_win);
  135.         return(0);
  136.     }
  137.                     /* save to disk? */
  138.     clear_line(r_win, 4, 4, 1);
  139.     if (dir_flag) {
  140.         sprintf(buf, "Save entry %d to disk", num);
  141.         save = yes_prompt(r_win, 4, 4, A_BOLD, buf);
  142.     }
  143.     else
  144.         save = yes_prompt(r_win, 4, 4, A_BOLD, "Save to disk");
  145.  
  146.                     /* update the files */
  147.     if (save && dir_flag) {
  148.         if (up_dir(num)) {
  149.             touchwin(r_win);
  150.             wrefresh(r_win);
  151.         }
  152.     }
  153.     if (save && param_flag) {
  154.         if (up_param()) {
  155.             touchwin(r_win);
  156.             wrefresh(r_win);
  157.         }
  158.     }
  159.     delwin(r_win);
  160.     return(1);
  161. }
  162.  
  163. /*
  164.  * Prompt for long distance code changes.  If new string is a space,
  165.  * change it to a null pointer.  Returns the new value or NULL on escape.
  166.  */
  167.  
  168. static char *
  169. ld_prompt(win, current_ld, name)
  170. WINDOW *win;
  171. char *current_ld, name;
  172. {
  173.     extern char *null_ptr;
  174.     char *ans, *get_str();
  175.  
  176.     werase(win);
  177.     mvwprintw(win, 2, 4, "%-20.20s", current_ld);
  178.     mvwprintw(win, 4, 4, "New LD code for %c: ", name);
  179.     box(win, VERT, HORZ);
  180.     wrefresh(win);
  181.  
  182.     if ((ans = get_str(win, 20, "", "")) == NULL)
  183.         return(NULL);
  184.                     /* if space, change to NULL pointer */
  185.     if (!strcmp(ans, " "))
  186.         ans = null_ptr;
  187.                     /* display new value */
  188.     clear_line(win, 2, 4, 1);
  189.     wattrstr(win, A_BOLD, ans);
  190.  
  191.     return(ans);
  192. }
  193.