parser_rest.c 6.8 KB

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