parser.c 64 KB

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