parser.c 64 KB

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