New file. A test shell for list operations
Showing
1 changed file
with
337 additions
and
0 deletions
examples/listop.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #include <unistd.h> | ||
19 | #include <stdio.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <mailutils/argcv.h> | ||
22 | #include <mailutils/mailutils.h> | ||
23 | |||
24 | void | ||
25 | usage(int code) | ||
26 | { | ||
27 | printf ("usage: listop [item..]\n"); | ||
28 | exit (code); | ||
29 | } | ||
30 | |||
31 | void | ||
32 | lperror (char *text, int rc) | ||
33 | { | ||
34 | fprintf (stderr, "%s: %s\n", text, mu_strerror (rc)); | ||
35 | exit (1); | ||
36 | } | ||
37 | |||
38 | void | ||
39 | print (list_t list) | ||
40 | { | ||
41 | iterator_t itr; | ||
42 | int rc; | ||
43 | |||
44 | rc = iterator_create (&itr, list); | ||
45 | if (rc) | ||
46 | lperror ("iterator_create", rc); | ||
47 | |||
48 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) | ||
49 | { | ||
50 | char *text; | ||
51 | |||
52 | rc = iterator_current (itr, (void**) &text); | ||
53 | if (rc) | ||
54 | lperror ("iterator_current", rc); | ||
55 | printf ("%s\n", text); | ||
56 | } | ||
57 | iterator_destroy (&itr); | ||
58 | } | ||
59 | |||
60 | void | ||
61 | next (iterator_t itr, char *arg) | ||
62 | { | ||
63 | int skip = arg ? strtoul (arg, NULL, 0) : 1; | ||
64 | |||
65 | if (skip == 0) | ||
66 | fprintf (stderr, "next arg?\n"); | ||
67 | while (skip--) | ||
68 | iterator_next (itr); | ||
69 | } | ||
70 | |||
71 | void | ||
72 | delete (list_t list, int argc, char **argv) | ||
73 | { | ||
74 | int rc; | ||
75 | |||
76 | if (argc == 1) | ||
77 | { | ||
78 | fprintf (stderr, "del arg?\n"); | ||
79 | return; | ||
80 | } | ||
81 | |||
82 | while (--argc) | ||
83 | { | ||
84 | rc = list_remove (list, strdup (*++argv)); | ||
85 | if (rc) | ||
86 | fprintf (stderr, "list_remove(%s): %s\n", *argv, mu_strerror (rc)); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | void | ||
91 | add (list_t list, int argc, char **argv) | ||
92 | { | ||
93 | int rc; | ||
94 | |||
95 | if (argc == 1) | ||
96 | { | ||
97 | fprintf (stderr, "add arg?\n"); | ||
98 | return; | ||
99 | } | ||
100 | |||
101 | while (--argc) | ||
102 | { | ||
103 | rc = list_append (list, strdup (*++argv)); | ||
104 | if (rc) | ||
105 | fprintf (stderr, "list_append: %s\n", mu_strerror (rc)); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | void | ||
110 | prep (list_t list, int argc, char **argv) | ||
111 | { | ||
112 | int rc; | ||
113 | |||
114 | if (argc == 1) | ||
115 | { | ||
116 | fprintf (stderr, "add arg?\n"); | ||
117 | return; | ||
118 | } | ||
119 | |||
120 | while (--argc) | ||
121 | { | ||
122 | rc = list_prepend (list, strdup (*++argv)); | ||
123 | if (rc) | ||
124 | fprintf (stderr, "list_append: %s\n", mu_strerror (rc)); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | void | ||
129 | repl (list_t list, int argc, char **argv) | ||
130 | { | ||
131 | int rc; | ||
132 | |||
133 | if (argc != 3) | ||
134 | { | ||
135 | fprintf (stderr, "repl src dst?\n"); | ||
136 | return; | ||
137 | } | ||
138 | |||
139 | rc = list_replace (list, argv[1], strdup (argv[2])); | ||
140 | if (rc) | ||
141 | fprintf (stderr, "list_replace: %s\n", mu_strerror (rc)); | ||
142 | } | ||
143 | |||
144 | #define NITR 4 | ||
145 | |||
146 | void | ||
147 | iter (int *pnum, int argc, char **argv) | ||
148 | { | ||
149 | int n; | ||
150 | |||
151 | if (argc != 2) | ||
152 | { | ||
153 | fprintf (stderr, "iter num?\n"); | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | n = strtoul (argv[1], NULL, 0); | ||
158 | if (n < 0 || n >= NITR) | ||
159 | { | ||
160 | fprintf (stderr, "iter [0-3]?\n"); | ||
161 | return; | ||
162 | } | ||
163 | *pnum = n; | ||
164 | } | ||
165 | |||
166 | void | ||
167 | find (iterator_t itr, char *arg) | ||
168 | { | ||
169 | char *text; | ||
170 | |||
171 | if (!arg) | ||
172 | { | ||
173 | fprintf (stderr, "find item?\n"); | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | iterator_current (itr, (void**)&text); | ||
178 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) | ||
179 | { | ||
180 | char *item; | ||
181 | |||
182 | iterator_current (itr, (void**)&item); | ||
183 | if (strcmp (arg, item) == 0) | ||
184 | return; | ||
185 | } | ||
186 | |||
187 | fprintf (stderr, "%s not in list\n", arg); | ||
188 | |||
189 | for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) | ||
190 | { | ||
191 | char *item; | ||
192 | |||
193 | iterator_current (itr, (void**)&item); | ||
194 | if (strcmp (text, item) == 0) | ||
195 | return; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | void | ||
200 | help () | ||
201 | { | ||
202 | printf ("next [count]\n"); | ||
203 | printf ("first\n"); | ||
204 | printf ("find item\n"); | ||
205 | printf ("del item [item...]\n"); | ||
206 | printf ("add item [item...]\n"); | ||
207 | printf ("prep item [item...]\n"); | ||
208 | printf ("repl old_item new_item\n"); | ||
209 | printf ("print\n"); | ||
210 | printf ("quit\n"); | ||
211 | printf ("iter num\n"); | ||
212 | printf ("help\n"); | ||
213 | printf ("NUMBER\n"); | ||
214 | } | ||
215 | |||
216 | void | ||
217 | shell (list_t list) | ||
218 | { | ||
219 | int num = 0; | ||
220 | iterator_t itr[NITR]; | ||
221 | int rc; | ||
222 | |||
223 | for (num = 0; num < NITR; num++) | ||
224 | { | ||
225 | rc = iterator_create (&itr[num], list); | ||
226 | if (rc) | ||
227 | lperror ("iterator_create", rc); | ||
228 | iterator_first (itr[num]); | ||
229 | } | ||
230 | |||
231 | num = 0; | ||
232 | while (1) | ||
233 | { | ||
234 | char *text; | ||
235 | char buf[80]; | ||
236 | int argc; | ||
237 | char **argv; | ||
238 | |||
239 | rc = iterator_current (itr[num], (void**) &text); | ||
240 | if (rc) | ||
241 | lperror ("iterator_current", rc); | ||
242 | |||
243 | printf ("%d:(%s)> ", num, text ? text : "NULL"); | ||
244 | if (fgets (buf, sizeof buf, stdin) == NULL) | ||
245 | return; | ||
246 | |||
247 | rc = argcv_get (buf, "", "#", &argc, &argv); | ||
248 | if (rc) | ||
249 | lperror ("argcv_get", rc); | ||
250 | |||
251 | if (argc > 0) | ||
252 | { | ||
253 | if (strcmp (argv[0], "next") == 0) | ||
254 | next (itr[num], argv[1]); | ||
255 | else if (strcmp (argv[0], "first") == 0) | ||
256 | iterator_first (itr[num]); | ||
257 | else if (strcmp (argv[0], "del") == 0) | ||
258 | delete (list, argc, argv); | ||
259 | else if (strcmp (argv[0], "add") == 0) | ||
260 | add (list, argc, argv); | ||
261 | else if (strcmp (argv[0], "prep") == 0) | ||
262 | prep (list, argc, argv); | ||
263 | else if (strcmp (argv[0], "repl") == 0) | ||
264 | repl (list, argc, argv); | ||
265 | else if (strcmp (argv[0], "print") == 0) | ||
266 | print (list); | ||
267 | else if (strcmp (argv[0], "quit") == 0) | ||
268 | return; | ||
269 | else if (strcmp (argv[0], "iter") == 0) | ||
270 | iter (&num, argc, argv); | ||
271 | else if (strcmp (argv[0], "find") == 0) | ||
272 | find (itr[num], argv[1]); | ||
273 | else if (strcmp (argv[0], "help") == 0) | ||
274 | help (); | ||
275 | else if (argc == 1) | ||
276 | { | ||
277 | char *p; | ||
278 | size_t n = strtoul (argv[0], &p, 0); | ||
279 | if (*p != 0) | ||
280 | fprintf (stderr, "?\n"); | ||
281 | else | ||
282 | { | ||
283 | rc = list_get (list, n, (void**) &text); | ||
284 | if (rc) | ||
285 | fprintf (stderr, "list_get: %s\n", mu_strerror (rc)); | ||
286 | else | ||
287 | printf ("%s\n", text); | ||
288 | } | ||
289 | } | ||
290 | else | ||
291 | fprintf (stderr, "?\n"); | ||
292 | } | ||
293 | argcv_free (argc, argv); | ||
294 | } | ||
295 | } | ||
296 | |||
297 | static int | ||
298 | string_comp (const void *item, const void *value) | ||
299 | { | ||
300 | return strcmp (item, value); | ||
301 | } | ||
302 | |||
303 | int | ||
304 | main (int argc, char **argv) | ||
305 | { | ||
306 | list_t list; | ||
307 | int rc; | ||
308 | |||
309 | while ((rc = getopt (argc, argv, "h")) != EOF) | ||
310 | switch (rc) | ||
311 | { | ||
312 | case 'h': | ||
313 | usage (0); | ||
314 | |||
315 | default: | ||
316 | usage (1); | ||
317 | } | ||
318 | |||
319 | argc -= optind; | ||
320 | argv += optind; | ||
321 | |||
322 | rc = list_create (&list); | ||
323 | if (rc) | ||
324 | lperror ("list_create", rc); | ||
325 | list_set_comparator (list, string_comp); | ||
326 | |||
327 | while (argc--) | ||
328 | { | ||
329 | rc = list_append (list, *argv++); | ||
330 | if (rc) | ||
331 | lperror ("list_append", rc); | ||
332 | } | ||
333 | |||
334 | shell (list); | ||
335 | |||
336 | return 0; | ||
337 | } |
-
Please register or sign in to post a comment