|
|
1.1 ! root 1: /* cmdlin.c ! 2: Parse a command line. ! 3: ! 4: Copyright (C) 1992 Ian Lance Taylor ! 5: ! 6: This file is part of the Taylor UUCP uuconf library. ! 7: ! 8: This library is free software; you can redistribute it and/or ! 9: modify it under the terms of the GNU Library General Public License ! 10: as published by the Free Software Foundation; either version 2 of ! 11: the License, or (at your option) any later version. ! 12: ! 13: This library is distributed in the hope that it will be useful, but ! 14: WITHOUT ANY WARRANTY; without even the implied warranty of ! 15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ! 16: Library General Public License for more details. ! 17: ! 18: You should have received a copy of the GNU Library General Public ! 19: License along with this library; if not, write to the Free Software ! 20: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ! 21: ! 22: The author of the program may be contacted at [email protected] or ! 23: c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254. ! 24: */ ! 25: ! 26: #include "uucnfi.h" ! 27: ! 28: #if USE_RCS_ID ! 29: const char _uuconf_cmdlin_rcsid[] = "$Id: cmdlin.c,v 1.1 93/07/30 08:07:05 bin Exp Locker: bin $"; ! 30: #endif ! 31: ! 32: #include <errno.h> ! 33: #include <ctype.h> ! 34: ! 35: /* Parse a command line into fields and process it via a command ! 36: table. The command table functions may keep the memory allocated ! 37: for the line, but they may not keep the memory allocated for the ! 38: argv list. This function strips # comments. */ ! 39: ! 40: #define CSTACK (16) ! 41: ! 42: int ! 43: uuconf_cmd_line (pglobal, zline, qtab, pinfo, pfiunknown, iflags, pblock) ! 44: pointer pglobal; ! 45: char *zline; ! 46: const struct uuconf_cmdtab *qtab; ! 47: pointer pinfo; ! 48: int (*pfiunknown) P((pointer, int, char **, pointer, pointer)); ! 49: int iflags; ! 50: pointer pblock; ! 51: { ! 52: struct sglobal *qglobal = (struct sglobal *) pglobal; ! 53: char *z; ! 54: int cargs; ! 55: char *azargs[CSTACK]; ! 56: char **pzargs; ! 57: int iret; ! 58: ! 59: /* Any # not preceeded by a backslash starts a comment. */ ! 60: z = zline; ! 61: while ((z = strchr (z, '#')) != NULL) ! 62: { ! 63: if (z == zline || *(z - 1) != '\\') ! 64: { ! 65: *z = '\0'; ! 66: break; ! 67: } ! 68: /* Remove the backslash. */ ! 69: while ((*(z - 1) = *z) != '\0') ! 70: ++z; ! 71: } ! 72: ! 73: /* Parse the first CSTACK arguments by hand to avoid malloc. */ ! 74: ! 75: z = zline; ! 76: cargs = 0; ! 77: pzargs = azargs; ! 78: while (TRUE) ! 79: { ! 80: while (*z != '\0' && isspace (BUCHAR (*z))) ! 81: ++z; ! 82: ! 83: if (*z == '\0') ! 84: break; ! 85: ! 86: if (cargs >= CSTACK) ! 87: { ! 88: char **pzsplit; ! 89: size_t csplit; ! 90: int cmore; ! 91: ! 92: pzsplit = NULL; ! 93: csplit = 0; ! 94: cmore = _uuconf_istrsplit (z, '\0', &pzsplit, &csplit); ! 95: if (cmore < 0) ! 96: { ! 97: qglobal->ierrno = errno; ! 98: return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO; ! 99: } ! 100: ! 101: pzargs = (char **) malloc ((cmore + CSTACK) * sizeof (char *)); ! 102: if (pzargs == NULL) ! 103: { ! 104: qglobal->ierrno = errno; ! 105: free ((pointer) pzsplit); ! 106: return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO; ! 107: } ! 108: ! 109: memcpy ((pointer) pzargs, (pointer) azargs, ! 110: CSTACK * sizeof (char *)); ! 111: memcpy ((pointer) (pzargs + CSTACK), (pointer) pzsplit, ! 112: cmore * sizeof (char *)); ! 113: cargs = cmore + CSTACK; ! 114: ! 115: free ((pointer) pzsplit); ! 116: ! 117: break; ! 118: } ! 119: ! 120: azargs[cargs] = z; ! 121: ++cargs; ! 122: ! 123: while (*z != '\0' && ! isspace (BUCHAR (*z))) ! 124: z++; ! 125: ! 126: if (*z == '\0') ! 127: break; ! 128: ! 129: *z++ = '\0'; ! 130: } ! 131: ! 132: if (cargs <= 0) ! 133: return UUCONF_CMDTABRET_CONTINUE; ! 134: ! 135: iret = uuconf_cmd_args (pglobal, cargs, pzargs, qtab, pinfo, pfiunknown, ! 136: iflags, pblock); ! 137: ! 138: if (pzargs != azargs) ! 139: free ((pointer) pzargs); ! 140: ! 141: return iret; ! 142: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.