parser.c 40 KB

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