parser.c 61 KB

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