parser.c 35 KB

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