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

  1. /*
  2.  * Routines to manipulate the dialing directory file pcomm.dial_dir
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "dial_dir.h"
  7. #include "param.h"
  8.  
  9. /*
  10.  * Read the dialing directory.  Returns a pointer to a static area
  11.  * containing the DIAL_DIR structure.  All of the entries are created
  12.  * regardless of the number of physical entries in the file.  Element
  13.  * number zero is reserved for the "manual" entry.  All errors are fatal.
  14.  */
  15.  
  16. struct DIAL_DIR *
  17. read_dir()
  18. {
  19.     extern char *null_ptr;
  20.     FILE *fp, *uid_fopen();
  21.     int i, line, oops;
  22.     char *str_dup(), buf[200], *temp_token, *str, *str_tok(), token[20];
  23.     char message[80], *sep, *findfile();
  24.     static struct DIAL_DIR d;
  25.     void error_win();
  26.  
  27.     if ((d.d_path = findfile("pcomm.dial_dir")) == NULL)
  28.         error_win(1, "Support file \"pcomm.dial_dir\" is missing", "or no read permission");
  29.  
  30.     if (!(fp = uid_fopen(d.d_path, "r"))) {
  31.         sprintf(buf, "\"%s\" for read", d.d_path);
  32.         error_win(1, "Can't open dialing directory file", buf);
  33.     }
  34.  
  35.     sep = ";;---;;\n";
  36.     line = 0;
  37.     oops = 0;
  38.     while (fgets(buf, 200, fp) != NULL) {
  39.         line++;
  40.         if (line > NUM_DIR)
  41.             break;
  42.                     /* get the token */
  43.         if (!(temp_token = str_tok(buf, '='))) {
  44.             sprintf(message, "is missing a token at line %d", line);
  45.             oops++;
  46.             break;
  47.         }
  48.         /*
  49.          * Parse the rest of the line.  This is similar to using
  50.          * the "real" strtok() function, but this version returns
  51.          * a pointer to NULL if the token is missing.  Note the use
  52.          * of the array of field separators.
  53.          */
  54.         for (i=0; i<8; i++) {
  55.             if (!(str = str_tok((char *) NULL, sep[i]))) {
  56.                 sprintf(message, "is missing a parameter at line %d", line);
  57.                 oops++;
  58.                 break;
  59.             }
  60.             switch (i) {
  61.                 case 0:
  62.                     d.name[line] = str_dup(str);
  63.                     break;
  64.                 case 1:
  65.                     d.number[line] = str_dup(str);
  66.                     break;
  67.                 case 2:
  68.                     d.baud[line] = (unsigned int) atoi(str);
  69.                     break;
  70.                 case 3:
  71.                     d.parity[line] = *str;
  72.                     break;
  73.                 case 4:
  74.                     d.data_bits[line] = atoi(str);
  75.                     break;
  76.                 case 5:
  77.                     d.stop_bits[line] = atoi(str);
  78.                     break;
  79.                 case 6:
  80.                     d.duplex[line] = *str;
  81.                     break;
  82.                 case 7:
  83.                     d.aux[line] = str_dup(str);
  84.                     break;
  85.             }
  86.         }
  87.         if (oops)
  88.             break;
  89.                     /* sanity checking */
  90.         sprintf(token, "DIR_%d", line);
  91.         if (strcmp(temp_token, token)) {
  92.             sprintf(message, "is corrupted at line %d", line);
  93.             oops++;
  94.             break;
  95.         }
  96.     }
  97.     fclose(fp);
  98.  
  99.     if (oops) {
  100.         sprintf(buf, "Dialing directory file \"%s\"", d.d_path);
  101.         error_win(1, buf, message);
  102.     }
  103.     d.d_entries = line;
  104.                     /* if empty database */
  105.     if (!line) {
  106.         sprintf(buf, "Dialing directory file \"%s\"", d.d_path);
  107.         error_win(0, buf, "has no data");
  108.     }
  109.                     /* fill in the rest with defaults */
  110.     for (i=line+1; i<=NUM_DIR; i++) {
  111.         d.name[i] = null_ptr;
  112.         d.number[i] = null_ptr;
  113.         d.baud[i] = param->d_baud;
  114.         d.parity[i] = param->d_parity;
  115.         d.data_bits[i] = param->d_data_bits;
  116.         d.stop_bits[i] = param->d_stop_bits;
  117.         d.duplex[i] = *param->d_duplex;
  118.         d.aux[i] = null_ptr;
  119.     }
  120.                     /* create an empty "manual" entry */
  121.     d.name[0] = null_ptr;
  122.     d.number[0] = null_ptr;
  123.     d.baud[0] = param->d_baud;
  124.     d.parity[0] = param->d_parity;
  125.     d.data_bits[0] = param->d_data_bits;
  126.     d.stop_bits[0] = param->d_stop_bits;
  127.     d.duplex[0] = *param->d_duplex;
  128.     d.aux[0] = null_ptr;
  129.                     /* create an empty queue */
  130.     for (i=0; i<NUM_QUEUE; i++) {
  131.         d.q_ld[i] = '\0';
  132.         d.q_num[i] = -1;
  133.     }
  134.                     /* the start up d_cur is 0 */
  135.     d.d_cur = 0;
  136.     return(&d);
  137. }
  138.  
  139. /*
  140.  * Update a dialing directory entry.  Update only the one entry asked for,
  141.  * not the entire image in memory.  If the new entry is beyond the end of
  142.  * the physical file, then fill in the holes, and update "dir->d_entries".
  143.  * A non-zero return code means a non-fatal error.
  144.  */
  145.  
  146. int
  147. up_dir(entry)
  148. int entry;
  149. {
  150.     FILE *fp_in, *fp_out, *uid_fopen();
  151.     int i;
  152.     char *temp[NUM_DIR+1], buf[200], *str_dup(), *str_rep();
  153.     void error_win(), free_ptr();
  154.  
  155.                     /* open for read */
  156.     if (!(fp_in = uid_fopen(dir->d_path, "r"))) {
  157.         sprintf(buf, "\"%s\" for read", dir->d_path);
  158.         error_win(1, "Can't open dialing directory file", buf);
  159.     }
  160.                     /* read in a temporary version */
  161.     i = 0;
  162.     while (fgets(buf, 200, fp_in) != NULL)
  163.         temp[++i] = str_dup(buf);
  164.  
  165.     fclose(fp_in);
  166.                     /* alter only 1 entry */
  167.     sprintf(buf, "DIR_%d=%s;%s;%d-%c-%d-%d;%c;%s\n", entry,
  168.      dir->name[entry], dir->number[entry], dir->baud[entry],
  169.      dir->parity[entry], dir->data_bits[entry], dir->stop_bits[entry],
  170.      dir->duplex[entry], dir->aux[entry]);
  171.  
  172.     if (entry <= dir->d_entries)
  173.         temp[entry] = str_rep(temp[entry], buf);
  174.     else
  175.         temp[entry] = str_dup(buf);
  176.  
  177.                     /* fill in holes if beyond end */
  178.     if (entry > dir->d_entries+1) {
  179.         for (i=dir->d_entries+1; i<entry; i++) {
  180.             sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i,
  181.              param->d_baud, param->d_parity, param->d_data_bits,
  182.              param->d_stop_bits, *param->d_duplex);
  183.             temp[i] = str_dup(buf);
  184.         }
  185.     }
  186.                     /* update "dir->d_entries" */
  187.     if (entry > dir->d_entries)
  188.         dir->d_entries = entry;
  189.  
  190.                     /* open for write */
  191.     if (!(fp_out = uid_fopen(dir->d_path, "w"))) {
  192.         for (i=1; i<=dir->d_entries; i++)
  193.             free_ptr(temp[i]);
  194.         sprintf(buf, "\"%s\"", dir->d_path);
  195.         error_win(0, "No write permission on dialing directory file", buf);
  196.         return(1);
  197.     }
  198.                     /* put it back */
  199.     for (i=1; i<=dir->d_entries; i++) {
  200.         fputs(temp[i], fp_out);
  201.         free_ptr(temp[i]);
  202.     }
  203.  
  204.     fclose(fp_out);
  205.     return(0);
  206. }
  207.  
  208. /*
  209.  * Delete a range of dialing directory entries.  Actually, just copies
  210.  * default (empty) entries in place of deleted entries.  However, it will
  211.  * shrink the file if deletions occur at the physical EOF.  A non-zero
  212.  * return code means a non-fatal error.
  213.  */
  214.  
  215. int
  216. del_dir(first, last)
  217. int first, last;
  218. {
  219.     FILE *fp_in, *fp_out, *uid_fopen();
  220.     int i;
  221.     char *temp[NUM_DIR+1], buf[200], *str_dup(), *str_rep();
  222.     void error_win(), free_ptr();
  223.                     /* sanity checking */
  224.     if (first > dir->d_entries)
  225.         return(0);
  226.     if (last > dir->d_entries)
  227.         last = dir->d_entries;
  228.  
  229.                     /* open for read */
  230.     if (!(fp_in = uid_fopen(dir->d_path, "r"))) {
  231.         sprintf(buf, "\"%s\" for read", dir->d_path);
  232.         error_win(1, "Can't open dialing directory file", buf);
  233.     }
  234.                     /* read in a temporary version */
  235.     i = 0;
  236.     while (fgets(buf, 200, fp_in) != NULL)
  237.         temp[++i] = str_dup(buf);
  238.  
  239.     fclose(fp_in);
  240.                     /* delete the range of values */
  241.     for (i=first; i<=last; i++) {
  242.         sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i, param->d_baud,
  243.          param->d_parity, param->d_data_bits, param->d_stop_bits,
  244.          *param->d_duplex);
  245.         temp[i] = str_rep(temp[i], buf);
  246.     }
  247.                     /* shrink the file? */
  248.     if (last >= dir->d_entries) {
  249.         for (i=first; i<=last; i++)
  250.             free_ptr(temp[i]);
  251.         dir->d_entries = first-1;
  252.     }
  253.                     /* open for write */
  254.     if (!(fp_out = uid_fopen(dir->d_path, "w"))) {
  255.         for (i=1; i<=dir->d_entries; i++)
  256.             free_ptr(temp[i]);
  257.         sprintf(buf, "\"%s\"", dir->d_path);
  258.         error_win(0, "No write permission on dialing directory file", buf);
  259.         return(1);
  260.     }
  261.                     /* put it all back */
  262.     for (i=1; i<=dir->d_entries; i++) {
  263.         fputs(temp[i], fp_out);
  264.         free_ptr(temp[i]);
  265.     }
  266.  
  267.     fclose(fp_out);
  268.     return(0);
  269. }
  270.