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 LIST *mutt_find_list (LIST *l, const char *data)
263 if (data && p->data && mutt_strcmp (p->data, data) == 0)
270 void mutt_free_list (LIST **list)
278 *list = (*list)->next;
284 HEADER *mutt_dup_header(HEADER *h)
288 hnew = mutt_new_header();
289 memcpy(hnew, h, sizeof (HEADER));
293 void mutt_free_header (HEADER **h)
295 if(!h || !*h) return;
296 mutt_free_envelope (&(*h)->env);
297 mutt_free_body (&(*h)->content);
298 FREE (&(*h)->maildir_flags);
302 mutt_free_list (&(*h)->chain);
304 #if defined USE_POP || defined USE_IMAP
307 FREE (h); /* __FREE_CHECKED__ */
310 /* returns true if the header contained in "s" is in list "t" */
311 int mutt_matches_ignore (const char *s, LIST *t)
313 for (; t; t = t->next)
315 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
321 /* prepend the path part of *path to *link */
322 void mutt_expand_link (char *newpath, const char *path, const char *link)
324 const char *lb = NULL;
327 /* link is full path */
330 strfcpy (newpath, link, _POSIX_PATH_MAX);
334 if ((lb = strrchr (path, '/')) == NULL)
336 /* no path in link */
337 strfcpy (newpath, link, _POSIX_PATH_MAX);
342 memcpy (newpath, path, len);
343 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
346 char *mutt_expand_path (char *s, size_t slen)
348 return _mutt_expand_path (s, slen, 0);
351 char *_mutt_expand_path (char *s, size_t slen, int rx)
353 char p[_POSIX_PATH_MAX] = "";
354 char q[_POSIX_PATH_MAX] = "";
355 char tmp[_POSIX_PATH_MAX];
370 if (*(s + 1) == '/' || *(s + 1) == 0)
372 strfcpy (p, NONULL(Homedir), sizeof (p));
378 if ((t = strchr (s + 1, '/')))
381 if ((pw = getpwnam (s + 1)))
383 strfcpy (p, pw->pw_dir, sizeof (p));
394 /* user not found! */
408 /* if folder = {host} or imap[s]://host/: don't append slash */
409 if (mx_is_imap (NONULL (Maildir)) &&
410 (Maildir[strlen (Maildir) - 1] == '}' ||
411 Maildir[strlen (Maildir) - 1] == '/'))
412 strfcpy (p, NONULL (Maildir), sizeof (p));
415 if (Maildir && *Maildir && Maildir[strlen (Maildir) - 1] == '/')
416 strfcpy (p, NONULL (Maildir), sizeof (p));
418 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
424 /* elm compatibility, @ expands alias to user name */
431 if ((alias = mutt_lookup_alias (s + 1)))
433 h = mutt_new_header();
434 h->env = mutt_new_envelope();
435 h->env->from = h->env->to = alias;
436 mutt_default_save (p, sizeof (p), h);
437 h->env->from = h->env->to = NULL;
438 mutt_free_header (&h);
439 /* Avoid infinite recursion if the resulting folder starts with '@' */
450 strfcpy (p, NONULL(Inbox), sizeof (p));
457 strfcpy (p, NONULL(Outbox), sizeof (p));
466 strfcpy (p, NONULL(LastFolder), sizeof (p));
471 strfcpy (p, NONULL(Spoolfile), sizeof (p));
479 strfcpy (p, NONULL(LastFolder), sizeof (p));
486 strfcpy (p, NONULL(CurrentFolder), sizeof (p));
498 if (rx && *p && !recurse)
500 mutt_rx_sanitize_string (q, sizeof (q), p);
501 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
504 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
506 strfcpy (s, tmp, slen);
511 /* Rewrite IMAP path in canonical form - aids in string comparisons of
512 * folders. May possibly fail, in which case s should be the same. */
514 imap_expand_path (s, slen);
520 /* Extract the real name from /etc/passwd's GECOS field.
521 * When set, honor the regular expression in GecosMask,
522 * otherwise assume that the GECOS field is a
523 * comma-separated list.
524 * Replace "&" by a capitalized version of the user's login
528 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
530 regmatch_t pat_match[1];
535 if (!pw || !pw->pw_gecos)
538 memset (dest, 0, destlen);
542 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
543 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
544 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
546 else if ((p = strchr (pw->pw_gecos, ',')))
547 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
549 strfcpy (dest, pw->pw_gecos, destlen);
551 pwnl = strlen (pw->pw_name);
553 for (idx = 0; dest[idx]; idx++)
555 if (dest[idx] == '&')
557 memmove (&dest[idx + pwnl], &dest[idx + 1],
558 MAX((ssize_t)(destlen - idx - pwnl - 1), 0));
559 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
560 dest[idx] = toupper ((unsigned char) dest[idx]);
568 char *mutt_get_parameter (const char *s, PARAMETER *p)
570 for (; p; p = p->next)
571 if (ascii_strcasecmp (s, p->attribute) == 0)
577 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
583 mutt_delete_parameter (attribute, p);
587 for(q = *p; q; q = q->next)
589 if (ascii_strcasecmp (attribute, q->attribute) == 0)
591 mutt_str_replace (&q->value, value);
596 q = mutt_new_parameter();
597 q->attribute = safe_strdup(attribute);
598 q->value = safe_strdup(value);
603 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
607 for (q = *p; q; p = &q->next, q = q->next)
609 if (ascii_strcasecmp (attribute, q->attribute) == 0)
613 mutt_free_parameter (&q);
619 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
620 int mutt_needs_mailcap (BODY *m)
626 if (!ascii_strcasecmp ("plain", m->subtype) ||
627 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
628 !ascii_strcasecmp ("enriched", m->subtype))
632 case TYPEAPPLICATION:
633 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
635 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
647 int mutt_is_text_part (BODY *b)
650 char *s = b->subtype;
652 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
658 if (t == TYPEMESSAGE)
660 if (!ascii_strcasecmp ("delivery-status", s))
664 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
666 if (!ascii_strcasecmp ("pgp-keys", s))
673 void mutt_free_envelope (ENVELOPE **p)
676 rfc822_free_address (&(*p)->return_path);
677 rfc822_free_address (&(*p)->from);
678 rfc822_free_address (&(*p)->to);
679 rfc822_free_address (&(*p)->cc);
680 rfc822_free_address (&(*p)->bcc);
681 rfc822_free_address (&(*p)->sender);
682 rfc822_free_address (&(*p)->reply_to);
683 rfc822_free_address (&(*p)->mail_followup_to);
685 FREE (&(*p)->list_post);
686 FREE (&(*p)->subject);
687 /* real_subj is just an offset to subject and shouldn't be freed */
688 FREE (&(*p)->message_id);
689 FREE (&(*p)->supersedes);
691 FREE (&(*p)->x_label);
693 mutt_buffer_free (&(*p)->spam);
695 mutt_free_list (&(*p)->references);
696 mutt_free_list (&(*p)->in_reply_to);
697 mutt_free_list (&(*p)->userhdrs);
698 FREE (p); /* __FREE_CHECKED__ */
701 /* move all the headers from extra not present in base into base */
702 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
704 /* copies each existing element if necessary, and sets the element
705 * to NULL in the source so that mutt_free_envelope doesn't leave us
706 * with dangling pointers. */
707 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
708 MOVE_ELEM(return_path);
715 MOVE_ELEM(mail_followup_to);
716 MOVE_ELEM(list_post);
717 MOVE_ELEM(message_id);
718 MOVE_ELEM(supersedes);
721 if (!base->refs_changed)
723 MOVE_ELEM(references);
725 if (!base->irt_changed)
727 MOVE_ELEM(in_reply_to);
730 /* real_subj is subordinate to subject */
733 base->subject = (*extra)->subject;
734 base->real_subj = (*extra)->real_subj;
735 (*extra)->subject = NULL;
736 (*extra)->real_subj = NULL;
738 /* spam and user headers should never be hashed, and the new envelope may
739 * have better values. Use new versions regardless. */
740 mutt_buffer_free (&base->spam);
741 mutt_free_list (&base->userhdrs);
746 mutt_free_envelope(extra);
749 void _mutt_mktemp (char *s, const char *src, int line)
751 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
752 dprint (3, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
756 void mutt_free_alias (ALIAS **p)
764 mutt_alias_delete_reverse (t);
766 rfc822_free_address (&t->addr);
771 /* collapse the pathname using ~ or = when possible */
772 void mutt_pretty_mailbox (char *s, size_t buflen)
779 scheme = url_check_scheme (s);
782 if (scheme == U_IMAP || scheme == U_IMAPS)
784 imap_pretty_mailbox (s);
789 /* if s is an url, only collapse path component */
790 if (scheme != U_UNKNOWN)
792 p = strchr(s, ':')+1;
793 if (!strncmp (p, "//", 2))
794 q = strchr (p+2, '/');
796 q = strchr (p, '\0');
801 if (strstr (p, "//") || strstr (p, "/./"))
803 /* first attempt to collapse the pathname, this is more
804 * lightweight than realpath() and doesn't resolve links
808 if (*p == '/' && p[1] == '/')
813 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
823 else if (strstr (p, "..") &&
824 (scheme == U_UNKNOWN || scheme == U_FILE) &&
826 strfcpy (p, tmp, buflen - (p - s));
828 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
832 memmove (s, s + len, mutt_strlen (s + len) + 1);
834 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
838 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
842 void mutt_pretty_size (char *s, size_t len, LOFF_T n)
845 strfcpy (s, "0K", len);
846 else if (n < 10189) /* 0.1K - 9.9K */
847 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
848 else if (n < 1023949) /* 10K - 999K */
850 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
851 snprintf (s, len, OFF_T_FMT "K", (n + 51) / 1024);
853 else if (n < 10433332) /* 1.0M - 9.9M */
854 snprintf (s, len, "%3.1fM", n / 1048576.0);
857 /* (10433332 + 52428) / 1048576 = 10 */
858 snprintf (s, len, OFF_T_FMT "M", (n + 52428) / 1048576);
862 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
864 char tmp[LONG_STRING];
866 mutt_quote_filename (tmp, sizeof (tmp), src);
867 mutt_expand_fmt (dest, destlen, fmt, tmp);
870 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
877 slen = mutt_strlen (src);
880 for (p = fmt, d = dest; destlen && *p; p++)
892 strfcpy (d, src, destlen + 1);
893 d += destlen > slen ? slen : destlen;
894 destlen -= destlen > slen ? slen : destlen;
912 if (!found && destlen > 0)
914 safe_strcat (dest, destlen, " ");
915 safe_strcat (dest, destlen, src);
920 /* return 0 on success, -1 on abort, 1 on error */
921 int mutt_check_overwrite (const char *attname, const char *path,
922 char *fname, size_t flen, int *append, char **directory)
925 char tmp[_POSIX_PATH_MAX];
928 strfcpy (fname, path, flen);
929 if (access (fname, F_OK) != 0)
931 if (stat (fname, &st) != 0)
933 if (S_ISDIR (st.st_mode))
937 switch (mutt_multi_choice
938 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
941 mutt_str_replace (directory, fname);
944 FREE (directory); /* __FREE_CHECKED__ */
947 FREE (directory); /* __FREE_CHECKED__ */
950 FREE (directory); /* __FREE_CHECKED__ */
954 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
955 return (rc == M_NO) ? 1 : -1;
957 if (!attname || !attname[0])
960 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
961 M_FILE | M_CLEAR) != 0 || !tmp[0])
963 mutt_concat_path (fname, path, tmp, flen);
966 mutt_concat_path (fname, path, mutt_basename (attname), flen);
969 if (*append == 0 && access (fname, F_OK) == 0)
971 switch (mutt_multi_choice
972 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
980 *append = M_SAVE_APPEND;
982 case 1: /* overwrite */
983 *append = M_SAVE_OVERWRITE;
990 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
994 strfcpy (d, a->mailbox, dsize);
995 if (!option (OPTSAVEADDRESS))
999 if ((p = strpbrk (d, "%@")))
1008 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
1012 mutt_save_path (s, l, a);
1013 for (p = s; *p; p++)
1014 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
1019 void mutt_FormatString (char *dest, /* output buffer */
1020 size_t destlen, /* output buffer len */
1021 size_t col, /* starting column (nonzero when called recursively) */
1022 const char *src, /* template string */
1023 format_t *callback, /* callback for processing */
1024 unsigned long data, /* callback data */
1025 format_flag flags) /* callback flags */
1027 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
1028 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
1029 size_t wlen, count, len, wid;
1036 destlen--; /* save room for the terminal \0 */
1037 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1040 if ((flags & M_FORMAT_NOFILTER) == 0)
1044 /* Do not consider filters if no pipe at end */
1045 n = mutt_strlen(src);
1046 if (n > 1 && src[n-1] == '|')
1048 /* Scan backwards for backslashes */
1050 while (off > 0 && src[off-2] == '\\')
1054 /* If number of backslashes is even, the pipe is real. */
1055 /* n-off is the number of backslashes. */
1056 if (off > 0 && ((n-off) % 2) == 0)
1058 BUFFER *srcbuf, *word, *command;
1059 char srccopy[LONG_STRING];
1064 dprint(3, (debugfile, "fmtpipe = %s\n", src));
1066 strncpy(srccopy, src, n);
1067 srccopy[n-1] = '\0';
1069 /* prepare BUFFERs */
1070 srcbuf = mutt_buffer_from(NULL, srccopy);
1071 srcbuf->dptr = srcbuf->data;
1072 word = mutt_buffer_init(NULL);
1073 command = mutt_buffer_init(NULL);
1075 /* Iterate expansions across successive arguments */
1079 /* Extract the command name and copy to command line */
1080 dprint(3, (debugfile, "fmtpipe +++: %s\n", srcbuf->dptr));
1083 mutt_extract_token(word, srcbuf, 0);
1084 dprint(3, (debugfile, "fmtpipe %2d: %s\n", i++, word->data));
1085 mutt_buffer_addch(command, '\'');
1086 mutt_FormatString(buf, sizeof(buf), 0, word->data, callback, data,
1087 flags | M_FORMAT_NOFILTER);
1088 for (p = buf; p && *p; p++)
1091 /* shell quoting doesn't permit escaping a single quote within
1092 * single-quoted material. double-quoting instead will lead
1093 * shell variable expansions, so break out of the single-quoted
1094 * span, insert a double-quoted single quote, and resume. */
1095 mutt_buffer_addstr(command, "'\"'\"'");
1097 mutt_buffer_addch(command, *p);
1099 mutt_buffer_addch(command, '\'');
1100 mutt_buffer_addch(command, ' ');
1101 } while (MoreArgs(srcbuf));
1103 dprint(3, (debugfile, "fmtpipe > %s\n", command->data));
1105 col -= wlen; /* reset to passed in value */
1106 wptr = dest; /* reset write ptr */
1107 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1108 if ((pid = mutt_create_filter(command->data, NULL, &filter, NULL)))
1110 n = fread(dest, 1, destlen /* already decremented */, filter);
1111 safe_fclose (&filter);
1113 while (dest[n-1] == '\n' || dest[n-1] == '\r')
1115 dprint(3, (debugfile, "fmtpipe < %s\n", dest));
1118 mutt_wait_filter(pid);
1120 /* If the result ends with '%', this indicates that the filter
1121 * generated %-tokens that mutt can expand. Eliminate the '%'
1122 * marker and recycle the string through mutt_FormatString().
1123 * To literally end with "%", use "%%". */
1124 if (dest[--n] == '%')
1126 dest[n] = '\0'; /* remove '%' */
1127 if (dest[--n] != '%')
1129 recycler = safe_strdup(dest);
1132 mutt_FormatString(dest, destlen++, col, recycler, callback, data, flags);
1140 /* Filter failed; erase write buffer */
1144 mutt_buffer_free(&command);
1145 mutt_buffer_free(&srcbuf);
1146 mutt_buffer_free(&word);
1151 while (*src && wlen < destlen)
1166 flags |= M_FORMAT_OPTIONAL;
1171 flags &= ~M_FORMAT_OPTIONAL;
1173 /* eat the format string */
1176 while (count < sizeof (prefix) &&
1177 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-' || *src == '='))
1186 break; /* bad format */
1188 ch = *src++; /* save the character to switch on */
1190 if (flags & M_FORMAT_OPTIONAL)
1193 break; /* bad format */
1196 /* eat the `if' part of the string */
1199 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
1206 /* eat the `else' part of the string (optional) */
1208 src++; /* skip the & */
1211 while (count < sizeof (elsestring) && *src && *src != '?')
1219 break; /* bad format */
1221 src++; /* move past the trailing `?' */
1224 /* handle generic cases first */
1225 if (ch == '>' || ch == '*')
1227 /* %>X: right justify to EOL, left takes precedence
1228 * %*X: right justify to EOL, right takes precedence */
1229 int soft = ch == '*';
1231 if ((pl = mutt_charlen (src, &pw)) <= 0)
1234 /* see if there's room to add content, else ignore */
1235 if ((col < COLS && wlen < destlen) || soft)
1239 /* get contents after padding */
1240 mutt_FormatString (buf, sizeof (buf), 0, src + pl, callback, data, flags);
1241 len = mutt_strlen (buf);
1242 wid = mutt_strwidth (buf);
1244 /* try to consume as many columns as we can, if we don't have
1245 * memory for that, use as much memory as possible */
1246 pad = (COLS - col - wid) / pw;
1247 if (pad > 0 && wlen + (pad * pl) + len > destlen)
1248 pad = ((signed)(destlen - wlen - len)) / pl;
1253 memcpy (wptr, src, pl);
1259 else if (soft && pad < 0)
1261 /* \0-terminate dest for length computation in mutt_wstr_trunc() */
1263 /* make sure right part is at most as wide as display */
1264 len = mutt_wstr_trunc (buf, destlen, COLS, &wid);
1265 /* truncate left so that right part fits completely in */
1266 wlen = mutt_wstr_trunc (dest, destlen - len, col + pad, &col);
1269 if (len + wlen > destlen)
1270 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1271 memcpy (wptr, buf, len);
1277 break; /* skip rest of input */
1283 if ((pl = mutt_charlen (src, &pw)) <= 0)
1286 /* see if there's room to add content, else ignore */
1287 if (col < COLS && wlen < destlen)
1289 c = (COLS - col) / pw;
1290 if (c > 0 && wlen + (c * pl) > destlen)
1291 c = ((signed)(destlen - wlen)) / pl;
1294 memcpy (wptr, src, pl);
1302 break; /* skip rest of input */
1309 while (ch == '_' || ch == ':')
1319 /* use callback function to handle this case */
1320 src = callback (buf, sizeof (buf), col, ch, src, prefix, ifstring, elsestring, data, flags);
1323 mutt_strlower (buf);
1332 if ((len = mutt_strlen (buf)) + wlen > destlen)
1333 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1335 memcpy (wptr, buf, len);
1338 col += mutt_strwidth (buf);
1341 else if (*src == '\\')
1374 /* in case of error, simply copy byte */
1375 if ((tmp = mutt_charlen (src, &w)) < 0)
1377 if (tmp > 0 && wlen + tmp < destlen)
1379 memcpy (wptr, src, tmp);
1387 src += destlen - wlen;
1395 if (flags & M_FORMAT_MAKEPRINT)
1397 /* Make sure that the string is printable by changing all non-printable
1398 chars to dots, or spaces for non-printable whitespace */
1399 for (cp = dest ; *cp ; cp++)
1400 if (!IsPrint (*cp) &&
1401 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1402 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1407 /* This function allows the user to specify a command to read stdout from in
1408 place of a normal file. If the last character in the string is a pipe (|),
1409 then we assume it is a commmand to run instead of a normal file. */
1410 FILE *mutt_open_read (const char *path, pid_t *thepid)
1415 int len = mutt_strlen (path);
1417 if (path[len - 1] == '|')
1419 /* read from a pipe */
1421 char *s = safe_strdup (path);
1425 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1430 if (stat (path, &s) < 0)
1432 if (S_ISDIR (s.st_mode))
1437 f = fopen (path, "r");
1443 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1444 int mutt_save_confirm (const char *s, struct stat *st)
1446 char tmp[_POSIX_PATH_MAX];
1451 magic = mx_get_magic (s);
1456 mutt_error _("Can't save message to POP mailbox.");
1461 if (magic > 0 && !mx_access (s, W_OK))
1463 if (option (OPTCONFIRMAPPEND))
1465 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1466 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1473 if (stat (s, st) != -1)
1477 mutt_error (_("%s is not a mailbox!"), s);
1483 if (magic != M_IMAP)
1484 #endif /* execute the block unconditionally if we don't use imap */
1489 if (errno == ENOENT)
1491 if (option (OPTCONFIRMCREATE))
1493 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1494 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1507 CLEARLINE (LINES-1);
1511 void state_prefix_putc (char c, STATE *s)
1513 if (s->flags & M_PENDINGPREFIX)
1515 state_reset_prefix (s);
1517 state_puts (s->prefix, s);
1523 state_set_prefix (s);
1526 int state_printf (STATE *s, const char *fmt, ...)
1532 rv = vfprintf (s->fpout, fmt, ap);
1538 void state_mark_attach (STATE *s)
1540 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1541 state_puts (AttachmentMarker, s);
1544 void state_attach_puts (const char *t, STATE *s)
1546 if (*t != '\n') state_mark_attach (s);
1550 if (*t++ == '\n' && *t)
1551 if (*t != '\n') state_mark_attach (s);
1555 void mutt_display_sanitize (char *s)
1564 void mutt_sleep (short s)
1573 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1574 * just initializes. Frees anything already in the buffer.
1576 * Disregards the 'destroy' flag, which seems reserved for caller.
1577 * This is bad, but there's no apparent protocol for it.
1579 BUFFER * mutt_buffer_init(BUFFER *b)
1583 b = safe_malloc(sizeof(BUFFER));
1591 memset(b, 0, sizeof(BUFFER));
1596 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1597 * just initializes. Frees anything already in the buffer. Copies in
1600 * Disregards the 'destroy' flag, which seems reserved for caller.
1601 * This is bad, but there's no apparent protocol for it.
1603 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1608 b = mutt_buffer_init(b);
1609 b->data = safe_strdup (seed);
1610 b->dsize = mutt_strlen (seed);
1611 b->dptr = (char *) b->data + b->dsize;
1615 int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
1617 va_list ap, ap_retry;
1618 int len, blen, doff;
1621 va_copy (ap_retry, ap);
1623 doff = buf->dptr - buf->data;
1624 blen = buf->dsize - doff;
1625 /* solaris 9 vsnprintf barfs when blen is 0 */
1630 safe_realloc (&buf->data, buf->dsize);
1631 buf->dptr = buf->data + doff;
1633 if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
1635 blen = ++len - blen;
1639 safe_realloc (&buf->data, buf->dsize);
1640 buf->dptr = buf->data + doff;
1641 len = vsnprintf (buf->dptr, len, fmt, ap_retry);
1652 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1654 mutt_buffer_add (buf, s, mutt_strlen (s));
1657 void mutt_buffer_addch (BUFFER* buf, char c)
1659 mutt_buffer_add (buf, &c, 1);
1662 void mutt_buffer_free (BUFFER **p)
1668 /* dptr is just an offset to data and shouldn't be freed */
1669 FREE(p); /* __FREE_CHECKED__ */
1672 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1673 * Always one byte bigger than necessary for the null terminator, and
1674 * the buffer is always null-terminated */
1675 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1679 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1681 offset = buf->dptr - buf->data;
1682 buf->dsize += len < 128 ? 128 : len + 1;
1683 /* suppress compiler aliasing warning */
1684 safe_realloc ((void**) (void*) &buf->data, buf->dsize);
1685 buf->dptr = buf->data + offset;
1687 memcpy (buf->dptr, s, len);
1689 *(buf->dptr) = '\0';
1692 /* Decrease a file's modification time by 1 second */
1694 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1696 struct utimbuf utim;
1702 if (stat (f, &_st) == -1)
1707 if ((mtime = st->st_mtime) == time (NULL))
1710 utim.actime = mtime;
1711 utim.modtime = mtime;
1718 /* sets mtime of 'to' to mtime of 'from' */
1719 void mutt_set_mtime (const char* from, const char* to)
1721 struct utimbuf utim;
1724 if (stat (from, &st) != -1)
1726 utim.actime = st.st_mtime;
1727 utim.modtime = st.st_mtime;
1732 const char *mutt_make_version (void)
1734 static char vstring[STRING];
1735 snprintf (vstring, sizeof (vstring), "Mutt %s (%s)",
1736 MUTT_VERSION, ReleaseDate);
1740 REGEXP *mutt_compile_regexp (const char *s, int flags)
1742 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1743 pp->pattern = safe_strdup (s);
1744 pp->rx = safe_calloc (sizeof (regex_t), 1);
1745 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1746 mutt_free_regexp (&pp);
1751 void mutt_free_regexp (REGEXP **pp)
1753 FREE (&(*pp)->pattern);
1754 regfree ((*pp)->rx);
1756 FREE (pp); /* __FREE_CHECKED__ */
1759 void mutt_free_rx_list (RX_LIST **list)
1767 *list = (*list)->next;
1768 mutt_free_regexp (&p->rx);
1773 void mutt_free_spam_list (SPAM_LIST **list)
1781 *list = (*list)->next;
1782 mutt_free_regexp (&p->rx);
1783 FREE (&p->template);
1788 int mutt_match_rx_list (const char *s, RX_LIST *l)
1792 for (; l; l = l->next)
1794 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1796 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1804 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1806 static regmatch_t *pmatch = NULL;
1807 static int nmatch = 0;
1815 for (; l; l = l->next)
1817 /* If this pattern needs more matches, expand pmatch. */
1818 if (l->nmatch > nmatch)
1820 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1824 /* Does this pattern match? */
1825 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1827 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1828 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", (int)l->rx->rx->re_nsub));
1830 /* Copy template into text, with substitutions. */
1831 for (p = l->template; *p;)
1835 n = atoi(++p); /* find pmatch index */
1836 while (isdigit((unsigned char)*p))
1837 ++p; /* skip subst token */
1838 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1839 text[tlen++] = s[i];
1843 text[tlen++] = *p++;
1847 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));