4
0

prepargs.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Parse arguments from a string and prepend them to an argv.
  2. Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. 02111-1307, USA. */
  15. /* Written by Paul Eggert <[email protected]>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include "prepargs.h"
  20. #include <sys/types.h>
  21. #include <xalloc.h>
  22. #if HAVE_STRING_H
  23. # include <string.h>
  24. #endif
  25. #include <ctype.h>
  26. /* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
  27. as an argument to <ctype.h> macros like "isspace". */
  28. #ifdef STDC_HEADERS
  29. # define IN_CTYPE_DOMAIN(c) 1
  30. #else
  31. # define IN_CTYPE_DOMAIN(c) ((c) <= 0177)
  32. #endif
  33. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  34. /* Find the white-space-separated options specified by OPTIONS, and
  35. using BUF to store copies of these options, set ARGV[0], ARGV[1],
  36. etc. to the option copies. Return the number N of options found.
  37. Do not set ARGV[N]. If ARGV is null, do not store ARGV[0]
  38. etc. Backslash can be used to escape whitespace (and backslashes). */
  39. static int
  40. prepend_args (char const *options, char *buf, char **argv)
  41. {
  42. char const *o = options;
  43. char *b = buf;
  44. int n = 0;
  45. for (;;)
  46. {
  47. while (ISSPACE ((unsigned char) *o))
  48. o++;
  49. if (!*o)
  50. return n;
  51. if (argv)
  52. argv[n] = b;
  53. n++;
  54. do
  55. if ((*b++ = *o++) == '\\' && *o)
  56. b[-1] = *o++;
  57. while (*o && ! ISSPACE ((unsigned char) *o));
  58. *b++ = '\0';
  59. }
  60. }
  61. /* Prepend the whitespace-separated options in OPTIONS to the argument
  62. vector of a main program with argument count *PARGC and argument
  63. vector *PARGV. */
  64. void
  65. prepend_default_options (char const *options, int *pargc, char ***pargv)
  66. {
  67. if (options)
  68. {
  69. char *buf = xmalloc (strlen (options) + 1);
  70. int prepended = prepend_args (options, buf, (char **) 0);
  71. int argc = *pargc;
  72. char * const *argv = *pargv;
  73. char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
  74. *pargc = prepended + argc;
  75. *pargv = pp;
  76. *pp++ = *argv++;
  77. pp += prepend_args (options, buf, pp);
  78. while ((*pp++ = *argv++))
  79. continue;
  80. }
  81. }