parser.c 36 KB

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