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

  1. /*
  2.  * The revise option of the dialing directory.  A non-zero return code
  3.  * means 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. static char *ld_prompt();
  13.  
  14. int
  15. revise()
  16. {
  17.     WINDOW *r_win, *newwin();
  18.     int count, dir_flag, param_flag, num, x, y, save;
  19.     char ans, buf[40], *ld, *str_rep();
  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] = '\0';
  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 || ans == DEL) {
  77.             if (!count) {
  78.                 beep();
  79.                 continue;
  80.             }
  81.             count--;
  82.             buf[count] = '\0';
  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.                     param->ld_plus = str_rep(param->ld_plus, ld);
  101.                     param_flag++;
  102.                 }
  103.                 break;
  104.             case '-':
  105.                 if ((ld = ld_prompt(r_win, param->ld_minus, ans)) != NULL) {
  106.                     param->ld_minus = str_rep(param->ld_minus, ld);
  107.                     param_flag++;
  108.                 }
  109.                 break;
  110.             case '@':
  111.                 if ((ld = ld_prompt(r_win, param->ld_at, ans)) != NULL) {
  112.                     param->ld_at = str_rep(param->ld_at, ld);
  113.                     param_flag++;
  114.                 }
  115.                 break;
  116.             case '#':
  117.                 if ((ld = ld_prompt(r_win, param->ld_pound, ans)) != NULL) {
  118.                     param->ld_pound = str_rep(param->ld_pound, ld);
  119.                     param_flag++;
  120.                 }
  121.                 break;
  122.             default:
  123.                 beep();
  124.                 continue;
  125.         }
  126.         break;
  127.     }
  128.                     /* if nothing changed */
  129.     if (!param_flag && !dir_flag) {
  130.         delwin(r_win);
  131.         return(0);
  132.     }
  133.                     /* save to disk? */
  134.     clear_line(r_win, 4, 4, TRUE);
  135.     if (dir_flag) {
  136.         sprintf(buf, "Save entry %d to disk", num);
  137.         save = yes_prompt(r_win, 4, 4, A_BOLD, buf);
  138.     }
  139.     else
  140.         save = yes_prompt(r_win, 4, 4, A_BOLD, "Save to disk");
  141.  
  142.                     /* update the files */
  143.     if (save && dir_flag) {
  144.         if (up_dir(num)) {
  145.             touchwin(r_win);
  146.             wrefresh(r_win);
  147.         }
  148.     }
  149.     if (save && param_flag) {
  150.         if (up_param()) {
  151.             touchwin(r_win);
  152.             wrefresh(r_win);
  153.         }
  154.     }
  155.     delwin(r_win);
  156.     return(1);
  157. }
  158.  
  159. /*
  160.  * Prompt for long distance code changes.  If new string is a space,
  161.  * change it to null_ptr.  Returns NULL on escape.  Since it uses
  162.  * get_str(), the return value is a pointer to a static area.
  163.  */
  164.  
  165. static char *
  166. ld_prompt(win, current_ld, name)
  167. WINDOW *win;
  168. char *current_ld, name;
  169. {
  170.     extern char *null_ptr;
  171.     char *ans, *get_str();
  172.  
  173.     werase(win);
  174.     mvwprintw(win, 2, 4, "%-20.20s", current_ld);
  175.     mvwprintw(win, 4, 4, "New LD code for %c: ", name);
  176.     box(win, VERT, HORZ);
  177.     wrefresh(win);
  178.  
  179.     if ((ans = get_str(win, 20, "", "\n")) == NULL)
  180.         return(NULL);
  181.                     /* if space, change to null_ptr */
  182.     if (!strcmp(ans, " "))
  183.         ans = null_ptr;
  184.                     /* display new value */
  185.     clear_line(win, 2, 4, TRUE);
  186.     wattrstr(win, A_BOLD, ans);
  187.  
  188.     return(ans);
  189. }
  190.