parser.c 36 KB

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