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