gsargs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gsargs.c,v 1.9 2004/08/04 23:33:29 stefan Exp $ */
  14. /* Command line argument list management */
  15. #include "ctype_.h"
  16. #include "stdio_.h"
  17. #include "string_.h"
  18. #include "gsexit.h"
  19. #include "gsmemory.h"
  20. #include "gsargs.h"
  21. #include "gserrors.h"
  22. /* Initialize an arg list. */
  23. void
  24. arg_init(arg_list * pal, const char **argv, int argc,
  25. FILE * (*arg_fopen) (const char *fname, void *fopen_data),
  26. void *fopen_data)
  27. {
  28. pal->expand_ats = true;
  29. pal->arg_fopen = arg_fopen;
  30. pal->fopen_data = fopen_data;
  31. pal->argp = argv + 1;
  32. pal->argn = argc - 1;
  33. pal->depth = 0;
  34. }
  35. /* Push a string onto an arg list. */
  36. int
  37. arg_push_memory_string(arg_list * pal, char *str, gs_memory_t * mem)
  38. {
  39. arg_source *pas;
  40. if (pal->depth == arg_depth_max) {
  41. lprintf("Too much nesting of @-files.\n");
  42. return 1;
  43. }
  44. pas = &pal->sources[pal->depth];
  45. pas->is_file = false;
  46. pas->u.s.chars = str;
  47. pas->u.s.memory = mem;
  48. pas->u.s.str = str;
  49. pal->depth++;
  50. return 0;
  51. }
  52. /* Clean up an arg list. */
  53. void
  54. arg_finit(arg_list * pal)
  55. {
  56. while (pal->depth) {
  57. arg_source *pas = &pal->sources[--(pal->depth)];
  58. if (pas->is_file)
  59. fclose(pas->u.file);
  60. else if (pas->u.s.memory)
  61. gs_free_object(pas->u.s.memory, pas->u.s.chars, "arg_finit");
  62. }
  63. }
  64. /* Get the next arg from a list. */
  65. /* Note that these are not copied to the heap. */
  66. const char *
  67. arg_next(arg_list * pal, int *code)
  68. {
  69. arg_source *pas;
  70. FILE *f;
  71. const char *astr = 0; /* initialized only to pacify gcc */
  72. char *cstr;
  73. const char *result;
  74. int endc;
  75. int c, i;
  76. bool in_quote, eol;
  77. top:pas = &pal->sources[pal->depth - 1];
  78. if (pal->depth == 0) {
  79. if (pal->argn <= 0) /* all done */
  80. return 0;
  81. pal->argn--;
  82. result = *(pal->argp++);
  83. goto at;
  84. }
  85. if (pas->is_file)
  86. f = pas->u.file, endc = EOF;
  87. else
  88. astr = pas->u.s.str, f = NULL, endc = 0;
  89. result = cstr = pal->cstr;
  90. #define cfsgetc() (f == NULL ? (*astr ? *astr++ : 0) : fgetc(f))
  91. #define is_eol(c) (c == '\r' || c == '\n')
  92. i = 0;
  93. in_quote = false;
  94. eol = true;
  95. c = cfsgetc();
  96. for (i = 0;;) {
  97. if (c == endc) {
  98. if (in_quote) {
  99. cstr[i] = 0;
  100. errprintf("Unterminated quote in @-file: %s\n", cstr);
  101. *code = gs_error_Fatal;
  102. return NULL;
  103. }
  104. if (i == 0) {
  105. /* EOF before any argument characters. */
  106. if (f != NULL)
  107. fclose(f);
  108. else if (pas->u.s.memory)
  109. gs_free_object(pas->u.s.memory, pas->u.s.chars,
  110. "arg_next");
  111. pal->depth--;
  112. goto top;
  113. }
  114. break;
  115. }
  116. /* c != endc */
  117. if (isspace(c)) {
  118. if (i == 0) {
  119. c = cfsgetc();
  120. continue;
  121. }
  122. if (!in_quote)
  123. break;
  124. }
  125. /* c isn't leading or terminating whitespace. */
  126. if (c == '#' && eol) {
  127. /* Skip a comment. */
  128. do {
  129. c = cfsgetc();
  130. } while (!(c == endc || is_eol(c)));
  131. if (c == '\r')
  132. c = cfsgetc();
  133. if (c == '\n')
  134. c = cfsgetc();
  135. continue;
  136. }
  137. if (c == '\\') {
  138. /* Check for \ followed by newline. */
  139. c = cfsgetc();
  140. if (is_eol(c)) {
  141. if (c == '\r')
  142. c = cfsgetc();
  143. if (c == '\n')
  144. c = cfsgetc();
  145. eol = true;
  146. continue;
  147. }
  148. /* \ anywhere else is treated as a printing character. */
  149. /* This is different from the Unix shells. */
  150. if (i == arg_str_max - 1) {
  151. cstr[i] = 0;
  152. errprintf("Command too long: %s\n", cstr);
  153. *code = gs_error_Fatal;
  154. return NULL;
  155. }
  156. cstr[i++] = '\\';
  157. eol = false;
  158. continue;
  159. }
  160. /* c will become part of the argument */
  161. if (i == arg_str_max - 1) {
  162. cstr[i] = 0;
  163. errprintf("Command too long: %s\n", cstr);
  164. *code = gs_error_Fatal;
  165. return NULL;
  166. }
  167. /* If input is coming from an @-file, allow quotes */
  168. /* to protect whitespace. */
  169. if (c == '"' && f != NULL)
  170. in_quote = !in_quote;
  171. else
  172. cstr[i++] = c;
  173. eol = is_eol(c);
  174. c = cfsgetc();
  175. }
  176. cstr[i] = 0;
  177. if (f == NULL)
  178. pas->u.s.str = astr;
  179. at:if (pal->expand_ats && result[0] == '@') {
  180. if (pal->depth == arg_depth_max) {
  181. lprintf("Too much nesting of @-files.\n");
  182. *code = gs_error_Fatal;
  183. return NULL;
  184. }
  185. result++; /* skip @ */
  186. f = (*pal->arg_fopen) (result, pal->fopen_data);
  187. if (f == NULL) {
  188. errprintf("Unable to open command line file %s\n", result);
  189. *code = gs_error_Fatal;
  190. return NULL;
  191. }
  192. pal->depth++;
  193. pas++;
  194. pas->is_file = true;
  195. pas->u.file = f;
  196. goto top;
  197. }
  198. return result;
  199. }
  200. /* Copy an argument string to the heap. */
  201. char *
  202. arg_copy(const char *str, gs_memory_t * mem)
  203. {
  204. char *sstr = (char *)gs_alloc_bytes(mem, strlen(str) + 1, "arg_copy");
  205. if (sstr == 0) {
  206. lprintf("Out of memory!\n");
  207. return NULL;
  208. }
  209. strcpy(sstr, str);
  210. return sstr;
  211. }