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