parser.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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 void 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. module_gui_wx();
  378. return -1;
  379. }
  380. else 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 "--rest-description" then print out
  410. * a ReST description of the task */
  411. if (strcmp(argv[1], "--rest-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. module_gui_wx();
  495. return -1;
  496. }
  497. /* Check multiple options */
  498. check_multiple_opts();
  499. /* Check answers against options and check subroutines */
  500. if (!opt_checked)
  501. check_opts();
  502. /* Make sure all required options are set */
  503. if (!st->suppress_required)
  504. check_required();
  505. G__check_option_rules();
  506. if (st->n_errors > 0) {
  507. if (G_verbose() > -1) {
  508. if (G_verbose() > G_verbose_min())
  509. G_usage();
  510. fprintf(stderr, "\n");
  511. for (i = 0; i < st->n_errors; i++) {
  512. fprintf(stderr, "%s: %s\n", _("ERROR"), st->error[i]);
  513. }
  514. }
  515. return -1;
  516. }
  517. if (check_overwrite())
  518. return -1;
  519. return 0;
  520. }
  521. /*!
  522. * \brief Creates command to run non-interactive.
  523. *
  524. * Creates a command-line that runs the current command completely
  525. * non-interactive.
  526. *
  527. * \return pointer to a char string
  528. */
  529. char *G_recreate_command(void)
  530. {
  531. char *buff;
  532. char flg[4];
  533. char *cur;
  534. const char *tmp;
  535. struct Flag *flag;
  536. struct Option *opt;
  537. int n, len, slen;
  538. int nalloced = 0;
  539. G_debug(3, "G_recreate_command()");
  540. /* Flag is not valid if there are no flags to set */
  541. buff = G_calloc(1024, sizeof(char));
  542. nalloced += 1024;
  543. tmp = G_program_name();
  544. len = strlen(tmp);
  545. if (len >= nalloced) {
  546. nalloced += (1024 > len) ? 1024 : len + 1;
  547. buff = G_realloc(buff, nalloced);
  548. }
  549. cur = buff;
  550. strcpy(cur, tmp);
  551. cur += len;
  552. if (st->n_flags) {
  553. flag = &st->first_flag;
  554. while (flag) {
  555. if (flag->answer == 1) {
  556. flg[0] = ' ';
  557. flg[1] = '-';
  558. flg[2] = flag->key;
  559. flg[3] = '\0';
  560. slen = strlen(flg);
  561. if (len + slen >= nalloced) {
  562. nalloced +=
  563. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  564. buff = G_realloc(buff, nalloced);
  565. cur = buff + len;
  566. }
  567. strcpy(cur, flg);
  568. cur += slen;
  569. len += slen;
  570. }
  571. flag = flag->next_flag;
  572. }
  573. }
  574. opt = &st->first_option;
  575. while (st->n_opts && opt) {
  576. if (opt->answer && opt->answers && opt->answers[0]) {
  577. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  578. if (len + slen >= nalloced) {
  579. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  580. buff = G_realloc(buff, nalloced);
  581. cur = buff + len;
  582. }
  583. strcpy(cur, " ");
  584. cur++;
  585. strcpy(cur, opt->key);
  586. cur = strchr(cur, '\0');
  587. strcpy(cur, "=");
  588. cur++;
  589. if (opt->type == TYPE_STRING) {
  590. strcpy(cur, "\"");
  591. cur++;
  592. }
  593. strcpy(cur, opt->answers[0]);
  594. cur = strchr(cur, '\0');
  595. len = cur - buff;
  596. for (n = 1; opt->answers[n]; n++) {
  597. if (!opt->answers[n])
  598. break;
  599. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  600. if (len + slen >= nalloced) {
  601. nalloced +=
  602. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  603. buff = G_realloc(buff, nalloced);
  604. cur = buff + len;
  605. }
  606. strcpy(cur, ",");
  607. cur++;
  608. strcpy(cur, opt->answers[n]);
  609. cur = strchr(cur, '\0');
  610. len = cur - buff;
  611. }
  612. if (opt->type == TYPE_STRING) {
  613. strcpy(cur, "\"");
  614. cur++;
  615. len = cur - buff;
  616. }
  617. }
  618. opt = opt->next_opt;
  619. }
  620. return buff;
  621. }
  622. /*!
  623. \brief Add keyword to the list
  624. \param keyword keyword string
  625. */
  626. void G_add_keyword(const char *keyword)
  627. {
  628. if (st->n_keys >= st->n_keys_alloc) {
  629. st->n_keys_alloc += 10;
  630. st->module_info.keywords = G_realloc(st->module_info.keywords,
  631. st->n_keys_alloc * sizeof(char *));
  632. }
  633. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  634. }
  635. /*!
  636. \brief Set keywords from the string
  637. \param keywords keywords separated by commas
  638. */
  639. void G_set_keywords(const char *keywords)
  640. {
  641. char **tokens = G_tokenize(keywords, ",");
  642. st->module_info.keywords = (const char **)tokens;
  643. st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
  644. }
  645. int G__uses_new_gisprompt(void)
  646. {
  647. struct Option *opt;
  648. char age[KEYLENGTH];
  649. char element[KEYLENGTH];
  650. char desc[KEYLENGTH];
  651. if (st->module_info.overwrite)
  652. return 1;
  653. /* figure out if any of the options use a "new" gisprompt */
  654. /* This is to see if we should spit out the --o flag */
  655. if (st->n_opts) {
  656. opt = &st->first_option;
  657. while (opt) {
  658. if (opt->gisprompt) {
  659. split_gisprompt(opt->gisprompt, age, element, desc);
  660. if (strcmp(age, "new") == 0)
  661. return 1;
  662. }
  663. opt = opt->next_opt;
  664. }
  665. }
  666. return 0;
  667. }
  668. /*!
  669. \brief Print list of keywords (internal use only)
  670. If <em>format</em> function is NULL than list of keywords is printed
  671. comma-separated.
  672. \param[out] fd file where to print
  673. \param format pointer to print function
  674. */
  675. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  676. {
  677. int i;
  678. for(i = 0; i < st->n_keys; i++) {
  679. if (!format) {
  680. fprintf(fd, "%s", st->module_info.keywords[i]);
  681. }
  682. else {
  683. format(fd, st->module_info.keywords[i]);
  684. }
  685. if (i < st->n_keys - 1)
  686. fprintf(fd, ", ");
  687. }
  688. fflush(fd);
  689. }
  690. /*!
  691. \brief Get overwrite value
  692. \return 1 overwrite enabled
  693. \return 0 overwrite disabled
  694. */
  695. int G_get_overwrite()
  696. {
  697. return st->overwrite;
  698. }
  699. void define_keywords(void)
  700. {
  701. st->n_keys = 0;
  702. st->n_keys_alloc = 0;
  703. }
  704. /**************************************************************************
  705. *
  706. * The remaining routines are all local (static) routines used to support
  707. * the parsing process.
  708. *
  709. **************************************************************************/
  710. /*!
  711. \brief Invoke GUI dialog
  712. */
  713. void module_gui_wx(void)
  714. {
  715. char script[GPATH_MAX];
  716. if (!st->pgm_path)
  717. st->pgm_path = G_program_name();
  718. if (!st->pgm_path)
  719. G_fatal_error(_("Unable to determine program name"));
  720. sprintf(script, "%s/gui/wxpython/gui_core/forms.py",
  721. getenv("GISBASE"));
  722. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, G_recreate_command(), NULL);
  723. }
  724. void set_flag(int f)
  725. {
  726. struct Flag *flag;
  727. char *err;
  728. err = NULL;
  729. /* Flag is not valid if there are no flags to set */
  730. if (!st->n_flags) {
  731. G_asprintf(&err, _("%s: Sorry, <%c> is not a valid flag"), G_program_name(), f);
  732. append_error(err);
  733. return;
  734. }
  735. /* Find flag with corrrect keyword */
  736. flag = &st->first_flag;
  737. while (flag) {
  738. if (flag->key == f) {
  739. flag->answer = 1;
  740. if (flag->suppress_required)
  741. st->suppress_required = 1;
  742. return;
  743. }
  744. flag = flag->next_flag;
  745. }
  746. G_asprintf(&err, _("%s: Sorry, <%c> is not a valid flag"), G_program_name(), f);
  747. append_error(err);
  748. }
  749. /* contents() is used to find things strings with characters like commas and
  750. * dashes.
  751. */
  752. int contains(const char *s, int c)
  753. {
  754. while (*s) {
  755. if (*s == c)
  756. return TRUE;
  757. s++;
  758. }
  759. return FALSE;
  760. }
  761. int valid_option_name(const char *string)
  762. {
  763. int m = strlen(string);
  764. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  765. if (!m)
  766. return 0;
  767. if (m != n)
  768. return 0;
  769. if (string[m-1] == '_')
  770. return 0;
  771. return 1;
  772. }
  773. int is_option(const char *string)
  774. {
  775. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  776. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  777. }
  778. int match_option_1(const char *string, const char *option)
  779. {
  780. const char *next;
  781. if (*string == '\0')
  782. return 1;
  783. if (*option == '\0')
  784. return 0;
  785. if (*string == *option && match_option_1(string + 1, option + 1))
  786. return 1;
  787. if (*option == '_' && match_option_1(string, option + 1))
  788. return 1;
  789. next = strchr(option, '_');
  790. if (!next)
  791. return 0;
  792. if (*string == '_')
  793. return match_option_1(string + 1, next + 1);
  794. return match_option_1(string, next + 1);
  795. }
  796. int match_option(const char *string, const char *option)
  797. {
  798. return (*string == *option)
  799. && match_option_1(string + 1, option + 1);
  800. }
  801. void set_option(const char *string)
  802. {
  803. struct Option *at_opt = NULL;
  804. struct Option *opt = NULL;
  805. size_t key_len;
  806. char the_key[KEYLENGTH];
  807. char *ptr, *err;
  808. struct Option *matches[MAX_MATCHES];
  809. int found = 0;
  810. err = NULL;
  811. for (ptr = the_key; *string != '='; ptr++, string++)
  812. *ptr = *string;
  813. *ptr = '\0';
  814. string++;
  815. /* Find option with best keyword match */
  816. key_len = strlen(the_key);
  817. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  818. if (!at_opt->key)
  819. continue;
  820. if (strcmp(the_key, at_opt->key) == 0) {
  821. matches[0] = at_opt;
  822. found = 1;
  823. break;
  824. }
  825. if (strncmp(the_key, at_opt->key, key_len) == 0 ||
  826. match_option(the_key, at_opt->key)) {
  827. if (found >= MAX_MATCHES)
  828. G_fatal_error("Too many matches (limit %d)", MAX_MATCHES);
  829. matches[found++] = at_opt;
  830. }
  831. }
  832. if (found > 1) {
  833. int shortest = 0;
  834. int length = strlen(matches[0]->key);
  835. int prefix = 1;
  836. int i;
  837. for (i = 1; i < found; i++) {
  838. int len = strlen(matches[i]->key);
  839. if (len < length) {
  840. length = len;
  841. shortest = i;
  842. }
  843. }
  844. for (i = 0; prefix && i < found; i++)
  845. if (strncmp(matches[i]->key, matches[shortest]->key, length) != 0)
  846. prefix = 0;
  847. if (prefix) {
  848. matches[0] = matches[shortest];
  849. found = 1;
  850. }
  851. else {
  852. G_asprintf(&err, _("%s: Sorry, <%s=> is ambiguous"), G_program_name(), the_key);
  853. append_error(err);
  854. for (i = 0; i < found; i++) {
  855. G_asprintf(&err, _("Option <%s=> matches"), matches[i]->key);
  856. append_error(err);
  857. }
  858. return;
  859. }
  860. }
  861. if (found)
  862. opt = matches[0];
  863. /* First, check if key has been renamed in GRASS 7 */
  864. if (found == 0) {
  865. const char *renamed_key = NULL;
  866. renamed_key = get_renamed_option(the_key);
  867. if (renamed_key) {
  868. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  869. if (strcmp(renamed_key, at_opt->key) == 0) {
  870. G_warning(_("Please update the usage of <%s>: "
  871. "option <%s> has been renamed to <%s>"),
  872. G_program_name(), the_key, renamed_key);
  873. opt = at_opt;
  874. found = 1;
  875. break;
  876. }
  877. }
  878. }
  879. }
  880. /* If there is no match, complain */
  881. if (found == 0) {
  882. G_asprintf(&err, _("%s: Sorry, <%s> is not a valid parameter"), G_program_name(), the_key);
  883. append_error(err);
  884. return;
  885. }
  886. if (getenv("GRASS_FULL_OPTION_NAMES") && strcmp(the_key, opt->key) != 0)
  887. G_warning(_("<%s> is an abbreviation for <%s>"), the_key, opt->key);
  888. /* Allocate memory where answer is stored */
  889. if (opt->count++) {
  890. if (!opt->multiple) {
  891. G_asprintf(&err, _("Option <%s> does not accept multiple answers"), opt->key);
  892. append_error(err);
  893. }
  894. opt->answer = G_realloc(opt->answer,
  895. strlen(opt->answer) + strlen(string) + 2);
  896. strcat(opt->answer, ",");
  897. strcat(opt->answer, string);
  898. }
  899. else
  900. opt->answer = G_store(string);
  901. }
  902. void check_opts(void)
  903. {
  904. struct Option *opt;
  905. int ans;
  906. if (!st->n_opts)
  907. return;
  908. opt = &st->first_option;
  909. while (opt) {
  910. /* Check answer against options if any */
  911. if (opt->answer) {
  912. if (opt->multiple == 0)
  913. check_an_opt(opt->key, opt->type,
  914. opt->options, opt->opts, &opt->answer);
  915. else {
  916. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  917. check_an_opt(opt->key, opt->type,
  918. opt->options, opt->opts, &opt->answers[ans]);
  919. }
  920. }
  921. /* Check answer against user's check subroutine if any */
  922. if (opt->checker)
  923. opt->checker(opt->answer);
  924. opt = opt->next_opt;
  925. }
  926. }
  927. void check_an_opt(const char *key, int type, const char *options,
  928. const char **opts, char **answerp)
  929. {
  930. const char *answer = *answerp;
  931. int error;
  932. char *err;
  933. int found;
  934. error = 0;
  935. err = NULL;
  936. switch (type) {
  937. case TYPE_INTEGER:
  938. error = check_int(answer, opts);
  939. break;
  940. case TYPE_DOUBLE:
  941. error = check_double(answer, opts);
  942. break;
  943. case TYPE_STRING:
  944. error = check_string(answer, opts, &found);
  945. break;
  946. }
  947. switch (error) {
  948. case 0:
  949. break;
  950. case BAD_SYNTAX:
  951. G_asprintf(&err,
  952. _("Illegal range syntax for parameter <%s>\n"
  953. "\tPresented as: %s"), key, options);
  954. append_error(err);
  955. break;
  956. case OUT_OF_RANGE:
  957. G_asprintf(&err,
  958. _("Value <%s> out of range for parameter <%s>\n"
  959. "\tLegal range: %s"), answer, key, options);
  960. append_error(err);
  961. break;
  962. case MISSING_VALUE:
  963. G_asprintf(&err,
  964. _("Missing value for parameter <%s>"),
  965. key);
  966. append_error(err);
  967. break;
  968. case AMBIGUOUS:
  969. G_asprintf(&err,
  970. _("Value <%s> ambiguous for parameter <%s>\n"
  971. "\tValid options: %s"), answer, key, options);
  972. append_error(err);
  973. break;
  974. case REPLACED:
  975. *answerp = G_store(opts[found]);
  976. error = 0;
  977. break;
  978. }
  979. }
  980. int check_int(const char *ans, const char **opts)
  981. {
  982. int d, i;
  983. /* "-" is reserved for standard input */
  984. if (strcmp(ans, "-") == 0)
  985. return 0;
  986. if (sscanf(ans, "%d", &d) != 1)
  987. return MISSING_VALUE;
  988. if (!opts)
  989. return 0;
  990. for (i = 0; opts[i]; i++) {
  991. const char *opt = opts[i];
  992. int lo, hi;
  993. if (contains(opt, '-')) {
  994. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  995. if (d >= lo && d <= hi)
  996. return 0;
  997. }
  998. else if (sscanf(opt, "-%d", &hi) == 1) {
  999. if (d <= hi)
  1000. return 0;
  1001. }
  1002. else if (sscanf(opt, "%d-", &lo) == 1) {
  1003. if (d >= lo)
  1004. return 0;
  1005. }
  1006. else
  1007. return BAD_SYNTAX;
  1008. }
  1009. else {
  1010. if (sscanf(opt, "%d", &lo) == 1) {
  1011. if (d == lo)
  1012. return 0;
  1013. }
  1014. else
  1015. return BAD_SYNTAX;
  1016. }
  1017. }
  1018. return OUT_OF_RANGE;
  1019. }
  1020. int check_double(const char *ans, const char **opts)
  1021. {
  1022. double d;
  1023. int i;
  1024. /* "-" is reserved for standard input */
  1025. if (strcmp(ans, "-") == 0)
  1026. return 0;
  1027. if (sscanf(ans, "%lf", &d) != 1)
  1028. return MISSING_VALUE;
  1029. if (!opts)
  1030. return 0;
  1031. for (i = 0; opts[i]; i++) {
  1032. const char *opt = opts[i];
  1033. double lo, hi;
  1034. if (contains(opt, '-')) {
  1035. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  1036. if (d >= lo && d <= hi)
  1037. return 0;
  1038. }
  1039. else if (sscanf(opt, "-%lf", &hi) == 1) {
  1040. if (d <= hi)
  1041. return 0;
  1042. }
  1043. else if (sscanf(opt, "%lf-", &lo) == 1) {
  1044. if (d >= lo)
  1045. return 0;
  1046. }
  1047. else
  1048. return BAD_SYNTAX;
  1049. }
  1050. else {
  1051. if (sscanf(opt, "%lf", &lo) == 1) {
  1052. if (d == lo)
  1053. return 0;
  1054. }
  1055. else
  1056. return BAD_SYNTAX;
  1057. }
  1058. }
  1059. return OUT_OF_RANGE;
  1060. }
  1061. int check_string(const char *ans, const char **opts, int *result)
  1062. {
  1063. int len = strlen(ans);
  1064. int found = 0;
  1065. int matches[MAX_MATCHES];
  1066. int i;
  1067. if (!opts)
  1068. return 0;
  1069. for (i = 0; opts[i]; i++) {
  1070. if (strcmp(ans, opts[i]) == 0)
  1071. return 0;
  1072. if (strncmp(ans, opts[i], len) == 0 || match_option(ans, opts[i])) {
  1073. if (found >= MAX_MATCHES)
  1074. G_fatal_error("too many matches (limit %d)", MAX_MATCHES);
  1075. matches[found++] = i;
  1076. }
  1077. }
  1078. if (found > 1) {
  1079. int shortest = 0;
  1080. int length = strlen(opts[matches[0]]);
  1081. int prefix = 1;
  1082. int i;
  1083. for (i = 1; i < found; i++) {
  1084. int len = strlen(opts[matches[i]]);
  1085. if (len < length) {
  1086. length = len;
  1087. shortest = i;
  1088. }
  1089. }
  1090. for (i = 0; prefix && i < found; i++)
  1091. if (strncmp(opts[matches[i]], opts[matches[shortest]], length) != 0)
  1092. prefix = 0;
  1093. if (prefix) {
  1094. matches[0] = matches[shortest];
  1095. found = 1;
  1096. }
  1097. }
  1098. if (found == 1)
  1099. *result = matches[0];
  1100. if (found > 0 && getenv("GRASS_FULL_OPTION_NAMES") && strcmp(ans, opts[matches[0]]) != 0)
  1101. G_warning(_("<%s> is an abbreviation for <%s>"), ans, opts[matches[0]]);
  1102. switch (found) {
  1103. case 0: return OUT_OF_RANGE;
  1104. case 1: return REPLACED;
  1105. default: return AMBIGUOUS;
  1106. }
  1107. }
  1108. void check_required(void)
  1109. {
  1110. struct Option *opt;
  1111. char *err;
  1112. err = NULL;
  1113. if (!st->n_opts)
  1114. return;
  1115. opt = &st->first_option;
  1116. while (opt) {
  1117. if (opt->required && !opt->answer) {
  1118. G_asprintf(&err, _("Required parameter <%s> not set:\n"
  1119. "\t(%s)"),
  1120. opt->key, (opt->label ? opt->label : opt->description));
  1121. append_error(err);
  1122. }
  1123. opt = opt->next_opt;
  1124. }
  1125. }
  1126. void split_opts(void)
  1127. {
  1128. struct Option *opt;
  1129. const char *ptr1;
  1130. const char *ptr2;
  1131. int allocated;
  1132. int ans_num;
  1133. int len;
  1134. if (!st->n_opts)
  1135. return;
  1136. opt = &st->first_option;
  1137. while (opt) {
  1138. if ( /*opt->multiple && */ opt->answer) {
  1139. /* Allocate some memory to store array of pointers */
  1140. allocated = 10;
  1141. opt->answers = G_malloc(allocated * sizeof(char *));
  1142. ans_num = 0;
  1143. ptr1 = opt->answer;
  1144. opt->answers[ans_num] = NULL;
  1145. for (;;) {
  1146. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1147. ptr2++, len++) ;
  1148. if (len > 0) { /* skip ,, */
  1149. opt->answers[ans_num] = G_malloc(len + 1);
  1150. memcpy(opt->answers[ans_num], ptr1, len);
  1151. opt->answers[ans_num][len] = 0;
  1152. ans_num++;
  1153. if (ans_num >= allocated) {
  1154. allocated += 10;
  1155. opt->answers = G_realloc(opt->answers,
  1156. allocated * sizeof(char *));
  1157. }
  1158. opt->answers[ans_num] = NULL;
  1159. }
  1160. if (*ptr2 == '\0')
  1161. break;
  1162. ptr1 = ptr2 + 1;
  1163. if (*ptr1 == '\0')
  1164. break;
  1165. }
  1166. }
  1167. opt = opt->next_opt;
  1168. }
  1169. }
  1170. void check_multiple_opts(void)
  1171. {
  1172. struct Option *opt;
  1173. const char *ptr;
  1174. int n_commas;
  1175. int n;
  1176. char *err;
  1177. if (!st->n_opts)
  1178. return;
  1179. err = NULL;
  1180. opt = &st->first_option;
  1181. while (opt) {
  1182. /* "-" is reserved from standard input/output */
  1183. if (opt->answer && strcmp(opt->answer, "-") && opt->key_desc) {
  1184. /* count commas */
  1185. n_commas = 1;
  1186. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1187. if (*ptr == ',')
  1188. n_commas++;
  1189. /* count items */
  1190. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1191. /* if not correct multiple of items */
  1192. if (n % n_commas) {
  1193. G_asprintf(&err,
  1194. _("Option <%s> must be provided in multiples of %d\n"
  1195. "\tYou provided %d item(s): %s"),
  1196. opt->key, n_commas, n, opt->answer);
  1197. append_error(err);
  1198. }
  1199. }
  1200. opt = opt->next_opt;
  1201. }
  1202. }
  1203. /* Check for all 'new' if element already exists */
  1204. int check_overwrite(void)
  1205. {
  1206. struct Option *opt;
  1207. char age[KEYLENGTH];
  1208. char element[KEYLENGTH];
  1209. char desc[KEYLENGTH];
  1210. int error = 0;
  1211. const char *overstr;
  1212. int over;
  1213. st->module_info.overwrite = 0;
  1214. if (!st->n_opts)
  1215. return (0);
  1216. over = 0;
  1217. /* Check the GRASS OVERWRITE variable */
  1218. if ((overstr = G__getenv("OVERWRITE"))) {
  1219. over = atoi(overstr);
  1220. }
  1221. /* Check the GRASS_OVERWRITE environment variable */
  1222. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1223. if (atoi(overstr))
  1224. over = 1;
  1225. }
  1226. if (st->overwrite || over) {
  1227. st->module_info.overwrite = 1;
  1228. /* Set the environment so that programs run in a script also obey --o */
  1229. putenv("GRASS_OVERWRITE=1");
  1230. /* No need to check options for existing files if overwrite is true */
  1231. return error;
  1232. }
  1233. opt = &st->first_option;
  1234. while (opt) {
  1235. if (opt->answer && opt->gisprompt) {
  1236. split_gisprompt(opt->gisprompt, age, element, desc);
  1237. if (strcmp(age, "new") == 0) {
  1238. int i;
  1239. char found;
  1240. for (i = 0; opt->answers[i]; i++) {
  1241. found = FALSE;
  1242. if (strcmp(element, "file") == 0) {
  1243. if (access(opt->answers[i], F_OK) == 0)
  1244. found = TRUE;
  1245. }
  1246. else if (strcmp(element, "mapset") != 0) {
  1247. /* TODO: also other elements should be
  1248. probably skipped */
  1249. if (G_find_file(element, opt->answers[i], G_mapset())) {
  1250. found = TRUE;
  1251. }
  1252. }
  1253. if (found) { /* found */
  1254. if (!st->overwrite && !over) {
  1255. if (G_verbose() > -1) {
  1256. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1257. fprintf(stderr,
  1258. _("ERROR: option <%s>: <%s> exists.\n"),
  1259. opt->key, opt->answers[i]);
  1260. }
  1261. else {
  1262. fprintf(stderr,
  1263. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1264. getpid(), opt->key, opt->answers[i]);
  1265. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1266. getpid());
  1267. }
  1268. }
  1269. error = 1;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. }
  1275. opt = opt->next_opt;
  1276. }
  1277. return (error);
  1278. }
  1279. void split_gisprompt(const char *gisprompt, char *age, char *element,
  1280. char *desc)
  1281. {
  1282. const char *ptr1;
  1283. char *ptr2;
  1284. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1285. if (*ptr1 == ',')
  1286. break;
  1287. *ptr2 = *ptr1;
  1288. }
  1289. *ptr2 = '\0';
  1290. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1291. if (*ptr1 == ',')
  1292. break;
  1293. *ptr2 = *ptr1;
  1294. }
  1295. *ptr2 = '\0';
  1296. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1297. if (*ptr1 == ',')
  1298. break;
  1299. *ptr2 = *ptr1;
  1300. }
  1301. *ptr2 = '\0';
  1302. }
  1303. void append_error(const char *msg)
  1304. {
  1305. st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
  1306. st->error[st->n_errors++] = G_store(msg);
  1307. }
  1308. const char *get_renamed_option(const char *key)
  1309. {
  1310. const char *pgm, *key_new;
  1311. char *pgm_key;
  1312. if (!st->renamed_options) {
  1313. /* read renamed options from file (renamed_options) */
  1314. char path[GPATH_MAX];
  1315. G_snprintf(path, GPATH_MAX, "%s/etc/renamed_options", G_gisbase());
  1316. st->renamed_options = G_read_key_value_file(path);
  1317. }
  1318. /* try to check global changes first */
  1319. key_new = G_find_key_value(key, st->renamed_options);
  1320. if (key_new)
  1321. return key_new;
  1322. /* then check module-relevant changes */
  1323. pgm = G_program_name();
  1324. pgm_key = (char *) G_malloc (strlen(pgm) + strlen(key) + 2);
  1325. G_asprintf(&pgm_key, "%s|%s", pgm, key);
  1326. key_new = G_find_key_value(pgm_key, st->renamed_options);
  1327. G_free(pgm_key);
  1328. return key_new;
  1329. }
  1330. /*!
  1331. \brief Get separator string from the option.
  1332. Calls G_fatal_error() on error. Allocated string can be later freed
  1333. by G_free().
  1334. \code
  1335. char *fs;
  1336. struct Option *opt_fs;
  1337. opt_fs = G_define_standard_option(G_OPT_F_SEP);
  1338. if (G_parser(argc, argv))
  1339. exit(EXIT_FAILURE);
  1340. fs = G_option_to_separator(opt_fs);
  1341. \endcode
  1342. \param option pointer to separator option
  1343. \return allocated string with separator
  1344. */
  1345. char* G_option_to_separator(const struct Option *option)
  1346. {
  1347. char* sep;
  1348. if (option->gisprompt == NULL ||
  1349. strcmp(option->gisprompt, "old,separator,separator") != 0)
  1350. G_fatal_error(_("%s= is not a separator option"), option->key);
  1351. if (option->answer == NULL)
  1352. G_fatal_error(_("No separator given for %s="), option->key);
  1353. if (strcmp(option->answer, "pipe") == 0)
  1354. sep = G_store("|");
  1355. else if (strcmp(option->answer, "comma") == 0)
  1356. sep = G_store(",");
  1357. else if (strcmp(option->answer, "space") == 0)
  1358. sep = G_store(" ");
  1359. else if (strcmp(option->answer, "tab") == 0 ||
  1360. strcmp(option->answer, "\\t") == 0)
  1361. sep = G_store("\t");
  1362. else if (strcmp(option->answer, "newline") == 0 ||
  1363. strcmp(option->answer, "\\n") == 0)
  1364. sep = G_store("\n");
  1365. else
  1366. sep = G_store(option->answer);
  1367. G_debug(2, "G_option_to_separator(): key = %s -> sep = '%s'",
  1368. option->key, sep);
  1369. return sep;
  1370. }
  1371. /*!
  1372. \brief Get an input/output file pointer from the option. If the file name is
  1373. omitted or '-', it returns either stdin or stdout based on the gisprompt.
  1374. Calls G_fatal_error() on error. File pointer can be later closed by
  1375. G_close_option_file().
  1376. \code
  1377. FILE *fp_input;
  1378. FILE *fp_output;
  1379. struct Option *opt_input;
  1380. struct Option *opt_output;
  1381. opt_input = G_define_standard_option(G_OPT_F_INPUT);
  1382. opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
  1383. if (G_parser(argc, argv))
  1384. exit(EXIT_FAILURE);
  1385. fp_input = G_open_option_file(opt_input);
  1386. fp_output = G_open_option_file(opt_output);
  1387. ...
  1388. G_close_option_file(fp_input);
  1389. G_close_option_file(fp_output);
  1390. \endcode
  1391. \param option pointer to a file option
  1392. \return file pointer
  1393. */
  1394. FILE *G_open_option_file(const struct Option *option)
  1395. {
  1396. int stdinout;
  1397. FILE *fp;
  1398. stdinout = !option->answer || !*(option->answer) ||
  1399. strcmp(option->answer, "-") == 0;
  1400. if (option->gisprompt == NULL)
  1401. G_fatal_error(_("%s= is not a file option"), option->key);
  1402. else if (option->multiple)
  1403. G_fatal_error(_("Opening multiple files not supported for %s="),
  1404. option->key);
  1405. else if (strcmp(option->gisprompt, "old,file,file") == 0) {
  1406. if (stdinout)
  1407. fp = stdin;
  1408. else if ((fp = fopen(option->answer, "r")) == NULL)
  1409. G_fatal_error(_("Unable to open %s file <%s>"),
  1410. option->key, option->answer);
  1411. } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
  1412. if (stdinout)
  1413. fp = stdout;
  1414. else if ((fp = fopen(option->answer, "w")) == NULL)
  1415. G_fatal_error(_("Unable to create %s file <%s>"),
  1416. option->key, option->answer);
  1417. } else
  1418. G_fatal_error(_("%s= is not a file option"), option->key);
  1419. return fp;
  1420. }
  1421. /*!
  1422. \brief Close an input/output file returned by G_open_option_file(). If the
  1423. file pointer is stdin, stdout, or stderr, nothing happens.
  1424. \param file pointer
  1425. */
  1426. void G_close_option_file(FILE *fp)
  1427. {
  1428. if (fp != stdin && fp != stdout && fp != stderr)
  1429. fclose(fp);
  1430. }