home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 25.ddi / root.2 / usr / ucbinclude / regexp.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-08  |  8.7 KB  |  533 lines

  1. /*    Copyright (c) 1990 UNIX System Laboratories, Inc.    */
  2. /*    Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T    */
  3. /*      All Rights Reserved      */
  4.  
  5. /*    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF         */
  6. /*    UNIX System Laboratories, Inc.                         */
  7. /*    The copyright notice above does not evidence any       */
  8. /*    actual or intended publication of such source code.    */
  9.  
  10.  
  11. #ident    "@(#)//usr/ucbinclude/regexp.h.sl 1.1 4.0 12/08/90 48373 AT&T-USL"
  12.  
  13. /*******************************************************************
  14.  
  15.         PROPRIETARY NOTICE (Combined)
  16.  
  17. This source code is unpublished proprietary information
  18. constituting, or derived under license from AT&T's UNIX(r) System V.
  19. In addition, portions of such source code were derived from Berkeley
  20. 4.3 BSD under license from the Regents of the University of
  21. California.
  22.  
  23.  
  24.  
  25.         Copyright Notice 
  26.  
  27. Notice of copyright on this source code product does not indicate 
  28. publication.
  29.  
  30.     (c) 1986,1987,1988,1989  Sun Microsystems, Inc
  31.     (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
  32.               All rights reserved.
  33. ********************************************************************/ 
  34.  
  35. #ifndef _REGEXP_H
  36. #define _REGEXP_H
  37.  
  38. #include <ctype.h>
  39.  
  40. #define    CBRA    2
  41. #define    CCHR    4
  42. #define    CDOT    8
  43. #define    CCL    12
  44. #define    CXCL    16
  45. #define    CDOL    20
  46. #define    CCEOF    22
  47. #define    CKET    24
  48. #define    CBACK    36
  49. #define NCCL    40
  50.  
  51. #define    STAR    01
  52. #define RNGE    03
  53.  
  54. #define    NBRA    9
  55.  
  56. #define PLACE(c)    ep[c >> 3] |= bittab[c & 07]
  57. #define ISTHERE(c)    (ep[c >> 3] & bittab[c & 07])
  58. #define ecmp(s1, s2, n)    (!strncmp(s1, s2, n))
  59.  
  60. static char    *braslist[NBRA];
  61. static char    *braelist[NBRA];
  62. int    sed, nbra;
  63. char    *loc1, *loc2, *locs;
  64. static int    nodelim;
  65.  
  66. int    circf;
  67. static int    low;
  68. static int    size;
  69.  
  70. static char    bittab[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
  71.  
  72. char *
  73. compile(instring, ep, endbuf, seof)
  74. register char *ep;
  75. char *instring, *endbuf;
  76. {
  77.     INIT    /* Dependent declarations and initializations */
  78.     register c;
  79.     register eof = seof;
  80.     char *lastep = instring;
  81.     int cclcnt;
  82.     char bracket[NBRA], *bracketp;
  83.     int closed;
  84.     int neg;
  85.     int lc;
  86.     int i, cflg;
  87.     int iflag; /* used for non-ascii characters in brackets */
  88.  
  89.     lastep = 0;
  90.     if((c = GETC()) == eof || c == '\n') {
  91.         if(c == '\n') {
  92.             UNGETC(c);
  93.             nodelim = 1;
  94.         }
  95.         if(*ep == 0 && !sed)
  96.             ERROR(41);
  97.         RETURN(ep);
  98.     }
  99.     bracketp = bracket;
  100.     circf = closed = nbra = 0;
  101.     if(c == '^')
  102.         circf++;
  103.     else
  104.         UNGETC(c);
  105.     while(1) {
  106.         if(ep >= endbuf)
  107.             ERROR(50);
  108.         c = GETC();
  109.         if(c != '*' && ((c != '\\') || (PEEKC() != '{')))
  110.             lastep = ep;
  111.         if(c == eof) {
  112.             *ep++ = CCEOF;
  113.             if (bracketp != bracket)
  114.                 ERROR(42);
  115.             RETURN(ep);
  116.         }
  117.         switch(c) {
  118.  
  119.         case '.':
  120.             *ep++ = CDOT;
  121.             continue;
  122.  
  123.         case '\n':
  124.             if(!sed) {
  125.                 UNGETC(c);
  126.                 *ep++ = CCEOF;
  127.                 nodelim = 1;
  128.                 if(bracketp != bracket)
  129.                     ERROR(42);
  130.                 RETURN(ep);
  131.             }
  132.             else ERROR(36);
  133.         case '*':
  134.             if(lastep == 0 || *lastep == CBRA || *lastep == CKET)
  135.                 goto defchar;
  136.             *lastep |= STAR;
  137.             continue;
  138.  
  139.         case '$':
  140.             if(PEEKC() != eof && PEEKC() != '\n')
  141.                 goto defchar;
  142.             *ep++ = CDOL;
  143.             continue;
  144.  
  145.         case '[':
  146.             if(&ep[17] >= endbuf)
  147.                 ERROR(50);
  148.  
  149.             *ep++ = CCL;
  150.             lc = 0;
  151.             for(i = 0; i < 16; i++)
  152.                 ep[i] = 0;
  153.  
  154.             neg = 0;
  155.             if((c = GETC()) == '^') {
  156.                 neg = 1;
  157.                 c = GETC();
  158.             }
  159.             iflag = 1;
  160.             do {
  161.                 c &= 0377;
  162.                 if(c == '\0' || c == '\n')
  163.                     ERROR(49);
  164.                 if((c & 0200) && iflag) {
  165.                     iflag = 0;
  166.                     if(&ep[32] >= endbuf)
  167.                         ERROR(50);
  168.                     ep[-1] = CXCL;
  169.                     for(i = 16; i < 32; i++)
  170.                         ep[i] = 0;
  171.                 }
  172.                 if(c == '-' && lc != 0) {
  173.                     if((c = GETC()) == ']') {
  174.                         PLACE('-');
  175.                         break;
  176.                     }
  177.                     if((c & 0200) && iflag) {
  178.                         iflag = 0;
  179.                         if(&ep[32] >= endbuf)
  180.                             ERROR(50);
  181.                         ep[-1] = CXCL;
  182.                         for(i = 16; i < 32; i++)
  183.                             ep[i] = 0;
  184.                     }
  185.                     while(lc < c ) {
  186.                         PLACE(lc);
  187.                         lc++;
  188.                     }
  189.                 }
  190.                 lc = c;
  191.                 PLACE(c);
  192.             } while((c = GETC()) != ']');
  193.             
  194.             if(iflag)
  195.                 iflag = 16;
  196.             else
  197.                 iflag = 32;
  198.             
  199.             if(neg) {
  200.                 if(iflag == 32) {
  201.                     for(cclcnt = 0; cclcnt < iflag; cclcnt++)
  202.                         ep[cclcnt] ^= 0377;
  203.                     ep[0] &= 0376;
  204.                 } else {
  205.                     ep[-1] = NCCL;
  206.                     /* make nulls match so test fails */
  207.                     ep[0] |= 01;
  208.                 }
  209.             }
  210.  
  211.             ep += iflag;
  212.  
  213.             continue;
  214.  
  215.         case '\\':
  216.             switch(c = GETC()) {
  217.  
  218.             case '(':
  219.                 if(nbra >= NBRA)
  220.                     ERROR(43);
  221.                 *bracketp++ = nbra;
  222.                 *ep++ = CBRA;
  223.                 *ep++ = nbra++;
  224.                 continue;
  225.  
  226.             case ')':
  227.                 if(bracketp <= bracket) 
  228.                     ERROR(42);
  229.                 *ep++ = CKET;
  230.                 *ep++ = *--bracketp;
  231.                 closed++;
  232.                 continue;
  233.  
  234.             case '{':
  235.                 if(lastep == (char *) 0)
  236.                     goto defchar;
  237.                 *lastep |= RNGE;
  238.                 cflg = 0;
  239.             nlim:
  240.                 c = GETC();
  241.                 i = 0;
  242.                 do {
  243.                     if('0' <= c && c <= '9')
  244.                         i = 10 * i + c - '0';
  245.                     else
  246.                         ERROR(16);
  247.                 } while(((c = GETC()) != '\\') && (c != ','));
  248.                 if(i >= 255)
  249.                     ERROR(11);
  250.                 *ep++ = i;
  251.                 if(c == ',') {
  252.                     if(cflg++)
  253.                         ERROR(44);
  254.                     if((c = GETC()) == '\\')
  255.                         *ep++ = 255;
  256.                     else {
  257.                         UNGETC(c);
  258.                         goto nlim;
  259.                         /* get 2'nd number */
  260.                     }
  261.                 }
  262.                 if(GETC() != '}')
  263.                     ERROR(45);
  264.                 if(!cflg)    /* one number */
  265.                     *ep++ = i;
  266.                 else if((ep[-1] & 0377) < (ep[-2] & 0377))
  267.                     ERROR(46);
  268.                 continue;
  269.  
  270.             case '\n':
  271.                 ERROR(36);
  272.  
  273.             case 'n':
  274.                 c = '\n';
  275.                 goto defchar;
  276.  
  277.             default:
  278.                 if(c >= '1' && c <= '9') {
  279.                     if((c -= '1') >= closed)
  280.                         ERROR(25);
  281.                     *ep++ = CBACK;
  282.                     *ep++ = c;
  283.                     continue;
  284.                 }
  285.             }
  286.     /* Drop through to default to use \ to turn off special chars */
  287.  
  288.         defchar:
  289.         default:
  290.             lastep = ep;
  291.             *ep++ = CCHR;
  292.             *ep++ = c;
  293.         }
  294.     }
  295. }
  296.  
  297. int step(p1, p2)
  298. register char *p1, *p2; 
  299. {
  300.     register c;
  301.  
  302.  
  303.     if(circf) {
  304.         loc1 = p1;
  305.         return(advance(p1, p2));
  306.     }
  307.     /* fast check for first character */
  308.     if(*p2 == CCHR) {
  309.         c = p2[1];
  310.         do {
  311.             if(*p1 != c)
  312.                 continue;
  313.             if(advance(p1, p2)) {
  314.                 loc1 = p1;
  315.                 return(1);
  316.             }
  317.         } while(*p1++);
  318.         return(0);
  319.     }
  320.         /* regular algorithm */
  321.     do {
  322.         if(advance(p1, p2)) {
  323.             loc1 = p1;
  324.             return(1);
  325.         }
  326.     } while(*p1++);
  327.     return(0);
  328. }
  329.  
  330. advance(lp, ep)
  331. register char *lp, *ep;
  332. {
  333.     register char *curlp;
  334.     int c;
  335.     char *bbeg; 
  336.     register char neg;
  337.     int ct;
  338.  
  339.     while(1) {
  340.         neg = 0;
  341.         switch(*ep++) {
  342.  
  343.         case CCHR:
  344.             if(*ep++ == *lp++)
  345.                 continue;
  346.             return(0);
  347.     
  348.         case CDOT:
  349.             if(*lp++)
  350.                 continue;
  351.             return(0);
  352.     
  353.         case CDOL:
  354.             if(*lp == 0)
  355.                 continue;
  356.             return(0);
  357.     
  358.         case CCEOF:
  359.             loc2 = lp;
  360.             return(1);
  361.     
  362.         case CXCL: 
  363.             c = (unsigned char)*lp++;
  364.             if(ISTHERE(c)) {
  365.                 ep += 32;
  366.                 continue;
  367.             }
  368.             return(0);
  369.         
  370.         case NCCL:    
  371.             neg = 1;
  372.  
  373.         case CCL: 
  374.             c = *lp++;
  375.             if(((c & 0200) == 0 && ISTHERE(c)) ^ neg) {
  376.                 ep += 16;
  377.                 continue;
  378.             }
  379.             return(0);
  380.         
  381.         case CBRA:
  382.             braslist[*ep++] = lp;
  383.             continue;
  384.     
  385.         case CKET:
  386.             braelist[*ep++] = lp;
  387.             continue;
  388.     
  389.         case CCHR | RNGE:
  390.             c = *ep++;
  391.             getrnge(ep);
  392.             while(low--)
  393.                 if(*lp++ != c)
  394.                     return(0);
  395.             curlp = lp;
  396.             while(size--) 
  397.                 if(*lp++ != c)
  398.                     break;
  399.             if(size < 0)
  400.                 lp++;
  401.             ep += 2;
  402.             goto star;
  403.     
  404.         case CDOT | RNGE:
  405.             getrnge(ep);
  406.             while(low--)
  407.                 if(*lp++ == '\0')
  408.                     return(0);
  409.             curlp = lp;
  410.             while(size--)
  411.                 if(*lp++ == '\0')
  412.                     break;
  413.             if(size < 0)
  414.                 lp++;
  415.             ep += 2;
  416.             goto star;
  417.     
  418.         case CXCL | RNGE:
  419.             getrnge(ep + 32);
  420.             while(low--) {
  421.                 c = (unsigned char)*lp++;
  422.                 if(!ISTHERE(c))
  423.                     return(0);
  424.             }
  425.             curlp = lp;
  426.             while(size--) {
  427.                 c = (unsigned char)*lp++;
  428.                 if(!ISTHERE(c))
  429.                     break;
  430.             }
  431.             if(size < 0)
  432.                 lp++;
  433.             ep += 34;        /* 32 + 2 */
  434.             goto star;
  435.         
  436.         case NCCL | RNGE:
  437.             neg = 1;
  438.         
  439.         case CCL | RNGE:
  440.             getrnge(ep + 16);
  441.             while(low--) {
  442.                 c = *lp++;
  443.                 if(((c & 0200) || !ISTHERE(c)) ^ neg)
  444.                     return(0);
  445.             }
  446.             curlp = lp;
  447.             while(size--) {
  448.                 c = *lp++;
  449.                 if(((c & 0200) || !ISTHERE(c)) ^ neg)
  450.                     break;
  451.             }
  452.             if(size < 0)
  453.                 lp++;
  454.             ep += 18;         /* 16 + 2 */
  455.             goto star;
  456.     
  457.         case CBACK:
  458.             bbeg = braslist[*ep];
  459.             ct = braelist[*ep++] - bbeg;
  460.     
  461.             if(ecmp(bbeg, lp, ct)) {
  462.                 lp += ct;
  463.                 continue;
  464.             }
  465.             return(0);
  466.     
  467.         case CBACK | STAR:
  468.             bbeg = braslist[*ep];
  469.             ct = braelist[*ep++] - bbeg;
  470.             curlp = lp;
  471.             while(ecmp(bbeg, lp, ct))
  472.                 lp += ct;
  473.     
  474.             while(lp >= curlp) {
  475.                 if(advance(lp, ep))    return(1);
  476.                 lp -= ct;
  477.             }
  478.             return(0);
  479.     
  480.     
  481.         case CDOT | STAR:
  482.             curlp = lp;
  483.             while(*lp++);
  484.             goto star;
  485.     
  486.         case CCHR | STAR:
  487.             curlp = lp;
  488.             while(*lp++ == *ep);
  489.             ep++;
  490.             goto star;
  491.     
  492.         case CXCL | STAR:
  493.             curlp = lp;
  494.             do {
  495.                 c = (unsigned char)*lp++;
  496.             } while(ISTHERE(c));
  497.             ep += 32;
  498.             goto star;
  499.         
  500.         case NCCL | STAR:
  501.             neg = 1;
  502.  
  503.         case CCL | STAR:
  504.             curlp = lp;
  505.             do {
  506.                 c = *lp++;
  507.             } while(((c & 0200) == 0 && ISTHERE(c)) ^ neg);
  508.             ep += 16;
  509.             goto star;
  510.     
  511.         star:
  512.             do {
  513.                 if(--lp == locs)
  514.                     break;
  515.                 if(advance(lp, ep))
  516.                     return(1);
  517.             } while(lp > curlp);
  518.             return(0);
  519.  
  520.         }
  521.     }
  522. }
  523.  
  524. static
  525. getrnge(str)
  526. register char *str;
  527. {
  528.     low = *str++ & 0377;
  529.     size = ((*str & 0377) == 255)? 20000: (*str &0377) - low;
  530. }
  531.  
  532. #endif     /* _REGEXP_H */
  533.