home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uuconf.subproj / cmdlin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  3.5 KB  |  146 lines

  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucnfi.h"
  27.  
  28. #if USE_RCS_ID
  29. const char _uuconf_cmdlin_rcsid[] = "$Id: cmdlin.c,v 1.6 1995/06/21 19:21:55 ian Rel $";
  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.   if ((iflags & UUCONF_CMDTABFLAG_NOCOMMENTS) == 0)
  60.     {
  61.       /* Any # not preceeded by a backslash starts a comment.  */
  62.       z = zline;
  63.       while ((z = strchr (z, '#')) != NULL)
  64.     {
  65.       if (z == zline || *(z - 1) != '\\')
  66.         {
  67.           *z = '\0';
  68.           break;
  69.         }
  70.       /* Remove the backslash.  */
  71.       while ((*(z - 1) = *z) != '\0')
  72.         ++z;
  73.     }
  74.     }
  75.  
  76.   /* Parse the first CSTACK arguments by hand to avoid malloc.  */
  77.  
  78.   z = zline;
  79.   cargs = 0;
  80.   pzargs = azargs;
  81.   while (TRUE)
  82.     {
  83.       while (*z != '\0' && isspace (BUCHAR (*z)))
  84.     ++z;
  85.  
  86.       if (*z == '\0')
  87.     break;
  88.  
  89.       if (cargs >= CSTACK)
  90.     {
  91.       char **pzsplit;
  92.       size_t csplit;
  93.       int cmore;
  94.  
  95.       pzsplit = NULL;
  96.       csplit = 0;
  97.       cmore = _uuconf_istrsplit (z, '\0', &pzsplit, &csplit);
  98.       if (cmore < 0)
  99.         {
  100.           qglobal->ierrno = errno;
  101.           return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  102.         }
  103.  
  104.       pzargs = (char **) malloc ((cmore + CSTACK) * sizeof (char *));
  105.       if (pzargs == NULL)
  106.         {
  107.           qglobal->ierrno = errno;
  108.           free ((pointer) pzsplit);
  109.           return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  110.         }
  111.  
  112.       memcpy ((pointer) pzargs, (pointer) azargs,
  113.           CSTACK * sizeof (char *));
  114.       memcpy ((pointer) (pzargs + CSTACK), (pointer) pzsplit,
  115.           cmore * sizeof (char *));
  116.       cargs = cmore + CSTACK;
  117.  
  118.       free ((pointer) pzsplit);
  119.  
  120.       break;
  121.     }
  122.  
  123.       azargs[cargs] = z;
  124.       ++cargs;
  125.  
  126.       while (*z != '\0' && ! isspace (BUCHAR (*z)))
  127.     z++;
  128.  
  129.       if (*z == '\0')
  130.     break;
  131.  
  132.       *z++ = '\0';
  133.     }
  134.  
  135.   if (cargs <= 0)
  136.     return UUCONF_CMDTABRET_CONTINUE;
  137.  
  138.   iret = uuconf_cmd_args (pglobal, cargs, pzargs, qtab, pinfo, pfiunknown,
  139.               iflags, pblock);
  140.  
  141.   if (pzargs != azargs)
  142.     free ((pointer) pzargs);
  143.  
  144.   return iret;
  145. }
  146.