parser.c 65 KB

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