/* librock/procede/avscan.c librock_CHISEL _summary scan argv-style lists of strings Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************/ #ifdef librock_NOTLIBROCK /**************************************************************/ /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED #include #define librock_PTR #define librock_CONST const #define librock_PRIVATE static #define librock_assert(a) int librock_avscan(int argc, const char **argv, /* argument array */ const char *optKeys, /* blank separated list of accepted options */ const char **ppszOpt, /* Store pointer into optKeys of match*/ const char **ppszValue); /* Store pointer to option value, if any */ #endif /* ifdef librock_NOTLIBROCK */ /**************************************************************/ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_avscan #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_avscan /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_avscan[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_avscan librock_avscan /**************************************************************/ #endif #ifndef librock_NOIMPL_avscan /**************************************************************/ #define librock_FNTYPE_avscan fni_i_av_s_S_S int librock_body_avscan(int argc, librock_CONST char librock_PTR * librock_PTR *argv, /* argument array */ librock_CONST char librock_PTR * optKeys, /* blank separated list accepted options */ librock_CONST char librock_PTR * librock_PTR *ppszOpt, /* Store pointer into optKeys of match*/ librock_CONST char librock_PTR * librock_PTR *ppszValue) /* Store pointer to option value, if any */ { /* Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_avscan /* librock_avscan - Recognize option prefixes and values in an argv[]-like list. */ /**/ #include #include int librock_avscan(int argc, const char **argv, /* argument array */ const char *optKeys, /* blank separated list of accepted options */ const char **ppszOpt, /* Store pointer into optKeys of match*/ const char **ppszValue) /* Store pointer to option value, if any */ /* Recognize and pre-process items in an argv[]-like array, recognizing option prefixes/keywords. Stores a pointer to the recognized option, and if the specified option takes a value, a pointer to the value, which may be located in the immediately following argv[] space. Returns the number of argv[] items to advance, or 0 if nothing was recognized. This replaces optarg(), with a better behaved function that does not use globals or static storage, or write error messages. librock_avscan is designed to be used in contexts other than command line argv[] processing. optKeys defines the accepted options. It is a list of blank separated strings of one or more of the following forms: -a -a= --long-name --long-name= where 'a' may be any single alphabetic character. "long-name" may contain any alpha-numeric character, and '_', and '-'. The suffix of '=' means that a value is always expected to be present. When the key is the short form '-a=', the value is taken to start at the character immediately following the 'a'. If there is no character following the 'a', the entire next argument in the argv input array is the value. If the character following 'a' is '=', the value is everything after the '='. When the key is the long form '--long-option', the value is taken to start after '=', if '=' is not present, the entire next argument in the argv array is the value. Typical use is */ #ifdef librock_TYPICAL_USE_avscan int testargc = 6; char *testargv[7] = { "testargv0", "-b", "-t=value", "--long-t", "-b", /* Argument to previous */ "-b=20", /* Not recognized. -b does not take a value */ 0 }; int iArg = 1; char *pszOpt; char *pszOptVal; while(iArg < testargc) { iArg += librock_avscan(testargc - iArg,&testargv[iArg],"-b -t= --long-t=",&pszOpt,&pszOptVal); if (!pszOpt) { /* Nothing matched on this one. Stop processing*/ break; } if (!librock_wordcmp(pszOpt,"-b")) { printf("Option: -b\n"); } if (!librock_wordcmp(pszOpt,"-t=")||!librock_wordcmp(pszOpt,"--long-t=")) { /* the testargv had one of the following -t=Optval --long-t=Optval -t Optval --long-t Optval -tOptval */ printf("Option: -t=%s\n",pszOptVal); } } #endif /* strncmp() librock_CHISEL _usesrc "./text/count.c" librock_countbl() librock_counttowh() librock_CHISEL _usesrc "./text/compsearch/strfn.c" librock_wordcmp() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ const char *p; int len; if (argc <= 0) { *ppszOpt = 0; return 0; } if (argv[0][0] != '-') { *ppszOpt = 0; return 0; } /* Find it in the option string */ p = optKeys; p += librock_countbl(p); while((len = librock_counttowh(p))) { /* Compare */ if (p[len-1] == '=') { len--; } if (!strncmp(argv[0],p,len)) { if (p[1]=='-') { /* --long-name */ if ((argv[0][len] == '=')&&(p[len]== '=')) { /* Form: --long-name=value */ *ppszOpt = p; *ppszValue = argv[0]+len+1; return 1; } if (!argv[0][len]) { if (p[len]== '=') { /* Form: --long-name value */ *ppszOpt = p; *ppszValue = argv[1]; return 2; } /* Form: --long-name */ *ppszOpt = p; *ppszValue = 0; return 1; } /* otherwise not matched */ } else { /* short */ if (p[len]== '=') { if (!argv[0][len]) { /* Form: -a value */ *ppszOpt = p; *ppszValue = argv[1]; return 2; } else { /* Form: -avalue */ *ppszOpt = p; if (argv[0][len] == '=') { /* 2002-07-31 */ len++; } *ppszValue = argv[0]+len; return 1; } } else if (!argv[0][len]) { /* Test added 2002-07-31 */ /* Form: -a */ *ppszOpt = p; *ppszValue = 0; return 1; } } } p += librock_counttowh(p); p += librock_countbl(p); } *ppszOpt = 0; return 0; } /* librock_avscan */ #endif /* NOIMP */ /* $Log: avscan.c,v $ Revision 1.7 2002/08/01 22:42:14 forrest@mibsoftware.com rights=#1 Tighter test on single-character options without values. Procede.h is obsolete. Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.6 2002/04/09 03:33:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.5 2002/04/06 04:21:52 forrest@mibsoftware.com rights=#1 librock/procede.h is obsolete. Revision 1.4 2002/02/07 17:12:17 forrest@mibsoftware.com rights=#1 fix LIDESC Revision 1.3 2002/02/06 14:03:26 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:07:53 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:35:08 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */