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

  1. /*
  2.  * Routines to manipulate the pcomm.modem file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "modem.h"
  7. #include "status.h"
  8.  
  9. /*
  10.  * Read the modem database file.  Returns a pointer to a static area
  11.  * containing the MODEM structure.  All modem entries and all tty entries
  12.  * are created reguardless of the number of physical entries in the file.
  13.  */
  14.  
  15. struct MODEM *
  16. read_modem()
  17. {
  18.     FILE *fp;
  19.     int i, tty, mod, line, oops, m_line, start, stop;
  20.     char *strdup(), buf[200], message[80], token[40], *str_tok(), *str;
  21.     char *temp_token, *t_sep, *m_sep, *m_letter;
  22.     static struct MODEM m;
  23.     void error_win();
  24.     extern char *null_ptr;
  25.  
  26.     if (!(fp = fopen(status->m_path, "r"))) {
  27.         sprintf(buf, "'%s' for read", status->m_path);
  28.         error_win(1, "Can't open modem file", buf);
  29.     }
  30.  
  31.     t_sep = ";;\n";
  32.     m_sep = ";;;;\n;;;;;\n;;;\n";
  33.     m_letter = "abc";
  34.     oops = 0;
  35.     tty = 0;
  36.     mod = 0;
  37.     line = 0;
  38.     m_line = 0;
  39.     while (fgets(buf, 200, fp) != NULL) {
  40.         line++;
  41.         if (tty >= NUM_TTY || mod >= NUM_MODEM)
  42.             break;
  43.                     /* get the token */
  44.         if (!(temp_token = str_tok(buf, '='))) {
  45.             sprintf(message, "is missing a token at line %d", line);
  46.             oops++;
  47.             break;
  48.         }
  49.         if (*temp_token != 'T' && *temp_token != 'M') {
  50.             sprintf(message, "is corrupted at line %d", line);
  51.             oops++;
  52.             break;
  53.         }
  54.                     /* the tty database */
  55.         if (*temp_token == 'T') {
  56.             /*
  57.              * This is similar to the "real" strtok() command
  58.              * but this one returns a null pointer on a missing
  59.              * attribute.  Note the use of the field separator
  60.              * array.
  61.              */
  62.             for (i=0; i<3; i++) {
  63.                 if (!(str = str_tok((char *) NULL, t_sep[i]))) {
  64.                     sprintf(message, "is missing a parameter at line %d", line);
  65.                     oops++;
  66.                     break;
  67.                 }
  68.                 switch(i) {    
  69.                     case 0:
  70.                         m.tty[tty] = strdup(str);
  71.                         break;
  72.                     case 1:
  73.                         m.tname[tty] = strdup(str);
  74.                         break;
  75.                     case 2:
  76.                         m.mbaud[tty] = atoi(str);
  77.                         break;
  78.                 }
  79.             }
  80.             if (oops)
  81.                 break;
  82.                     /* sanity checking */
  83.             sprintf(token, "TTY_%d", tty+1);
  84.             if (strcmp(token, temp_token)) {
  85.                 sprintf(message, "is corrupted at line %d", line);
  86.                 oops++;
  87.                 break;
  88.             }
  89.             tty++;
  90.             continue;
  91.         }
  92.                     /* the modem database */
  93.         else {
  94.             sprintf(token, "MODEM_%d%c", mod+1, m_letter[m_line]);
  95.             if (strcmp(token, temp_token)) {
  96.                 sprintf(message, "is corrupted at line %d", line);
  97.                 oops++;
  98.                 break;
  99.             }
  100.             /*
  101.              * There are three lines to the modem database.  They
  102.              * distinguished by the letters a, b, and, c appended
  103.              * to the entry number.
  104.              */
  105.             switch(m_line) {
  106.                 case 0:
  107.                     start = 0;
  108.                     stop = 5;
  109.                     break;
  110.                 case 1:
  111.                     start = 5;
  112.                     stop = 11;
  113.                     break;
  114.                 case 2:
  115.                     start = 11;
  116.                     stop = 15;
  117.                     break;
  118.             }
  119.             for (i=start; i<stop; i++) {
  120.                 if (!(str = str_tok((char *) NULL, m_sep[i]))) {
  121.                     sprintf(message, "is missing a parameter at line %d", line);
  122.                     oops++;
  123.                     break;
  124.                 }
  125.                 switch(i) {
  126.                     case 0:
  127.                         m.mname[mod] = strdup(str);
  128.                         break;
  129.                     case 1:
  130.                         m.init[mod] = strdup(str);
  131.                         break;
  132.                     case 2:
  133.                         m.dial[mod] = strdup(str);
  134.                         break;
  135.                     case 3:
  136.                         m.suffix[mod] = strdup(str);
  137.                         break;
  138.                     case 4:
  139.                         m.hangup[mod] = strdup(str);
  140.                         break;
  141.                     case 5:
  142.                         m.con_3[mod] = strdup(str);
  143.                         break;
  144.                     case 6:
  145.                         m.con_12[mod] = strdup(str);
  146.                         break;
  147.                     case 7:
  148.                         m.con_24[mod] = strdup(str);
  149.                         break;
  150.                     case 8:
  151.                         m.con_48[mod] = strdup(str);
  152.                         break;
  153.                     case 9:
  154.                         m.con_96[mod] = strdup(str);
  155.                         break;
  156.                     case 10:
  157.                         m.con_192[mod] = strdup(str);
  158.                         break;
  159.                     case 11:
  160.                         m.no_con1[mod] = strdup(str);
  161.                         break;
  162.                     case 12:
  163.                         m.no_con2[mod] = strdup(str);
  164.                         break;
  165.                     case 13:
  166.                         m.no_con3[mod] = strdup(str);
  167.                         break;
  168.                     case 14:
  169.                         m.no_con4[mod] = strdup(str);
  170.                         break;
  171.                 }
  172.             }
  173.             if (oops)
  174.                 break;
  175.             m_line++;
  176.             if (m_line >= 3) {
  177.                 m_line = 0;
  178.                 mod++;
  179.             }
  180.         }
  181.     }
  182.     fclose(fp);
  183.  
  184.     if (oops) {
  185.         sprintf(buf, "Modem file '%s'", status->m_path);
  186.         error_win(1, buf, message);
  187.     }
  188.     m.t_entries = tty;
  189.     m.m_entries = mod;
  190.     m.t_cur = -1;
  191.     m.m_cur = -1;
  192.                     /* fill in the rest */
  193.     for (; tty<NUM_TTY; tty++) {
  194.         m.tty[tty] = null_ptr;
  195.         m.tname[tty] = null_ptr;
  196.         m.mbaud[tty] = 0;
  197.     }
  198.     for (; mod<NUM_MODEM; mod++) {
  199.         m.mname[mod] = null_ptr;
  200.         m.init[mod] = null_ptr;
  201.         m.dial[mod] = null_ptr;
  202.         m.suffix[mod] = null_ptr;
  203.         m.hangup[mod] = null_ptr;
  204.  
  205.         m.con_3[mod] = null_ptr;
  206.         m.con_12[mod] = null_ptr;
  207.         m.con_24[mod] = null_ptr;
  208.         m.con_48[mod] = null_ptr;
  209.         m.con_96[mod] = null_ptr;
  210.         m.con_192[mod] = null_ptr;
  211.  
  212.         m.no_con1[mod] = null_ptr;
  213.         m.no_con2[mod] = null_ptr;
  214.         m.no_con3[mod] = null_ptr;
  215.         m.no_con4[mod] = null_ptr;
  216.     }
  217.     return(&m);
  218. }
  219.  
  220. /*
  221.  * Update the modem database.  Other routines actually do the changes
  222.  * or deletions in memory.  A return code of 1 means non-fatal error.
  223.  */
  224.  
  225. int
  226. update_modem()
  227. {
  228.     FILE *fp;
  229.     char buf[80];
  230.     int i;
  231.     void error_win();
  232.  
  233.                     /* open for write */
  234.     if (!(fp = fopen(status->m_path, "w"))) {
  235.         sprintf(buf, "'%s'", status->m_path);
  236.         error_win(0, "No write permission on modem file", buf);
  237.         return(1);
  238.     }
  239.                     /* put back the tty entries */
  240.     for (i=0; i<modem->t_entries; i++)
  241.         fprintf(fp, "TTY_%d=%s;%s;%d\n", i+1, modem->tty[i],
  242.          modem->tname[i], modem->mbaud[i]);
  243.  
  244.                     /* put back the modem entries */
  245.     for (i=0; i<modem->m_entries; i++) {
  246.         fprintf(fp, "MODEM_%da=%s;%s;%s;%s;%s\n", i+1, modem->mname[i],
  247.          modem->init[i], modem->dial[i], modem->suffix[i],
  248.          modem->hangup[i]);
  249.  
  250.         fprintf(fp, "MODEM_%db=%s;%s;%s;%s;%s;%s\n", i+1,
  251.          modem->con_3[i], modem->con_12[i], modem->con_24[i],
  252.          modem->con_48[i], modem->con_96[i], modem->con_192[i]);
  253.  
  254.         fprintf(fp, "MODEM_%dc=%s;%s;%s;%s\n", i+1, modem->no_con1[i],
  255.          modem->no_con2[i], modem->no_con3[i], modem->no_con4[i]);
  256.     }
  257.  
  258.     fclose(fp);
  259.     return(0);
  260. }
  261.  
  262. /*
  263.  * See if the new modem is already in the database.  If it's not,
  264.  * then create a slot for it and update the modem->m_cur variable.
  265.  */
  266.  
  267. void
  268. create_modem(str)
  269. char *str;
  270. {
  271.     int i;
  272.     char *strdup(), buf[80];
  273.     void error_win(), free_ptr();
  274.                     /* modem entry already exists? */
  275.     for (i=0; i<modem->m_entries; i++) {
  276.         if (!strcmp(str, modem->mname[i]))
  277.             return;
  278.     }
  279.                     /* empty slot available? */
  280.     if (modem->m_entries == NUM_MODEM) {
  281.         sprintf(buf, "'%s'", status->m_path);
  282.         error_win(0, "No empty modem slots in", buf);
  283.         return;
  284.     }
  285.                     /* create a new entry */
  286.     free_ptr(modem->mname[modem->m_entries]);
  287.     modem->mname[modem->m_entries] = strdup(str);
  288.  
  289.                     /* update number of entries */
  290.     modem->m_entries++;
  291.     return;
  292. }
  293.  
  294. /*
  295.  * See if the modem names in the list still need to be in the database.
  296.  * If you find a "lost" entry, delete it and collapse the list.
  297.  */
  298.  
  299. void
  300. delete_modem()
  301. {
  302.     int i, j, match;
  303.     char *strdup();
  304.     void free_ptr();
  305.     extern char *null_ptr;
  306.  
  307.     for (i=0; i<modem->m_entries; i++) {
  308.         match = 0;
  309.         for (j=0; j<modem->t_entries; j++) {
  310.             if (!strcmp(modem->mname[i], modem->tname[j])) {
  311.                 match = 1;
  312.                 break;
  313.             }
  314.         }
  315.                     /* found a "lost" modem name */
  316.         if (!match) {
  317.             for (j=i; j<modem->m_entries-1; j++) {
  318.                 free_ptr(modem->mname[j]);
  319.                 free_ptr(modem->init[j]);
  320.                 free_ptr(modem->dial[j]);
  321.                 free_ptr(modem->suffix[j]);
  322.                 free_ptr(modem->hangup[j]);
  323.  
  324.                 free_ptr(modem->con_3[j]);
  325.                 free_ptr(modem->con_12[j]);
  326.                 free_ptr(modem->con_24[j]);
  327.                 free_ptr(modem->con_48[j]);
  328.                 free_ptr(modem->con_96[j]);
  329.                 free_ptr(modem->con_192[j]);
  330.  
  331.                 free_ptr(modem->no_con1[j]);
  332.                 free_ptr(modem->no_con2[j]);
  333.                 free_ptr(modem->no_con3[j]);
  334.                 free_ptr(modem->no_con4[j]);
  335.  
  336.                     /* copy the info */
  337.                 modem->mname[j] = strdup(modem->mname[j+1]);
  338.                 modem->init[j] = strdup(modem->init[j+1]);
  339.                 modem->dial[j] = strdup(modem->dial[j+1]);
  340.                 modem->suffix[j] = strdup(modem->suffix[j+1]);
  341.                 modem->hangup[j] = strdup(modem->hangup[j+1]);
  342.  
  343.                 modem->con_3[j] = strdup(modem->con_3[j+1]);
  344.                 modem->con_12[j] = strdup(modem->con_12[j+1]);
  345.                 modem->con_24[j] = strdup(modem->con_24[j+1]);
  346.                 modem->con_48[j] = strdup(modem->con_48[j+1]);
  347.                 modem->con_96[j] = strdup(modem->con_96[j+1]);
  348.                 modem->con_192[j] = strdup(modem->con_192[j+1]);
  349.  
  350.                 modem->no_con1[j] = strdup(modem->no_con1[j+1]);
  351.                 modem->no_con2[j] = strdup(modem->no_con2[j+1]);
  352.                 modem->no_con3[j] = strdup(modem->no_con3[j+1]);
  353.                 modem->no_con4[j] = strdup(modem->no_con4[j+1]);
  354.             }
  355.             j = modem->m_entries -1;
  356.  
  357.             free_ptr(modem->mname[j]);
  358.             free_ptr(modem->init[j]);
  359.             free_ptr(modem->dial[j]);
  360.             free_ptr(modem->suffix[j]);
  361.             free_ptr(modem->hangup[j]);
  362.  
  363.             free_ptr(modem->con_3[j]);
  364.             free_ptr(modem->con_12[j]);
  365.             free_ptr(modem->con_24[j]);
  366.             free_ptr(modem->con_48[j]);
  367.             free_ptr(modem->con_96[j]);
  368.             free_ptr(modem->con_192[j]);
  369.  
  370.             free_ptr(modem->no_con1[j]);
  371.             free_ptr(modem->no_con2[j]);
  372.             free_ptr(modem->no_con3[j]);
  373.             free_ptr(modem->no_con4[j]);
  374.  
  375.                     /* create an empty entry */
  376.             modem->mname[j] = null_ptr;
  377.             modem->init[j] = null_ptr;
  378.             modem->dial[j] = null_ptr;
  379.             modem->suffix[j] = null_ptr;
  380.             modem->hangup[j] = null_ptr;
  381.  
  382.             modem->con_3[j] = null_ptr;
  383.             modem->con_12[j] = null_ptr;
  384.             modem->con_24[j] = null_ptr;
  385.             modem->con_48[j] = null_ptr;
  386.             modem->con_96[j] = null_ptr;
  387.             modem->con_192[j] = null_ptr;
  388.  
  389.             modem->no_con1[j] = null_ptr;
  390.             modem->no_con2[j] = null_ptr;
  391.             modem->no_con3[j] = null_ptr;
  392.             modem->no_con4[j] = null_ptr;
  393.  
  394.                     /* update the counts */
  395.             modem->m_entries--;
  396.             if (modem->m_cur >= modem->m_entries)
  397.                 modem->m_cur = -1;
  398.             return;
  399.         }
  400.     }
  401.     return;
  402. }
  403.