home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / contool2.2 / part02 / regexp.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-26  |  3.2 KB  |  110 lines

  1. /************************************************************************/
  2. /*    Copyright 1988, 1989 by Chuck Musciano and Harris Corporation    */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.  This     */
  15. /*    software may not be sold without the prior explicit permission    */
  16. /*    of Harris Corporation.                        */
  17. /************************************************************************/
  18.  
  19. #include    <stdio.h>
  20.  
  21. #include    "contool.h"
  22.  
  23. PRIVATE    regexp_error();
  24.  
  25. #define        INIT            register char *expbuf = ep, *sp = instring;
  26. #define        GETC()            (*sp++)
  27. #define        PEEKC()            (*sp)
  28. #define        UNGETC(c)        (--sp)
  29. #define        RETURN(p)        {bcopy(expbuf, sp = (char *) malloc(p - expbuf), p - expbuf); return(sp);}
  30. #define        ERROR(val)        {regexp_error(val, instring); return(NULL);}
  31.  
  32. #include    <regexp.h>
  33.  
  34. PRIVATE    char    error_message[512];
  35.  
  36. /************************************************************************/
  37. EXPORT    int    match_exp(exp, circ, str)
  38.  
  39. char    *exp;
  40. int    circ;
  41. char    *str;
  42.  
  43. {
  44.     circf = circ;
  45.     return(step(str, exp));
  46. }
  47.  
  48. /************************************************************************/
  49. PRIVATE    regexp_error(val, string)
  50.  
  51. int    val;
  52. char    *string;
  53.  
  54. {    char    *msg;
  55.  
  56.     switch (val) {
  57.        case 11 : msg = "range endpoint too large";
  58.        case 16 : msg = "bad number";
  59.        case 25 : msg = "\"\\digit\" out of range";
  60.        case 36 : msg = "illegal or missing delimiter";
  61.        case 41 : msg = "no remembered search string";
  62.        case 42 : msg = "\\(\\) imbalance";
  63.        case 43 : msg = "too many \\(";
  64.        case 44 : msg = "more than 2 numbers given in \\{\\}";
  65.        case 45 : msg = "} expected after \\";
  66.        case 46 : msg = "first number exceeds second in \\{\\}";
  67.        case 49 : msg = "[] imbalance";
  68.        case 50 : msg = "regular expression overflow";
  69.        default : msg = "regular expression error";
  70.        }
  71.     sprintf(error_message, "%s in\n\t%s", msg, string);
  72. }
  73.  
  74. /************************************************************************/
  75. EXPORT    char    *compile_exp(filter, start, end)
  76.  
  77. f_ptr    filter;
  78. char    *start;
  79. char    *end;
  80.  
  81. {    char    rbuf[1024], *sre, *ere;
  82.     int    sc, ec;
  83.  
  84.     sre = ere = NULL;
  85.     if ((sre = compile(start, rbuf, rbuf+1024, '\0')) == NULL)
  86.        return(error_message);
  87.     sc = circf;
  88.     if (end) {
  89.        if ((ere = compile(end, rbuf, rbuf+1024, '\0')) == NULL) {
  90.           free(sre);
  91.           return(error_message);
  92.           }
  93.        ec = circf;
  94.        }
  95.     if (filter) {
  96.        filter->start = start;
  97.        filter->end = end;
  98.        filter->start_re = sre;
  99.        filter->end_re = ere;
  100.        filter->scircf = sc;
  101.        filter->ecircf = ec;
  102.        }
  103.     else {
  104.        free(sre);
  105.        if (ere)
  106.           free(ere);
  107.        }
  108.     return(NULL);
  109. }
  110.