parser.c 40 KB

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