home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / arg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  3.9 KB  |  163 lines

  1. /* 
  2. ** ACE -- CLI/Shell command line argument functions: 
  3. ** Copyright (C) 1998 David Benn
  4. ** 
  5. ** This program is free software; you can redistribute it and/or
  6. ** modify it under the terms of the GNU General Public License
  7. ** as published by the Free Software Foundation; either version 2
  8. ** of the License, or (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. **
  19. **      parse_cli_args -- parses raw command line buffer, counts arguments
  20. **                       and creates a buffer of NULL terminated arguments.
  21. **
  22. **           arg             -- copies the Nth command line argument to the 
  23. **                    specified buffer. 
  24. **
  25. **      argcount       -- returns the number of command line arguments.
  26. **
  27. ** Author: David J Benn
  28. **   Date: 6th June 1993,
  29. **       24th August 1994,
  30. **       10th September 1994
  31. */
  32.  
  33. #include <exec/types.h>
  34. #include <exec/memory.h>
  35. #include <libraries/dos.h>
  36. #include <libraries/dosextens.h>
  37.  
  38. #define QUOTE '"'
  39.  
  40. /* CLI/shell Process address */
  41. extern struct Process *TaskAddress;
  42.  
  43. /* raw command line data */
  44. extern char *cmdbuf;
  45. extern long cmdlen;
  46.  
  47. /* final command line argument data */
  48. char   *argbuf=NULL;
  49. static long nargs=0L;
  50.  
  51. void parse_cli_args()
  52. {
  53. char *src,*dest;
  54. long nchars;
  55.  
  56.  /* 
  57.  ** This function takes a source buffer consisting of
  58.  ** the characters following a CLI command and converts
  59.  ** it into a series of discrete arguments. An array of
  60.  ** pointers is set to hold the start address of each 
  61.  ** of up to 32 such arguments. 
  62.  */ 
  63.  
  64.  /* 
  65.  ** Allocate memory for arg buffer; allocating
  66.  ** at least as much memory as there is in the
  67.  ** source buffer will guarantee enough space
  68.  ** is set aside for the arguments.
  69.  ** There is a space before the first character
  70.  ** which makes up for the lack of a terminating 
  71.  ** NULL in the string.
  72.  */
  73.  argbuf = (char *)AllocMem(cmdlen,MEMF_ANY);
  74.  if (argbuf == NULL) return;
  75.  
  76.  /* point to source and target buffers */
  77.  src = cmdbuf;
  78.  dest = argbuf;
  79.  
  80.  /* number of characters in buffer */
  81.  nchars = cmdlen;
  82.  
  83.  while (nchars > 0)
  84.  { 
  85.   /* skip whitespace */
  86.   while (*src <= ' ' && nchars > 0) { ++src; --nchars; }
  87.  
  88.   /* end of CLI buffer? */
  89.   if (nchars == 0) return;
  90.  
  91.   /* increment argument counter */
  92.   ++nargs;
  93.  
  94.   /* get quoted argument? */
  95.   if (*src == QUOTE)
  96.   {
  97.    ++src; --nchars; /* skip first quote */
  98.    while (*src != QUOTE && nchars > 0) { *dest++ = *src++; --nchars; }
  99.    *dest++ = '\0'; 
  100.    if (*src == QUOTE) { ++src; --nchars; } /* skip second quote */
  101.   }
  102.   else
  103.   {
  104.    /* get unquoted argument */  
  105.    while (*src > ' ' && nchars > 0)  { *dest++ = *src++; --nchars; }
  106.    *dest++ = '\0';
  107.   }
  108.  }
  109. }
  110.  
  111. char *arg(buf,n)
  112. char *buf;
  113. long n;
  114. {
  115. long   count,length,i;
  116. char   *src,*dest;
  117. struct CommandLineInterface *cli;
  118.  
  119. /* Return the Nth command line argument 
  120.    or the CLI/shell command name in the 
  121.    specified buffer. 
  122.  */
  123.  
  124.  if (n == 0)
  125.  {
  126.   /* return command name */
  127.   cli = (struct CommandLineInterface *) ((long)TaskAddress->pr_CLI << 2);
  128.   src = (char *) ((long)cli->cli_CommandName << 2);
  129.   length = (long)src[0];
  130.   src++;  /* skip past BCPL string character count */
  131.   dest = buf;
  132.   for (i=1;i<=length;i++) *dest++ = *src++;
  133.   *dest = '\0';
  134.  }
  135.  else
  136.  if (n > nargs || argbuf == NULL) 
  137.     /* not N arguments or arg buffer not allocated -> NULL string */ 
  138.     buf[0] = '\0';
  139.  else
  140.  {
  141.   /* get Nth argument */
  142.   count=1;
  143.   src=argbuf;
  144.   while (count != n) 
  145.   {
  146.    while (*src++ != '\0');
  147.    ++count;
  148.   } 
  149.  
  150.   /* copy argument into buffer */
  151.   dest=buf;
  152.   while (*dest++ = *src++); 
  153.  }
  154.  
  155.  return(buf);
  156. }
  157.  
  158. long argcount()
  159. {
  160.  /* Returns the number of command line arguments. */
  161.  return(nargs);
  162. }
  163.