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

  1. /*
  2.  * Routines to manipulate the pcomm.extrnl file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "extrnl.h"
  7.  
  8. /*
  9.  * Read the external file transfer program database.  Returns a pointer
  10.  * to a static area containing the EXTRNL structure.  This support file is 
  11.  * optional.
  12.  */
  13.  
  14. struct EXTRNL *
  15. read_extrnl()
  16. {
  17.     extern char *null_ptr;
  18.     FILE *fp, *uid_fopen();
  19.     int i, line, up, entry, oops;
  20.     char *str_dup(), buf[200], message[80], token[40], *str_tok(), *str;
  21.     char *sep, *temp_token, *findfile();
  22.     static struct EXTRNL e;
  23.     void error_win();
  24.  
  25.     if ((e.e_path = findfile("pcomm.extrnl")) == NULL) {
  26.                     /* not required to exist */
  27.         for (i=0; i<3; i++) {
  28.             e.name[0][i] = null_ptr;
  29.             e.command[0][i] = null_ptr;
  30.             e.prompt[0][i] = 'N';
  31.             e.name[1][i] = null_ptr;
  32.             e.command[1][i] = null_ptr;
  33.             e.prompt[1][i] = 'N';
  34.         }
  35.         e.up_entries = 0;
  36.         e.dn_entries = 0;
  37.  
  38.         return(&e);
  39.     }
  40.  
  41.     if (!(fp = uid_fopen(e.e_path, "r"))) {
  42.         sprintf(buf, "\"%s\" for read", e.e_path);
  43.         error_win(1, "Can't open external program file", buf);
  44.     }
  45.  
  46.     sep = ";;\n";
  47.     line = 0;
  48.     up = 1;
  49.     oops = 0;
  50.     while (fgets(buf, 200, fp) != NULL) {
  51.         line++;
  52.         if (line <= 3)
  53.             entry = line-1;
  54.         else {
  55.             up = 0;
  56.             entry = line-4;
  57.         }
  58.                     /* get the token */
  59.         if (!(temp_token = str_tok(buf, '='))) {
  60.             sprintf(message, "is missing a token at line %d", line);
  61.             oops++;
  62.             break;
  63.         }
  64.         /*
  65.          * Parse the rest of the line.  This is similar to using
  66.          * the "real" strtok() function, but this version returns
  67.          * a pointer to NULL if the token is missing.  Note the
  68.          * use of the array of separators.
  69.          */
  70.         for (i=0; i<3; i++) {
  71.             if (!(str = str_tok((char *) NULL, sep[i]))) {
  72.                 sprintf(message, "is missing a parameter at line %d", line);
  73.                 oops++;
  74.                 break;
  75.             }
  76.             switch(i) {
  77.                 case 0:
  78.                     e.name[up][entry] = str_dup(str);
  79.                     break;
  80.                 case 1:
  81.                     e.command[up][entry] = str_dup(str);
  82.                     break;
  83.                 case 2:
  84.                     e.prompt[up][entry] = *str;
  85.                     break;
  86.             }
  87.         }
  88.         if (oops)
  89.             break;
  90.  
  91.                     /* sanity checking */
  92.         if (up)
  93.             sprintf(token, "SEND_%d", entry+1);
  94.         else
  95.             sprintf(token, "RCV_%d", entry+1);
  96.  
  97.         if (strcmp(temp_token, token)) {
  98.             sprintf(message, "is corrupted at line %d", line);
  99.             oops++;
  100.             break;
  101.         }
  102.     }
  103.     fclose(fp);
  104.  
  105.     if (oops) {
  106.         sprintf(buf, "External program file \"%s\"", e.e_path);
  107.         error_win(1, buf, message);
  108.     }
  109.                     /* find number of upload entries */
  110.     for (i=0; i<3; i++) {
  111.         if (e.name[1][i] == null_ptr)
  112.             break;
  113.     }
  114.     e.up_entries = i;
  115.                     /* find number of download entries */
  116.     for (i=0; i<3; i++) {
  117.         if (e.name[0][i] == null_ptr)
  118.             break;
  119.     }
  120.     e.dn_entries = i;
  121.                     /* if empty database */
  122.     if (!e.up_entries || !e.dn_entries) {
  123.         sprintf(buf, "External program file \"%s\"", e.e_path);
  124.         error_win(0, buf, "has no data");
  125.     }
  126.  
  127.     return(&e);
  128. }
  129.  
  130. /*
  131.  * Update the external file transfer program database.  A non-zero return
  132.  * code means a non-fatal error.
  133.  */
  134.  
  135. int
  136. up_extrnl()
  137. {
  138.     FILE *fp, *uid_fopen();
  139.     int i, up, entry;
  140.     char buf[200];
  141.     void error_win();
  142.  
  143.     /* 
  144.      * I don't remember why I made this file optional.  For the next
  145.      * release, it will be mandatory!  The following is kludge to tell
  146.      * the user that there is no file to save anything to.
  147.      */
  148.     if (extrnl->e_path == NULL) {
  149.         error_win(0, "No \"pcomm.extrnl\" file in use", "");
  150.         return(1);
  151.     }
  152.                     /* open for write */
  153.     if (!(fp = uid_fopen(extrnl->e_path, "w"))) {
  154.         sprintf(buf, "\"%s\"", extrnl->e_path);
  155.         error_win(0, "No write permission on external program file", buf);
  156.         return(1);
  157.     }
  158.                     /* put 'em back */
  159.     up = 1;
  160.     for (i=0; i<6; i++) {
  161.         if (i < 3)
  162.             entry = i;
  163.         else {
  164.             up = 0;
  165.             entry = i-3;
  166.         }
  167.         if (up)
  168.             fprintf(fp, "SEND_%d=%s;%s;%c\n", entry+1, extrnl->name[up][entry], extrnl->command[up][entry], extrnl->prompt[up][entry]);
  169.         else
  170.             fprintf(fp, "RCV_%d=%s;%s;%c\n", entry+1, extrnl->name[up][entry], extrnl->command[up][entry], extrnl->prompt[up][entry]);
  171.     }
  172.  
  173.     fclose(fp);
  174.     return(0);
  175. }
  176.