Minor changes.
* mu/shell.c (input_line_script): Use mu_getline. * libmu_argp/compat.c: Remove. * libmu_argp/Makefile.am: Remove compat.c * po/POTFILES.in: Likewise.
Showing
4 changed files
with
3 additions
and
343 deletions
libmu_argp/compat.c
deleted
100644 → 0
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 3 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library 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 GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General | ||
15 | Public License along with this library; if not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | Boston, MA 02110-1301 USA */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include <config.h> | ||
21 | #endif | ||
22 | #include <sys/types.h> | ||
23 | #include <sys/stat.h> | ||
24 | #include <string.h> | ||
25 | #include "mailutils/cctype.h" | ||
26 | #include "mailutils/libargp.h" | ||
27 | #include "mailutils/argcv.h" | ||
28 | #include "mailutils/mutil.h" | ||
29 | #ifdef WITH_TLS | ||
30 | # include "mailutils/tls.h" | ||
31 | #endif | ||
32 | #ifdef WITH_GSASL | ||
33 | # include "mailutils/gsasl.h" | ||
34 | #endif | ||
35 | #include "mailutils/sieve.h" | ||
36 | |||
37 | #ifndef MU_COMPAT_CONFIG_FILE | ||
38 | # define MU_COMPAT_CONFIG_FILE SYSCONFDIR "/mailutils.rc" | ||
39 | #endif | ||
40 | |||
41 | #ifndef MU_COMPAT_USER_CONFIG_FILE | ||
42 | # define MU_COMPAT_USER_CONFIG_FILE "~/.mailutils" | ||
43 | #endif | ||
44 | |||
45 | static int | ||
46 | member (const char *array[], const char *text, size_t len) | ||
47 | { | ||
48 | int i; | ||
49 | for (i = 0; array[i]; i++) | ||
50 | if (strncmp (array[i], text, len) == 0) | ||
51 | return 1; | ||
52 | return 0; | ||
53 | } | ||
54 | |||
55 | /* Appends applicable options found in file NAME to argv. If progname | ||
56 | is NULL, all the options found are assumed to apply. Otherwise they | ||
57 | apply only if the line starts with ":something", and something is | ||
58 | found in the CAPA array, or the line starts with PROGNAME. | ||
59 | */ | ||
60 | void | ||
61 | read_rc (const char *progname, const char *name, const char *capa[], | ||
62 | int *argc, char ***argv) | ||
63 | { | ||
64 | FILE *fp; | ||
65 | char *linebuf = NULL; | ||
66 | char *buf = NULL; | ||
67 | size_t n = 0; | ||
68 | int x_argc = *argc; | ||
69 | char **x_argv = *argv; | ||
70 | char* rcfile = mu_tilde_expansion (name, "/", NULL); | ||
71 | |||
72 | if (!rcfile) | ||
73 | return; | ||
74 | |||
75 | fp = fopen (rcfile, "r"); | ||
76 | if (!fp) | ||
77 | { | ||
78 | free(rcfile); | ||
79 | return; | ||
80 | } | ||
81 | |||
82 | while (getline (&buf, &n, fp) > 0) | ||
83 | { | ||
84 | char *kwp, *p; | ||
85 | int len; | ||
86 | |||
87 | for (kwp = buf; *kwp && mu_isspace (*kwp); kwp++) | ||
88 | ; | ||
89 | |||
90 | if (*kwp == '#' || *kwp == 0) | ||
91 | continue; | ||
92 | |||
93 | len = strlen (kwp); | ||
94 | if (kwp[len-1] == '\n') | ||
95 | kwp[--len] = 0; | ||
96 | |||
97 | if (kwp[len-1] == '\\' || linebuf) | ||
98 | { | ||
99 | int cont; | ||
100 | |||
101 | if (kwp[len-1] == '\\') | ||
102 | { | ||
103 | kwp[--len] = 0; | ||
104 | cont = 1; | ||
105 | } | ||
106 | else | ||
107 | cont = 0; | ||
108 | |||
109 | if (!linebuf) | ||
110 | linebuf = calloc (len + 1, 1); | ||
111 | else | ||
112 | linebuf = realloc (linebuf, strlen (linebuf) + len + 1); | ||
113 | |||
114 | if (!linebuf) | ||
115 | { | ||
116 | fprintf (stderr, _("%s: not enough memory\n"), progname); | ||
117 | exit (1); | ||
118 | } | ||
119 | |||
120 | strcpy (linebuf + strlen (linebuf), kwp); | ||
121 | if (cont) | ||
122 | continue; | ||
123 | kwp = linebuf; | ||
124 | } | ||
125 | |||
126 | len = 0; | ||
127 | if (progname) | ||
128 | { | ||
129 | for (p = kwp; *p && !mu_isspace (*p); p++) | ||
130 | len++; | ||
131 | } | ||
132 | else | ||
133 | p = kwp; /* Use the whole line. */ | ||
134 | |||
135 | if (progname == NULL | ||
136 | || (kwp[0] == ':' && member (capa, kwp+1, len-1)) | ||
137 | || strncmp (progname, kwp, len) == 0 | ||
138 | ) | ||
139 | { | ||
140 | int i, n_argc = 0; | ||
141 | char **n_argv; | ||
142 | |||
143 | if (mu_argcv_get (p, "", NULL, &n_argc, &n_argv)) | ||
144 | { | ||
145 | mu_argcv_free (n_argc, n_argv); | ||
146 | if (linebuf) | ||
147 | free (linebuf); | ||
148 | linebuf = NULL; | ||
149 | continue; | ||
150 | } | ||
151 | x_argv = realloc (x_argv, | ||
152 | (x_argc + n_argc) * sizeof (x_argv[0])); | ||
153 | if (!x_argv) | ||
154 | { | ||
155 | fprintf (stderr, _("%s: not enough memory\n"), progname); | ||
156 | exit (1); | ||
157 | } | ||
158 | |||
159 | for (i = 0; i < n_argc; i++) | ||
160 | x_argv[x_argc++] = mu_tilde_expansion (n_argv[i], "/", NULL); | ||
161 | |||
162 | free (n_argv); | ||
163 | } | ||
164 | if (linebuf) | ||
165 | free (linebuf); | ||
166 | linebuf = NULL; | ||
167 | } | ||
168 | fclose (fp); | ||
169 | free(rcfile); | ||
170 | |||
171 | *argc = x_argc; | ||
172 | *argv = x_argv; | ||
173 | } | ||
174 | |||
175 | |||
176 | void | ||
177 | mu_create_argcv (const char *capa[], | ||
178 | int argc, char **argv, int *p_argc, char ***p_argv) | ||
179 | { | ||
180 | char *progname; | ||
181 | int x_argc; | ||
182 | char **x_argv; | ||
183 | int i; | ||
184 | int rcdir = 0; | ||
185 | |||
186 | progname = strrchr (argv[0], '/'); | ||
187 | if (progname) | ||
188 | progname++; | ||
189 | else | ||
190 | progname = argv[0]; | ||
191 | |||
192 | x_argv = malloc (sizeof (x_argv[0])); | ||
193 | if (!x_argv) | ||
194 | { | ||
195 | fprintf (stderr, _("%s: not enough memory\n"), progname); | ||
196 | exit (1); | ||
197 | } | ||
198 | |||
199 | /* Add command name */ | ||
200 | x_argc = 0; | ||
201 | x_argv[x_argc] = argv[x_argc]; | ||
202 | x_argc++; | ||
203 | |||
204 | /* Add global config file. */ | ||
205 | read_rc (progname, MU_COMPAT_CONFIG_FILE, capa, &x_argc, &x_argv); | ||
206 | |||
207 | /* Look for per-user config files in ~/.mailutils/ or in ~/, but | ||
208 | not both. This allows mailutils' utilities to have their config | ||
209 | files segregated, if necessary. */ | ||
210 | |||
211 | { | ||
212 | struct stat s; | ||
213 | char *rcdirname = mu_tilde_expansion (MU_COMPAT_USER_CONFIG_FILE, "/", NULL); | ||
214 | |||
215 | if (!rcdirname | ||
216 | || (stat(rcdirname, &s) == 0 && S_ISDIR(s.st_mode))) | ||
217 | rcdir = 1; | ||
218 | |||
219 | free(rcdirname); | ||
220 | } | ||
221 | |||
222 | /* Add per-user config file. */ | ||
223 | if (!rcdir) | ||
224 | { | ||
225 | read_rc (progname, MU_COMPAT_USER_CONFIG_FILE, capa, &x_argc, &x_argv); | ||
226 | } | ||
227 | else | ||
228 | { | ||
229 | char *userrc = NULL; | ||
230 | |||
231 | userrc = malloc (sizeof (MU_COMPAT_USER_CONFIG_FILE) | ||
232 | /* provides an extra slot | ||
233 | for null byte as well */ | ||
234 | + 1 /* slash */ | ||
235 | + 9 /*mailutils*/); | ||
236 | |||
237 | if (!userrc) | ||
238 | { | ||
239 | fprintf (stderr, _("%s: not enough memory\n"), progname); | ||
240 | exit (1); | ||
241 | } | ||
242 | |||
243 | sprintf (userrc, "%s/mailutils", MU_COMPAT_USER_CONFIG_FILE); | ||
244 | read_rc (progname, userrc, capa, &x_argc, &x_argv); | ||
245 | |||
246 | free (userrc); | ||
247 | } | ||
248 | |||
249 | /* Add per-user, per-program config file. */ | ||
250 | { | ||
251 | char *progrc = NULL; | ||
252 | int size; | ||
253 | |||
254 | if (rcdir) | ||
255 | size = sizeof (MU_COMPAT_USER_CONFIG_FILE) | ||
256 | + 1 | ||
257 | + strlen (progname) | ||
258 | + 2 /* rc */; | ||
259 | else | ||
260 | size = 6 /*~/.mu.*/ | ||
261 | + strlen (progname) | ||
262 | + 3 /* "rc" + null terminator */; | ||
263 | |||
264 | progrc = malloc (size); | ||
265 | |||
266 | if (!progrc) | ||
267 | { | ||
268 | fprintf (stderr, _("%s: not enough memory\n"), progname); | ||
269 | exit (1); | ||
270 | } | ||
271 | |||
272 | if (rcdir) | ||
273 | sprintf (progrc, "%s/%src", MU_COMPAT_USER_CONFIG_FILE, progname); | ||
274 | else | ||
275 | sprintf (progrc, "~/.mu.%src", progname); | ||
276 | |||
277 | read_rc (NULL, progrc, capa, &x_argc, &x_argv); | ||
278 | free (progrc); | ||
279 | } | ||
280 | |||
281 | /* Finally, add the command line options */ | ||
282 | x_argv = realloc (x_argv, (x_argc + argc) * sizeof (x_argv[0])); | ||
283 | for (i = 1; i < argc; i++) | ||
284 | x_argv[x_argc++] = argv[i]; | ||
285 | |||
286 | x_argv[x_argc] = NULL; | ||
287 | |||
288 | *p_argc = x_argc; | ||
289 | *p_argv = x_argv; | ||
290 | } | ||
291 | |||
292 | error_t | ||
293 | mu_argp_parse (const struct argp *myargp, | ||
294 | int *pargc, char **pargv[], | ||
295 | unsigned flags, | ||
296 | const char *capa[], | ||
297 | int *arg_index, | ||
298 | void *input) | ||
299 | { | ||
300 | struct argp *argp; | ||
301 | error_t rc; | ||
302 | const struct argp argpnull = { 0 }; | ||
303 | int i; | ||
304 | |||
305 | /* Make sure we have program version and bug address initialized */ | ||
306 | mu_argp_init (argp_program_version, argp_program_bug_address); | ||
307 | |||
308 | mu_set_program_name ((*pargv)[0]); | ||
309 | mu_libargp_init (); | ||
310 | for (i = 0; capa[i]; i++) | ||
311 | { | ||
312 | #ifdef WITH_TLS | ||
313 | if (strcmp (capa[i], "tls") == 0) | ||
314 | mu_gocs_register ("tls", mu_tls_module_init); | ||
315 | else | ||
316 | #endif /* WITH_TLS */ | ||
317 | #ifdef WITH_GSASL | ||
318 | if (strcmp (capa[i], "gsasl") == 0) | ||
319 | mu_gocs_register ("gsasl", mu_gsasl_module_init); | ||
320 | else | ||
321 | #endif | ||
322 | if (strcmp (capa[i], "sieve") == 0) | ||
323 | mu_gocs_register ("sieve", mu_sieve_module_init); | ||
324 | else | ||
325 | mu_gocs_register_std (capa[i]); | ||
326 | } | ||
327 | |||
328 | if (!myargp) | ||
329 | myargp = &argpnull; | ||
330 | argp = mu_argp_build (myargp, NULL); | ||
331 | rc = argp_parse (argp, *pargc, *pargv, flags, arg_index, input); | ||
332 | mu_argp_done (argp); | ||
333 | if (rc) | ||
334 | return rc; | ||
335 | |||
336 | mu_gocs_flush (); | ||
337 | |||
338 | return 0; | ||
339 | } |
... | @@ -518,9 +518,10 @@ input_line_interactive () | ... | @@ -518,9 +518,10 @@ input_line_interactive () |
518 | static char * | 518 | static char * |
519 | input_line_script () | 519 | input_line_script () |
520 | { | 520 | { |
521 | size_t size = 0; | 521 | size_t size = 0, n; |
522 | char *buf = NULL; | 522 | char *buf = NULL; |
523 | if (getline (&buf, &size, stdin) <= 0) | 523 | |
524 | if (mu_stream_getline (mustrin, &buf, &size, &n) || n == 0) | ||
524 | return NULL; | 525 | return NULL; |
525 | return buf; | 526 | return buf; |
526 | } | 527 | } | ... | ... |
... | @@ -42,7 +42,6 @@ lib/strexit.c | ... | @@ -42,7 +42,6 @@ lib/strexit.c |
42 | libmu_argp/auth.c | 42 | libmu_argp/auth.c |
43 | libmu_argp/cmdline.c | 43 | libmu_argp/cmdline.c |
44 | libmu_argp/common.c | 44 | libmu_argp/common.c |
45 | libmu_argp/compat.c | ||
46 | libmu_argp/mu_argp.c | 45 | libmu_argp/mu_argp.c |
47 | libmu_argp/sieve.c | 46 | libmu_argp/sieve.c |
48 | libmu_argp/tls.c | 47 | libmu_argp/tls.c | ... | ... |
-
Please register or sign in to post a comment