File: regex.patch

package info (click to toggle)
file 3.26-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 784 kB
  • ctags: 365
  • sloc: ansic: 2,469; sh: 152; makefile: 141; perl: 46
file content (203 lines) | stat: -rw-r--r-- 4,746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
diff -bBur file-3.20.1/apprentice.c zzz-file/apprentice.c
--- file-3.20.1/apprentice.c	Wed Jun 18 23:58:04 1997
+++ zzz-file/apprentice.c	Wed Mar 12 17:07:11 1997
@@ -45,7 +45,7 @@
 
 static int getvalue	__P((struct magic *, char **));
 static int hextoint	__P((int));
-static char *getstr	__P((char *, char *, int, int *));
+static char *getstr	__P((char *, char *, int, int *, char));
 static int parse	__P((char *, int *, int));
 static void eatsize	__P((char **));
 
@@ -162,6 +162,7 @@
 			v = (long) v;
 			break;
 		case STRING:
+		case REGEX:
 			break;
 		default:
 			magwarn("can't happen: m->type=%d\n",
@@ -276,6 +277,7 @@
 #define NLESHORT	7
 #define NLELONG		6
 #define NLEDATE		6
+#define NREGEX		5
 
 	if (*l == 'u') {
 		++l;
@@ -316,6 +318,10 @@
 	} else if (strncmp(l, "ledate", NLEDATE)==0) {
 		m->type = LEDATE;
 		l += NLEDATE;
+	} else if (strncmp(l, "regex", NREGEX)==0) {
+		m->type = REGEX;
+		m->reln = '=';
+		l += NREGEX;
 	} else {
 		magwarn("type %s invalid", l);
 		return -1;
@@ -329,7 +335,7 @@
 		m->mask = ~0L;
 	EATAB;
   
-	switch (*l) {
+	if (m->type!=REGEX) switch (*l) {	// regex ignores reln
 	case '>':
 	case '<':
 	/* Old-style anding: "0 byte &0x80 dynamically linked" */
@@ -402,8 +408,8 @@
 {
 	int slen;
 
-	if (m->type == STRING) {
-		*p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
+	if (m->type == STRING || m->type == REGEX) {
+		*p = getstr(*p, m->value.s, sizeof(m->value.s), &slen, m->type==REGEX);
 		m->vallen = slen;
 	} else
 		if (m->reln != 'x') {
@@ -420,18 +426,25 @@
  * Return updated scan pointer as function result.
  */
 static char *
-getstr(s, p, plen, slen)
+getstr(s, p, plen, slen, regex)
 register char	*s;
 register char	*p;
 int	plen, *slen;
+char	regex;
 {
 	char	*origs = s, *origp = p;
 	char	*pmax = p + plen - 1;
 	register int	c;
 	register int	val;
 
+	if (regex)
+	{
+		regex = *s++;
+		if (regex==0) goto out;
+	}
+
 	while ((c = *s++) != '\0') {
-		if (isspace((unsigned char) c))
+		if ((regex && (c==regex)) || (!regex && isspace((unsigned char) c)))
 			break;
 		if (p >= pmax) {
 			fprintf(stderr, "String too long: %s\n", origs);
@@ -519,6 +532,7 @@
 		} else
 			*p++ = (char)c;
 	}
+	if (regex) s++;
 out:
 	*p = '\0';
 	*slen = p - origp;
diff -bBur file-3.20.1/print.c zzz-file/print.c
--- file-3.20.1/print.c	Wed Jun 18 23:58:19 1997
+++ zzz-file/print.c	Wed Mar 12 17:09:30 1997
@@ -52,7 +52,7 @@
 	static char *typ[] = {   "invalid", "byte", "short", "invalid",
 				 "long", "string", "date", "beshort",
 				 "belong", "bedate", "leshort", "lelong",
-				 "ledate" };
+				 "ledate","regex" };
 	(void) fputc('[', stderr);
 	(void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
 		       m->offset);
@@ -84,6 +84,7 @@
 	    case BELONG:
 		    (void) fprintf(stderr, "%ld", m->value.l);
 		    break;
+	    case REGEX:
 	    case STRING:
 		    showstr(stderr, m->value.s, -1);
 		    break;
diff -bBur file-3.20.1/softmagic.c zzz-file/softmagic.c
--- file-3.20.1/softmagic.c	Thu Jun 19 00:36:54 1997
+++ zzz-file/softmagic.c	Wed Mar 12 18:45:11 1997
@@ -30,10 +30,11 @@
 #include <string.h>
 #include <time.h>
 #include <sys/types.h>
+#include <regex.h>
 
 #include "file.h"
 
-#if 0
+#ifndef	lint
 static char *moduleid = 
 	"@(#)$Id: softmagic.c,v 1.31 1996/06/22 22:04:22 christos Exp $";
 #endif	/* lint */
@@ -233,12 +234,17 @@
 		else {
 			/* nick@feedback.com.ar   Only print things before first newline */
 			char *qqq;
-			if ((qqq=strchr(p->s,'\n'))) *qqq='\0';
+			if (qqq=strchr(p->s,'\n')) *qqq='\0';
 			(void) printf(m->desc, p->s);
 			t = m->offset + strlen(p->s);
 		}
 		break;
 
+  	case REGEX:
+		(void) fputs(m->desc,stdout);
+		t = m->offset + 32;	/* FIXME! */
+		break;
+
 	case DATE:
 	case BEDATE:
 	case LEDATE:
@@ -247,7 +253,7 @@
 			*rt = '\0';
 		(void) printf(m->desc, pp);
 		t = m->offset + sizeof(time_t);
-		break;
+		return;
 	default:
 		error("invalid m->type (%d) in mprint().\n", m->type);
 		/*NOTREACHED*/
@@ -268,6 +274,7 @@
 	case SHORT:
 	case LONG:
 	case DATE:
+	case REGEX:
 		return 1;
 	case STRING:
 		{
@@ -376,6 +383,24 @@
 	return 1;
 }
 
+static int regex_match(const char *s, const char *pat)
+{
+	regex_t reg;
+	regmatch_t *rm;
+	int err;
+
+	err=regcomp(&reg,pat,REG_NEWLINE);
+	if (err)
+	{
+		error("Bad regex pattern: %s\n",pat);
+	}
+	rm=(regmatch_t*) malloc( sizeof(regmatch_t) * (reg.re_nsub+1) );
+	err=regexec(&reg,s,reg.re_nsub+1,rm,0);
+	regfree(&reg);
+	free(rm);
+	return err;
+}
+
 static int
 mcheck(p, m)
 union VALUETYPE* p;
@@ -428,6 +453,10 @@
 				if ((v = *b++ - *a++) != 0)
 					break;
 		}
+		break;
+	case REGEX:
+		l = 0;
+		v = regex_match(p->s, m->value.s);
 		break;
 	default:
 		error("invalid type %d in mcheck().\n", m->type);