GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-565e82de51
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parser_rest.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/parser_rest.c
3 
4  \brief GIS Library - Argument parsing functions (reStructuredText output)
5 
6  (C) 2012 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Luca Delucchi
12  */
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #include "parser_local_proto.h"
20 
21 static void print_escaped_for_rest(FILE *f, const char *str);
22 static void print_escaped_for_rest_options(FILE *f, const char *str);
23 
24 /*!
25  \brief Print module usage description in reStructuredText format.
26  */
27 void G__usage_rest(void)
28 {
29  struct Option *opt;
30  struct Flag *flag;
31  const char *type;
32  int new_prompt = 0;
33  unsigned int s;
34 
35  new_prompt = G__uses_new_gisprompt();
36 
37  if (!st->pgm_name)
38  st->pgm_name = G_program_name();
39  if (!st->pgm_name)
40  st->pgm_name = "??";
41 
42  fprintf(stdout, "=================");
43  for (s = 0; s <= strlen(st->pgm_name); s++) {
44  fprintf(stdout, "=");
45  }
46  fprintf(stdout, "\n");
47  fprintf(stdout, "%s - GRASS GIS manual\n", st->pgm_name);
48  fprintf(stdout, "=================");
49  for (s = 0; s <= strlen(st->pgm_name); s++) {
50  fprintf(stdout, "=");
51  }
52  fprintf(stdout, "\n\n");
53 
54  fprintf(stdout, ".. figure:: grass_logo.png\n");
55  fprintf(stdout, " :align: center\n");
56  fprintf(stdout, " :alt: GRASS logo\n\n");
57 
58  fprintf(stdout, "%s\n----\n", _("NAME"));
59  fprintf(stdout, "**%s**", st->pgm_name);
60 
61  if (st->module_info.label || st->module_info.description)
62  fprintf(stdout, " - ");
63 
64  if (st->module_info.label)
65  fprintf(stdout, "%s\n\n", st->module_info.label);
66 
67  if (st->module_info.description)
68  fprintf(stdout, "%s\n", st->module_info.description);
69 
70  fprintf(stdout, "\n%s\n----------------------\n", _("KEYWORDS"));
71  if (st->module_info.keywords) {
72  G__print_keywords(stdout, NULL, FALSE);
73  fprintf(stdout, "\n");
74  }
75  fprintf(stdout, "\n%s\n----------------------\n", _("SYNOPSIS"));
76  fprintf(stdout, "**%s**\n\n", st->pgm_name);
77  fprintf(stdout, "**%s** --help\n\n", st->pgm_name);
78 
79  fprintf(stdout, "**%s**", st->pgm_name);
80 
81  /* print short version first */
82  if (st->n_flags) {
83  flag = &st->first_flag;
84  fprintf(stdout, " [**-");
85  while (flag != NULL) {
86  fprintf(stdout, "%c", flag->key);
87  flag = flag->next_flag;
88  }
89  fprintf(stdout, "**] ");
90  }
91  else
92  fprintf(stdout, " ");
93 
94  if (st->n_opts) {
95  opt = &st->first_option;
96 
97  while (opt != NULL) {
98  if (opt->key_desc != NULL)
99  type = opt->key_desc;
100  else
101  switch (opt->type) {
102  case TYPE_INTEGER:
103  type = "integer";
104  break;
105  case TYPE_DOUBLE:
106  type = "float";
107  break;
108  case TYPE_STRING:
109  type = "string";
110  break;
111  default:
112  type = "string";
113  break;
114  }
115  if (!opt->required)
116  fprintf(stdout, " [");
117  fprintf(stdout, "**%s** = *%s*", opt->key, type);
118  if (opt->multiple) {
119  fprintf(stdout, " [, *%s* ,...]", type);
120  }
121  if (!opt->required)
122  fprintf(stdout, "] ");
123 
124  opt = opt->next_opt;
125  fprintf(stdout, " ");
126  }
127  }
128  if (new_prompt)
129  fprintf(stdout, " [-- **overwrite**] ");
130 
131  fprintf(stdout, " [-- **verbose**] ");
132  fprintf(stdout, " [-- **quiet**] ");
133 
134  fprintf(stdout, "\n");
135 
136  /* now long version */
137  fprintf(stdout, "\n");
138  if (st->n_flags || new_prompt) {
139  flag = &st->first_flag;
140  fprintf(stdout, "%s:\n~~~~~~\n", _("Flags"));
141  while (st->n_flags && flag != NULL) {
142  fprintf(stdout, "**-%c**\n", flag->key);
143 
144  if (flag->label) {
145  fprintf(stdout, " %s", flag->label);
146  }
147 
148  if (flag->description) {
149  fprintf(stdout, " %s", flag->description);
150  }
151 
152  flag = flag->next_flag;
153  fprintf(stdout, "\n");
154  }
155  if (new_prompt) {
156  fprintf(stdout, "-- **overwrite**\n");
157  fprintf(stdout, " %s\n",
158  _("Allow output files to overwrite existing files"));
159  }
160 
161  fprintf(stdout, "-- **verbose**\n");
162  fprintf(stdout, " %s\n", _("Verbose module output"));
163 
164  fprintf(stdout, "-- **quiet**\n");
165  fprintf(stdout, " %s\n", _("Quiet module output"));
166 
167  fprintf(stdout, "\n");
168  }
169 
170  fprintf(stdout, "\n");
171  if (st->n_opts) {
172  opt = &st->first_option;
173  fprintf(stdout, "%s:\n~~~~~~~~~~~\n", _("Parameters"));
174 
175  while (opt != NULL) {
176  /* TODO: make this a enumeration type? */
177  if (opt->key_desc != NULL)
178  type = opt->key_desc;
179  else
180  switch (opt->type) {
181  case TYPE_INTEGER:
182  type = "integer";
183  break;
184  case TYPE_DOUBLE:
185  type = "float";
186  break;
187  case TYPE_STRING:
188  type = "string";
189  break;
190  default:
191  type = "string";
192  break;
193  }
194  fprintf(stdout, "**%s** = *%s*", opt->key, type);
195  if (opt->multiple) {
196  fprintf(stdout, " [, *%s* ,...]", type);
197  }
198  /* fprintf(stdout, "*"); */
199  if (opt->required) {
200  fprintf(stdout, " **[required]**");
201  }
202  fprintf(stdout, "\n\n");
203  if (opt->label) {
204  fprintf(stdout, "\t");
205  print_escaped_for_rest(stdout, opt->label);
206  /* fprintf(stdout, " %s\n", opt->label); */
207  fprintf(stdout, "\n\n");
208  }
209  if (opt->description) {
210  fprintf(stdout, "\t");
211  print_escaped_for_rest(stdout, opt->description);
212  /* fprintf(stdout, " %s\n", opt->description); */
213  fprintf(stdout, "\n\n");
214  }
215 
216  if (opt->options) {
217  fprintf(stdout, "\t%s: *", _("Options"));
218  print_escaped_for_rest_options(stdout, opt->options);
219  /* fprintf(stdout, "%s", opt->options); */
220  fprintf(stdout, "*\n\n");
221  }
222 
223  if (opt->def) {
224  fprintf(stdout, "\t%s:", _("Default"));
225  /* TODO check if value is empty
226  if (!opt->def.empty()){ */
227  fprintf(stdout, " *");
228  print_escaped_for_rest(stdout, opt->def);
229  /* fprintf(stdout,"%s", opt->def); */
230  fprintf(stdout, "*\n\n");
231  /* } */
232  fprintf(stdout, "\n\n");
233  }
234 
235  if (opt->descs) {
236  int i = 0;
237 
238  while (opt->opts[i]) {
239  if (opt->descs[i]) {
240  fprintf(stdout, "\t\t**");
241  print_escaped_for_rest(stdout, opt->opts[i]);
242  /*fprintf(stdout,"%s", opt->opts[i]); */
243  fprintf(stdout, "** : ");
244  print_escaped_for_rest(stdout, opt->descs[i]);
245  /* fprintf(stdout, "%s\n", opt->descs[i]); */
246  fprintf(stdout, "\n\n");
247  }
248  i++;
249  }
250  }
251 
252  opt = opt->next_opt;
253  fprintf(stdout, "\n");
254  }
255  fprintf(stdout, "\n");
256  }
257 }
258 
259 /*!
260  * \brief Format text for reStructuredText output
261  */
262 #define do_escape(c, escaped) \
263  case c: \
264  fputs(escaped, f); \
265  break
266 void print_escaped_for_rest(FILE *f, const char *str)
267 {
268  const char *s;
269 
270  for (s = str; *s; s++) {
271  switch (*s) {
272  do_escape('\n', "\n\n");
273  default:
274  fputc(*s, f);
275  }
276  }
277 }
278 
279 void print_escaped_for_rest_options(FILE *f, const char *str)
280 {
281  const char *s;
282 
283  for (s = str; *s; s++) {
284  switch (*s) {
285  do_escape('\n', "\n\n");
286  default:
287  fputc(*s, f);
288  }
289  }
290 }
291 
292 #undef do_escape
#define NULL
Definition: ccmath.h:32
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:28
#define TYPE_STRING
Definition: gis.h:186
#define TYPE_INTEGER
Definition: gis.h:184
#define FALSE
Definition: gis.h:83
#define TYPE_DOUBLE
Definition: gis.h:185
#define _(str)
Definition: glocale.h:10
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *), int newline)
Print list of keywords (internal use only)
Definition: parser.c:927
int G__uses_new_gisprompt(void)
Definition: parser.c:890
struct state * st
Definition: parser.c:104
#define do_escape(c, escaped)
Format text for reStructuredText output.
Definition: parser_rest.c:262
void G__usage_rest(void)
Print module usage description in reStructuredText format.
Definition: parser_rest.c:27
Structure that stores flag info.
Definition: gis.h:589
struct Flag * next_flag
Definition: gis.h:598
const char * description
Definition: gis.h:595
char key
Definition: gis.h:590
const char * label
Definition: gis.h:594
Structure that stores option information.
Definition: gis.h:558
const char * key
Definition: gis.h:559
struct Option * next_opt
Definition: gis.h:575
const char * key_desc
Definition: gis.h:565
const char ** opts
Definition: gis.h:564
const char * label
Definition: gis.h:566
int type
Definition: gis.h:560
const char * def
Definition: gis.h:573
const char * description
Definition: gis.h:567
int required
Definition: gis.h:561
const char ** descs
Definition: gis.h:570
const char * options
Definition: gis.h:563
int multiple
Definition: gis.h:562