| [9] | 1 | /* Copyright (c) 1985 Ceriel J.H. Jacobs */
|
|---|
| 2 |
|
|---|
| 3 | # ifndef lint
|
|---|
| 4 | static char rcsid[] = "$Header: /cvsup/minix/src/commands/yap/pattern.c,v 1.1.1.1 2005/04/21 14:55:41 beng Exp $";
|
|---|
| 5 | # endif not lint
|
|---|
| 6 |
|
|---|
| 7 | # define _PATTERN_
|
|---|
| 8 |
|
|---|
| 9 | # include "in_all.h"
|
|---|
| 10 | # include "pattern.h"
|
|---|
| 11 | # include "getline.h"
|
|---|
| 12 |
|
|---|
| 13 | # if V8_REGEX
|
|---|
| 14 | # include <regexp.h>
|
|---|
| 15 | # endif V8_REGEX
|
|---|
| 16 |
|
|---|
| 17 | /*
|
|---|
| 18 | * Interface to regular expression routines.
|
|---|
| 19 | * Also: simple minded patterns without meta-characters.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | # if USG_REGEX
|
|---|
| 23 | static char *pattern; /* Pointer to compiled pattern */
|
|---|
| 24 | char *regcmp(), *regex();
|
|---|
| 25 | # endif USG_REGEX
|
|---|
| 26 | # if V8_REGEX
|
|---|
| 27 | static struct regexp *pattern;
|
|---|
| 28 | static char *rc_error;
|
|---|
| 29 | struct regexp *regcomp();
|
|---|
| 30 | # endif V8_REGEX
|
|---|
| 31 |
|
|---|
| 32 | # if USG_REGEX || V8_REGEX
|
|---|
| 33 | /*
|
|---|
| 34 | * Compile a new pattern, but first free previous result.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | char *
|
|---|
| 38 | re_comp(s) char *s; {
|
|---|
| 39 |
|
|---|
| 40 | if (!*s) {
|
|---|
| 41 | /*
|
|---|
| 42 | * user wants previous pattern
|
|---|
| 43 | */
|
|---|
| 44 | return (char *) 0;
|
|---|
| 45 | }
|
|---|
| 46 | if (pattern) {
|
|---|
| 47 | /*
|
|---|
| 48 | * there was a compiled pattern
|
|---|
| 49 | */
|
|---|
| 50 | free(pattern);
|
|---|
| 51 | pattern = 0;
|
|---|
| 52 | }
|
|---|
| 53 | # if USG_REGEX
|
|---|
| 54 | return (pattern = regcmp(s, (char *) 0)) ?
|
|---|
| 55 | (char *) 0 :
|
|---|
| 56 | "Error in pattern";
|
|---|
| 57 | # endif USG_REGEX
|
|---|
| 58 | # if V8_REGEX
|
|---|
| 59 | pattern = regcomp(s);
|
|---|
| 60 | if (pattern) return (char *) 0;
|
|---|
| 61 | if (rc_error) return rc_error;
|
|---|
| 62 | return "Error in pattern";
|
|---|
| 63 | # endif V8_REGEX
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | # if V8_REGEX
|
|---|
| 67 | VOID
|
|---|
| 68 | regerror(str) char *str; {
|
|---|
| 69 | rc_error = str;
|
|---|
| 70 | }
|
|---|
| 71 | # endif V8_REGEX
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * Search for compiled pattern in string "s". Return 0 if not found.
|
|---|
| 75 | */
|
|---|
| 76 |
|
|---|
| 77 | re_exec(s) char *s; {
|
|---|
| 78 |
|
|---|
| 79 | # if USG_REGEX
|
|---|
| 80 | return !(regex(pattern,s) == 0);
|
|---|
| 81 | # endif USG_REGEX
|
|---|
| 82 | # if V8_REGEX
|
|---|
| 83 | # if _MINIX
|
|---|
| 84 | return regexec(pattern,s,1);
|
|---|
| 85 | # else
|
|---|
| 86 | return regexec(pattern,s);
|
|---|
| 87 | # endif
|
|---|
| 88 | # endif V8_REGEX
|
|---|
| 89 | }
|
|---|
| 90 | # else
|
|---|
| 91 | # ifndef BSD_REGEX
|
|---|
| 92 | /*
|
|---|
| 93 | * In this case, simple minded pattern search without meta-characters
|
|---|
| 94 | */
|
|---|
| 95 |
|
|---|
| 96 | char *strcpy();
|
|---|
| 97 |
|
|---|
| 98 | static char *pattern;
|
|---|
| 99 |
|
|---|
| 100 | /*
|
|---|
| 101 | * re_comp : Just remember pattern.
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 | char *
|
|---|
| 105 | re_comp(s) char *s; {
|
|---|
| 106 |
|
|---|
| 107 | if (!*s) {
|
|---|
| 108 | /*
|
|---|
| 109 | * User wants previous pattern
|
|---|
| 110 | */
|
|---|
| 111 | if (!pattern) {
|
|---|
| 112 | return "No previous regular expression";
|
|---|
| 113 | }
|
|---|
| 114 | return (char *) 0;
|
|---|
| 115 | }
|
|---|
| 116 | if (pattern) {
|
|---|
| 117 | /*
|
|---|
| 118 | * Free old pattern
|
|---|
| 119 | */
|
|---|
| 120 | free(pattern);
|
|---|
| 121 | }
|
|---|
| 122 | pattern = alloc((unsigned) (strlen(s) + 1), 0);
|
|---|
| 123 | (VOID) strcpy(pattern,s);
|
|---|
| 124 | return (char *) 0;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | * re-exec : Simple minded pattern matcher
|
|---|
| 129 | */
|
|---|
| 130 |
|
|---|
| 131 | re_exec(s) register char *s; {
|
|---|
| 132 |
|
|---|
| 133 | register char *ppat, *pstr;
|
|---|
| 134 |
|
|---|
| 135 | for (; *s; s++) {
|
|---|
| 136 | /*
|
|---|
| 137 | * As long as there are characters ...
|
|---|
| 138 | */
|
|---|
| 139 | ppat = pattern; /* Try the pattern again */
|
|---|
| 140 | pstr = s;
|
|---|
| 141 | while (*ppat == *pstr) {
|
|---|
| 142 | if (*++ppat == '\0') {
|
|---|
| 143 | /*
|
|---|
| 144 | * The pattern matched! Report success
|
|---|
| 145 | */
|
|---|
| 146 | return 1;
|
|---|
| 147 | }
|
|---|
| 148 | if (*++pstr == '\0') {
|
|---|
| 149 | /*
|
|---|
| 150 | * Not enough characters left in the string.
|
|---|
| 151 | * Report failure
|
|---|
| 152 | */
|
|---|
| 153 | return 0;
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | return 0; /* Failure */
|
|---|
| 158 | }
|
|---|
| 159 | # endif not BSD_REGEX
|
|---|
| 160 | # endif
|
|---|