


      RRRREEEEGGGGEEEEXXXXPPPP((((5555))))                   ((((UUUUNNNNIIIIXXXX 5555....0000))))                  RRRREEEEGGGGEEEEXXXXPPPP((((5555))))



      NNNNAAAAMMMMEEEE
           regexp - regular expression compile and match routines

      SSSSYYYYNNNNTTTTAAAAXXXX
           ####ddddeeeeffffiiiinnnneeee IIIINNNNIIIITTTT <declarations>
           ####ddddeeeeffffiiiinnnneeee GGGGEEEETTTTCCCC(((()))) <getc code>
           ####ddddeeeeffffiiiinnnneeee PPPPEEEEEEEEKKKKCCCC(((()))) <peekc code>
           ####ddddeeeeffffiiiinnnneeee UUUUNNNNGGGGEEEETTTTCCCC((((c)))) <ungetc code>
           ####ddddeeeeffffiiiinnnneeee RRRREEEETTTTUUUURRRRNNNN((((pointer)))) <return code>
           ####ddddeeeeffffiiiinnnneeee EEEERRRRRRRROOOORRRR((((val)))) <error code>

           ####iiiinnnncccclllluuuuddddeeee  <<<<rrrreeeeggggeeeexxxxpppp....hhhh>>>>

           cccchhhhaaaarrrr ****ccccoooommmmppppiiiilllleeee((((iiiinnnnssssttttrrrriiiinnnngggg,,,, eeeexxxxppppbbbbuuuuffff,,,, eeeennnnddddbbbbuuuuffff,,,, eeeeooooffff))))
           cccchhhhaaaarrrr ****iiiinnnnssssttttrrrriiiinnnngggg,,,, ****eeeexxxxppppbbbbuuuuffff,,,, ****eeeennnnddddbbbbuuuuffff;;;;

           iiiinnnntttt sssstttteeeepppp((((ssssttttrrrriiiinnnngggg,,,, eeeexxxxppppbbbbuuuuffff))))
           cccchhhhaaaarrrr ****ssssttttrrrriiiinnnngggg,,,, ****eeeexxxxppppbbbbuuuuffff;;;;

      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
           This page describes general purpose regular expression
           matching routines in the form of _e_d(1), defined in
           ////uuuussssrrrr////iiiinnnncccclllluuuuddddeeee////rrrreeeeggggeeeexxxxpppp....hhhh.  Programs such as _e_d(1), _s_e_d(1),
           _g_r_e_p(1), _b_s(1), _e_x_p_r(1), etc., which perform regular
           expression matching use this source file.  In this way, only
           this file need be changed to maintain regular expression
           compatibility.

           The interface to this file is unpleasantly complex.
           Programs that include this file must have the following five
           macros declared before the ``#include <regexp.h>''
           statement.  These macros are used by the _c_o_m_p_i_l_e routine.

           GETC()              Return the value of the next character
                               in the regular expression pattern.
                               Successive calls to GETC() should return
                               successive characters of the regular
                               expression.

           PEEKC()             Return the next character in the regular
                               expression.  Successive calls to PEEKC()
                               should return the same character (which
                               should also be the next character
                               returned by GETC()).

           UNGETC(_c)           Cause the argument _c to be returned by
                               the next call to GETC() (and PEEKC()).
                               No more that one character of pushback
                               is ever needed and this character is
                               guaranteed to be the last character read
                               by GETC().  The value of the macro
                               UNGETC(_c) is always ignored.



      Page 1                                                   (9010-E)






      RRRREEEEGGGGEEEEXXXXPPPP((((5555))))                   ((((UUUUNNNNIIIIXXXX 5555....0000))))                  RRRREEEEGGGGEEEEXXXXPPPP((((5555))))



           RETURN(_p_o_i_n_t_e_r)     This macro is used on normal exit of the
                               _c_o_m_p_i_l_e routine.  The value of the
                               argument _p_o_i_n_t_e_r is a pointer to the
                               character after the last character of
                               the compiled regular expression.  This
                               is useful to programs which have memory
                               allocation to manage.

           ERROR(_v_a_l)          This is the abnormal return from the
                               _c_o_m_p_i_l_e routine.  The argument _v_a_l is an
                               error number (see table below for
                               meanings).  This call should never
                               return.

                ERROR     MEANING
                11        Range endpoint too large.
                16        Bad number.
                25        ``\\\\digit'' out of range.
                36        Illegal or missing delimiter.
                41        No remembered search string.
                42        \\\\(((( \\\\)))) imbalance.
                43        Too many \\\\((((.
                44        More than 2 numbers given in \\\\{{{{ \\\\}}}}.
                45        }}}} expected after \\\\.
                46        First number exceeds second in \\\\{{{{ \\\\}}}}.
                49        [[[[ ]]]] imbalance.
                50        Regular expression overflow.

           The syntax of the _c_o_m_p_i_l_e routine is as follows:

                compile(instring, expbuf, endbuf, eof)

           The first parameter _i_n_s_t_r_i_n_g is never used explicitly by the
           _c_o_m_p_i_l_e routine but is useful for programs that pass down
           different pointers to input characters.  It is sometimes
           used in the INIT declaration (see below).  Programs which
           call functions to input characters or have characters in an
           external array can pass down a value of ((char *) 0) for
           this parameter.

           The next parameter _e_x_p_b_u_f is a character pointer.  It points
           to the place where the compiled regular expression will be
           placed.

           The parameter _e_n_d_b_u_f is one more than the highest address
           where the compiled regular expression may be placed.  If the
           compiled expression cannot fit in (_e_n_d_b_u_f-_e_x_p_b_u_f) bytes, a
           call to ERROR(50) is made.

           The parameter _e_o_f is the character which marks the end of
           the regular expression.  For example, in _e_d(1), this
           character is usually a ////.



      Page 2                                                   (9010-E)






      RRRREEEEGGGGEEEEXXXXPPPP((((5555))))                   ((((UUUUNNNNIIIIXXXX 5555....0000))))                  RRRREEEEGGGGEEEEXXXXPPPP((((5555))))



           Each program that includes this file must have a ####ddddeeeeffffiiiinnnneeee
           statement for INIT.  This definition will be placed right
           after the declaration for the function _c_o_m_p_i_l_e and the
           opening curly brace ({{{{).  It is used for dependent
           declarations and initializations.  Most often it is used to
           set a register variable to point the beginning of the
           regular expression so that this register variable can be
           used in the declarations for GETC(), PEEKC() and UNGETC().
           Otherwise it can be used to declare external variables that
           might be used by GETC(), PEEKC() and UNGETC().  See the
           example below of the declarations taken from _g_r_e_p(1).

           There are other functions in this file which perform actual
           regular expression matching, one of which is the function
           _s_t_e_p.  The call to _s_t_e_p is as follows:

                step(string, expbuf)

           The first parameter to _s_t_e_p is a pointer to a string of
           characters to be checked for a match.  This string should be
           null terminated.

           The second parameter _e_x_p_b_u_f is the compiled regular
           expression which was obtained by a call of the function
           _c_o_m_p_i_l_e.

           The function _s_t_e_p returns one, if the given string matches
           the regular expression, and zero if the expressions do not
           match.  If there is a match, two external character pointers
           are set as a side effect to the call to _s_t_e_p.  The variable
           set in _s_t_e_p is _l_o_c_1.  This is a pointer to the first
           character that matched the regular expression.  The variable
           _l_o_c_2, which is set by the function _a_d_v_a_n_c_e, points the
           character after the last character that matches the regular
           expression.  Thus if the regular expression matches the
           entire line, loc1 will point to the first character of
           _s_t_r_i_n_g and _l_o_c_2 will point to the null at the end of _s_t_r_i_n_g.

           _S_t_e_p uses the external variable _c_i_r_c_f which is set by
           _c_o_m_p_i_l_e if the regular expression begins with ^^^^.  If this is
           set then _s_t_e_p will only try to match the regular expression
           to the beginning of the string.  If more than one regular
           expression is to be compiled before the first is executed
           the value of _c_i_r_c_f should be saved for each compiled
           expression and _c_i_r_c_f should be set to that saved value
           before each call to _s_t_e_p.

           The function _a_d_v_a_n_c_e is called from _s_t_e_p with the same
           arguments as _s_t_e_p.  The purpose of _s_t_e_p is to step through
           the _s_t_r_i_n_g argument and call _a_d_v_a_n_c_e until _a_d_v_a_n_c_e returns a
           one indicating a match or until the end of _s_t_r_i_n_g is
           reached.  If one wants to constrain _s_t_r_i_n_g to the beginning



      Page 3                                                   (9010-E)






      RRRREEEEGGGGEEEEXXXXPPPP((((5555))))                   ((((UUUUNNNNIIIIXXXX 5555....0000))))                  RRRREEEEGGGGEEEEXXXXPPPP((((5555))))



           of the line in all cases, _s_t_e_p need not be called, simply
           call _a_d_v_a_n_c_e.

           When _a_d_v_a_n_c_e encounters a **** or \\\\{{{{ \\\\}}}} sequence in the regular
           expression it will advance its pointer to the string to be
           matched as far as possible and will recursively call itself
           trying to match the rest of the string to the rest of the
           regular expression.  As long as there is no match, _a_d_v_a_n_c_e
           will back up along the string until it finds a match or
           reaches the point in the string that initially matched the ****
           or \\\\{{{{ \\\\}}}}.  It is sometimes desirable to stop this backing up
           before the initial point in the string is reached.  If the
           external character pointer _l_o_c_s is equal to the point in the
           string at sometime during the backing up process, _a_d_v_a_n_c_e
           will break out of the loop that backs up and will return
           zero.  This is used be _e_d(1) and _s_e_d(1) for substitutions
           done globally (not just the first occurrence, but the whole
           line) so, for example, expressions like ssss////yyyy****////////gggg do not loop
           forever.

           The routines _e_c_m_p and _g_e_t_r_a_n_g_e are trivial and are called by
           the routines previously mentioned.

      EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
           The following is an example of how the regular expression
           macros and calls look from _g_r_e_p(1):

           #define INIT   register char *sp = instring;
           #define GETC() (*sp++)
           #define PEEKC()     (*sp)
           #define UNGETC(c)   (--sp)
           #define RETURN(c)   return;
           #define ERROR(c)    regerr()

           #include <regexp.h>
           ...
                   compile(*argv, expbuf, &expbuf[ESIZE], '\0');
           ...
                   if(step(linebuf, expbuf))
                           succeed();

      FFFFIIIILLLLEEEESSSS
           /usr/include/regexp.h

      SSSSEEEEEEEE AAAALLLLSSSSOOOO
           ed(1), grep(1), sed(1).

      BBBBUUUUGGGGSSSS
           The handling of _c_i_r_c_f is kludgy.
           The routine _e_c_m_p is equivalent to the Standard I/O routine
           _s_t_r_n_c_m_p and should be replaced by that routine.
           The actual code is probably easier to understand than this



      Page 4                                                   (9010-E)






      RRRREEEEGGGGEEEEXXXXPPPP((((5555))))                   ((((UUUUNNNNIIIIXXXX 5555....0000))))                  RRRREEEEGGGGEEEEXXXXPPPP((((5555))))



           manual page.






















































      Page 5                                                   (9010-E)



