2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 #include "mutt_curses.h"
37 #include "mutt_crypt.h"
48 #include <sys/types.h>
51 BODY *mutt_new_body (void)
53 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
55 p->disposition = DISPATTACH;
61 /* Modified by blong to accept a "suggestion" for file name. If
62 * that file exists, then construct one with unique name but
63 * keep any extension. This might fail, I guess.
64 * Renamed to mutt_adv_mktemp so I only have to change where it's
65 * called, and not all possible cases.
67 void mutt_adv_mktemp (char *s, size_t l)
69 char buf[_POSIX_PATH_MAX];
70 char tmp[_POSIX_PATH_MAX];
75 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
76 mutt_expand_path (buf, sizeof (buf));
79 snprintf (s, l, "%s/muttXXXXXX", buf);
84 strfcpy (tmp, s, sizeof (tmp));
85 mutt_sanitize_filename (tmp, 1);
86 snprintf (s, l, "%s/%s", buf, tmp);
87 if (lstat (s, &sb) == -1 && errno == ENOENT)
89 if ((period = strrchr (tmp, '.')) != NULL)
91 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
97 strfcpy(s + sl, period, l - sl);
102 /* create a send-mode duplicate from a receive-mode body */
104 int mutt_copy_body (FILE *fp, BODY **tgt, BODY *src)
106 char tmp[_POSIX_PATH_MAX];
109 PARAMETER *par, **ppar;
116 strfcpy (tmp, src->filename, sizeof (tmp));
124 mutt_adv_mktemp (tmp, sizeof (tmp));
125 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
128 *tgt = mutt_new_body ();
131 memcpy (b, src, sizeof (BODY));
135 b->filename = safe_strdup (tmp);
136 b->use_disp = use_disp;
139 if (mutt_is_text_part (b))
142 b->xtype = safe_strdup (b->xtype);
143 b->subtype = safe_strdup (b->subtype);
144 b->form_name = safe_strdup (b->form_name);
145 b->filename = safe_strdup (b->filename);
146 b->d_filename = safe_strdup (b->d_filename);
147 b->description = safe_strdup (b->description);
150 * we don't seem to need the HEADER structure currently.
151 * XXX - this may change in the future
154 if (b->hdr) b->hdr = NULL;
156 /* copy parameters */
157 for (par = b->parameter, ppar = &b->parameter; par; ppar = &(*ppar)->next, par = par->next)
159 *ppar = mutt_new_parameter ();
160 (*ppar)->attribute = safe_strdup (par->attribute);
161 (*ppar)->value = safe_strdup (par->value);
164 mutt_stamp_attachment (b);
171 void mutt_free_body (BODY **p)
181 mutt_free_parameter (&b->parameter);
182 if (b->unlink && b->filename)
184 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
185 unlink (b->filename);
187 else if (b->filename)
188 dprint (1, (debugfile, "mutt_free_body: Not unlinking %s.\n", b->filename));
194 FREE (&b->description);
195 FREE (&b->form_name);
199 /* Don't free twice (b->hdr->content = b->parts) */
200 b->hdr->content = NULL;
201 mutt_free_header(&b->hdr);
205 mutt_free_body (&b->parts);
213 void mutt_free_parameter (PARAMETER **p)
220 FREE (&t->attribute);
229 LIST *mutt_add_list (LIST *head, const char *data)
233 for (tmp = head; tmp && tmp->next; tmp = tmp->next)
237 tmp->next = safe_malloc (sizeof (LIST));
241 head = tmp = safe_malloc (sizeof (LIST));
243 tmp->data = safe_strdup (data);
248 void mutt_free_list (LIST **list)
256 *list = (*list)->next;
262 HEADER *mutt_dup_header(HEADER *h)
266 hnew = mutt_new_header();
267 memcpy(hnew, h, sizeof (HEADER));
271 void mutt_free_header (HEADER **h)
273 if(!h || !*h) return;
274 mutt_free_envelope (&(*h)->env);
275 mutt_free_body (&(*h)->content);
276 FREE (&(*h)->maildir_flags);
280 mutt_free_list (&(*h)->chain);
282 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
288 /* returns true if the header contained in "s" is in list "t" */
289 int mutt_matches_ignore (const char *s, LIST *t)
291 for (; t; t = t->next)
293 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
299 /* prepend the path part of *path to *link */
300 void mutt_expand_link (char *newpath, const char *path, const char *link)
302 const char *lb = NULL;
305 /* link is full path */
308 strfcpy (newpath, link, _POSIX_PATH_MAX);
312 if ((lb = strrchr (path, '/')) == NULL)
314 /* no path in link */
315 strfcpy (newpath, link, _POSIX_PATH_MAX);
320 memcpy (newpath, path, len);
321 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
324 char *mutt_expand_path (char *s, size_t slen)
326 return _mutt_expand_path (s, slen, 0);
329 char *_mutt_expand_path (char *s, size_t slen, int rx)
331 char p[_POSIX_PATH_MAX] = "";
332 char q[_POSIX_PATH_MAX] = "";
333 char tmp[_POSIX_PATH_MAX];
348 if (*(s + 1) == '/' || *(s + 1) == 0)
350 strfcpy (p, NONULL(Homedir), sizeof (p));
356 if ((t = strchr (s + 1, '/')))
359 if ((pw = getpwnam (s + 1)))
361 strfcpy (p, pw->pw_dir, sizeof (p));
372 /* user not found! */
386 /* if folder = {host} or imap[s]://host/: don't append slash */
387 if (mx_is_imap (NONULL (Maildir)) &&
388 (Maildir[strlen (Maildir) - 1] == '}' ||
389 Maildir[strlen (Maildir) - 1] == '/'))
390 strfcpy (p, NONULL (Maildir), sizeof (p));
393 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
399 /* elm compatibility, @ expands alias to user name */
406 if ((alias = mutt_lookup_alias (s + 1)))
408 h = mutt_new_header();
409 h->env = mutt_new_envelope();
410 h->env->from = h->env->to = alias;
411 mutt_default_save (p, sizeof (p), h);
412 h->env->from = h->env->to = NULL;
413 mutt_free_header (&h);
414 /* Avoid infinite recursion if the resulting folder starts with '@' */
425 strfcpy (p, NONULL(Inbox), sizeof (p));
432 strfcpy (p, NONULL(Outbox), sizeof (p));
441 strfcpy (p, NONULL(LastFolder), sizeof (p));
446 strfcpy (p, NONULL(Spoolfile), sizeof (p));
454 strfcpy (p, NONULL(LastFolder), sizeof (p));
461 strfcpy (p, NONULL(CurrentFolder), sizeof (p));
473 if (rx && *p && !recurse)
475 mutt_rx_sanitize_string (q, sizeof (q), p);
476 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
479 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
481 strfcpy (s, tmp, slen);
486 /* Rewrite IMAP path in canonical form - aids in string comparisons of
487 * folders. May possibly fail, in which case s should be the same. */
489 imap_expand_path (s, slen);
495 /* Extract the real name from /etc/passwd's GECOS field.
496 * When set, honor the regular expression in GecosMask,
497 * otherwise assume that the GECOS field is a
498 * comma-separated list.
499 * Replace "&" by a capitalized version of the user's login
503 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
505 regmatch_t pat_match[1];
510 if (!pw || !pw->pw_gecos)
513 memset (dest, 0, destlen);
517 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
518 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
519 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
521 else if ((p = strchr (pw->pw_gecos, ',')))
522 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
524 strfcpy (dest, pw->pw_gecos, destlen);
526 pwnl = strlen (pw->pw_name);
528 for (idx = 0; dest[idx]; idx++)
530 if (dest[idx] == '&')
532 memmove (&dest[idx + pwnl], &dest[idx + 1],
533 MAX(destlen - idx - pwnl - 1, 0));
534 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
535 dest[idx] = toupper ((unsigned char) dest[idx]);
543 char *mutt_get_parameter (const char *s, PARAMETER *p)
545 for (; p; p = p->next)
546 if (ascii_strcasecmp (s, p->attribute) == 0)
552 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
558 mutt_delete_parameter (attribute, p);
562 for(q = *p; q; q = q->next)
564 if (ascii_strcasecmp (attribute, q->attribute) == 0)
566 mutt_str_replace (&q->value, value);
571 q = mutt_new_parameter();
572 q->attribute = safe_strdup(attribute);
573 q->value = safe_strdup(value);
578 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
582 for (q = *p; q; p = &q->next, q = q->next)
584 if (ascii_strcasecmp (attribute, q->attribute) == 0)
588 mutt_free_parameter (&q);
594 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
595 int mutt_needs_mailcap (BODY *m)
601 if (!ascii_strcasecmp ("plain", m->subtype) ||
602 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
603 !ascii_strcasecmp ("enriched", m->subtype))
607 case TYPEAPPLICATION:
608 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
610 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
622 int mutt_is_text_part (BODY *b)
625 char *s = b->subtype;
627 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
633 if (t == TYPEMESSAGE)
635 if (!ascii_strcasecmp ("delivery-status", s))
639 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
641 if (!ascii_strcasecmp ("pgp-keys", s))
648 void mutt_free_envelope (ENVELOPE **p)
651 rfc822_free_address (&(*p)->return_path);
652 rfc822_free_address (&(*p)->from);
653 rfc822_free_address (&(*p)->to);
654 rfc822_free_address (&(*p)->cc);
655 rfc822_free_address (&(*p)->bcc);
656 rfc822_free_address (&(*p)->sender);
657 rfc822_free_address (&(*p)->reply_to);
658 rfc822_free_address (&(*p)->mail_followup_to);
660 FREE (&(*p)->list_post);
661 FREE (&(*p)->subject);
662 /* real_subj is just an offset to subject and shouldn't be freed */
663 FREE (&(*p)->message_id);
664 FREE (&(*p)->supersedes);
666 FREE (&(*p)->x_label);
667 FREE (&(*p)->organization);
669 FREE (&(*p)->newsgroups);
671 FREE (&(*p)->followup_to);
672 FREE (&(*p)->x_comment_to);
675 mutt_buffer_free (&(*p)->spam);
676 mutt_free_list (&(*p)->references);
677 mutt_free_list (&(*p)->in_reply_to);
678 mutt_free_list (&(*p)->userhdrs);
682 void _mutt_mktemp (char *s, const char *src, int line)
684 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
685 dprint (1, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
689 void mutt_free_alias (ALIAS **p)
698 rfc822_free_address (&t->addr);
703 /* collapse the pathname using ~ or = when possible */
704 void mutt_pretty_mailbox (char *s)
710 scheme = url_check_scheme (s);
713 if (scheme == U_IMAP || scheme == U_IMAPS)
715 imap_pretty_mailbox (s);
720 /* if s is an url, only collapse path component */
721 if (scheme != U_UNKNOWN)
723 p = strchr(s, ':')+1;
724 if (!strncmp (p, "//", 2))
725 q = strchr (p+2, '/');
727 q = strchr (p, '\0');
731 /* first attempt to collapse the pathname */
734 if (*p == '/' && p[1] == '/')
739 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
749 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
753 memmove (s, s + len, mutt_strlen (s + len) + 1);
755 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
759 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
763 void mutt_pretty_size (char *s, size_t len, long n)
766 strfcpy (s, "0K", len);
767 else if (n < 10189) /* 0.1K - 9.9K */
768 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
769 else if (n < 1023949) /* 10K - 999K */
771 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
772 snprintf (s, len, "%ldK", (n + 51) / 1024);
774 else if (n < 10433332) /* 1.0M - 9.9M */
775 snprintf (s, len, "%3.1fM", n / 1048576.0);
778 /* (10433332 + 52428) / 1048576 = 10 */
779 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
783 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
785 char tmp[LONG_STRING];
787 mutt_quote_filename (tmp, sizeof (tmp), src);
788 mutt_expand_fmt (dest, destlen, fmt, tmp);
791 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
798 slen = mutt_strlen (src);
801 for (p = fmt, d = dest; destlen && *p; p++)
813 strfcpy (d, src, destlen + 1);
814 d += destlen > slen ? slen : destlen;
815 destlen -= destlen > slen ? slen : destlen;
833 if (!found && destlen > 0)
835 safe_strcat (dest, destlen, " ");
836 safe_strcat (dest, destlen, src);
841 /* return 0 on success, -1 on abort, 1 on error */
842 int mutt_check_overwrite (const char *attname, const char *path,
843 char *fname, size_t flen, int *append, char **directory)
846 char tmp[_POSIX_PATH_MAX];
849 strfcpy (fname, path, flen);
850 if (access (fname, F_OK) != 0)
852 if (stat (fname, &st) != 0)
854 if (S_ISDIR (st.st_mode))
858 switch (mutt_multi_choice
859 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
862 mutt_str_replace (directory, fname);
875 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
876 return (rc == M_NO) ? 1 : -1;
878 if (!attname || !attname[0])
881 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
882 M_FILE | M_CLEAR) != 0 || !tmp[0])
884 mutt_concat_path (fname, path, tmp, flen);
887 mutt_concat_path (fname, path, mutt_basename (attname), flen);
890 if (*append == 0 && access (fname, F_OK) == 0)
892 switch (mutt_multi_choice
893 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
901 *append = M_SAVE_APPEND;
903 case 1: /* overwrite */
904 *append = M_SAVE_OVERWRITE;
911 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
915 strfcpy (d, a->mailbox, dsize);
916 if (!option (OPTSAVEADDRESS))
920 if ((p = strpbrk (d, "%@")))
929 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
933 mutt_save_path (s, l, a);
935 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
939 /* counts how many characters in s can be skipped while none of the
940 * characters of c appears */
941 int mutt_skipchars (const char* s, const char* c) {
945 register const char* t = c;
954 return (mutt_strlen (p));
957 void mutt_FormatString (char *dest, /* output buffer */
958 size_t destlen, /* output buffer len */
959 const char *src, /* template string */
960 format_t *callback, /* callback for processing */
961 unsigned long data, /* callback data */
962 format_flag flags) /* callback flags */
964 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
965 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
966 size_t wlen, count, len, col, wid;
969 destlen--; /* save room for the terminal \0 */
970 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
973 while (*src && wlen < destlen)
988 flags |= M_FORMAT_OPTIONAL;
993 flags &= ~M_FORMAT_OPTIONAL;
995 /* eat the format string */
998 while (count < sizeof (prefix) &&
999 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
1008 break; /* bad format */
1010 ch = *src++; /* save the character to switch on */
1012 if (flags & M_FORMAT_OPTIONAL)
1015 break; /* bad format */
1018 /* eat the `if' part of the string */
1021 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
1028 /* eat the `else' part of the string (optional) */
1030 src++; /* skip the & */
1033 while (count < sizeof (elsestring) && *src && *src != '?')
1041 break; /* bad format */
1043 src++; /* move past the trailing `?' */
1046 /* handle generic cases first */
1049 /* right justify to EOL */
1050 ch = *src++; /* pad char */
1051 /* calculate space left on line. if we've already written more data
1052 than will fit on the line, ignore the rest of the line */
1053 if ( DrawFullLine || option(OPTSTATUSONTOP))
1054 count = (COLS < destlen ? COLS : destlen);
1056 count = ((COLS-SidebarWidth) < destlen ? (COLS - SidebarWidth) : destlen);
1059 count -= col; /* how many columns left on this line */
1060 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1061 wid = mutt_strlen (buf);
1064 count -= wid; /* how many chars to pad */
1065 memset (wptr, ch, count);
1069 if (wid + wlen > destlen)
1070 len = destlen - wlen;
1073 memcpy (wptr, buf, len);
1076 col += mutt_strwidth (buf);
1078 break; /* skip rest of input */
1088 count = destlen - wlen;
1089 memset (wptr, ch, count);
1092 break; /* skip rest of input */
1099 while (ch == '_' || ch == ':')
1109 /* use callback function to handle this case */
1110 src = callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring, data, flags);
1113 mutt_strlower (buf);
1122 if ((len = mutt_strlen (buf)) + wlen > destlen)
1123 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1125 memcpy (wptr, buf, len);
1128 col += mutt_strwidth (buf);
1131 else if (*src == '\\')
1163 unsigned int bar = mutt_skipchars (src, "%\\");
1164 char* bar2 = safe_malloc (bar+1);
1165 strfcpy (bar2, src, bar+1);
1170 col += mutt_strwidth (bar2);
1177 if (flags & M_FORMAT_MAKEPRINT)
1179 /* Make sure that the string is printable by changing all non-printable
1180 chars to dots, or spaces for non-printable whitespace */
1181 for (cp = dest ; *cp ; cp++)
1182 if (!IsPrint (*cp) &&
1183 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1184 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1189 /* This function allows the user to specify a command to read stdout from in
1190 place of a normal file. If the last character in the string is a pipe (|),
1191 then we assume it is a commmand to run instead of a normal file. */
1192 FILE *mutt_open_read (const char *path, pid_t *thepid)
1197 int len = mutt_strlen (path);
1199 if (path[len - 1] == '|')
1201 /* read from a pipe */
1203 char *s = safe_strdup (path);
1207 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1212 if (stat (path, &s) < 0)
1214 if (S_ISDIR (s.st_mode))
1219 f = fopen (path, "r");
1225 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1226 int mutt_save_confirm (const char *s, struct stat *st)
1228 char tmp[_POSIX_PATH_MAX];
1233 magic = mx_get_magic (s);
1238 mutt_error _("Can't save message to POP mailbox.");
1244 if (magic == M_NNTP)
1246 mutt_error _("Can't save message to newsserver.");
1251 if (stat (s, st) != -1)
1255 mutt_error (_("%s is not a mailbox!"), s);
1259 if (option (OPTCONFIRMAPPEND) &&
1260 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1261 /* if we're appending to the trash, there's no point in asking */
1263 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1264 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1273 if (magic != M_IMAP)
1274 #endif /* execute the block unconditionally if we don't use imap */
1279 if (errno == ENOENT)
1281 if (option (OPTCONFIRMCREATE))
1283 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1284 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1298 CLEARLINE (LINES-1);
1302 void state_prefix_putc (char c, STATE *s)
1304 if (s->flags & M_PENDINGPREFIX)
1308 i = strlen (Quotebuf);
1311 if (i == sizeof (Quotebuf) - 1 || c == '\n')
1313 char buf[2 * SHORT_STRING];
1314 int j = 0, offset = 0;
1315 regmatch_t pmatch[1];
1317 state_reset_prefix (s);
1318 while (regexec ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch, 0) == 0)
1319 offset += pmatch->rm_eo;
1321 if (!option (OPTQUOTEEMPTY) && Quotebuf[0] == '\n')
1322 strcpy (buf, Quotebuf);
1323 else if (option (OPTQUOTEQUOTED) && offset)
1325 for (i = 0; i < offset; i++)
1326 if (Quotebuf[i] != ' ')
1328 strncpy (buf, Quotebuf, j + 1);
1329 strcpy (buf + j + 1, Quotebuf + j);
1332 snprintf (buf, sizeof (buf), "%s%s", NONULL(s->prefix), Quotebuf);
1334 state_puts (buf, s);
1342 state_set_prefix (s);
1347 int state_printf (STATE *s, const char *fmt, ...)
1353 rv = vfprintf (s->fpout, fmt, ap);
1359 void state_mark_attach (STATE *s)
1361 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1362 state_puts (AttachmentMarker, s);
1365 void state_attach_puts (const char *t, STATE *s)
1367 if (*t != '\n') state_mark_attach (s);
1371 if (*t++ == '\n' && *t)
1372 if (*t != '\n') state_mark_attach (s);
1376 void mutt_display_sanitize (char *s)
1385 void mutt_sleep (short s)
1394 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1395 * just initializes. Frees anything already in the buffer.
1397 * Disregards the 'destroy' flag, which seems reserved for caller.
1398 * This is bad, but there's no apparent protocol for it.
1400 BUFFER * mutt_buffer_init(BUFFER *b)
1404 b = safe_malloc(sizeof(BUFFER));
1412 memset(b, 0, sizeof(BUFFER));
1417 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1418 * just initializes. Frees anything already in the buffer. Copies in
1421 * Disregards the 'destroy' flag, which seems reserved for caller.
1422 * This is bad, but there's no apparent protocol for it.
1424 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1429 b = mutt_buffer_init(b);
1430 b->data = safe_strdup (seed);
1431 b->dsize = mutt_strlen (seed);
1432 b->dptr = (char *) b->data + b->dsize;
1436 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1438 mutt_buffer_add (buf, s, mutt_strlen (s));
1441 void mutt_buffer_addch (BUFFER* buf, char c)
1443 mutt_buffer_add (buf, &c, 1);
1446 void mutt_buffer_free (BUFFER **p)
1452 /* dptr is just an offset to data and shouldn't be freed */
1456 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1457 * Always one byte bigger than necessary for the null terminator, and
1458 * the buffer is always null-terminated */
1459 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1463 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1465 offset = buf->dptr - buf->data;
1466 buf->dsize += len < 128 ? 128 : len + 1;
1467 safe_realloc ((void**) &buf->data, buf->dsize);
1468 buf->dptr = buf->data + offset;
1470 memcpy (buf->dptr, s, len);
1472 *(buf->dptr) = '\0';
1475 /* Decrease a file's modification time by 1 second */
1477 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1479 struct utimbuf utim;
1485 if (stat (f, &_st) == -1)
1490 if ((mtime = st->st_mtime) == time (NULL))
1493 utim.actime = mtime;
1494 utim.modtime = mtime;
1501 const char *mutt_make_version (void)
1503 static char vstring[STRING];
1504 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1505 MUTT_VERSION, ReleaseDate);
1509 REGEXP *mutt_compile_regexp (const char *s, int flags)
1511 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1512 pp->pattern = safe_strdup (s);
1513 pp->rx = safe_calloc (sizeof (regex_t), 1);
1514 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1515 mutt_free_regexp (&pp);
1520 void mutt_free_regexp (REGEXP **pp)
1522 FREE (&(*pp)->pattern);
1523 regfree ((*pp)->rx);
1528 void mutt_free_rx_list (RX_LIST **list)
1536 *list = (*list)->next;
1537 mutt_free_regexp (&p->rx);
1542 void mutt_free_spam_list (SPAM_LIST **list)
1550 *list = (*list)->next;
1551 mutt_free_regexp (&p->rx);
1552 safe_free(&p->template);
1557 int mutt_match_rx_list (const char *s, RX_LIST *l)
1561 for (; l; l = l->next)
1563 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1565 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1573 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1575 static regmatch_t *pmatch = NULL;
1576 static int nmatch = 0;
1584 for (; l; l = l->next)
1586 /* If this pattern needs more matches, expand pmatch. */
1587 if (l->nmatch > nmatch)
1589 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1593 /* Does this pattern match? */
1594 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1596 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1597 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", l->rx->rx->re_nsub));
1599 /* Copy template into text, with substitutions. */
1600 for (p = l->template; *p;)
1604 n = atoi(++p); /* find pmatch index */
1606 ++p; /* skip subst token */
1607 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1608 text[tlen++] = s[i];
1612 text[tlen++] = *p++;
1616 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));