]> diplodocus.org Git - nmh/blobdiff - uip/mhmisc.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / mhmisc.c
index a3da759fcd7804408402bf2c82dcff6fe248c71c..acaae82ab0b54bf6702b3987c04ef8104717aa09 100644 (file)
@@ -1,8 +1,4 @@
-
-/*
- * mhparse.c -- misc routines to process MIME messages
- *
- * $Id$
+/* mhmisc.c -- misc routines to process MIME messages
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
@@ -10,9 +6,11 @@
  */
 
 #include <h/mh.h>
-#include <errno.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
+#include <h/utils.h>
+#include "sbr/makedir.h"
+#include "mhmisc.h"
 
 extern int debugsw;
 
@@ -24,37 +22,52 @@ int ntype = 0;
 char *parts[NPARTS + 1];
 char *types[NTYPES + 1];
 
-int endian = 0;                /* little or big endian */
 int userrs = 0;
 
 static char *errs = NULL;
 
 
-/*
- * prototypes
- */
-int part_ok (CT, int);
-int type_ok (CT, int);
-void set_endian (void);
-int make_intermediates (char *);
-void content_error (char *, CT, char *, ...);
-void flush_errors (void);
-
-
 int
-part_ok (CT ct, int sP)
+part_ok (CT ct)
 {
     char **ap;
     int len;
 
-    if (npart == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
+    /* a part is "ok", i.e., should be processed, if:
+       - there were no -part arguments
+       - this part is a multipart
+     */
+    if (npart == 0 || ct->c_type == CT_MULTIPART) {
        return 1;
+    }
 
+    /* or if:
+       - this part is a an exact match for any -part option
+       - this part is a sub-part of any -part option
+     */
     for (ap = parts; *ap; ap++) {
         len = strlen(*ap);
         if (!strncmp (*ap, ct->c_partno, len) &&
-                (!ct->c_partno[len] || ct->c_partno[len] == '.' ))
+                (!ct->c_partno[len] || ct->c_partno[len] == '.' )) {
+            return 1;
+       }
+    }
+
+    return 0;
+}
+
+int
+part_exact(CT ct)
+{
+    char **ap;
+
+    if (!ct->c_partno)
+       return 0;
+
+    for (ap = parts; *ap; ap++) {
+        if (!strcmp (*ap, ct->c_partno)) {
             return 1;
+       }
     }
 
     return 0;
@@ -80,21 +93,27 @@ type_ok (CT ct, int sP)
 }
 
 
-void
-set_endian (void)
+/*
+ * Returns true if the content has a disposition of "inline".
+ *
+ * Technically we should check parent content to see if they have
+ * disposition to use as a default, but we don't right now.  Maybe
+ * later ....
+ */
+
+int
+is_inline(CT ct)
 {
-    union {
-       long l;
-       char c[sizeof(long)];
-    } un;
-
-    un.l = 1;
-    endian = un.c[0] ? -1 : 1;
-    if (debugsw)
-       fprintf (stderr, "%s endian architecture\n",
-               endian > 0 ? "big" : "little");
-}
+    /*
+     * If there isn't any disposition at all, it's "inline".  Obviously
+     * if it's "inline", then it's inline.  RFC 2183 says if it's an unknown
+     * disposition, treat it as 'attachment'.
+     */
 
+    if (! ct->c_dispo_type || strcasecmp(ct->c_dispo_type, "inline") == 0)
+       return 1;
+    return 0;
+}
 
 int
 make_intermediates (char *file)
@@ -116,13 +135,13 @@ losing_directory:
            }
 
            ep = concat ("Create directory \"", file, "\"? ", NULL);
-           answer = getanswer (ep);
+           answer = read_yes_or_no_if_tty (ep);
            free (ep);
 
            if (!answer)
                goto losing_directory;
            if (!makedir (file)) {
-               advise (NULL, "unable to create directory %s", file);
+               inform("unable to create directory %s", file);
                goto losing_directory;
            }
        }
@@ -142,7 +161,7 @@ void
 content_error (char *what, CT ct, char *fmt, ...)
 {
     va_list arglist;
-    int        i, len, buflen;
+    int        len, buflen;
     char *bp, buffer[BUFSIZ];
     CI ci;
 
@@ -185,10 +204,8 @@ content_error (char *what, CT ct, char *fmt, ...)
        buflen -= len;
     }
 
-    i = strlen (invo_name) + 2;
-
     /* Now add content type and subtype */
-    snprintf (bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
+    snprintf (bp, buflen, "\n    (content %s/%s",
        ci->ci_type, ci->ci_subtype);
     len = strlen (bp);
     bp += len;
@@ -221,8 +238,10 @@ content_error (char *what, CT ct, char *fmt, ...)
 
        errs = add (buffer, errs);
     } else {
-       advise (NULL, "%s", buffer);
+       inform("%s", buffer);
     }
+
+    va_end(arglist);
 }
 
 
@@ -231,7 +250,7 @@ flush_errors (void)
 {
     if (errs) {
        fflush (stdout);
-       fprintf (stderr, "%s", errs);
+       fputs(errs, stderr);
        free (errs);
        errs = NULL;
     }