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