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

  1. /*===========================================================================*
  2.  * == main program ==
  3.  * pmt.c - Shell pattern matching test using modified regexp.h header file.
  4.  *
  5.  * (C)Copyright IBM Corporation, 1989, 1990, 1991.         Brian E. Yoder
  6.  *
  7.  * This program contains an algorithm that attempts to expand (convert)
  8.  * a shell pattern-matching expression into a regular expression.
  9.  *
  10.  * The first argument should be a shell pattern-matching expression enclosed
  11.  * in double quotes.  The arguments that follow are compared with the expanded
  12.  * regular expression.
  13.  *
  14.  * This program uses a modified version of the regexp.h header file that is
  15.  * supplied with AIX.  As a result, this program is about half the size of
  16.  * the rxls program which uses the regcmp() and regex() subroutines from the
  17.  * programmer's workbench library.
  18.  *
  19.  * 12/01/89 - Created from rxls.c, rxh.c, and modified regexp.h files.
  20.  * 12/04/89 - Split out r*() subroutines into 'rxpm.c' module.
  21.  * 12/05/89 - Initial version completed.
  22.  * 12/08/89 - Removed references to 'envp' on main(): 'envp' is not portable
  23.  *            to all environments.
  24.  * 01/10/90 - Moved eexpr[] and cexpr[] buffers to static area, since OS/2's
  25.  *            startup code fails when these buffers are on the stack.
  26.  * 05/25/90 - Include libutil.h instead of rxpm.h.
  27.  * 04/04/91 - Ported from AIX to DOS and C/2.  When -d flag is specified,
  28.  *            then certain pointers are displayed in hexadecimal.  This
  29.  *            program assumes that pointers and integers are 32 bits wide.
  30.  *            For DOS and 16-bit OS/2, ints are 16-bits.  However, this
  31.  *            is left alone for now.
  32.  *===========================================================================*/
  33.  
  34. char ver[] = "pmt: Pattern-matching test.  (C)IBM Corp. 1989, 1990";
  35. char author[] = "Brian E. Yoder";
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <sys/types.h>
  40.  
  41. #include "util.h"
  42.  
  43. #define MINARGS  1             /* Mimimum command line arguments required */
  44.  
  45. #define ELEN     1024          /* Length of expression buffers */
  46.  
  47. #define NO       0             /* Yes/no definitions */
  48. #define YES      1
  49.  
  50. /*===========================================================================*
  51.  * Function prototypes for subroutines in this module
  52.  *===========================================================================*/
  53.  
  54. static void syntax();
  55.  
  56. /*===========================================================================*
  57.  * Data used by the main() program
  58.  *===========================================================================*/
  59.  
  60. static char *xmatch  = "**not initialized";
  61. static char *match   = "          matched";
  62. static char *nomatch = "--- did not match";
  63.  
  64. static char  eexpr[ELEN];      /* Full regular (expanded) expression */
  65. static char  cexpr[ELEN];      /* Compiled expression */
  66.  
  67. /*===========================================================================*
  68.  * Main program entry point
  69.  *===========================================================================*/
  70.  
  71. main(argc, argv)
  72.  
  73. int    argc;
  74. char **argv;
  75.  
  76. {
  77.   int rc;                      /* return code storage */
  78.  
  79.   int   exact;                 /* Exact match? */
  80.   int   debug;                 /* Print extra debugging information? */
  81.  
  82.   char *expr;                  /* Pointer to regular expression entered */
  83.  
  84.   char *next_eexpr;            /* Pointer to next expanded expression */
  85.   char *next_cexpr;            /* Pointer to next compiled expression */
  86.  
  87.   char *mstart;                /* For rscan(): Pointer to start of match */
  88.   char *mend;                  /*              Pointer past end of match */
  89.  
  90.   char *mrslt;                 /* Pointer to match result string */
  91.  
  92.   argv++;                      /* Ignore 1st argument (program name) */
  93.   argc--;
  94.  
  95.   if (argc <  MINARGS)         /* If not enough arguments: display syntax */
  96.      syntax();
  97.  
  98.   if (strcmp(*argv, "-d") == 0)
  99.   {                            /* If -d flag: */
  100.      debug = YES;                /* Set 'debug' flag = no */
  101.      argv++;                     /* Bump to next argument */
  102.      argc--;
  103.      if (argc <  MINARGS)      /* If not enough arguments: display syntax */
  104.         syntax();
  105.   }
  106.   else
  107.   {
  108.      debug = NO;               /* Set 'debug' flag = yes */
  109.   }
  110.  
  111.   if (strcmp(*argv, "-s") == 0)
  112.   {                            /* If -s flag: */
  113.      exact = NO;                  /* Set 'exact' flag = no */
  114.      argv++;                      /* Bump to next argument */
  115.      argc--;
  116.      if (argc <  MINARGS)      /* If not enough arguments: display syntax */
  117.         syntax();
  118.   }
  119.   else
  120.   {
  121.      exact = YES;              /* Set 'exact' flag = yes */
  122.   }
  123.  
  124.   expr = *argv;                /* Store pointer to expression */
  125.  
  126.   argv++;                      /* Set argv/argc up to get rest of arguments */
  127.   argc--;
  128.  
  129.  /*--------------------------------------------------------------------------*
  130.   * Expand 'expr' and store in 'exxpr[]'
  131.   *--------------------------------------------------------------------------*/
  132.  
  133.   rc = rexpand(expr, eexpr, &eexpr[ELEN+1], &next_eexpr);
  134.   if (rc != 0)
  135.   {
  136.      printf("--- Expanded expression is too large.\n");
  137.      return(0);
  138.   }
  139.  
  140.  /*--------------------------------------------------------------------------*
  141.   * Display original and expanded regular expressions
  142.   *--------------------------------------------------------------------------*/
  143.  
  144.   printf("%s\n", ver);
  145.   printf("Pattern:             '%s'\n", expr);
  146.   printf("Expanded expression: '%s'\n", eexpr);
  147.  
  148.   if (debug == YES)
  149.   {
  150.      printf("Pointer to expanded expression:   0x%08X\n", eexpr);
  151.      printf("Pointer past expanded expression: 0x%08X\n", next_eexpr);
  152.   }
  153.  
  154.  /*--------------------------------------------------------------------------*
  155.   * Compile 'eexpr' and store in 'cexpr[]'
  156.   *--------------------------------------------------------------------------*/
  157.  
  158.   rc = rcompile(eexpr, cexpr, &cexpr[ELEN+1], '\0', &next_cexpr);
  159.   if (rc != 0)
  160.   {
  161.      printf("--- Cannot compile expanded expression: %s\n",
  162.         rcmpmsg(rc));
  163.      return(0);
  164.   }
  165.  
  166.   if (debug == YES)
  167.   {
  168.      printf("Pointer to compiled expression:   0x%08X\n", cexpr);
  169.      printf("Pointer past compiled expression: 0x%08X\n", next_cexpr);
  170.      printf("Value of 'circf' stored in compiled expression: %d\n",
  171.         *(int *)cexpr);
  172.   }
  173.  
  174.  /*--------------------------------------------------------------------------*
  175.   * Display each successive command line argument and whether or not it
  176.   * matches the expanded regular expression in 'eexpr'
  177.   *--------------------------------------------------------------------------*/
  178.  
  179.   printf("\n");
  180.   if (exact == YES)
  181.      printf("Arguments checked for exact match with expression:\n");
  182.   else
  183.      printf("Arguments scanned to see if they contain the expression:\n");
  184.   printf("\n");
  185.  
  186.   while (argc > 0)                 /* For each argument passed by the shell: */
  187.   {
  188.      mrslt = xmatch;                   /* For now: 'mrslt' is not initialized */
  189.  
  190.      if (exact == YES)                 /* See if expression matches argument */
  191.         rc = rmatch(cexpr, *argv);
  192.      else
  193.         rc = rscan(cexpr, *argv, &mstart, &mend);
  194.  
  195.      if (rc == RMATCH)
  196.         mrslt = match;
  197.      else
  198.         mrslt = nomatch;
  199.  
  200.      printf("%s: '%s'",                /* Display the result of the match */
  201.         mrslt,                         /* along with the argument itself */
  202.         *argv);
  203.  
  204.      if ((exact == NO) && (rc == RMATCH))  /* If not an exact match: */
  205.      {                                     /* Also display the substring matched: */
  206.         *mend = '\0';                      /* Terminate substring w/null byte */
  207.         printf(" : '%s'", mstart);         /* Display it */
  208.      }
  209.  
  210.      printf("\n");                     /* Add newline */
  211.  
  212.      argv++;                           /* Point to next argument */
  213.      argc--;
  214.   }
  215.  
  216.   printf("\n");
  217.   return(0);
  218. }
  219.  
  220. /*===========================================================================*
  221.  * syntax() - Display command syntax and EXIT TO OS!
  222.  *===========================================================================*/
  223. static void syntax()
  224. {
  225.   printf("\n%s\n", ver);                      /* Display version */
  226.   printf("\n");
  227.   printf("pmt  [-d]  [-s]  \"expr\"  \"str\"  [\"str\" . . .]\n");
  228.   printf("\n");
  229.   printf("The first argument is treated as a pattern-matching expression\n");
  230.   printf("and is expanded into a full regular expression.\n");
  231.   printf("\n");
  232.   printf("The expanded expression is then matched against all arguments\n");
  233.   printf("that follow.  Enclose an argument in quotes if it contains\n");
  234.   printf("embedded blanks or special characters.\n");
  235.   printf("\n");
  236.   printf("if the -d (debug) flag is specified, then additional debugging\n");
  237.   printf("information is displayed.\n");
  238.   printf("\n");
  239.   printf("If the -s (scan) flag is specified, this program scans each argument\n");
  240.   printf("for the expanded regular expression.  Othewise, this program checks\n");
  241.   printf("each argument for an exact match with the regular expression.\n");
  242.   printf("\n");
  243.   exit(1);
  244. }
  245.