Use argp_parse instead of getopt_long.
Showing
4 changed files
with
213 additions
and
335 deletions
... | @@ -47,22 +47,26 @@ typedef struct utmp UTMP; | ... | @@ -47,22 +47,26 @@ typedef struct utmp UTMP; |
47 | 47 | ||
48 | #define MAX_TTY_SIZE (sizeof (PATH_TTY_PFX) + sizeof (((UTMP*)0)->ut_line)) | 48 | #define MAX_TTY_SIZE (sizeof (PATH_TTY_PFX) + sizeof (((UTMP*)0)->ut_line)) |
49 | 49 | ||
50 | static char short_options[] = "c:dhim:p:t:v"; | 50 | const char *argp_program_version = "comsatd (" PACKAGE ") " VERSION; |
51 | static struct option long_options[] = | 51 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; |
52 | static char doc[] = "GNU comsatd"; | ||
53 | |||
54 | static struct argp_option options[] = | ||
52 | { | 55 | { |
53 | {"config", required_argument, 0, 'c'}, | 56 | {"config", 'c', "FILE", 0, "Read configuration from FILE", 0}, |
54 | {"daemon", no_argument, 0, 'd'}, | 57 | { NULL, 0, NULL, 0, NULL, 0 } |
55 | {"help", no_argument, 0, 'h'}, | ||
56 | {"inetd", no_argument, 0, 'i'}, | ||
57 | {"maildir", required_argument, 0, 'm'}, | ||
58 | {"port", required_argument, 0, 'p'}, | ||
59 | {"timeout", required_argument, 0, 't'}, | ||
60 | {"version", no_argument, 0, 'v'}, | ||
61 | {0, 0, 0, 0} | ||
62 | }; | 58 | }; |
63 | 59 | ||
64 | #define MODE_INETD 0 | 60 | static error_t comsatd_parse_opt (int key, char *arg, struct argp_state *state); |
65 | #define MODE_DAEMON 1 | 61 | |
62 | static struct argp argp = { | ||
63 | options, | ||
64 | comsatd_parse_opt, | ||
65 | NULL, | ||
66 | doc, | ||
67 | mu_daemon_argp_child, | ||
68 | NULL, NULL | ||
69 | }; | ||
66 | 70 | ||
67 | #define SUCCESS 0 | 71 | #define SUCCESS 0 |
68 | #define NOT_HERE 1 | 72 | #define NOT_HERE 1 |
... | @@ -72,13 +76,16 @@ static struct option long_options[] = | ... | @@ -72,13 +76,16 @@ static struct option long_options[] = |
72 | # define MAXHOSTNAMELEN 64 | 76 | # define MAXHOSTNAMELEN 64 |
73 | #endif | 77 | #endif |
74 | 78 | ||
75 | int mode = MODE_INETD; | 79 | struct daemon_param daemon_param = { |
76 | int port = 512; /* Default biff port */ | 80 | MODE_INTERACTIVE, /* Start in interactive (inetd) mode */ |
77 | int timeout = 0; | 81 | 20, /* Default maximum number of children. |
82 | Currently unused */ | ||
83 | 512, /* Default biff port */ | ||
84 | 0, /* Default timeout */ | ||
85 | }; | ||
78 | int maxlines = 5; | 86 | int maxlines = 5; |
79 | char hostname[MAXHOSTNAMELEN]; | 87 | char hostname[MAXHOSTNAMELEN]; |
80 | const char *username; | 88 | const char *username; |
81 | const char *maildir = MU_PATH_MAILDIR; | ||
82 | 89 | ||
83 | static void comsat_init (void); | 90 | static void comsat_init (void); |
84 | static void comsat_daemon_init (void); | 91 | static void comsat_daemon_init (void); |
... | @@ -92,55 +99,35 @@ static void change_user (const char *user); | ... | @@ -92,55 +99,35 @@ static void change_user (const char *user); |
92 | 99 | ||
93 | static int xargc; | 100 | static int xargc; |
94 | static char **xargv; | 101 | static char **xargv; |
102 | char *config_file = NULL; | ||
95 | 103 | ||
96 | int | 104 | static error_t |
97 | main(int argc, char **argv) | 105 | comsatd_parse_opt (int key, char *arg, struct argp_state *state) |
98 | { | 106 | { |
99 | int c; | 107 | switch (key) |
100 | char *config_file = NULL; | ||
101 | |||
102 | while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) | ||
103 | != -1) | ||
104 | { | 108 | { |
105 | switch (c) | 109 | case ARGP_KEY_INIT: |
106 | { | 110 | state->child_inputs[0] = state->input; |
107 | case 'c': | ||
108 | config_file = optarg; | ||
109 | break; | ||
110 | |||
111 | case 'd': | ||
112 | mode = MODE_DAEMON; | ||
113 | break; | 111 | break; |
114 | 112 | ||
115 | case 'h': | 113 | case 'c': |
116 | help (); | 114 | config_file = arg; |
117 | /*NOTREACHED*/ | ||
118 | |||
119 | case 'i': | ||
120 | mode = MODE_INETD; | ||
121 | break; | ||
122 | |||
123 | case 'm': | ||
124 | maildir = optarg; | ||
125 | break; | 115 | break; |
126 | 116 | ||
127 | case 'p': | 117 | default: |
128 | port = strtoul (optarg, NULL, 10); | 118 | return ARGP_ERR_UNKNOWN; |
129 | break; | 119 | } |
120 | return 0; | ||
121 | } | ||
130 | 122 | ||
131 | case 't': | ||
132 | timeout = strtoul (optarg, NULL, 10); | ||
133 | break; | ||
134 | 123 | ||
135 | case 'v': | 124 | int |
136 | printf (IMPL " ("PACKAGE " " VERSION ")\n"); | 125 | main(int argc, char **argv) |
137 | exit (EXIT_SUCCESS); | 126 | { |
138 | break; | 127 | int c; |
139 | 128 | ||
140 | default: | 129 | mu_create_argcv (argc, argv, &argc, &argv); |
141 | exit (EXIT_FAILURE); | 130 | argp_parse (&argp, argc, argv, 0, 0, &daemon_param); |
142 | } | ||
143 | } | ||
144 | 131 | ||
145 | maildir = mu_normalize_maildir (maildir); | 132 | maildir = mu_normalize_maildir (maildir); |
146 | if (!maildir) | 133 | if (!maildir) |
... | @@ -149,7 +136,7 @@ main(int argc, char **argv) | ... | @@ -149,7 +136,7 @@ main(int argc, char **argv) |
149 | exit (1); | 136 | exit (1); |
150 | } | 137 | } |
151 | 138 | ||
152 | if (timeout > 0 && mode == MODE_DAEMON) | 139 | if (daemon_param.timeout > 0 && daemon_param.mode == MODE_DAEMON) |
153 | { | 140 | { |
154 | fprintf (stderr, "--timeout and --daemon are incompatible\n"); | 141 | fprintf (stderr, "--timeout and --daemon are incompatible\n"); |
155 | exit (EXIT_FAILURE); | 142 | exit (EXIT_FAILURE); |
... | @@ -157,7 +144,7 @@ main(int argc, char **argv) | ... | @@ -157,7 +144,7 @@ main(int argc, char **argv) |
157 | 144 | ||
158 | comsat_init (); | 145 | comsat_init (); |
159 | 146 | ||
160 | if (mode == MODE_DAEMON) | 147 | if (daemon_param.mode == MODE_DAEMON) |
161 | { | 148 | { |
162 | /* Preserve invocation arguments */ | 149 | /* Preserve invocation arguments */ |
163 | xargc = argc; | 150 | xargc = argc; |
... | @@ -174,8 +161,8 @@ main(int argc, char **argv) | ... | @@ -174,8 +161,8 @@ main(int argc, char **argv) |
174 | 161 | ||
175 | chdir ("/"); | 162 | chdir ("/"); |
176 | 163 | ||
177 | if (mode == MODE_DAEMON) | 164 | if (daemon_param.mode == MODE_DAEMON) |
178 | comsat_daemon (port); | 165 | comsat_daemon (daemon_param.port); |
179 | else | 166 | else |
180 | c = comsat_main (0); | 167 | c = comsat_main (0); |
181 | 168 | ... | ... |
... | @@ -16,112 +16,87 @@ | ... | @@ -16,112 +16,87 @@ |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
17 | 17 | ||
18 | #include "imap4d.h" | 18 | #include "imap4d.h" |
19 | |||
20 | #ifdef HAVE_MYSQL | 19 | #ifdef HAVE_MYSQL |
21 | # include "../MySql/MySql.h" | 20 | # include "../MySql/MySql.h" |
22 | #endif | 21 | #endif |
23 | 22 | ||
24 | FILE *ofile; | 23 | FILE *ofile; |
25 | unsigned int timeout = 1800; /* RFC2060: 30 minutes, if enable. */ | ||
26 | mailbox_t mbox; | 24 | mailbox_t mbox; |
27 | char *homedir; | 25 | char *homedir; |
28 | char *maildir = MU_PATH_MAILDIR; | ||
29 | int state = STATE_NONAUTH; | 26 | int state = STATE_NONAUTH; |
30 | 27 | ||
28 | struct daemon_param daemon_param = { | ||
29 | MODE_INTERACTIVE, /* Start in interactive (inetd) mode */ | ||
30 | 20, /* Default maximum number of children */ | ||
31 | 143, /* Standard IMAP4 port */ | ||
32 | 1800 /* RFC2060: 30 minutes. */ | ||
33 | }; | ||
34 | |||
31 | /* Number of child processes. */ | 35 | /* Number of child processes. */ |
32 | volatile size_t children; | 36 | volatile size_t children; |
33 | 37 | ||
34 | static struct option long_options[] = | 38 | const char *argp_program_version = "imap4d (" PACKAGE ") " VERSION; |
39 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; | ||
40 | static char doc[] = "GNU imap4d -- the IMAP4D daemon"; | ||
41 | |||
42 | static struct argp_option options[] = | ||
35 | { | 43 | { |
36 | {"daemon", optional_argument, 0, 'd'}, | 44 | {"other-namespace", 'O', "PATHLIST", 0, |
37 | {"help", no_argument, 0, 'h'}, | 45 | "set the `other' namespace", 0}, |
38 | {"inetd", no_argument, 0, 'i'}, | 46 | {"shared-namespace", 'S', "PATHLIST", 0, |
39 | {"maildir", required_argument, 0, 'm'}, | 47 | "set the `shared' namespace", 0}, |
40 | {"port", required_argument, 0, 'p'}, | 48 | { NULL, 0, NULL, 0, NULL, 0 } |
41 | {"other-namespace", required_argument, 0, 'O'}, | ||
42 | {"shared-namespace", required_argument, 0, 'S'}, | ||
43 | {"timeout", required_argument, 0, 't'}, | ||
44 | {"version", no_argument, 0, 'v'}, | ||
45 | {0, 0, 0, 0} | ||
46 | }; | 49 | }; |
47 | 50 | ||
48 | const char *short_options ="d::him:p:t:vO:P:S:"; | 51 | static error_t imap4d_parse_opt (int key, char *arg, struct argp_state *state); |
52 | |||
53 | static struct argp argp = { | ||
54 | options, | ||
55 | imap4d_parse_opt, | ||
56 | NULL, | ||
57 | doc, | ||
58 | mu_daemon_argp_child, | ||
59 | NULL, NULL | ||
60 | }; | ||
49 | 61 | ||
50 | static int imap4d_mainloop __P ((int, int)); | 62 | static int imap4d_mainloop __P ((int, int)); |
51 | static void imap4d_daemon_init __P ((void)); | 63 | static void imap4d_daemon_init __P ((void)); |
52 | static void imap4d_daemon __P ((unsigned int, unsigned int)); | 64 | static void imap4d_daemon __P ((unsigned int, unsigned int)); |
53 | static int imap4d_mainloop __P ((int, int)); | 65 | static int imap4d_mainloop __P ((int, int)); |
54 | static void imap4d_usage __P ((char *)); | ||
55 | 66 | ||
56 | #ifndef DEFMAXCHILDREN | 67 | static error_t |
57 | # define DEFMAXCHILDREN 20 /* Default maximum number of children */ | 68 | imap4d_parse_opt (int key, char *arg, struct argp_state *state) |
58 | #endif | ||
59 | |||
60 | int | ||
61 | main (int argc, char **argv) | ||
62 | { | 69 | { |
63 | struct group *gr; | 70 | switch (key) |
64 | static int mode = INTERACTIVE; | ||
65 | size_t maxchildren = DEFMAXCHILDREN; | ||
66 | int c = 0; | ||
67 | int status = EXIT_SUCCESS; | ||
68 | unsigned int port; | ||
69 | |||
70 | port = 143; /* Default IMAP4 port. */ | ||
71 | timeout = 1800; /* RFC2060: 30 minutes, if enable. */ | ||
72 | state = STATE_NONAUTH; /* Starting state in non-auth. */ | ||
73 | |||
74 | while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) | ||
75 | != -1) | ||
76 | { | ||
77 | switch (c) | ||
78 | { | 71 | { |
79 | case 'd': | 72 | case ARGP_KEY_INIT: |
80 | mode = DAEMON; | 73 | state->child_inputs[0] = state->input; |
81 | if (optarg) | ||
82 | maxchildren = strtoul (optarg, NULL, 10); | ||
83 | if (maxchildren == 0) | ||
84 | maxchildren = DEFMAXCHILDREN; | ||
85 | break; | ||
86 | |||
87 | case 'h': | ||
88 | imap4d_usage (argv[0]); | ||
89 | break; | ||
90 | |||
91 | case 'i': | ||
92 | mode = INTERACTIVE; | ||
93 | break; | ||
94 | |||
95 | case 'm': | ||
96 | maildir = optarg; | ||
97 | break; | ||
98 | |||
99 | case 'p': | ||
100 | mode = DAEMON; | ||
101 | port = strtoul (optarg, NULL, 10); | ||
102 | break; | 74 | break; |
103 | 75 | ||
104 | case 'O': | 76 | case 'O': |
105 | set_namespace (NS_OTHER, optarg); | 77 | set_namespace (NS_OTHER, arg); |
106 | break; | 78 | break; |
107 | 79 | ||
108 | case 'S': | 80 | case 'S': |
109 | set_namespace (NS_SHARED, optarg); | 81 | set_namespace (NS_SHARED, arg); |
110 | break; | ||
111 | |||
112 | case 't': | ||
113 | timeout = strtoul (optarg, NULL, 10); | ||
114 | break; | ||
115 | |||
116 | case 'v': | ||
117 | printf ("GNU imap4 daemon" "("PACKAGE " " VERSION ")\n"); | ||
118 | exit (0); | ||
119 | break; | 82 | break; |
120 | 83 | ||
121 | default: | 84 | default: |
122 | break; | 85 | return ARGP_ERR_UNKNOWN; |
123 | } | ||
124 | } | 86 | } |
87 | return 0; | ||
88 | } | ||
89 | |||
90 | int | ||
91 | main (int argc, char **argv) | ||
92 | { | ||
93 | struct group *gr; | ||
94 | int status = EXIT_SUCCESS; | ||
95 | |||
96 | state = STATE_NONAUTH; /* Starting state in non-auth. */ | ||
97 | |||
98 | mu_create_argcv (argc, argv, &argc, &argv); | ||
99 | argp_parse (&argp, argc, argv, 0, 0, &daemon_param); | ||
125 | 100 | ||
126 | maildir = mu_normalize_maildir (maildir); | 101 | maildir = mu_normalize_maildir (maildir); |
127 | if (!maildir) | 102 | if (!maildir) |
... | @@ -172,7 +147,7 @@ main (int argc, char **argv) | ... | @@ -172,7 +147,7 @@ main (int argc, char **argv) |
172 | /*signal (SIGPIPE, SIG_IGN); */ | 147 | /*signal (SIGPIPE, SIG_IGN); */ |
173 | signal (SIGABRT, imap4d_signal); | 148 | signal (SIGABRT, imap4d_signal); |
174 | 149 | ||
175 | if (mode == DAEMON) | 150 | if (daemon_param.mode == MODE_DAEMON) |
176 | imap4d_daemon_init (); | 151 | imap4d_daemon_init (); |
177 | else | 152 | else |
178 | { | 153 | { |
... | @@ -181,7 +156,7 @@ main (int argc, char **argv) | ... | @@ -181,7 +156,7 @@ main (int argc, char **argv) |
181 | } | 156 | } |
182 | 157 | ||
183 | /* Set up for syslog. */ | 158 | /* Set up for syslog. */ |
184 | openlog ("gnu-imap4d", LOG_PID, LOG_FACILITY); | 159 | openlog ("gnu-imap4d", LOG_PID, log_facility); |
185 | 160 | ||
186 | /* Redirect any stdout error from the library to syslog, they | 161 | /* Redirect any stdout error from the library to syslog, they |
187 | should not go to the client. */ | 162 | should not go to the client. */ |
... | @@ -190,8 +165,8 @@ main (int argc, char **argv) | ... | @@ -190,8 +165,8 @@ main (int argc, char **argv) |
190 | umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */ | 165 | umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */ |
191 | 166 | ||
192 | /* Actually run the daemon. */ | 167 | /* Actually run the daemon. */ |
193 | if (mode == DAEMON) | 168 | if (daemon_param.mode == MODE_DAEMON) |
194 | imap4d_daemon (maxchildren, port); | 169 | imap4d_daemon (daemon_param.maxchildren, daemon_param.port); |
195 | /* exit (0) -- no way out of daemon except a signal. */ | 170 | /* exit (0) -- no way out of daemon except a signal. */ |
196 | else | 171 | else |
197 | status = imap4d_mainloop (fileno (stdin), fileno (stdout)); | 172 | status = imap4d_mainloop (fileno (stdin), fileno (stdout)); |
... | @@ -353,30 +328,5 @@ imap4d_daemon (unsigned int maxchildren, unsigned int port) | ... | @@ -353,30 +328,5 @@ imap4d_daemon (unsigned int maxchildren, unsigned int port) |
353 | } | 328 | } |
354 | } | 329 | } |
355 | 330 | ||
356 | /* Prints out usage information and exits the program */ | ||
357 | |||
358 | static void | ||
359 | imap4d_usage (char *argv0) | ||
360 | { | ||
361 | printf ("Usage: %s [OPTIONS]\n", argv0); | ||
362 | printf ("Runs the GNU IMAP4 daemon.\n\n"); | ||
363 | printf (" -d, --daemon[=MAXCHILDREN] runs in daemon mode with a maximum\n"); | ||
364 | printf (" of MAXCHILDREN child processes\n"); | ||
365 | printf (" MAXCHILDREN defaults to %d\n", | ||
366 | DEFMAXCHILDREN); | ||
367 | printf (" -h, --help display this help and exit\n"); | ||
368 | printf (" -i, --inetd runs in inetd mode (default)\n"); | ||
369 | printf (" -p, --port=PORT specifies port to listen on, implies -d\n" | ||
370 | ); | ||
371 | printf (" defaults to 143, which need not be specified\n"); | ||
372 | printf (" -m, --maildir=PATH set path to the mailspool directory\n"); | ||
373 | printf (" -O, --other-namespace=PATHLIST sets the `other' namespace\n"); | ||
374 | printf (" -S, --shared-namespace=PATHLIST sets the `shared' namespace\n"); | ||
375 | printf (" -t, --timeout=TIMEOUT sets idle timeout to TIMEOUT seconds\n"); | ||
376 | printf (" TIMEOUT default is 1800 (30 minutes)\n"); | ||
377 | printf (" -v, --version display version information and exit\n"); | ||
378 | printf ("\nReport bugs to bug-mailutils@gnu.org\n"); | ||
379 | exit (0); | ||
380 | } | ||
381 | 331 | ||
382 | 332 | ... | ... |
... | @@ -22,7 +22,6 @@ int multiple_delivery; | ... | @@ -22,7 +22,6 @@ int multiple_delivery; |
22 | int ex_quota_tempfail; | 22 | int ex_quota_tempfail; |
23 | int exit_code = EX_OK; | 23 | int exit_code = EX_OK; |
24 | uid_t uid; | 24 | uid_t uid; |
25 | char *maildir = MU_PATH_MAILDIR; | ||
26 | char *quotadbname = NULL; | 25 | char *quotadbname = NULL; |
27 | int lock_timeout = 300; | 26 | int lock_timeout = 300; |
28 | 27 | ||
... | @@ -40,47 +39,72 @@ void guess_retval (int ec); | ... | @@ -40,47 +39,72 @@ void guess_retval (int ec); |
40 | void mailer_err (char *fmt, ...); | 39 | void mailer_err (char *fmt, ...); |
41 | void notify_biff (mailbox_t mbox, char *name, size_t size); | 40 | void notify_biff (mailbox_t mbox, char *name, size_t size); |
42 | 41 | ||
43 | char short_opts[] = "hf:Llm:q:r:s:x::vW;"; | 42 | const char *argp_program_version = "mail.local (" PACKAGE ") " VERSION; |
44 | 43 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; | |
45 | static struct option long_opts[] = { | 44 | static char doc[] = "GNU mail.local -- the local MDA"; |
46 | { "ex-multiple-delivery-success", no_argument, &multiple_delivery, 1 }, | 45 | static char args_doc[] = "recipient [recipient ...]"; |
47 | { "ex-quota-tempfail", no_argument, &ex_quota_tempfail, 1 }, | ||
48 | { "from", required_argument, 0, 'f' }, | ||
49 | { "help", no_argument, 0, 'h' }, | ||
50 | { "license", no_argument, 0, 'L' }, | ||
51 | { "maildir", required_argument, 0, 'm' }, | ||
52 | { "quota-db", required_argument, 0, 'q' }, | ||
53 | { "source", required_argument, 0, 's' }, | ||
54 | { "timeout", required_argument, 0, 't' }, | ||
55 | { "debug", optional_argument, 0, 'x' }, | ||
56 | { "version", no_argument, 0, 'v' }, | ||
57 | { 0, 0, 0, 0 } | ||
58 | }; | ||
59 | 46 | ||
47 | #define ARG_MULTIPLE_DELIVERY 1 | ||
48 | #define ARG_QUOTA_TEMPFAIL 2 | ||
60 | 49 | ||
61 | int | 50 | static struct argp_option options[] = |
62 | main (int argc, char *argv[]) | ||
63 | { | 51 | { |
64 | int c; | 52 | { "ex-multiple-delivery-success", ARG_MULTIPLE_DELIVERY, NULL, 0, |
65 | FILE *fp; | 53 | "Don't return errors when delivering to multiple recipients", 0 }, |
66 | char *from = NULL; | 54 | { "ex-quota-tempfail", ARG_QUOTA_TEMPFAIL, NULL, 0, |
67 | char *progfile_pattern = NULL; | 55 | "Return temporary failure if disk or mailbox quota is exceeded", 0 }, |
68 | char *tempfile = NULL; | 56 | { "from", 'f', "EMAIL", 0, |
57 | "Specify the sender's name" }, | ||
58 | { NULL, 'r', NULL, OPTION_ALIAS, NULL }, | ||
59 | #ifdef USE_DBM | ||
60 | { "quota-db", 'q', "FILE", 0, | ||
61 | "Specify path to quota database", 0 }, | ||
62 | #endif | ||
63 | #ifdef WITH_GUILE | ||
64 | { "source", 's', "PATTERN", 0, | ||
65 | "Set name pattern for user-defined mail filters", 0 }, | ||
66 | #endif | ||
67 | { "debug", 'x', | ||
68 | #ifdef WITH_GUILE | ||
69 | "{NUMBER|guile}", | ||
70 | #else | ||
71 | "NUMBER", | ||
72 | #endif | ||
73 | 0, | ||
74 | "Enable debugging", 0 }, | ||
75 | { "timeout", 't', "NUMBER", 0, | ||
76 | "Set timeout for acquiring the lockfile" }, | ||
69 | 77 | ||
70 | /* Preparative work: close inherited fds, force a reasonable umask | 78 | { NULL, 0, NULL, 0, NULL, 0 } |
71 | and prepare a logging. */ | 79 | }; |
72 | close_fds (); | ||
73 | umask (0077); | ||
74 | 80 | ||
75 | openlog ("mail.local", LOG_PID, LOG_FACILITY); | 81 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
76 | mu_error_set_print (mu_syslog_error_printer); | ||
77 | 82 | ||
78 | uid = getuid (); | 83 | static struct argp argp = { |
79 | while ((c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != EOF) | 84 | options, |
80 | switch (c) | 85 | parse_opt, |
86 | args_doc, | ||
87 | doc, | ||
88 | mu_common_argp_child, | ||
89 | NULL, NULL | ||
90 | }; | ||
91 | |||
92 | char *from = NULL; | ||
93 | char *progfile_pattern = NULL; | ||
94 | |||
95 | static error_t | ||
96 | parse_opt (int key, char *arg, struct argp_state *state) | ||
97 | { | ||
98 | switch (key) | ||
81 | { | 99 | { |
82 | case 0: /* option already handled */ | 100 | case ARG_MULTIPLE_DELIVERY: |
101 | multiple_delivery = 1; | ||
102 | break; | ||
103 | |||
104 | case ARG_QUOTA_TEMPFAIL: | ||
105 | ex_quota_tempfail = 1; | ||
83 | break; | 106 | break; |
107 | |||
84 | case 'r': | 108 | case 'r': |
85 | case 'f': | 109 | case 'f': |
86 | if (from != NULL) | 110 | if (from != NULL) |
... | @@ -88,24 +112,12 @@ main (int argc, char *argv[]) | ... | @@ -88,24 +112,12 @@ main (int argc, char *argv[]) |
88 | mu_error ("multiple --from options"); | 112 | mu_error ("multiple --from options"); |
89 | return EX_USAGE; | 113 | return EX_USAGE; |
90 | } | 114 | } |
91 | from = optarg; | 115 | from = arg; |
92 | break; | ||
93 | |||
94 | case 'h': | ||
95 | print_help (); | ||
96 | break; | ||
97 | |||
98 | case 'L': | ||
99 | print_license (); | ||
100 | break; | ||
101 | |||
102 | case 'm': | ||
103 | maildir = optarg; | ||
104 | break; | 116 | break; |
105 | 117 | ||
106 | #ifdef USE_DBM | 118 | #ifdef USE_DBM |
107 | case 'q': | 119 | case 'q': |
108 | quotadbname = optarg; | 120 | quotadbname = arg; |
109 | break; | 121 | break; |
110 | #endif | 122 | #endif |
111 | 123 | ||
... | @@ -138,16 +150,37 @@ main (int argc, char *argv[]) | ... | @@ -138,16 +150,37 @@ main (int argc, char *argv[]) |
138 | } | 150 | } |
139 | break; | 151 | break; |
140 | 152 | ||
141 | case 'v': | ||
142 | print_version (); | ||
143 | break; | ||
144 | |||
145 | default: | 153 | default: |
146 | return EX_USAGE; | 154 | return ARGP_ERR_UNKNOWN; |
155 | |||
156 | case ARGP_KEY_ERROR: | ||
157 | exit (EX_USAGE); | ||
147 | } | 158 | } |
159 | return 0; | ||
160 | } | ||
148 | 161 | ||
149 | argc -= optind; | 162 | int |
150 | argv += optind; | 163 | main (int argc, char *argv[]) |
164 | { | ||
165 | FILE *fp; | ||
166 | char *tempfile = NULL; | ||
167 | int arg_index; | ||
168 | |||
169 | /* Preparative work: close inherited fds, force a reasonable umask | ||
170 | and prepare a logging. */ | ||
171 | close_fds (); | ||
172 | umask (0077); | ||
173 | |||
174 | mu_create_argcv (argc, argv, &argc, &argv); | ||
175 | argp_parse (&argp, argc, argv, 0, &arg_index, NULL); | ||
176 | |||
177 | openlog ("mail.local", LOG_PID, log_facility); | ||
178 | mu_error_set_print (mu_syslog_error_printer); | ||
179 | |||
180 | uid = getuid (); | ||
181 | |||
182 | argc -= arg_index; | ||
183 | argv += arg_index; | ||
151 | 184 | ||
152 | if (!argc) | 185 | if (!argc) |
153 | print_help (); | 186 | print_help (); |
... | @@ -729,31 +762,5 @@ static char help_message[] = | ... | @@ -729,31 +762,5 @@ static char help_message[] = |
729 | exit (0); | 762 | exit (0); |
730 | } | 763 | } |
731 | 764 | ||
732 | void | ||
733 | print_license () | ||
734 | { | ||
735 | static char license_text[] = | ||
736 | " This program is free software; you can redistribute it and/or modify\n" | ||
737 | " it under the terms of the GNU General Public License as published by\n" | ||
738 | " the Free Software Foundation; either version 2, or (at your option)\n" | ||
739 | " any later version.\n" | ||
740 | "\n" | ||
741 | " This program is distributed in the hope that it will be useful,\n" | ||
742 | " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | ||
743 | " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | ||
744 | " GNU General Public License for more details.\n" | ||
745 | "\n" | ||
746 | " You should have received a copy of the GNU General Public License\n" | ||
747 | " along with this program; if not, write to the Free Software\n" | ||
748 | " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"; | ||
749 | printf ("%s", license_text); | ||
750 | exit (0); | ||
751 | } | ||
752 | 765 | ||
753 | void | ||
754 | print_version () | ||
755 | { | ||
756 | printf ("mail.local ("PACKAGE " " VERSION ")\n"); | ||
757 | exit (0); | ||
758 | } | ||
759 | 766 | ... | ... |
... | @@ -22,95 +22,49 @@ | ... | @@ -22,95 +22,49 @@ |
22 | #endif | 22 | #endif |
23 | 23 | ||
24 | mailbox_t mbox; | 24 | mailbox_t mbox; |
25 | unsigned int timeout; | ||
26 | int state; | 25 | int state; |
27 | char *username; | 26 | char *username; |
28 | char *maildir = MU_PATH_MAILDIR; | ||
29 | FILE *ifile; | 27 | FILE *ifile; |
30 | FILE *ofile; | 28 | FILE *ofile; |
31 | char *md5shared; | 29 | char *md5shared; |
32 | /* Number of child processes. */ | ||
33 | volatile size_t children; | ||
34 | 30 | ||
35 | static struct option long_options[] = | 31 | struct daemon_param daemon_param = { |
36 | { | 32 | MODE_INTERACTIVE, /* Start in interactive (inetd) mode */ |
37 | {"daemon", optional_argument, 0, 'd'}, | 33 | 20, /* Default maximum number of children */ |
38 | {"help", no_argument, 0, 'h'}, | 34 | 110, /* Standard POP3 port */ |
39 | {"inetd", no_argument, 0, 'i'}, | 35 | 600 /* Idle timeout */ |
40 | {"maildir", required_argument, 0, 'm'}, | ||
41 | {"port", required_argument, 0, 'p'}, | ||
42 | {"timeout", required_argument, 0, 't'}, | ||
43 | {"version", no_argument, 0, 'v'}, | ||
44 | {0, 0, 0, 0} | ||
45 | }; | 36 | }; |
46 | 37 | ||
47 | const char *short_options = "d::him:p:t:v"; | 38 | /* Number of child processes. */ |
39 | volatile size_t children; | ||
48 | 40 | ||
49 | static int pop3d_mainloop __P ((int, int)); | 41 | static int pop3d_mainloop __P ((int, int)); |
50 | static void pop3d_daemon_init __P ((void)); | 42 | static void pop3d_daemon_init __P ((void)); |
51 | static void pop3d_daemon __P ((unsigned int, unsigned int)); | 43 | static void pop3d_daemon __P ((unsigned int, unsigned int)); |
52 | static void pop3d_usage __P ((char *)); | 44 | static void pop3d_usage __P ((char *)); |
53 | 45 | ||
54 | #ifndef DEFMAXCHILDREN | 46 | const char *argp_program_version = "pop3d (" PACKAGE ") " VERSION; |
55 | # define DEFMAXCHILDREN 10 /* Default maximum number of children */ | 47 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; |
56 | #endif | 48 | static char doc[] = "GNU pop3d -- the POP3 daemon"; |
49 | |||
50 | static struct argp argp = { | ||
51 | NULL, | ||
52 | NULL, | ||
53 | NULL, | ||
54 | doc, | ||
55 | mu_daemon_argp_child, | ||
56 | NULL, NULL | ||
57 | }; | ||
58 | |||
57 | 59 | ||
58 | int | 60 | int |
59 | main (int argc, char **argv) | 61 | main (int argc, char **argv) |
60 | { | 62 | { |
61 | struct group *gr; | 63 | struct group *gr; |
62 | static int mode = INTERACTIVE; | ||
63 | size_t maxchildren = DEFMAXCHILDREN; | ||
64 | int c = 0; | ||
65 | int status = OK; | 64 | int status = OK; |
66 | unsigned int port; | ||
67 | 65 | ||
68 | port = 110; /* Default POP3 port. */ | 66 | mu_create_argcv (argc, argv, &argc, &argv); |
69 | timeout = 600; /* Default timeout of 600. */ | 67 | argp_parse (&argp, argc, argv, 0, 0, &daemon_param); |
70 | |||
71 | while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) | ||
72 | != -1) | ||
73 | { | ||
74 | switch (c) | ||
75 | { | ||
76 | case 'd': | ||
77 | mode = DAEMON; | ||
78 | if (optarg) | ||
79 | maxchildren = strtoul (optarg, NULL, 10); | ||
80 | if (maxchildren == 0) | ||
81 | maxchildren = DEFMAXCHILDREN; | ||
82 | break; | ||
83 | |||
84 | case 'h': | ||
85 | pop3d_usage (argv[0]); | ||
86 | break; | ||
87 | |||
88 | case 'i': | ||
89 | mode = INTERACTIVE; | ||
90 | break; | ||
91 | |||
92 | case 'm': | ||
93 | maildir = optarg; | ||
94 | break; | ||
95 | |||
96 | case 'p': | ||
97 | mode = DAEMON; | ||
98 | port = strtoul (optarg, NULL, 10); | ||
99 | break; | ||
100 | |||
101 | case 't': | ||
102 | timeout = strtoul (optarg, NULL, 10); | ||
103 | break; | ||
104 | |||
105 | case 'v': | ||
106 | printf (IMPL " ("PACKAGE " " VERSION ")\n"); | ||
107 | exit (EXIT_SUCCESS); | ||
108 | break; | ||
109 | |||
110 | default: | ||
111 | break; | ||
112 | } | ||
113 | } | ||
114 | 68 | ||
115 | maildir = mu_normalize_maildir (maildir); | 69 | maildir = mu_normalize_maildir (maildir); |
116 | if (!maildir) | 70 | if (!maildir) |
... | @@ -162,7 +116,7 @@ main (int argc, char **argv) | ... | @@ -162,7 +116,7 @@ main (int argc, char **argv) |
162 | signal (SIGPIPE, pop3d_signal); | 116 | signal (SIGPIPE, pop3d_signal); |
163 | signal (SIGABRT, pop3d_signal); | 117 | signal (SIGABRT, pop3d_signal); |
164 | 118 | ||
165 | if (mode == DAEMON) | 119 | if (daemon_param.mode == MODE_DAEMON) |
166 | pop3d_daemon_init (); | 120 | pop3d_daemon_init (); |
167 | else | 121 | else |
168 | { | 122 | { |
... | @@ -171,7 +125,7 @@ main (int argc, char **argv) | ... | @@ -171,7 +125,7 @@ main (int argc, char **argv) |
171 | } | 125 | } |
172 | 126 | ||
173 | /* Set up for syslog. */ | 127 | /* Set up for syslog. */ |
174 | openlog ("gnu-pop3d", LOG_PID, LOG_FACILITY); | 128 | openlog ("gnu-pop3d", LOG_PID, log_facility); |
175 | /* Redirect any stdout error from the library to syslog, they | 129 | /* Redirect any stdout error from the library to syslog, they |
176 | should not go to the client. */ | 130 | should not go to the client. */ |
177 | mu_error_set_print (mu_syslog_error_printer); | 131 | mu_error_set_print (mu_syslog_error_printer); |
... | @@ -179,8 +133,8 @@ main (int argc, char **argv) | ... | @@ -179,8 +133,8 @@ main (int argc, char **argv) |
179 | umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */ | 133 | umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */ |
180 | 134 | ||
181 | /* Actually run the daemon. */ | 135 | /* Actually run the daemon. */ |
182 | if (mode == DAEMON) | 136 | if (daemon_param.mode == MODE_DAEMON) |
183 | pop3d_daemon (maxchildren, port); | 137 | pop3d_daemon (daemon_param.maxchildren, daemon_param.port); |
184 | /* exit (EXIT_SUCCESS) -- no way out of daemon except a signal. */ | 138 | /* exit (EXIT_SUCCESS) -- no way out of daemon except a signal. */ |
185 | else | 139 | else |
186 | status = pop3d_mainloop (fileno (stdin), fileno (stdout)); | 140 | status = pop3d_mainloop (fileno (stdin), fileno (stdout)); |
... | @@ -451,26 +405,6 @@ pop3d_daemon (unsigned int maxchildren, unsigned int port) | ... | @@ -451,26 +405,6 @@ pop3d_daemon (unsigned int maxchildren, unsigned int port) |
451 | } | 405 | } |
452 | } | 406 | } |
453 | 407 | ||
454 | /* Prints out usage information and exits the program */ | ||
455 | 408 | ||
456 | static void | ||
457 | pop3d_usage (char *argv0) | ||
458 | { | ||
459 | printf ("Usage: %s [OPTIONS]\n", argv0); | ||
460 | printf ("Runs the GNU POP3 daemon.\n\n"); | ||
461 | printf (" -d, --daemon=MAXCHILDREN runs in daemon mode with a maximum\n"); | ||
462 | printf (" of MAXCHILDREN child processes\n"); | ||
463 | printf (" -h, --help display this help and exit\n"); | ||
464 | printf (" -i, --inetd runs in inetd mode (default)\n"); | ||
465 | printf (" -m, --maildir=PATH sets path to the mailspool directory\n"); | ||
466 | printf (" -p, --port=PORT specifies port to listen on, implies -d\n" | ||
467 | ); | ||
468 | printf (" defaults to 110, which need not be specified\n"); | ||
469 | printf (" -t, --timeout=TIMEOUT sets idle timeout to TIMEOUT seconds\n"); | ||
470 | printf (" TIMEOUT default is 600 (10 minutes)\n"); | ||
471 | printf (" -v, --version display version information and exit\n"); | ||
472 | printf ("\nReport bugs to bug-mailutils@gnu.org\n"); | ||
473 | exit (EXIT_SUCCESS); | ||
474 | } | ||
475 | 409 | ||
476 | 410 | ... | ... |
-
Please register or sign in to post a comment