list.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. \file list.c
  3. \brief List elements
  4. \author Unknown (probably CERL)
  5. (C) 2000 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. */
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. static int broken_pipe;
  19. static int hit_return = 0;
  20. static int list_element(FILE *, const char *, const char *, const char *,
  21. int (*)(const char *, const char *, const char *));
  22. static void sigpipe_catch(int);
  23. int G_set_list_hit_return(int flag)
  24. {
  25. hit_return = flag;
  26. return 0;
  27. }
  28. /**
  29. \brief General purpose list function.
  30. Will list files from all mapsets
  31. in the mapset list for a specified database element.
  32. Note: output is to stdout piped thru the more utility
  33. lister (char *name char *mapset, char* buf)
  34. Given file 'name', and 'mapset', lister() should copy a string into 'buf'
  35. when called with name == "", should set buf to general title for mapset list.
  36. \param element Database element (eg, "cell", "cellhd", etc)
  37. \param desc Description for element (if NULL, element is used)
  38. \param mapset Mapset to be listed "" to list all mapsets in mapset search list
  39. "." will list current mapset
  40. \param lister If given will call this routine to get a list
  41. title. NULL if no titles desired.
  42. \return Number of elements
  43. */
  44. int G_list_element(const char *element,
  45. const char *desc,
  46. const char *mapset,
  47. int (*lister) (const char *, const char *, const char *))
  48. {
  49. int n;
  50. FILE *more;
  51. int count;
  52. #ifdef SIGPIPE
  53. void (*sigpipe) ();
  54. #endif
  55. /* must catch broken pipe in case "more" quits */
  56. broken_pipe = 0;
  57. #ifdef SIGPIPE
  58. sigpipe = signal(SIGPIPE, sigpipe_catch);
  59. #endif
  60. count = 0;
  61. if (desc == 0 || *desc == 0)
  62. desc = element;
  63. /* G_popen() does not work with MinGW? */
  64. #ifndef __MINGW32__
  65. /*
  66. * G_popen() the more command to page the output
  67. */
  68. if (isatty(1)) {
  69. #ifdef __MINGW32__
  70. more = G_popen("%GRASS_PAGER%", "w");
  71. #else
  72. more = G_popen("$GRASS_PAGER", "w");
  73. #endif
  74. if (!more)
  75. more = stdout;
  76. }
  77. else
  78. #endif
  79. more = stdout;
  80. fprintf(more, "----------------------------------------------\n");
  81. /*
  82. * if no specific mapset is requested, list the mapsets
  83. * from the mapset search list
  84. * otherwise just list the specified mapset
  85. */
  86. if (mapset == 0 || *mapset == 0)
  87. for (n = 0; !broken_pipe && (mapset = G__mapset_name(n)); n++)
  88. count += list_element(more, element, desc, mapset, lister);
  89. else
  90. count += list_element(more, element, desc, mapset, lister);
  91. if (!broken_pipe) {
  92. if (count == 0) {
  93. if (mapset == 0 || *mapset == 0)
  94. fprintf(more, _("no %s files available in current mapset\n"),
  95. desc);
  96. else
  97. fprintf(more, _("no %s files available in mapset <%s>\n"),
  98. desc, mapset);
  99. }
  100. fprintf(more, "----------------------------------------------\n");
  101. }
  102. /*
  103. * close the more
  104. */
  105. if (more != stdout)
  106. G_pclose(more);
  107. #ifdef SIGPIPE
  108. signal(SIGPIPE, sigpipe);
  109. #endif
  110. if (hit_return && isatty(1)) {
  111. fprintf(stderr, _("hit RETURN to continue -->"));
  112. while (getchar() != '\n') ;
  113. }
  114. return 0;
  115. }
  116. static void sigpipe_catch(int n)
  117. {
  118. broken_pipe = 1;
  119. signal(n, sigpipe_catch);
  120. }
  121. static int list_element(FILE * out,
  122. const char *element, const char *desc,
  123. const char *mapset, int (*lister) (const char *,
  124. const char *,
  125. const char *))
  126. {
  127. char path[GPATH_MAX];
  128. int count = 0;
  129. char **list;
  130. int i;
  131. /*
  132. * convert . to current mapset
  133. */
  134. if (strcmp(mapset, ".") == 0)
  135. mapset = G_mapset();
  136. /*
  137. * get the full name of the GIS directory within the mapset
  138. * and list its contents (if it exists)
  139. *
  140. * if lister() routine is given, the ls command must give 1 name
  141. */
  142. G__file_name(path, element, "", mapset);
  143. if (access(path, 0) != 0) {
  144. fprintf(out, "\n");
  145. return count;
  146. }
  147. /*
  148. * if a title so that we can call lister() with the names
  149. * otherwise the ls must be forced into columnar form.
  150. */
  151. list = G__ls(path, &count);
  152. if (count > 0) {
  153. fprintf(out, _("%s files available in mapset <%s>:\n"), desc, mapset);
  154. if (lister) {
  155. char title[400];
  156. char name[GNAME_MAX];
  157. *name = *title = 0;
  158. lister(name, mapset, title);
  159. if (*title)
  160. fprintf(out, "\n%-18s %-.60s\n", name, title);
  161. }
  162. }
  163. if (lister) {
  164. for (i = 0; i < count; i++) {
  165. char title[400];
  166. lister(list[i], mapset, title);
  167. fprintf(out, "%-18s %-.60s\n", list[i], title);
  168. }
  169. }
  170. else
  171. G_ls_format(list, count, 0, out);
  172. fprintf(out, "\n");
  173. for (i = 0; i < count; i++)
  174. G_free((char *)list[i]);
  175. if (list)
  176. G_free(list);
  177. return count;
  178. }
  179. /*!
  180. * \brief List specified type of elements. Application must release
  181. the allocated memory.
  182. * \param element Element type (G_ELEMENT_RASTER, G_ELEMENT_VECTOR,
  183. G_ELEMENT_REGION )
  184. * \param gisbase Path to GISBASE
  185. * \param location Location name
  186. * \param mapset Mapset name
  187. * \return Zero terminated array of element names
  188. */
  189. char **G_list(int element, const char *gisbase, const char *location,
  190. const char *mapset)
  191. {
  192. char *el;
  193. char *buf;
  194. DIR *dirp;
  195. struct dirent *dp;
  196. int count;
  197. char **list;
  198. switch (element) {
  199. case G_ELEMENT_RASTER:
  200. el = "cell";
  201. break;
  202. case G_ELEMENT_GROUP:
  203. el = "group";
  204. break;
  205. case G_ELEMENT_VECTOR:
  206. el = "vector";
  207. break;
  208. case G_ELEMENT_REGION:
  209. el = "windows";
  210. break;
  211. default:
  212. G_fatal_error(_("G_list: Unknown element type"));
  213. }
  214. buf = (char *)G_malloc(strlen(gisbase) + strlen(location)
  215. + strlen(mapset) + strlen(el) + 4);
  216. sprintf(buf, "%s/%s/%s/%s", gisbase, location, mapset, el);
  217. dirp = opendir(buf);
  218. G_free(buf);
  219. if (dirp == NULL) { /* this can happen if element does not exist */
  220. list = (char **)G_calloc(1, sizeof(char *));
  221. return list;
  222. }
  223. count = 0;
  224. while ((dp = readdir(dirp)) != NULL) {
  225. if (dp->d_name[0] == '.')
  226. continue;
  227. count++;
  228. }
  229. rewinddir(dirp);
  230. list = (char **)G_calloc(count + 1, sizeof(char *));
  231. count = 0;
  232. while ((dp = readdir(dirp)) != NULL) {
  233. if (dp->d_name[0] == '.')
  234. continue;
  235. list[count] = (char *)G_malloc(strlen(dp->d_name) + 1);
  236. strcpy(list[count], dp->d_name);
  237. count++;
  238. }
  239. closedir(dirp);
  240. return list;
  241. }
  242. /**
  243. \brief Free list
  244. \param list char* array to be freed
  245. \return
  246. */
  247. void G_free_list(char **list)
  248. {
  249. int i = 0;
  250. if (!list)
  251. return;
  252. while (list[i]) {
  253. G_free(list[i]);
  254. i++;
  255. }
  256. G_free(list);
  257. }