1 /***************************************************************************
2  *   copyright           : (C) 2003 by Hendrik Sattler                     *
3  *   mail                : post@hendrik-sattler.de                         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  ***************************************************************************/
11 
12 #include <config.h>
13 #include <options.h>
14 #include <helper.h>
15 #include <string.h>
16 
17 #if PARSELIB == 2
18 #include <popt.h>
19 
args_parse(int argc,char ** argv,struct args_def * list,void (* arg_fill)(int short_opt,const char * long_opt,char * argument))20 char** args_parse (int argc, char** argv,
21 		   struct args_def* list,
22 		   void (*arg_fill) (int short_opt,
23 				     const char* long_opt,
24 				     char* argument))
25 {
26   int arg;
27   poptContext pcon;
28   int argcount = 0;
29   char** temp;
30   char** retval;
31   int i = 0;
32   struct poptOption* myoptions;
33   struct poptOption listEnd = POPT_TABLEEND;
34 
35   while(list[i].long_name != NULL ||
36 	list[i].short_name != 0)
37     ++i;
38   myoptions = mem_alloc((i+1)*sizeof(*myoptions),0);
39   memcpy(&myoptions[i--],&listEnd,sizeof(listEnd));
40   for (; i >= 0; --i) {
41     myoptions[i].longName = list[i].long_name;
42     myoptions[i].shortName = list[i].short_name;
43     if (list[i].ptype == ARGS_PARAM_NONE)
44       myoptions[i].argInfo = POPT_ARG_NONE;
45     else
46       myoptions[i].argInfo = POPT_ARG_STRING;
47     myoptions[i].arg = NULL;
48     myoptions[i].val = i+1;
49     myoptions[i].descrip = NULL;
50     myoptions[i].argDescrip = NULL;
51   }
52 
53   pcon = poptGetContext(NULL,argc,(const char **)argv,
54 			(const struct poptOption*)myoptions,
55 			POPT_CONTEXT_POSIXMEHARDER);
56   while ((arg = poptGetNextOpt(pcon)) > 0) {
57     arg_fill(myoptions[arg-1].shortName,myoptions[arg-1].longName,
58 	     (char*)poptGetOptArg(pcon));
59   }
60   if (arg < -1) {
61     fprintf(stderr, "%s: %s\n",
62 	    poptBadOption(pcon, POPT_BADOPTION_NOALIAS),
63 	    poptStrerror(arg));
64   }
65 
66   temp = (char**) poptGetArgs(pcon);
67   if (temp != NULL) {
68     while (*temp != NULL) {
69       ++argcount;
70     }
71   }
72   retval = mem_alloc(argcount+1,0);
73   for (i=0; i < argcount;++i) {
74     retval[i] = temp[i];
75   }
76   retval[i]=NULL;
77 
78   poptFreeContext(pcon);
79   mem_realloc(myoptions,0);
80   return retval;
81 }
82 
83 #endif
84