parser.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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(int, char **, 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. if (st->n_exclusive >= st->allocated_exclusive) {
  734. st->allocated_exclusive += 10;
  735. st->exclusive = G_realloc(st->exclusive,
  736. st->allocated_exclusive *
  737. sizeof(struct Exclusive));
  738. }
  739. exclusive = &st->exclusive[st->n_exclusive++];
  740. exclusive->name = name;
  741. exclusive->allocated_keys = 10;
  742. exclusive->n_keys = 0;
  743. exclusive->keys = G_malloc(exclusive->allocated_keys *
  744. sizeof(char *));
  745. }
  746. if (!has_exclusive_key(exclusive->n_keys, exclusive->keys,
  747. 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(int n_keys, char **keys, char *key)
  774. {
  775. int i;
  776. for (i = 0; i < n_keys; i++) {
  777. if (strcmp(keys[i], key) == 0)
  778. return 1;
  779. }
  780. return 0;
  781. }
  782. static void check_exclusive(int print_group)
  783. {
  784. int i, n, allocated, len;
  785. char **keys, *err;
  786. n = 0;
  787. allocated = 0;
  788. len = 0;
  789. keys = NULL;
  790. for (i = 0; i < st->n_exclusive; i++) {
  791. struct Exclusive *exclusive;
  792. exclusive = &st->exclusive[i];
  793. G_debug(1, "check_exclusive(): Exclusive option/flag group: %s",
  794. exclusive->name);
  795. if (exclusive->n_keys >= 1)
  796. G_debug(1, "check_exclusive():\t%s", exclusive->keys[0]);
  797. if (exclusive->n_keys > 1) {
  798. int j;
  799. if (print_group) {
  800. len = strlen(exclusive->name);
  801. for (j = 0; j < exclusive->n_keys; j++) {
  802. len += strlen(exclusive->keys[j]) + 2; /* 2 for comma adn
  803. space */
  804. if (j > 0)
  805. G_debug(1, "check_exclusive():\t%s",
  806. exclusive->keys[j]);
  807. }
  808. err = G_malloc(len + 100);
  809. sprintf(err, _("Mutually exclusive options/flags "
  810. "in group '%s': "), exclusive->name);
  811. len = strlen(err);
  812. for (j = 0; j < exclusive->n_keys - 2; j++) {
  813. sprintf(err + len, "%s, ", exclusive->keys[j]);
  814. len = strlen(err);
  815. }
  816. sprintf(err + len, _("%s and %s"), exclusive->keys[j],
  817. exclusive->keys[j + 1]);
  818. append_error(err);
  819. }
  820. else {
  821. for (j = 0; j < exclusive->n_keys; j++) {
  822. if (!has_exclusive_key(n, keys, exclusive->keys[j])) {
  823. if (n >= allocated) {
  824. allocated += 10;
  825. keys = G_realloc(keys, allocated * sizeof(char *));
  826. }
  827. keys[n] = exclusive->keys[j];
  828. len += strlen(keys[n++]) + 2; /* 2 for comma and space
  829. */
  830. }
  831. }
  832. }
  833. }
  834. }
  835. if (!print_group && n) {
  836. err = G_malloc(len + 100);
  837. sprintf(err, _("Mutually exclusive options/flags: "));
  838. len = strlen(err);
  839. for (i = 0; i < n - 2; i++) {
  840. sprintf(err + len, "%s, ", keys[i]);
  841. len = strlen(err);
  842. }
  843. sprintf(err + len, _("%s and %s"), keys[i], keys[i + 1]);
  844. append_error(err);
  845. }
  846. }
  847. static void set_flag(int f)
  848. {
  849. struct Flag *flag;
  850. char *err;
  851. err = NULL;
  852. /* Flag is not valid if there are no flags to set */
  853. if (!st->n_flags) {
  854. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  855. append_error(err);
  856. return;
  857. }
  858. /* Find flag with corrrect keyword */
  859. flag = &st->first_flag;
  860. while (flag) {
  861. if (flag->key == f) {
  862. flag->answer = 1;
  863. if (flag->suppress_required)
  864. st->suppress_required = 1;
  865. add_exclusive(NULL, f, flag->exclusive);
  866. return;
  867. }
  868. flag = flag->next_flag;
  869. }
  870. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  871. append_error(err);
  872. }
  873. /* contents() is used to find things strings with characters like commas and
  874. * dashes.
  875. */
  876. static int contains(const char *s, int c)
  877. {
  878. while (*s) {
  879. if (*s == c)
  880. return TRUE;
  881. s++;
  882. }
  883. return FALSE;
  884. }
  885. static int is_option(const char *string)
  886. {
  887. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  888. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  889. }
  890. static int match_option_1(const char *string, const char *option)
  891. {
  892. const char *next;
  893. if (*string == '\0')
  894. return 1;
  895. if (*option == '\0')
  896. return 0;
  897. if (*string == *option && match_option_1(string + 1, option + 1))
  898. return 1;
  899. if (*option == '_' && match_option_1(string, option + 1))
  900. return 1;
  901. next = strchr(option, '_');
  902. if (!next)
  903. return 0;
  904. if (*string == '_')
  905. return match_option_1(string + 1, next + 1);
  906. return match_option_1(string, next + 1);
  907. }
  908. static int match_option(const char *string, const char *option)
  909. {
  910. return (*string == *option)
  911. && match_option_1(string + 1, option + 1);
  912. }
  913. static void set_option(const char *string)
  914. {
  915. struct Option *at_opt = NULL;
  916. struct Option *opt = NULL;
  917. int found;
  918. int prefix;
  919. size_t key_len;
  920. char the_key[KEYLENGTH];
  921. char *ptr, *err;
  922. err = NULL;
  923. for (ptr = the_key; *string != '='; ptr++, string++)
  924. *ptr = *string;
  925. *ptr = '\0';
  926. string++;
  927. /* Find option with best keyword match */
  928. found = 0;
  929. prefix = 0;
  930. key_len = strlen(the_key);
  931. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  932. if (!at_opt->key)
  933. continue;
  934. if (strcmp(the_key, at_opt->key) == 0) {
  935. opt = at_opt;
  936. found = 1;
  937. break;
  938. }
  939. if (strncmp(the_key, at_opt->key, key_len) == 0) {
  940. opt = at_opt;
  941. found++;
  942. prefix++;
  943. }
  944. else if (match_option(the_key, at_opt->key)) {
  945. if (!found)
  946. opt = at_opt;
  947. found++;
  948. }
  949. }
  950. if (found > 1 && prefix > 1) {
  951. G_asprintf(&err, _("Sorry, <%s=> is ambiguous"), the_key);
  952. append_error(err);
  953. return;
  954. }
  955. /* If there is no match, complain */
  956. if (found == 0) {
  957. G_asprintf(&err, _("Sorry, <%s> is not a valid parameter"), the_key);
  958. append_error(err);
  959. return;
  960. }
  961. /* Allocate memory where answer is stored */
  962. if (opt->count++) {
  963. if (!opt->multiple) {
  964. G_asprintf(&err, _("Option <%s> does not accept multiple answers"), opt->key);
  965. append_error(err);
  966. }
  967. opt->answer = G_realloc(opt->answer,
  968. strlen(opt->answer) + strlen(string) + 2);
  969. strcat(opt->answer, ",");
  970. strcat(opt->answer, string);
  971. }
  972. else
  973. opt->answer = G_store(string);
  974. add_exclusive(opt->key, 0, opt->exclusive);
  975. }
  976. static void check_opts(void)
  977. {
  978. struct Option *opt;
  979. int ans;
  980. if (!st->n_opts)
  981. return;
  982. opt = &st->first_option;
  983. while (opt) {
  984. /* Check answer against options if any */
  985. if (opt->answer) {
  986. if (opt->multiple == 0)
  987. check_an_opt(opt->key, opt->type,
  988. opt->options, opt->opts, &opt->answer);
  989. else {
  990. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  991. check_an_opt(opt->key, opt->type,
  992. opt->options, opt->opts, &opt->answers[ans]);
  993. }
  994. }
  995. /* Check answer against user's check subroutine if any */
  996. if (opt->checker)
  997. opt->checker(opt->answer);
  998. opt = opt->next_opt;
  999. }
  1000. }
  1001. static void check_an_opt(const char *key, int type, const char *options,
  1002. const char **opts, char **answerp)
  1003. {
  1004. const char *answer = *answerp;
  1005. int error;
  1006. char *err;
  1007. int found;
  1008. error = 0;
  1009. err = NULL;
  1010. switch (type) {
  1011. case TYPE_INTEGER:
  1012. error = check_int(answer, opts);
  1013. break;
  1014. case TYPE_DOUBLE:
  1015. error = check_double(answer, opts);
  1016. break;
  1017. case TYPE_STRING:
  1018. error = check_string(answer, opts, &found);
  1019. break;
  1020. }
  1021. switch (error) {
  1022. case 0:
  1023. break;
  1024. case BAD_SYNTAX:
  1025. G_asprintf(&err,
  1026. _("Illegal range syntax for parameter <%s>\n"
  1027. "\tPresented as: %s"), key, options);
  1028. append_error(err);
  1029. break;
  1030. case OUT_OF_RANGE:
  1031. G_asprintf(&err,
  1032. _("Value <%s> out of range for parameter <%s>\n"
  1033. "\tLegal range: %s"), answer, key, options);
  1034. append_error(err);
  1035. break;
  1036. case MISSING_VALUE:
  1037. G_asprintf(&err,
  1038. _("Missing value for parameter <%s>"),
  1039. key);
  1040. append_error(err);
  1041. break;
  1042. case AMBIGUOUS:
  1043. G_asprintf(&err,
  1044. _("Value <%s> ambiguous for parameter <%s>\n"
  1045. "\tValid options: %s"), answer, key, options);
  1046. append_error(err);
  1047. break;
  1048. case REPLACED:
  1049. *answerp = G_store(opts[found]);
  1050. error = 0;
  1051. break;
  1052. }
  1053. }
  1054. static int check_int(const char *ans, const char **opts)
  1055. {
  1056. int d, i;
  1057. /* "-" is reserved for standard input */
  1058. if (strcmp(ans, "-") == 0)
  1059. return 0;
  1060. if (sscanf(ans, "%d", &d) != 1)
  1061. return MISSING_VALUE;
  1062. if (!opts)
  1063. return 0;
  1064. for (i = 0; opts[i]; i++) {
  1065. const char *opt = opts[i];
  1066. int lo, hi;
  1067. if (contains(opt, '-')) {
  1068. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  1069. if (d >= lo && d <= hi)
  1070. return 0;
  1071. }
  1072. else if (sscanf(opt, "-%d", &hi) == 1) {
  1073. if (d <= hi)
  1074. return 0;
  1075. }
  1076. else if (sscanf(opt, "%d-", &lo) == 1) {
  1077. if (d >= lo)
  1078. return 0;
  1079. }
  1080. else
  1081. return BAD_SYNTAX;
  1082. }
  1083. else {
  1084. if (sscanf(opt, "%d", &lo) == 1) {
  1085. if (d == lo)
  1086. return 0;
  1087. }
  1088. else
  1089. return BAD_SYNTAX;
  1090. }
  1091. }
  1092. return OUT_OF_RANGE;
  1093. }
  1094. static int check_double(const char *ans, const char **opts)
  1095. {
  1096. double d;
  1097. int i;
  1098. /* "-" is reserved for standard input */
  1099. if (strcmp(ans, "-") == 0)
  1100. return 0;
  1101. if (sscanf(ans, "%lf", &d) != 1)
  1102. return MISSING_VALUE;
  1103. if (!opts)
  1104. return 0;
  1105. for (i = 0; opts[i]; i++) {
  1106. const char *opt = opts[i];
  1107. double lo, hi;
  1108. if (contains(opt, '-')) {
  1109. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  1110. if (d >= lo && d <= hi)
  1111. return 0;
  1112. }
  1113. else if (sscanf(opt, "-%lf", &hi) == 1) {
  1114. if (d <= hi)
  1115. return 0;
  1116. }
  1117. else if (sscanf(opt, "%lf-", &lo) == 1) {
  1118. if (d >= lo)
  1119. return 0;
  1120. }
  1121. else
  1122. return BAD_SYNTAX;
  1123. }
  1124. else {
  1125. if (sscanf(opt, "%lf", &lo) == 1) {
  1126. if (d == lo)
  1127. return 0;
  1128. }
  1129. else
  1130. return BAD_SYNTAX;
  1131. }
  1132. }
  1133. return OUT_OF_RANGE;
  1134. }
  1135. static int check_string(const char *ans, const char **opts, int *result)
  1136. {
  1137. int len = strlen(ans);
  1138. int found = 0;
  1139. int prefix = 0;
  1140. int i;
  1141. if (!opts)
  1142. return 0;
  1143. for (i = 0; opts[i]; i++) {
  1144. if (strcmp(ans, opts[i]) == 0)
  1145. return 0;
  1146. if (strncmp(ans, opts[i], len) == 0) {
  1147. *result = i;
  1148. found++;
  1149. prefix++;
  1150. }
  1151. else if (match_option(ans, opts[i])) {
  1152. if (!found)
  1153. *result = i;
  1154. found++;
  1155. }
  1156. }
  1157. switch (found) {
  1158. case 0: return OUT_OF_RANGE;
  1159. case 1: return REPLACED;
  1160. default:
  1161. switch (prefix) {
  1162. case 1: return REPLACED;
  1163. default: return AMBIGUOUS;
  1164. }
  1165. }
  1166. }
  1167. static void check_required(void)
  1168. {
  1169. struct Option *opt;
  1170. char *err;
  1171. err = NULL;
  1172. if (!st->n_opts)
  1173. return;
  1174. opt = &st->first_option;
  1175. while (opt) {
  1176. if (opt->required && !opt->answer) {
  1177. G_asprintf(&err, _("Required parameter <%s> not set:\n"
  1178. "\t(%s)"),
  1179. opt->key, (opt->label ? opt->label : opt->description));
  1180. append_error(err);
  1181. }
  1182. opt = opt->next_opt;
  1183. }
  1184. }
  1185. static void split_opts(void)
  1186. {
  1187. struct Option *opt;
  1188. const char *ptr1;
  1189. const char *ptr2;
  1190. int allocated;
  1191. int ans_num;
  1192. int len;
  1193. if (!st->n_opts)
  1194. return;
  1195. opt = &st->first_option;
  1196. while (opt) {
  1197. if ( /*opt->multiple && */ opt->answer) {
  1198. /* Allocate some memory to store array of pointers */
  1199. allocated = 10;
  1200. opt->answers = G_malloc(allocated * sizeof(char *));
  1201. ans_num = 0;
  1202. ptr1 = opt->answer;
  1203. opt->answers[ans_num] = NULL;
  1204. for (;;) {
  1205. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1206. ptr2++, len++) ;
  1207. if (len > 0) { /* skip ,, */
  1208. opt->answers[ans_num] = G_malloc(len + 1);
  1209. memcpy(opt->answers[ans_num], ptr1, len);
  1210. opt->answers[ans_num][len] = 0;
  1211. ans_num++;
  1212. if (ans_num >= allocated) {
  1213. allocated += 10;
  1214. opt->answers = G_realloc(opt->answers,
  1215. allocated * sizeof(char *));
  1216. }
  1217. opt->answers[ans_num] = NULL;
  1218. }
  1219. if (*ptr2 == '\0')
  1220. break;
  1221. ptr1 = ptr2 + 1;
  1222. if (*ptr1 == '\0')
  1223. break;
  1224. }
  1225. }
  1226. opt = opt->next_opt;
  1227. }
  1228. }
  1229. static void check_multiple_opts(void)
  1230. {
  1231. struct Option *opt;
  1232. const char *ptr;
  1233. int n_commas;
  1234. int n;
  1235. char *err;
  1236. if (!st->n_opts)
  1237. return;
  1238. err = NULL;
  1239. opt = &st->first_option;
  1240. while (opt) {
  1241. /* "-" is reserved from standard input/output */
  1242. if (opt->answer && strcmp(opt->answer, "-") && opt->key_desc) {
  1243. /* count commas */
  1244. n_commas = 1;
  1245. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1246. if (*ptr == ',')
  1247. n_commas++;
  1248. /* count items */
  1249. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1250. /* if not correct multiple of items */
  1251. if (n % n_commas) {
  1252. G_asprintf(&err,
  1253. _("Option <%s> must be provided in multiples of %d\n"
  1254. "\tYou provided %d item(s): %s"),
  1255. opt->key, n_commas, n, opt->answer);
  1256. append_error(err);
  1257. }
  1258. }
  1259. opt = opt->next_opt;
  1260. }
  1261. }
  1262. /* Check for all 'new' if element already exists */
  1263. static int check_overwrite(void)
  1264. {
  1265. struct Option *opt;
  1266. char age[KEYLENGTH];
  1267. char element[KEYLENGTH];
  1268. char desc[KEYLENGTH];
  1269. int error = 0;
  1270. const char *overstr;
  1271. int over;
  1272. st->module_info.overwrite = 0;
  1273. if (!st->n_opts)
  1274. return (0);
  1275. over = 0;
  1276. /* Check the GRASS OVERWRITE variable */
  1277. if ((overstr = G__getenv("OVERWRITE"))) {
  1278. over = atoi(overstr);
  1279. }
  1280. /* Check the GRASS_OVERWRITE environment variable */
  1281. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1282. if (atoi(overstr))
  1283. over = 1;
  1284. }
  1285. if (st->overwrite || over) {
  1286. st->module_info.overwrite = 1;
  1287. /* Set the environment so that programs run in a script also obey --o */
  1288. putenv("GRASS_OVERWRITE=1");
  1289. /* No need to check options for existing files if overwrite is true */
  1290. return error;
  1291. }
  1292. opt = &st->first_option;
  1293. while (opt) {
  1294. if (opt->answer && opt->gisprompt) {
  1295. split_gisprompt(opt->gisprompt, age, element, desc);
  1296. if (strcmp(age, "new") == 0) {
  1297. int i;
  1298. char found;
  1299. for (i = 0; opt->answers[i]; i++) {
  1300. found = FALSE;
  1301. if (strcmp(element, "file") == 0) {
  1302. if (access(opt->answers[i], F_OK) == 0)
  1303. found = TRUE;
  1304. }
  1305. else if (strcmp(element, "mapset") != 0) {
  1306. /* TODO: also other elements should be
  1307. probably skipped */
  1308. if (G_find_file(element, opt->answers[i], G_mapset())) {
  1309. found = TRUE;
  1310. }
  1311. }
  1312. if (found) { /* found */
  1313. if (!st->overwrite && !over) {
  1314. if (G_verbose() > -1) {
  1315. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1316. fprintf(stderr,
  1317. _("ERROR: option <%s>: <%s> exists.\n"),
  1318. opt->key, opt->answers[i]);
  1319. }
  1320. else {
  1321. fprintf(stderr,
  1322. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1323. getpid(), opt->key, opt->answers[i]);
  1324. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1325. getpid());
  1326. }
  1327. }
  1328. error = 1;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. }
  1334. opt = opt->next_opt;
  1335. }
  1336. return (error);
  1337. }
  1338. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1339. char *desc)
  1340. {
  1341. const char *ptr1;
  1342. char *ptr2;
  1343. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1344. if (*ptr1 == ',')
  1345. break;
  1346. *ptr2 = *ptr1;
  1347. }
  1348. *ptr2 = '\0';
  1349. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1350. if (*ptr1 == ',')
  1351. break;
  1352. *ptr2 = *ptr1;
  1353. }
  1354. *ptr2 = '\0';
  1355. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1356. if (*ptr1 == ',')
  1357. break;
  1358. *ptr2 = *ptr1;
  1359. }
  1360. *ptr2 = '\0';
  1361. }
  1362. static void append_error(const char *msg)
  1363. {
  1364. st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
  1365. st->error[st->n_errors++] = G_store(msg);
  1366. }
  1367. /*!
  1368. \brief Get separator string from the option.
  1369. Calls G_fatal_error() on error. Allocated string can be later freed
  1370. by G_free().
  1371. \code
  1372. char *fs;
  1373. struct Option *opt_fs;
  1374. opt_fs = G_define_standard_option(G_OPT_F_SEP);
  1375. if (G_parser(argc, argv))
  1376. exit(EXIT_FAILURE);
  1377. fs = G_option_to_separator(opt_fs);
  1378. \endcode
  1379. \param option pointer to separator option
  1380. \return allocated string with separator
  1381. */
  1382. char* G_option_to_separator(const struct Option *option)
  1383. {
  1384. char* sep;
  1385. if (option->gisprompt == NULL ||
  1386. strcmp(option->gisprompt, "old,separator,separator") != 0)
  1387. G_fatal_error(_("%s= is not a separator option"), option->key);
  1388. if (option->answer == NULL)
  1389. G_fatal_error(_("No separator given for %s="), option->key);
  1390. if (strcmp(option->answer, "pipe") == 0)
  1391. sep = G_store("|");
  1392. else if (strcmp(option->answer, "comma") == 0)
  1393. sep = G_store(",");
  1394. else if (strcmp(option->answer, "space") == 0)
  1395. sep = G_store(" ");
  1396. else if (strcmp(option->answer, "tab") == 0 ||
  1397. strcmp(option->answer, "\\t") == 0)
  1398. sep = G_store("\t");
  1399. else if (strcmp(option->answer, "newline") == 0 ||
  1400. strcmp(option->answer, "\\n") == 0)
  1401. sep = G_store("\n");
  1402. else
  1403. sep = G_store(option->answer);
  1404. G_debug(1, "G_option_to_separator(): key = %s -> sep = '%s'",
  1405. option->key, sep);
  1406. return sep;
  1407. }
  1408. /*!
  1409. \brief Get an input/output file pointer from the option. If the file name is
  1410. omitted or '-', it returns either stdin or stdout based on the gisprompt.
  1411. Calls G_fatal_error() on error. File pointer can be later closed by
  1412. G_close_option_file().
  1413. \code
  1414. FILE *fp_input;
  1415. FILE *fp_output;
  1416. struct Option *opt_input;
  1417. struct Option *opt_output;
  1418. opt_input = G_define_standard_option(G_OPT_F_INPUT);
  1419. opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
  1420. if (G_parser(argc, argv))
  1421. exit(EXIT_FAILURE);
  1422. fp_input = G_open_option_file(opt_input);
  1423. fp_output = G_open_option_file(opt_output);
  1424. ...
  1425. G_close_option_file(fp_input);
  1426. G_close_option_file(fp_output);
  1427. \endcode
  1428. \param option pointer to a file option
  1429. \return file pointer
  1430. */
  1431. FILE *G_open_option_file(const struct Option *option)
  1432. {
  1433. int stdinout;
  1434. FILE *fp;
  1435. stdinout = !option->answer || !*(option->answer) ||
  1436. strcmp(option->answer, "-") == 0;
  1437. if (option->gisprompt == NULL)
  1438. G_fatal_error(_("%s= is not a file option"), option->key);
  1439. else if (option->multiple)
  1440. G_fatal_error(_("Opening multiple files not supported for %s="),
  1441. option->key);
  1442. else if (strcmp(option->gisprompt, "old,file,file") == 0) {
  1443. if (stdinout)
  1444. fp = stdin;
  1445. else if ((fp = fopen(option->answer, "r")) == NULL)
  1446. G_fatal_error(_("Unable to open %s file <%s>"),
  1447. option->key, option->answer);
  1448. } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
  1449. if (stdinout)
  1450. fp = stdout;
  1451. else if ((fp = fopen(option->answer, "w")) == NULL)
  1452. G_fatal_error(_("Unable to create %s file <%s>"),
  1453. option->key, option->answer);
  1454. } else
  1455. G_fatal_error(_("%s= is not a file option"), option->key);
  1456. return fp;
  1457. }
  1458. /*!
  1459. \brief Close an input/output file returned by G_open_option_file(). If the
  1460. file pointer is stdin, stdout, or stderr, nothing happens.
  1461. \param file pointer
  1462. */
  1463. void G_close_option_file(FILE *fp)
  1464. {
  1465. if (fp != stdin && fp != stdout && fp != stderr)
  1466. fclose(fp);
  1467. }