parser.c 45 KB

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