]> diplodocus.org Git - nmh/blobdiff - uip/folder.c
Removed "true" from end of flex fixups. I don't know why it was
[nmh] / uip / folder.c
index a94f1315ff94a83c5e9fe702280f2ced870f87f4..98ecde68d70f27ac102083dfb6012e8e04a799a7 100644 (file)
@@ -4,68 +4,48 @@
  *             -- push/pop a folder onto/from the folder stack
  *             -- list the folder stack
  *
  *             -- push/pop a folder onto/from the folder stack
  *             -- list the folder stack
  *
- * $Id$
- *
- * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * This code is Copyright (c) 2002, 2008, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/crawl_folders.h>
 #include <h/utils.h>
 #include <h/utils.h>
-#include <errno.h>
-
-static struct swit switches[] = {
-#define        ALLSW           0
-    { "all", 0 },
-#define NALLSW         1
-    { "noall", 0 },
-#define        CREATSW         2
-    { "create", 0 },
-#define        NCREATSW        3
-    { "nocreate", 0 },
-#define        FASTSW          4
-    { "fast", 0 },
-#define        NFASTSW         5
-    { "nofast", 0 },
-#define        HDRSW           6
-    { "header", 0 },
-#define        NHDRSW          7
-    { "noheader", 0 },
-#define        PACKSW          8
-    { "pack", 0 },
-#define        NPACKSW         9
-    { "nopack", 0 },
-#define        VERBSW         10
-    { "verbose", 0 },
-#define        NVERBSW        11
-    { "noverbose", 0 },
-#define        RECURSW        12
-    { "recurse", 0 },
-#define        NRECRSW        13
-    { "norecurse", 0 },
-#define        TOTALSW        14
-    { "total", 0 },
-#define        NTOTLSW        15
-    { "nototal", 0 },
-#define        LISTSW         16
-    { "list", 0 },
-#define        NLISTSW        17
-    { "nolist", 0 },
-#define        PRNTSW         18
-    { "print", 0 },
-#define        NPRNTSW        19
-    { "noprint", -4 },
-#define        PUSHSW         20
-    { "push", 0 },
-#define        POPSW          21
-    { "pop", 0 },
-#define VERSIONSW      22
-    { "version", 0 },
-#define        HELPSW         23
-    { "help", 0 },
-    { NULL, 0 }
-};
+
+#define FOLDER_SWITCHES \
+    X("all", 0, ALLSW) \
+    X("noall", 0, NALLSW) \
+    X("create", 0, CREATSW) \
+    X("nocreate", 0, NCREATSW) \
+    X("fast", 0, FASTSW) \
+    X("nofast", 0, NFASTSW) \
+    X("header", 0, HDRSW) \
+    X("noheader", 0, NHDRSW) \
+    X("pack", 0, PACKSW) \
+    X("nopack", 0, NPACKSW) \
+    X("verbose", 0, VERBSW) \
+    X("noverbose", 0, NVERBSW) \
+    X("recurse", 0, RECURSW) \
+    X("norecurse", 0, NRECRSW) \
+    X("total", 0, TOTALSW) \
+    X("nototal", 0, NTOTLSW) \
+    X("list", 0, LISTSW) \
+    X("nolist", 0, NLISTSW) \
+    X("print", 0, PRNTSW) \
+    X("noprint", -4, NPRNTSW) \
+    X("push", 0, PUSHSW) \
+    X("pop", 0, POPSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(FOLDER);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(FOLDER, switches);
+#undef X
 
 static int fshort   = 0;       /* output only folder names                 */
 static int fcreat   = 0;       /* should we ask to create new folders?     */
 
 static int fshort   = 0;       /* output only folder names                 */
 static int fcreat   = 0;       /* should we ask to create new folders?     */
@@ -78,22 +58,10 @@ static int all      = 0;    /* should we output all folders             */
 
 static int total_folders = 0;  /* total number of folders                  */
 
 
 static int total_folders = 0;  /* total number of folders                  */
 
-static int start = 0;
-static int foldp = 0;
-
 static char *nmhdir;
 static char *stack = "Folder-Stack";
 static char folder[BUFSIZ];
 
 static char *nmhdir;
 static char *stack = "Folder-Stack";
 static char folder[BUFSIZ];
 
-#define NUMFOLDERS 100
-
-/*
- * This is how many folders we currently can hold in the array
- * `folds'.  We increase this amount by NUMFOLDERS at a time.
- */
-static int maxfolders;
-static char **folds;
-
 /*
  * Structure to hold information about
  * folders as we scan them.
 /*
  * Structure to hold information about
  * folders as we scan them.
@@ -118,13 +86,10 @@ static int maxFolderInfo;
 /*
  * static prototypes
  */
 /*
  * static prototypes
  */
-static void dodir (char *);
 static int get_folder_info (char *, char *);
 static int get_folder_info (char *, char *);
+static crawl_callback_t get_folder_info_callback;
 static void print_folders (void);
 static int sfold (struct msgs *, char *);
 static void print_folders (void);
 static int sfold (struct msgs *, char *);
-static void addir (char *);
-static void addfold (char *);
-static int compare (char *, char *);
 static void readonly_folders (void);
 
 
 static void readonly_folders (void);
 
 
@@ -136,13 +101,7 @@ main (int argc, char **argv)
     char *cp, *dp, *msg = NULL, *argfolder = NULL;
     char **ap, **argp, buf[BUFSIZ], **arguments;
 
     char *cp, *dp, *msg = NULL, *argfolder = NULL;
     char **ap, **argp, buf[BUFSIZ], **arguments;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 1)) { return 1; }
 
     /*
      * If program was invoked with name ending
 
     /*
      * If program was invoked with name ending
@@ -167,10 +126,10 @@ main (int argc, char **argv)
                    snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
                        invo_name);
                    print_help (buf, switches, 1);
                    snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
                        invo_name);
                    print_help (buf, switches, 1);
-                   done (1);
+                   done (0);
                case VERSIONSW:
                    print_version(invo_name);
                case VERSIONSW:
                    print_version(invo_name);
-                   done (1);
+                   done (0);
 
                case ALLSW: 
                    all = 1;
 
                case ALLSW: 
                    all = 1;
@@ -350,30 +309,26 @@ main (int argc, char **argv)
            done (0);
     }
 
            done (0);
     }
 
-    /* Allocate initial space to record folder names */
-    maxfolders = NUMFOLDERS;
-    folds = mh_xmalloc (maxfolders * sizeof(char *));
-
     /* Allocate initial space to record folder information */
     /* Allocate initial space to record folder information */
-    maxFolderInfo = NUMFOLDERS;
+    maxFolderInfo = CRAWL_NUMFOLDERS;
     fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
 
     /*
      * Scan the folders
      */
     fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
 
     /*
      * Scan the folders
      */
+    /* change directory to base of nmh directory for crawl_folders */
+    if (chdir (nmhdir) == NOTOK)
+       adios (nmhdir, "unable to change directory to");
     if (all || ftotal > 0) {
        /*
         * If no folder is given, do them all
         */
     if (all || ftotal > 0) {
        /*
         * If no folder is given, do them all
         */
-       /* change directory to base of nmh directory for dodir */
-       if (chdir (nmhdir) == NOTOK)
-           adios (nmhdir, "unable to change directory to");
        if (!argfolder) {
            if (msg)
                admonish (NULL, "no folder given for message %s", msg);
            readonly_folders (); /* do any readonly folders */
            strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
        if (!argfolder) {
            if (msg)
                admonish (NULL, "no folder given for message %s", msg);
            readonly_folders (); /* do any readonly folders */
            strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
-           dodir (".");
+           crawl_folders (".", get_folder_info_callback, NULL);
        } else {
            strncpy (folder, argfolder, sizeof(folder));
            if (get_folder_info (argfolder, msg)) {
        } else {
            strncpy (folder, argfolder, sizeof(folder));
            if (get_folder_info (argfolder, msg)) {
@@ -385,7 +340,7 @@ main (int argc, char **argv)
             * we still need to list all level-1 sub-folders.
             */
            if (!frecurse)
             * we still need to list all level-1 sub-folders.
             */
            if (!frecurse)
-               dodir (folder);
+               crawl_folders (folder, get_folder_info_callback, NULL);
        }
     } else {
        strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
        }
     } else {
        strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
@@ -412,32 +367,8 @@ main (int argc, char **argv)
     return 1;
 }
 
     return 1;
 }
 
-/*
- * Base routine for scanning a folder
- */
-
-static void
-dodir (char *dir)
-{
-    int i;
-    int os = start;
-    int of = foldp;
-
-    start = foldp;
-
-    addir (dir);
-
-    for (i = start; i < foldp; i++) {
-       get_folder_info (folds[i], NULL);
-       fflush (stdout);
-    }
-
-    start = os;
-    foldp = of;
-}
-
 static int
 static int
-get_folder_info (char *fold, char *msg)
+get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
 {
     int        i, retval = 1;
     struct msgs *mp = NULL;
 {
     int        i, retval = 1;
     struct msgs *mp = NULL;
@@ -449,7 +380,7 @@ get_folder_info (char *fold, char *msg)
      * for folder information
      */
     if (total_folders >= maxFolderInfo) {
      * for folder information
      */
     if (total_folders >= maxFolderInfo) {
-       maxFolderInfo += NUMFOLDERS;
+       maxFolderInfo += CRAWL_NUMFOLDERS;
        fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
     }
 
        fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
     }
 
@@ -465,7 +396,7 @@ get_folder_info (char *fold, char *msg)
        /*
         * create message structure and get folder info
         */
        /*
         * create message structure and get folder info
         */
-       if (!(mp = folder_read (fold))) {
+       if (!(mp = folder_read (fold, 1))) {
            admonish (NULL, "unable to read folder %s", fold);
            return 0;
        }
            admonish (NULL, "unable to read folder %s", fold);
            return 0;
        }
@@ -493,8 +424,34 @@ get_folder_info (char *fold, char *msg)
        folder_free (mp); /* free folder/message structure */
     }
 
        folder_free (mp); /* free folder/message structure */
     }
 
-    if (frecurse && (fshort || fi[i].others) && (fi[i].error == 0))
-       dodir (fold);
+    *crawl_children = (frecurse && (fshort || fi[i].others)
+                      && (fi[i].error == 0));
+    return retval;
+}
+
+static boolean
+get_folder_info_callback (char *fold, void *baton)
+{
+    boolean crawl_children;
+    NMH_UNUSED (baton);
+
+    get_folder_info_body (fold, NULL, &crawl_children);
+    fflush (stdout);
+    return crawl_children;
+}
+
+static int
+get_folder_info (char *fold, char *msg)
+{
+    boolean crawl_children;
+    int retval;
+
+    retval = get_folder_info_body (fold, msg, &crawl_children);
+
+    if (crawl_children) {
+       crawl_folders (fold, get_folder_info_callback, NULL);
+    }
+
     return retval;
 }
 
     return retval;
 }
 
@@ -654,99 +611,6 @@ sfold (struct msgs *mp, char *msg)
 }
 
 
 }
 
 
-static void
-addir (char *name)
-{
-    char *prefix, *child;
-    struct stat st;
-    struct dirent *dp;
-    DIR * dd;
-    int child_is_folder;
-
-    if (!(dd = opendir (name))) {
-       admonish (name, "unable to read directory ");
-       return;
-    }
-
-    if (strcmp (name, ".") == 0) {
-       prefix = getcpy ("");
-    } else {
-       prefix = concat (name, "/", (void *)NULL);
-    }
-
-    while ((dp = readdir (dd))) {
-       /* If the system supports it, try to skip processing of children we
-        * know are not directories or symlinks. */
-       child_is_folder = -1;
-#if defined(HAVE_STRUCT_DIRENT_D_TYPE)
-       if (dp->d_type == DT_DIR) {
-           child_is_folder = 1;
-       } else if (dp->d_type != DT_LNK && dp->d_type != DT_UNKNOWN) {
-           continue;
-       }
-#endif
-       if (!strcmp (dp->d_name, ".") || !strcmp (dp->d_name, "..")) {
-           continue;
-       }
-       child = concat (prefix, dp->d_name, (void *)NULL);
-       /* If we have no d_type or d_type is DT_LNK or DT_UNKNOWN, stat the
-        * child to see what it is. */
-       if (child_is_folder == -1) {
-           child_is_folder = (stat (child, &st) != -1 && S_ISDIR(st.st_mode));
-       }
-       if (child_is_folder) {
-           /* addfold saves child in the list, don't free it */
-           addfold (child);
-       } else {
-           free (child);
-       }
-    }
-
-    closedir (dd);
-    free(prefix);
-}
-
-/*
- * Add the folder name into the
- * list in a sorted fashion.
- */
-
-static void
-addfold (char *fold)
-{
-    register int i, j;
-
-    /* if necessary, reallocate the space for folder names */
-    if (foldp >= maxfolders) {
-       maxfolders += NUMFOLDERS;
-       folds = mh_xrealloc (folds, maxfolders * sizeof(char *));
-    }
-
-    for (i = start; i < foldp; i++)
-       if (compare (fold, folds[i]) < 0) {
-           for (j = foldp - 1; j >= i; j--)
-               folds[j + 1] = folds[j];
-           foldp++;
-           folds[i] = fold;
-           return;
-       }
-
-    folds[foldp++] = fold;
-}
-
-
-static int
-compare (char *s1, char *s2)
-{
-    register int i;
-
-    while (*s1 || *s2)
-       if ((i = *s1++ - *s2++))
-           return i;
-
-    return 0;
-}
-
 /*
  * Do the read only folders
  */
 /*
  * Do the read only folders
  */