Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / args.c
1 /*
2  args.c : small frontend to libPopt command line argument parser
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "args.h"
23
24 GArray *iopt_tables = NULL;
25
26 void args_register(struct poptOption *options)
27 {
28         if (iopt_tables == NULL) {
29                 iopt_tables = g_array_new(TRUE, TRUE,
30                                           sizeof(struct poptOption));
31         }
32
33         while (options->longName != NULL || options->shortName != '\0' ||
34                options->arg != NULL) {
35                 g_array_append_val(iopt_tables, *options);
36                 options = options+1;
37         }
38 }
39
40 void args_execute(int argc, char *argv[])
41 {
42         poptContext con;
43         int nextopt;
44
45         if (iopt_tables == NULL)
46                 return;
47
48         con = poptGetContext(PACKAGE, argc, argv,
49                              (struct poptOption *) (iopt_tables->data), 0);
50         poptReadDefaultConfig(con, TRUE);
51
52         while ((nextopt = poptGetNextOpt(con)) > 0) ;
53
54         if (nextopt != -1) {
55                 printf("Error on option %s: %s.\n"
56                        "Run '%s --help' to see a full list of "
57                        "available command line options.\n",
58                        poptBadOption(con, 0), poptStrerror(nextopt), argv[0]);
59                 exit(1);
60         }
61
62         g_array_free(iopt_tables, TRUE);
63         iopt_tables = NULL;
64
65         poptFreeContext(con);
66 }