/*********************************************************************
|
/*********************************************************************
|
This file is part of the argtable2 library.
|
This file is part of the argtable2 library.
|
Copyright (C) 1998-2001,2003-2008 Stewart Heitmann
|
Copyright (C) 1998-2001,2003-2008 Stewart Heitmann
|
sheitmann@users.sourceforge.net
|
sheitmann@users.sourceforge.net
|
|
|
The argtable2 library is free software; you can redistribute it and/or
|
The argtable2 library is free software; you can redistribute it and/or
|
modify it under the terms of the GNU Library General Public License as
|
modify it under the terms of the GNU Library General Public License as
|
published by the Free Software Foundation; either version 2 of the
|
published by the Free Software Foundation; either version 2 of the
|
License, or (at your option) any later version.
|
License, or (at your option) any later version.
|
|
|
This software is distributed in the hope that it will be useful,
|
This software is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
Library General Public License for more details.
|
Library General Public License for more details.
|
|
|
You should have received a copy of the GNU Library General Public
|
You should have received a copy of the GNU Library General Public
|
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
USA.
|
USA.
|
**********************************************************************/
|
**********************************************************************/
|
|
|
/* config.h must be included before anything else */
|
/* config.h must be included before anything else */
|
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
#include "config.h"
|
#include "config.h"
|
#endif
|
#endif
|
|
|
/* #ifdef HAVE_STDLIB_H */
|
/* #ifdef HAVE_STDLIB_H */
|
#include <stdlib.h>
|
#include <stdlib.h>
|
/* #endif */
|
/* #endif */
|
|
|
#include "argtable2.h"
|
#include "argtable2.h"
|
|
|
static void resetfn(struct arg_end *parent)
|
static void resetfn(struct arg_end *parent)
|
{
|
{
|
/*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
|
/*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
|
parent->count = 0;
|
parent->count = 0;
|
}
|
}
|
|
|
static void errorfn(void *parent, FILE *fp, int error, const char *argval, const char *progname)
|
static void errorfn(void *parent, FILE *fp, int error, const char *argval, const char *progname)
|
{
|
{
|
progname = progname ? progname : "";
|
progname = progname ? progname : "";
|
argval = argval ? argval : "";
|
argval = argval ? argval : "";
|
|
|
fprintf(fp,"%s: ",progname);
|
fprintf(fp,"%s: ",progname);
|
switch(error)
|
switch(error)
|
{
|
{
|
case ARG_ELIMIT:
|
case ARG_ELIMIT:
|
fputs("too many errors to display",fp);
|
fputs("too many errors to display",fp);
|
break;
|
break;
|
case ARG_EMALLOC:
|
case ARG_EMALLOC:
|
fputs("insufficent memory",fp);
|
fputs("insufficent memory",fp);
|
break;
|
break;
|
case ARG_ENOMATCH:
|
case ARG_ENOMATCH:
|
fprintf(fp,"unexpected argument \"%s\"",argval);
|
fprintf(fp,"unexpected argument \"%s\"",argval);
|
break;
|
break;
|
case ARG_EMISSARG:
|
case ARG_EMISSARG:
|
fprintf(fp,"option \"%s\" requires an argument",argval);
|
fprintf(fp,"option \"%s\" requires an argument",argval);
|
break;
|
break;
|
case ARG_ELONGOPT:
|
case ARG_ELONGOPT:
|
fprintf(fp,"invalid option \"%s\"",argval);
|
fprintf(fp,"invalid option \"%s\"",argval);
|
break;
|
break;
|
default:
|
default:
|
fprintf(fp,"invalid option \"-%c\"",error);
|
fprintf(fp,"invalid option \"-%c\"",error);
|
break;
|
break;
|
}
|
}
|
fputc('\n',fp);
|
fputc('\n',fp);
|
}
|
}
|
|
|
|
|
struct arg_end* arg_end(int maxcount)
|
struct arg_end* arg_end(int maxcount)
|
{
|
{
|
size_t nbytes;
|
size_t nbytes;
|
struct arg_end *result;
|
struct arg_end *result;
|
|
|
nbytes = sizeof(struct arg_end)
|
nbytes = sizeof(struct arg_end)
|
+ maxcount * sizeof(int) /* storage for int error[maxcount] array*/
|
+ maxcount * sizeof(int) /* storage for int error[maxcount] array*/
|
+ maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */
|
+ maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */
|
+ maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */
|
+ maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */
|
|
|
result = (struct arg_end*)malloc(nbytes);
|
result = (struct arg_end*)malloc(nbytes);
|
if (result)
|
if (result)
|
{
|
{
|
/* init the arg_hdr struct */
|
/* init the arg_hdr struct */
|
result->hdr.flag = ARG_TERMINATOR;
|
result->hdr.flag = ARG_TERMINATOR;
|
result->hdr.shortopts = NULL;
|
result->hdr.shortopts = NULL;
|
result->hdr.longopts = NULL;
|
result->hdr.longopts = NULL;
|
result->hdr.datatype = NULL;
|
result->hdr.datatype = NULL;
|
result->hdr.glossary = NULL;
|
result->hdr.glossary = NULL;
|
result->hdr.mincount = 1;
|
result->hdr.mincount = 1;
|
result->hdr.maxcount = maxcount;
|
result->hdr.maxcount = maxcount;
|
result->hdr.parent = result;
|
result->hdr.parent = result;
|
result->hdr.resetfn = (arg_resetfn*)resetfn;
|
result->hdr.resetfn = (arg_resetfn*)resetfn;
|
result->hdr.scanfn = NULL;
|
result->hdr.scanfn = NULL;
|
result->hdr.checkfn = NULL;
|
result->hdr.checkfn = NULL;
|
result->hdr.errorfn = errorfn;
|
result->hdr.errorfn = errorfn;
|
|
|
/* store error[maxcount] array immediately after struct arg_end */
|
/* store error[maxcount] array immediately after struct arg_end */
|
result->error = (int*)(result+1);
|
result->error = (int*)(result+1);
|
|
|
/* store parent[maxcount] array immediately after error[] array */
|
/* store parent[maxcount] array immediately after error[] array */
|
result->parent = (void**)(result->error + maxcount );
|
result->parent = (void**)(result->error + maxcount );
|
|
|
/* store argval[maxcount] array immediately after parent[] array */
|
/* store argval[maxcount] array immediately after parent[] array */
|
result->argval = (const char**)(result->parent + maxcount );
|
result->argval = (const char**)(result->parent + maxcount );
|
}
|
}
|
|
|
/*printf("arg_end(%d) returns %p\n",maxcount,result);*/
|
/*printf("arg_end(%d) returns %p\n",maxcount,result);*/
|
return result;
|
return result;
|
}
|
}
|
|
|
|
|
void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname)
|
void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname)
|
{
|
{
|
int i;
|
int i;
|
/*printf("arg_errors()\n");*/
|
/*printf("arg_errors()\n");*/
|
for (i=0; i<end->count; i++)
|
for (i=0; i<end->count; i++)
|
{
|
{
|
struct arg_hdr *errorparent = (struct arg_hdr *)(end->parent[i]);
|
struct arg_hdr *errorparent = (struct arg_hdr *)(end->parent[i]);
|
if (errorparent->errorfn)
|
if (errorparent->errorfn)
|
errorparent->errorfn(end->parent[i],fp,end->error[i],end->argval[i],progname);
|
errorparent->errorfn(end->parent[i],fp,end->error[i],end->argval[i],progname);
|
}
|
}
|
}
|
}
|
|
|
|
|
|
|