]> diplodocus.org Git - nmh/blobdiff - uip/mhshowsbr.c
Removed msh relics from scansbr.c, including a global.
[nmh] / uip / mhshowsbr.c
index 8bd5f99add6d9c10834696226f9bd8c0451e2844..d99f58273a506129db7ae14361bd676bc6d718a2 100644 (file)
@@ -39,11 +39,6 @@ int type_ok (CT, int);
 void content_error (char *, CT, char *, ...);
 void flush_errors (void);
 
-/*
- * prototypes
- */
-int show_content_aux (CT, int, char *, char *, struct format *fmt);
-
 /*
  * static prototypes
  */
@@ -263,8 +258,15 @@ show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
                                          inlineonly, fmt);
 
                case MESSAGE_RFC822:
-               default:
                    return show_message_rfc822 (ct, alternate, fmt);
+
+               /*
+                * Treat unknown message types as equivalent to
+                * application/octet-stream for now
+                */
+               default:
+                   return show_content (ct, alternate, textonly,
+                                        inlineonly, fmt);
            }
 
        case CT_TEXT:
@@ -274,10 +276,8 @@ show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
        case CT_IMAGE:
        case CT_VIDEO:
        case CT_APPLICATION:
-           return show_content (ct, alternate, textonly, inlineonly, fmt);
-
        default:
-           adios (NULL, "unknown content type %d", ct->c_type);
+           return show_content (ct, alternate, textonly, inlineonly, fmt);
     }
 
     return 0;  /* NOT REACHED */
@@ -292,7 +292,7 @@ static int
 show_content (CT ct, int alternate, int textonly, int inlineonly,
              struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
     /*
@@ -305,15 +305,8 @@ show_content (CT ct, int alternate, int textonly, int inlineonly,
        return OK;
     }
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     if ((cp = ct->c_showproc))
@@ -411,7 +404,7 @@ show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
            fprintf (stderr, " using command %s\n", buffer);
     }
 
-    if (xlist) {
+    if (xlist && fmt) {
        output_marker(ct, fmt, 0);
     }
 
@@ -423,6 +416,7 @@ show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
     if (buffer[0] == '\0') {
        char readbuf[BUFSIZ];
        ssize_t cc;
+       char lastchar = '\n';
 
        if (fd == NOTOK) {
            advise(NULL, "Cannot use NULL command to display content-type "
@@ -432,6 +426,7 @@ show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
 
        while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
            fwrite(readbuf, sizeof(char), cc, stdout);
+           lastchar = readbuf[cc - 1];
        }
 
        if (cc < 0) {
@@ -439,6 +434,20 @@ show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
            return NOTOK;
        }
 
+       /*
+        * The MIME standards allow content to not have a trailing newline.
+        * But because we are (presumably) sending this to stdout, include
+        * a newline for text content if the final character was not a
+        * newline.  Only do this for mhshow.
+        */
+
+       if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
+           ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
+           putc('\n', stdout);
+       }
+
+       fflush(stdout);
+
        return OK;
     }
 
@@ -489,15 +498,8 @@ show_text (CT ct, int alternate, int concatsw, struct format *fmt)
     char *cp, buffer[BUFSIZ];
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     /*
@@ -529,18 +531,11 @@ static int
 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
            struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_multi_aux (ct, alternate, cp, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_multi_aux (ct, alternate, cp, fmt);
 
     if ((cp = ct->c_showproc))
@@ -689,18 +684,11 @@ show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
 static int
 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     if ((cp = ct->c_showproc))
@@ -1222,9 +1210,11 @@ convert_content_charset (CT ct, char **file) {
  * Compile our format string and save any parameters we care about.
  */
 
-#define DEFAULT_MARKER "[ part %{part} - %{content-type} - %<{description}" \
-                      "%{description}%?{cdispo-filename}%{cdispo-filename}" \
-                      "%|%{ctype-name}%> %<(unseen)\\(suppressed\\)%> ]"
+#define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
+                      "%<{description}%{description}" \
+                        "%?{cdispo-filename}%{cdispo-filename}" \
+                        "%|%{ctype-name}%>  " \
+                      "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
 
 static struct format *
 compile_marker(char *markerform)
@@ -1287,8 +1277,9 @@ compile_marker(char *markerform)
 static void
 output_marker(CT ct, struct format *fmt, int hidden)
 {
-    char outbuf[BUFSIZ];
+    charstring_t outbuf = charstring_create (BUFSIZ);
     struct param_comp_list *pcentry;
+    int partsize;
     int dat[5];
 
     /*
@@ -1322,15 +1313,23 @@ output_marker(CT ct, struct format *fmt, int hidden)
                                          pcentry->param, '?', 0);
     }
 
+    if (ct->c_cesizefnx)
+       partsize = (*ct->c_cesizefnx) (ct);
+    else
+       partsize = ct->c_end - ct->c_begin;
+
     /* make the part's hidden aspect available by overloading the
-     * %(unseen) function.  see comments in h/fmt_scan.h.
+     * %(unseen) function.  make the part's size available via %(size).
+     * see comments in h/fmt_scan.h.
      */
-    dat[0] = dat[1] = dat[2] = dat[3] = 0;
+    dat[2] = partsize;
     dat[4] = hidden;
+    dat[0] = dat[1] = dat[3] = 0;
 
-    fmt_scan(fmt, outbuf, sizeof(outbuf), sizeof(outbuf), dat, NULL);
+    fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
 
-    fputs(outbuf, stdout);
+    fputs(charstring_buffer (outbuf), stdout);
+    charstring_free (outbuf);
 
     fmt_freecomptext();
 }