| 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 |
|
|
/* local error codes */
|
| 34 |
|
|
enum {EMINCOUNT=1,EMAXCOUNT};
|
| 35 |
|
|
|
| 36 |
|
|
static void resetfn(struct arg_lit *parent)
|
| 37 |
|
|
{
|
| 38 |
|
|
/*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
|
| 39 |
|
|
parent->count = 0;
|
| 40 |
|
|
}
|
| 41 |
|
|
|
| 42 |
|
|
static int scanfn(struct arg_lit *parent, const char *argval)
|
| 43 |
|
|
{
|
| 44 |
|
|
int errorcode = 0;
|
| 45 |
|
|
if (parent->count < parent->hdr.maxcount )
|
| 46 |
|
|
parent->count++;
|
| 47 |
|
|
else
|
| 48 |
|
|
errorcode = EMAXCOUNT;
|
| 49 |
|
|
/*printf("%s:scanfn(%p,%s) returns %d\n",__FILE__,parent,argval,errorcode);*/
|
| 50 |
|
|
return errorcode;
|
| 51 |
|
|
}
|
| 52 |
|
|
|
| 53 |
|
|
static int checkfn(struct arg_lit *parent)
|
| 54 |
|
|
{
|
| 55 |
|
|
int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
|
| 56 |
|
|
/*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
|
| 57 |
|
|
return errorcode;
|
| 58 |
|
|
}
|
| 59 |
|
|
|
| 60 |
|
|
static void errorfn(struct arg_lit *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
|
| 61 |
|
|
{
|
| 62 |
|
|
const char *shortopts = parent->hdr.shortopts;
|
| 63 |
|
|
const char *longopts = parent->hdr.longopts;
|
| 64 |
|
|
const char *datatype = parent->hdr.datatype;
|
| 65 |
|
|
|
| 66 |
|
|
switch(errorcode)
|
| 67 |
|
|
{
|
| 68 |
|
|
case EMINCOUNT:
|
| 69 |
|
|
fprintf(fp,"%s: missing option ",progname);
|
| 70 |
|
|
arg_print_option(fp,shortopts,longopts,datatype,"\n");
|
| 71 |
|
|
fprintf(fp,"\n");
|
| 72 |
|
|
break;
|
| 73 |
|
|
|
| 74 |
|
|
case EMAXCOUNT:
|
| 75 |
|
|
fprintf(fp,"%s: extraneous option ",progname);
|
| 76 |
|
|
arg_print_option(fp,shortopts,longopts,datatype,"\n");
|
| 77 |
|
|
break;
|
| 78 |
|
|
}
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
struct arg_lit* arg_lit0(const char* shortopts,
|
| 82 |
|
|
const char* longopts,
|
| 83 |
|
|
const char* glossary)
|
| 84 |
|
|
{return arg_litn(shortopts,longopts,0,1,glossary);}
|
| 85 |
|
|
|
| 86 |
|
|
struct arg_lit* arg_lit1(const char* shortopts,
|
| 87 |
|
|
const char* longopts,
|
| 88 |
|
|
const char* glossary)
|
| 89 |
|
|
{return arg_litn(shortopts,longopts,1,1,glossary);}
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
|
|
struct arg_lit* arg_litn(const char* shortopts,
|
| 93 |
|
|
const char* longopts,
|
| 94 |
|
|
int mincount,
|
| 95 |
|
|
int maxcount,
|
| 96 |
|
|
const char *glossary)
|
| 97 |
|
|
{
|
| 98 |
|
|
struct arg_lit *result;
|
| 99 |
|
|
|
| 100 |
|
|
/* foolproof things by ensuring maxcount is not less than mincount */
|
| 101 |
|
|
maxcount = (maxcount<mincount) ? mincount : maxcount;
|
| 102 |
|
|
|
| 103 |
|
|
result = (struct arg_lit*)malloc(sizeof(struct arg_lit));
|
| 104 |
|
|
if (result)
|
| 105 |
|
|
{
|
| 106 |
|
|
/* init the arg_hdr struct */
|
| 107 |
|
|
result->hdr.flag = 0;
|
| 108 |
|
|
result->hdr.shortopts = shortopts;
|
| 109 |
|
|
result->hdr.longopts = longopts;
|
| 110 |
|
|
result->hdr.datatype = NULL;
|
| 111 |
|
|
result->hdr.glossary = glossary;
|
| 112 |
|
|
result->hdr.mincount = mincount;
|
| 113 |
|
|
result->hdr.maxcount = maxcount;
|
| 114 |
|
|
result->hdr.parent = result;
|
| 115 |
|
|
result->hdr.resetfn = (arg_resetfn*)resetfn;
|
| 116 |
|
|
result->hdr.scanfn = (arg_scanfn*)scanfn;
|
| 117 |
|
|
result->hdr.checkfn = (arg_checkfn*)checkfn;
|
| 118 |
|
|
result->hdr.errorfn = (arg_errorfn*)errorfn;
|
| 119 |
|
|
|
| 120 |
|
|
/* init local variables */
|
| 121 |
|
|
result->count = 0;
|
| 122 |
|
|
}
|
| 123 |
|
|
/*printf("arg_litn() returns %p\n",result);*/
|
| 124 |
|
|
return result;
|
| 125 |
|
|
}
|