home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / DSSource / fconfig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  6.5 KB  |  299 lines  |  [TEXT/MPS ]

  1. /*
  2. *  Sconf is set up with an array of character string pointers representing
  3. *  keywords which may be found in the input.
  4. *  Each keyword is assigned a state.  When that keyword is found, everything
  5. *  following that keyword up to the next separator is lumped into one string
  6. *  and then processed in the case statement in Sconfile.  Each element of
  7. *  the case is free to do what it needs to do depending on the keyword's
  8. *  parameters.
  9. *
  10. *  For example, in NCSA DataScope, the "vimage" keyword looks like this:
  11. *  vimage=101,400,300
  12. *  When case KVIMAGE is hit, the parameter string contains:  "101,400,300"
  13. *  and sscanf is used to get the values.
  14. *
  15. */
  16.  
  17. #include "df.h"
  18.  
  19. char *index();
  20. #define strchr(A,B) index(A,B)
  21.  
  22. static unsigned char
  23.             *DSspace;
  24.  
  25. static int
  26.         lineno,                    /* line number in hosts file */
  27.         position,                /* position for scanning string */
  28.         constate,                /* state for config file parser */
  29.         inquote;                /* flag, inside quotes now */
  30.  
  31. /*
  32. *  information about dataset which we are reading.
  33. *  When told to read and process the info field, these fields are set to 
  34. *  indicate what the results were.
  35. */
  36. struct {
  37.     int
  38.         sref,                    /* return flag for sdg */
  39.         bref,                    /* ref #'s for each of these */
  40.         iref,
  41.         pref,
  42.         bxx,bxy,                /* expansion factors for block image */
  43.         ixx,ixy,                /* interpolated image factors */
  44.         pxx,                    /* expansion for polar image */
  45.         ashift,
  46.         dleft,
  47.         dtop,                    /* viewport for polar image */
  48.         dright,
  49.         dbottom,
  50.         dxsize,                    /* sizes for interp image windows */
  51.         dysize;
  52.         
  53. } dsinfo;
  54.  
  55.  
  56. /*
  57. *   States for config file reading state machine.
  58. *   One for each type of keyword and some for controlling.
  59. */
  60.  
  61. #define    KSDG    101
  62. #define KBLOCK    102
  63. #define KINTERP    103
  64. #define KPOLAR    104
  65. #define KNOTES     105
  66. #define KPVIEW  106
  67. #define KVIMAGE 107
  68.  
  69. static char *Skeyw[] = {
  70.         "",    
  71.         "sdg",                            /* ? */
  72.         "block",                        /* block image */
  73.         "interp",                        /* interpolated image */
  74.         "polar",                        /* polar image */
  75.         "notes",                        /* notebook data  ==5== */
  76.         "pview",                        /* viewport for polar */
  77.         "vimage",                        /* image size parameters */
  78.         ""
  79.     };
  80.  
  81.  
  82.  
  83. /************************************************************************/
  84. /*  Sconf
  85. *   read in the hosts file into our in-memory data structure.
  86. */
  87. #define MAXSTUFF 512
  88. #define EOF -1
  89.  
  90. Sconf(cp,touse)
  91.     char *cp;
  92.     int touse;                /*  number of characters which are left to process */
  93.     {
  94.     int c,
  95.         retval;
  96.  
  97.     *(cp+touse) = EOF;        /* set end of input */
  98.  
  99. /*
  100. *  Set the default values for the info structure.  They will be
  101. *  changed if there are settings in the file.
  102. */
  103.     dsinfo.sref = -1;
  104.     dsinfo.bref = -1;
  105.     dsinfo.iref = -1;
  106.     dsinfo.pref = -1;
  107.     dsinfo.dleft = 1;
  108.     dsinfo.dright = -1;                    /* illegal settings, so we know if changed */
  109.     dsinfo.dxsize = -1;
  110.     dsinfo.dysize = -1;
  111.  
  112.     DSspace = (char *) NewPtr(256);                /* get room for gathering stuff */
  113.     if (DSspace == NULL) {
  114.         return(-1);
  115.     }
  116.     position = constate = inquote = lineno = 0;   /* state vars */    
  117.  
  118.     retval = 0;
  119.     while (!retval) {
  120.         c = *cp++;
  121.         if (c == '#' && !inquote) {
  122.             while (c != EOF && c != '\n' && c != '\r')        /* skip to EOL */
  123.                 c = *cp++;
  124.         }
  125.         if (c == '\n' || c == '\r')
  126.             lineno++;
  127.         retval = DScontoken(c);        /* add character to token */
  128.     }
  129.  
  130.     DisposPtr(DSspace);
  131.  
  132.     if (retval == EOF)                /* EOF is normal end */
  133.         return(0);
  134.     else
  135.         return(retval);
  136.  
  137. }
  138.  
  139.  
  140. /************************************************************************/
  141. /*  ncstrcmp
  142. *   No case string compare.
  143. *   Only returns 0=match, 1=no match, does not compare greater or less
  144. *   There is a tiny bit of overlap with the | 32 trick, but shouldn't be
  145. *   a problem.  It causes some different symbols to match.
  146. */
  147. ncstrcmp(sa,sb)
  148.     char *sa,*sb;
  149.     {
  150.  
  151.     while (*sa && *sa < 33)        /* don't compare leading spaces */
  152.         sa++;
  153.     while (*sb && *sb < 33)
  154.         sb++;
  155.  
  156.     while (*sa && *sb) {
  157.         if ((*sa != *sb) && ((*sa | 32) != (*sb | 32)))
  158.             return(1);
  159.         sa++;sb++;
  160.     }
  161.     if (!*sa && !*sb)        /* if both at end of string */
  162.         return(0);
  163.     else
  164.         return(1);
  165. }
  166.  
  167. /************************************************************************/
  168. /* Scontoken
  169. *  tokenize the strings which get passed to Sconfile.
  170. *  Handles quotes and uses separators:  <33, ;:=
  171. */
  172. DScontoken(c)
  173.     int c;
  174.     {
  175.     int retval;
  176.  
  177.     if (c == EOF) {
  178.         DSspace[position++] = '\0';
  179.         Sconfile(DSspace);
  180.         return(-1);
  181.     }
  182.  
  183.     if (!position && DSissep(c))        /* skip over junk before token */
  184.         return(0);
  185.  
  186.     if (inquote || !DSissep(c)) {
  187.  
  188.         if (position > 200) {
  189.             return(1);
  190.         }
  191. /*
  192. *  check for quotes, a little mixed up here, could be reorganized
  193. */
  194.         if (c == '"' ) {
  195.             if (!inquote) {                /* beginning of quotes */
  196.                 inquote = 1;
  197.                 return(0);
  198.             }
  199.             else
  200.                 inquote = 0;            /* turn off flag and drop through */
  201.  
  202.         }
  203.         else {                        
  204.             if (c == '\n') {            /* check for EOL inside quotes */
  205.                 return(1);
  206.             }
  207.             DSspace[position++] = c;    /* include in current string */
  208.             return(0);
  209.         }
  210.                 
  211.     }
  212.  
  213.     DSspace[position++] = '\0';
  214.  
  215.     retval = DSconfile(DSspace);            /* pass the token along */
  216.  
  217.     position = 0;
  218.     inquote = 0;
  219.     DSspace[0] = '\0';
  220.  
  221.     return(retval);
  222. }
  223.  
  224. /************************************************************************/
  225. /*  Sconfile
  226. *   take the characters read from the file and parse them for keywords
  227. *   which require configuration action.
  228. */
  229. DSconfile(s)
  230.     char *s;
  231.     {
  232.     int i,laststate;
  233.     
  234.     laststate = constate;
  235.     constate = 0;
  236.     
  237.     switch (laststate) {
  238.         case 0:                                /* lookup keyword */
  239.             if (!(*s))                        /* empty token */
  240.                 return(0);
  241.  
  242.  
  243.             for (i=1; *Skeyw[i] && ncstrcmp(Skeyw[i],s); i++)        /* search list */
  244.                     ;
  245.             if (!(*Skeyw[i])) {            /* not in list */
  246.                 return(0);                /* don't die - helps backward compatibility */
  247.             }
  248.             constate = 100+i;            /* change to state for keyword */
  249.             break;
  250.  
  251.  
  252.         case KSDG:                        /* ref for SDG */
  253.             sscanf(DSspace,"%d",&dsinfo.sref);
  254.             break;
  255.             
  256.         case KBLOCK:
  257.             sscanf(DSspace,"%d,%d,%d",&dsinfo.bref,&dsinfo.bxx,&dsinfo.bxy);
  258.             break;
  259.             
  260.         case KINTERP:
  261.             sscanf(DSspace,"%d,%d,%d",&dsinfo.iref,&dsinfo.ixx,&dsinfo.ixy);
  262.             break;
  263.             
  264.         case KPOLAR:
  265.             sscanf(DSspace,"%d,%d",&dsinfo.pref,&dsinfo.pxx);
  266.             break;
  267.             
  268.         case KPVIEW:
  269.             sscanf(DSspace,"%d,%d,%d,%d,%d",
  270.                 &dsinfo.ashift,&dsinfo.dleft,&dsinfo.dtop,&dsinfo.dright,&dsinfo.dbottom);
  271.             break;
  272.             
  273.         case KVIMAGE:
  274.             sscanf(DSspace,"%d,%d,%d",&dsinfo.iref,&dsinfo.dxsize,&dsinfo.dysize);
  275.             break;
  276.             
  277.         case KNOTES:
  278.             sscanf(DSspace,"%d",&i);
  279.             break;
  280.  
  281.         default:
  282.             constate = 0;
  283.             break;
  284.     }
  285.  
  286.  
  287.     return(0);
  288. }
  289.  
  290. /*************************************************************************/
  291. DSissep(c)
  292.     char c;
  293.     {
  294.     if (c == ' ' || c == '=' || c == '\t' || c == '\n' || c == '\r')
  295.         return(1);
  296.         
  297.     return(0);
  298.     
  299. }