2 * Copyright (C) 1996-2000,2007 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2008 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_curses.h"
35 #include "mutt_crypt.h"
46 #include <sys/types.h>
49 BODY *mutt_new_body (void)
51 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
53 p->disposition = DISPATTACH;
59 /* Modified by blong to accept a "suggestion" for file name. If
60 * that file exists, then construct one with unique name but
61 * keep any extension. This might fail, I guess.
62 * Renamed to mutt_adv_mktemp so I only have to change where it's
63 * called, and not all possible cases.
65 void mutt_adv_mktemp (char *s, size_t l)
67 char buf[_POSIX_PATH_MAX];
68 char tmp[_POSIX_PATH_MAX];
73 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
74 mutt_expand_path (buf, sizeof (buf));
77 snprintf (s, l, "%s/muttXXXXXX", buf);
82 strfcpy (tmp, s, sizeof (tmp));
83 mutt_sanitize_filename (tmp, 1);
84 snprintf (s, l, "%s/%s", buf, tmp);
85 if (lstat (s, &sb) == -1 && errno == ENOENT)
87 if ((period = strrchr (tmp, '.')) != NULL)
89 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
95 strfcpy(s + sl, period, l - sl);
100 /* create a send-mode duplicate from a receive-mode body */
102 int mutt_copy_body (FILE *fp, BODY **tgt, BODY *src)
104 char tmp[_POSIX_PATH_MAX];
107 PARAMETER *par, **ppar;
114 strfcpy (tmp, src->filename, sizeof (tmp));
122 mutt_adv_mktemp (tmp, sizeof (tmp));
123 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
126 *tgt = mutt_new_body ();
129 memcpy (b, src, sizeof (BODY));
133 b->filename = safe_strdup (tmp);
134 b->use_disp = use_disp;
137 if (mutt_is_text_part (b))
140 b->xtype = safe_strdup (b->xtype);
141 b->subtype = safe_strdup (b->subtype);
142 b->form_name = safe_strdup (b->form_name);
143 b->filename = safe_strdup (b->filename);
144 b->d_filename = safe_strdup (b->d_filename);
145 b->description = safe_strdup (b->description);
148 * we don't seem to need the HEADER structure currently.
149 * XXX - this may change in the future
152 if (b->hdr) b->hdr = NULL;
154 /* copy parameters */
155 for (par = b->parameter, ppar = &b->parameter; par; ppar = &(*ppar)->next, par = par->next)
157 *ppar = mutt_new_parameter ();
158 (*ppar)->attribute = safe_strdup (par->attribute);
159 (*ppar)->value = safe_strdup (par->value);
162 mutt_stamp_attachment (b);
169 void mutt_free_body (BODY **p)
179 mutt_free_parameter (&b->parameter);
180 if (b->unlink && b->filename)
182 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
183 unlink (b->filename);
185 else if (b->filename)
186 dprint (1, (debugfile, "mutt_free_body: Not unlinking %s.\n", b->filename));
192 FREE (&b->description);
193 FREE (&b->form_name);
197 /* Don't free twice (b->hdr->content = b->parts) */
198 b->hdr->content = NULL;
199 mutt_free_header(&b->hdr);
203 mutt_free_body (&b->parts);
211 void mutt_free_parameter (PARAMETER **p)
218 FREE (&t->attribute);
227 LIST *mutt_add_list (LIST *head, const char *data)
229 size_t len = mutt_strlen (data);
231 return mutt_add_list_n (head, data, len ? len + 1 : 0);
234 LIST *mutt_add_list_n (LIST *head, const void *data, size_t len)
238 for (tmp = head; tmp && tmp->next; tmp = tmp->next)
242 tmp->next = safe_malloc (sizeof (LIST));
246 head = tmp = safe_malloc (sizeof (LIST));
248 tmp->data = safe_malloc (len);
250 memcpy (tmp->data, data, len);
255 void mutt_free_list (LIST **list)
263 *list = (*list)->next;
269 HEADER *mutt_dup_header(HEADER *h)
273 hnew = mutt_new_header();
274 memcpy(hnew, h, sizeof (HEADER));
278 void mutt_free_header (HEADER **h)
280 if(!h || !*h) return;
281 mutt_free_envelope (&(*h)->env);
282 mutt_free_body (&(*h)->content);
283 FREE (&(*h)->maildir_flags);
287 mutt_free_list (&(*h)->chain);
289 #if defined USE_POP || defined USE_IMAP
292 FREE (h); /* __FREE_CHECKED__ */
295 /* returns true if the header contained in "s" is in list "t" */
296 int mutt_matches_ignore (const char *s, LIST *t)
298 for (; t; t = t->next)
300 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
306 /* prepend the path part of *path to *link */
307 void mutt_expand_link (char *newpath, const char *path, const char *link)
309 const char *lb = NULL;
312 /* link is full path */
315 strfcpy (newpath, link, _POSIX_PATH_MAX);
319 if ((lb = strrchr (path, '/')) == NULL)
321 /* no path in link */
322 strfcpy (newpath, link, _POSIX_PATH_MAX);
327 memcpy (newpath, path, len);
328 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
331 char *mutt_expand_path (char *s, size_t slen)
333 return _mutt_expand_path (s, slen, 0);
336 char *_mutt_expand_path (char *s, size_t slen, int rx)
338 char p[_POSIX_PATH_MAX] = "";
339 char q[_POSIX_PATH_MAX] = "";
340 char tmp[_POSIX_PATH_MAX];
355 if (*(s + 1) == '/' || *(s + 1) == 0)
357 strfcpy (p, NONULL(Homedir), sizeof (p));
363 if ((t = strchr (s + 1, '/')))
366 if ((pw = getpwnam (s + 1)))
368 strfcpy (p, pw->pw_dir, sizeof (p));
379 /* user not found! */
393 /* if folder = {host} or imap[s]://host/: don't append slash */
394 if (mx_is_imap (NONULL (Maildir)) &&
395 (Maildir[strlen (Maildir) - 1] == '}' ||
396 Maildir[strlen (Maildir) - 1] == '/'))
397 strfcpy (p, NONULL (Maildir), sizeof (p));
400 if (Maildir && *Maildir && Maildir[strlen (Maildir) - 1] == '/')
401 strfcpy (p, NONULL (Maildir), sizeof (p));
403 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
409 /* elm compatibility, @ expands alias to user name */
416 if ((alias = mutt_lookup_alias (s + 1)))
418 h = mutt_new_header();
419 h->env = mutt_new_envelope();
420 h->env->from = h->env->to = alias;
421 mutt_default_save (p, sizeof (p), h);
422 h->env->from = h->env->to = NULL;
423 mutt_free_header (&h);
424 /* Avoid infinite recursion if the resulting folder starts with '@' */
435 strfcpy (p, NONULL(Inbox), sizeof (p));
442 strfcpy (p, NONULL(Outbox), sizeof (p));
451 strfcpy (p, NONULL(LastFolder), sizeof (p));
456 strfcpy (p, NONULL(Spoolfile), sizeof (p));
464 strfcpy (p, NONULL(LastFolder), sizeof (p));
471 strfcpy (p, NONULL(CurrentFolder), sizeof (p));
483 if (rx && *p && !recurse)
485 mutt_rx_sanitize_string (q, sizeof (q), p);
486 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
489 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
491 strfcpy (s, tmp, slen);
496 /* Rewrite IMAP path in canonical form - aids in string comparisons of
497 * folders. May possibly fail, in which case s should be the same. */
499 imap_expand_path (s, slen);
505 /* Extract the real name from /etc/passwd's GECOS field.
506 * When set, honor the regular expression in GecosMask,
507 * otherwise assume that the GECOS field is a
508 * comma-separated list.
509 * Replace "&" by a capitalized version of the user's login
513 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
515 regmatch_t pat_match[1];
520 if (!pw || !pw->pw_gecos)
523 memset (dest, 0, destlen);
527 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
528 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
529 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
531 else if ((p = strchr (pw->pw_gecos, ',')))
532 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
534 strfcpy (dest, pw->pw_gecos, destlen);
536 pwnl = strlen (pw->pw_name);
538 for (idx = 0; dest[idx]; idx++)
540 if (dest[idx] == '&')
542 memmove (&dest[idx + pwnl], &dest[idx + 1],
543 MAX((ssize_t)(destlen - idx - pwnl - 1), 0));
544 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
545 dest[idx] = toupper ((unsigned char) dest[idx]);
553 char *mutt_get_parameter (const char *s, PARAMETER *p)
555 for (; p; p = p->next)
556 if (ascii_strcasecmp (s, p->attribute) == 0)
562 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
568 mutt_delete_parameter (attribute, p);
572 for(q = *p; q; q = q->next)
574 if (ascii_strcasecmp (attribute, q->attribute) == 0)
576 mutt_str_replace (&q->value, value);
581 q = mutt_new_parameter();
582 q->attribute = safe_strdup(attribute);
583 q->value = safe_strdup(value);
588 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
592 for (q = *p; q; p = &q->next, q = q->next)
594 if (ascii_strcasecmp (attribute, q->attribute) == 0)
598 mutt_free_parameter (&q);
604 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
605 int mutt_needs_mailcap (BODY *m)
611 if (!ascii_strcasecmp ("plain", m->subtype) ||
612 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
613 !ascii_strcasecmp ("enriched", m->subtype))
617 case TYPEAPPLICATION:
618 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
620 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
632 int mutt_is_text_part (BODY *b)
635 char *s = b->subtype;
637 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
643 if (t == TYPEMESSAGE)
645 if (!ascii_strcasecmp ("delivery-status", s))
649 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
651 if (!ascii_strcasecmp ("pgp-keys", s))
658 void mutt_free_envelope (ENVELOPE **p)
661 rfc822_free_address (&(*p)->return_path);
662 rfc822_free_address (&(*p)->from);
663 rfc822_free_address (&(*p)->to);
664 rfc822_free_address (&(*p)->cc);
665 rfc822_free_address (&(*p)->bcc);
666 rfc822_free_address (&(*p)->sender);
667 rfc822_free_address (&(*p)->reply_to);
668 rfc822_free_address (&(*p)->mail_followup_to);
670 FREE (&(*p)->list_post);
671 FREE (&(*p)->subject);
672 /* real_subj is just an offset to subject and shouldn't be freed */
673 FREE (&(*p)->message_id);
674 FREE (&(*p)->supersedes);
676 FREE (&(*p)->x_label);
678 mutt_buffer_free (&(*p)->spam);
680 mutt_free_list (&(*p)->references);
681 mutt_free_list (&(*p)->in_reply_to);
682 mutt_free_list (&(*p)->userhdrs);
683 FREE (p); /* __FREE_CHECKED__ */
686 /* move all the headers from extra not present in base into base */
687 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
689 /* copies each existing element if necessary, and sets the element
690 * to NULL in the source so that mutt_free_envelope doesn't leave us
691 * with dangling pointers. */
692 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
693 MOVE_ELEM(return_path);
700 MOVE_ELEM(mail_followup_to);
701 MOVE_ELEM(list_post);
702 MOVE_ELEM(message_id);
703 MOVE_ELEM(supersedes);
706 if (!base->refs_changed)
708 MOVE_ELEM(references);
710 if (!base->irt_changed)
712 MOVE_ELEM(in_reply_to);
715 /* real_subj is subordinate to subject */
718 base->subject = (*extra)->subject;
719 base->real_subj = (*extra)->real_subj;
720 (*extra)->subject = NULL;
721 (*extra)->real_subj = NULL;
723 /* spam and user headers should never be hashed, and the new envelope may
724 * have better values. Use new versions regardless. */
725 mutt_buffer_free (&base->spam);
726 mutt_free_list (&base->userhdrs);
731 mutt_free_envelope(extra);
734 void _mutt_mktemp (char *s, const char *src, int line)
736 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
737 dprint (3, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
741 void mutt_free_alias (ALIAS **p)
749 mutt_alias_delete_reverse (t);
751 rfc822_free_address (&t->addr);
756 /* collapse the pathname using ~ or = when possible */
757 void mutt_pretty_mailbox (char *s, size_t buflen)
762 char tmp[_POSIX_PATH_MAX];
764 scheme = url_check_scheme (s);
767 if (scheme == U_IMAP || scheme == U_IMAPS)
769 imap_pretty_mailbox (s);
774 /* if s is an url, only collapse path component */
775 if (scheme != U_UNKNOWN)
777 p = strchr(s, ':')+1;
778 if (!strncmp (p, "//", 2))
779 q = strchr (p+2, '/');
781 q = strchr (p, '\0');
786 if (strstr (p, "//") || strstr (p, "/./"))
788 /* first attempt to collapse the pathname, this is more
789 * lightweight than realpath() and doesn't resolve links
793 if (*p == '/' && p[1] == '/')
798 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
808 else if (strstr (p, "..") &&
809 (scheme == U_UNKNOWN || scheme == U_FILE) &&
811 strfcpy (p, tmp, buflen - (p - s));
813 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
817 memmove (s, s + len, mutt_strlen (s + len) + 1);
819 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
823 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
827 void mutt_pretty_size (char *s, size_t len, LOFF_T n)
830 strfcpy (s, "0K", len);
831 else if (n < 10189) /* 0.1K - 9.9K */
832 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
833 else if (n < 1023949) /* 10K - 999K */
835 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
836 snprintf (s, len, OFF_T_FMT "K", (n + 51) / 1024);
838 else if (n < 10433332) /* 1.0M - 9.9M */
839 snprintf (s, len, "%3.1fM", n / 1048576.0);
842 /* (10433332 + 52428) / 1048576 = 10 */
843 snprintf (s, len, OFF_T_FMT "M", (n + 52428) / 1048576);
847 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
849 char tmp[LONG_STRING];
851 mutt_quote_filename (tmp, sizeof (tmp), src);
852 mutt_expand_fmt (dest, destlen, fmt, tmp);
855 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
862 slen = mutt_strlen (src);
865 for (p = fmt, d = dest; destlen && *p; p++)
877 strfcpy (d, src, destlen + 1);
878 d += destlen > slen ? slen : destlen;
879 destlen -= destlen > slen ? slen : destlen;
897 if (!found && destlen > 0)
899 safe_strcat (dest, destlen, " ");
900 safe_strcat (dest, destlen, src);
905 /* return 0 on success, -1 on abort, 1 on error */
906 int mutt_check_overwrite (const char *attname, const char *path,
907 char *fname, size_t flen, int *append, char **directory)
910 char tmp[_POSIX_PATH_MAX];
913 strfcpy (fname, path, flen);
914 if (access (fname, F_OK) != 0)
916 if (stat (fname, &st) != 0)
918 if (S_ISDIR (st.st_mode))
922 switch (mutt_multi_choice
923 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
926 mutt_str_replace (directory, fname);
929 FREE (directory); /* __FREE_CHECKED__ */
932 FREE (directory); /* __FREE_CHECKED__ */
935 FREE (directory); /* __FREE_CHECKED__ */
939 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
940 return (rc == M_NO) ? 1 : -1;
942 if (!attname || !attname[0])
945 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
946 M_FILE | M_CLEAR) != 0 || !tmp[0])
948 mutt_concat_path (fname, path, tmp, flen);
951 mutt_concat_path (fname, path, mutt_basename (attname), flen);
954 if (*append == 0 && access (fname, F_OK) == 0)
956 switch (mutt_multi_choice
957 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
965 *append = M_SAVE_APPEND;
967 case 1: /* overwrite */
968 *append = M_SAVE_OVERWRITE;
975 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
979 strfcpy (d, a->mailbox, dsize);
980 if (!option (OPTSAVEADDRESS))
984 if ((p = strpbrk (d, "%@")))
993 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
997 mutt_save_path (s, l, a);
999 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
1004 void mutt_FormatString (char *dest, /* output buffer */
1005 size_t destlen, /* output buffer len */
1006 size_t col, /* starting column (nonzero when called recursively) */
1007 const char *src, /* template string */
1008 format_t *callback, /* callback for processing */
1009 unsigned long data, /* callback data */
1010 format_flag flags) /* callback flags */
1012 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
1013 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
1014 size_t wlen, count, len, wid;
1021 destlen--; /* save room for the terminal \0 */
1022 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1025 if ((flags & M_FORMAT_NOFILTER) == 0)
1029 /* Do not consider filters if no pipe at end */
1030 n = mutt_strlen(src);
1031 if (n > 1 && src[n-1] == '|')
1033 /* Scan backwards for backslashes */
1035 while (off > 0 && src[off-2] == '\\')
1039 /* If number of backslashes is even, the pipe is real. */
1040 /* n-off is the number of backslashes. */
1041 if (off > 0 && ((n-off) % 2) == 0)
1043 BUFFER *srcbuf, *word, *command;
1044 char srccopy[LONG_STRING];
1049 dprint(3, (debugfile, "fmtpipe = %s\n", src));
1051 strncpy(srccopy, src, n);
1052 srccopy[n-1] = '\0';
1054 /* prepare BUFFERs */
1055 srcbuf = mutt_buffer_from(NULL, srccopy);
1056 srcbuf->dptr = srcbuf->data;
1057 word = mutt_buffer_init(NULL);
1058 command = mutt_buffer_init(NULL);
1060 /* Iterate expansions across successive arguments */
1064 /* Extract the command name and copy to command line */
1065 dprint(3, (debugfile, "fmtpipe +++: %s\n", srcbuf->dptr));
1068 mutt_extract_token(word, srcbuf, 0);
1069 dprint(3, (debugfile, "fmtpipe %2d: %s\n", i++, word->data));
1070 mutt_buffer_addch(command, '\'');
1071 mutt_FormatString(buf, sizeof(buf), 0, word->data, callback, data,
1072 flags | M_FORMAT_NOFILTER);
1073 for (p = buf; p && *p; p++)
1076 /* shell quoting doesn't permit escaping a single quote within
1077 * single-quoted material. double-quoting instead will lead
1078 * shell variable expansions, so break out of the single-quoted
1079 * span, insert a double-quoted single quote, and resume. */
1080 mutt_buffer_addstr(command, "'\"'\"'");
1082 mutt_buffer_addch(command, *p);
1084 mutt_buffer_addch(command, '\'');
1085 mutt_buffer_addch(command, ' ');
1086 } while (MoreArgs(srcbuf));
1088 dprint(3, (debugfile, "fmtpipe > %s\n", command->data));
1090 col -= wlen; /* reset to passed in value */
1091 wptr = dest; /* reset write ptr */
1092 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1093 if ((pid = mutt_create_filter(command->data, NULL, &filter, NULL)))
1095 n = fread(dest, 1, destlen /* already decremented */, filter);
1098 while (dest[n-1] == '\n' || dest[n-1] == '\r')
1100 dprint(3, (debugfile, "fmtpipe < %s\n", dest));
1103 mutt_wait_filter(pid);
1105 /* If the result ends with '%', this indicates that the filter
1106 * generated %-tokens that mutt can expand. Eliminate the '%'
1107 * marker and recycle the string through mutt_FormatString().
1108 * To literally end with "%", use "%%". */
1109 if (dest[--n] == '%')
1111 dest[n] = '\0'; /* remove '%' */
1112 if (dest[--n] != '%')
1114 recycler = safe_strdup(dest);
1117 mutt_FormatString(dest, destlen++, col, recycler, callback, data, flags);
1125 /* Filter failed; erase write buffer */
1129 mutt_buffer_free(&command);
1130 mutt_buffer_free(&srcbuf);
1131 mutt_buffer_free(&word);
1136 while (*src && wlen < destlen)
1151 flags |= M_FORMAT_OPTIONAL;
1156 flags &= ~M_FORMAT_OPTIONAL;
1158 /* eat the format string */
1161 while (count < sizeof (prefix) &&
1162 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-' || *src == '='))
1171 break; /* bad format */
1173 ch = *src++; /* save the character to switch on */
1175 if (flags & M_FORMAT_OPTIONAL)
1178 break; /* bad format */
1181 /* eat the `if' part of the string */
1184 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
1191 /* eat the `else' part of the string (optional) */
1193 src++; /* skip the & */
1196 while (count < sizeof (elsestring) && *src && *src != '?')
1204 break; /* bad format */
1206 src++; /* move past the trailing `?' */
1209 /* handle generic cases first */
1210 if (ch == '>' || ch == '*')
1212 /* %>X: right justify to EOL, left takes precedence
1213 * %*X: right justify to EOL, right takes precedence */
1214 int soft = ch == '*';
1216 if ((pl = mutt_charlen (src, &pw)) <= 0)
1219 /* see if there's room to add content, else ignore */
1220 if ((col < COLS && wlen < destlen) || soft)
1224 /* get contents after padding */
1225 mutt_FormatString (buf, sizeof (buf), 0, src + pl, callback, data, flags);
1226 len = mutt_strlen (buf);
1227 wid = mutt_strwidth (buf);
1229 /* try to consume as many columns as we can, if we don't have
1230 * memory for that, use as much memory as possible */
1231 pad = (COLS - col - wid) / pw;
1232 if (pad > 0 && wlen + (pad * pl) + len > destlen)
1233 pad = ((signed)(destlen - wlen - len)) / pl;
1238 memcpy (wptr, src, pl);
1244 else if (soft && pad < 0)
1246 /* \0-terminate dest for length computation in mutt_wstr_trunc() */
1248 /* make sure right part is at most as wide as display */
1249 len = mutt_wstr_trunc (buf, destlen, COLS, &wid);
1250 /* truncate left so that right part fits completely in */
1251 wlen = mutt_wstr_trunc (dest, destlen - len, col + pad, &col);
1254 if (len + wlen > destlen)
1255 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1256 memcpy (wptr, buf, len);
1262 break; /* skip rest of input */
1268 if ((pl = mutt_charlen (src, &pw)) <= 0)
1271 /* see if there's room to add content, else ignore */
1272 if (col < COLS && wlen < destlen)
1274 c = (COLS - col) / pw;
1275 if (c > 0 && wlen + (c * pl) > destlen)
1276 c = ((signed)(destlen - wlen)) / pl;
1279 memcpy (wptr, src, pl);
1287 break; /* skip rest of input */
1294 while (ch == '_' || ch == ':')
1304 /* use callback function to handle this case */
1305 src = callback (buf, sizeof (buf), col, ch, src, prefix, ifstring, elsestring, data, flags);
1308 mutt_strlower (buf);
1317 if ((len = mutt_strlen (buf)) + wlen > destlen)
1318 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1320 memcpy (wptr, buf, len);
1323 col += mutt_strwidth (buf);
1326 else if (*src == '\\')
1359 /* in case of error, simply copy byte */
1360 if ((tmp = mutt_charlen (src, &w)) < 0)
1362 if (tmp > 0 && wlen + tmp < destlen)
1364 memcpy (wptr, src, tmp);
1372 src += destlen - wlen;
1380 if (flags & M_FORMAT_MAKEPRINT)
1382 /* Make sure that the string is printable by changing all non-printable
1383 chars to dots, or spaces for non-printable whitespace */
1384 for (cp = dest ; *cp ; cp++)
1385 if (!IsPrint (*cp) &&
1386 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1387 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1392 /* This function allows the user to specify a command to read stdout from in
1393 place of a normal file. If the last character in the string is a pipe (|),
1394 then we assume it is a commmand to run instead of a normal file. */
1395 FILE *mutt_open_read (const char *path, pid_t *thepid)
1400 int len = mutt_strlen (path);
1402 if (path[len - 1] == '|')
1404 /* read from a pipe */
1406 char *s = safe_strdup (path);
1410 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1415 if (stat (path, &s) < 0)
1417 if (S_ISDIR (s.st_mode))
1422 f = fopen (path, "r");
1428 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1429 int mutt_save_confirm (const char *s, struct stat *st)
1431 char tmp[_POSIX_PATH_MAX];
1436 magic = mx_get_magic (s);
1441 mutt_error _("Can't save message to POP mailbox.");
1446 if (magic > 0 && !mx_access (s, W_OK))
1448 if (option (OPTCONFIRMAPPEND))
1450 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1451 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1458 if (stat (s, st) != -1)
1462 mutt_error (_("%s is not a mailbox!"), s);
1468 if (magic != M_IMAP)
1469 #endif /* execute the block unconditionally if we don't use imap */
1474 if (errno == ENOENT)
1476 if (option (OPTCONFIRMCREATE))
1478 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1479 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1492 CLEARLINE (LINES-1);
1496 void state_prefix_putc (char c, STATE *s)
1498 if (s->flags & M_PENDINGPREFIX)
1500 state_reset_prefix (s);
1502 state_puts (s->prefix, s);
1508 state_set_prefix (s);
1511 int state_printf (STATE *s, const char *fmt, ...)
1517 rv = vfprintf (s->fpout, fmt, ap);
1523 void state_mark_attach (STATE *s)
1525 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1526 state_puts (AttachmentMarker, s);
1529 void state_attach_puts (const char *t, STATE *s)
1531 if (*t != '\n') state_mark_attach (s);
1535 if (*t++ == '\n' && *t)
1536 if (*t != '\n') state_mark_attach (s);
1540 void mutt_display_sanitize (char *s)
1549 void mutt_sleep (short s)
1558 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1559 * just initializes. Frees anything already in the buffer.
1561 * Disregards the 'destroy' flag, which seems reserved for caller.
1562 * This is bad, but there's no apparent protocol for it.
1564 BUFFER * mutt_buffer_init(BUFFER *b)
1568 b = safe_malloc(sizeof(BUFFER));
1576 memset(b, 0, sizeof(BUFFER));
1581 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1582 * just initializes. Frees anything already in the buffer. Copies in
1585 * Disregards the 'destroy' flag, which seems reserved for caller.
1586 * This is bad, but there's no apparent protocol for it.
1588 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1593 b = mutt_buffer_init(b);
1594 b->data = safe_strdup (seed);
1595 b->dsize = mutt_strlen (seed);
1596 b->dptr = (char *) b->data + b->dsize;
1600 int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
1602 va_list ap, ap_retry;
1603 int len, blen, doff;
1606 va_copy (ap_retry, ap);
1608 doff = buf->dptr - buf->data;
1609 blen = buf->dsize - doff;
1610 /* solaris 9 vsnprintf barfs when blen is 0 */
1615 safe_realloc (&buf->data, buf->dsize);
1616 buf->dptr = buf->data + doff;
1618 if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
1620 blen = ++len - blen;
1624 safe_realloc (&buf->data, buf->dsize);
1625 buf->dptr = buf->data + doff;
1626 len = vsnprintf (buf->dptr, len, fmt, ap_retry);
1637 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1639 mutt_buffer_add (buf, s, mutt_strlen (s));
1642 void mutt_buffer_addch (BUFFER* buf, char c)
1644 mutt_buffer_add (buf, &c, 1);
1647 void mutt_buffer_free (BUFFER **p)
1653 /* dptr is just an offset to data and shouldn't be freed */
1654 FREE(p); /* __FREE_CHECKED__ */
1657 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1658 * Always one byte bigger than necessary for the null terminator, and
1659 * the buffer is always null-terminated */
1660 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1664 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1666 offset = buf->dptr - buf->data;
1667 buf->dsize += len < 128 ? 128 : len + 1;
1668 /* suppress compiler aliasing warning */
1669 safe_realloc ((void**) (void*) &buf->data, buf->dsize);
1670 buf->dptr = buf->data + offset;
1672 memcpy (buf->dptr, s, len);
1674 *(buf->dptr) = '\0';
1677 /* Decrease a file's modification time by 1 second */
1679 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1681 struct utimbuf utim;
1687 if (stat (f, &_st) == -1)
1692 if ((mtime = st->st_mtime) == time (NULL))
1695 utim.actime = mtime;
1696 utim.modtime = mtime;
1703 /* sets mtime of 'to' to mtime of 'from' */
1704 void mutt_set_mtime (const char* from, const char* to)
1706 struct utimbuf utim;
1709 if (stat (from, &st) != -1)
1711 utim.actime = st.st_mtime;
1712 utim.modtime = st.st_mtime;
1717 const char *mutt_make_version (void)
1719 static char vstring[STRING];
1720 snprintf (vstring, sizeof (vstring), "Mutt %s (%s)",
1721 MUTT_VERSION, ReleaseDate);
1725 REGEXP *mutt_compile_regexp (const char *s, int flags)
1727 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1728 pp->pattern = safe_strdup (s);
1729 pp->rx = safe_calloc (sizeof (regex_t), 1);
1730 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1731 mutt_free_regexp (&pp);
1736 void mutt_free_regexp (REGEXP **pp)
1738 FREE (&(*pp)->pattern);
1739 regfree ((*pp)->rx);
1741 FREE (pp); /* __FREE_CHECKED__ */
1744 void mutt_free_rx_list (RX_LIST **list)
1752 *list = (*list)->next;
1753 mutt_free_regexp (&p->rx);
1758 void mutt_free_spam_list (SPAM_LIST **list)
1766 *list = (*list)->next;
1767 mutt_free_regexp (&p->rx);
1768 FREE (&p->template);
1773 int mutt_match_rx_list (const char *s, RX_LIST *l)
1777 for (; l; l = l->next)
1779 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1781 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1789 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1791 static regmatch_t *pmatch = NULL;
1792 static int nmatch = 0;
1800 for (; l; l = l->next)
1802 /* If this pattern needs more matches, expand pmatch. */
1803 if (l->nmatch > nmatch)
1805 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1809 /* Does this pattern match? */
1810 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1812 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1813 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", (int)l->rx->rx->re_nsub));
1815 /* Copy template into text, with substitutions. */
1816 for (p = l->template; *p;)
1820 n = atoi(++p); /* find pmatch index */
1821 while (isdigit((unsigned char)*p))
1822 ++p; /* skip subst token */
1823 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1824 text[tlen++] = s[i];
1828 text[tlen++] = *p++;
1832 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));