parser.c 63 KB

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