home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part04 / p_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  5.7 KB  |  234 lines

  1. /*
  2.  * Routines to manipulate the pcomm.param file.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "param.h"
  7. #include "status.h"
  8.  
  9. /*
  10.  * Read the parameter structure from the pcomm.param file.  Returns a
  11.  * pointer to the PARAM structure.  All errors are fatal.
  12.  */
  13.  
  14. struct PARAM *
  15. read_param()
  16. {
  17.     FILE *fp;
  18.     int i, oops;
  19.     char buf[80], *temp_token, *str, *strdup();
  20.     char message[80], *str_tok();
  21.     static char *token[NUM_PARAM] = {"D_BAUD", "D_PARITY", "D_DBITS",
  22.     "D_SBITS", "HOT", "ASCII_HOT", "D_DUPLEX", "FLOW", "CR_IN", "CR_OUT",
  23.     "LOGFILE", "DUMPFILE", "STRIP", "PAUSE_CHAR", "CR_CHAR", "CTRL_CHAR",
  24.     "ESC_CHAR", "ABORT", "CDELAY", "PAUSE", "LECHO", "EXPAND", "CR_DELAY",
  25.     "PACE", "CR_UP", "LF_UP", "TIMER", "CR_DN", "LF_DN", "LD_PLUS",
  26.     "LD_MINUS", "LD_AT", "LD_POUND"};
  27.     static struct PARAM p;
  28.     void error_win();
  29.                     /* read permission already checked */
  30.     fp = fopen(status->p_path, "r");
  31.  
  32.     oops = 0;
  33.     for (i=0; i<NUM_PARAM; i++) {
  34.         if (fgets(buf, 80, fp) == NULL) {
  35.             sprintf(message, "is truncated at line %d", i+1);
  36.             oops++;
  37.             break;
  38.         }
  39.                     /* parse the input line */
  40.         if (!(temp_token = str_tok(buf, '='))) {
  41.             sprintf(message, "is missing a token at line %d", i+1);
  42.             oops++;
  43.             break;
  44.         }
  45.         if (!(str = str_tok((char *) NULL, '\n'))) {
  46.             sprintf(message, "is missing a parameter at line %d", i+1);
  47.             oops++;
  48.             break;
  49.         }
  50.                     /* sanity checking */
  51.         if (strcmp(temp_token, token[i])) {
  52.             sprintf(message, "is corrupted at line %d", i+1);
  53.             oops++;
  54.             break;
  55.         }
  56.  
  57.         switch(i) {
  58.                     /* used in line_set_menu() */
  59.             case LINE_SET:
  60.                 p.d_baud = atoi(str);
  61.                 break;
  62.             case LINE_SET+1:
  63.                 p.d_parity = *str;
  64.                 break;
  65.             case LINE_SET+2:
  66.                 p.d_dbits = atoi(str);
  67.                 break;
  68.             case LINE_SET+3:
  69.                 p.d_sbits = atoi(str);
  70.                 break;
  71.  
  72.                     /* used in term_setup() */
  73.             case TERM_SETUP:
  74.                 p.hot = atoi(str);
  75.                 break;
  76.             case TERM_SETUP+1:
  77.                 p.ascii_hot = strdup(str);
  78.                 break;
  79.             case TERM_SETUP+2:
  80.                 p.d_duplex = strdup(str);
  81.                 break;
  82.             case TERM_SETUP+3:
  83.                 p.flow = strdup(str);
  84.                 break;
  85.             case TERM_SETUP+4:
  86.                 p.cr_in = strdup(str);
  87.                 break;
  88.             case TERM_SETUP+5:
  89.                 p.cr_out = strdup(str);
  90.                 break;
  91.  
  92.                     /* used in gen_setup() */
  93.             case GEN_SETUP:
  94.                 p.logfile = strdup(str);
  95.                 break;
  96.             case GEN_SETUP+1:
  97.                 p.dumpfile = strdup(str);
  98.                 break;
  99.             case GEN_SETUP+2:
  100.                 p.strip = strdup(str);
  101.                 break;
  102.             case GEN_SETUP+3:
  103.                 p.pause_char = *str;
  104.                 break;
  105.             case GEN_SETUP+4:
  106.                 p.cr_char = *str;
  107.                 break;
  108.             case GEN_SETUP+5:
  109.                 p.ctrl_char = *str;
  110.                 break;
  111.             case GEN_SETUP+6:
  112.                 p.esc_char = *str;
  113.                 break;
  114.             case GEN_SETUP+7:
  115.                 p.abort = strdup(str);
  116.                 break;
  117.  
  118.                     /* used in gen_setup() delay_times() */
  119.             case DELAY_TIMES:
  120.                 p.cdelay = atoi(str);
  121.                 break;
  122.             case DELAY_TIMES+1:
  123.                 p.pause = atoi(str);
  124.                 break;
  125.  
  126.                     /* used in ascii_xfer_setup() */
  127.             case ASCII_SETUP:
  128.                 p.lecho = strdup(str);
  129.                 break;
  130.             case ASCII_SETUP+1:
  131.                 p.expand = strdup(str);
  132.                 break;
  133.             case ASCII_SETUP+2:
  134.                 p.cr_delay = atoi(str);
  135.                 break;
  136.             case ASCII_SETUP+3:
  137.                 p.pace = strdup(str);
  138.                 break;
  139.             case ASCII_SETUP+4:
  140.                 p.cr_up = strdup(str);
  141.                 break;
  142.             case ASCII_SETUP+5:
  143.                 p.lf_up = strdup(str);
  144.                 break;
  145.             case ASCII_SETUP+6:
  146.                 p.timer = atoi(str);
  147.                 break;
  148.             case ASCII_SETUP+7:
  149.                 p.cr_dn = strdup(str);
  150.                 break;
  151.             case ASCII_SETUP+8:
  152.                 p.lf_dn = strdup(str);
  153.                 break;
  154.  
  155.                     /* used in d_revise() */
  156.             case LD_CODES:
  157.                 p.ld_plus = strdup(str);
  158.                 break;
  159.             case LD_CODES+1:
  160.                 p.ld_minus = strdup(str);
  161.                 break;
  162.             case LD_CODES+2:
  163.                 p.ld_at = strdup(str);
  164.                 break;
  165.             case LD_CODES+3:
  166.                 p.ld_pound = strdup(str);
  167.                 break;
  168.         }
  169.     }
  170.     fclose(fp);
  171.     if (oops) {
  172.         sprintf(buf, "Parameter file '%s'", status->p_path);
  173.         error_win(1, buf, message);
  174.     }
  175.     return(&p);
  176. }
  177.  
  178. /*
  179.  * Write the updated param structure to disk.  The values in memory should
  180.  * have already been "purified".  Later, we'll update only the entries that
  181.  * have been explicitly asked for.  A return code of 1 means non-fatal error.
  182.  */
  183.  
  184. int
  185. update_param()
  186. {
  187.     FILE *fp;
  188.     char buf[80];
  189.     void error_win();
  190.                     /* open for write */
  191.     if (!(fp = fopen(status->p_path, "w"))) {
  192.         sprintf(buf, "'%s'", status->p_path);
  193.         error_win(0, "No write permission on parameter file", buf);
  194.         return(1);
  195.     }
  196.  
  197.     fprintf(fp, "D_BAUD=%d\n", param->d_baud);
  198.     fprintf(fp, "D_PARITY=%c\n", param->d_parity);
  199.     fprintf(fp, "D_DBITS=%d\n", param->d_dbits);
  200.     fprintf(fp, "D_SBITS=%d\n", param->d_sbits);
  201.     fprintf(fp, "HOT=%d\n", param->hot);
  202.     fprintf(fp, "ASCII_HOT=%s\n", param->ascii_hot);
  203.     fprintf(fp, "D_DUPLEX=%s\n", param->d_duplex);
  204.     fprintf(fp, "FLOW=%s\n", param->flow);
  205.     fprintf(fp, "CR_IN=%s\n", param->cr_in);
  206.     fprintf(fp, "CR_OUT=%s\n", param->cr_out);
  207.     fprintf(fp, "LOGFILE=%s\n", param->logfile);
  208.     fprintf(fp, "DUMPFILE=%s\n", param->dumpfile);
  209.     fprintf(fp, "STRIP=%s\n", param->strip);
  210.     fprintf(fp, "PAUSE_CHAR=%c\n", param->pause_char);
  211.     fprintf(fp, "CR_CHAR=%c\n", param->cr_char);
  212.     fprintf(fp, "CTRL_CHAR=%c\n", param->ctrl_char);
  213.     fprintf(fp, "ESC_CHAR=%c\n", param->esc_char);
  214.     fprintf(fp, "ABORT=%s\n", param->abort);
  215.     fprintf(fp, "CDELAY=%d\n", param->cdelay);
  216.     fprintf(fp, "PAUSE=%d\n", param->pause);
  217.     fprintf(fp, "LECHO=%s\n", param->lecho);
  218.     fprintf(fp, "EXPAND=%s\n", param->expand);
  219.     fprintf(fp, "CR_DELAY=%d\n", param->cr_delay);
  220.     fprintf(fp, "PACE=%s\n", param->pace);
  221.     fprintf(fp, "CR_UP=%s\n", param->cr_up);
  222.     fprintf(fp, "LF_UP=%s\n", param->lf_up);
  223.     fprintf(fp, "TIMER=%d\n", param->timer);
  224.     fprintf(fp, "CR_DN=%s\n", param->cr_dn);
  225.     fprintf(fp, "LF_DN=%s\n", param->lf_dn);
  226.     fprintf(fp, "LD_PLUS=%s\n", param->ld_plus);
  227.     fprintf(fp, "LD_MINUS=%s\n", param->ld_minus);
  228.     fprintf(fp, "LD_AT=%s\n", param->ld_at);
  229.     fprintf(fp, "LD_POUND=%s\n", param->ld_pound);
  230.  
  231.     fclose(fp);
  232.     return(0);
  233. }
  234.