1 /* $NetBSD: txtwalk.c,v 1.11 2004/11/11 20:14:02 dsl Exp $ */
4 * Copyright 1997 Piermont Information Systems Inc.
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, item list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, item list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of item software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Piermont Information Systems Inc.
21 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
22 * or promote products derived from item software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
40 * walk a text buffer, processing matched lines
42 * Written by Philip A. Nelson.
59 static int process(const struct lookfor
*, char *);
60 static int match(char *, const struct lookfor
*, size_t);
61 static int finddata(const struct lookfor
*, char *, struct data
*, size_t *);
64 * Walk the buffer, call match for each line.
67 walk(char *buffer
, size_t size
, const struct lookfor
*these
, size_t numthese
)
75 /* Ignore zero characters. */
76 if (*buffer
== '\0') {
80 /* Assume item starts a line. */
82 while (buffer
[len
] != '\n' && buffer
[len
] != '\0')
86 printf ("%5d: %s\n", line
, buffer
);
88 error
= match(buffer
, these
, numthese
);
100 * Match the current line with a string of interest.
101 * For each match in these, process the match.
104 match(char *line
, const struct lookfor
*these
, size_t numthese
)
106 size_t linelen
; /* Line length */
107 size_t patlen
; /* Pattern length */
108 size_t which
; /* Which pattern we are using */
111 linelen
= strlen(line
);
113 for (which
= 0; which
< numthese
; which
++) {
114 patlen
= strlen(these
[which
].head
);
115 if (linelen
< patlen
)
117 if (strncmp(these
[which
].head
, line
, patlen
) == 0) {
118 error
= process(&these
[which
], line
);
127 /* process the matched line. */
129 process(const struct lookfor
*item
, char *line
)
131 struct data found
[MAXDATA
];
138 if (finddata(item
, line
, found
, &numfound
)) {
140 printf("process: \"%s\"\n", line
);
141 for (i
= 0; i
< numfound
; i
++) {
142 printf ("\t%d: ", i
);
143 switch (found
[i
].what
) {
145 printf ("%d\n", found
[i
].u
.i_val
);
148 printf ("'%s'\n", found
[i
].u
.s_val
);
153 /* Process the stuff. */
154 switch (item
->todo
[0]) {
155 case 'a': /* Assign data */
158 while (*p
&& *p
!= '$')
163 i
= strtoul(p
, &np
, 10);
167 switch (found
[i
].what
) {
169 *((int *)item
->var
+j
)
173 strlcpy(*((char **)item
->var
+j
),
178 while (*p
&& *p
!= '$')
183 if (j
>= item
->nument
)
187 case 'c': /* Call a function with data. */
188 error
= (*item
->func
)(found
, numfound
);
198 * find the expected data. Return 1 if successful, return 0 if not.
199 * Successful means running into the end of the expect string before
200 * running out of line data or encountering other bad data.
202 * Side Effect -- sets numfound and found.
205 finddata(const struct lookfor
*item
, char *line
, struct data
*found
, size_t *numfound
)
213 for (fmt
= item
->fmt
; *fmt
; fmt
++) {
221 case '%': /* The char %. */
226 case 'i': /* Ignore characters */
230 while (*line
&& !isspace((unsigned char)*line
))
233 while (*line
&& *line
!= fmt
[1])
236 case 'd': /* Nextoken should be an integer. */
237 i
= strtoul(line
, &np
, 10);
240 found
[*numfound
].what
= INT
;
241 found
[(*numfound
)++].u
.i_val
= i
;
244 case 's': /* Matches a 'space' separated string. */
246 while (line
[len
] && !isspace((unsigned char)line
[len
])
247 && line
[len
] != fmt
[1])
249 found
[*numfound
].what
= STR
;
250 found
[(*numfound
)++].u
.s_val
= line
;
261 while (isspace((unsigned char)*line
))
273 /* Ran out of fmt. */