parser.c 63 KB

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