home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / pcomm2 / part05 / m_lib.c next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  9.8 KB  |  419 lines

  1. /*
  2.  * Routines to manipulate the pcomm.modem file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "modem.h"
  7.  
  8. /*
  9.  * Read the modem/TTY database file.  Returns a pointer to a static area
  10.  * containing the MODEM structure.  All modem entries and all TTY entries
  11.  * are created regardless of the number of physical entries in the file.
  12.  */
  13.  
  14. struct MODEM *
  15. read_modem(extra)
  16. char *extra;
  17. {
  18.     extern char *null_ptr;
  19.     FILE *fp, *my_fopen();
  20.     int i, tty, mod, line, oops, m_line, start, stop;
  21.     char *strdup(), buf[200], message[80], token[40], *str_tok(), *str;
  22.     char *temp_token, *t_sep, *m_sep, *m_letter, *findfile();
  23.     static struct MODEM m;
  24.     void error_win();
  25.  
  26.     if ((m.m_path = findfile(extra, "pcomm.modem")) == NULL)
  27.         error_win(1, "Support file 'pcomm.modem' is missing", "or no read permission");
  28.                     /* read permission checked */
  29.     fp = my_fopen(m.m_path, "r");
  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.init_sp[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.              * are distinguished by the letters a, b, and, c
  103.              * appended 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 = 12;
  113.                     break;
  114.                 case 2:
  115.                     start = 12;
  116.                     stop = 16;
  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.hang_up[mod] = strdup(str);
  140.                         break;
  141.                     case 5:
  142.                         m.auto_baud[mod] = *str;
  143.                         break;
  144.                     case 6:
  145.                         m.con_3[mod] = strdup(str);
  146.                         break;
  147.                     case 7:
  148.                         m.con_12[mod] = strdup(str);
  149.                         break;
  150.                     case 8:
  151.                         m.con_24[mod] = strdup(str);
  152.                         break;
  153.                     case 9:
  154.                         m.con_48[mod] = strdup(str);
  155.                         break;
  156.                     case 10:
  157.                         m.con_96[mod] = strdup(str);
  158.                         break;
  159.                     case 11:
  160.                         m.con_192[mod] = strdup(str);
  161.                         break;
  162.                     case 12:
  163.                         m.no_con1[mod] = strdup(str);
  164.                         break;
  165.                     case 13:
  166.                         m.no_con2[mod] = strdup(str);
  167.                         break;
  168.                     case 14:
  169.                         m.no_con3[mod] = strdup(str);
  170.                         break;
  171.                     case 15:
  172.                         m.no_con4[mod] = strdup(str);
  173.                         break;
  174.                 }
  175.             }
  176.             if (oops)
  177.                 break;
  178.             m_line++;
  179.             if (m_line >= 3) {
  180.                 m_line = 0;
  181.                 mod++;
  182.             }
  183.         }
  184.     }
  185.     fclose(fp);
  186.  
  187.     if (oops) {
  188.         sprintf(buf, "Modem/TTY database file '%s'", m.m_path);
  189.         error_win(1, buf, message);
  190.     }
  191.     m.t_entries = tty;
  192.     m.m_entries = mod;
  193.     m.t_cur = -1;
  194.     m.m_cur = -1;
  195.                     /* if empty database */
  196.     if (!tty) {
  197.         sprintf(buf, "Modem/TTY database file '%s'", m.m_path);
  198.         error_win(0, buf, "has no TTY data");
  199.     }
  200.     if (!mod) {
  201.         sprintf(buf, "Modem/TTY database file '%s'", m.m_path);
  202.         error_win(0, buf, "has no modem data");
  203.     }
  204.                     /* fill in the rest */
  205.     for (; tty<NUM_TTY; tty++) {
  206.         m.tty[tty] = null_ptr;
  207.         m.tname[tty] = null_ptr;
  208.         m.init_sp[tty] = 0;
  209.     }
  210.     for (; mod<NUM_MODEM; mod++) {
  211.         m.mname[mod] = null_ptr;
  212.         m.init[mod] = null_ptr;
  213.         m.dial[mod] = null_ptr;
  214.         m.suffix[mod] = null_ptr;
  215.         m.hang_up[mod] = null_ptr;
  216.  
  217.         m.auto_baud[mod] = 'Y';
  218.         m.con_3[mod] = null_ptr;
  219.         m.con_12[mod] = null_ptr;
  220.         m.con_24[mod] = null_ptr;
  221.         m.con_48[mod] = null_ptr;
  222.         m.con_96[mod] = null_ptr;
  223.         m.con_192[mod] = null_ptr;
  224.  
  225.         m.no_con1[mod] = null_ptr;
  226.         m.no_con2[mod] = null_ptr;
  227.         m.no_con3[mod] = null_ptr;
  228.         m.no_con4[mod] = null_ptr;
  229.     }
  230.     return(&m);
  231. }
  232.  
  233. /*
  234.  * Update the modem database.  Other routines actually do the changes
  235.  * or deletions in memory.  A return code of 1 means non-fatal error.
  236.  */
  237.  
  238. int
  239. up_modem()
  240. {
  241.     FILE *fp, *my_fopen();
  242.     char buf[80];
  243.     int i;
  244.     void error_win();
  245.  
  246.                     /* open for write */
  247.     if (!(fp = my_fopen(modem->m_path, "w"))) {
  248.         sprintf(buf, "'%s'", modem->m_path);
  249.         error_win(0, "No write permission on modem/TTY database file", buf);
  250.         return(1);
  251.     }
  252.                     /* put back the TTY entries */
  253.     for (i=0; i<modem->t_entries; i++)
  254.         fprintf(fp, "TTY_%d=%s;%s;%d\n", i+1, modem->tty[i],
  255.          modem->tname[i], modem->init_sp[i]);
  256.  
  257.                     /* put back the modem entries */
  258.     for (i=0; i<modem->m_entries; i++) {
  259.         fprintf(fp, "MODEM_%da=%s;%s;%s;%s;%s\n", i+1, modem->mname[i],
  260.          modem->init[i], modem->dial[i], modem->suffix[i],
  261.          modem->hang_up[i]);
  262.  
  263.         fprintf(fp, "MODEM_%db=%c;%s;%s;%s;%s;%s;%s\n", i+1,
  264.          modem->auto_baud[i], modem->con_3[i], modem->con_12[i],
  265.          modem->con_24[i], modem->con_48[i], modem->con_96[i],
  266.          modem->con_192[i]);
  267.  
  268.         fprintf(fp, "MODEM_%dc=%s;%s;%s;%s\n", i+1, modem->no_con1[i],
  269.          modem->no_con2[i], modem->no_con3[i], modem->no_con4[i]);
  270.     }
  271.  
  272.     fclose(fp);
  273.     return(0);
  274. }
  275.  
  276. /*
  277.  * See if the new modem is already in the database.  If it's not, create
  278.  * a slot for it and update the modem->m_cur variable.
  279.  */
  280.  
  281. void
  282. create_modem(str)
  283. char *str;
  284. {
  285.     int i;
  286.     char *strdup(), buf[80];
  287.     void error_win(), free_ptr();
  288.                     /* modem entry already exists? */
  289.     for (i=0; i<modem->m_entries; i++) {
  290.         if (!strcmp(str, modem->mname[i]))
  291.             return;
  292.     }
  293.                     /* empty slot available? */
  294.     if (modem->m_entries == NUM_MODEM) {
  295.         sprintf(buf, "'%s'", modem->m_path);
  296.         error_win(0, "No empty modem slots in", buf);
  297.         return;
  298.     }
  299.                     /* create a new entry */
  300.     free_ptr(modem->mname[modem->m_entries]);
  301.     modem->mname[modem->m_entries] = strdup(str);
  302.  
  303.                     /* update number of entries */
  304.     modem->m_entries++;
  305.     return;
  306. }
  307.  
  308. /*
  309.  * See if the modem names in the list still need to be in the database.
  310.  * If you find a "lost" entry, delete it and collapse the list.
  311.  */
  312.  
  313. void
  314. del_modem()
  315. {
  316.     extern char *null_ptr;
  317.     int i, j, match;
  318.     char *strdup();
  319.     void free_ptr();
  320.  
  321.     for (i=0; i<modem->m_entries; i++) {
  322.         match = 0;
  323.         for (j=0; j<modem->t_entries; j++) {
  324.             if (!strcmp(modem->mname[i], modem->tname[j])) {
  325.                 match++;
  326.                 break;
  327.             }
  328.         }
  329.                     /* found a "lost" modem name */
  330.         if (!match) {
  331.             for (j=i; j<modem->m_entries-1; j++) {
  332.                 free_ptr(modem->mname[j]);
  333.                 free_ptr(modem->init[j]);
  334.                 free_ptr(modem->dial[j]);
  335.                 free_ptr(modem->suffix[j]);
  336.                 free_ptr(modem->hang_up[j]);
  337.  
  338.                 free_ptr(modem->con_3[j]);
  339.                 free_ptr(modem->con_12[j]);
  340.                 free_ptr(modem->con_24[j]);
  341.                 free_ptr(modem->con_48[j]);
  342.                 free_ptr(modem->con_96[j]);
  343.                 free_ptr(modem->con_192[j]);
  344.  
  345.                 free_ptr(modem->no_con1[j]);
  346.                 free_ptr(modem->no_con2[j]);
  347.                 free_ptr(modem->no_con3[j]);
  348.                 free_ptr(modem->no_con4[j]);
  349.  
  350.                     /* copy the info */
  351.                 modem->mname[j] = strdup(modem->mname[j+1]);
  352.                 modem->init[j] = strdup(modem->init[j+1]);
  353.                 modem->dial[j] = strdup(modem->dial[j+1]);
  354.                 modem->suffix[j] = strdup(modem->suffix[j+1]);
  355.                 modem->hang_up[j] = strdup(modem->hang_up[j+1]);
  356.  
  357.                 modem->auto_baud[j] = modem->auto_baud[j+1];
  358.                 modem->con_3[j] = strdup(modem->con_3[j+1]);
  359.                 modem->con_12[j] = strdup(modem->con_12[j+1]);
  360.                 modem->con_24[j] = strdup(modem->con_24[j+1]);
  361.                 modem->con_48[j] = strdup(modem->con_48[j+1]);
  362.                 modem->con_96[j] = strdup(modem->con_96[j+1]);
  363.                 modem->con_192[j] = strdup(modem->con_192[j+1]);
  364.  
  365.                 modem->no_con1[j] = strdup(modem->no_con1[j+1]);
  366.                 modem->no_con2[j] = strdup(modem->no_con2[j+1]);
  367.                 modem->no_con3[j] = strdup(modem->no_con3[j+1]);
  368.                 modem->no_con4[j] = strdup(modem->no_con4[j+1]);
  369.             }
  370.             j = modem->m_entries -1;
  371.  
  372.             free_ptr(modem->mname[j]);
  373.             free_ptr(modem->init[j]);
  374.             free_ptr(modem->dial[j]);
  375.             free_ptr(modem->suffix[j]);
  376.             free_ptr(modem->hang_up[j]);
  377.  
  378.             free_ptr(modem->con_3[j]);
  379.             free_ptr(modem->con_12[j]);
  380.             free_ptr(modem->con_24[j]);
  381.             free_ptr(modem->con_48[j]);
  382.             free_ptr(modem->con_96[j]);
  383.             free_ptr(modem->con_192[j]);
  384.  
  385.             free_ptr(modem->no_con1[j]);
  386.             free_ptr(modem->no_con2[j]);
  387.             free_ptr(modem->no_con3[j]);
  388.             free_ptr(modem->no_con4[j]);
  389.  
  390.                     /* create an empty entry */
  391.             modem->mname[j] = null_ptr;
  392.             modem->init[j] = null_ptr;
  393.             modem->dial[j] = null_ptr;
  394.             modem->suffix[j] = null_ptr;
  395.             modem->hang_up[j] = null_ptr;
  396.  
  397.             modem->auto_baud[j] = 'Y';
  398.             modem->con_3[j] = null_ptr;
  399.             modem->con_12[j] = null_ptr;
  400.             modem->con_24[j] = null_ptr;
  401.             modem->con_48[j] = null_ptr;
  402.             modem->con_96[j] = null_ptr;
  403.             modem->con_192[j] = null_ptr;
  404.  
  405.             modem->no_con1[j] = null_ptr;
  406.             modem->no_con2[j] = null_ptr;
  407.             modem->no_con3[j] = null_ptr;
  408.             modem->no_con4[j] = null_ptr;
  409.  
  410.                     /* update the counts */
  411.             modem->m_entries--;
  412.             if (modem->m_cur >= modem->m_entries)
  413.                 modem->m_cur = -1;
  414.             return;
  415.         }
  416.     }
  417.     return;
  418. }
  419.