parser.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*!
  2. * \file gis/parser.c
  3. *
  4. * \brief GIS Library - Argument parsing functions.
  5. *
  6. * Parses the command line provided through argc and argv. Example:
  7. * Assume the previous calls:
  8. *
  9. \code
  10. opt1 = G_define_option() ;
  11. opt1->key = "map",
  12. opt1->type = TYPE_STRING,
  13. opt1->required = YES,
  14. opt1->checker = sub,
  15. opt1->description= "Name of an existing raster map" ;
  16. opt2 = G_define_option() ;
  17. opt2->key = "color",
  18. opt2->type = TYPE_STRING,
  19. opt2->required = NO,
  20. opt2->answer = "white",
  21. opt2->options = "red,orange,blue,white,black",
  22. opt2->description= "Color used to display the map" ;
  23. opt3 = G_define_option() ;
  24. opt3->key = "number",
  25. opt3->type = TYPE_DOUBLE,
  26. opt3->required = NO,
  27. opt3->answer = "12345.67",
  28. opt3->options = "0-99999",
  29. opt3->description= "Number to test parser" ;
  30. \endcode
  31. *
  32. * G_parser() will respond to the following command lines as described:
  33. *
  34. \verbatim
  35. command (No command line arguments)
  36. \endverbatim
  37. * Parser enters interactive mode.
  38. *
  39. \verbatim
  40. command map=map.name
  41. \endverbatim
  42. * Parser will accept this line. Map will be set to "map.name", the
  43. * 'a' and 'b' flags will remain off and the num option will be set
  44. * to the default of 5.
  45. *
  46. \verbatim
  47. command -ab map=map.name num=9
  48. command -a -b map=map.name num=9
  49. command -ab map.name num=9
  50. command map.name num=9 -ab
  51. command num=9 -a map=map.name -b
  52. \endverbatim
  53. * These are all treated as acceptable and identical. Both flags are
  54. * set to on, the map option is "map.name" and the num option is "9".
  55. * Note that the "map=" may be omitted from the command line if it
  56. * is part of the first option (flags do not count).
  57. *
  58. \verbatim
  59. command num=12
  60. \endverbatim
  61. * This command line is in error in two ways. The user will be told
  62. * that the "map" option is required and also that the number 12 is
  63. * out of range. The acceptable range (or list) will be printed.
  64. *
  65. * (C) 2001-2009, 2011 by the GRASS Development Team
  66. *
  67. * This program is free software under the GNU General Public License
  68. * (>=v2). Read the file COPYING that comes with GRASS for details.
  69. *
  70. * \author Original author CERL
  71. * \author Soeren Gebbert added Dec. 2009 WPS process_description document
  72. */
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <unistd.h>
  77. #include <grass/gis.h>
  78. #include <grass/spawn.h>
  79. #include <grass/glocale.h>
  80. #include "parser_local_proto.h"
  81. enum opt_error {
  82. BAD_SYNTAX = 1,
  83. OUT_OF_RANGE = 2,
  84. MISSING_VALUE = 3,
  85. AMBIGUOUS = 4,
  86. REPLACED = 5
  87. };
  88. #define KEYLENGTH 64
  89. /* initialize the global struct */
  90. struct state state;
  91. struct state *st = &state;
  92. /* local prototypes */
  93. static void set_flag(int);
  94. static int contains(const char *, int);
  95. static int is_option(const char *);
  96. static void set_option(const char *);
  97. static void check_opts(void);
  98. static void check_an_opt(const char *, int, const char *, const char **, char **);
  99. static int check_int(const char *, const char **);
  100. static int check_double(const char *, const char **);
  101. static int check_string(const char *, const char **, int *);
  102. static void check_required(void);
  103. static void split_opts(void);
  104. static void check_multiple_opts(void);
  105. static int check_overwrite(void);
  106. static void define_keywords(void);
  107. static void split_gisprompt(const char *gisprompt, char *age, char *element, char *desc);
  108. static void module_gui_wx(void);
  109. static void append_error(const char *);
  110. /*!
  111. * \brief Disables the ability of the parser to operate interactively.
  112. *
  113. * When a user calls a command with no arguments on the command line,
  114. * the parser will enter its own standardized interactive session in
  115. * which all flags and options are presented to the user for input. A
  116. * call to G_disable_interactive() disables the parser's interactive
  117. * prompting.
  118. *
  119. */
  120. void G_disable_interactive(void)
  121. {
  122. st->no_interactive = 1;
  123. }
  124. /*!
  125. * \brief Initializes a Flag struct.
  126. *
  127. * Allocates memory for the Flag structure and returns a pointer to
  128. * this memory.
  129. *
  130. * Flags are always represented by single letters. A user "turns them
  131. * on" at the command line using a minus sign followed by the
  132. * character representing the flag.
  133. *
  134. * \return Pointer to a Flag struct
  135. */
  136. struct Flag *G_define_flag(void)
  137. {
  138. struct Flag *flag;
  139. struct Item *item;
  140. /* Allocate memory if not the first flag */
  141. if (st->n_flags) {
  142. flag = G_malloc(sizeof(struct Flag));
  143. st->current_flag->next_flag = flag;
  144. }
  145. else
  146. flag = &st->first_flag;
  147. /* Zero structure */
  148. G_zero(flag, sizeof(struct Flag));
  149. st->current_flag = flag;
  150. st->n_flags++;
  151. if (st->n_items) {
  152. item = G_malloc(sizeof(struct Item));
  153. st->current_item->next_item = item;
  154. }
  155. else
  156. item = &st->first_item;
  157. G_zero(item, sizeof(struct Item));
  158. item->flag = flag;
  159. item->option = NULL;
  160. st->current_item = item;
  161. st->n_items++;
  162. return (flag);
  163. }
  164. /*!
  165. * \brief Initializes an Option struct.
  166. *
  167. * Allocates memory for the Option structure and returns a pointer to
  168. * this memory.
  169. *
  170. * Options are provided by user on command line using the standard
  171. * format: <i>key=value</i>. Options identified as REQUIRED must be
  172. * specified by user on command line. The option string can either
  173. * specify a range of values (e.g. "10-100") or a list of acceptable
  174. * values (e.g. "red,orange,yellow"). Unless the option string is
  175. * NULL, user provided input will be evaluated agaist this string.
  176. *
  177. * \return pointer to an Option struct
  178. */
  179. struct Option *G_define_option(void)
  180. {
  181. struct Option *opt;
  182. struct Item *item;
  183. /* Allocate memory if not the first option */
  184. if (st->n_opts) {
  185. opt = G_malloc(sizeof(struct Option));
  186. st->current_option->next_opt = opt;
  187. }
  188. else
  189. opt = &st->first_option;
  190. /* Zero structure */
  191. G_zero(opt, sizeof(struct Option));
  192. opt->required = NO;
  193. opt->multiple = NO;
  194. st->current_option = opt;
  195. st->n_opts++;
  196. if (st->n_items) {
  197. item = G_malloc(sizeof(struct Item));
  198. st->current_item->next_item = item;
  199. }
  200. else
  201. item = &st->first_item;
  202. G_zero(item, sizeof(struct Item));
  203. item->option = opt;
  204. st->current_item = item;
  205. st->n_items++;
  206. return (opt);
  207. }
  208. /*!
  209. * \brief Initializes a new module.
  210. *
  211. * \return pointer to a GModule struct
  212. */
  213. struct GModule *G_define_module(void)
  214. {
  215. struct GModule *module;
  216. /* Allocate memory */
  217. module = &st->module_info;
  218. /* Zero structure */
  219. G_zero(module, sizeof(struct GModule));
  220. /* Allocate keywords array */
  221. define_keywords();
  222. return (module);
  223. }
  224. /*!
  225. * \brief Parse command line.
  226. *
  227. * The command line parameters <i>argv</i> and the number of
  228. * parameters <i>argc</i> from the main() routine are passed directly
  229. * to G_parser(). G_parser() accepts the command line input entered by
  230. * the user, and parses this input according to the input options
  231. * and/or flags that were defined by the programmer.
  232. *
  233. * <b>Note:</b> The only functions which can legitimately be called
  234. * before G_parser() are:
  235. *
  236. * - G_gisinit()
  237. * - G_no_gisinit()
  238. * - G_define_module()
  239. * - G_define_flag()
  240. * - G_define_option()
  241. * - G_define_standard_flag()
  242. * - G_define_standard_option()
  243. * - G_disable_interactive()
  244. * - G_option_exclusive()
  245. * - G_option_required()
  246. * - G_option_requires()
  247. * - G_option_requires_all()
  248. * - G_option_excludes()
  249. * - G_option_collective()
  250. *
  251. * The usual order a module calls functions is:
  252. *
  253. * # G_gisinit()
  254. * # G_define_module()
  255. * # G_define_standard_flag()
  256. * # G_define_standard_option()
  257. * # G_define_flag()
  258. * # G_define_option()
  259. * # G_option_exclusive()
  260. * # G_option_required()
  261. * # G_option_requires()
  262. * # G_option_requires_all()
  263. * # G_option_excludes()
  264. * # G_option_collective()
  265. * # G_parser()
  266. *
  267. * \param argc number of arguments
  268. * \param argv argument list
  269. *
  270. * \return 0 on success
  271. * \return -1 on error and calls G_usage()
  272. */
  273. int G_parser(int argc, char **argv)
  274. {
  275. int need_first_opt;
  276. int opt_checked = 0;
  277. char *ptr, *tmp_name, *err;
  278. int i;
  279. struct Option *opt;
  280. char force_gui = FALSE;
  281. err = NULL;
  282. need_first_opt = 1;
  283. tmp_name = G_store(argv[0]);
  284. st->pgm_path = tmp_name;
  285. st->n_errors = 0;
  286. st->error = NULL;
  287. i = strlen(tmp_name);
  288. while (--i >= 0) {
  289. if (G_is_dirsep(tmp_name[i])) {
  290. tmp_name += i + 1;
  291. break;
  292. }
  293. }
  294. G_basename(tmp_name, "exe");
  295. st->pgm_name = tmp_name;
  296. /* Stash default answers */
  297. opt = &st->first_option;
  298. while (opt) {
  299. if (opt->required)
  300. st->has_required = 1;
  301. /* Parse options */
  302. if (opt->options) {
  303. int cnt = 0;
  304. char **tokens, delm[2];
  305. delm[0] = ',';
  306. delm[1] = '\0';
  307. tokens = G_tokenize(opt->options, delm);
  308. i = 0;
  309. while (tokens[i]) {
  310. cnt++;
  311. i++;
  312. }
  313. opt->opts = G_calloc(cnt + 1, sizeof(const char *));
  314. i = 0;
  315. while (tokens[i]) {
  316. opt->opts[i] = G_store(tokens[i]);
  317. i++;
  318. }
  319. G_free_tokens(tokens);
  320. if (opt->descriptions) {
  321. delm[0] = ';';
  322. opt->descs = G_calloc(cnt + 1, sizeof(const char *));
  323. tokens = G_tokenize(opt->descriptions, delm);
  324. i = 0;
  325. while (tokens[i]) {
  326. int j, found;
  327. if (!tokens[i + 1])
  328. break;
  329. j = 0;
  330. found = 0;
  331. while (opt->opts[j]) {
  332. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  333. found = 1;
  334. break;
  335. }
  336. j++;
  337. }
  338. if (!found) {
  339. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  340. tokens[i], opt->key);
  341. }
  342. else {
  343. opt->descs[j] = G_store(tokens[i + 1]);
  344. }
  345. i += 2;
  346. }
  347. G_free_tokens(tokens);
  348. }
  349. }
  350. /* Copy answer */
  351. if (opt->multiple && opt->answers && opt->answers[0]) {
  352. opt->answer = G_malloc(strlen(opt->answers[0]) + 1);
  353. strcpy(opt->answer, opt->answers[0]);
  354. for (i = 1; opt->answers[i]; i++) {
  355. opt->answer = G_realloc(opt->answer,
  356. strlen(opt->answer) +
  357. strlen(opt->answers[i]) + 2);
  358. strcat(opt->answer, ",");
  359. strcat(opt->answer, opt->answers[i]);
  360. }
  361. }
  362. opt->def = opt->answer;
  363. opt = opt->next_opt;
  364. }
  365. /* If there are NO arguments, go interactive */
  366. if (argc < 2 && (st->has_required || G__has_required_rule())
  367. && !st->no_interactive && isatty(0)) {
  368. module_gui_wx();
  369. return -1;
  370. }
  371. else if (argc < 2 && st->has_required && isatty(0)) {
  372. G_usage();
  373. return -1;
  374. }
  375. else if (argc >= 2) {
  376. /* If first arg is "help" give a usage/syntax message */
  377. if (strcmp(argv[1], "help") == 0 ||
  378. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  379. G_usage();
  380. exit(EXIT_SUCCESS);
  381. }
  382. /* If first arg is "--help-text" give a usage/syntax message
  383. * with machine-readable sentinels */
  384. if (strcmp(argv[1], "--help-text") == 0) {
  385. G__usage_text();
  386. exit(EXIT_SUCCESS);
  387. }
  388. /* If first arg is "--interface-description" then print out
  389. * a xml description of the task */
  390. if (strcmp(argv[1], "--interface-description") == 0) {
  391. G__usage_xml();
  392. exit(EXIT_SUCCESS);
  393. }
  394. /* If first arg is "--html-description" then print out
  395. * a html description of the task */
  396. if (strcmp(argv[1], "--html-description") == 0) {
  397. G__usage_html();
  398. exit(EXIT_SUCCESS);
  399. }
  400. /* If first arg is "--rest-description" then print out
  401. * a ReST description of the task */
  402. if (strcmp(argv[1], "--rest-description") == 0) {
  403. G__usage_rest();
  404. exit(EXIT_SUCCESS);
  405. }
  406. /* If first arg is "--wps-process-description" then print out
  407. * the wps process description of the task */
  408. if (strcmp(argv[1], "--wps-process-description") == 0) {
  409. G__wps_print_process_description();
  410. exit(EXIT_SUCCESS);
  411. }
  412. /* If first arg is "--script" then then generate
  413. * g.parser boilerplate */
  414. if (strcmp(argv[1], "--script") == 0) {
  415. G__script();
  416. exit(EXIT_SUCCESS);
  417. }
  418. /* Loop thru all command line arguments */
  419. while (--argc) {
  420. ptr = *(++argv);
  421. if (strcmp(ptr, "help") == 0 || strcmp(ptr, "--h") == 0 ||
  422. strcmp(ptr, "-help") == 0 || strcmp(ptr, "--help") == 0) {
  423. G_usage();
  424. exit(EXIT_SUCCESS);
  425. }
  426. /* Overwrite option */
  427. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  428. st->overwrite = 1;
  429. }
  430. /* Verbose option */
  431. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  432. char buff[32];
  433. /* print everything: max verbosity level */
  434. st->module_info.verbose = G_verbose_max();
  435. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  436. putenv(G_store(buff));
  437. if (st->quiet == 1) {
  438. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  439. }
  440. st->quiet = -1;
  441. }
  442. /* Quiet option */
  443. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  444. char buff[32];
  445. /* print nothing, but errors and warnings */
  446. st->module_info.verbose = G_verbose_min();
  447. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  448. putenv(G_store(buff));
  449. if (st->quiet == -1) {
  450. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  451. }
  452. st->quiet = 1; /* for passing to gui init */
  453. }
  454. /* Force gui to come up */
  455. else if (strcmp(ptr, "--ui") == 0) {
  456. force_gui = TRUE;
  457. }
  458. /* If we see a flag */
  459. else if (*ptr == '-') {
  460. while (*(++ptr))
  461. set_flag(*ptr);
  462. }
  463. /* If we see standard option format (option=val) */
  464. else if (is_option(ptr)) {
  465. set_option(ptr);
  466. need_first_opt = 0;
  467. }
  468. /* If we see the first option with no equal sign */
  469. else if (need_first_opt && st->n_opts) {
  470. st->first_option.answer = G_store(ptr);
  471. st->first_option.count++;
  472. need_first_opt = 0;
  473. }
  474. /* If we see the non valid argument (no "=", just argument) */
  475. else {
  476. G_asprintf(&err, _("Sorry <%s> is not a valid option"), ptr);
  477. append_error(err);
  478. }
  479. }
  480. }
  481. /* Split options where multiple answers are OK */
  482. split_opts();
  483. /* Run the gui if it was specifically requested */
  484. if (force_gui) {
  485. module_gui_wx();
  486. return -1;
  487. }
  488. /* Check multiple options */
  489. check_multiple_opts();
  490. /* Check answers against options and check subroutines */
  491. if (!opt_checked)
  492. check_opts();
  493. /* Make sure all required options are set */
  494. if (!st->suppress_required)
  495. check_required();
  496. G__check_option_rules();
  497. if (st->n_errors > 0) {
  498. if (G_verbose() > -1) {
  499. if (G_verbose() > G_verbose_min())
  500. G_usage();
  501. fprintf(stderr, "\n");
  502. for (i = 0; i < st->n_errors; i++) {
  503. fprintf(stderr, "%s: %s\n", _("ERROR"), st->error[i]);
  504. }
  505. }
  506. return -1;
  507. }
  508. if (check_overwrite())
  509. return -1;
  510. return 0;
  511. }
  512. /*!
  513. * \brief Creates command to run non-interactive.
  514. *
  515. * Creates a command-line that runs the current command completely
  516. * non-interactive.
  517. *
  518. * \return pointer to a char string
  519. */
  520. char *G_recreate_command(void)
  521. {
  522. char *buff;
  523. char flg[4];
  524. char *cur;
  525. const char *tmp;
  526. struct Flag *flag;
  527. struct Option *opt;
  528. int n, len, slen;
  529. int nalloced = 0;
  530. G_debug(3, "G_recreate_command()");
  531. /* Flag is not valid if there are no flags to set */
  532. buff = G_calloc(1024, sizeof(char));
  533. nalloced += 1024;
  534. tmp = G_program_name();
  535. len = strlen(tmp);
  536. if (len >= nalloced) {
  537. nalloced += (1024 > len) ? 1024 : len + 1;
  538. buff = G_realloc(buff, nalloced);
  539. }
  540. cur = buff;
  541. strcpy(cur, tmp);
  542. cur += len;
  543. if (st->n_flags) {
  544. flag = &st->first_flag;
  545. while (flag) {
  546. if (flag->answer == 1) {
  547. flg[0] = ' ';
  548. flg[1] = '-';
  549. flg[2] = flag->key;
  550. flg[3] = '\0';
  551. slen = strlen(flg);
  552. if (len + slen >= nalloced) {
  553. nalloced +=
  554. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  555. buff = G_realloc(buff, nalloced);
  556. cur = buff + len;
  557. }
  558. strcpy(cur, flg);
  559. cur += slen;
  560. len += slen;
  561. }
  562. flag = flag->next_flag;
  563. }
  564. }
  565. opt = &st->first_option;
  566. while (opt) {
  567. if (opt->answer && opt->answers && opt->answers[0]) {
  568. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  569. if (len + slen >= nalloced) {
  570. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  571. buff = G_realloc(buff, nalloced);
  572. cur = buff + len;
  573. }
  574. strcpy(cur, " ");
  575. cur++;
  576. strcpy(cur, opt->key);
  577. cur = strchr(cur, '\0');
  578. strcpy(cur, "=");
  579. cur++;
  580. if (opt->type == TYPE_STRING) {
  581. strcpy(cur, "\"");
  582. cur++;
  583. }
  584. strcpy(cur, opt->answers[0]);
  585. cur = strchr(cur, '\0');
  586. len = cur - buff;
  587. for (n = 1; opt->answers[n]; n++) {
  588. if (!opt->answers[n])
  589. break;
  590. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  591. if (len + slen >= nalloced) {
  592. nalloced +=
  593. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  594. buff = G_realloc(buff, nalloced);
  595. cur = buff + len;
  596. }
  597. strcpy(cur, ",");
  598. cur++;
  599. strcpy(cur, opt->answers[n]);
  600. cur = strchr(cur, '\0');
  601. len = cur - buff;
  602. }
  603. if (opt->type == TYPE_STRING) {
  604. strcpy(cur, "\"");
  605. cur++;
  606. len = cur - buff;
  607. }
  608. }
  609. opt = opt->next_opt;
  610. }
  611. return buff;
  612. }
  613. /*!
  614. \brief Add keyword to the list
  615. \param keyword keyword string
  616. */
  617. void G_add_keyword(const char *keyword)
  618. {
  619. if (st->n_keys >= st->n_keys_alloc) {
  620. st->n_keys_alloc += 10;
  621. st->module_info.keywords = G_realloc(st->module_info.keywords,
  622. st->n_keys_alloc * sizeof(char *));
  623. }
  624. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  625. }
  626. /*!
  627. \brief Set keywords from the string
  628. \param keywords keywords separated by commas
  629. */
  630. void G_set_keywords(const char *keywords)
  631. {
  632. char **tokens = G_tokenize(keywords, ",");
  633. st->module_info.keywords = (const char **)tokens;
  634. st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
  635. }
  636. int G__uses_new_gisprompt(void)
  637. {
  638. struct Option *opt;
  639. char age[KEYLENGTH];
  640. char element[KEYLENGTH];
  641. char desc[KEYLENGTH];
  642. if (st->module_info.overwrite)
  643. return 1;
  644. /* figure out if any of the options use a "new" gisprompt */
  645. /* This is to see if we should spit out the --o flag */
  646. if (st->n_opts) {
  647. opt = &st->first_option;
  648. while (opt) {
  649. if (opt->gisprompt) {
  650. split_gisprompt(opt->gisprompt, age, element, desc);
  651. if (strcmp(age, "new") == 0)
  652. return 1;
  653. }
  654. opt = opt->next_opt;
  655. }
  656. }
  657. return 0;
  658. }
  659. /*!
  660. \brief Print list of keywords (internal use only)
  661. If <em>format</em> function is NULL than list of keywords is printed
  662. comma-separated.
  663. \param[out] fd file where to print
  664. \param format pointer to print function
  665. */
  666. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  667. {
  668. int i;
  669. for(i = 0; i < st->n_keys; i++) {
  670. if (!format) {
  671. fprintf(fd, "%s", st->module_info.keywords[i]);
  672. }
  673. else {
  674. format(fd, st->module_info.keywords[i]);
  675. }
  676. if (i < st->n_keys - 1)
  677. fprintf(fd, ", ");
  678. }
  679. fflush(fd);
  680. }
  681. /*!
  682. \brief Get overwrite value
  683. \return 1 overwrite enabled
  684. \return 0 overwrite disabled
  685. */
  686. int G_get_overwrite()
  687. {
  688. return st->overwrite;
  689. }
  690. void define_keywords(void)
  691. {
  692. st->n_keys = 0;
  693. st->n_keys_alloc = 0;
  694. }
  695. /**************************************************************************
  696. *
  697. * The remaining routines are all local (static) routines used to support
  698. * the parsing process.
  699. *
  700. **************************************************************************/
  701. /*!
  702. \brief Invoke GUI dialog
  703. */
  704. static void module_gui_wx(void)
  705. {
  706. char script[GPATH_MAX];
  707. if (!st->pgm_path)
  708. st->pgm_path = G_program_name();
  709. if (!st->pgm_path)
  710. G_fatal_error(_("Unable to determine program name"));
  711. sprintf(script, "%s/gui/wxpython/gui_core/forms.py",
  712. getenv("GISBASE"));
  713. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, G_recreate_command(), NULL);
  714. }
  715. static void set_flag(int f)
  716. {
  717. struct Flag *flag;
  718. char *err;
  719. err = NULL;
  720. /* Flag is not valid if there are no flags to set */
  721. if (!st->n_flags) {
  722. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  723. append_error(err);
  724. return;
  725. }
  726. /* Find flag with corrrect keyword */
  727. flag = &st->first_flag;
  728. while (flag) {
  729. if (flag->key == f) {
  730. flag->answer = 1;
  731. if (flag->suppress_required)
  732. st->suppress_required = 1;
  733. return;
  734. }
  735. flag = flag->next_flag;
  736. }
  737. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  738. append_error(err);
  739. }
  740. /* contents() is used to find things strings with characters like commas and
  741. * dashes.
  742. */
  743. static int contains(const char *s, int c)
  744. {
  745. while (*s) {
  746. if (*s == c)
  747. return TRUE;
  748. s++;
  749. }
  750. return FALSE;
  751. }
  752. static int is_option(const char *string)
  753. {
  754. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  755. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  756. }
  757. static int match_option_1(const char *string, const char *option)
  758. {
  759. const char *next;
  760. if (*string == '\0')
  761. return 1;
  762. if (*option == '\0')
  763. return 0;
  764. if (*string == *option && match_option_1(string + 1, option + 1))
  765. return 1;
  766. if (*option == '_' && match_option_1(string, option + 1))
  767. return 1;
  768. next = strchr(option, '_');
  769. if (!next)
  770. return 0;
  771. if (*string == '_')
  772. return match_option_1(string + 1, next + 1);
  773. return match_option_1(string, next + 1);
  774. }
  775. static int match_option(const char *string, const char *option)
  776. {
  777. return (*string == *option)
  778. && match_option_1(string + 1, option + 1);
  779. }
  780. static void set_option(const char *string)
  781. {
  782. struct Option *at_opt = NULL;
  783. struct Option *opt = NULL;
  784. int found;
  785. int prefix;
  786. size_t key_len;
  787. char the_key[KEYLENGTH];
  788. char *ptr, *err;
  789. err = NULL;
  790. for (ptr = the_key; *string != '='; ptr++, string++)
  791. *ptr = *string;
  792. *ptr = '\0';
  793. string++;
  794. /* Find option with best keyword match */
  795. found = 0;
  796. prefix = 0;
  797. key_len = strlen(the_key);
  798. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  799. if (!at_opt->key)
  800. continue;
  801. if (strcmp(the_key, at_opt->key) == 0) {
  802. opt = at_opt;
  803. found = 1;
  804. break;
  805. }
  806. if (strncmp(the_key, at_opt->key, key_len) == 0) {
  807. opt = at_opt;
  808. found++;
  809. prefix++;
  810. }
  811. else if (match_option(the_key, at_opt->key)) {
  812. if (!found)
  813. opt = at_opt;
  814. found++;
  815. }
  816. }
  817. if (found > 1 && prefix > 1) {
  818. G_asprintf(&err, _("Sorry, <%s=> is ambiguous"), the_key);
  819. append_error(err);
  820. return;
  821. }
  822. /* If there is no match, complain */
  823. if (found == 0) {
  824. G_asprintf(&err, _("Sorry, <%s> is not a valid parameter"), the_key);
  825. append_error(err);
  826. return;
  827. }
  828. /* Allocate memory where answer is stored */
  829. if (opt->count++) {
  830. if (!opt->multiple) {
  831. G_asprintf(&err, _("Option <%s> does not accept multiple answers"), opt->key);
  832. append_error(err);
  833. }
  834. opt->answer = G_realloc(opt->answer,
  835. strlen(opt->answer) + strlen(string) + 2);
  836. strcat(opt->answer, ",");
  837. strcat(opt->answer, string);
  838. }
  839. else
  840. opt->answer = G_store(string);
  841. }
  842. static void check_opts(void)
  843. {
  844. struct Option *opt;
  845. int ans;
  846. if (!st->n_opts)
  847. return;
  848. opt = &st->first_option;
  849. while (opt) {
  850. /* Check answer against options if any */
  851. if (opt->answer) {
  852. if (opt->multiple == 0)
  853. check_an_opt(opt->key, opt->type,
  854. opt->options, opt->opts, &opt->answer);
  855. else {
  856. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  857. check_an_opt(opt->key, opt->type,
  858. opt->options, opt->opts, &opt->answers[ans]);
  859. }
  860. }
  861. /* Check answer against user's check subroutine if any */
  862. if (opt->checker)
  863. opt->checker(opt->answer);
  864. opt = opt->next_opt;
  865. }
  866. }
  867. static void check_an_opt(const char *key, int type, const char *options,
  868. const char **opts, char **answerp)
  869. {
  870. const char *answer = *answerp;
  871. int error;
  872. char *err;
  873. int found;
  874. error = 0;
  875. err = NULL;
  876. switch (type) {
  877. case TYPE_INTEGER:
  878. error = check_int(answer, opts);
  879. break;
  880. case TYPE_DOUBLE:
  881. error = check_double(answer, opts);
  882. break;
  883. case TYPE_STRING:
  884. error = check_string(answer, opts, &found);
  885. break;
  886. }
  887. switch (error) {
  888. case 0:
  889. break;
  890. case BAD_SYNTAX:
  891. G_asprintf(&err,
  892. _("Illegal range syntax for parameter <%s>\n"
  893. "\tPresented as: %s"), key, options);
  894. append_error(err);
  895. break;
  896. case OUT_OF_RANGE:
  897. G_asprintf(&err,
  898. _("Value <%s> out of range for parameter <%s>\n"
  899. "\tLegal range: %s"), answer, key, options);
  900. append_error(err);
  901. break;
  902. case MISSING_VALUE:
  903. G_asprintf(&err,
  904. _("Missing value for parameter <%s>"),
  905. key);
  906. append_error(err);
  907. break;
  908. case AMBIGUOUS:
  909. G_asprintf(&err,
  910. _("Value <%s> ambiguous for parameter <%s>\n"
  911. "\tValid options: %s"), answer, key, options);
  912. append_error(err);
  913. break;
  914. case REPLACED:
  915. *answerp = G_store(opts[found]);
  916. error = 0;
  917. break;
  918. }
  919. }
  920. static int check_int(const char *ans, const char **opts)
  921. {
  922. int d, i;
  923. /* "-" is reserved for standard input */
  924. if (strcmp(ans, "-") == 0)
  925. return 0;
  926. if (sscanf(ans, "%d", &d) != 1)
  927. return MISSING_VALUE;
  928. if (!opts)
  929. return 0;
  930. for (i = 0; opts[i]; i++) {
  931. const char *opt = opts[i];
  932. int lo, hi;
  933. if (contains(opt, '-')) {
  934. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  935. if (d >= lo && d <= hi)
  936. return 0;
  937. }
  938. else if (sscanf(opt, "-%d", &hi) == 1) {
  939. if (d <= hi)
  940. return 0;
  941. }
  942. else if (sscanf(opt, "%d-", &lo) == 1) {
  943. if (d >= lo)
  944. return 0;
  945. }
  946. else
  947. return BAD_SYNTAX;
  948. }
  949. else {
  950. if (sscanf(opt, "%d", &lo) == 1) {
  951. if (d == lo)
  952. return 0;
  953. }
  954. else
  955. return BAD_SYNTAX;
  956. }
  957. }
  958. return OUT_OF_RANGE;
  959. }
  960. static int check_double(const char *ans, const char **opts)
  961. {
  962. double d;
  963. int i;
  964. /* "-" is reserved for standard input */
  965. if (strcmp(ans, "-") == 0)
  966. return 0;
  967. if (sscanf(ans, "%lf", &d) != 1)
  968. return MISSING_VALUE;
  969. if (!opts)
  970. return 0;
  971. for (i = 0; opts[i]; i++) {
  972. const char *opt = opts[i];
  973. double lo, hi;
  974. if (contains(opt, '-')) {
  975. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  976. if (d >= lo && d <= hi)
  977. return 0;
  978. }
  979. else if (sscanf(opt, "-%lf", &hi) == 1) {
  980. if (d <= hi)
  981. return 0;
  982. }
  983. else if (sscanf(opt, "%lf-", &lo) == 1) {
  984. if (d >= lo)
  985. return 0;
  986. }
  987. else
  988. return BAD_SYNTAX;
  989. }
  990. else {
  991. if (sscanf(opt, "%lf", &lo) == 1) {
  992. if (d == lo)
  993. return 0;
  994. }
  995. else
  996. return BAD_SYNTAX;
  997. }
  998. }
  999. return OUT_OF_RANGE;
  1000. }
  1001. static int check_string(const char *ans, const char **opts, int *result)
  1002. {
  1003. int len = strlen(ans);
  1004. int found = 0;
  1005. int prefix = 0;
  1006. int i;
  1007. if (!opts)
  1008. return 0;
  1009. for (i = 0; opts[i]; i++) {
  1010. if (strcmp(ans, opts[i]) == 0)
  1011. return 0;
  1012. if (strncmp(ans, opts[i], len) == 0) {
  1013. *result = i;
  1014. found++;
  1015. prefix++;
  1016. }
  1017. else if (match_option(ans, opts[i])) {
  1018. if (!found)
  1019. *result = i;
  1020. found++;
  1021. }
  1022. }
  1023. switch (found) {
  1024. case 0: return OUT_OF_RANGE;
  1025. case 1: return REPLACED;
  1026. default:
  1027. switch (prefix) {
  1028. case 1: return REPLACED;
  1029. default: return AMBIGUOUS;
  1030. }
  1031. }
  1032. }
  1033. static void check_required(void)
  1034. {
  1035. struct Option *opt;
  1036. char *err;
  1037. err = NULL;
  1038. if (!st->n_opts)
  1039. return;
  1040. opt = &st->first_option;
  1041. while (opt) {
  1042. if (opt->required && !opt->answer) {
  1043. G_asprintf(&err, _("Required parameter <%s> not set:\n"
  1044. "\t(%s)"),
  1045. opt->key, (opt->label ? opt->label : opt->description));
  1046. append_error(err);
  1047. }
  1048. opt = opt->next_opt;
  1049. }
  1050. }
  1051. static void split_opts(void)
  1052. {
  1053. struct Option *opt;
  1054. const char *ptr1;
  1055. const char *ptr2;
  1056. int allocated;
  1057. int ans_num;
  1058. int len;
  1059. if (!st->n_opts)
  1060. return;
  1061. opt = &st->first_option;
  1062. while (opt) {
  1063. if ( /*opt->multiple && */ opt->answer) {
  1064. /* Allocate some memory to store array of pointers */
  1065. allocated = 10;
  1066. opt->answers = G_malloc(allocated * sizeof(char *));
  1067. ans_num = 0;
  1068. ptr1 = opt->answer;
  1069. opt->answers[ans_num] = NULL;
  1070. for (;;) {
  1071. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1072. ptr2++, len++) ;
  1073. if (len > 0) { /* skip ,, */
  1074. opt->answers[ans_num] = G_malloc(len + 1);
  1075. memcpy(opt->answers[ans_num], ptr1, len);
  1076. opt->answers[ans_num][len] = 0;
  1077. ans_num++;
  1078. if (ans_num >= allocated) {
  1079. allocated += 10;
  1080. opt->answers = G_realloc(opt->answers,
  1081. allocated * sizeof(char *));
  1082. }
  1083. opt->answers[ans_num] = NULL;
  1084. }
  1085. if (*ptr2 == '\0')
  1086. break;
  1087. ptr1 = ptr2 + 1;
  1088. if (*ptr1 == '\0')
  1089. break;
  1090. }
  1091. }
  1092. opt = opt->next_opt;
  1093. }
  1094. }
  1095. static void check_multiple_opts(void)
  1096. {
  1097. struct Option *opt;
  1098. const char *ptr;
  1099. int n_commas;
  1100. int n;
  1101. char *err;
  1102. if (!st->n_opts)
  1103. return;
  1104. err = NULL;
  1105. opt = &st->first_option;
  1106. while (opt) {
  1107. /* "-" is reserved from standard input/output */
  1108. if (opt->answer && strcmp(opt->answer, "-") && opt->key_desc) {
  1109. /* count commas */
  1110. n_commas = 1;
  1111. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1112. if (*ptr == ',')
  1113. n_commas++;
  1114. /* count items */
  1115. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1116. /* if not correct multiple of items */
  1117. if (n % n_commas) {
  1118. G_asprintf(&err,
  1119. _("Option <%s> must be provided in multiples of %d\n"
  1120. "\tYou provided %d item(s): %s"),
  1121. opt->key, n_commas, n, opt->answer);
  1122. append_error(err);
  1123. }
  1124. }
  1125. opt = opt->next_opt;
  1126. }
  1127. }
  1128. /* Check for all 'new' if element already exists */
  1129. static int check_overwrite(void)
  1130. {
  1131. struct Option *opt;
  1132. char age[KEYLENGTH];
  1133. char element[KEYLENGTH];
  1134. char desc[KEYLENGTH];
  1135. int error = 0;
  1136. const char *overstr;
  1137. int over;
  1138. st->module_info.overwrite = 0;
  1139. if (!st->n_opts)
  1140. return (0);
  1141. over = 0;
  1142. /* Check the GRASS OVERWRITE variable */
  1143. if ((overstr = G__getenv("OVERWRITE"))) {
  1144. over = atoi(overstr);
  1145. }
  1146. /* Check the GRASS_OVERWRITE environment variable */
  1147. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1148. if (atoi(overstr))
  1149. over = 1;
  1150. }
  1151. if (st->overwrite || over) {
  1152. st->module_info.overwrite = 1;
  1153. /* Set the environment so that programs run in a script also obey --o */
  1154. putenv("GRASS_OVERWRITE=1");
  1155. /* No need to check options for existing files if overwrite is true */
  1156. return error;
  1157. }
  1158. opt = &st->first_option;
  1159. while (opt) {
  1160. if (opt->answer && opt->gisprompt) {
  1161. split_gisprompt(opt->gisprompt, age, element, desc);
  1162. if (strcmp(age, "new") == 0) {
  1163. int i;
  1164. char found;
  1165. for (i = 0; opt->answers[i]; i++) {
  1166. found = FALSE;
  1167. if (strcmp(element, "file") == 0) {
  1168. if (access(opt->answers[i], F_OK) == 0)
  1169. found = TRUE;
  1170. }
  1171. else if (strcmp(element, "mapset") != 0) {
  1172. /* TODO: also other elements should be
  1173. probably skipped */
  1174. if (G_find_file(element, opt->answers[i], G_mapset())) {
  1175. found = TRUE;
  1176. }
  1177. }
  1178. if (found) { /* found */
  1179. if (!st->overwrite && !over) {
  1180. if (G_verbose() > -1) {
  1181. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1182. fprintf(stderr,
  1183. _("ERROR: option <%s>: <%s> exists.\n"),
  1184. opt->key, opt->answers[i]);
  1185. }
  1186. else {
  1187. fprintf(stderr,
  1188. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1189. getpid(), opt->key, opt->answers[i]);
  1190. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1191. getpid());
  1192. }
  1193. }
  1194. error = 1;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. opt = opt->next_opt;
  1201. }
  1202. return (error);
  1203. }
  1204. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1205. char *desc)
  1206. {
  1207. const char *ptr1;
  1208. char *ptr2;
  1209. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1210. if (*ptr1 == ',')
  1211. break;
  1212. *ptr2 = *ptr1;
  1213. }
  1214. *ptr2 = '\0';
  1215. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1216. if (*ptr1 == ',')
  1217. break;
  1218. *ptr2 = *ptr1;
  1219. }
  1220. *ptr2 = '\0';
  1221. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1222. if (*ptr1 == ',')
  1223. break;
  1224. *ptr2 = *ptr1;
  1225. }
  1226. *ptr2 = '\0';
  1227. }
  1228. static void append_error(const char *msg)
  1229. {
  1230. st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
  1231. st->error[st->n_errors++] = G_store(msg);
  1232. }
  1233. /*!
  1234. \brief Get separator string from the option.
  1235. Calls G_fatal_error() on error. Allocated string can be later freed
  1236. by G_free().
  1237. \code
  1238. char *fs;
  1239. struct Option *opt_fs;
  1240. opt_fs = G_define_standard_option(G_OPT_F_SEP);
  1241. if (G_parser(argc, argv))
  1242. exit(EXIT_FAILURE);
  1243. fs = G_option_to_separator(opt_fs);
  1244. \endcode
  1245. \param option pointer to separator option
  1246. \return allocated string with separator
  1247. */
  1248. char* G_option_to_separator(const struct Option *option)
  1249. {
  1250. char* sep;
  1251. if (option->gisprompt == NULL ||
  1252. strcmp(option->gisprompt, "old,separator,separator") != 0)
  1253. G_fatal_error(_("%s= is not a separator option"), option->key);
  1254. if (option->answer == NULL)
  1255. G_fatal_error(_("No separator given for %s="), option->key);
  1256. if (strcmp(option->answer, "pipe") == 0)
  1257. sep = G_store("|");
  1258. else if (strcmp(option->answer, "comma") == 0)
  1259. sep = G_store(",");
  1260. else if (strcmp(option->answer, "space") == 0)
  1261. sep = G_store(" ");
  1262. else if (strcmp(option->answer, "tab") == 0 ||
  1263. strcmp(option->answer, "\\t") == 0)
  1264. sep = G_store("\t");
  1265. else if (strcmp(option->answer, "newline") == 0 ||
  1266. strcmp(option->answer, "\\n") == 0)
  1267. sep = G_store("\n");
  1268. else
  1269. sep = G_store(option->answer);
  1270. G_debug(1, "G_option_to_separator(): key = %s -> sep = '%s'",
  1271. option->key, sep);
  1272. return sep;
  1273. }
  1274. /*!
  1275. \brief Get an input/output file pointer from the option. If the file name is
  1276. omitted or '-', it returns either stdin or stdout based on the gisprompt.
  1277. Calls G_fatal_error() on error. File pointer can be later closed by
  1278. G_close_option_file().
  1279. \code
  1280. FILE *fp_input;
  1281. FILE *fp_output;
  1282. struct Option *opt_input;
  1283. struct Option *opt_output;
  1284. opt_input = G_define_standard_option(G_OPT_F_INPUT);
  1285. opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
  1286. if (G_parser(argc, argv))
  1287. exit(EXIT_FAILURE);
  1288. fp_input = G_open_option_file(opt_input);
  1289. fp_output = G_open_option_file(opt_output);
  1290. ...
  1291. G_close_option_file(fp_input);
  1292. G_close_option_file(fp_output);
  1293. \endcode
  1294. \param option pointer to a file option
  1295. \return file pointer
  1296. */
  1297. FILE *G_open_option_file(const struct Option *option)
  1298. {
  1299. int stdinout;
  1300. FILE *fp;
  1301. stdinout = !option->answer || !*(option->answer) ||
  1302. strcmp(option->answer, "-") == 0;
  1303. if (option->gisprompt == NULL)
  1304. G_fatal_error(_("%s= is not a file option"), option->key);
  1305. else if (option->multiple)
  1306. G_fatal_error(_("Opening multiple files not supported for %s="),
  1307. option->key);
  1308. else if (strcmp(option->gisprompt, "old,file,file") == 0) {
  1309. if (stdinout)
  1310. fp = stdin;
  1311. else if ((fp = fopen(option->answer, "r")) == NULL)
  1312. G_fatal_error(_("Unable to open %s file <%s>"),
  1313. option->key, option->answer);
  1314. } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
  1315. if (stdinout)
  1316. fp = stdout;
  1317. else if ((fp = fopen(option->answer, "w")) == NULL)
  1318. G_fatal_error(_("Unable to create %s file <%s>"),
  1319. option->key, option->answer);
  1320. } else
  1321. G_fatal_error(_("%s= is not a file option"), option->key);
  1322. return fp;
  1323. }
  1324. /*!
  1325. \brief Close an input/output file returned by G_open_option_file(). If the
  1326. file pointer is stdin, stdout, or stderr, nothing happens.
  1327. \param file pointer
  1328. */
  1329. void G_close_option_file(FILE *fp)
  1330. {
  1331. if (fp != stdin && fp != stdout && fp != stderr)
  1332. fclose(fp);
  1333. }