00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "gis.h"
00046 #include "glocale.h"
00047 #include <unistd.h>
00048 #include <signal.h>
00049 #include <string.h>
00050
00051 static int broken_pipe;
00052 static int hit_return = 0;
00053 static int list_element(FILE *,char *,char *,char *,int (*)());
00054 static void sigpipe_catch(int);
00055
00056 int G_set_list_hit_return(int flag)
00057 {
00058 hit_return = flag;
00059 return 0;
00060 }
00061
00062 int G_list_element (
00063 char *element,
00064 char *desc,
00065 char *mapset,
00066 int (*lister)())
00067 {
00068 int n;
00069 FILE *more;
00070 int count;
00071 #ifdef SIGPIPE
00072 void (*sigpipe)();
00073 #endif
00074
00075
00076 broken_pipe = 0;
00077 #ifdef SIGPIPE
00078 sigpipe = signal (SIGPIPE, sigpipe_catch);
00079 #endif
00080
00081 count = 0;
00082 if (desc == 0 || *desc == 0)
00083 desc = element;
00084
00085
00086
00087 if (isatty(1))
00088 {
00089 more = G_popen ("$GRASS_PAGER","w");
00090 if (!more) more = stdout;
00091 }
00092 else
00093 more = stdout;
00094 fprintf (more,"----------------------------------------------\n");
00095
00096
00097
00098
00099
00100
00101 if (mapset == 0 || *mapset == 0)
00102 for (n = 0; !broken_pipe && (mapset = G__mapset_name (n)); n++)
00103 count += list_element (more, element, desc, mapset, lister);
00104 else
00105 count += list_element (more, element, desc, mapset, lister);
00106
00107 if (!broken_pipe)
00108 {
00109 if (count == 0){
00110 if (mapset == 0 || *mapset == 0)
00111 fprintf (more,_("no %s files available in current mapset\n"), desc);
00112 else
00113 fprintf (more,_("no %s files available in mapset %s\n"), desc, mapset);
00114 }
00115
00116 fprintf (more,"----------------------------------------------\n");
00117 }
00118
00119
00120
00121 if (more != stdout) G_pclose (more);
00122 #ifdef SIGPIPE
00123 signal (SIGPIPE, sigpipe);
00124 #endif
00125 if (hit_return && isatty(1))
00126 {
00127 fprintf (stderr, _("hit RETURN to continue -->"));
00128 while (getchar() != '\n')
00129 ;
00130 }
00131
00132 return 0;
00133 }
00134
00135 static void sigpipe_catch(int n)
00136 {
00137 broken_pipe = 1;
00138 signal (n,sigpipe_catch);
00139 }
00140
00141 static int list_element( FILE *out, char *element,
00142 char *desc, char *mapset, int (*lister)())
00143 {
00144 char path[1000];
00145 char buf[400];
00146 FILE *ls;
00147 FILE *G_popen();
00148 int count;
00149
00150 count = 0;
00151
00152
00153
00154 if (strcmp (mapset,".") == 0)
00155 mapset = G_mapset();
00156
00157
00158
00159
00160
00161
00162
00163
00164 G__file_name (path, element, "", mapset);
00165 if(access(path, 0) == 0)
00166 {
00167
00168
00169
00170
00171 if (lister)
00172 sprintf(buf,"ls '%s'", path);
00173 else
00174 sprintf(buf,"ls -C '%s'", path);
00175
00176 if ((ls = G_popen(buf,"r")))
00177 {
00178 while (!broken_pipe && fgets(buf, sizeof buf, ls))
00179 {
00180 if (count++ == 0)
00181 {
00182 fprintf(out, _("%s files available in mapset %s:\n"), desc, mapset);
00183 if (lister)
00184 {
00185 char title[400];
00186 char name[30];
00187
00188 *name = *title = 0;
00189 lister (name, mapset, title);
00190 if (*title)
00191 fprintf(out,"\n%-18s %-.60s\n",name,title);
00192 }
00193 }
00194 if (lister)
00195 {
00196 char *b;
00197 char title[400];
00198
00199
00200 for (b = buf; *b; b++)
00201 if (*b == '\n')
00202 *b = 0;
00203
00204 lister (buf, mapset, title);
00205 fprintf(out,"%-18s %-.60s\n",buf,title);
00206 }
00207 else
00208 fprintf(out,"%s", buf);
00209 }
00210 G_pclose (ls);
00211 }
00212 }
00213 if (!broken_pipe && (count > 0))
00214 fprintf(out,"\n");
00215 return count;
00216 }