list.c

Go to the documentation of this file.
00001 /*
00002  * $Id: list.c,v 2.2 2004/12/13 08:54:43 markus Exp $
00003  *
00004  ****************************************************************************
00005  *
00006  * MODULE:       GRASS 5 gis library, list.c
00007  * AUTHOR(S):    unknown
00008  * PURPOSE:      list elements
00009  * COPYRIGHT:    (C) 2000 by the GRASS Development Team
00010  *
00011  *               This program is free software under the GNU General Public
00012  *               License (>=v2). Read the file COPYING that comes with GRASS
00013  *               for details.
00014  *
00015  *****************************************************************************/
00016 
00017 /**********************************************************************
00018  *  G_list_element (element, desc, mapset, lister)
00019  *
00020  *      char *element    database element (eg, "cell", "cellhd", etc)
00021  *      char *desc       desc for element (if NULL, element is used)
00022  *      char *mapset     mapset to be listed.
00023  *                       "" to list all mapsets in mapset search list
00024  *                       "." will list current mapset
00025  *
00026  *      int (*lister)()  if given will call this routine to get a list
00027  *                       title. NULL if no titles desired.
00028  *
00029  *                       lister (name, mapset, buf)
00030  *                       char *name, *mapset, *buf
00031  *
00032  *                       given file 'name', and 'mapset',
00033  *                       lister() should copy a string into 'buf'
00034  *
00035  *                       when called with name == "", should set buf
00036  *                       to general title for mapset list.
00037  *
00038  *   General purpose list function. Will list files from all mapsets
00039  *   in the mapset list for a specified database element.
00040  *
00041  *   note:
00042  *      output is to stdout piped thru the more utility
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 /* must catch broken pipe in case "more" quits */
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  * G_popen() the more command to page the output
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  * if no specific mapset is requested, list the mapsets
00098  * from the mapset search list
00099  * otherwise just list the specified mapset
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  * close the more
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  * convert . to current mapset
00153  */
00154     if (strcmp (mapset,".") == 0)
00155         mapset = G_mapset();
00156 
00157 
00158 /*
00159  * get the full name of the GIS directory within the mapset
00160  * and list its contents (if it exists)
00161  *
00162  * if lister() routine is given, the ls command must give 1 name
00163  */
00164     G__file_name (path, element, "", mapset);
00165     if(access(path, 0) == 0)
00166     {
00167 /*
00168  * if a title so that we can call lister() with the names
00169  * otherwise the ls must be forced into columnar form.
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                 /* remove the trailing newline */
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 }

Generated on Sat Jul 22 22:06:15 2006 for GRASS by  doxygen 1.4.7