main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /****************************************************************************
  2. *
  3. * MODULE: g.mlist
  4. *
  5. * AUTHOR(S): Huidae Cho
  6. * Based on general/manage/cmd/list.c by Michael Shapiro.
  7. *
  8. * PURPOSE: Lists available GRASS data base files of the
  9. * user-specified data type to standard output
  10. *
  11. * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/manage.h>
  23. #include <grass/glocale.h>
  24. #include <grass/spawn.h>
  25. static int any = 0;
  26. static void make_list(const struct list *,
  27. const char *, const char *,
  28. int, int, int);
  29. int main(int argc, char *argv[])
  30. {
  31. struct GModule *module;
  32. struct
  33. {
  34. struct Option *type;
  35. struct Option *pattern;
  36. struct Option *exclude;
  37. struct Option *separator;
  38. struct Option *mapset;
  39. } opt;
  40. struct
  41. {
  42. struct Flag *regex;
  43. struct Flag *extended;
  44. struct Flag *type;
  45. struct Flag *mapset;
  46. struct Flag *pretty;
  47. struct Flag *full;
  48. } flag;
  49. int i, n, all, num_types, nlist;
  50. void *filter = NULL, *exclude = NULL;
  51. const char *mapset;
  52. char separator[2];
  53. G_gisinit(argv[0]);
  54. module = G_define_module();
  55. G_add_keyword(_("general"));
  56. G_add_keyword(_("map management"));
  57. G_add_keyword(_("list"));
  58. module->description =
  59. _("Lists available GRASS data base files "
  60. "of the user-specified data type optionally using the search pattern.");
  61. M_read_list(FALSE, &nlist);
  62. opt.type = G_define_standard_option(G_OPT_M_DATATYPE);
  63. opt.type->multiple = YES;
  64. opt.type->options = M_get_options(TRUE);
  65. opt.type->descriptions = M_get_option_desc(TRUE);
  66. opt.pattern = G_define_option();
  67. opt.pattern->key = "pattern";
  68. opt.pattern->type = TYPE_STRING;
  69. opt.pattern->required = NO;
  70. opt.pattern->multiple = NO;
  71. opt.pattern->description = _("Map name search pattern (default: all)");
  72. opt.pattern->guisection = _("Pattern");
  73. opt.exclude = G_define_option();
  74. opt.exclude->key = "exclude";
  75. opt.exclude->type = TYPE_STRING;
  76. opt.exclude->required = NO;
  77. opt.exclude->multiple = NO;
  78. opt.exclude->description = _("Map name exclusion pattern (default: none)");
  79. opt.exclude->guisection = _("Pattern");
  80. opt.mapset = G_define_standard_option(G_OPT_M_MAPSET);
  81. opt.mapset->label =
  82. _("Name of mapset to list (default: current search path)");
  83. opt.separator = G_define_standard_option(G_OPT_F_SEP);
  84. opt.separator->answer = "newline";
  85. flag.regex = G_define_flag();
  86. flag.regex->key = 'r';
  87. flag.regex->description =
  88. _("Use basic regular expressions instead of wildcards");
  89. flag.regex->guisection = _("Pattern");
  90. flag.extended = G_define_flag();
  91. flag.extended->key = 'e';
  92. flag.extended->description =
  93. _("Use extended regular expressions instead of wildcards");
  94. flag.extended->guisection = _("Pattern");
  95. flag.type = G_define_flag();
  96. flag.type->key = 't';
  97. flag.type->description = _("Print data types");
  98. flag.type->guisection = _("Print");
  99. flag.mapset = G_define_flag();
  100. flag.mapset->key = 'm';
  101. flag.mapset->description = _("Print fully-qualified map names (including mapsets)");
  102. flag.mapset->guisection = _("Print");
  103. flag.pretty = G_define_flag();
  104. flag.pretty->key = 'p';
  105. flag.pretty->description = _("Pretty printing in human readable format");
  106. flag.pretty->guisection = _("Print");
  107. flag.full = G_define_flag();
  108. flag.full->key = 'f';
  109. flag.full->description = _("Verbose listing (also list map titles)");
  110. flag.full->guisection = _("Print");
  111. if (G_parser(argc, argv))
  112. exit(EXIT_FAILURE);
  113. if (flag.regex->answer && flag.extended->answer)
  114. G_fatal_error(_("-r and -e are mutually exclusive"));
  115. if (opt.pattern->answer) {
  116. if (flag.regex->answer || flag.extended->answer)
  117. filter = G_ls_regex_filter(opt.pattern->answer, 0, (int) flag.extended->answer);
  118. else
  119. filter = G_ls_glob_filter(opt.pattern->answer, 0);
  120. }
  121. if (opt.exclude->answer) {
  122. if (flag.regex->answer || flag.extended->answer)
  123. exclude = G_ls_regex_filter(opt.exclude->answer, 1, (int) flag.extended->answer);
  124. else
  125. exclude = G_ls_glob_filter(opt.exclude->answer, 1);
  126. }
  127. if (strcmp(opt.separator->answer, "newline") == 0)
  128. separator[0] = '\n';
  129. else if (strcmp(opt.separator->answer, "comma") == 0)
  130. separator[0] = ',';
  131. else if (strcmp(opt.separator->answer, "space") == 0)
  132. separator[0] = ' ';
  133. else if (strcmp(opt.separator->answer, "tab") == 0)
  134. separator[0] = '\t';
  135. else
  136. separator[0] = opt.separator->answer[0];
  137. separator[1] = 0;
  138. mapset = opt.mapset->answer;
  139. if (mapset == NULL)
  140. mapset = "";
  141. if (G_strcasecmp(mapset, ".") == 0)
  142. mapset = G_mapset();
  143. for (i = 0; opt.type->answers[i]; i++) {
  144. if (strcmp(opt.type->answers[i], "all") == 0)
  145. break;
  146. }
  147. if (opt.type->answers[i]) {
  148. all = 1;
  149. num_types = nlist;
  150. }
  151. else {
  152. all = 0;
  153. num_types = i;
  154. }
  155. for (i = 0; i < num_types; i++) {
  156. n = all ? i : M_get_element(opt.type->answers[i]);
  157. if (flag.full->answer) {
  158. char lister[GPATH_MAX];
  159. sprintf(lister, "%s/etc/lister/%s", G_gisbase(),
  160. M_get_list(n)->element[0]);
  161. G_debug(3, "lister CMD: %s", lister);
  162. if (access(lister, 1) == 0) { /* execute permission? */
  163. G_spawn(lister, lister, mapset, NULL);
  164. continue;
  165. }
  166. }
  167. else
  168. make_list(M_get_list(n), mapset, separator,
  169. flag.pretty->answer, flag.type->answer,
  170. flag.mapset->answer);
  171. }
  172. if (!flag.pretty->answer && any)
  173. fprintf(stdout, "\n");
  174. if (filter)
  175. G_free_ls_filter(filter);
  176. if (exclude)
  177. G_free_ls_filter(exclude);
  178. exit(EXIT_SUCCESS);
  179. }
  180. static void make_list(
  181. const struct list *elem,
  182. const char *mapset, const char *separator,
  183. int pretty, int add_type, int add_mapset)
  184. {
  185. char path[GPATH_MAX];
  186. const char *element = elem->element[0];
  187. const char *alias = elem->alias;
  188. char **list;
  189. int count;
  190. int i;
  191. if (pretty) {
  192. G_list_element(element, alias, mapset, NULL);
  193. return;
  194. }
  195. if (!mapset || !*mapset) {
  196. int n;
  197. for (n = 0; mapset = G__mapset_name(n), mapset; n++)
  198. make_list(elem, mapset, separator,
  199. pretty, add_type, add_mapset);
  200. return;
  201. }
  202. G_file_name(path, element, "", mapset);
  203. if (access(path, 0) != 0)
  204. return;
  205. list = G__ls(path, &count);
  206. if (!list)
  207. return;
  208. if (count > 0) {
  209. if (any) {
  210. if (pretty)
  211. fprintf(stdout, "\n");
  212. else
  213. fprintf(stdout, "%s", separator);
  214. }
  215. G_message(_("%s available in mapset <%s>:"),
  216. elem->text, mapset);
  217. }
  218. for (i = 0; i < count; i++) {
  219. char *name = list[i];
  220. int need_mapset = 0;
  221. if (any && i != 0)
  222. fprintf(stdout, "%s", separator);
  223. if (add_type)
  224. fprintf(stdout, "%s/", alias);
  225. fprintf(stdout, "%s", name);
  226. if (!add_mapset) {
  227. const char *mapset2 = G_find_file2(element, name, "");
  228. need_mapset = strcmp(mapset, mapset2) != 0;
  229. }
  230. if (add_mapset || need_mapset)
  231. fprintf(stdout, "@%s", mapset);
  232. G_free(name);
  233. any++;
  234. }
  235. fflush(stdout);
  236. G_free(list);
  237. }