Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndml_stzf.c
1 /*
2  * Copyright 2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *      Stanza files look about like this:
35  *              [stanza name line]
36  *              stanza body lines
37  *
38  *      These are used for config files.
39  */
40
41
42 #include "ndmlib.h"
43
44
45 int
46 ndmstz_getline (FILE *fp, char *buf, int n_buf)
47 {
48         int             c;
49         char *          p;
50
51   again:
52         c = getc (fp);
53         if (c == EOF)
54                 return EOF;
55
56         if (c == '[') {
57                 /* end-of-stanza */
58                 ungetc (c, fp);
59                 return -2;
60         }
61
62         if (c == '#') {
63                 /* comment */
64                 while ((c = getc(fp)) != EOF && c != '\n')
65                         continue;
66                 goto again;
67         }
68
69         ungetc (c, fp);
70         p = buf;
71         while ((c = getc(fp)) != EOF && c != '\n') {
72                 if (p < &buf[n_buf-1])
73                         *p++ = c;
74         }
75         *p = 0;
76         return p - buf;
77 }
78
79 int
80 ndmstz_getstanza (FILE *fp, char *buf, int n_buf)
81 {
82         int             c;
83         char *          p;
84
85   again:
86         c = getc (fp);
87         if (c == EOF)
88                 return EOF;
89
90         if (c == '\n')
91                 goto again;     /* blank line */
92
93         if (c != '[') {
94                 /* not a stanza header, eat line */
95                 while ((c = getc(fp)) != EOF && c != '\n')
96                         continue;
97                 goto again;
98         }
99
100         p = buf;
101         while ((c = getc(fp)) != EOF && c != '\n' && c != ']') {
102                 if (p < &buf[n_buf-1])
103                         *p++ = c;
104         }
105         *p = 0;
106
107         if (c == ']') {
108                 /* eat rest of line */
109                 while ((c = getc(fp)) != EOF && c != '\n')
110                         continue;
111         }
112
113         /* fp is left pointing to begining of first line */
114
115         return p - buf;
116 }
117
118 int
119 ndmstz_parse (char *buf, char *argv[], int max_argv)
120 {
121         char *          p = buf;
122         char *          q = buf;
123         int             inword = 0;
124         int             inquote = 0;
125         int             argc = 0;
126         int             c;
127
128         while ((c = *p++) != 0) {
129                 if (inquote) {
130                         if (c == inquote) {
131                                 inquote = 0;
132                         } else {
133                                 *q++ = c;
134                         }
135                         continue;
136                 }
137
138                 if (isspace(c)) {
139                         if (inword) {
140                                 *q++ = 0;
141                                 inword = 0;
142                         }
143                         continue;
144                 }
145
146                 if (!inword) {
147                         if (argc > max_argv-1)
148                                 break;
149                         argv[argc++] = q;
150                         inword = 1;
151                 }
152
153                 if (c == '"' || c == '\'') {
154                         inquote = c;
155                         continue;
156                 }
157
158                 *q++ = c;
159         }
160         if (inword)
161                 *q++ = 0;
162         argv[argc] = 0;
163
164         return argc;
165 }
166
167
168
169 #ifdef SELF_TEST
170
171 int
172 main (int ac, char *av[])
173 {
174         int             i, found, argc;
175         FILE *          fp;
176         char            buf[512];
177         char *          argv[100];
178
179         if (ac < 2) {
180                 printf ("bad usage\n");
181                 return 1;
182         }
183
184         fp = fopen (av[1], "r");
185         if (!fp) {
186                 perror (av[1]);
187                 return 2;
188         }
189
190         if (ac == 2) {
191                 while (ndmstz_getstanza (fp, buf, sizeof buf) >= 0)
192                         printf ("%s\n", buf);
193         } else {
194                 for (i = 2; i < ac; i++) {
195                         rewind (fp);
196                         found = 0;
197                         while (ndmstz_getstanza (fp, buf, sizeof buf) >= 0) {
198                                 if (strcmp (av[i], buf) == 0) {
199                                         found = 1;
200                                         break;
201                                 }
202                         }
203                         if (!found) {
204                                 printf ("Search for '%s' failed\n", av[i]);
205                                 continue;
206                         }
207                         printf ("'%s'\n", buf);
208                         printf ("========================================\n");
209                         while (ndmstz_getline (fp, buf, sizeof buf) >= 0) {
210                                 printf ("= %s", buf);
211                                 argc = ndmstz_parse (buf, argv, 100);
212                                 printf (" [%d]\n", argc);
213                         }
214                         printf ("========================================\n");
215                 }
216         }
217
218         fclose (fp);
219
220         return 0;
221 }
222
223 #endif /* SELF_TEST */