parser.c 70 KB

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