parser.c 36 KB

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