Moved to mailbox
Showing
2 changed files
with
0 additions
and
869 deletions
lib/mu_argp.c
deleted
100644 → 0
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program 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 | This program 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 this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <getline.h> | ||
23 | #include <pwd.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include <syslog.h> | ||
28 | #include <unistd.h> | ||
29 | #include <xalloc.h> | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/stat.h> | ||
33 | |||
34 | #ifdef HAVE_STRINGS_H | ||
35 | # include <strings.h> | ||
36 | #endif | ||
37 | |||
38 | #include <mailutils/error.h> | ||
39 | #include <mailutils/errno.h> | ||
40 | #include <mailutils/mutil.h> | ||
41 | #include <mailutils/locker.h> | ||
42 | |||
43 | #include <argcv.h> | ||
44 | #include <mu_argp.h> | ||
45 | #include <mu_asprintf.h> | ||
46 | |||
47 | #ifdef HAVE_MYSQL | ||
48 | # include "../MySql/MySql.h" | ||
49 | #endif | ||
50 | |||
51 | #define ARG_LOG_FACILITY 1 | ||
52 | #define ARG_SQL_GETPWNAM 2 | ||
53 | #define ARG_SQL_GETPWUID 3 | ||
54 | #define ARG_SQL_GETPASS 4 | ||
55 | #define ARG_SQL_HOST 5 | ||
56 | #define ARG_SQL_USER 6 | ||
57 | #define ARG_SQL_PASSWD 7 | ||
58 | #define ARG_SQL_DB 8 | ||
59 | #define ARG_SQL_PORT 9 | ||
60 | #define ARG_PAM_SERVICE 10 | ||
61 | #define ARG_LOCK_FLAGS 11 | ||
62 | |||
63 | static struct argp_option mu_common_argp_options[] = | ||
64 | { | ||
65 | { NULL, 0, NULL, 0, "Common options:", 0}, | ||
66 | { NULL, 0, NULL, 0, NULL, 0 } | ||
67 | }; | ||
68 | |||
69 | /* Option to print the license. */ | ||
70 | static struct argp_option mu_license_argp_option[] = { | ||
71 | { "license", 'L', NULL, 0, "Print license and exit", -2 }, | ||
72 | { NULL, 0, NULL, 0, NULL, 0 } | ||
73 | }; | ||
74 | |||
75 | /* Options used by programs that access mailboxes. */ | ||
76 | static struct argp_option mu_mailbox_argp_option[] = { | ||
77 | {"mail-spool", 'm', "URL", 0, | ||
78 | "Use specified URL as a mailspool directory", 0}, | ||
79 | {"lock-flags", ARG_LOCK_FLAGS, "FLAGS", 0, | ||
80 | "Default locker flags (E=external, R=retry, T=time, P=pid)", 0}, | ||
81 | { NULL, 0, NULL, 0, NULL, 0 } | ||
82 | }; | ||
83 | |||
84 | /* Options used by programs that do address mapping. */ | ||
85 | static struct argp_option mu_address_argp_option[] = { | ||
86 | {"email-addr", 'E', "EMAIL", 0, | ||
87 | "Set current user's email address (default is loginname@defaultdomain)", 0}, | ||
88 | {"email-domain", 'D', "DOMAIN", 0, | ||
89 | "Set domain for unqualified user names (default is this host)", 0}, | ||
90 | { NULL, 0, NULL, 0, NULL, 0 } | ||
91 | }; | ||
92 | |||
93 | /* Options used by programs that log to syslog. */ | ||
94 | static struct argp_option mu_logging_argp_option[] = { | ||
95 | {"log-facility", ARG_LOG_FACILITY, "FACILITY", 0, | ||
96 | "Output logs to syslog FACILITY", 0}, | ||
97 | { NULL, 0, NULL, 0, NULL, 0 } | ||
98 | }; | ||
99 | |||
100 | /* Options used by programs that use extended authentication mechanisms. */ | ||
101 | static struct argp_option mu_auth_argp_option[] = { | ||
102 | #ifdef USE_LIBPAM | ||
103 | { "pam-service", ARG_PAM_SERVICE, "STRING", 0, | ||
104 | "Use STRING as PAM service name", 0}, | ||
105 | #endif | ||
106 | #ifdef HAVE_MYSQL | ||
107 | {"sql-getpwnam", ARG_SQL_GETPWNAM, "QUERY", 0, | ||
108 | "SQL query to retrieve a passwd entry based on username", 0}, | ||
109 | {"sql-getpwuid", ARG_SQL_GETPWUID, "QUERY", 0, | ||
110 | "SQL query to retrieve a passwd entry based on UID", 0}, | ||
111 | {"sql-getpass", ARG_SQL_GETPASS, "QUERY", 0, | ||
112 | "SQL query to retrieve a password from the database", 0}, | ||
113 | {"sql-host", ARG_SQL_HOST, "HOSTNAME", 0, | ||
114 | "Name or IP of MySQL server to connect to", 0}, | ||
115 | {"sql-user", ARG_SQL_USER, "NAME", 0, | ||
116 | "SQL user name", 0}, | ||
117 | {"sql-passwd", ARG_SQL_PASSWD, "STRING", 0, | ||
118 | "SQL connection password", 0}, | ||
119 | {"sql-db", ARG_SQL_DB, "STRING", 0, | ||
120 | "Name of the database to connect to", 0}, | ||
121 | {"sql-port", ARG_SQL_PORT, "NUMBER", 0, | ||
122 | "Port to use", 0}, | ||
123 | #endif | ||
124 | { NULL, 0, NULL, 0, NULL, 0 } | ||
125 | }; | ||
126 | |||
127 | /* Options used by programs that become daemons. */ | ||
128 | static struct argp_option mu_daemon_argp_option[] = { | ||
129 | {"daemon", 'd', "NUMBER", OPTION_ARG_OPTIONAL, | ||
130 | "Runs in daemon mode with a maximum of NUMBER children"}, | ||
131 | {"inetd", 'i', 0, 0, | ||
132 | "Run in inetd mode", 0}, | ||
133 | {"port", 'p', "PORT", 0, | ||
134 | "Listen on specified port number", 0}, | ||
135 | {"timeout", 't', "NUMBER", 0, | ||
136 | "Set idle timeout value to NUMBER seconds", 0}, | ||
137 | { NULL, 0, NULL, 0, NULL, 0 } | ||
138 | }; | ||
139 | |||
140 | static error_t mu_common_argp_parser __P((int key, char *arg, | ||
141 | struct argp_state *state)); | ||
142 | static error_t mu_daemon_argp_parser __P((int key, char *arg, | ||
143 | struct argp_state *state)); | ||
144 | |||
145 | struct argp mu_common_argp = { | ||
146 | mu_common_argp_options, | ||
147 | mu_common_argp_parser, | ||
148 | }; | ||
149 | |||
150 | struct argp_child mu_common_argp_child = { | ||
151 | &mu_common_argp, | ||
152 | 0, | ||
153 | NULL, | ||
154 | 0, | ||
155 | }; | ||
156 | |||
157 | struct argp mu_license_argp = { | ||
158 | mu_license_argp_option, | ||
159 | mu_common_argp_parser, | ||
160 | }; | ||
161 | |||
162 | struct argp_child mu_license_argp_child = { | ||
163 | &mu_license_argp, | ||
164 | 0, | ||
165 | NULL, | ||
166 | 0 | ||
167 | }; | ||
168 | |||
169 | struct argp mu_mailbox_argp = { | ||
170 | mu_mailbox_argp_option, | ||
171 | mu_common_argp_parser, | ||
172 | }; | ||
173 | |||
174 | struct argp_child mu_mailbox_argp_child = { | ||
175 | &mu_mailbox_argp, | ||
176 | 0, | ||
177 | NULL, | ||
178 | 0 | ||
179 | }; | ||
180 | |||
181 | struct argp mu_address_argp = { | ||
182 | mu_address_argp_option, | ||
183 | mu_common_argp_parser, | ||
184 | }; | ||
185 | |||
186 | struct argp_child mu_address_argp_child = { | ||
187 | &mu_address_argp, | ||
188 | 0, | ||
189 | NULL, | ||
190 | 0 | ||
191 | }; | ||
192 | |||
193 | struct argp mu_logging_argp = { | ||
194 | mu_logging_argp_option, | ||
195 | mu_common_argp_parser, | ||
196 | }; | ||
197 | |||
198 | struct argp_child mu_logging_argp_child = { | ||
199 | &mu_logging_argp, | ||
200 | 0, | ||
201 | NULL, | ||
202 | 0 | ||
203 | }; | ||
204 | |||
205 | struct argp mu_auth_argp = { | ||
206 | mu_auth_argp_option, | ||
207 | mu_common_argp_parser, | ||
208 | }; | ||
209 | |||
210 | struct argp_child mu_auth_argp_child = { | ||
211 | &mu_auth_argp, | ||
212 | 0, | ||
213 | "Authentication options", | ||
214 | 0 | ||
215 | }; | ||
216 | |||
217 | struct argp mu_daemon_argp = { | ||
218 | mu_daemon_argp_option, | ||
219 | mu_daemon_argp_parser, | ||
220 | }; | ||
221 | |||
222 | struct argp_child mu_daemon_argp_child = { | ||
223 | &mu_daemon_argp, | ||
224 | 0, | ||
225 | "Daemon configuration options", | ||
226 | 0 | ||
227 | }; | ||
228 | |||
229 | int log_facility = LOG_FACILITY; | ||
230 | int mu_argp_error_code = 1; | ||
231 | |||
232 | static int | ||
233 | parse_log_facility (const char *str) | ||
234 | { | ||
235 | int i; | ||
236 | static struct { | ||
237 | char *name; | ||
238 | int facility; | ||
239 | } syslog_kw[] = { | ||
240 | { "USER", LOG_USER }, | ||
241 | { "DAEMON", LOG_DAEMON }, | ||
242 | { "AUTH", LOG_AUTH }, | ||
243 | { "LOCAL0", LOG_LOCAL0 }, | ||
244 | { "LOCAL1", LOG_LOCAL1 }, | ||
245 | { "LOCAL2", LOG_LOCAL2 }, | ||
246 | { "LOCAL3", LOG_LOCAL3 }, | ||
247 | { "LOCAL4", LOG_LOCAL4 }, | ||
248 | { "LOCAL5", LOG_LOCAL5 }, | ||
249 | { "LOCAL6", LOG_LOCAL6 }, | ||
250 | { "LOCAL7", LOG_LOCAL7 }, | ||
251 | { "MAIL", LOG_MAIL } | ||
252 | }; | ||
253 | |||
254 | if (strncmp (str, "LOG_", 4) == 0) | ||
255 | str += 4; | ||
256 | |||
257 | for (i = 0; i < sizeof (syslog_kw) / sizeof (syslog_kw[0]); i++) | ||
258 | if (strcasecmp (syslog_kw[i].name, str) == 0) | ||
259 | return syslog_kw[i].facility; | ||
260 | fprintf (stderr, "unknown facility `%s'\n", str); | ||
261 | return LOG_FACILITY; | ||
262 | } | ||
263 | |||
264 | static char license_text[] = | ||
265 | " This program is free software; you can redistribute it and/or modify\n" | ||
266 | " it under the terms of the GNU General Public License as published by\n" | ||
267 | " the Free Software Foundation; either version 2, or (at your option)\n" | ||
268 | " any later version.\n" | ||
269 | "\n" | ||
270 | " This program is distributed in the hope that it will be useful,\n" | ||
271 | " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | ||
272 | " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | ||
273 | " GNU General Public License for more details.\n" | ||
274 | "\n" | ||
275 | " You should have received a copy of the GNU General Public License\n" | ||
276 | " along with this program; if not, write to the Free Software\n" | ||
277 | " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"; | ||
278 | |||
279 | #ifdef HAVE_MYSQL | ||
280 | char *sql_getpwnam_query; | ||
281 | char *sql_getpass_query; | ||
282 | char *sql_getpwuid_query; | ||
283 | char *sql_host = MHOST; | ||
284 | char *sql_user = MUSER; | ||
285 | char *sql_passwd = MPASS; | ||
286 | char *sql_db = MDB; | ||
287 | char *sql_socket = MSOCKET; | ||
288 | int sql_port = MPORT; | ||
289 | #endif | ||
290 | #ifdef USE_LIBPAM | ||
291 | char *pam_service = NULL; | ||
292 | #endif | ||
293 | |||
294 | static error_t | ||
295 | mu_common_argp_parser (int key, char *arg, struct argp_state *state) | ||
296 | { | ||
297 | int err = 0; | ||
298 | char *p; | ||
299 | |||
300 | switch (key) | ||
301 | { | ||
302 | /* common */ | ||
303 | case 'L': | ||
304 | printf ("License for %s:\n\n", argp_program_version); | ||
305 | printf ("%s", license_text); | ||
306 | exit (0); | ||
307 | |||
308 | /* mailbox */ | ||
309 | case 'm': | ||
310 | mu_path_maildir = arg; | ||
311 | break; | ||
312 | |||
313 | case ARG_LOCK_FLAGS: | ||
314 | { | ||
315 | int flags = 0; | ||
316 | for ( ; *arg; arg++) | ||
317 | { | ||
318 | switch (*arg) | ||
319 | { | ||
320 | case 'E': | ||
321 | flags |= MU_LOCKER_EXTERNAL; | ||
322 | break; | ||
323 | case 'R': | ||
324 | flags |= MU_LOCKER_RETRY; | ||
325 | break; | ||
326 | case 'T': | ||
327 | flags |= MU_LOCKER_TIME; | ||
328 | break; | ||
329 | case 'P': | ||
330 | flags |= MU_LOCKER_PID; | ||
331 | break; | ||
332 | default: | ||
333 | argp_error (state, "invalid lock flag '%c'", *arg); | ||
334 | } | ||
335 | } | ||
336 | locker_set_default_flags(flags); | ||
337 | } | ||
338 | break; | ||
339 | |||
340 | /* address */ | ||
341 | case 'E': | ||
342 | if ((err = mu_set_user_email(arg)) != 0) | ||
343 | { | ||
344 | argp_error (state, "invalid email-addr '%s': %s", | ||
345 | arg, mu_errstring(err)); | ||
346 | } | ||
347 | break; | ||
348 | |||
349 | case 'D': | ||
350 | if ((err = mu_set_user_email_domain(arg)) != 0) | ||
351 | { | ||
352 | argp_error (state, "invalid email-domain '%s': %s", | ||
353 | arg, mu_errstring(err)); | ||
354 | } | ||
355 | break; | ||
356 | |||
357 | /* log */ | ||
358 | case ARG_LOG_FACILITY: | ||
359 | log_facility = parse_log_facility (arg); | ||
360 | break; | ||
361 | |||
362 | /* authentication */ | ||
363 | #ifdef USE_LIBPAM | ||
364 | case ARG_PAM_SERVICE: | ||
365 | pam_service = arg; | ||
366 | break; | ||
367 | #endif | ||
368 | |||
369 | #ifdef HAVE_MYSQL | ||
370 | case ARG_SQL_GETPWNAM: | ||
371 | sql_getpwnam_query = arg; | ||
372 | break; | ||
373 | |||
374 | case ARG_SQL_GETPWUID: | ||
375 | sql_getpwuid_query = arg; | ||
376 | break; | ||
377 | |||
378 | case ARG_SQL_GETPASS: | ||
379 | sql_getpass_query = arg; | ||
380 | break; | ||
381 | |||
382 | case ARG_SQL_HOST: | ||
383 | sql_host = arg; | ||
384 | break; | ||
385 | |||
386 | case ARG_SQL_USER: | ||
387 | sql_user = arg; | ||
388 | break; | ||
389 | |||
390 | case ARG_SQL_PASSWD: | ||
391 | sql_passwd = arg; | ||
392 | break; | ||
393 | |||
394 | case ARG_SQL_DB: | ||
395 | sql_db = arg; | ||
396 | break; | ||
397 | |||
398 | case ARG_SQL_PORT: | ||
399 | sql_port = strtoul (arg, NULL, 0); | ||
400 | if (sql_port == 0) | ||
401 | { | ||
402 | sql_host = NULL; | ||
403 | sql_socket = arg; | ||
404 | } | ||
405 | break; | ||
406 | |||
407 | #endif | ||
408 | case ARGP_KEY_FINI: | ||
409 | p = mu_normalize_maildir (mu_path_maildir); | ||
410 | if (!p) | ||
411 | { | ||
412 | argp_error (state, "badly formed maildir: %s", mu_path_maildir); | ||
413 | } | ||
414 | mu_path_maildir = p; | ||
415 | break; | ||
416 | |||
417 | default: | ||
418 | return ARGP_ERR_UNKNOWN; | ||
419 | } | ||
420 | return 0; | ||
421 | } | ||
422 | |||
423 | static error_t | ||
424 | mu_daemon_argp_parser (int key, char *arg, struct argp_state *state) | ||
425 | { | ||
426 | struct daemon_param *p = state->input; | ||
427 | if (!p) | ||
428 | return ARGP_ERR_UNKNOWN; | ||
429 | switch (key) | ||
430 | { | ||
431 | case 'd': | ||
432 | p->mode = MODE_DAEMON; | ||
433 | if (arg) | ||
434 | { | ||
435 | size_t n = strtoul (arg, NULL, 10); | ||
436 | if (n > 0) | ||
437 | p->maxchildren = n; | ||
438 | } | ||
439 | break; | ||
440 | |||
441 | case 'i': | ||
442 | p->mode = MODE_INTERACTIVE; | ||
443 | break; | ||
444 | |||
445 | case 'p': | ||
446 | p->mode = MODE_DAEMON; | ||
447 | p->port = strtoul (arg, NULL, 10); | ||
448 | break; | ||
449 | |||
450 | case 't': | ||
451 | p->timeout = strtoul (arg, NULL, 10); | ||
452 | break; | ||
453 | |||
454 | default: | ||
455 | return ARGP_ERR_UNKNOWN; | ||
456 | } | ||
457 | return 0; | ||
458 | } | ||
459 | |||
460 | #ifndef MU_CONFIG_FILE | ||
461 | # define MU_CONFIG_FILE SYSCONFDIR "/mailutils.rc" | ||
462 | #endif | ||
463 | |||
464 | #ifndef MU_USER_CONFIG_FILE | ||
465 | # define MU_USER_CONFIG_FILE "~/.mailutils" | ||
466 | #endif | ||
467 | |||
468 | static int | ||
469 | member (const char *array[], const char *text, size_t len) | ||
470 | { | ||
471 | int i; | ||
472 | for (i = 0; array[i]; i++) | ||
473 | if (strncmp (array[i], text, len) == 0) | ||
474 | return 1; | ||
475 | return 0; | ||
476 | } | ||
477 | |||
478 | /* | ||
479 | Appends applicable options found in file NAME to argv. If progname | ||
480 | is NULL, all the options found are assumed to apply. Otherwise they | ||
481 | apply only if the line starts with ":something", and something is | ||
482 | found in the CAPA array, or the line starts with PROGNAME. | ||
483 | */ | ||
484 | void | ||
485 | read_rc (const char *progname, const char *name, const char *capa[], | ||
486 | int *argc, char ***argv) | ||
487 | { | ||
488 | FILE *fp; | ||
489 | char *linebuf = NULL; | ||
490 | char *buf = NULL; | ||
491 | size_t n = 0; | ||
492 | int x_argc = *argc; | ||
493 | char **x_argv = *argv; | ||
494 | char* rcfile = mu_tilde_expansion (name, "/", NULL); | ||
495 | |||
496 | if (!rcfile) | ||
497 | { | ||
498 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
499 | exit (1); | ||
500 | } | ||
501 | |||
502 | fp = fopen (rcfile, "r"); | ||
503 | if (!fp) | ||
504 | { | ||
505 | free(rcfile); | ||
506 | return; | ||
507 | } | ||
508 | |||
509 | while (getline (&buf, &n, fp) > 0) | ||
510 | { | ||
511 | char *kwp, *p; | ||
512 | int len; | ||
513 | |||
514 | for (kwp = buf; *kwp && isspace (*kwp); kwp++) | ||
515 | ; | ||
516 | |||
517 | if (*kwp == '#' || *kwp == 0) | ||
518 | continue; | ||
519 | |||
520 | len = strlen (kwp); | ||
521 | if (kwp[len-1] == '\n') | ||
522 | kwp[--len] = 0; | ||
523 | |||
524 | if (kwp[len-1] == '\\' || linebuf) | ||
525 | { | ||
526 | int cont; | ||
527 | |||
528 | if (kwp[len-1] == '\\') | ||
529 | { | ||
530 | kwp[--len] = 0; | ||
531 | cont = 1; | ||
532 | } | ||
533 | else | ||
534 | cont = 0; | ||
535 | |||
536 | if (!linebuf) | ||
537 | linebuf = calloc (len + 1, 1); | ||
538 | else | ||
539 | linebuf = realloc (linebuf, strlen (linebuf) + len + 1); | ||
540 | |||
541 | if (!linebuf) | ||
542 | { | ||
543 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
544 | exit (1); | ||
545 | } | ||
546 | |||
547 | strcpy (linebuf + strlen (linebuf), kwp); | ||
548 | if (cont) | ||
549 | continue; | ||
550 | kwp = linebuf; | ||
551 | } | ||
552 | |||
553 | len = 0; | ||
554 | if(progname) | ||
555 | { | ||
556 | for (p = kwp; *p && !isspace (*p); p++) | ||
557 | len++; | ||
558 | } | ||
559 | else | ||
560 | p = kwp; /* Use the whole line. */ | ||
561 | |||
562 | if (progname == NULL | ||
563 | || (kwp[0] == ':' && member (capa, kwp+1, len-1)) | ||
564 | || strncmp (progname, kwp, len) == 0 | ||
565 | ) | ||
566 | { | ||
567 | int i, n_argc = 0; | ||
568 | char **n_argv; | ||
569 | |||
570 | if (argcv_get (p, "", NULL, &n_argc, &n_argv)) | ||
571 | { | ||
572 | argcv_free (n_argc, n_argv); | ||
573 | if (linebuf) | ||
574 | free (linebuf); | ||
575 | linebuf = NULL; | ||
576 | continue; | ||
577 | } | ||
578 | x_argv = realloc (x_argv, | ||
579 | (x_argc + n_argc) * sizeof (x_argv[0])); | ||
580 | if (!x_argv) | ||
581 | { | ||
582 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
583 | exit (1); | ||
584 | } | ||
585 | |||
586 | for (i = 0; i < n_argc; i++) | ||
587 | x_argv[x_argc++] = n_argv[i]; | ||
588 | |||
589 | free (n_argv); | ||
590 | if (linebuf) | ||
591 | free (linebuf); | ||
592 | linebuf = NULL; | ||
593 | } | ||
594 | } | ||
595 | fclose (fp); | ||
596 | free(rcfile); | ||
597 | |||
598 | *argc = x_argc; | ||
599 | *argv = x_argv; | ||
600 | } | ||
601 | |||
602 | |||
603 | void | ||
604 | mu_create_argcv (const char *capa[], | ||
605 | int argc, char **argv, int *p_argc, char ***p_argv) | ||
606 | { | ||
607 | char *progname; | ||
608 | int x_argc; | ||
609 | char **x_argv; | ||
610 | int i; | ||
611 | int rcdir = 0; | ||
612 | |||
613 | progname = strrchr (argv[0], '/'); | ||
614 | if (progname) | ||
615 | progname++; | ||
616 | else | ||
617 | progname = argv[0]; | ||
618 | |||
619 | x_argv = malloc (sizeof (x_argv[0])); | ||
620 | if (!x_argv) | ||
621 | { | ||
622 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
623 | exit (1); | ||
624 | } | ||
625 | |||
626 | /* Add command name */ | ||
627 | x_argc = 0; | ||
628 | x_argv[x_argc] = argv[x_argc]; | ||
629 | x_argc++; | ||
630 | |||
631 | /* Add global config file. */ | ||
632 | read_rc (progname, MU_CONFIG_FILE, capa, &x_argc, &x_argv); | ||
633 | |||
634 | /* Look for per-user config files in ~/.mailutils/ or in ~/, but | ||
635 | not both. This allows mailutils' utilities to have their config | ||
636 | files segregated, if necessary. */ | ||
637 | |||
638 | { | ||
639 | struct stat s; | ||
640 | char* rcdirname = mu_tilde_expansion (MU_USER_CONFIG_FILE, "/", NULL); | ||
641 | |||
642 | if (!rcdirname) | ||
643 | { | ||
644 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
645 | exit (1); | ||
646 | } | ||
647 | if(stat(rcdirname, &s) == 0 && S_ISDIR(s.st_mode)) | ||
648 | rcdir = 1; | ||
649 | |||
650 | free(rcdirname); | ||
651 | } | ||
652 | |||
653 | /* Add per-user config file. */ | ||
654 | if(!rcdir) | ||
655 | { | ||
656 | read_rc (progname, MU_USER_CONFIG_FILE, capa, &x_argc, &x_argv); | ||
657 | } | ||
658 | else | ||
659 | { | ||
660 | char* userrc = NULL; | ||
661 | |||
662 | mu_asprintf(&userrc, "%s/mailutils", MU_USER_CONFIG_FILE); | ||
663 | |||
664 | if (!userrc) | ||
665 | { | ||
666 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
667 | exit (1); | ||
668 | } | ||
669 | read_rc (progname, userrc, capa, &x_argc, &x_argv); | ||
670 | |||
671 | free(userrc); | ||
672 | } | ||
673 | |||
674 | /* Add per-user, per-program config file. */ | ||
675 | { | ||
676 | char* progrc = NULL; | ||
677 | |||
678 | if(rcdir) | ||
679 | mu_asprintf(&progrc, "%s/%src", MU_USER_CONFIG_FILE, progname); | ||
680 | else | ||
681 | mu_asprintf(&progrc, "~/.mu.%src", progname); | ||
682 | |||
683 | if (!progrc) | ||
684 | { | ||
685 | fprintf (stderr, "%s: not enough memory\n", progname); | ||
686 | exit (1); | ||
687 | } | ||
688 | |||
689 | read_rc (NULL, progrc, capa, &x_argc, &x_argv); | ||
690 | free (progrc); | ||
691 | } | ||
692 | |||
693 | /* Finally, add the command line options */ | ||
694 | x_argv = realloc (x_argv, (x_argc + argc) * sizeof (x_argv[0])); | ||
695 | for (i = 1; i < argc; i++) | ||
696 | x_argv[x_argc++] = argv[i]; | ||
697 | |||
698 | x_argv[x_argc] = NULL; | ||
699 | |||
700 | *p_argc = x_argc; | ||
701 | *p_argv = x_argv; | ||
702 | } | ||
703 | |||
704 | struct argp_capa { | ||
705 | char *capability; | ||
706 | struct argp_child *child; | ||
707 | } mu_argp_capa[] = { | ||
708 | {"common", &mu_common_argp_child}, | ||
709 | {"license", &mu_license_argp_child}, | ||
710 | {"mailbox", &mu_mailbox_argp_child}, | ||
711 | {"address", &mu_address_argp_child}, | ||
712 | {"logging", &mu_logging_argp_child}, | ||
713 | {"auth", &mu_auth_argp_child}, | ||
714 | {"daemon", &mu_daemon_argp_child}, | ||
715 | {NULL,} | ||
716 | }; | ||
717 | |||
718 | static struct argp_child * | ||
719 | find_argp_child (const char *capa) | ||
720 | { | ||
721 | int i; | ||
722 | for (i = 0; mu_argp_capa[i].capability; i++) | ||
723 | if (strcmp (mu_argp_capa[i].capability, capa) == 0) | ||
724 | return mu_argp_capa[i].child; | ||
725 | return NULL; | ||
726 | } | ||
727 | |||
728 | static struct argp * | ||
729 | mu_build_argp (const struct argp *template, const char *capa[]) | ||
730 | { | ||
731 | int n; | ||
732 | int nchild; | ||
733 | struct argp_child *ap; | ||
734 | const struct argp_option *opt; | ||
735 | struct argp *argp; | ||
736 | int group = 0; | ||
737 | |||
738 | /* Count the capabilities. */ | ||
739 | for (n = 0; capa && capa[n]; n++) | ||
740 | ; | ||
741 | if (template->children) | ||
742 | for (; template->children[n].argp; n++) | ||
743 | ; | ||
744 | |||
745 | ap = calloc (n + 1, sizeof (*ap)); | ||
746 | if (!ap) | ||
747 | { | ||
748 | mu_error ("out of memory"); | ||
749 | abort (); | ||
750 | } | ||
751 | |||
752 | /* Copy the template's children. */ | ||
753 | nchild = 0; | ||
754 | if (template->children) | ||
755 | for (n = 0; template->children[n].argp; n++, nchild++) | ||
756 | ap[nchild] = template->children[n]; | ||
757 | |||
758 | /* Find next group number */ | ||
759 | for (opt = template->options; | ||
760 | opt && ((opt->name && opt->key) || opt->doc); opt++) | ||
761 | if (opt->group > group) | ||
762 | group = opt->group; | ||
763 | |||
764 | group++; | ||
765 | |||
766 | /* Append any capabilities to the children or options, as appropriate. */ | ||
767 | for (n = 0; capa && capa[n]; n++) | ||
768 | { | ||
769 | struct argp_child *child = find_argp_child (capa[n]); | ||
770 | if (!child) | ||
771 | { | ||
772 | mu_error ("INTERNAL ERROR: requested unknown argp capability %s", | ||
773 | capa[n]); | ||
774 | abort (); | ||
775 | } | ||
776 | ap[nchild] = *child; | ||
777 | ap[nchild].group = group++; | ||
778 | nchild++; | ||
779 | } | ||
780 | ap[nchild].argp = NULL; | ||
781 | |||
782 | /* Copy the template, and give it the expanded children. */ | ||
783 | argp = malloc (sizeof (*argp)); | ||
784 | if (!argp) | ||
785 | { | ||
786 | mu_error ("out of memory"); | ||
787 | abort (); | ||
788 | } | ||
789 | |||
790 | memcpy (argp, template, sizeof (*argp)); | ||
791 | |||
792 | argp->children = ap; | ||
793 | |||
794 | return argp; | ||
795 | } | ||
796 | |||
797 | error_t | ||
798 | mu_argp_parse(const struct argp *argp, | ||
799 | int *pargc, char **pargv[], | ||
800 | unsigned flags, | ||
801 | const char *capa[], | ||
802 | int *arg_index, | ||
803 | void *input) | ||
804 | { | ||
805 | error_t ret; | ||
806 | const struct argp argpnull = { 0 }; | ||
807 | |||
808 | if(!argp) | ||
809 | argp = &argpnull; | ||
810 | |||
811 | argp = mu_build_argp (argp, capa); | ||
812 | mu_create_argcv (capa, *pargc, *pargv, pargc, pargv); | ||
813 | ret = argp_parse (argp, *pargc, *pargv, flags, arg_index, input); | ||
814 | free ((void*) argp->children); | ||
815 | free ((void*) argp); | ||
816 | return ret; | ||
817 | } | ||
818 |
lib/mu_argp.h
deleted
100644 → 0
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program 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 | This program 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 this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | #ifndef MU_ARGP_H | ||
19 | #define MU_ARGP_H 1 | ||
20 | |||
21 | #include <mailutils/mailbox.h> | ||
22 | #include <argp.h> | ||
23 | |||
24 | #define MODE_INTERACTIVE 0 | ||
25 | #define MODE_DAEMON 1 | ||
26 | |||
27 | struct daemon_param { | ||
28 | int mode; | ||
29 | size_t maxchildren; | ||
30 | unsigned int port; | ||
31 | unsigned int timeout; | ||
32 | }; | ||
33 | |||
34 | #ifdef USE_LIBPAM | ||
35 | extern char *pam_service; | ||
36 | #endif | ||
37 | extern int log_facility; | ||
38 | extern int mu_argp_error_code; | ||
39 | |||
40 | extern void mu_create_argcv __P((const char *capa[], | ||
41 | int argc, char **argv, | ||
42 | int *p_argc, char ***p_argv)); | ||
43 | extern error_t mu_argp_parse __P((const struct argp *argp, | ||
44 | int *p_argc, char ***p_argv, | ||
45 | unsigned flags, | ||
46 | const char *capa[], | ||
47 | int *arg_index, | ||
48 | void *input)); | ||
49 | |||
50 | #endif | ||
51 |
-
Please register or sign in to post a comment