[cig-commits] r7510 - cs/cigma/branches/cigma-0.9/src
luis at geodynamics.org
luis at geodynamics.org
Tue Jun 26 09:59:16 PDT 2007
Author: luis
Date: 2007-06-26 09:59:15 -0700 (Tue, 26 Jun 2007)
New Revision: 7510
Added:
cs/cigma/branches/cigma-0.9/src/arg_dbl.c
cs/cigma/branches/cigma-0.9/src/arg_end.c
cs/cigma/branches/cigma-0.9/src/arg_file.c
cs/cigma/branches/cigma-0.9/src/arg_int.c
cs/cigma/branches/cigma-0.9/src/arg_lit.c
cs/cigma/branches/cigma-0.9/src/arg_rem.c
cs/cigma/branches/cigma-0.9/src/arg_str.c
cs/cigma/branches/cigma-0.9/src/argtable2.c
cs/cigma/branches/cigma-0.9/src/argtable2.h
Log:
darcs patch:
* Added source files for argtable2 from http://argtable.sourceforge.net/
Added: cs/cigma/branches/cigma-0.9/src/arg_dbl.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_dbl.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_dbl.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,167 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include "argtable2.h"
+
+/* local error codes */
+enum {EMINCOUNT=1,EMAXCOUNT,EBADDOUBLE};
+
+static void resetfn(struct arg_dbl *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count=0;
+ }
+
+static int scanfn(struct arg_dbl *parent, const char *argval)
+ {
+ int errorcode = 0;
+
+ if (parent->count == parent->hdr.maxcount)
+ {
+ /* maximum number of arguments exceeded */
+ errorcode = EMAXCOUNT;
+ }
+ else if (!argval)
+ {
+ /* a valid argument with no argument value was given. */
+ /* This happens when an optional argument value was invoked. */
+ /* leave parent arguiment value unaltered but still count the argument. */
+ parent->count++;
+ }
+ else
+ {
+ double val;
+ char *end;
+
+ /* extract double from argval into val */
+ val = strtod(argval,&end);
+
+ /* if success then store result in parent->dval[] array otherwise return error*/
+ if (*end==0)
+ parent->dval[parent->count++] = val;
+ else
+ errorcode = EBADDOUBLE;
+ }
+
+ /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static int checkfn(struct arg_dbl *parent)
+ {
+ int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
+ /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static void errorfn(struct arg_dbl *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
+ {
+ const char *shortopts = parent->hdr.shortopts;
+ const char *longopts = parent->hdr.longopts;
+ const char *datatype = parent->hdr.datatype;
+
+ /* make argval NULL safe */
+ argval = argval ? argval : "";
+
+ fprintf(fp,"%s: ",progname);
+ switch(errorcode)
+ {
+ case EMINCOUNT:
+ fputs("missing option ",fp);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+
+ case EMAXCOUNT:
+ fputs("excess option ",fp);
+ arg_print_option(fp,shortopts,longopts,argval,"\n");
+ break;
+
+ case EBADDOUBLE:
+ fprintf(fp,"invalid argument \"%s\" to option ",argval);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+ }
+ }
+
+
+struct arg_dbl* arg_dbl0(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_dbln(shortopts,longopts,datatype,0,1,glossary);
+ }
+
+struct arg_dbl* arg_dbl1(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_dbln(shortopts,longopts,datatype,1,1,glossary);
+ }
+
+
+struct arg_dbl* arg_dbln(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary)
+ {
+ size_t nbytes;
+ struct arg_dbl *result;
+
+ nbytes = sizeof(struct arg_dbl) /* storage for struct arg_dbl */
+ + maxcount * sizeof(double); /* storage for dval[maxcount] array */
+
+ result = (struct arg_dbl*)malloc(nbytes);
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = ARG_HASVALUE;
+ result->hdr.shortopts = shortopts;
+ result->hdr.longopts = longopts;
+ result->hdr.datatype = datatype ? datatype : "<double>";
+ result->hdr.glossary = glossary;
+ result->hdr.mincount = mincount;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = (arg_scanfn*)scanfn;
+ result->hdr.checkfn = (arg_checkfn*)checkfn;
+ result->hdr.errorfn = (arg_errorfn*)errorfn;
+
+ /* store the dval[maxcount] array immediately after the arg_dbl struct */
+ result->dval = (double*)(result+1);
+ result->count = 0;
+ }
+ /*printf("arg_dbln() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/arg_end.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_end.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_end.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,124 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* #ifdef HAVE_STDLIB_H */
+#include <stdlib.h>
+/* #endif */
+
+#include "argtable2.h"
+
+static void resetfn(struct arg_end *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count = 0;
+ }
+
+static void errorfn(void *parent, FILE *fp, int error, const char *argval, const char *progname)
+ {
+ progname = progname ? progname : "";
+ argval = argval ? argval : "";
+
+ fprintf(fp,"%s: ",progname);
+ switch(error)
+ {
+ case ARG_ELIMIT:
+ fputs("too many errors to display",fp);
+ break;
+ case ARG_EMALLOC:
+ fputs("insufficent memory",fp);
+ break;
+ case ARG_ENOMATCH:
+ fprintf(fp,"unexpected argument \"%s\"",argval);
+ break;
+ case ARG_EMISSARG:
+ fprintf(fp,"option \"%s\" requires an argument",argval);
+ break;
+ case ARG_ELONGOPT:
+ fprintf(fp,"invalid option \"%s\"",argval);
+ break;
+ default:
+ fprintf(fp,"invalid option \"-%c\"",error);
+ break;
+ }
+ fputc('\n',fp);
+ }
+
+
+struct arg_end* arg_end(int maxcount)
+ {
+ size_t nbytes;
+ struct arg_end *result;
+
+ nbytes = sizeof(struct arg_end)
+ + maxcount * sizeof(int) /* storage for int error[maxcount] array*/
+ + maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */
+ + maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */
+
+ result = (struct arg_end*)malloc(nbytes);
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = ARG_TERMINATOR;
+ result->hdr.shortopts = NULL;
+ result->hdr.longopts = NULL;
+ result->hdr.datatype = NULL;
+ result->hdr.glossary = NULL;
+ result->hdr.mincount = 1;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = NULL;
+ result->hdr.checkfn = NULL;
+ result->hdr.errorfn = errorfn;
+
+ /* store error[maxcount] array immediately after struct arg_end */
+ result->error = (int*)(result+1);
+
+ /* store parent[maxcount] array immediately after error[] array */
+ result->parent = (void**)(result->error + maxcount );
+
+ /* store argval[maxcount] array immediately after parent[] array */
+ result->argval = (const char**)(result->parent + maxcount );
+ }
+
+ /*printf("arg_end(%d) returns %p\n",maxcount,result);*/
+ return result;
+ }
+
+
+void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname)
+ {
+ int i;
+ /*printf("arg_errors()\n");*/
+ for (i=0; i<end->count; i++)
+ {
+ struct arg_hdr *errorparent = (struct arg_hdr *)(end->parent[i]);
+ if (errorparent->errorfn)
+ errorparent->errorfn(end->parent[i],fp,end->error[i],end->argval[i],progname);
+ }
+ }
+
+
Added: cs/cigma/branches/cigma-0.9/src/arg_file.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_file.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_file.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,199 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include "argtable2.h"
+
+#ifdef WIN32
+# define FILESEPARATOR '\\'
+#else
+# define FILESEPARATOR '/'
+#endif
+
+/* local error codes */
+enum {EMINCOUNT=1,EMAXCOUNT};
+
+
+static void resetfn(struct arg_file *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count=0;
+ }
+
+
+/* Returns ptr to the base filename within *filename */
+static const char* arg_basename(const char *filename)
+ {
+ const char *result = (filename ? strrchr(filename,FILESEPARATOR) : NULL);
+ if (result)
+ result++;
+ else
+ result = filename;
+ return result;
+ }
+
+
+/* Returns ptr to the file extension within *filename */
+static const char* arg_extension(const char *filename)
+ {
+ const char *result = (filename ? strrchr(filename,'.') : NULL);
+ if (filename && !result)
+ result = filename+strlen(filename);
+ return result;
+ }
+
+
+static int scanfn(struct arg_file *parent, const char *argval)
+ {
+ int errorcode = 0;
+
+ if (parent->count == parent->hdr.maxcount)
+ {
+ /* maximum number of arguments exceeded */
+ errorcode = EMAXCOUNT;
+ }
+ else if (!argval)
+ {
+ /* a valid argument with no argument value was given. */
+ /* This happens when an optional argument value was invoked. */
+ /* leave parent arguiment value unaltered but still count the argument. */
+ parent->count++;
+ }
+ else
+ {
+ parent->filename[parent->count] = argval;
+ parent->basename[parent->count] = arg_basename(argval);
+ parent->extension[parent->count] = arg_extension(argval);
+ parent->count++;
+ }
+
+ /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+
+static int checkfn(struct arg_file *parent)
+ {
+ int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
+ /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+
+static void errorfn(struct arg_file *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
+ {
+ const char *shortopts = parent->hdr.shortopts;
+ const char *longopts = parent->hdr.longopts;
+ const char *datatype = parent->hdr.datatype;
+
+ /* make argval NULL safe */
+ argval = argval ? argval : "";
+
+ fprintf(fp,"%s: ",progname);
+ switch(errorcode)
+ {
+ case EMINCOUNT:
+ fputs("missing option ",fp);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+
+ case EMAXCOUNT:
+ fputs("excess option ",fp);
+ arg_print_option(fp,shortopts,longopts,argval,"\n");
+ break;
+
+ default:
+ fprintf(fp,"unknown error at \"%s\"\n",argval);
+ }
+ }
+
+
+struct arg_file* arg_file0(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_filen(shortopts,longopts,datatype,0,1,glossary);
+ }
+
+
+struct arg_file* arg_file1(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_filen(shortopts,longopts,datatype,1,1,glossary);
+ }
+
+
+struct arg_file* arg_filen(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary)
+ {
+ size_t nbytes;
+ struct arg_file *result;
+
+ nbytes = sizeof(struct arg_file) /* storage for struct arg_file */
+ + sizeof(char*) * maxcount /* storage for filename[maxcount] array */
+ + sizeof(char*) * maxcount /* storage for basename[maxcount] array */
+ + sizeof(char*) * maxcount; /* storage for extension[maxcount] array */
+
+ result = (struct arg_file*)malloc(nbytes);
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = ARG_HASVALUE;
+ result->hdr.shortopts = shortopts;
+ result->hdr.longopts = longopts;
+ result->hdr.glossary = glossary;
+ result->hdr.datatype = datatype ? datatype : "<file>";
+ result->hdr.mincount = mincount;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = (arg_scanfn*)scanfn;
+ result->hdr.checkfn = (arg_checkfn*)checkfn;
+ result->hdr.errorfn = (arg_errorfn*)errorfn;
+
+ /* store the filename,basename,extension arrays immediately after the arg_file struct */
+ result->filename = (const char**)(result+1);
+ result->basename = result->filename + maxcount;
+ result->extension = result->basename + maxcount;
+ result->count = 0;
+ }
+ /*printf("arg_filen() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/arg_int.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_int.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_int.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,167 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/* #ifdef HAVE_STDLIB_H */
+#include <stdlib.h>
+/* #endif */
+
+#include "argtable2.h"
+
+/* local error codes */
+enum {EMINCOUNT=1,EMAXCOUNT,EBADINT};
+
+static void resetfn(struct arg_int *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count=0;
+ }
+
+static int scanfn(struct arg_int *parent, const char *argval)
+ {
+ int errorcode = 0;
+
+ if (parent->count == parent->hdr.maxcount)
+ {
+ /* maximum number of arguments exceeded */
+ errorcode = EMAXCOUNT;
+ }
+ else if (!argval)
+ {
+ /* a valid argument with no argument value was given. */
+ /* This happens when an optional argument value was invoked. */
+ /* leave parent arguiment value unaltered but still count the argument. */
+ parent->count++;
+ }
+ else
+ {
+ int val;
+ char *end;
+
+ /* extract base10 integer from argval into val */
+ val = (int)strtol(argval,&end,10);
+
+ /* if success then store result in parent->ival[] array otherwise return error*/
+ if (*end==0)
+ parent->ival[parent->count++] = val;
+ else
+ errorcode = EBADINT;
+ }
+
+ /* printf("%s:scanfn(%p,%p) returns %d\n",__FILE__,parent,argval,errorcode); */
+ return errorcode;
+ }
+
+static int checkfn(struct arg_int *parent)
+ {
+ int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
+ /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static void errorfn(struct arg_int *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
+ {
+ const char *shortopts = parent->hdr.shortopts;
+ const char *longopts = parent->hdr.longopts;
+ const char *datatype = parent->hdr.datatype;
+
+ /* make argval NULL safe */
+ argval = argval ? argval : "";
+
+ fprintf(fp,"%s: ",progname);
+ switch(errorcode)
+ {
+ case EMINCOUNT:
+ fputs("missing option ",fp);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+
+ case EMAXCOUNT:
+ fputs("excess option ",fp);
+ arg_print_option(fp,shortopts,longopts,argval,"\n");
+ break;
+
+ case EBADINT:
+ fprintf(fp,"invalid argument \"%s\" to option ",argval);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+ }
+ }
+
+
+struct arg_int* arg_int0(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_intn(shortopts,longopts,datatype,0,1,glossary);
+ }
+
+struct arg_int* arg_int1(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_intn(shortopts,longopts,datatype,1,1,glossary);
+ }
+
+
+struct arg_int* arg_intn(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary)
+ {
+ size_t nbytes;
+ struct arg_int *result;
+
+ nbytes = sizeof(struct arg_int) /* storage for struct arg_int */
+ + maxcount * sizeof(int); /* storage for ival[maxcount] array */
+
+ result = (struct arg_int*)malloc(nbytes);
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = ARG_HASVALUE;
+ result->hdr.shortopts = shortopts;
+ result->hdr.longopts = longopts;
+ result->hdr.datatype = datatype ? datatype : "<int>";
+ result->hdr.glossary = glossary;
+ result->hdr.mincount = mincount;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = (arg_scanfn*)scanfn;
+ result->hdr.checkfn = (arg_checkfn*)checkfn;
+ result->hdr.errorfn = (arg_errorfn*)errorfn;
+
+ /* store the ival[maxcount] array immediately after the arg_int struct */
+ result->ival = (int*)(result+1);
+ result->count = 0;
+ }
+ /*printf("arg_intn() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/arg_lit.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_lit.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_lit.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,120 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include "argtable2.h"
+
+/* local error codes */
+enum {EMINCOUNT=1,EMAXCOUNT};
+
+static void resetfn(struct arg_lit *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count = 0;
+ }
+
+static int scanfn(struct arg_lit *parent, const char *argval)
+ {
+ int errorcode = 0;
+ if (parent->count < parent->hdr.maxcount )
+ parent->count++;
+ else
+ errorcode = EMAXCOUNT;
+ /*printf("%s:scanfn(%p,%s) returns %d\n",__FILE__,parent,argval,errorcode);*/
+ return errorcode;
+ }
+
+static int checkfn(struct arg_lit *parent)
+ {
+ int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
+ /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static void errorfn(struct arg_lit *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
+ {
+ const char *shortopts = parent->hdr.shortopts;
+ const char *longopts = parent->hdr.longopts;
+ const char *datatype = parent->hdr.datatype;
+
+ switch(errorcode)
+ {
+ case EMINCOUNT:
+ fprintf(fp,"%s: missing option ",progname);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ fprintf(fp,"\n");
+ break;
+
+ case EMAXCOUNT:
+ fprintf(fp,"%s: extraneous option ",progname);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+ }
+ }
+
+struct arg_lit* arg_lit0(const char* shortopts,
+ const char* longopts,
+ const char* glossary)
+ {return arg_litn(shortopts,longopts,0,1,glossary);}
+
+struct arg_lit* arg_lit1(const char* shortopts,
+ const char* longopts,
+ const char* glossary)
+ {return arg_litn(shortopts,longopts,1,1,glossary);}
+
+
+struct arg_lit* arg_litn(const char* shortopts,
+ const char* longopts,
+ int mincount,
+ int maxcount,
+ const char *glossary)
+ {
+ struct arg_lit *result = (struct arg_lit*)malloc(sizeof(struct arg_lit));
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = 0;
+ result->hdr.shortopts = shortopts;
+ result->hdr.longopts = longopts;
+ result->hdr.datatype = NULL;
+ result->hdr.glossary = glossary;
+ result->hdr.mincount = mincount;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = (arg_scanfn*)scanfn;
+ result->hdr.checkfn = (arg_checkfn*)checkfn;
+ result->hdr.errorfn = (arg_errorfn*)errorfn;
+
+ /* init local variables */
+ result->count = 0;
+ }
+ /*printf("arg_litn() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/arg_rem.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_rem.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_rem.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,55 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+/*#ifdef HAVE_STDLIB_H*/
+#include <stdlib.h>
+/*#endif*/
+
+#include "argtable2.h"
+
+struct arg_rem* arg_rem(const char *datatype,
+ const char *glossary)
+ {
+ struct arg_rem *result = (struct arg_rem*)malloc(sizeof(struct arg_rem));
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = 0;
+ result->hdr.shortopts = NULL;
+ result->hdr.longopts = NULL;
+ result->hdr.datatype = datatype;
+ result->hdr.glossary = glossary;
+ result->hdr.mincount = 1;
+ result->hdr.maxcount = 1;
+ result->hdr.parent = result;
+ result->hdr.resetfn = NULL;
+ result->hdr.scanfn = NULL;
+ result->hdr.checkfn = NULL;
+ result->hdr.errorfn = NULL;
+ }
+ /*printf("arg_rem() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/arg_str.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/arg_str.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/arg_str.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,152 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include "argtable2.h"
+
+/* local error codes */
+enum {EMINCOUNT=1,EMAXCOUNT};
+
+static void resetfn(struct arg_str *parent)
+ {
+ /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
+ parent->count=0;
+ }
+
+static int scanfn(struct arg_str *parent, const char *argval)
+ {
+ int errorcode = 0;
+
+ if (parent->count == parent->hdr.maxcount)
+ {
+ /* maximum number of arguments exceeded */
+ errorcode = EMAXCOUNT;
+ }
+ else if (!argval)
+ {
+ /* a valid argument with no argument value was given. */
+ /* This happens when an optional argument value was invoked. */
+ /* leave parent arguiment value unaltered but still count the argument. */
+ parent->count++;
+ }
+ else
+ {
+ parent->sval[parent->count++] = argval;
+ }
+
+ /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static int checkfn(struct arg_str *parent)
+ {
+ int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
+ /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
+ return errorcode;
+ }
+
+static void errorfn(struct arg_str *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
+ {
+ const char *shortopts = parent->hdr.shortopts;
+ const char *longopts = parent->hdr.longopts;
+ const char *datatype = parent->hdr.datatype;
+
+ /* make argval NULL safe */
+ argval = argval ? argval : "";
+
+ fprintf(fp,"%s: ",progname);
+ switch(errorcode)
+ {
+ case EMINCOUNT:
+ fputs("missing option ",fp);
+ arg_print_option(fp,shortopts,longopts,datatype,"\n");
+ break;
+
+ case EMAXCOUNT:
+ fputs("excess option ",fp);
+ arg_print_option(fp,shortopts,longopts,argval,"\n");
+ break;
+ }
+ }
+
+
+struct arg_str* arg_str0(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_strn(shortopts,longopts,datatype,0,1,glossary);
+ }
+
+struct arg_str* arg_str1(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ const char *glossary)
+ {
+ return arg_strn(shortopts,longopts,datatype,1,1,glossary);
+ }
+
+
+struct arg_str* arg_strn(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary)
+ {
+ size_t nbytes;
+ struct arg_str *result;
+
+ nbytes = sizeof(struct arg_str) /* storage for struct arg_str */
+ + maxcount * sizeof(char*); /* storage for sval[maxcount] array */
+
+ result = (struct arg_str*)malloc(nbytes);
+ if (result)
+ {
+ /* init the arg_hdr struct */
+ result->hdr.flag = ARG_HASVALUE;
+ result->hdr.shortopts = shortopts;
+ result->hdr.longopts = longopts;
+ result->hdr.datatype = datatype ? datatype : "<string>";
+ result->hdr.glossary = glossary;
+ result->hdr.mincount = mincount;
+ result->hdr.maxcount = maxcount;
+ result->hdr.parent = result;
+ result->hdr.resetfn = (arg_resetfn*)resetfn;
+ result->hdr.scanfn = (arg_scanfn*)scanfn;
+ result->hdr.checkfn = (arg_checkfn*)checkfn;
+ result->hdr.errorfn = (arg_errorfn*)errorfn;
+
+ /* store the sval[maxcount] array immediately after the arg_str struct */
+ result->sval = (const char**)(result+1);
+ result->count = 0;
+ }
+ /*printf("arg_strn() returns %p\n",result);*/
+ return result;
+ }
Added: cs/cigma/branches/cigma-0.9/src/argtable2.c
===================================================================
--- cs/cigma/branches/cigma-0.9/src/argtable2.c 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/argtable2.c 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,1125 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+
+/* config.h must be included before anything else */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef STDC_HEADERS
+#include <stdlib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#endif
+
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+#include "./getopt.h"
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include "argtable2.h"
+#include "./getopt.h"
+
+static
+void arg_register_error(struct arg_end *end, void *parent, int error, const char *argval)
+ {
+ /* printf("arg_register_error(%p,%p,%d,%s)\n",end,parent,error,argval); */
+ if (end->count < end->hdr.maxcount)
+ {
+ end->error[end->count] = error;
+ end->parent[end->count] = parent;
+ end->argval[end->count] = argval;
+ end->count++;
+ }
+ else
+ {
+ end->error[end->hdr.maxcount-1] = ARG_ELIMIT;
+ end->parent[end->hdr.maxcount-1] = end;
+ end->argval[end->hdr.maxcount-1] = NULL;
+ }
+ }
+
+
+/*
+ * Return index of first table entry with a matching short option
+ * or -1 if no match was found.
+ */
+static
+int find_shortoption(struct arg_hdr **table, char shortopt)
+ {
+ int tabindex;
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ if (table[tabindex]->shortopts && strchr(table[tabindex]->shortopts,shortopt))
+ return tabindex;
+ }
+ return -1;
+ }
+
+
+struct longoptions
+ {
+ int getoptval;
+ int noptions;
+ struct option *options;
+ };
+
+#ifndef NDEBUG
+static
+void dump_longoptions(struct longoptions* longoptions)
+ {
+ int i;
+ printf("getoptval = %d\n", longoptions->getoptval);
+ printf("noptions = %d\n", longoptions->noptions);
+ for (i=0; i<longoptions->noptions; i++)
+ {
+ printf("options[%d].name = \"%s\"\n", i, longoptions->options[i].name);
+ printf("options[%d].has_arg = %d\n", i, longoptions->options[i].has_arg);
+ printf("options[%d].flag = %p\n", i, longoptions->options[i].flag);
+ printf("options[%d].val = %d\n", i, longoptions->options[i].val);
+ }
+ }
+#endif
+
+static
+struct longoptions* alloc_longoptions(struct arg_hdr **table)
+ {
+ struct longoptions *result;
+ size_t nbytes;
+ int noptions = 1;
+ size_t longoptlen = 0;
+ int tabindex;
+
+ /*
+ * Determine the total number of option structs required
+ * by counting the number of comma separated long options
+ * in all table entries and return the count in noptions.
+ * note: noptions starts at 1 not 0 because we getoptlong
+ * requires a NULL option entry to terminate the option array.
+ * While we are at it, count the number of chars required
+ * to store private copies of all the longoption strings
+ * and return that count in logoptlen.
+ */
+ tabindex=0;
+ do
+ {
+ const char *longopts = table[tabindex]->longopts;
+ longoptlen += (longopts?strlen(longopts):0) + 1;
+ while (longopts)
+ {
+ noptions++;
+ longopts = strchr(longopts+1,',');
+ }
+ }while(!(table[tabindex++]->flag&ARG_TERMINATOR));
+ /*printf("%d long options consuming %d chars in total\n",noptions,longoptlen);*/
+
+
+ /* allocate storage for return data structure as: */
+ /* (struct longoptions) + (struct options)[noptions] + char[longoptlen] */
+ nbytes = sizeof(struct longoptions)
+ + sizeof(struct option)*noptions
+ + longoptlen;
+ result = (struct longoptions*)malloc(nbytes);
+ if (result)
+ {
+ int option_index=0;
+ char *store;
+
+ result->getoptval=0;
+ result->noptions = noptions;
+ result->options = (struct option*)(result + 1);
+ store = (char*)(result->options + noptions);
+
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ const char *longopts = table[tabindex]->longopts;
+
+ while(longopts && *longopts)
+ {
+ char *storestart = store;
+
+ /* copy progressive longopt strings into the store */
+ while (*longopts!=0 && *longopts!=',')
+ *store++ = *longopts++;
+ *store++ = 0;
+ if (*longopts==',')
+ longopts++;
+ /*fprintf(stderr,"storestart=\"%s\"\n",storestart);*/
+
+ result->options[option_index].name = storestart;
+ result->options[option_index].flag = &(result->getoptval);
+ result->options[option_index].val = tabindex;
+ if (table[tabindex]->flag & ARG_HASOPTVALUE)
+ result->options[option_index].has_arg = 2;
+ else if (table[tabindex]->flag & ARG_HASVALUE)
+ result->options[option_index].has_arg = 1;
+ else
+ result->options[option_index].has_arg = 0;
+
+ option_index++;
+ }
+ }
+ /* terminate the options array with a zero-filled entry */
+ result->options[option_index].name = 0;
+ result->options[option_index].has_arg = 0;
+ result->options[option_index].flag = 0;
+ result->options[option_index].val = 0;
+ }
+
+ /*dump_longoptions(result);*/
+ return result;
+ }
+
+static
+char* alloc_shortoptions(struct arg_hdr **table)
+ {
+ char *result;
+ size_t len = 2;
+ int tabindex;
+
+ /* determine the total number of option chars required */
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ struct arg_hdr *hdr = table[tabindex];
+ len += 3 * (hdr->shortopts?strlen(hdr->shortopts):0);
+ }
+
+ result = malloc(len);
+ if (result)
+ {
+ char *res = result;
+
+ /* add a leading ':' so getopt return codes distinguish */
+ /* unrecognised option and options missing argument values */
+ *res++=':';
+
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ struct arg_hdr *hdr = table[tabindex];
+ const char *shortopts = hdr->shortopts;
+ while(shortopts && *shortopts)
+ {
+ *res++ = *shortopts++;
+ if (hdr->flag & ARG_HASVALUE)
+ *res++=':';
+ if (hdr->flag & ARG_HASOPTVALUE)
+ *res++=':';
+ }
+ }
+ /* null terminate the string */
+ *res=0;
+ }
+
+ /*printf("alloc_shortoptions() returns \"%s\"\n",(result?result:"NULL"));*/
+ return result;
+ }
+
+
+/* return index of the table terminator entry */
+static
+int arg_endindex(struct arg_hdr **table)
+ {
+ int tabindex=0;
+ while (!(table[tabindex]->flag&ARG_TERMINATOR))
+ tabindex++;
+ return tabindex;
+ }
+
+
+static
+void arg_parse_tagged(int argc, char **argv, struct arg_hdr **table, struct arg_end *endtable)
+ {
+ struct longoptions *longoptions;
+ char *shortoptions;
+ int copt;
+
+ /*printf("arg_parse_tagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/
+
+ /* allocate short and long option arrays for the given opttable[]. */
+ /* if the allocs fail then put an error msg in the last table entry. */
+ longoptions = alloc_longoptions(table);
+ shortoptions = alloc_shortoptions(table);
+ if (!longoptions || !shortoptions)
+ {
+ /* one or both memory allocs failed */
+ arg_register_error(endtable,endtable,ARG_EMALLOC,NULL);
+ /* free anything that was allocated (this is null safe) */
+ free(shortoptions);
+ free(longoptions);
+ return;
+ }
+
+ /*dump_longoptions(longoptions);*/
+
+ /* reset getopts internal option-index to zero, and disable error reporting */
+ optind = 0;
+ opterr = 0;
+
+ /* fetch and process args using getopt_long */
+ while( (copt=getopt_long(argc,argv,shortoptions,longoptions->options,NULL)) != -1)
+ {
+ /*
+ printf("optarg='%s'\n",optarg);
+ printf("optind=%d\n",optind);
+ printf("copt=%c\n",(char)copt);
+ printf("optopt=%c (%d)\n",optopt, (int)(optopt));
+ */
+ switch(copt)
+ {
+ case 0:
+ {
+ int tabindex = longoptions->getoptval;
+ void *parent = table[tabindex]->parent;
+ /*printf("long option detected from argtable[%d]\n", tabindex);*/
+ if (optarg && optarg[0]==0 && (table[tabindex]->flag & ARG_HASVALUE))
+ {
+ printf(": long option %s requires an argument\n",argv[optind-1]);
+ arg_register_error(endtable,endtable,ARG_EMISSARG,argv[optind-1]);
+ }
+ else if (table[tabindex]->scanfn)
+ {
+ int errorcode = table[tabindex]->scanfn(parent,optarg);
+ if (errorcode!=0)
+ arg_register_error(endtable,parent,errorcode,optarg);
+ }
+ }
+ break;
+
+ case '?':
+ /*
+ * getarg_long() found an unrecognised short option.
+ * if it was a short option its value is in optopt
+ * if it was a long option then optopt=0
+ */
+ switch (optopt)
+ {
+ case 0:
+ /*printf("?0 unrecognised long option %s\n",argv[optind-1]);*/
+ arg_register_error(endtable,endtable,ARG_ELONGOPT,argv[optind-1]);
+ break;
+ default:
+ /*printf("?* unrecognised short option '%c'\n",optopt);*/
+ arg_register_error(endtable,endtable,optopt,NULL);
+ break;
+ }
+ break;
+
+ case':':
+ /*
+ * getarg_long() found an option with its argument missing
+ */
+ printf(": option %s requires an argument\n",argv[optind-1]);
+ arg_register_error(endtable,endtable,ARG_EMISSARG,argv[optind-1]);
+ break;
+
+ default:
+ {
+ /* getopt_long() found a valid short option */
+ int tabindex = find_shortoption(table,(char)copt);
+ /*printf("short option detected from argtable[%d]\n", tabindex);*/
+ if (tabindex==-1)
+ {
+ /* should never get here - but handle it just in case */
+ /*printf("unrecognised short option %d\n",copt);*/
+ arg_register_error(endtable,endtable,copt,NULL);
+ }
+ else
+ {
+ if (table[tabindex]->scanfn)
+ {
+ void *parent = table[tabindex]->parent;
+ int errorcode = table[tabindex]->scanfn(parent,optarg);
+ if (errorcode!=0)
+ arg_register_error(endtable,parent,errorcode,optarg);
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ free(shortoptions);
+ free(longoptions);
+ }
+
+
+static
+void arg_parse_untagged(int argc, char **argv, struct arg_hdr **table, struct arg_end *endtable)
+ {
+ int tabindex=0;
+ int errorlast=0;
+ const char *optarglast = NULL;
+ void *parentlast = NULL;
+
+ /*printf("arg_parse_untagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/
+ while (!(table[tabindex]->flag&ARG_TERMINATOR))
+ {
+ void *parent;
+ int errorcode;
+
+ /* if we have exhausted our argv[optind] entries then we have finished */
+ if (optind>=argc)
+ {
+ /*printf("arg_parse_untagged(): argv[] exhausted\n");*/
+ return;
+ }
+
+ /* skip table entries with non-null long or short options (they are not untagged entries) */
+ if (table[tabindex]->longopts || table[tabindex]->shortopts)
+ {
+ /*printf("arg_parse_untagged(): skipping argtable[%d] (tagged argument)\n",tabindex);*/
+ tabindex++;
+ continue;
+ }
+
+ /* skip table entries with NULL scanfn */
+ if (!(table[tabindex]->scanfn))
+ {
+ /*printf("arg_parse_untagged(): skipping argtable[%d] (NULL scanfn)\n",tabindex);*/
+ tabindex++;
+ continue;
+ }
+
+ /* attempt to scan the current argv[optind] with the current */
+ /* table[tabindex] entry. If it succeeds then keep it, otherwise */
+ /* try again with the next table[] entry. */
+ parent = table[tabindex]->parent;
+ errorcode = table[tabindex]->scanfn(parent,argv[optind]);
+ if (errorcode==0)
+ {
+ /* success, move onto next argv[optind] but stay with same table[tabindex] */
+ /*printf("arg_parse_untagged(): argtable[%d] successfully matched\n",tabindex);*/
+ optind++;
+
+ /* clear the last tentative error */
+ errorlast = 0;
+ }
+ else
+ {
+ /* failure, try same argv[optind] with next table[tabindex] entry */
+ /*printf("arg_parse_untagged(): argtable[%d] failed match\n",tabindex);*/
+ tabindex++;
+
+ /* remember this as a tentative error we may wish to reinstate later */
+ errorlast = errorcode;
+ optarglast = argv[optind];
+ parentlast = parent;
+ }
+
+ }
+
+ /* if a tenative error still remains at this point then register it as a proper error */
+ if (errorlast)
+ {
+ arg_register_error(endtable,parentlast,errorlast,optarglast);
+ optind++;
+ }
+
+ /* only get here when not all argv[] entries were consumed */
+ /* register an error for each unused argv[] entry */
+ while (optind<argc)
+ {
+ /*printf("arg_parse_untagged(): argv[%d]=\"%s\" not consumed\n",optind,argv[optind]);*/
+ arg_register_error(endtable,endtable,ARG_ENOMATCH,argv[optind++]);
+ }
+
+ return;
+ }
+
+
+static
+void arg_parse_check(struct arg_hdr **table, struct arg_end *endtable)
+ {
+ int tabindex=0;
+ do
+ {
+ if (table[tabindex]->checkfn)
+ {
+ void *parent = table[tabindex]->parent;
+ int errorcode = table[tabindex]->checkfn(parent);
+ if (errorcode!=0)
+ arg_register_error(endtable,parent,errorcode,NULL);
+ }
+ }while(!(table[tabindex++]->flag&ARG_TERMINATOR));
+ }
+
+
+static
+void arg_reset(void **argtable)
+ {
+ struct arg_hdr **table=(struct arg_hdr**)argtable;
+ int tabindex=0;
+ /*printf("arg_reset(%p)\n",argtable);*/
+ do
+ {
+ if (table[tabindex]->resetfn)
+ table[tabindex]->resetfn(table[tabindex]->parent);
+ } while(!(table[tabindex++]->flag&ARG_TERMINATOR));
+ }
+
+
+int arg_parse(int argc, char **argv, void **argtable)
+ {
+ struct arg_hdr **table = (struct arg_hdr **)argtable;
+ struct arg_end *endtable;
+ int endindex;
+ char **argvcopy = NULL;
+
+ /*printf("arg_parse(%d,%p,%p)\n",argc,argv,argtable);*/
+
+ /* reset any argtable data from previous invocations */
+ arg_reset(argtable);
+
+ /* locate the first end-of-table marker within the array */
+ endindex = arg_endindex(table);
+ endtable = (struct arg_end*)table[endindex];
+
+ /* alloc space for a local copy of argv[] */
+ argvcopy = malloc(sizeof(char *) * argc);
+ if (argvcopy)
+ {
+ int i;
+
+ /*
+ Fill in the local copy of argv[]. We need a local copy
+ because getopt rearranges argv[] which adversely affects
+ susbsequent parsing attempts.
+ */
+ for (i=0; i<argc; i++)
+ argvcopy[i] = argv[i];
+
+ /* parse the command line (local copy) for tagged options */
+ arg_parse_tagged(argc,argvcopy,table,endtable);
+
+ /* parse the command line (local copy) for untagged options */
+ arg_parse_untagged(argc,argvcopy,table,endtable);
+
+ /* if no errors so far then perform post-parse checks otherwise dont bother */
+ if (endtable->count==0)
+ arg_parse_check(table,endtable);
+
+ /* release the local copt of argv[] */
+ free(argvcopy);
+ }
+ else
+ {
+ /* memory alloc failed */
+ arg_register_error(endtable,endtable,ARG_EMALLOC,NULL);
+ }
+
+ return endtable->count;
+ }
+
+
+/*
+ * Concatenate contents of src[] string onto *pdest[] string.
+ * The *pdest pointer is altered to point to the end of the
+ * target string and *pndest is decremented by the same number
+ * of chars.
+ * Does not append more than *pndest chars into *pdest[]
+ * so as to prevent buffer overruns.
+ * Its something like strncat() but more efficient for repeated
+ * calls on the same destination string.
+ * Example of use:
+ * char dest[30] = "good"
+ * size_t ndest = sizeof(dest);
+ * char *pdest = dest;
+ * arg_char(&pdest,"bye ",&ndest);
+ * arg_char(&pdest,"cruel ",&ndest);
+ * arg_char(&pdest,"world!",&ndest);
+ * Results in:
+ * dest[] == "goodbye cruel world!"
+ * ndest == 10
+ */
+static
+void arg_cat(char **pdest, const char *src, size_t *pndest)
+ {
+ char *dest = *pdest;
+ char *end = dest + *pndest;
+
+ /*locate null terminator of dest string */
+ while(dest<end && *dest!=0)
+ dest++;
+
+ /* concat src string to dest string */
+ while(dest<end && *src!=0)
+ *dest++ = *src++;
+
+ /* null terminate dest string */
+ *dest=0;
+
+ /* update *pdest and *pndest */
+ *pndest = end - dest;
+ *pdest = dest;
+ }
+
+
+static
+void arg_cat_option(char *dest, size_t ndest, const char *shortopts, const char *longopts, const char *datatype, int optvalue)
+ {
+ if (shortopts)
+ {
+ char option[3];
+
+ /* note: option array[] is initialiazed dynamically here to satisfy */
+ /* a deficiency in the watcom compiler wrt static array initializers. */
+ option[0] = '-';
+ option[1] = shortopts[0];
+ option[2] = 0;
+
+ arg_cat(&dest,option,&ndest);
+ if (datatype)
+ {
+ arg_cat(&dest," ",&ndest);
+ if (optvalue)
+ {
+ arg_cat(&dest,"[",&ndest);
+ arg_cat(&dest,datatype,&ndest);
+ arg_cat(&dest,"]",&ndest);
+ }
+ else
+ arg_cat(&dest,datatype,&ndest);
+ }
+ }
+ else if (longopts)
+ {
+ size_t ncspn;
+
+ /* add "--" tag prefix */
+ arg_cat(&dest,"--",&ndest);
+
+ /* add comma separated option tag */
+ ncspn = strcspn(longopts,",");
+ strncat(dest,longopts,(ncspn<ndest)?ncspn:ndest);
+
+ if (datatype)
+ {
+ arg_cat(&dest,"=",&ndest);
+ if (optvalue)
+ {
+ arg_cat(&dest,"[",&ndest);
+ arg_cat(&dest,datatype,&ndest);
+ arg_cat(&dest,"]",&ndest);
+ }
+ else
+ arg_cat(&dest,datatype,&ndest);
+ }
+ }
+ else if (datatype)
+ {
+ if (optvalue)
+ {
+ arg_cat(&dest,"[",&ndest);
+ arg_cat(&dest,datatype,&ndest);
+ arg_cat(&dest,"]",&ndest);
+ }
+ else
+ arg_cat(&dest,datatype,&ndest);
+ }
+ }
+
+static
+void arg_cat_optionv(char *dest, size_t ndest, const char *shortopts, const char *longopts, const char *datatype, int optvalue, const char *separator)
+ {
+ separator = separator ? separator : "";
+
+ if (shortopts)
+ {
+ const char *c = shortopts;
+ while(*c)
+ {
+ /* "-a|-b|-c" */
+ char shortopt[3];
+
+ /* note: shortopt array[] is initialiazed dynamically here to satisfy */
+ /* a deficiency in the watcom compiler wrt static array initializers. */
+ shortopt[0]='-';
+ shortopt[1]=*c;
+ shortopt[2]=0;
+
+ arg_cat(&dest,shortopt,&ndest);
+ if (*++c)
+ arg_cat(&dest,separator,&ndest);
+ }
+ }
+
+ /* put separator between long opts and short opts */
+ if (shortopts && longopts)
+ arg_cat(&dest,separator,&ndest);
+
+ if (longopts)
+ {
+ const char *c = longopts;
+ while(*c)
+ {
+ size_t ncspn;
+
+ /* add "--" tag prefix */
+ arg_cat(&dest,"--",&ndest);
+
+ /* add comma separated option tag */
+ ncspn = strcspn(c,",");
+ strncat(dest,c,(ncspn<ndest)?ncspn:ndest);
+ c+=ncspn;
+
+ /* add given separator in place of comma */
+ if (*c==',')
+ {
+ arg_cat(&dest,separator,&ndest);
+ c++;
+ }
+ }
+ }
+
+ if (datatype)
+ {
+ if (longopts)
+ arg_cat(&dest,"=",&ndest);
+ else if (shortopts)
+ arg_cat(&dest," ",&ndest);
+
+ if (optvalue)
+ {
+ arg_cat(&dest,"[",&ndest);
+ arg_cat(&dest,datatype,&ndest);
+ arg_cat(&dest,"]",&ndest);
+ }
+ else
+ arg_cat(&dest,datatype,&ndest);
+ }
+ }
+
+
+/* this function should be deprecated because it doesnt consider optional argument values (ARG_HASOPTVALUE) */
+void arg_print_option(FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix)
+ {
+ char syntax[200]="";
+ suffix = suffix ? suffix : "";
+
+ /* there is no way of passing the proper optvalue for optional argument values here, so we must ignore it */
+ arg_cat_optionv(syntax,sizeof(syntax),shortopts,longopts,datatype,0,"|");
+
+ fputs(syntax,fp);
+ fputs(suffix,fp);
+ }
+
+
+/*
+ * Print a GNU style [OPTION] string in which all short options that
+ * do not take argument values are presented in abbreviated form, as
+ * in: -xvfsd, or -xvf[sd], or [-xvsfd]
+ */
+static
+void arg_print_gnuswitch(FILE *fp, struct arg_hdr **table)
+ {
+ int tabindex;
+ char *format1=" -%c";
+ char *format2=" [-%c";
+ char *suffix="";
+
+ /* print all mandatory switches that are without argument values */
+ for(tabindex=0; table[tabindex] && !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ /* skip optional options */
+ if (table[tabindex]->mincount<1)
+ continue;
+
+ /* skip non-short options */
+ if (table[tabindex]->shortopts==NULL)
+ continue;
+
+ /* skip options that take argument values */
+ if (table[tabindex]->flag&ARG_HASVALUE)
+ continue;
+
+ /* print the short option (only the first short option char, ignore multiple choices)*/
+ fprintf(fp,format1,table[tabindex]->shortopts[0]);
+ format1="%c";
+ format2="[%c";
+ }
+
+ /* print all optional switches that are without argument values */
+ for(tabindex=0; table[tabindex] && !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ /* skip mandatory args */
+ if (table[tabindex]->mincount>0)
+ continue;
+
+ /* skip args without short options */
+ if (table[tabindex]->shortopts==NULL)
+ continue;
+
+ /* skip args with values */
+ if (table[tabindex]->flag&ARG_HASVALUE)
+ continue;
+
+ /* print first short option */
+ fprintf(fp,format2,table[tabindex]->shortopts[0]);
+ format2="%c";
+ suffix="]";
+ }
+
+ fprintf(fp,suffix);
+ }
+
+
+void arg_print_syntax(FILE *fp, void **argtable, const char *suffix)
+ {
+ struct arg_hdr **table = (struct arg_hdr**)argtable;
+ int i,tabindex;
+
+ /* print GNU style [OPTION] string */
+ arg_print_gnuswitch(fp, table);
+
+ /* print remaining options in abbreviated style */
+ for(tabindex=0; table[tabindex] && !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ char syntax[200]="";
+ const char *shortopts, *longopts, *datatype;
+
+ /* skip short options without arg values (they were printed by arg_print_gnu_switch) */
+ if (table[tabindex]->shortopts && !(table[tabindex]->flag&ARG_HASVALUE))
+ continue;
+
+ shortopts = table[tabindex]->shortopts;
+ longopts = table[tabindex]->longopts;
+ datatype = table[tabindex]->datatype;
+ arg_cat_option(syntax,sizeof(syntax),shortopts,longopts,datatype, table[tabindex]->flag&ARG_HASOPTVALUE);
+
+ if (strlen(syntax)>0)
+ {
+ /* print mandatory instances of this option */
+ for (i=0; i<table[tabindex]->mincount; i++)
+ fprintf(fp, " %s",syntax);
+
+ /* print optional instances enclosed in "[..]" */
+ switch ( table[tabindex]->maxcount - table[tabindex]->mincount )
+ {
+ case 0:
+ break;
+ case 1:
+ fprintf(fp, " [%s]",syntax);
+ break;
+ case 2:
+ fprintf(fp, " [%s] [%s]",syntax,syntax);
+ break;
+ default:
+ fprintf(fp, " [%s]...",syntax);
+ break;
+ }
+ }
+ }
+
+ if (suffix)
+ fprintf(fp, "%s",suffix);
+ }
+
+
+void arg_print_syntaxv(FILE *fp, void **argtable, const char *suffix)
+ {
+ struct arg_hdr **table = (struct arg_hdr**)argtable;
+ int i,tabindex;
+
+ /* print remaining options in abbreviated style */
+ for(tabindex=0; table[tabindex] && !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ char syntax[200]="";
+ const char *shortopts, *longopts, *datatype;
+
+ shortopts = table[tabindex]->shortopts;
+ longopts = table[tabindex]->longopts;
+ datatype = table[tabindex]->datatype;
+ arg_cat_optionv(syntax,sizeof(syntax),shortopts,longopts,datatype,table[tabindex]->flag&ARG_HASOPTVALUE, "|");
+
+ /* print mandatory options */
+ for (i=0; i<table[tabindex]->mincount; i++)
+ fprintf(fp," %s",syntax);
+
+ /* print optional args enclosed in "[..]" */
+ switch ( table[tabindex]->maxcount - table[tabindex]->mincount )
+ {
+ case 0:
+ break;
+ case 1:
+ fprintf(fp, " [%s]",syntax);
+ break;
+ case 2:
+ fprintf(fp, " [%s] [%s]",syntax,syntax);
+ break;
+ default:
+ fprintf(fp, " [%s]...",syntax);
+ break;
+ }
+ }
+
+ if (suffix)
+ fprintf(fp,"%s",suffix);
+ }
+
+
+void arg_print_glossary(FILE *fp, void **argtable, const char *format)
+ {
+ struct arg_hdr **table = (struct arg_hdr**)argtable;
+ int tabindex;
+
+ format = format ? format : " %-20s %s\n";
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ if (table[tabindex]->glossary)
+ {
+ char syntax[200]="";
+ const char *shortopts = table[tabindex]->shortopts;
+ const char *longopts = table[tabindex]->longopts;
+ const char *datatype = table[tabindex]->datatype;
+ const char *glossary = table[tabindex]->glossary;
+ arg_cat_optionv(syntax,sizeof(syntax),shortopts,longopts,datatype,table[tabindex]->flag&ARG_HASOPTVALUE,", ");
+ fprintf(fp,format,syntax,glossary);
+ }
+ }
+ }
+
+
+/**
+ * Print a piece of text formatted, which means in a column with a
+ * left and a right margin. The lines are wrapped at whitspaces next
+ * to right margin. The function does not indent the first line, but
+ * only the following ones.
+ *
+ * Example:
+ * arg_print_formatted( fp, 0, 5, "Some text that doesn't fit." )
+ * will result in the following output:
+ *
+ * Some
+ * text
+ * that
+ * doesn'
+ * t fit.
+ *
+ * Too long lines will be wrapped in the middle of a word.
+ *
+ * arg_print_formatted( fp, 2, 7, "Some text that doesn't fit." )
+ * will result in the following output:
+ *
+ * Some
+ * text
+ * that
+ * doesn'
+ * t fit.
+ *
+ * As you see, the first line is not indented. This enables output of
+ * lines, which start in a line where output already happened.
+ *
+ * Author: Uli Fouquet
+ */
+static
+void arg_print_formatted( FILE *fp, const unsigned lmargin, const unsigned rmargin, const char *text )
+ {
+ const unsigned textlen = strlen( text );
+ unsigned line_start = 0;
+ unsigned line_end = textlen + 1;
+ const unsigned colwidth = (rmargin - lmargin) + 1;
+
+ /* Someone doesn't like us... */
+ if ( line_end < line_start )
+ { fprintf( fp, "%s\n", text ); }
+
+ while (line_end-1 > line_start )
+ {
+ /* Eat leading whitespaces. This is essential because while
+ wrapping lines, there will often be a whitespace at beginning
+ of line */
+ while ( isspace(*(text+line_start)) )
+ { line_start++; }
+
+ if ((line_end - line_start) > colwidth )
+ { line_end = line_start + colwidth; }
+
+ /* Find last whitespace, that fits into line */
+ while ( ( line_end > line_start )
+ && ( line_end - line_start > colwidth )
+ && !isspace(*(text+line_end)))
+ { line_end--; }
+
+ /* Do not print trailing whitespace. If this text
+ has got only one line, line_end now points to the
+ last char due to initialization. */
+ line_end--;
+
+ /* Output line of text */
+ while ( line_start < line_end )
+ {
+ fputc(*(text+line_start), fp );
+ line_start++;
+ }
+ fputc( '\n', fp );
+
+ /* Initialize another line */
+ if ( line_end+1 < textlen )
+ {
+ unsigned i;
+
+ for (i=0; i < lmargin; i++ )
+ { fputc( ' ', fp ); }
+
+ line_end = textlen;
+ }
+
+ /* If we have to print another line, get also the last char. */
+ line_end++;
+
+ } /* lines of text */
+ }
+
+/**
+ * Prints the glossary in strict GNU format.
+ * Differences to arg_print_glossary() are:
+ * - wraps lines after 80 chars
+ * - indents lines without shortops
+ * - does not accept formatstrings
+ *
+ * Contributed by Uli Fouquet
+ */
+void arg_print_glossary_gnu(FILE *fp, void **argtable )
+ {
+ struct arg_hdr **table = (struct arg_hdr**)argtable;
+ int tabindex;
+
+ for(tabindex=0; !(table[tabindex]->flag&ARG_TERMINATOR); tabindex++)
+ {
+ if (table[tabindex]->glossary)
+ {
+ char syntax[200]="";
+ const char *shortopts = table[tabindex]->shortopts;
+ const char *longopts = table[tabindex]->longopts;
+ const char *datatype = table[tabindex]->datatype;
+ const char *glossary = table[tabindex]->glossary;
+
+ if ( !shortopts && longopts )
+ {
+ /* Indent trailing line by 4 spaces... */
+ memset( syntax, ' ', 4 );
+ *(syntax+4) = '\0';
+ }
+
+ arg_cat_optionv(syntax,sizeof(syntax),shortopts,longopts,datatype,table[tabindex]->flag&ARG_HASOPTVALUE,", ");
+
+ /* If syntax fits not into column, print glossary in new line... */
+ if ( strlen(syntax) > 25 )
+ {
+ fprintf( fp, " %-25s %s\n", syntax, "" );
+ *syntax = '\0';
+ }
+
+ fprintf( fp, " %-25s ", syntax );
+ arg_print_formatted( fp, 28, 79, glossary );
+ }
+ } /* for each table entry */
+
+ fputc( '\n', fp );
+ }
+
+
+/**
+ * Checks the argtable[] array for NULL entries and returns 1
+ * if any are found, zero otherwise.
+ */
+int arg_nullcheck(void **argtable)
+ {
+ struct arg_hdr **table = (struct arg_hdr **)argtable;
+ int tabindex;
+ /*printf("arg_nullcheck(%p)\n",argtable);*/
+
+ if (!table)
+ return 1;
+
+ tabindex=0;
+ do
+ {
+ /*printf("argtable[%d]=%p\n",tabindex,argtable[tabindex]);*/
+ if (!table[tabindex])
+ return 1;
+ } while(!(table[tabindex++]->flag&ARG_TERMINATOR));
+
+ return 0;
+ }
+
+
+/*
+ * arg_free() is deprecated in favour of arg_freetable() due to a flaw in its design.
+ * The flaw results in memory leak in the (very rare) case that an intermediate
+ * entry in the argtable array failed its memory allocation while others following
+ * that entry were still allocated ok. Those subsequent allocations will not be
+ * deallocated by arg_free().
+ * Despite the unlikeliness of the problem occurring, and the even unlikelier event
+ * that it has any deliterious effect, it is fixed regardless by replacing arg_free()
+ * with the newer arg_freetable() function.
+ * We still keep arg_free() for backwards compatibility.
+ */
+void arg_free(void **argtable)
+ {
+ struct arg_hdr **table=(struct arg_hdr**)argtable;
+ int tabindex=0;
+ int flag;
+ /*printf("arg_free(%p)\n",argtable);*/
+ do
+ {
+ /*
+ if we encounter a NULL entry then somewhat incorrectly we presume
+ we have come to the end of the array. It isnt strictly true because
+ an intermediate entry could be NULL with other non-NULL entries to follow.
+ The subsequent argtable entries would then not be freed as they should.
+ */
+ if (table[tabindex]==NULL)
+ break;
+
+ flag = table[tabindex]->flag;
+ free(table[tabindex]);
+ table[tabindex++]=NULL;
+
+ } while(!(flag&ARG_TERMINATOR));
+ }
+
+/* frees each non-NULL element of argtable[], where n is the size of the number of entries in the array */
+void arg_freetable(void **argtable, size_t n)
+ {
+ struct arg_hdr **table=(struct arg_hdr**)argtable;
+ int tabindex=0;
+ /*printf("arg_freetable(%p)\n",argtable);*/
+ for (tabindex=0; tabindex<n; tabindex++)
+ {
+ if (table[tabindex]==NULL)
+ continue;
+
+ free(table[tabindex]);
+ table[tabindex]=NULL;
+ };
+ }
+
Added: cs/cigma/branches/cigma-0.9/src/argtable2.h
===================================================================
--- cs/cigma/branches/cigma-0.9/src/argtable2.h 2007-06-26 16:57:53 UTC (rev 7509)
+++ cs/cigma/branches/cigma-0.9/src/argtable2.h 2007-06-26 16:59:15 UTC (rev 7510)
@@ -0,0 +1,304 @@
+/*********************************************************************
+This file is part of the argtable2 library.
+Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
+sheitmann at users.sourceforge.net
+
+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
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+USA.
+**********************************************************************/
+#ifndef ARGTABLE2
+#define ARGTABLE2
+
+#include <stdio.h> /* FILE */
+#include <time.h> /* struct tm */
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* bit masks for arg_hdr.flag */
+enum
+ {
+ ARG_TERMINATOR=0x1,
+ ARG_HASVALUE=0x2,
+ ARG_HASOPTVALUE=0x4
+ };
+
+typedef void (arg_resetfn)(void *parent);
+typedef int (arg_scanfn)(void *parent, const char *argval);
+typedef int (arg_checkfn)(void *parent);
+typedef void (arg_errorfn)(void *parent, FILE *fp, int error, const char *argval, const char *progname);
+
+
+/*
+ * The arg_hdr struct defines properties that are common to all arg_xxx structs.
+ * The argtable library requires each arg_xxx struct to have an arg_hdr
+ * struct as its first data member.
+ * The argtable library functions then use this data to identify the
+ * properties of the command line option, such as its option tags,
+ * datatype string, and glossary strings, and so on.
+ * Moreover, the arg_hdr struct contains pointers to custom functions that
+ * are provided by each arg_xxx struct which perform the tasks of parsing
+ * that particular arg_xxx arguments, performing post-parse checks, and
+ * reporting errors.
+ * These functions are private to the individual arg_xxx source code
+ * and are the pointer to them are initiliased by that arg_xxx struct's
+ * constructor function. The user could alter them after construction
+ * if desired, but the original intention is for them to be set by the
+ * constructor and left unaltered.
+ */
+struct arg_hdr
+ {
+ char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
+ const char *shortopts; /* String defining the short options */
+ const char *longopts; /* String defiing the long options */
+ const char *datatype; /* Description of the argument data type */
+ const char *glossary; /* Description of the option as shown by arg_print_glossary function */
+ int mincount; /* Minimum number of occurences of this option accepted */
+ int maxcount; /* Maximum number of occurences if this option accepted */
+ void *parent; /* Pointer to parent arg_xxx struct */
+ arg_resetfn *resetfn; /* Pointer to parent arg_xxx reset function */
+ arg_scanfn *scanfn; /* Pointer to parent arg_xxx scan function */
+ arg_checkfn *checkfn; /* Pointer to parent arg_xxx check function */
+ arg_errorfn *errorfn; /* Pointer to parent arg_xxx error function */
+ void *priv; /* Pointer to private header data for use by arg_xxx functions */
+ };
+
+struct arg_rem
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ };
+
+struct arg_lit
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args */
+ };
+
+struct arg_int
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args */
+ int *ival; /* Array of parsed argument values */
+ };
+
+struct arg_dbl
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args */
+ double *dval; /* Array of parsed argument values */
+ };
+
+struct arg_str
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args */
+ const char **sval; /* Array of parsed argument values */
+ };
+
+struct arg_rex
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args */
+ const char **sval; /* Array of parsed argument values */
+ };
+
+struct arg_file
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of matching command line args*/
+ const char **filename; /* Array of parsed filenames (eg: /home/foo.bar) */
+ const char **basename; /* Array of parsed basenames (eg: foo.bar) */
+ const char **extension; /* Array of parsed extensions (eg: bar) */
+ };
+
+struct arg_date
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ const char *format; /* strptime format string used to parse the date */
+ int count; /* Number of matching command line args */
+ struct tm *tmval; /* Array of parsed time values */
+ };
+
+enum {ARG_ELIMIT=1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG};
+struct arg_end
+ {
+ struct arg_hdr hdr; /* The mandatory argtable header struct */
+ int count; /* Number of errors encountered */
+ int *error; /* Array of error codes */
+ void **parent; /* Array of pointers to offending arg_xxx struct */
+ const char **argval; /* Array of pointers to offending argv[] string */
+ };
+
+
+/**** arg_xxx constructor functions *********************************/
+
+struct arg_rem* arg_rem(const char* datatype, const char* glossary);
+
+struct arg_lit* arg_lit0(const char* shortopts,
+ const char* longopts,
+ const char* glossary);
+struct arg_lit* arg_lit1(const char* shortopts,
+ const char* longopts,
+ const char *glossary);
+struct arg_lit* arg_litn(const char* shortopts,
+ const char* longopts,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_key* arg_key0(const char* keyword,
+ int flags,
+ const char* glossary);
+struct arg_key* arg_key1(const char* keyword,
+ int flags,
+ const char* glossary);
+struct arg_key* arg_keyn(const char* keyword,
+ int flags,
+ int mincount,
+ int maxcount,
+ const char* glossary);
+
+struct arg_int* arg_int0(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char* glossary);
+struct arg_int* arg_int1(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char *glossary);
+struct arg_int* arg_intn(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_dbl* arg_dbl0(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char* glossary);
+struct arg_dbl* arg_dbl1(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char *glossary);
+struct arg_dbl* arg_dbln(const char* shortopts,
+ const char* longopts,
+ const char *datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_str* arg_str0(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char* glossary);
+struct arg_str* arg_str1(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char *glossary);
+struct arg_str* arg_strn(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_rex* arg_rex0(const char* shortopts,
+ const char* longopts,
+ const char* pattern,
+ const char* datatype,
+ int flags,
+ const char* glossary);
+struct arg_rex* arg_rex1(const char* shortopts,
+ const char* longopts,
+ const char* pattern,
+ const char* datatype,
+ int flags,
+ const char *glossary);
+struct arg_rex* arg_rexn(const char* shortopts,
+ const char* longopts,
+ const char* pattern,
+ const char* datatype,
+ int mincount,
+ int maxcount,
+ int flags,
+ const char *glossary);
+
+struct arg_file* arg_file0(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char* glossary);
+struct arg_file* arg_file1(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ const char *glossary);
+struct arg_file* arg_filen(const char* shortopts,
+ const char* longopts,
+ const char* datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_date* arg_date0(const char* shortopts,
+ const char* longopts,
+ const char* format,
+ const char* datatype,
+ const char* glossary);
+struct arg_date* arg_date1(const char* shortopts,
+ const char* longopts,
+ const char* format,
+ const char* datatype,
+ const char *glossary);
+struct arg_date* arg_daten(const char* shortopts,
+ const char* longopts,
+ const char* format,
+ const char* datatype,
+ int mincount,
+ int maxcount,
+ const char *glossary);
+
+struct arg_end* arg_end(int maxerrors);
+
+
+/**** other functions *******************************************/
+int arg_nullcheck(void **argtable);
+int arg_parse(int argc, char **argv, void **argtable);
+void arg_print_option(FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix);
+void arg_print_syntax(FILE *fp, void **argtable, const char *suffix);
+void arg_print_syntaxv(FILE *fp, void **argtable, const char *suffix);
+void arg_print_glossary(FILE *fp, void **argtable, const char *format);
+void arg_print_glossary_gnu(FILE *fp, void **argtable);
+void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
+void arg_freetable(void **argtable, size_t n);
+
+/**** deprecated functions, for back-compatibility only ********/
+void arg_free(void **argtable);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+
+
+
+
+
+
+
+
+
More information about the cig-commits
mailing list