1 |
19 |
jeremybenn |
/*********************************************************************
|
2 |
|
|
This file is part of the argtable2 library.
|
3 |
|
|
Copyright (C) 1998-2001,2003-2008 Stewart Heitmann
|
4 |
|
|
sheitmann@users.sourceforge.net
|
5 |
|
|
|
6 |
|
|
The argtable2 library is free software; you can redistribute it and/or
|
7 |
|
|
modify it under the terms of the GNU Library General Public License as
|
8 |
|
|
published by the Free Software Foundation; either version 2 of the
|
9 |
|
|
License, or (at your option) any later version.
|
10 |
|
|
|
11 |
|
|
This software 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 GNU
|
14 |
|
|
Library General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU Library General Public
|
17 |
|
|
License along with this library; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
19 |
|
|
USA.
|
20 |
|
|
**********************************************************************/
|
21 |
|
|
|
22 |
|
|
/* config.h must be included before anything else */
|
23 |
|
|
#ifdef HAVE_CONFIG_H
|
24 |
|
|
#include "config.h"
|
25 |
|
|
#endif
|
26 |
|
|
|
27 |
|
|
/* #ifdef HAVE_STDLIB_H */
|
28 |
|
|
#include <stdlib.h>
|
29 |
|
|
/* #endif */
|
30 |
|
|
|
31 |
|
|
#include "argtable2.h"
|
32 |
|
|
|
33 |
|
|
static void resetfn(struct arg_end *parent)
|
34 |
|
|
{
|
35 |
|
|
/*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
|
36 |
|
|
parent->count = 0;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
static void errorfn(void *parent, FILE *fp, int error, const char *argval, const char *progname)
|
40 |
|
|
{
|
41 |
|
|
progname = progname ? progname : "";
|
42 |
|
|
argval = argval ? argval : "";
|
43 |
|
|
|
44 |
|
|
fprintf(fp,"%s: ",progname);
|
45 |
|
|
switch(error)
|
46 |
|
|
{
|
47 |
|
|
case ARG_ELIMIT:
|
48 |
|
|
fputs("too many errors to display",fp);
|
49 |
|
|
break;
|
50 |
|
|
case ARG_EMALLOC:
|
51 |
|
|
fputs("insufficent memory",fp);
|
52 |
|
|
break;
|
53 |
|
|
case ARG_ENOMATCH:
|
54 |
|
|
fprintf(fp,"unexpected argument \"%s\"",argval);
|
55 |
|
|
break;
|
56 |
|
|
case ARG_EMISSARG:
|
57 |
|
|
fprintf(fp,"option \"%s\" requires an argument",argval);
|
58 |
|
|
break;
|
59 |
|
|
case ARG_ELONGOPT:
|
60 |
|
|
fprintf(fp,"invalid option \"%s\"",argval);
|
61 |
|
|
break;
|
62 |
|
|
default:
|
63 |
|
|
fprintf(fp,"invalid option \"-%c\"",error);
|
64 |
|
|
break;
|
65 |
|
|
}
|
66 |
|
|
fputc('\n',fp);
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
struct arg_end* arg_end(int maxcount)
|
71 |
|
|
{
|
72 |
|
|
size_t nbytes;
|
73 |
|
|
struct arg_end *result;
|
74 |
|
|
|
75 |
|
|
nbytes = sizeof(struct arg_end)
|
76 |
|
|
+ maxcount * sizeof(int) /* storage for int error[maxcount] array*/
|
77 |
|
|
+ maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */
|
78 |
|
|
+ maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */
|
79 |
|
|
|
80 |
|
|
result = (struct arg_end*)malloc(nbytes);
|
81 |
|
|
if (result)
|
82 |
|
|
{
|
83 |
|
|
/* init the arg_hdr struct */
|
84 |
|
|
result->hdr.flag = ARG_TERMINATOR;
|
85 |
|
|
result->hdr.shortopts = NULL;
|
86 |
|
|
result->hdr.longopts = NULL;
|
87 |
|
|
result->hdr.datatype = NULL;
|
88 |
|
|
result->hdr.glossary = NULL;
|
89 |
|
|
result->hdr.mincount = 1;
|
90 |
|
|
result->hdr.maxcount = maxcount;
|
91 |
|
|
result->hdr.parent = result;
|
92 |
|
|
result->hdr.resetfn = (arg_resetfn*)resetfn;
|
93 |
|
|
result->hdr.scanfn = NULL;
|
94 |
|
|
result->hdr.checkfn = NULL;
|
95 |
|
|
result->hdr.errorfn = errorfn;
|
96 |
|
|
|
97 |
|
|
/* store error[maxcount] array immediately after struct arg_end */
|
98 |
|
|
result->error = (int*)(result+1);
|
99 |
|
|
|
100 |
|
|
/* store parent[maxcount] array immediately after error[] array */
|
101 |
|
|
result->parent = (void**)(result->error + maxcount );
|
102 |
|
|
|
103 |
|
|
/* store argval[maxcount] array immediately after parent[] array */
|
104 |
|
|
result->argval = (const char**)(result->parent + maxcount );
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/*printf("arg_end(%d) returns %p\n",maxcount,result);*/
|
108 |
|
|
return result;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname)
|
113 |
|
|
{
|
114 |
|
|
int i;
|
115 |
|
|
/*printf("arg_errors()\n");*/
|
116 |
|
|
for (i=0; i<end->count; i++)
|
117 |
|
|
{
|
118 |
|
|
struct arg_hdr *errorparent = (struct arg_hdr *)(end->parent[i]);
|
119 |
|
|
if (errorparent->errorfn)
|
120 |
|
|
errorparent->errorfn(end->parent[i],fp,end->error[i],end->argval[i],progname);
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
|