parser.c 39 KB

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