parser.c 36 KB

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