parser.c 39 KB

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