parser.c 43 KB

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