parser.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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 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. */
  74. #include <grass/config.h>
  75. #if defined(HAVE_LANGINFO_H)
  76. #include <langinfo.h>
  77. #endif
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <ctype.h>
  82. #include <unistd.h>
  83. #include <stdarg.h>
  84. #include <sys/types.h>
  85. #include <grass/gis.h>
  86. #include <grass/glocale.h>
  87. #include <grass/spawn.h>
  88. #define BAD_SYNTAX 1
  89. #define OUT_OF_RANGE 2
  90. #define MISSING_VALUE 3
  91. #define AMBIGUOUS 4
  92. #define REPLACED 5
  93. #define KEYLENGTH 64
  94. struct Item
  95. {
  96. struct Option *option;
  97. struct Flag *flag;
  98. struct Item *next_item;
  99. };
  100. static struct state {
  101. int no_interactive;
  102. int n_opts;
  103. int n_flags;
  104. int overwrite;
  105. int quiet;
  106. int has_required;
  107. struct GModule module_info; /* general information on the corresponding module */
  108. const char *pgm_name;
  109. struct Flag first_flag; /* First flag in a linked list */
  110. struct Flag *current_flag; /* Pointer for traversing list */
  111. struct Option first_option;
  112. struct Option *current_option;
  113. struct Item first_item;
  114. struct Item *current_item;
  115. int n_items;
  116. } state;
  117. static struct state *st = &state;
  118. static void show_options(int, const char *);
  119. static int show(const char *, int);
  120. static int set_flag(int);
  121. static int contains(const char *, int);
  122. static int is_option(const char *);
  123. static int set_option(char *);
  124. static int check_opts();
  125. static int check_an_opt(const char *, int, const char *, const char **, char **);
  126. static int check_int(const char *, const char **);
  127. static int check_double(const char *, const char **);
  128. static int check_string(const char *, const char **, int *);
  129. static int check_required(void);
  130. static void split_opts(void);
  131. static int check_multiple_opts(void);
  132. static int check_overwrite(void);
  133. static void split_gisprompt(const char *, char *, char *, char *);
  134. static void G_gui(void);
  135. static void G_usage_xml(void);
  136. static void G_usage_html(void);
  137. static void G_script(void);
  138. /*!
  139. * \brief Disables the ability of the parser to operate interactively.
  140. *
  141. * When a user calls a command with no arguments on the command line,
  142. * the parser will enter its own standardized interactive session in
  143. * which all flags and options are presented to the user for input. A
  144. * call to G_disable_interactive() disables the parser's interactive
  145. * prompting.
  146. *
  147. */
  148. void G_disable_interactive(void)
  149. {
  150. st->no_interactive = 1;
  151. }
  152. /*!
  153. * \brief Initializes a Flag struct.
  154. *
  155. * Allocates memory for the Flag structure and returns a pointer to
  156. * this memory.
  157. *
  158. * Flags are always represented by single letters. A user "turns them
  159. * on" at the command line using a minus sign followed by the
  160. * character representing the flag.
  161. *
  162. * \return Pointer to a Flag struct
  163. */
  164. struct Flag *G_define_flag(void)
  165. {
  166. struct Flag *flag;
  167. struct Item *item;
  168. /* Allocate memory if not the first flag */
  169. if (st->n_flags) {
  170. flag = (struct Flag *)G_malloc(sizeof(struct Flag));
  171. st->current_flag->next_flag = flag;
  172. }
  173. else
  174. flag = &st->first_flag;
  175. /* Zero structure */
  176. G_zero((char *)flag, sizeof(struct Flag));
  177. st->current_flag = flag;
  178. st->n_flags++;
  179. if (st->n_items) {
  180. item = (struct Item *)G_malloc(sizeof(struct Item));
  181. st->current_item->next_item = item;
  182. }
  183. else
  184. item = &st->first_item;
  185. G_zero((char *)item, sizeof(struct Item));
  186. item->flag = flag;
  187. item->option = NULL;
  188. st->current_item = item;
  189. st->n_items++;
  190. return (flag);
  191. }
  192. /*!
  193. * \brief Initializes an Option struct.
  194. *
  195. * Allocates memory for the Option structure and returns a pointer to
  196. * this memory.
  197. *
  198. * Options are provided by user on command line using the standard
  199. * format: <i>key=value</i>. Options identified as REQUIRED must be
  200. * specified by user on command line. The option string can either
  201. * specify a range of values (e.g. "10-100") or a list of acceptable
  202. * values (e.g. "red,orange,yellow"). Unless the option string is
  203. * NULL, user provided input will be evaluated agaist this string.
  204. *
  205. * \return pointer to an Option struct
  206. */
  207. struct Option *G_define_option(void)
  208. {
  209. struct Option *opt;
  210. struct Item *item;
  211. /* Allocate memory if not the first option */
  212. if (st->n_opts) {
  213. opt = (struct Option *)G_malloc(sizeof(struct Option));
  214. st->current_option->next_opt = opt;
  215. }
  216. else
  217. opt = &st->first_option;
  218. /* Zero structure */
  219. G_zero((char *)opt, sizeof(struct Option));
  220. opt->required = NO;
  221. opt->multiple = NO;
  222. opt->answer = NULL;
  223. opt->answers = NULL;
  224. opt->def = NULL;
  225. opt->checker = NULL;
  226. opt->options = NULL;
  227. opt->key_desc = NULL;
  228. opt->gisprompt = NULL;
  229. opt->label = NULL;
  230. opt->opts = NULL;
  231. opt->description = NULL;
  232. opt->descriptions = NULL;
  233. opt->guisection = NULL;
  234. st->current_option = opt;
  235. st->n_opts++;
  236. if (st->n_items) {
  237. item = (struct Item *)G_malloc(sizeof(struct Item));
  238. st->current_item->next_item = item;
  239. }
  240. else
  241. item = &st->first_item;
  242. G_zero((char *)item, sizeof(struct Item));
  243. item->option = opt;
  244. item->flag = NULL;
  245. st->current_item = item;
  246. st->n_items++;
  247. return (opt);
  248. }
  249. /*!
  250. * \brief Create standardised Option structure.
  251. *
  252. * This function will create a standardised Option structure defined
  253. * by parameter opt. A list of valid parameters can be found in gis.h.
  254. * It allocates memory for the Option structure and returns a pointer
  255. * to this memory.
  256. *
  257. * If an invalid parameter was specified a empty Option structure will
  258. * be returned (not NULL).
  259. *
  260. * - general:
  261. * - G_OPT_DB_WHERE
  262. * - G_OPT_DB_COLUMN
  263. * - G_OPT_DB_COLUMNS
  264. * - G_OPT_DB_TABLE
  265. * - G_OPT_DB_DRIVER
  266. * - G_OPT_DB_DATABASE
  267. *
  268. * - imagery:
  269. * - G_OPT_I_GROUP
  270. * - G_OPT_I_SUBGROUP
  271. *
  272. * - raster:
  273. * - G_OPT_R_INPUT
  274. * - G_OPT_R_INPUTS
  275. * - G_OPT_R_OUTPUT
  276. * - G_OPT_R_MAP
  277. * - G_OPT_R_MAPS
  278. * - G_OPT_R_BASE
  279. * - G_OPT_R_COVER
  280. * - G_OPT_R_ELEV
  281. * - G_OPT_R_ELEVS
  282. *
  283. * - raster3d:
  284. * - G_OPT_R3_INPUT
  285. * - G_OPT_R3_INPUTS
  286. * - G_OPT_R3_OUTPUT
  287. * - G_OPT_R3_MAP
  288. * - G_OPT_R3_MAPS
  289. *
  290. * - vector:
  291. * - G_OPT_V_INPUT
  292. * - G_OPT_V_INPUTS
  293. * - G_OPT_V_OUTPUT
  294. * - G_OPT_V_MAP
  295. * - G_OPT_V_MAPS
  296. * - G_OPT_V_TYPE
  297. * - G_OPT_V_FIELD
  298. * - G_OPT_V_CAT
  299. * - G_OPT_V_CATS
  300. *
  301. * \param opt type of Option struct to create
  302. *
  303. * \return pointer to an Option struct
  304. */
  305. struct Option *G_define_standard_option(int opt)
  306. {
  307. struct Option *Opt;
  308. Opt = G_define_option();
  309. switch (opt) {
  310. case G_OPT_DB_WHERE:
  311. Opt->key = "where";
  312. Opt->type = TYPE_STRING;
  313. Opt->key_desc = "sql_query";
  314. Opt->required = NO;
  315. Opt->label = _("WHERE conditions of SQL statement without 'where' keyword");
  316. Opt->description = _("Example: income < 1000 and inhab >= 10000");
  317. break;
  318. case G_OPT_DB_TABLE:
  319. Opt->key = "table";
  320. Opt->type = TYPE_STRING;
  321. Opt->key_desc = "name";
  322. Opt->required = NO;
  323. Opt->multiple = NO;
  324. Opt->description = _("Table name");
  325. Opt->gisprompt = "old_dbtable,dbtable,dbtable";
  326. break;
  327. case G_OPT_DB_DRIVER:
  328. Opt->key = "driver";
  329. Opt->type = TYPE_STRING;
  330. Opt->key_desc = "name";
  331. Opt->required = NO;
  332. Opt->multiple = NO;
  333. Opt->description = _("Driver name");
  334. Opt->gisprompt = "old_dbdriver,dbdriver,dbdriver";
  335. break;
  336. case G_OPT_DB_DATABASE:
  337. Opt->key = "database";
  338. Opt->type = TYPE_STRING;
  339. Opt->key_desc = "name";
  340. Opt->required = NO;
  341. Opt->multiple = NO;
  342. Opt->description = _("Database name");
  343. Opt->gisprompt = "old_dbname,dbname,dbname";
  344. break;
  345. case G_OPT_DB_COLUMN:
  346. Opt->key = "column";
  347. Opt->type = TYPE_STRING;
  348. Opt->key_desc = "name";
  349. Opt->required = NO;
  350. Opt->multiple = NO;
  351. Opt->description = _("Name of attribute column");
  352. Opt->gisprompt = "old_dbcolumn,dbcolumn,dbcolumn";
  353. break;
  354. case G_OPT_DB_COLUMNS:
  355. Opt->key = "columns";
  356. Opt->type = TYPE_STRING;
  357. Opt->key_desc = "name";
  358. Opt->required = NO;
  359. Opt->multiple = YES;
  360. Opt->description = _("Name of attribute column(s)");
  361. Opt->gisprompt = "old_table,table,dbcolumn";
  362. break;
  363. /* imagery group */
  364. case G_OPT_I_GROUP:
  365. Opt->key = "group";
  366. Opt->type = TYPE_STRING;
  367. Opt->key_desc = "name";
  368. Opt->required = YES;
  369. Opt->gisprompt = "old,group,group";
  370. Opt->description = _("Name of input imagery group");
  371. break;
  372. case G_OPT_I_SUBGROUP:
  373. Opt->key = "subgroup";
  374. Opt->type = TYPE_STRING;
  375. Opt->key_desc = "name";
  376. Opt->required = YES;
  377. Opt->gisprompt = "old,subgroup,subgroup";
  378. Opt->description = _("Name of input imagery subgroup");
  379. break;
  380. /* raster maps */
  381. case G_OPT_R_INPUT:
  382. Opt->key = "input";
  383. Opt->type = TYPE_STRING;
  384. Opt->key_desc = "name";
  385. Opt->required = YES;
  386. Opt->gisprompt = "old,cell,raster";
  387. Opt->description = _("Name of input raster map");
  388. break;
  389. case G_OPT_R_INPUTS:
  390. Opt->key = "input";
  391. Opt->type = TYPE_STRING;
  392. Opt->key_desc = "name";
  393. Opt->required = YES;
  394. Opt->multiple = YES;
  395. Opt->gisprompt = "old,cell,raster";
  396. Opt->description = _("Name of input raster map(s)");
  397. break;
  398. case G_OPT_R_OUTPUT:
  399. Opt->key = "output";
  400. Opt->type = TYPE_STRING;
  401. Opt->key_desc = "name";
  402. Opt->required = YES;
  403. Opt->gisprompt = "new,cell,raster";
  404. Opt->description = _("Name for output raster map");
  405. break;
  406. case G_OPT_R_MAP:
  407. Opt->key = "map";
  408. Opt->type = TYPE_STRING;
  409. Opt->key_desc = "name";
  410. Opt->required = YES;
  411. Opt->gisprompt = "old,cell,raster";
  412. Opt->description = _("Name of input raster map");
  413. break;
  414. case G_OPT_R_MAPS:
  415. Opt->key = "map";
  416. Opt->type = TYPE_STRING;
  417. Opt->key_desc = "name";
  418. Opt->required = YES;
  419. Opt->multiple = YES;
  420. Opt->gisprompt = "old,cell,raster";
  421. Opt->description = _("Name of input raster map(s)");
  422. break;
  423. case G_OPT_R_BASE:
  424. Opt->key = "base";
  425. Opt->type = TYPE_STRING;
  426. Opt->key_desc = "name";
  427. Opt->required = YES;
  428. Opt->gisprompt = "old,cell,raster";
  429. Opt->description = _("Name of base raster map");
  430. break;
  431. case G_OPT_R_COVER:
  432. Opt->key = "cover";
  433. Opt->type = TYPE_STRING;
  434. Opt->key_desc = "name";
  435. Opt->required = YES;
  436. Opt->gisprompt = "old,cell,raster";
  437. Opt->description = _("Name of cover raster map");
  438. break;
  439. case G_OPT_R_ELEV:
  440. Opt->key = "elevation";
  441. Opt->type = TYPE_STRING;
  442. Opt->key_desc = "name";
  443. Opt->required = YES;
  444. Opt->gisprompt = "old,cell,raster";
  445. Opt->description = _("Name of elevation raster map");
  446. break;
  447. case G_OPT_R_ELEVS:
  448. Opt->key = "elevation";
  449. Opt->type = TYPE_STRING;
  450. Opt->key_desc = "name";
  451. Opt->required = YES;
  452. Opt->multiple = YES;
  453. Opt->gisprompt = "old,cell,raster";
  454. Opt->description = _("Name of elevation raster map(s)");
  455. break;
  456. /*g3d maps */
  457. case G_OPT_R3_INPUT:
  458. Opt->key = "input";
  459. Opt->type = TYPE_STRING;
  460. Opt->key_desc = "name";
  461. Opt->required = YES;
  462. Opt->gisprompt = "old,grid3,3d-raster";
  463. Opt->description = _("Name of input raster3d map");
  464. break;
  465. case G_OPT_R3_INPUTS:
  466. Opt->key = "input";
  467. Opt->type = TYPE_STRING;
  468. Opt->key_desc = "name";
  469. Opt->required = YES;
  470. Opt->multiple = YES;
  471. Opt->gisprompt = "old,grid3,3d-raster";
  472. Opt->description = _("Name of input raster3d map(s)");
  473. break;
  474. case G_OPT_R3_OUTPUT:
  475. Opt->key = "output";
  476. Opt->type = TYPE_STRING;
  477. Opt->key_desc = "name";
  478. Opt->required = YES;
  479. Opt->gisprompt = "new,grid3,3d-raster";
  480. Opt->description = _("Name for output raster3d map");
  481. break;
  482. case G_OPT_R3_MAP:
  483. Opt->key = "map";
  484. Opt->type = TYPE_STRING;
  485. Opt->key_desc = "name";
  486. Opt->required = YES;
  487. Opt->gisprompt = "old,grid3,3d-raster";
  488. Opt->description = _("Name of input raster3d map");
  489. break;
  490. case G_OPT_R3_MAPS:
  491. Opt->key = "map";
  492. Opt->type = TYPE_STRING;
  493. Opt->key_desc = "name";
  494. Opt->required = YES;
  495. Opt->multiple = YES;
  496. Opt->gisprompt = "old,grid3,3d-raster";
  497. Opt->description = _("Name of input raster3d map(s)");
  498. break;
  499. /*vector maps */
  500. case G_OPT_V_INPUT:
  501. Opt->key = "input";
  502. Opt->type = TYPE_STRING;
  503. Opt->key_desc = "name";
  504. Opt->required = YES;
  505. Opt->gisprompt = "old,vector,vector";
  506. Opt->description = _("Name of input vector map");
  507. break;
  508. case G_OPT_V_INPUTS:
  509. Opt->key = "input";
  510. Opt->type = TYPE_STRING;
  511. Opt->key_desc = "name";
  512. Opt->required = YES;
  513. Opt->multiple = YES;
  514. Opt->gisprompt = "old,vector,vector";
  515. Opt->description = _("Name of input vector map(s)");
  516. break;
  517. case G_OPT_V_OUTPUT:
  518. Opt->key = "output";
  519. Opt->type = TYPE_STRING;
  520. Opt->key_desc = "name";
  521. Opt->required = YES;
  522. Opt->gisprompt = "new,vector,vector";
  523. Opt->description = _("Name for output vector map");
  524. break;
  525. case G_OPT_V_MAP:
  526. Opt->key = "map";
  527. Opt->type = TYPE_STRING;
  528. Opt->key_desc = "name";
  529. Opt->required = YES;
  530. Opt->gisprompt = "old,vector,vector";
  531. Opt->description = _("Name of input vector map");
  532. break;
  533. case G_OPT_V_MAPS:
  534. Opt->key = "map";
  535. Opt->type = TYPE_STRING;
  536. Opt->key_desc = "name";
  537. Opt->required = YES;
  538. Opt->multiple = YES;
  539. Opt->gisprompt = "old,vector,vector";
  540. Opt->description = _("Name of input vector map(s)");
  541. break;
  542. case G_OPT_V_TYPE:
  543. Opt->key = "type";
  544. Opt->type = TYPE_STRING;
  545. Opt->required = NO;
  546. Opt->multiple = YES;
  547. Opt->answer = "point,line,boundary,centroid,area";
  548. Opt->options = "point,line,boundary,centroid,area";
  549. Opt->description = _("Feature type");
  550. break;
  551. case G_OPT_V_FIELD:
  552. Opt->key = "layer";
  553. Opt->type = TYPE_INTEGER;
  554. Opt->required = NO;
  555. Opt->answer = "1";
  556. Opt->label = _("Layer number");
  557. Opt->description =
  558. _("A single vector map can be connected to multiple database "
  559. "tables. This number determines which table to use.");
  560. Opt->gisprompt = "old_layer,layer,layer";
  561. break;
  562. case G_OPT_V_CAT:
  563. Opt->key = "cat";
  564. Opt->type = TYPE_INTEGER;
  565. Opt->required = NO;
  566. Opt->description = _("Category value");
  567. break;
  568. case G_OPT_V_CATS:
  569. Opt->key = "cats";
  570. Opt->type = TYPE_STRING;
  571. Opt->key_desc = "range";
  572. Opt->required = NO;
  573. Opt->label = _("Category values");
  574. Opt->description = _("Example: 1,3,7-9,13");
  575. break;
  576. case G_OPT_V_ID:
  577. Opt->key = "id";
  578. Opt->type = TYPE_INTEGER;
  579. Opt->required = NO;
  580. Opt->description = _("Feature id");
  581. break;
  582. case G_OPT_V_IDS:
  583. Opt->key = "ids";
  584. Opt->type = TYPE_STRING;
  585. Opt->key_desc = "range";
  586. Opt->required = NO;
  587. Opt->label = _("Feature ids");
  588. Opt->description = _("Example: 1,3,7-9,13");
  589. break;
  590. /* files */
  591. case G_OPT_F_INPUT:
  592. Opt->key = "input";
  593. Opt->type = TYPE_STRING;
  594. Opt->key_desc = "name";
  595. Opt->required = YES;
  596. Opt->gisprompt = "old_file,file,input";
  597. Opt->description = _("Name of input file");
  598. break;
  599. case G_OPT_F_OUTPUT:
  600. Opt->key = "output";
  601. Opt->type = TYPE_STRING;
  602. Opt->key_desc = "name";
  603. Opt->required = YES;
  604. Opt->gisprompt = "new_file,file,output";
  605. Opt->description = _("Name for output file");
  606. break;
  607. case G_OPT_F_SEP:
  608. Opt->key = "fs";
  609. Opt->type = TYPE_STRING;
  610. Opt->key_desc = "character";
  611. Opt->required = NO;
  612. Opt->answer = "|";
  613. Opt->description = _("Field separator");
  614. break;
  615. /* colors */
  616. case G_OPT_C_FG:
  617. Opt->key = "color";
  618. Opt->type = TYPE_STRING;
  619. Opt->key_desc = "name";
  620. Opt->required = NO;
  621. Opt->answer = DEFAULT_FG_COLOR;
  622. Opt->gisprompt = "old_color,color,color";
  623. Opt->label = _("Color");
  624. Opt->description = _("Either a standard color name or R:G:B triplet");
  625. break;
  626. case G_OPT_C_BG:
  627. Opt->key = "bgcolor";
  628. Opt->type = TYPE_STRING;
  629. Opt->key_desc = "name";
  630. Opt->required = NO;
  631. Opt->answer = DEFAULT_BG_COLOR;
  632. Opt->gisprompt = "old_color,color,color_none";
  633. Opt->label = _("Background color");
  634. Opt->description =
  635. _("Either a standard GRASS color, R:G:B triplet, or \"none\"");
  636. break;
  637. }
  638. return (Opt);
  639. }
  640. /*!
  641. * \brief Initializes a new module.
  642. *
  643. * \return pointer to a GModule struct
  644. */
  645. struct GModule *G_define_module(void)
  646. {
  647. struct GModule *module;
  648. /* Allocate memory */
  649. module = &st->module_info;
  650. /* Zero structure */
  651. G_zero((char *)module, sizeof(struct GModule));
  652. return (module);
  653. }
  654. /*!
  655. * \brief Parse command line.
  656. *
  657. * The command line parameters <i>argv</i> and the number of
  658. * parameters <i>argc</i> from the main() routine are passed directly
  659. * to G_parser(). G_parser() accepts the command line input entered by
  660. * the user, and parses this input according to the input options
  661. * and/or flags that were defined by the programmer.
  662. *
  663. * <b>Note:</b> The only functions which can legitimately be called
  664. * before G_parser() are:
  665. *
  666. * - G_gisinit()
  667. * - G_no_gisinit()
  668. * - G_define_module()
  669. * - G_define_flag()
  670. * - G_define_option()
  671. * - G_define_standard_option()
  672. * - G_disable_interactive()
  673. *
  674. * The usual order a module calls functions is:
  675. *
  676. * # G_gisinit()
  677. * # G_define_module()
  678. * # G_define_flag()
  679. * # G_define_option()
  680. * # G_parser()
  681. *
  682. * \param argc number of arguments
  683. * \param argv argument list
  684. *
  685. * \return 0 on success
  686. * \return -1 on error and calls G_usage()
  687. */
  688. int G_parser(int argc, char **argv)
  689. {
  690. int need_first_opt;
  691. int opt_checked = 0;
  692. int error;
  693. char *ptr, *tmp_name;
  694. int i;
  695. struct Option *opt;
  696. char force_gui = FALSE;
  697. error = 0;
  698. need_first_opt = 1;
  699. i = strlen(tmp_name = G_store(argv[0]));
  700. while (--i >= 0) {
  701. if (G_is_dirsep(tmp_name[i])) {
  702. tmp_name += i + 1;
  703. break;
  704. }
  705. }
  706. G_basename(tmp_name, "exe");
  707. st->pgm_name = tmp_name;
  708. /* Stash default answers */
  709. opt = &st->first_option;
  710. while (opt != NULL) {
  711. if (opt->required)
  712. st->has_required = 1;
  713. /* Parse options */
  714. if (opt->options) {
  715. int cnt = 0;
  716. char **tokens, delm[2];
  717. delm[0] = ',';
  718. delm[1] = '\0';
  719. tokens = G_tokenize(opt->options, delm);
  720. i = 0;
  721. while (tokens[i]) {
  722. cnt++;
  723. i++;
  724. }
  725. opt->opts =
  726. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  727. i = 0;
  728. while (tokens[i]) {
  729. opt->opts[i] = G_store(tokens[i]);
  730. i++;
  731. }
  732. G_free_tokens(tokens);
  733. if (opt->descriptions) {
  734. delm[0] = ';';
  735. opt->descs =
  736. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  737. tokens = G_tokenize(opt->descriptions, delm);
  738. i = 0;
  739. while (tokens[i]) {
  740. int j, found;
  741. if (!tokens[i + 1])
  742. break;
  743. j = 0;
  744. found = 0;
  745. while (opt->opts[j]) {
  746. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  747. found = 1;
  748. break;
  749. }
  750. j++;
  751. }
  752. if (!found) {
  753. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  754. tokens[i], opt->key);
  755. }
  756. else {
  757. opt->descs[j] = G_store(tokens[i + 1]);
  758. }
  759. i += 2;
  760. }
  761. G_free_tokens(tokens);
  762. }
  763. }
  764. /* Copy answer */
  765. if (opt->multiple && opt->answers && opt->answers[0]) {
  766. opt->answer = (char *)G_malloc(strlen(opt->answers[0]) + 1);
  767. strcpy(opt->answer, opt->answers[0]);
  768. for (i = 1; opt->answers[i]; i++) {
  769. opt->answer = (char *)G_realloc(opt->answer,
  770. strlen(opt->answer) +
  771. strlen(opt->answers[i]) + 2);
  772. strcat(opt->answer, ",");
  773. strcat(opt->answer, opt->answers[i]);
  774. }
  775. }
  776. opt->def = opt->answer;
  777. opt = opt->next_opt;
  778. }
  779. /* If there are NO arguments, go interactive */
  780. if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
  781. G_gui();
  782. return -1;
  783. }
  784. else if (argc < 2 && st->has_required && isatty(0)) {
  785. G_usage();
  786. return -1;
  787. }
  788. else if (argc >= 2) {
  789. /* If first arg is "help" give a usage/syntax message */
  790. if (strcmp(argv[1], "help") == 0 ||
  791. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  792. G_usage();
  793. exit(EXIT_SUCCESS);
  794. }
  795. /* If first arg is "--interface-description" then print out
  796. * a xml description of the task */
  797. if (strcmp(argv[1], "--interface-description") == 0) {
  798. G_usage_xml();
  799. exit(EXIT_SUCCESS);
  800. }
  801. /* If first arg is "--html-description" then print out
  802. * a html description of the task */
  803. if (strcmp(argv[1], "--html-description") == 0) {
  804. G_usage_html();
  805. exit(EXIT_SUCCESS);
  806. }
  807. /* If first arg is "--script" then then generate
  808. * g.parser boilerplate */
  809. if (strcmp(argv[1], "--script") == 0) {
  810. G_script();
  811. exit(EXIT_SUCCESS);
  812. }
  813. /* Loop thru all command line arguments */
  814. while (--argc) {
  815. ptr = *(++argv);
  816. /* Overwrite option */
  817. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  818. st->overwrite = 1;
  819. }
  820. /* Verbose option */
  821. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  822. char buff[32];
  823. /* print everything: max verbosity level */
  824. st->module_info.verbose = G_verbose_max();
  825. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  826. putenv(G_store(buff));
  827. if (st->quiet == 1) {
  828. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  829. }
  830. st->quiet = -1;
  831. }
  832. /* Quiet option */
  833. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  834. char buff[32];
  835. /* print nothing, but errors and warnings */
  836. st->module_info.verbose = G_verbose_min();
  837. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  838. putenv(G_store(buff));
  839. if (st->quiet == -1) {
  840. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  841. }
  842. st->quiet = 1; /* for passing to gui init */
  843. }
  844. /* Force gui to come up */
  845. else if (strcmp(ptr, "--ui") == 0) {
  846. force_gui = TRUE;
  847. }
  848. /* If we see a flag */
  849. else if (*ptr == '-') {
  850. while (*(++ptr))
  851. error += set_flag(*ptr);
  852. }
  853. /* If we see standard option format (option=val) */
  854. else if (is_option(ptr)) {
  855. error += set_option(ptr);
  856. need_first_opt = 0;
  857. }
  858. /* If we see the first option with no equal sign */
  859. else if (need_first_opt && st->n_opts) {
  860. st->first_option.answer = G_store(ptr);
  861. need_first_opt = 0;
  862. }
  863. /* If we see the non valid argument (no "=", just argument) */
  864. else if (contains(ptr, '=') == 0) {
  865. fprintf(stderr, _("Sorry <%s> is not a valid option\n"), ptr);
  866. error = 1;
  867. }
  868. }
  869. }
  870. /* Run the gui if it was specifically requested */
  871. if (force_gui) {
  872. G_gui();
  873. return -1;
  874. }
  875. /* Split options where multiple answers are OK */
  876. split_opts();
  877. /* Check multiple options */
  878. error += check_multiple_opts();
  879. /* Check answers against options and check subroutines */
  880. if (!opt_checked)
  881. error += check_opts();
  882. /* Make sure all required options are set */
  883. error += check_required();
  884. if (error) {
  885. if (G_verbose() > G_verbose_min())
  886. G_usage();
  887. return -1;
  888. }
  889. if (check_overwrite())
  890. return -1;
  891. return (0);
  892. }
  893. static int uses_new_gisprompt(void)
  894. {
  895. struct Option *opt;
  896. char age[KEYLENGTH];
  897. char element[KEYLENGTH];
  898. char desc[KEYLENGTH];
  899. if (st->module_info.overwrite)
  900. return 1;
  901. /* figure out if any of the options use a "new" gisprompt */
  902. /* This is to see if we should spit out the --o flag */
  903. if (st->n_opts) {
  904. opt = &st->first_option;
  905. while (opt != NULL) {
  906. if (opt->gisprompt) {
  907. split_gisprompt(opt->gisprompt, age, element, desc);
  908. if (strcmp(age, "new") == 0)
  909. return 1;
  910. }
  911. opt = opt->next_opt;
  912. }
  913. }
  914. return 0;
  915. }
  916. /*!
  917. * \brief Command line help/usage message.
  918. *
  919. * Calls to G_usage() allow the programmer to print the usage
  920. * message at any time. This will explain the allowed and required
  921. * command line input to the user. This description is given according
  922. * to the programmer's definitions for options and flags. This function
  923. * becomes useful when the user enters options and/or flags on the
  924. * command line that are syntactically valid to the parser, but
  925. * functionally invalid for the command (e.g. an invalid file name.)
  926. *
  927. * For example, the parser logic doesn't directly support grouping
  928. * options. If two options be specified together or not at all, the
  929. * parser must be told that these options are not required and the
  930. * programmer must check that if one is specified the other must be as
  931. * well. If this additional check fails, then G_parser() will succeed,
  932. * but the programmer can then call G_usage() to print the standard
  933. * usage message and print additional information about how the two
  934. * options work together.
  935. */
  936. void G_usage(void)
  937. {
  938. struct Option *opt;
  939. struct Flag *flag;
  940. char item[256];
  941. const char *key_desc;
  942. int maxlen;
  943. int len, n;
  944. int new_prompt = 0;
  945. new_prompt = uses_new_gisprompt();
  946. if (!st->pgm_name) /* v.dave && r.michael */
  947. st->pgm_name = G_program_name();
  948. if (!st->pgm_name)
  949. st->pgm_name = "??";
  950. if (st->module_info.label || st->module_info.description) {
  951. fprintf(stderr, _("\nDescription:\n"));
  952. if (st->module_info.label)
  953. fprintf(stderr, " %s\n", st->module_info.label);
  954. if (st->module_info.description)
  955. fprintf(stderr, " %s\n", st->module_info.description);
  956. }
  957. if (st->module_info.keywords) {
  958. fprintf(stderr, _("\nKeywords:\n"));
  959. fprintf(stderr, " %s\n", st->module_info.keywords);
  960. }
  961. fprintf(stderr, _("\nUsage:\n "));
  962. len = show(st->pgm_name, 1);
  963. /* Print flags */
  964. if (st->n_flags) {
  965. item[0] = ' ';
  966. item[1] = '[';
  967. item[2] = '-';
  968. flag = &st->first_flag;
  969. for (n = 3; flag != NULL; n++, flag = flag->next_flag)
  970. item[n] = flag->key;
  971. item[n++] = ']';
  972. item[n] = 0;
  973. len = show(item, len);
  974. }
  975. maxlen = 0;
  976. if (st->n_opts) {
  977. opt = &st->first_option;
  978. while (opt != NULL) {
  979. if (opt->key_desc != NULL)
  980. key_desc = opt->key_desc;
  981. else if (opt->type == TYPE_STRING)
  982. key_desc = "string";
  983. else
  984. key_desc = "value";
  985. n = strlen(opt->key);
  986. if (n > maxlen)
  987. maxlen = n;
  988. strcpy(item, " ");
  989. if (!opt->required)
  990. strcat(item, "[");
  991. strcat(item, opt->key);
  992. strcat(item, "=");
  993. strcat(item, key_desc);
  994. if (opt->multiple) {
  995. strcat(item, "[,");
  996. strcat(item, key_desc);
  997. strcat(item, ",...]");
  998. }
  999. if (!opt->required)
  1000. strcat(item, "]");
  1001. len = show(item, len);
  1002. opt = opt->next_opt;
  1003. }
  1004. }
  1005. if (new_prompt) {
  1006. strcpy(item, " [--overwrite]");
  1007. len = show(item, len);
  1008. }
  1009. strcpy(item, " [--verbose]");
  1010. len = show(item, len);
  1011. strcpy(item, " [--quiet]");
  1012. len = show(item, len);
  1013. fprintf(stderr, "\n");
  1014. /* Print help info for flags */
  1015. fprintf(stderr, _("\nFlags:\n"));
  1016. if (st->n_flags) {
  1017. flag = &st->first_flag;
  1018. while (flag != NULL) {
  1019. fprintf(stderr, " -%c ", flag->key);
  1020. if (flag->label) {
  1021. fprintf(stderr, "%s\n", flag->label);
  1022. if (flag->description)
  1023. fprintf(stderr, " %s\n", flag->description);
  1024. }
  1025. else if (flag->description) {
  1026. fprintf(stderr, "%s\n", flag->description);
  1027. }
  1028. flag = flag->next_flag;
  1029. }
  1030. }
  1031. if (new_prompt)
  1032. fprintf(stderr, " --o %s\n",
  1033. _("Allow output files to overwrite existing files"));
  1034. fprintf(stderr, " --v %s\n", _("Verbose module output"));
  1035. fprintf(stderr, " --q %s\n", _("Quiet module output"));
  1036. /* Print help info for options */
  1037. if (st->n_opts) {
  1038. fprintf(stderr, _("\nParameters:\n"));
  1039. opt = &st->first_option;
  1040. while (opt != NULL) {
  1041. fprintf(stderr, " %*s ", maxlen, opt->key);
  1042. if (opt->label) {
  1043. fprintf(stderr, "%s\n", opt->label);
  1044. if (opt->description) {
  1045. fprintf(stderr, " %*s %s\n",
  1046. maxlen, " ", opt->description);
  1047. }
  1048. }
  1049. else if (opt->description) {
  1050. fprintf(stderr, "%s\n", opt->description);
  1051. }
  1052. if (opt->options)
  1053. show_options(maxlen, opt->options);
  1054. /*
  1055. fprintf (stderr, " %*s options: %s\n", maxlen, " ",
  1056. _(opt->options)) ;
  1057. */
  1058. if (opt->def)
  1059. fprintf(stderr, _(" %*s default: %s\n"), maxlen, " ",
  1060. opt->def);
  1061. if (opt->descs) {
  1062. int i = 0;
  1063. while (opt->opts[i]) {
  1064. if (opt->descs[i])
  1065. fprintf(stderr, " %*s %s: %s\n",
  1066. maxlen, " ", opt->opts[i], opt->descs[i]);
  1067. i++;
  1068. }
  1069. }
  1070. opt = opt->next_opt;
  1071. }
  1072. }
  1073. }
  1074. /*!
  1075. * \brief Formats text for XML.
  1076. *
  1077. * \param[in,out] fp file to write to
  1078. * \param str string to write
  1079. */
  1080. static void print_escaped_for_xml(FILE * fp, const char *str)
  1081. {
  1082. for (; *str; str++) {
  1083. switch (*str) {
  1084. case '&':
  1085. fputs("&amp;", fp);
  1086. break;
  1087. case '<':
  1088. fputs("&lt;", fp);
  1089. break;
  1090. case '>':
  1091. fputs("&gt;", fp);
  1092. break;
  1093. default:
  1094. fputc(*str, fp);
  1095. }
  1096. }
  1097. }
  1098. /*!
  1099. * \brief Format text for HTML output
  1100. */
  1101. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  1102. static void print_escaped_for_html(FILE * f, const char *str)
  1103. {
  1104. const char *s;
  1105. for (s = str; *s; s++) {
  1106. switch (*s) {
  1107. do_escape('&', "&amp;");
  1108. do_escape('<', "&lt;");
  1109. do_escape('>', "&gt;");
  1110. do_escape('\n', "<br>");
  1111. default:
  1112. fputc(*s, f);
  1113. }
  1114. }
  1115. }
  1116. #undef do_escape
  1117. /*!
  1118. \brief Print module usage description in XML format.
  1119. */
  1120. static void G_usage_xml(void)
  1121. {
  1122. struct Option *opt;
  1123. struct Flag *flag;
  1124. char *type;
  1125. char *s, *top;
  1126. int i;
  1127. char *encoding;
  1128. int new_prompt = 0;
  1129. new_prompt = uses_new_gisprompt();
  1130. /* gettext converts strings to encoding returned by nl_langinfo(CODESET) */
  1131. #if defined(HAVE_LANGINFO_H)
  1132. encoding = nl_langinfo(CODESET);
  1133. if (!encoding || strlen(encoding) == 0) {
  1134. encoding = "UTF-8";
  1135. }
  1136. #else
  1137. encoding = "UTF-8";
  1138. #endif
  1139. if (!st->pgm_name) /* v.dave && r.michael */
  1140. st->pgm_name = G_program_name();
  1141. if (!st->pgm_name)
  1142. st->pgm_name = "??";
  1143. fprintf(stdout, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
  1144. fprintf(stdout, "<!DOCTYPE task SYSTEM \"grass-interface.dtd\">\n");
  1145. fprintf(stdout, "<task name=\"%s\">\n", st->pgm_name);
  1146. if (st->module_info.label) {
  1147. fprintf(stdout, "\t<label>\n\t\t");
  1148. print_escaped_for_xml(stdout, st->module_info.label);
  1149. fprintf(stdout, "\n\t</label>\n");
  1150. }
  1151. if (st->module_info.description) {
  1152. fprintf(stdout, "\t<description>\n\t\t");
  1153. print_escaped_for_xml(stdout, st->module_info.description);
  1154. fprintf(stdout, "\n\t</description>\n");
  1155. }
  1156. if (st->module_info.keywords) {
  1157. fprintf(stdout, "\t<keywords>\n\t\t");
  1158. print_escaped_for_xml(stdout, st->module_info.keywords);
  1159. fprintf(stdout, "\n\t</keywords>\n");
  1160. }
  1161. /***** Don't use parameter-groups for now. We'll reimplement this later
  1162. ***** when we have a concept of several mutually exclusive option
  1163. ***** groups
  1164. if (st->n_opts || st->n_flags)
  1165. fprintf(stdout, "\t<parameter-group>\n");
  1166. *****
  1167. *****
  1168. *****/
  1169. if (st->n_opts) {
  1170. opt = &st->first_option;
  1171. while (opt != NULL) {
  1172. /* TODO: make this a enumeration type? */
  1173. switch (opt->type) {
  1174. case TYPE_INTEGER:
  1175. type = "integer";
  1176. break;
  1177. case TYPE_DOUBLE:
  1178. type = "float";
  1179. break;
  1180. case TYPE_STRING:
  1181. type = "string";
  1182. break;
  1183. default:
  1184. type = "string";
  1185. break;
  1186. }
  1187. fprintf(stdout, "\t<parameter "
  1188. "name=\"%s\" "
  1189. "type=\"%s\" "
  1190. "required=\"%s\" "
  1191. "multiple=\"%s\">\n",
  1192. opt->key,
  1193. type,
  1194. opt->required == YES ? "yes" : "no",
  1195. opt->multiple == YES ? "yes" : "no");
  1196. if (opt->label) {
  1197. fprintf(stdout, "\t\t<label>\n\t\t\t");
  1198. print_escaped_for_xml(stdout, opt->label);
  1199. fprintf(stdout, "\n\t\t</label>\n");
  1200. }
  1201. if (opt->description) {
  1202. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1203. print_escaped_for_xml(stdout, opt->description);
  1204. fprintf(stdout, "\n\t\t</description>\n");
  1205. }
  1206. if (opt->key_desc) {
  1207. fprintf(stdout, "\t\t<keydesc>\n");
  1208. top = G_calloc(strlen(opt->key_desc) + 1, 1);
  1209. strcpy(top, opt->key_desc);
  1210. s = strtok(top, ",");
  1211. for (i = 1; s != NULL; i++) {
  1212. fprintf(stdout, "\t\t\t<item order=\"%d\">", i);
  1213. print_escaped_for_xml(stdout, s);
  1214. fprintf(stdout, "</item>\n");
  1215. s = strtok(NULL, ",");
  1216. }
  1217. fprintf(stdout, "\t\t</keydesc>\n");
  1218. G_free(top);
  1219. }
  1220. if (opt->gisprompt) {
  1221. const char *atts[] = { "age", "element", "prompt", NULL };
  1222. top = G_calloc(strlen(opt->gisprompt) + 1, 1);
  1223. strcpy(top, opt->gisprompt);
  1224. s = strtok(top, ",");
  1225. fprintf(stdout, "\t\t<gisprompt ");
  1226. for (i = 0; s != NULL && atts[i] != NULL; i++) {
  1227. fprintf(stdout, "%s=\"%s\" ", atts[i], s);
  1228. s = strtok(NULL, ",");
  1229. }
  1230. fprintf(stdout, "/>\n");
  1231. G_free(top);
  1232. }
  1233. if (opt->def) {
  1234. fprintf(stdout, "\t\t<default>\n\t\t\t");
  1235. print_escaped_for_xml(stdout, opt->def);
  1236. fprintf(stdout, "\n\t\t</default>\n");
  1237. }
  1238. if (opt->options) {
  1239. /* TODO:
  1240. * add something like
  1241. * <range min="xxx" max="xxx"/>
  1242. * to <values> */
  1243. i = 0;
  1244. fprintf(stdout, "\t\t<values>\n");
  1245. while (opt->opts[i]) {
  1246. fprintf(stdout, "\t\t\t<value>\n");
  1247. fprintf(stdout, "\t\t\t\t<name>");
  1248. print_escaped_for_xml(stdout, opt->opts[i]);
  1249. fprintf(stdout, "</name>\n");
  1250. if (opt->descs && opt->opts[i] && opt->descs[i]) {
  1251. fprintf(stdout, "\t\t\t\t<description>");
  1252. print_escaped_for_xml(stdout, opt->descs[i]);
  1253. fprintf(stdout, "</description>\n");
  1254. }
  1255. fprintf(stdout, "\t\t\t</value>\n");
  1256. i++;
  1257. }
  1258. fprintf(stdout, "\t\t</values>\n");
  1259. }
  1260. if (opt->guisection) {
  1261. fprintf(stdout, "\t\t<guisection>\n\t\t\t");
  1262. print_escaped_for_xml(stdout, opt->guisection);
  1263. fprintf(stdout, "\n\t\t</guisection>\n");
  1264. }
  1265. /* TODO:
  1266. * - key_desc?
  1267. * - there surely are some more. which ones?
  1268. */
  1269. opt = opt->next_opt;
  1270. fprintf(stdout, "\t</parameter>\n");
  1271. }
  1272. }
  1273. if (st->n_flags) {
  1274. flag = &st->first_flag;
  1275. while (flag != NULL) {
  1276. fprintf(stdout, "\t<flag name=\"%c\">\n", flag->key);
  1277. if (flag->label) {
  1278. fprintf(stdout, "\t\t<label>\n\t\t\t");
  1279. print_escaped_for_xml(stdout, flag->label);
  1280. fprintf(stdout, "\n\t\t</label>\n");
  1281. }
  1282. if (flag->description) {
  1283. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1284. print_escaped_for_xml(stdout, flag->description);
  1285. fprintf(stdout, "\n\t\t</description>\n");
  1286. }
  1287. if (flag->guisection) {
  1288. fprintf(stdout, " \t\t<guisection>\n\t\t\t");
  1289. print_escaped_for_xml(stdout, flag->guisection);
  1290. fprintf(stdout, "\n\t\t</guisection>\n");
  1291. }
  1292. flag = flag->next_flag;
  1293. fprintf(stdout, "\t</flag>\n");
  1294. }
  1295. }
  1296. /***** Don't use parameter-groups for now. We'll reimplement this later
  1297. ***** when we have a concept of several mutually exclusive option
  1298. ***** groups
  1299. if (st->n_opts || st->n_flags)
  1300. fprintf(stdout, "\t</parameter-group>\n");
  1301. *****
  1302. *****
  1303. *****/
  1304. if (new_prompt) {
  1305. /* overwrite */
  1306. fprintf(stdout, "\t<flag name=\"%s\">\n", "overwrite");
  1307. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1308. print_escaped_for_xml(stdout,
  1309. "Allow output files to overwrite existing files");
  1310. fprintf(stdout, "\n\t\t</description>\n");
  1311. fprintf(stdout, "\t</flag>\n");
  1312. }
  1313. /* verbose */
  1314. fprintf(stdout, "\t<flag name=\"%s\">\n", "verbose");
  1315. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1316. print_escaped_for_xml(stdout, "Verbose module output");
  1317. fprintf(stdout, "\n\t\t</description>\n");
  1318. fprintf(stdout, "\t</flag>\n");
  1319. /* quiet */
  1320. fprintf(stdout, "\t<flag name=\"%s\">\n", "quiet");
  1321. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1322. print_escaped_for_xml(stdout, "Quiet module output");
  1323. fprintf(stdout, "\n\t\t</description>\n");
  1324. fprintf(stdout, "\t</flag>\n");
  1325. fprintf(stdout, "</task>\n");
  1326. }
  1327. /*!
  1328. \brief Print module usage description in HTML format.
  1329. */
  1330. static void G_usage_html(void)
  1331. {
  1332. struct Option *opt;
  1333. struct Flag *flag;
  1334. const char *type;
  1335. int new_prompt = 0;
  1336. new_prompt = uses_new_gisprompt();
  1337. if (!st->pgm_name) /* v.dave && r.michael */
  1338. st->pgm_name = G_program_name();
  1339. if (!st->pgm_name)
  1340. st->pgm_name = "??";
  1341. fprintf(stdout,
  1342. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
  1343. fprintf(stdout, "<html>\n<head>\n");
  1344. fprintf(stdout, "<title>GRASS GIS manual: %s</title>\n", st->pgm_name);
  1345. fprintf(stdout,
  1346. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  1347. fprintf(stdout,
  1348. "<link rel=\"stylesheet\" href=\"grassdocs.css\" type=\"text/css\">\n");
  1349. fprintf(stdout, "</head>\n");
  1350. fprintf(stdout, "<body bgcolor=\"white\">\n\n");
  1351. fprintf(stdout,
  1352. "<img src=\"grass_logo.png\" alt=\"GRASS logo\"><hr align=center size=6 noshade>\n\n");
  1353. fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
  1354. fprintf(stdout, "<em><b>%s</b></em> ", st->pgm_name);
  1355. if (st->module_info.label || st->module_info.description)
  1356. fprintf(stdout, " - ");
  1357. if (st->module_info.label)
  1358. fprintf(stdout, "%s<BR>\n", st->module_info.label);
  1359. if (st->module_info.description)
  1360. fprintf(stdout, "%s\n", st->module_info.description);
  1361. fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
  1362. if (st->module_info.keywords) {
  1363. fprintf(stdout, "%s", st->module_info.keywords);
  1364. fprintf(stdout, "\n");
  1365. }
  1366. fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
  1367. fprintf(stdout, "<b>%s</b><br>\n", st->pgm_name);
  1368. fprintf(stdout, "<b>%s help</b><br>\n", st->pgm_name);
  1369. fprintf(stdout, "<b>%s</b>", st->pgm_name);
  1370. /* print short version first */
  1371. if (st->n_flags) {
  1372. flag = &st->first_flag;
  1373. fprintf(stdout, " [-<b>");
  1374. while (flag != NULL) {
  1375. fprintf(stdout, "%c", flag->key);
  1376. flag = flag->next_flag;
  1377. }
  1378. fprintf(stdout, "</b>] ");
  1379. }
  1380. else
  1381. fprintf(stdout, " ");
  1382. if (st->n_opts) {
  1383. opt = &st->first_option;
  1384. while (opt != NULL) {
  1385. if (opt->key_desc != NULL)
  1386. type = opt->key_desc;
  1387. else
  1388. switch (opt->type) {
  1389. case TYPE_INTEGER:
  1390. type = "integer";
  1391. break;
  1392. case TYPE_DOUBLE:
  1393. type = "float";
  1394. break;
  1395. case TYPE_STRING:
  1396. type = "string";
  1397. break;
  1398. default:
  1399. type = "string";
  1400. break;
  1401. }
  1402. if (!opt->required)
  1403. fprintf(stdout, " [");
  1404. fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
  1405. if (opt->multiple) {
  1406. fprintf(stdout, "[,<i>%s</i>,...]", type);
  1407. }
  1408. if (!opt->required)
  1409. fprintf(stdout, "] ");
  1410. opt = opt->next_opt;
  1411. fprintf(stdout, " ");
  1412. }
  1413. }
  1414. if (new_prompt)
  1415. fprintf(stdout, " [--<b>overwrite</b>] ");
  1416. fprintf(stdout, " [--<b>verbose</b>] ");
  1417. fprintf(stdout, " [--<b>quiet</b>] ");
  1418. fprintf(stdout, "\n");
  1419. /* now long version */
  1420. fprintf(stdout, "\n");
  1421. if (st->n_flags || new_prompt) {
  1422. flag = &st->first_flag;
  1423. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  1424. fprintf(stdout, "<DL>\n");
  1425. while (st->n_flags && flag != NULL) {
  1426. fprintf(stdout, "<DT><b>-%c</b></DT>\n", flag->key);
  1427. if (flag->label) {
  1428. fprintf(stdout, "<DD>");
  1429. fprintf(stdout, "%s", flag->label);
  1430. fprintf(stdout, "</DD>\n");
  1431. }
  1432. if (flag->description) {
  1433. fprintf(stdout, "<DD>");
  1434. fprintf(stdout, "%s", flag->description);
  1435. fprintf(stdout, "</DD>\n");
  1436. }
  1437. flag = flag->next_flag;
  1438. fprintf(stdout, "\n");
  1439. }
  1440. if (new_prompt) {
  1441. fprintf(stdout, "<DT><b>--overwrite</b></DT>\n");
  1442. fprintf(stdout, "<DD>%s</DD>\n",
  1443. _("Allow output files to overwrite existing files"));
  1444. }
  1445. fprintf(stdout, "<DT><b>--verbose</b></DT>\n");
  1446. fprintf(stdout, "<DD>%s</DD>\n", _("Verbose module output"));
  1447. fprintf(stdout, "<DT><b>--quiet</b></DT>\n");
  1448. fprintf(stdout, "<DD>%s</DD>\n", _("Quiet module output"));
  1449. fprintf(stdout, "</DL>\n");
  1450. }
  1451. fprintf(stdout, "\n");
  1452. if (st->n_opts) {
  1453. opt = &st->first_option;
  1454. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  1455. fprintf(stdout, "<DL>\n");
  1456. while (opt != NULL) {
  1457. /* TODO: make this a enumeration type? */
  1458. if (opt->key_desc != NULL)
  1459. type = opt->key_desc;
  1460. else
  1461. switch (opt->type) {
  1462. case TYPE_INTEGER:
  1463. type = "integer";
  1464. break;
  1465. case TYPE_DOUBLE:
  1466. type = "float";
  1467. break;
  1468. case TYPE_STRING:
  1469. type = "string";
  1470. break;
  1471. default:
  1472. type = "string";
  1473. break;
  1474. }
  1475. fprintf(stdout, "<DT><b>%s</b>=<em>%s", opt->key, type);
  1476. if (opt->multiple) {
  1477. fprintf(stdout, "[,<i>%s</i>,...]", type);
  1478. }
  1479. fprintf(stdout, "</em></DT>\n");
  1480. if (opt->label) {
  1481. fprintf(stdout, "<DD>");
  1482. print_escaped_for_html(stdout, opt->label);
  1483. fprintf(stdout, "</DD>\n");
  1484. }
  1485. if (opt->description) {
  1486. fprintf(stdout, "<DD>");
  1487. print_escaped_for_html(stdout, opt->description);
  1488. fprintf(stdout, "</DD>\n");
  1489. }
  1490. if (opt->options) {
  1491. fprintf(stdout, "<DD>%s: <em>", _("Options"));
  1492. print_escaped_for_html(stdout, opt->options);
  1493. fprintf(stdout, "</em></DD>\n");
  1494. }
  1495. if (opt->def) {
  1496. fprintf(stdout, "<DD>%s: <em>", _("Default"));
  1497. print_escaped_for_html(stdout, opt->def);
  1498. fprintf(stdout, "</em></DD>\n");
  1499. }
  1500. if (opt->descs) {
  1501. int i = 0;
  1502. while (opt->opts[i]) {
  1503. if (opt->descs[i]) {
  1504. fprintf(stdout, "<DD><b>");
  1505. print_escaped_for_html(stdout, opt->opts[i]);
  1506. fprintf(stdout, "</b>: ");
  1507. print_escaped_for_html(stdout, opt->descs[i]);
  1508. fprintf(stdout, "</DD>\n");
  1509. }
  1510. i++;
  1511. }
  1512. }
  1513. opt = opt->next_opt;
  1514. fprintf(stdout, "\n");
  1515. }
  1516. fprintf(stdout, "</DL>\n");
  1517. }
  1518. fprintf(stdout, "</body>\n</html>\n");
  1519. }
  1520. /*!
  1521. \brief Print a module parameter template to assist with creating shell script wrappers.
  1522. */
  1523. static void G_script(void)
  1524. {
  1525. FILE *fp = stdout;
  1526. char *type;
  1527. fprintf(fp, "#!/bin/sh\n\n");
  1528. fprintf(fp,
  1529. "############################################################################\n");
  1530. fprintf(fp, "#\n");
  1531. fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
  1532. fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
  1533. fprintf(fp, "# PURPOSE: \n");
  1534. fprintf(fp, "# COPYRIGHT: (C) 2009 GRASS Development Team/%s\n",
  1535. G_whoami());
  1536. fprintf(fp, "#\n");
  1537. fprintf(fp,
  1538. "# This program is free software; you can redistribute it and/or modify\n");
  1539. fprintf(fp,
  1540. "# it under the terms of the GNU General Public License as published by\n");
  1541. fprintf(fp,
  1542. "# the Free Software Foundation; either version 2 of the License, or\n");
  1543. fprintf(fp, "# (at your option) any later version.\n");
  1544. fprintf(fp, "#\n");
  1545. fprintf(fp,
  1546. "# This program is distributed in the hope that it will be useful,\n");
  1547. fprintf(fp,
  1548. "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  1549. fprintf(fp,
  1550. "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  1551. fprintf(fp, "# GNU General Public License for more details.\n");
  1552. fprintf(fp, "#\n");
  1553. fprintf(fp,
  1554. "############################################################################\n");
  1555. fprintf(fp, "#%%Module\n");
  1556. if (st->module_info.label)
  1557. fprintf(fp, "#%% label: %s\n", st->module_info.label);
  1558. if (st->module_info.description)
  1559. fprintf(fp, "#%% description: %s\n", st->module_info.description);
  1560. if (st->module_info.keywords)
  1561. fprintf(fp, "#%% keywords: %s\n", st->module_info.keywords);
  1562. fprintf(fp, "#%%End\n");
  1563. if (st->n_flags) {
  1564. struct Flag *flag;
  1565. for (flag = &st->first_flag; flag; flag = flag->next_flag) {
  1566. fprintf(fp, "#%%Flag\n");
  1567. fprintf(fp, "#%% key: %c\n", flag->key);
  1568. if (flag->label)
  1569. fprintf(fp, "#%% label: %s\n", flag->label);
  1570. if (flag->description)
  1571. fprintf(fp, "#%% description: %s\n", flag->description);
  1572. if (flag->guisection)
  1573. fprintf(fp, "#%% guisection: %s\n", flag->guisection);
  1574. fprintf(fp, "#%%End\n");
  1575. }
  1576. }
  1577. if (st->n_opts) {
  1578. struct Option *opt;
  1579. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  1580. switch (opt->type) {
  1581. case TYPE_INTEGER:
  1582. type = "integer";
  1583. break;
  1584. case TYPE_DOUBLE:
  1585. type = "double";
  1586. break;
  1587. case TYPE_STRING:
  1588. type = "string";
  1589. break;
  1590. default:
  1591. type = "string";
  1592. break;
  1593. }
  1594. fprintf(fp, "#%%Option\n");
  1595. fprintf(fp, "#%% key: %s\n", opt->key);
  1596. fprintf(fp, "#%% type: %s\n", type);
  1597. fprintf(fp, "#%% required: %s\n", opt->required ? "yes" : "no");
  1598. fprintf(fp, "#%% multiple: %s\n", opt->multiple ? "yes" : "no");
  1599. if (opt->options)
  1600. fprintf(fp, "#%% options: %s\n", opt->options);
  1601. if (opt->key_desc)
  1602. fprintf(fp, "#%% key_desc: %s\n", opt->key_desc);
  1603. if (opt->label)
  1604. fprintf(fp, "#%% label: %s\n", opt->label);
  1605. if (opt->description)
  1606. fprintf(fp, "#%% description: %s\n", opt->description);
  1607. if (opt->descriptions)
  1608. fprintf(fp, "#%% descriptions: %s\n", opt->descriptions);
  1609. if (opt->answer)
  1610. fprintf(fp, "#%% answer: %s\n", opt->answer);
  1611. if (opt->gisprompt)
  1612. fprintf(fp, "#%% gisprompt: %s\n", opt->gisprompt);
  1613. if (opt->guisection)
  1614. fprintf(fp, "#%% guisection: %s\n", opt->guisection);
  1615. fprintf(fp, "#%%End\n");
  1616. }
  1617. }
  1618. fprintf(fp,
  1619. "\nif [ -z \"$GISBASE\" ] ; then\n"
  1620. " echo \"You must be in GRASS GIS to run this program.\" 1>&2\n"
  1621. " exit 1\n"
  1622. "fi\n"
  1623. "\n"
  1624. "if [ \"$1\" != \"@ARGS_PARSED@\" ] ; then\n"
  1625. " exec g.parser \"$0\" \"$@\"\n"
  1626. "fi\n" "\n" "# CODE GOES HERE\n" "\n");
  1627. }
  1628. /*!
  1629. \brief Build wxPython GUI dialog.
  1630. */
  1631. static void G_gui_wx(void)
  1632. {
  1633. char script[GPATH_MAX];
  1634. if (!st->pgm_name)
  1635. st->pgm_name = G_program_name();
  1636. if (!st->pgm_name)
  1637. G_fatal_error(_("Unable to determine program name"));
  1638. sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
  1639. getenv("GISBASE"));
  1640. G_spawn(getenv("GRASS_PYTHON"), "menuform.py", script, st->pgm_name, NULL);
  1641. }
  1642. /*!
  1643. \brief Invoke GUI dialog
  1644. Use G_gui_wx() to generate GUI dialog.
  1645. G_gui_wx() is called by default (if GRASS_GUI is not defined)
  1646. */
  1647. static void G_gui(void)
  1648. {
  1649. G_gui_wx();
  1650. }
  1651. /**************************************************************************
  1652. *
  1653. * The remaining routines are all local (static) routines used to support
  1654. * the parsing process.
  1655. *
  1656. **************************************************************************/
  1657. static void show_options(int maxlen, const char *str)
  1658. {
  1659. char *buff = G_store(str);
  1660. char *p1, *p2;
  1661. int totlen, len;
  1662. fprintf(stderr, _(" %*s options: "), maxlen, " ");
  1663. totlen = maxlen + 13;
  1664. p1 = buff;
  1665. while ((p2 = G_index(p1, ','))) {
  1666. *p2 = '\0';
  1667. len = strlen(p1) + 1;
  1668. if ((len + totlen) > 76) {
  1669. totlen = maxlen + 13;
  1670. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  1671. }
  1672. fprintf(stderr, "%s,", p1);
  1673. totlen += len;
  1674. p1 = p2 + 1;
  1675. }
  1676. len = strlen(p1);
  1677. if ((len + totlen) > 76)
  1678. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  1679. fprintf(stderr, "%s\n", p1);
  1680. G_free(buff);
  1681. }
  1682. static int show(const char *item, int len)
  1683. {
  1684. int n;
  1685. n = strlen(item) + (len > 0);
  1686. if (n + len > 76) {
  1687. if (len)
  1688. fprintf(stderr, "\n ");
  1689. len = 0;
  1690. }
  1691. fprintf(stderr, "%s", item);
  1692. return n + len;
  1693. }
  1694. static int set_flag(int f)
  1695. {
  1696. struct Flag *flag;
  1697. /* Flag is not valid if there are no flags to set */
  1698. if (!st->n_flags) {
  1699. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  1700. return (1);
  1701. }
  1702. /* Find flag with corrrect keyword */
  1703. flag = &st->first_flag;
  1704. while (flag != NULL) {
  1705. if (flag->key == f) {
  1706. flag->answer = 1;
  1707. return (0);
  1708. }
  1709. flag = flag->next_flag;
  1710. }
  1711. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  1712. return (1);
  1713. }
  1714. /* contents() is used to find things strings with characters like commas and
  1715. * dashes.
  1716. */
  1717. static int contains(const char *s, int c)
  1718. {
  1719. while (*s) {
  1720. if (*s == c)
  1721. return (1);
  1722. s++;
  1723. }
  1724. return (0);
  1725. }
  1726. static int is_option(const char *string)
  1727. {
  1728. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  1729. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  1730. }
  1731. static int set_option(char *string)
  1732. {
  1733. struct Option *at_opt = NULL;
  1734. struct Option *opt = NULL;
  1735. int got_one;
  1736. size_t key_len;
  1737. char the_key[KEYLENGTH];
  1738. char *ptr;
  1739. for (ptr = the_key; *string != '='; ptr++, string++)
  1740. *ptr = *string;
  1741. *ptr = '\0';
  1742. string++;
  1743. /* Find option with best keyword match */
  1744. got_one = 0;
  1745. key_len = strlen(the_key);
  1746. for (at_opt = &st->first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
  1747. if (at_opt->key == NULL || strncmp(the_key, at_opt->key, key_len))
  1748. continue;
  1749. got_one++;
  1750. opt = at_opt;
  1751. /* changed 1/15/91 -dpg old code is in parser.old */
  1752. /* overide ambiguous check, if we get an exact match */
  1753. if (strlen(at_opt->key) == key_len) {
  1754. opt = at_opt;
  1755. got_one = 1;
  1756. break;
  1757. }
  1758. }
  1759. if (got_one > 1) {
  1760. fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
  1761. return (1);
  1762. }
  1763. /* If there is no match, complain */
  1764. if (got_one == 0) {
  1765. fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
  1766. return (1);
  1767. }
  1768. /* Allocate memory where answer is stored */
  1769. if (opt->count++) {
  1770. opt->answer = (char *)G_realloc(opt->answer,
  1771. strlen(opt->answer) + strlen(string) +
  1772. 2);
  1773. strcat(opt->answer, ",");
  1774. strcat(opt->answer, string);
  1775. }
  1776. else
  1777. opt->answer = G_store(string);
  1778. return (0);
  1779. }
  1780. static int check_opts(void)
  1781. {
  1782. struct Option *opt;
  1783. int error;
  1784. int ans;
  1785. error = 0;
  1786. if (!st->n_opts)
  1787. return (0);
  1788. opt = &st->first_option;
  1789. while (opt != NULL) {
  1790. /* Check answer against options if any */
  1791. if (opt->options && opt->answer) {
  1792. if (opt->multiple == 0)
  1793. error += check_an_opt(opt->key, opt->type,
  1794. opt->options, opt->opts, &opt->answer);
  1795. else {
  1796. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  1797. error += check_an_opt(opt->key, opt->type,
  1798. opt->options, opt->opts, &opt->answers[ans]);
  1799. }
  1800. }
  1801. /* Check answer against user's check subroutine if any */
  1802. if (opt->checker)
  1803. error += opt->checker(opt->answer);
  1804. opt = opt->next_opt;
  1805. }
  1806. return (error);
  1807. }
  1808. static int check_an_opt(const char *key, int type, const char *options,
  1809. const char **opts, char **answerp)
  1810. {
  1811. const char *answer = *answerp;
  1812. int error;
  1813. int found;
  1814. error = 0;
  1815. switch (type) {
  1816. case TYPE_INTEGER:
  1817. error = check_int(answer, opts);
  1818. break;
  1819. case TYPE_DOUBLE:
  1820. error = check_double(answer, opts);
  1821. break;
  1822. case TYPE_STRING:
  1823. error = check_string(answer, opts, &found);
  1824. break;
  1825. }
  1826. switch (error) {
  1827. case 0:
  1828. break;
  1829. case BAD_SYNTAX:
  1830. fprintf(stderr,
  1831. _("\nERROR: illegal range syntax for parameter <%s>\n"), key);
  1832. fprintf(stderr, _(" Presented as: %s\n"), options);
  1833. break;
  1834. case OUT_OF_RANGE:
  1835. fprintf(stderr,
  1836. _("\nERROR: value <%s> out of range for parameter <%s>\n"),
  1837. answer, key);
  1838. fprintf(stderr, _(" Legal range: %s\n"), options);
  1839. break;
  1840. case MISSING_VALUE:
  1841. fprintf(stderr, _("\nERROR: Missing value for parameter <%s>\n"),
  1842. key);
  1843. break;
  1844. case AMBIGUOUS:
  1845. fprintf(stderr, _("\nERROR: value <%s> ambiguous for parameter <%s>\n"),
  1846. answer, key);
  1847. fprintf(stderr, _(" valid options: %s\n"), options);
  1848. break;
  1849. case REPLACED:
  1850. *answerp = G_store(opts[found]);
  1851. error = 0;
  1852. break;
  1853. }
  1854. return error;
  1855. }
  1856. static int check_int(const char *ans, const char **opts)
  1857. {
  1858. int d, i;
  1859. if (sscanf(ans, "%d", &d) != 1)
  1860. return MISSING_VALUE;
  1861. for (i = 0; opts[i]; i++) {
  1862. const char *opt = opts[i];
  1863. int lo, hi;
  1864. if (contains(opt, '-')) {
  1865. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  1866. if (d >= lo && d <= hi)
  1867. return 0;
  1868. }
  1869. else if (sscanf(opt, "-%d", &hi) == 1) {
  1870. if (d <= hi)
  1871. return 0;
  1872. }
  1873. else if (sscanf(opt, "%d-", &lo) == 1) {
  1874. if (d >= lo)
  1875. return 0;
  1876. }
  1877. else
  1878. return BAD_SYNTAX;
  1879. }
  1880. else {
  1881. if (sscanf(opt, "%d", &lo) == 1) {
  1882. if (d == lo)
  1883. return 0;
  1884. }
  1885. else
  1886. return BAD_SYNTAX;
  1887. }
  1888. }
  1889. return OUT_OF_RANGE;
  1890. }
  1891. static int check_double(const char *ans, const char **opts)
  1892. {
  1893. double d;
  1894. int i;
  1895. if (sscanf(ans, "%lf", &d) != 1)
  1896. return MISSING_VALUE;
  1897. for (i = 0; opts[i]; i++) {
  1898. const char *opt = opts[i];
  1899. double lo, hi;
  1900. if (contains(opt, '-')) {
  1901. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  1902. if (d >= lo && d <= hi)
  1903. return 0;
  1904. }
  1905. else if (sscanf(opt, "-%lf", &hi) == 1) {
  1906. if (d <= hi)
  1907. return 0;
  1908. }
  1909. else if (sscanf(opt, "%lf-", &lo) == 1) {
  1910. if (d >= lo)
  1911. return 0;
  1912. }
  1913. else
  1914. return BAD_SYNTAX;
  1915. }
  1916. else {
  1917. if (sscanf(opt, "%lf", &lo) == 1) {
  1918. if (d == lo)
  1919. return 0;
  1920. }
  1921. else
  1922. return BAD_SYNTAX;
  1923. }
  1924. }
  1925. return OUT_OF_RANGE;
  1926. }
  1927. static int check_string(const char *ans, const char **opts, int *result)
  1928. {
  1929. int len = strlen(ans);
  1930. int found = 0;
  1931. int i;
  1932. for (i = 0; opts[i]; i++) {
  1933. if (strcmp(ans, opts[i]) == 0)
  1934. return 0;
  1935. if (strncmp(ans, opts[i], len) == 0) {
  1936. *result = i;
  1937. found++;
  1938. }
  1939. }
  1940. switch (found) {
  1941. case 0: return OUT_OF_RANGE;
  1942. case 1: return REPLACED;
  1943. default: return AMBIGUOUS;
  1944. }
  1945. }
  1946. static int check_required(void)
  1947. {
  1948. struct Option *opt;
  1949. int err;
  1950. err = 0;
  1951. if (!st->n_opts)
  1952. return (0);
  1953. opt = &st->first_option;
  1954. while (opt != NULL) {
  1955. if (opt->required && opt->answer == NULL) {
  1956. fprintf(stderr,
  1957. _("\nERROR: Required parameter <%s> not set:\n (%s).\n"),
  1958. opt->key, (opt->label ? opt->label : opt->description) );
  1959. err++;
  1960. }
  1961. opt = opt->next_opt;
  1962. }
  1963. return (err);
  1964. }
  1965. static void split_opts(void)
  1966. {
  1967. struct Option *opt;
  1968. char *ptr1;
  1969. char *ptr2;
  1970. int allocated;
  1971. int ans_num;
  1972. int len;
  1973. if (!st->n_opts)
  1974. return;
  1975. opt = &st->first_option;
  1976. while (opt != NULL) {
  1977. if ( /*opt->multiple && */ (opt->answer != NULL)) {
  1978. /* Allocate some memory to store array of pointers */
  1979. allocated = 10;
  1980. opt->answers = (char **)G_malloc(allocated * sizeof(char *));
  1981. ans_num = 0;
  1982. ptr1 = opt->answer;
  1983. opt->answers[ans_num] = NULL;
  1984. for (;;) {
  1985. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1986. ptr2++, len++) ;
  1987. if (len > 0) { /* skip ,, */
  1988. opt->answers[ans_num] = (char *)G_malloc(len + 1);
  1989. G_copy(opt->answers[ans_num], ptr1, len);
  1990. opt->answers[ans_num][len] = 0;
  1991. ans_num++;
  1992. if (ans_num >= allocated) {
  1993. allocated += 10;
  1994. opt->answers =
  1995. (char **)G_realloc((char *)opt->answers,
  1996. allocated * sizeof(char *));
  1997. }
  1998. opt->answers[ans_num] = NULL;
  1999. }
  2000. if (*ptr2 == '\0')
  2001. break;
  2002. ptr1 = ptr2 + 1;
  2003. if (*ptr1 == '\0')
  2004. break;
  2005. }
  2006. }
  2007. opt = opt->next_opt;
  2008. }
  2009. }
  2010. static int check_multiple_opts(void)
  2011. {
  2012. struct Option *opt;
  2013. const char *ptr;
  2014. int n_commas;
  2015. int n;
  2016. int error;
  2017. if (!st->n_opts)
  2018. return (0);
  2019. error = 0;
  2020. opt = &st->first_option;
  2021. while (opt != NULL) {
  2022. if ((opt->answer != NULL) && (opt->key_desc != NULL)) {
  2023. /* count commas */
  2024. n_commas = 1;
  2025. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  2026. if (*ptr == ',')
  2027. n_commas++;
  2028. /* count items */
  2029. for (n = 0; opt->answers[n] != '\0'; n++) ;
  2030. /* if not correct multiple of items */
  2031. if (n % n_commas) {
  2032. fprintf(stderr,
  2033. _("\nERROR: option <%s> must be provided in multiples of %d\n"),
  2034. opt->key, n_commas);
  2035. fprintf(stderr, _(" You provided %d items:\n"), n);
  2036. fprintf(stderr, " %s\n", opt->answer);
  2037. error++;
  2038. }
  2039. }
  2040. opt = opt->next_opt;
  2041. }
  2042. return (error);
  2043. }
  2044. /* Check for all 'new' if element already exists */
  2045. static int check_overwrite(void)
  2046. {
  2047. struct Option *opt;
  2048. char age[KEYLENGTH];
  2049. char element[KEYLENGTH];
  2050. char desc[KEYLENGTH];
  2051. int error = 0;
  2052. const char *overstr;
  2053. int over;
  2054. st->module_info.overwrite = 0;
  2055. if (!st->n_opts)
  2056. return (0);
  2057. over = 0;
  2058. /* Check the GRASS OVERWRITE variable */
  2059. if ((overstr = G__getenv("OVERWRITE"))) {
  2060. over = atoi(overstr);
  2061. }
  2062. /* Check the GRASS_OVERWRITE environment variable */
  2063. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  2064. if (atoi(overstr))
  2065. over = 1;
  2066. }
  2067. if (st->overwrite || over) {
  2068. st->module_info.overwrite = 1;
  2069. /* Set the environment so that programs run in a script also obey --o */
  2070. putenv("GRASS_OVERWRITE=1");
  2071. /* No need to check options for existing files if overwrite is true */
  2072. return error;
  2073. }
  2074. opt = &st->first_option;
  2075. while (opt != NULL) {
  2076. if ((opt->answer != NULL) && (opt->gisprompt != NULL)) {
  2077. split_gisprompt(opt->gisprompt, age, element, desc);
  2078. if (strcmp(age, "new") == 0) {
  2079. int i;
  2080. for (i = 0; opt->answers[i]; i++) {
  2081. if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
  2082. if (!st->overwrite && !over) {
  2083. if (G_info_format() != G_INFO_FORMAT_GUI) {
  2084. fprintf(stderr,
  2085. _("ERROR: option <%s>: <%s> exists.\n"),
  2086. opt->key, opt->answers[i]);
  2087. }
  2088. else {
  2089. fprintf(stderr,
  2090. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  2091. getpid(), opt->key, opt->answers[i]);
  2092. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  2093. getpid());
  2094. }
  2095. error = 1;
  2096. }
  2097. }
  2098. }
  2099. }
  2100. }
  2101. opt = opt->next_opt;
  2102. }
  2103. return (error);
  2104. }
  2105. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  2106. char *desc)
  2107. {
  2108. const char *ptr1;
  2109. char *ptr2;
  2110. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  2111. if (*ptr1 == ',')
  2112. break;
  2113. *ptr2 = *ptr1;
  2114. }
  2115. *ptr2 = '\0';
  2116. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  2117. if (*ptr1 == ',')
  2118. break;
  2119. *ptr2 = *ptr1;
  2120. }
  2121. *ptr2 = '\0';
  2122. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  2123. if (*ptr1 == ',')
  2124. break;
  2125. *ptr2 = *ptr1;
  2126. }
  2127. *ptr2 = '\0';
  2128. }
  2129. /*!
  2130. * \brief Creates command to run non-interactive.
  2131. *
  2132. * Creates a command-line that runs the current command completely
  2133. * non-interactive.
  2134. *
  2135. * \return pointer to a char string
  2136. */
  2137. char *G_recreate_command(void)
  2138. {
  2139. char *buff;
  2140. char flg[4];
  2141. char *cur;
  2142. const char *tmp;
  2143. struct Flag *flag;
  2144. struct Option *opt;
  2145. int n, len, slen;
  2146. int nalloced = 0;
  2147. G_debug(3, "G_recreate_command()");
  2148. /* Flag is not valid if there are no flags to set */
  2149. buff = G_calloc(1024, sizeof(char));
  2150. nalloced += 1024;
  2151. tmp = G_program_name();
  2152. len = strlen(tmp);
  2153. if (len >= nalloced) {
  2154. nalloced += (1024 > len) ? 1024 : len + 1;
  2155. buff = G_realloc(buff, nalloced);
  2156. }
  2157. cur = buff;
  2158. strcpy(cur, tmp);
  2159. cur += len;
  2160. if (st->n_flags) {
  2161. flag = &st->first_flag;
  2162. while (flag != '\0') {
  2163. if (flag->answer == 1) {
  2164. flg[0] = ' ';
  2165. flg[1] = '-';
  2166. flg[2] = flag->key;
  2167. flg[3] = '\0';
  2168. slen = strlen(flg);
  2169. if (len + slen >= nalloced) {
  2170. nalloced +=
  2171. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2172. buff = G_realloc(buff, nalloced);
  2173. cur = buff + len;
  2174. }
  2175. strcpy(cur, flg);
  2176. cur += slen;
  2177. len += slen;
  2178. }
  2179. flag = flag->next_flag;
  2180. }
  2181. }
  2182. opt = &st->first_option;
  2183. while (opt != '\0') {
  2184. if (opt->answer != '\0' && opt->answers[0] != NULL) {
  2185. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  2186. if (len + slen >= nalloced) {
  2187. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2188. buff = G_realloc(buff, nalloced);
  2189. cur = buff + len;
  2190. }
  2191. strcpy(cur, " ");
  2192. cur++;
  2193. strcpy(cur, opt->key);
  2194. cur = strchr(cur, '\0');
  2195. strcpy(cur, "=");
  2196. cur++;
  2197. if (opt->type == TYPE_STRING) {
  2198. strcpy(cur, "\"");
  2199. cur++;
  2200. }
  2201. strcpy(cur, opt->answers[0]);
  2202. cur = strchr(cur, '\0');
  2203. len = cur - buff;
  2204. for (n = 1; opt->answers[n] != NULL && opt->answers[n] != '\0';
  2205. n++) {
  2206. if (opt->answers[n] == NULL)
  2207. break;
  2208. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  2209. if (len + slen >= nalloced) {
  2210. nalloced +=
  2211. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2212. buff = G_realloc(buff, nalloced);
  2213. cur = buff + len;
  2214. }
  2215. strcpy(cur, ",");
  2216. cur++;
  2217. strcpy(cur, opt->answers[n]);
  2218. cur = strchr(cur, '\0');
  2219. len = cur - buff;
  2220. }
  2221. if (opt->type == TYPE_STRING) {
  2222. strcpy(cur, "\"");
  2223. cur++;
  2224. len = cur - buff;
  2225. }
  2226. }
  2227. opt = opt->next_opt;
  2228. }
  2229. return (buff);
  2230. }