home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / cfdmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-17  |  9.6 KB  |  269 lines

  1. /*============================================================================*
  2.  * main() module: cfdmp.c - Configuration file dump program.
  3.  *
  4.  * (C)Copyright IBM Corporation, 1990, 1991.                     Brian E. Yoder
  5.  *
  6.  * Test program for the configuration file access subroutines in the
  7.  * 'cfaccess.c' source module.
  8.  *
  9.  * The programmer can use the source code in this test program for examples
  10.  * of how to use these subroutines.
  11.  *
  12.  * 03/21/90 - Created.
  13.  * 03/22/90 - Initial version.
  14.  * 05/25/90 - Added test for new cfline() subroutine in cfaccess.c.
  15.  * 03/25/91 - Added test for new cfsetfile() subroutine in cfaccess.c.
  16.  * 04/03/91 - Ported from AIX to DOS C/2.
  17.  * 11/17/91 - Processes "configuration" files, not "customization" files,
  18.  *            in keeping with the enhancement to the cfaccess.c module.
  19.  *============================================================================*/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24.  
  25. #include "util.h"
  26.  
  27. #define MINARGS  1     /* Minimum no. of (non-flag) command line arguments required */
  28.  
  29. #define NO       0
  30. #define YES      1
  31.  
  32. /*============================================================================*
  33.  * Define internal functions to allow forward access
  34.  *============================================================================*/
  35.  
  36. static void syntax();
  37.  
  38. /*============================================================================*
  39.  * Main Program Entry Point
  40.  *============================================================================*/
  41.  
  42. main(argc, argv)
  43.  
  44. int argc;           /* arg count */
  45. char *argv[];       /* arg pointers */
  46.  
  47. {
  48.   int numargs;            /* No. of arguments processed, not incl. -flags */
  49.  
  50.   int  rc;                /* Return code store area */
  51.   char cf;                /* Current flag character (if any) */
  52.  
  53.   char *cfname;           /* Pointer to file name */
  54.   int  f_flag;            /* -f (only list 'file' entries) flag */
  55.  
  56.   int  t_flag;            /* -t:tagname flag */
  57.   char *tagname;          /* Pointer to tagname (if -t) */
  58.  
  59.   int  i_flag;            /* -i flag */
  60.  
  61.   int     tokc;           /* Number of tokens found in a line */
  62.   char  **tokv;           /* Pointer to array of token pointers */
  63.  
  64.   char   *name;           /* Pointer to a file name */
  65.  
  66. /*============================================================================*
  67.  * Set initial value of some variables:
  68.  *============================================================================*/
  69.  
  70.   numargs = 0;            /* Initialize number of arguments processed = none */
  71.   f_flag = NO;            /* Initialize -f = no */
  72.   t_flag = NO;            /* Initialize -t = no */
  73.   i_flag = NO;            /* Initialize -i = no */
  74.  
  75. /*============================================================================*
  76.  * Process each command line argument:
  77.  *============================================================================*/
  78.  
  79.   argc--;                 /* Ignore 1st argument (program name) */
  80.   argv++;
  81.  
  82.   while (argc > 0)        /* For each command line argument: */
  83.   {
  84.     /*========================================================================*/
  85.     if ((*argv)[0] == '-')    /*   If argument is a -flag: */
  86.     {
  87.        cf = (*argv)[1];       /* Set cf = flag character: */
  88.        switch (cf)
  89.        {
  90.            case 'f':          /* -f */
  91.                 f_flag = YES;
  92.                 break;
  93.  
  94.            case 'i':          /* -i */
  95.                 i_flag = YES;
  96.                 break;
  97.  
  98.            case 't':          /* -t:tagname */
  99.                 if ((*argv)[2] != ':')           /* Be sure colon is there */
  100.                 {
  101.                    fprintf(stderr, "Missing colon on '-%c:'\n", cf);
  102.                    return(1);
  103.                 }
  104.                 tagname = (*argv)+3;             /* Point to char after flag */
  105.                 t_flag = YES;
  106.                 break;
  107.  
  108.            default:
  109.                 fprintf(stderr, "Unrecognized flag: '-%c'\n",
  110.                    cf);
  111.                 return(1);
  112.                 break; }
  113.     }   /* end if: flag processing */
  114.  
  115.     /*========================================================================*/
  116.  
  117.     else {          /* Argument is NOT a flag: */
  118.        /*
  119.         * The value 'numargs' indicates how many (non-flag) arguments have
  120.         * been processed, and hence, what to do with the current (non-flag)
  121.         * argument:
  122.         * When argument[numargs] (0=first, 1=second, etc.) is processed,
  123.         * be sure to increment 'numargs' so that successive arguments will
  124.         * be processed.
  125.        */
  126.        switch (numargs)
  127.        {
  128.            case 0:                   /* Name of input file */
  129.                 cfname = *argv;
  130.                 numargs++;
  131.                 break;
  132.  
  133.            default:
  134.                 fprintf(stderr, "Extra argument: '%s'\n",
  135.                    *argv);
  136.                 return(1);
  137.                 break; }
  138.     }   /* end else: argument processing */
  139.  
  140.     argc--;                     /* Decrement command line argument counter */
  141.     argv++;                     /* Point to next argument */
  142.  
  143.   }   /* end of command line argument processing loop */
  144.  
  145.   if (numargs < MINARGS) syntax(); /* If not enough arguments: Display syntax */
  146.                                    /* Otherwise, continue */
  147.  
  148. /*============================================================================*
  149.  * Get value for specified tag, if -t
  150.  *============================================================================*/
  151.  
  152.   if (t_flag == YES)
  153.   {
  154.      tokc = cfgetbyname(cfname, tagname, &tokv, &name);
  155.  
  156.      printf("Return from cfgetbyname() = %d\n", tokc);
  157.  
  158.      if (name == NULL)
  159.         name = "** none **";
  160.  
  161.      printf("Tag = '%s':  Value = '%s'\n",
  162.         tagname,
  163.         name);
  164.  
  165.      return(0);
  166.   }
  167.  
  168. /*============================================================================*
  169.  * Dump the configuration file
  170.  *============================================================================*/
  171.  
  172.   if (f_flag == NO)
  173.   {
  174.      /*-----------------------------------------------------------------------*
  175.       * Dump all tag entries
  176.       *-----------------------------------------------------------------------*/
  177.  
  178.       if (i_flag == NO)            /* If -i is not specified: */
  179.       {
  180.          rc = cfopen(cfname);           /* Open the configuration file */
  181.          if (rc != 0)
  182.          {
  183.             fprintf(stderr, "Cannot open configuration file: %s\n",
  184.                cfname);
  185.             return(1);
  186.          }
  187.       }
  188.       else                         /* Otherwise: */
  189.          cfsetfile(stdin);              /* Process standard input */
  190.  
  191.       for (;;)                     /* For each entry in the file: */
  192.       {
  193.          tokc = cfread(&tokv);          /* Read the entry */
  194.  
  195.          printf("Line %lu: Number of tokens = %d%s\n",
  196.             cfline(),
  197.             tokc,
  198.             cfisapp() ? "  (.INI application)" : "" );
  199.  
  200.          while (*tokv != NULL)          /* Display each token */
  201.          {
  202.             printf("   '%s'\n", *tokv);
  203.             tokv++;
  204.          }
  205.  
  206.          if (tokc == 0)                 /* If no tokens: */
  207.             break;                      /*    We're done: break out of loop */
  208.       }
  209.  
  210.       cfclose();                   /* Close the configuration file */
  211.   }
  212.   else
  213.   {
  214.      /*-----------------------------------------------------------------------*
  215.       * Dump only 'file' entries
  216.       *-----------------------------------------------------------------------*/
  217.  
  218.       if (i_flag == NO)            /* If -i is not specified: */
  219.       {
  220.          rc = cfopen(cfname);           /* Open the configuration file */
  221.          if (rc != 0)
  222.          {
  223.             fprintf(stderr, "Cannot open configuration file: %s\n",
  224.                cfname);
  225.             return(1);
  226.          }
  227.       }
  228.       else                         /* Otherwise: */
  229.          cfsetfile(stdin);              /* Process standard input */
  230.  
  231.       for (;;)                     /* For each entry in the file: */
  232.       {
  233.          name = cfreadfile();           /* Read a file entry */
  234.  
  235.          if (name == NULL)              /* If at end of file: */
  236.             break;                      /*    We're done: break out of loop */
  237.  
  238.          printf("File name: '%s'\n", name);
  239.       }
  240.  
  241.       cfclose();                   /* Close the configuration file */
  242.   }
  243.  
  244.   return(0);                    /* Done with main(): Return */
  245.  
  246. } /* end of main() */
  247.  
  248. /*============================================================================*
  249.  * syntax() - Display command syntax and exit to operating system!
  250.  *============================================================================*/
  251. static void syntax()
  252. {
  253.   fprintf(stderr, "Usage: cfdmp [-f | -t:tagname] [-i] filename\n");
  254.   fprintf(stderr, "\n");
  255.   fprintf(stderr, "This program dumps the significant lines (non-blank, non-\n");
  256.   fprintf(stderr, "comment) in the specified configuration 'filename'.  If '-i'\n");
  257.   fprintf(stderr, "is specified instead of a filename, then cfdmp processes the\n");
  258.   fprintf(stderr, "lines from stdin, and 'filename' is ignored.\n");
  259.   fprintf(stderr, "\n");
  260.   fprintf(stderr, "If -f is specified, then only the \"file\" entries in the\n");
  261.   fprintf(stderr, "configuration file are dumped.\n");
  262.   fprintf(stderr, "\n");
  263.   fprintf(stderr, "If -t is specified, then the tokens in the first line of the\n");
  264.   fprintf(stderr, "configuration file whose first token is 'tagname' are dumped.\n");
  265.   fprintf(stderr, "Note: If -t is specified, the -i is ignored.\n");
  266.   fprintf(stderr, "\n");
  267.   exit(1);
  268. }
  269.