pattern.c

Go to the documentation of this file.
00001 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
00002 
00003 # ifndef lint
00004 static char rcsid[] = "$Header: /opt/proj/minix/cvsroot/src/commands/yap/pattern.c,v 1.1.1.1 2005/04/21 14:55:41 beng Exp $";
00005 # endif not lint
00006 
00007 # define _PATTERN_
00008 
00009 # include "in_all.h"
00010 # include "pattern.h"
00011 # include "getline.h"
00012 
00013 # if V8_REGEX
00014 # include <regexp.h>
00015 # endif V8_REGEX
00016 
00017 /*
00018  * Interface to regular expression routines.
00019  * Also: simple minded patterns without meta-characters.
00020  */
00021 
00022 # if USG_REGEX
00023 static char *pattern;           /* Pointer to compiled pattern */
00024 char *regcmp(), *regex();
00025 # endif USG_REGEX
00026 # if V8_REGEX
00027 static struct regexp *pattern;
00028 static char *rc_error;
00029 struct regexp *regcomp();
00030 # endif V8_REGEX
00031 
00032 # if USG_REGEX || V8_REGEX
00033 /*
00034  * Compile a new pattern, but first free previous result.
00035  */
00036 
00037 char *
00038 re_comp(s) char *s; {
00039 
00040         if (!*s) {
00041                 /*
00042                  * user wants previous pattern
00043                  */
00044                 return (char *) 0;
00045         }
00046         if (pattern) {
00047                 /*
00048                  * there was a compiled pattern
00049                  */
00050                 free(pattern);
00051                 pattern = 0;
00052         }
00053 # if USG_REGEX
00054         return (pattern = regcmp(s, (char *) 0)) ?
00055                 (char *) 0 :
00056                 "Error in pattern";
00057 # endif USG_REGEX
00058 # if V8_REGEX
00059         pattern = regcomp(s);
00060         if (pattern) return (char *) 0;
00061         if (rc_error) return rc_error;
00062         return "Error in pattern";
00063 # endif V8_REGEX
00064 }
00065 
00066 # if V8_REGEX
00067 VOID
00068 regerror(str) char *str; {
00069         rc_error = str;
00070 }
00071 # endif V8_REGEX
00072 
00073 /*
00074  * Search for compiled pattern in string "s". Return 0 if not found.
00075  */
00076 
00077 re_exec(s) char *s; {
00078 
00079 # if USG_REGEX
00080         return !(regex(pattern,s) == 0);
00081 # endif USG_REGEX
00082 # if V8_REGEX
00083 #  if _MINIX
00084         return regexec(pattern,s,1);
00085 #  else
00086         return regexec(pattern,s);
00087 #  endif
00088 # endif V8_REGEX
00089 }
00090 # else
00091 # ifndef BSD_REGEX
00092 /*
00093  * In this case, simple minded pattern search without meta-characters
00094  */
00095 
00096 char    *strcpy();
00097 
00098 static char *pattern;
00099 
00100 /*
00101  * re_comp : Just remember pattern.
00102  */
00103 
00104 char *
00105 re_comp(s) char *s; {
00106 
00107         if (!*s) {
00108                 /*
00109                  * User wants previous pattern
00110                  */
00111                 if (!pattern) {
00112                         return "No previous regular expression";
00113                 }
00114                 return (char *) 0;
00115         }
00116         if (pattern) {
00117                 /*
00118                  * Free old pattern
00119                  */
00120                 free(pattern);
00121         }
00122         pattern = alloc((unsigned) (strlen(s) + 1), 0);
00123         (VOID) strcpy(pattern,s);
00124         return (char *) 0;
00125 }
00126 
00127 /*
00128  * re-exec : Simple minded pattern matcher
00129  */
00130 
00131 re_exec(s) register char *s; {
00132 
00133         register char *ppat, *pstr;
00134 
00135         for (; *s; s++) {
00136                 /*
00137                  * As long as there are characters ...
00138                  */
00139                 ppat = pattern; /* Try the pattern again */
00140                 pstr = s;
00141                 while (*ppat == *pstr) {
00142                         if (*++ppat == '\0') {
00143                                 /*
00144                                  * The pattern matched! Report success
00145                                  */
00146                                 return 1;
00147                         }
00148                         if (*++pstr == '\0') {
00149                                 /*
00150                                  * Not enough characters left in the string.
00151                                  * Report failure
00152                                  */
00153                                 return 0;
00154                         }
00155                 }
00156         }
00157         return 0;               /* Failure */
00158 }
00159 # endif not BSD_REGEX
00160 # endif

Generated on Fri Apr 14 22:57:14 2006 for minix by  doxygen 1.4.6