parser.c 65 KB

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