re.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* re.c: This file contains the regular expression interface routines for
  2. the ed line editor. */
  3. /*-
  4. * Copyright (c) 1993 Andrew Moore, Talke Studio.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. #include "ed.h"
  30. const char *errmsg = "";
  31. /* get_compiled_pattern: return pointer to compiled pattern from command
  32. buffer */
  33. pattern_t *
  34. get_compiled_pattern(void)
  35. {
  36. static pattern_t *expr = NULL;
  37. static char error[1024];
  38. char *exprs;
  39. char delimiter;
  40. int n;
  41. if ((delimiter = *ibufp) == ' ') {
  42. errmsg = "invalid pattern delimiter";
  43. return NULL;
  44. } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
  45. if (!expr)
  46. errmsg = "no previous pattern";
  47. return expr;
  48. } else if ((exprs = extract_pattern(delimiter)) == NULL)
  49. return NULL;
  50. /* buffer alloc'd && not reserved */
  51. if (expr && !patlock)
  52. regfree(expr);
  53. else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
  54. fprintf(stderr, "%s\n", strerror(errno));
  55. errmsg = "out of memory";
  56. return NULL;
  57. }
  58. patlock = 0;
  59. if ((n = regcomp(expr, exprs, 0))) {
  60. regerror(n, expr, error, sizeof error);
  61. errmsg = error;
  62. free(expr);
  63. return expr = NULL;
  64. }
  65. return expr;
  66. }
  67. /* extract_pattern: copy a pattern string from the command buffer; return
  68. pointer to the copy */
  69. char *
  70. extract_pattern(int delimiter)
  71. {
  72. static char *lhbuf = NULL; /* buffer */
  73. static int lhbufsz = 0; /* buffer size */
  74. char *nd;
  75. int len;
  76. for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
  77. switch (*nd) {
  78. default:
  79. break;
  80. case '[':
  81. if ((nd = parse_char_class(nd + 1)) == NULL) {
  82. errmsg = "unbalanced brackets ([])";
  83. return NULL;
  84. }
  85. break;
  86. case '\\':
  87. if (*++nd == '\n') {
  88. errmsg = "trailing backslash (\\)";
  89. return NULL;
  90. }
  91. break;
  92. }
  93. len = nd - ibufp;
  94. REALLOC(lhbuf, lhbufsz, len + 1, NULL);
  95. memcpy(lhbuf, ibufp, len);
  96. lhbuf[len] = '\0';
  97. ibufp = nd;
  98. return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
  99. }
  100. /* parse_char_class: expand a POSIX character class */
  101. char *
  102. parse_char_class(char *s)
  103. {
  104. int c, d;
  105. if (*s == '^')
  106. s++;
  107. if (*s == ']')
  108. s++;
  109. for (; *s != ']' && *s != '\n'; s++)
  110. if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
  111. for (s++, c = *++s; *s != ']' || c != d; s++)
  112. if ((c = *s) == '\n')
  113. return NULL;
  114. return (*s == ']') ? s : NULL;
  115. }