regexpr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Simple Regular Expression functions. Derived from Unix 7th Edition,
  3. * /usr/src/cmd/expr.y
  4. *
  5. * Modified by Gunnar Ritter, Freiburg i. Br., Germany, January 2003.
  6. *
  7. * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * Redistributions of source code and documentation must retain the
  13. * above copyright notice, this list of conditions and the following
  14. * disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * This product includes software developed or owned by Caldera
  21. * International, Inc.
  22. * Neither the name of Caldera International, Inc. nor the names of
  23. * other contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
  27. * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  34. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  35. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  36. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  37. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /* Sccsid @(#)regexpr.c 1.8 (gritter) 10/13/04 */
  40. #include <stdlib.h>
  41. #include "regexpr.h"
  42. int regerrno, reglength;
  43. static int circf;
  44. static char *regexpr_compile(char *, char *, const char *, int);
  45. char *
  46. compile(const char *instring, char *ep, char *endbuf)
  47. {
  48. char *cp;
  49. int sz = 0;
  50. if (ep == 0) {
  51. for (cp = (char *)instring; *cp != '\0'; cp++)
  52. if (*cp == '[')
  53. sz += 32;
  54. sz += 2 * (cp - instring) + 5;
  55. if ((ep = malloc(sz)) == 0) {
  56. regerrno = 11;
  57. return 0;
  58. }
  59. endbuf = &ep[sz];
  60. ep[1] = '\0';
  61. }
  62. if ((cp=regexpr_compile((char *)instring, &ep[1], endbuf, '\0')) == 0) {
  63. if (sz)
  64. free(ep);
  65. return 0;
  66. }
  67. ep[0] = circf;
  68. reglength = cp - ep;
  69. return sz ? ep : cp;
  70. }
  71. #define INIT register char *sp = instring;
  72. #define GETC() (*sp++)
  73. #define PEEKC() (*sp)
  74. #define UNGETC(c) (--sp)
  75. #define RETURN(c) return (c);
  76. #define ERROR(c) { regerrno = c; return 0; }
  77. #define compile(a, b, c, d) regexpr_compile(a, b, c, d)
  78. #define regexp_h_static static
  79. #define REGEXP_H_STEP_INIT circf = *p2++;
  80. #define REGEXP_H_ADVANCE_INIT circf = *ep++;
  81. #include "regexp.h"