parser.c 63 KB

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