/* exec/process/vararg/prsargv.c version librock_CHISEL _summary argv array creation and concatenation 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 #define librock_PTR #define librock_CONST const /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_prsargv #include #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_prsargv /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ char *librock_LIDESC_prsargv[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /* Function wrapping and tracing features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #define librock_body_prsargv_free librock_prsargv_free #define librock_body_prsargv_split librock_prsargv_split #define librock_body_prsargv_glom librock_prsargv_glom /**************************************************************/ #endif #ifdef librock_MANUAL_prsargv_split /* librock_prsargv_split - malloc and create an argv-style char ** from words in a string librock_prsargv_free - Free an argv-style char **, previously returned from librock_prsargv_split librock_prsargv_glom - Create a string from an argv-style char ** */ /**/ #include int librock_prsargv_split(const char *line, char ***argvp); void librock_prsargv_free(char ***argvp); char * librock_prsargv_glom(const char **av); /* librock_prsargv_split parses a string into words separated by tabs and spaces, creating a NULL-terminated array of pointers to each word. This mallocs space for a copy of the string and the argv array, which should be free'd by eventually calling librock_prsargv_free. This does not change the original string. If *argvp isn't NULL, it will first call librock_prsarg_free. On success, the return value is the number of non-NULL points at *argvp. Otherwise the return is -1 on malloc() failure. There is no recognition of quote, escape, or other special characters. It considers newlines and other characters to be non-spaces. librock_decbe could be helpful to the caller. librock_prsargv_glom mallocs and creates a string by contatenating all strings in a array terminated by NULL, inserting a single space between elements. The return value is NULL on error. Return value on success should eventually be given to free(). Typical use is to break lines read from files, such as configuration files, into a list for making function calls. */ #ifdef librock_TYPICAL_USE_prsargv_split #include char **testargv = 0; /* Be sure to initialize to 0! */ int cnt; cnt = librock_prsargv_split("This is a test",&testargv); if (cnt < 0) { /* Error handling */ } else { /* Process */ int i = 0; while(i < cnt) { printf("%d: '%s'\n",i,testargv[i]); i++; } } /* . . . */ librock_prsargv_free(&testargv); #endif /* malloc free strlen strcpy librock_CHISEL _usesrc "text/count.c" librock_countbl(); */ /* 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 */ #ifndef librock_NOIMPL_prsargv_free /***************************************************************/ void librock_body_prsargv_free(char ***argvp) { /*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/ */ if (**argvp) { free(*argvp); *argvp = 0; } } /***************************************************************/ #endif /* librock_NOIMPL_prsargv_free */ #ifndef librock_NOIMPL_prsargv_split int librock_body_prsargv_split(const char *line, char ***argvp) { /*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/ */ char **argv; char *p; int i; if (*argvp) { librock_prsargv_free(argvp); } /* Copy the line, which we will split up. */ line += librock_countbl(line); i = strlen(line); /* Allocate worst-case amount of space. */ argv = malloc((sizeof(char *) * (i/2+2)) + i + 1); if (!argv) { return -1; } p = (char *) (&argv[i/2+2]); strcpy(p, line); *argvp = argv; while(*p) { *argv++ = p; while(*p && (*p != ' ') && (*p != '\t')) { p++; } if (*p == '\0') { break; } *p++ = '\0'; p += librock_countbl(p); } *argv = 0; return argv - *argvp; } /***************************************************************/ #endif /* librock_NOIMPL_prsargv_split */ #ifndef librock_NOIMPL_prsargv_glom char *librock_body_prsargv_glom(const char **av) { /*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/ */ const char **v; char *ptr; int cb; char *ret; cb = 0; v = av; while(*v) { cb += strlen(*v) + 1; v++; } ret = malloc(cb); if (!ret) { return 0; } ptr = ret; v = av; while(*v) { if (ptr > ret) { *ptr++ = ' '; } strcpy(ptr,*v); ptr += strlen(ptr); v++; } return ret; } /***************************************************************/ #endif /* librock_NOIMPL_prsargv_glom */ /* $Log: prsargv.c,v $ Revision 1.8 2002/08/02 04:04:25 forrest@mibsoftware.com rights=#1 procede.h is obsolete. Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.7 2002/04/09 03:33:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.6 2002/04/06 04:21:53 forrest@mibsoftware.com rights=#1 librock/procede.h is obsolete. Revision 1.5 2002/03/19 20:53:20 forrest@mibsoftware.com rights=#1 Fix manual page Revision 1.4 2002/03/19 20:52:34 forrest@mibsoftware.com rights=#1 Rewrote for drop INN licensing. Revision 1.3 2002/02/06 14:03:27 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:24:27 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 */