parser.c 38 KB

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