Commit 063c3071 063c30715d8be4b60fdb384ec7752d3f3035d003 by Sergey Poznyakoff

Moved from /mailbox

1 parent 8bccb389
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002 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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <string.h>
22 #include <mailutils/mailutils.h>
23 #include <mailutils/argp.h>
24 #include <mu_asprintf.h>
25
26 const char *program_version = "mailutils-config (" PACKAGE_STRING ")";
27 static char doc[] = N_("GNU mailutils-config -- Display compiler and loader options needed for building a program with mailutils");
28 static char args_doc[] = N_("[arg...]");
29
30 static struct argp_option options[] = {
31 {"compile", 'c', NULL, 0,
32 N_("Print C compiler flags to compile with"), 0},
33 {"link", 'l', NULL, 0,
34 N_("Print libraries to link with. Possible arguments are: auth, guile, all, "
35 "mbox, mh, maildir, imap, pop"), 0},
36 {"info", 'i', NULL, 0,
37 N_("Print a list of configuration options used to build mailutils. If arguments "
38 "are given, they are interpreted as a list of configuration options to check "
39 "for. In this case the program prints those options from this list that "
40 "have been defined. It exits with zero status if all of the "
41 "specified options are defined. Otherwise, the exit status is 1."), 0},
42 {0, 0, 0, 0}
43 };
44
45 enum config_mode {
46 MODE_VOID,
47 MODE_COMPILE,
48 MODE_LINK,
49 MODE_INFO
50 };
51
52 enum config_mode mode;
53
54 static error_t
55 parse_opt (int key, char *arg, struct argp_state *state)
56 {
57 switch (key)
58 {
59 case 'l':
60 mode = MODE_LINK;
61 break;
62
63 case 'c':
64 mode = MODE_COMPILE;
65 break;
66
67 case 'i':
68 mode = MODE_INFO;
69 break;
70
71 default:
72 return ARGP_ERR_UNKNOWN;
73 }
74 return 0;
75 }
76
77 static struct argp argp = {
78 options,
79 parse_opt,
80 args_doc,
81 doc,
82 NULL,
83 NULL, NULL
84 };
85
86 static const char *argp_capa[] = {
87 "common",
88 "license",
89 NULL
90 };
91
92 #ifdef WITH_TLS
93 # define TLSAUTH 1
94 #else
95 # define TLSAUTH 0
96 #endif
97
98 struct lib_descr {
99 char *name;
100 char *libname;
101 int needauth;
102 } lib_descr[] = {
103 { "mbox", "mu_mbox", 0 },
104 { "mh", "mu_mh", 0 },
105 { "maildir","mu_maildir", 0 },
106 { "imap", "mu_imap", TLSAUTH },
107 { "pop", "mu_pop", TLSAUTH },
108 { "nntp", "mu_nntp", 0 },
109 { NULL }
110 };
111
112 struct lib_entry {
113 int level;
114 char *ptr;
115 } lib_entry[10];
116
117 int nentry;
118
119 void
120 add_entry (int level, char *ptr)
121 {
122 int i;
123 if (nentry >= sizeof(lib_entry)/sizeof(lib_entry[0]))
124 {
125 mu_error (_("Too many arguments"));
126 exit (1);
127 }
128
129 for (i = 0; i < nentry; i++)
130 if (strcmp (lib_entry[i].ptr, ptr) == 0)
131 return;
132 lib_entry[nentry].level = level;
133 lib_entry[nentry].ptr = ptr;
134 nentry++;
135 }
136
137 /* Sort the entires by their level. */
138 void
139 sort_entries ()
140 {
141 int j;
142
143 for (j = 0; j < nentry; j++)
144 {
145 int i;
146
147 for (i = j; i < nentry; i++)
148 if (lib_entry[j].level > lib_entry[i].level)
149 {
150 struct lib_entry tmp;
151 tmp = lib_entry[i];
152 lib_entry[i] = lib_entry[j];
153 lib_entry[j] = tmp;
154 }
155
156 }
157 }
158
159 int
160 main (int argc, char **argv)
161 {
162 int index;
163
164 mu_argp_init (program_version, NULL);
165 if (mu_argp_parse (&argp, &argc, &argv, 0, argp_capa, &index, NULL))
166 {
167 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
168 return 1;
169 }
170
171 argc -= index;
172 argv += index;
173
174 switch (mode)
175 {
176 case MODE_VOID:
177 break;
178
179 case MODE_LINK:
180 {
181 int j;
182 char *ptr;
183
184 add_entry (-1, LINK_FLAGS);
185 add_entry (100, LINK_POSTFLAGS);
186 add_entry (1, "-lmailbox");
187 #ifdef ENABLE_NLS
188 if (sizeof (I18NLIBS) > 1)
189 add_entry (10, I18NLIBS);
190 #endif
191
192 for ( ; argc > 0; argc--, argv++)
193 {
194 if (strcmp (argv[0], "auth") == 0)
195 {
196 add_entry (2, "-lmuauth " AUTHLIBS);
197 }
198 #ifdef WITH_GUILE
199 else if (strcmp (argv[0], "guile") == 0)
200 {
201 add_entry (-1, "-lmu_scm " GUILE_LIBS);
202 }
203 #endif
204 else if (strcmp (argv[0], "all") == 0)
205 {
206 struct lib_descr *p;
207
208 for (p = lib_descr; p->name; p++)
209 {
210 asprintf (&ptr, "-l%s", p->libname);
211 add_entry (0, ptr);
212 if (p->needauth)
213 add_entry (2, "-lmuauth " AUTHLIBS);
214 }
215 }
216 else
217 {
218 struct lib_descr *p;
219
220 for (p = lib_descr; p->name; p++)
221 if (strcasecmp (p->name, argv[0]) == 0)
222 break;
223
224 if (p->name)
225 {
226 asprintf (&ptr, "-l%s", p->libname);
227 add_entry (0, ptr);
228 if (p->needauth)
229 add_entry (2, "-lmuauth " AUTHLIBS);
230 }
231 else
232 {
233 argp_help (&argp, stdout, ARGP_HELP_USAGE,
234 program_invocation_short_name);
235 return 1;
236 }
237 }
238 }
239
240 sort_entries ();
241
242 /* At least one entry is always present */
243 printf ("%s", lib_entry[0].ptr);
244
245 /* Print the rest of them separated by a space */
246 for (j = 1; j < nentry; j++)
247 {
248 printf (" %s", lib_entry[j].ptr);
249 }
250 printf ("\n");
251 return 0;
252 }
253
254 case MODE_COMPILE:
255 if (argc != 0)
256 break;
257 printf ("%s\n", COMPILE_FLAGS);
258 return 0;
259
260 case MODE_INFO:
261 if (argc == 0)
262 mu_print_options ();
263 else
264 {
265 int i, found = 0;
266
267 for (i = 0; i < argc; i++)
268 {
269 const char *val = mu_check_option (argv[i]);
270 if (val)
271 {
272 found++;
273 printf ("%s\n", val);
274 }
275 }
276 return found == argc ? 0 : 1;
277 }
278 return 0;
279 }
280
281 argp_help (&argp, stdout, ARGP_HELP_USAGE, program_invocation_short_name);
282 return 0;
283 }
284
285