improve behaviour under VPC, fixes from nicolas tittley.
[minix.git] / commands / yap / pattern.c
blob8bf19269523ce6dbcbaf4a39e072bfc0c16cf488
1 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
3 # ifndef lint
4 static char rcsid[] = "$Header$";
5 # endif not lint
7 # define _PATTERN_
9 # include "in_all.h"
10 # include "pattern.h"
11 # include "getline.h"
13 # if V8_REGEX
14 # include <regexp.h>
15 # endif V8_REGEX
18 * Interface to regular expression routines.
19 * Also: simple minded patterns without meta-characters.
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
32 # if USG_REGEX || V8_REGEX
34 * Compile a new pattern, but first free previous result.
37 char *
38 re_comp(s) char *s; {
40 if (!*s) {
42 * user wants previous pattern
44 return (char *) 0;
46 if (pattern) {
48 * there was a compiled pattern
50 free(pattern);
51 pattern = 0;
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
66 # if V8_REGEX
67 VOID
68 regerror(str) char *str; {
69 rc_error = str;
71 # endif V8_REGEX
74 * Search for compiled pattern in string "s". Return 0 if not found.
77 re_exec(s) char *s; {
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
90 # else
91 # ifndef BSD_REGEX
93 * In this case, simple minded pattern search without meta-characters
96 char *strcpy();
98 static char *pattern;
101 * re_comp : Just remember pattern.
104 char *
105 re_comp(s) char *s; {
107 if (!*s) {
109 * User wants previous pattern
111 if (!pattern) {
112 return "No previous regular expression";
114 return (char *) 0;
116 if (pattern) {
118 * Free old pattern
120 free(pattern);
122 pattern = alloc((unsigned) (strlen(s) + 1), 0);
123 (VOID) strcpy(pattern,s);
124 return (char *) 0;
128 * re-exec : Simple minded pattern matcher
131 re_exec(s) register char *s; {
133 register char *ppat, *pstr;
135 for (; *s; s++) {
137 * As long as there are characters ...
139 ppat = pattern; /* Try the pattern again */
140 pstr = s;
141 while (*ppat == *pstr) {
142 if (*++ppat == '\0') {
144 * The pattern matched! Report success
146 return 1;
148 if (*++pstr == '\0') {
150 * Not enough characters left in the string.
151 * Report failure
153 return 0;
157 return 0; /* Failure */
159 # endif not BSD_REGEX
160 # endif