parser.c 84 KB

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