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));
78 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);
94 strfcpy (s + sl, period, l - sl);
99 /* create a send-mode duplicate from a receive-mode body */
101 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
103 char tmp[_POSIX_PATH_MAX];
106 PARAMETER *par, **ppar;
112 strfcpy (tmp, src->filename, sizeof (tmp));
119 mutt_adv_mktemp (tmp, sizeof (tmp));
120 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
123 *tgt = mutt_new_body ();
126 memcpy (b, src, sizeof (BODY));
130 b->filename = safe_strdup (tmp);
131 b->use_disp = use_disp;
134 if (mutt_is_text_part (b))
137 b->xtype = safe_strdup (b->xtype);
138 b->subtype = safe_strdup (b->subtype);
139 b->form_name = safe_strdup (b->form_name);
140 b->filename = safe_strdup (b->filename);
141 b->d_filename = safe_strdup (b->d_filename);
142 b->description = safe_strdup (b->description);
145 * we don't seem to need the HEADER structure currently.
146 * XXX - this may change in the future
152 /* copy parameters */
153 for (par = b->parameter, ppar = &b->parameter; par;
154 ppar = &(*ppar)->next, par = par->next) {
155 *ppar = mutt_new_parameter ();
156 (*ppar)->attribute = safe_strdup (par->attribute);
157 (*ppar)->value = safe_strdup (par->value);
160 mutt_stamp_attachment (b);
167 void mutt_free_body (BODY ** p)
176 mutt_free_parameter (&b->parameter);
177 if (b->unlink && b->filename) {
178 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
179 unlink (b->filename);
181 else if (b->filename)
183 (debugfile, "mutt_free_body: Not unlinking %s.\n",
190 FREE (&b->description);
191 FREE (&b->form_name);
194 /* Don't free twice (b->hdr->content = b->parts) */
195 b->hdr->content = NULL;
196 mutt_free_header (&b->hdr);
200 mutt_free_body (&b->parts);
208 void mutt_free_parameter (PARAMETER ** p)
214 FREE (&t->attribute);
223 LIST *mutt_add_list (LIST * head, const char *data)
227 for (tmp = head; tmp && tmp->next; tmp = tmp->next);
229 tmp->next = safe_malloc (sizeof (LIST));
233 head = tmp = safe_malloc (sizeof (LIST));
235 tmp->data = safe_strdup (data);
240 void mutt_free_list (LIST ** list)
248 *list = (*list)->next;
254 HEADER *mutt_dup_header (HEADER * h)
258 hnew = mutt_new_header ();
259 memcpy (hnew, h, sizeof (HEADER));
263 void mutt_free_header (HEADER ** h)
267 mutt_free_envelope (&(*h)->env);
268 mutt_free_body (&(*h)->content);
269 FREE (&(*h)->maildir_flags);
273 mutt_free_list (&(*h)->chain);
275 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
281 /* returns true if the header contained in "s" is in list "t" */
282 int mutt_matches_ignore (const char *s, LIST * t)
284 for (; t; t = t->next) {
285 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data))
292 /* prepend the path part of *path to *link */
293 void mutt_expand_link (char *newpath, const char *path, const char *link)
295 const char *lb = NULL;
298 /* link is full path */
300 strfcpy (newpath, link, _POSIX_PATH_MAX);
304 if ((lb = strrchr (path, '/')) == NULL) {
305 /* no path in link */
306 strfcpy (newpath, link, _POSIX_PATH_MAX);
311 memcpy (newpath, path, len);
312 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
315 char *mutt_expand_path (char *s, size_t slen)
317 return _mutt_expand_path (s, slen, 0);
320 char *_mutt_expand_path (char *s, size_t slen, int rx)
322 char p[_POSIX_PATH_MAX] = "";
323 char q[_POSIX_PATH_MAX] = "";
324 char tmp[_POSIX_PATH_MAX];
337 if (*(s + 1) == '/' || *(s + 1) == 0) {
338 strfcpy (p, NONULL (Homedir), sizeof (p));
344 if ((t = strchr (s + 1, '/')))
347 if ((pw = getpwnam (s + 1))) {
348 strfcpy (p, pw->pw_dir, sizeof (p));
357 /* user not found! */
371 /* if folder = {host} or imap[s]://host/: don't append slash */
372 if (mx_is_imap (NONULL (Maildir)) &&
373 (Maildir[strlen (Maildir) - 1] == '}' ||
374 Maildir[strlen (Maildir) - 1] == '/'))
375 strfcpy (p, NONULL (Maildir), sizeof (p));
378 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
384 /* elm compatibility, @ expands alias to user name */
391 if ((alias = mutt_lookup_alias (s + 1))) {
392 h = mutt_new_header ();
393 h->env = mutt_new_envelope ();
394 h->env->from = h->env->to = alias;
395 mutt_default_save (p, sizeof (p), h);
396 h->env->from = h->env->to = NULL;
397 mutt_free_header (&h);
398 /* Avoid infinite recursion if the resulting folder starts with '@' */
409 strfcpy (p, NONULL (Inbox), sizeof (p));
416 strfcpy (p, NONULL (Outbox), sizeof (p));
423 if (*(s + 1) == '!') {
424 strfcpy (p, NONULL (LastFolder), sizeof (p));
428 strfcpy (p, NONULL (Spoolfile), sizeof (p));
436 strfcpy (p, NONULL (LastFolder), sizeof (p));
443 strfcpy (p, NONULL (CurrentFolder), sizeof (p));
455 if (rx && *p && !recurse) {
456 mutt_rx_sanitize_string (q, sizeof (q), p);
457 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
460 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
462 strfcpy (s, tmp, slen);
467 /* Rewrite IMAP path in canonical form - aids in string comparisons of
468 * folders. May possibly fail, in which case s should be the same. */
470 imap_expand_path (s, slen);
476 /* Extract the real name from /etc/passwd's GECOS field.
477 * When set, honor the regular expression in GecosMask,
478 * otherwise assume that the GECOS field is a
479 * comma-separated list.
480 * Replace "&" by a capitalized version of the user's login
484 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
486 regmatch_t pat_match[1];
491 if (!pw || !pw->pw_gecos)
494 memset (dest, 0, destlen);
497 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
498 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
499 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
501 else if ((p = strchr (pw->pw_gecos, ',')))
502 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
504 strfcpy (dest, pw->pw_gecos, destlen);
506 pwnl = strlen (pw->pw_name);
508 for (idx = 0; dest[idx]; idx++) {
509 if (dest[idx] == '&') {
510 memmove (&dest[idx + pwnl], &dest[idx + 1],
511 MAX (destlen - idx - pwnl - 1, 0));
512 memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
513 dest[idx] = toupper ((unsigned char) dest[idx]);
521 char *mutt_get_parameter (const char *s, PARAMETER * p)
523 for (; p; p = p->next)
524 if (ascii_strcasecmp (s, p->attribute) == 0)
530 void mutt_set_parameter (const char *attribute, const char *value,
536 mutt_delete_parameter (attribute, p);
540 for (q = *p; q; q = q->next) {
541 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
542 mutt_str_replace (&q->value, value);
547 q = mutt_new_parameter ();
548 q->attribute = safe_strdup (attribute);
549 q->value = safe_strdup (value);
554 void mutt_delete_parameter (const char *attribute, PARAMETER ** p)
558 for (q = *p; q; p = &q->next, q = q->next) {
559 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
562 mutt_free_parameter (&q);
568 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
569 int mutt_needs_mailcap (BODY * m)
574 if (!ascii_strcasecmp ("plain", m->subtype) ||
575 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
576 !ascii_strcasecmp ("enriched", m->subtype))
580 case TYPEAPPLICATION:
581 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (m))
583 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (m))
595 int mutt_is_text_part (BODY * b)
598 char *s = b->subtype;
600 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
606 if (t == TYPEMESSAGE) {
607 if (!ascii_strcasecmp ("delivery-status", s))
611 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION) {
612 if (!ascii_strcasecmp ("pgp-keys", s))
619 void mutt_free_envelope (ENVELOPE ** p)
623 rfc822_free_address (&(*p)->return_path);
624 rfc822_free_address (&(*p)->from);
625 rfc822_free_address (&(*p)->to);
626 rfc822_free_address (&(*p)->cc);
627 rfc822_free_address (&(*p)->bcc);
628 rfc822_free_address (&(*p)->sender);
629 rfc822_free_address (&(*p)->reply_to);
630 rfc822_free_address (&(*p)->mail_followup_to);
632 FREE (&(*p)->list_post);
633 FREE (&(*p)->subject);
634 /* real_subj is just an offset to subject and shouldn't be freed */
635 FREE (&(*p)->message_id);
636 FREE (&(*p)->supersedes);
638 FREE (&(*p)->x_label);
639 FREE (&(*p)->organization);
641 FREE (&(*p)->newsgroups);
643 FREE (&(*p)->followup_to);
644 FREE (&(*p)->x_comment_to);
647 mutt_buffer_free (&(*p)->spam);
648 mutt_free_list (&(*p)->references);
649 mutt_free_list (&(*p)->in_reply_to);
650 mutt_free_list (&(*p)->userhdrs);
654 void _mutt_mktemp (char *s, const char *src, int line)
656 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir),
657 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++);
659 (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
663 void mutt_free_alias (ALIAS ** p)
671 rfc822_free_address (&t->addr);
676 /* collapse the pathname using ~ or = when possible */
677 void mutt_pretty_mailbox (char *s)
683 scheme = url_check_scheme (s);
686 if (scheme == U_IMAP || scheme == U_IMAPS) {
687 imap_pretty_mailbox (s);
692 /* if s is an url, only collapse path component */
693 if (scheme != U_UNKNOWN) {
694 p = strchr (s, ':') + 1;
695 if (!strncmp (p, "//", 2))
696 q = strchr (p + 2, '/');
698 q = strchr (p, '\0');
702 /* first attempt to collapse the pathname */
704 if (*p == '/' && p[1] == '/') {
708 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
717 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
720 memmove (s, s + len, mutt_strlen (s + len) + 1);
722 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
725 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
729 void mutt_pretty_size (char *s, size_t len, long n)
732 strfcpy (s, "0K", len);
733 else if (n < 10189) /* 0.1K - 9.9K */
734 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
735 else if (n < 1023949) { /* 10K - 999K */
736 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
737 snprintf (s, len, "%ldK", (n + 51) / 1024);
739 else if (n < 10433332) /* 1.0M - 9.9M */
740 snprintf (s, len, "%3.1fM", n / 1048576.0);
743 /* (10433332 + 52428) / 1048576 = 10 */
744 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
748 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt,
751 char tmp[LONG_STRING];
753 mutt_quote_filename (tmp, sizeof (tmp), src);
754 mutt_expand_fmt (dest, destlen, fmt, tmp);
757 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
765 slen = mutt_strlen (src);
768 for (p = fmt, d = dest; destlen && *p; p++) {
777 strfcpy (d, src, destlen + 1);
778 d += destlen > slen ? slen : destlen;
779 destlen -= destlen > slen ? slen : destlen;
796 if (!found && destlen > 0) {
797 safe_strcat (dest, destlen, " ");
798 safe_strcat (dest, destlen, src);
803 /* return 0 on success, -1 on abort, 1 on error */
804 int mutt_check_overwrite (const char *attname, const char *path,
805 char *fname, size_t flen, int *append,
809 char tmp[_POSIX_PATH_MAX];
812 strfcpy (fname, path, flen);
813 if (access (fname, F_OK) != 0)
815 if (stat (fname, &st) != 0)
817 if (S_ISDIR (st.st_mode)) {
819 switch (mutt_multi_choice
820 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
823 mutt_str_replace (directory, fname);
838 mutt_yesorno (_("File is a directory, save under it?"),
840 return (rc == M_NO) ? 1 : -1;
842 if (!attname || !attname[0]) {
844 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
845 M_FILE | M_CLEAR) != 0 || !tmp[0])
847 mutt_concat_path (fname, path, tmp, flen);
850 mutt_concat_path (fname, path, mutt_basename (attname), flen);
853 if (*append == 0 && access (fname, F_OK) == 0) {
854 switch (mutt_multi_choice
855 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
863 *append = M_SAVE_APPEND;
865 case 1: /* overwrite */
866 *append = M_SAVE_OVERWRITE;
873 void mutt_save_path (char *d, size_t dsize, ADDRESS * a)
875 if (a && a->mailbox) {
876 strfcpy (d, a->mailbox, dsize);
877 if (!option (OPTSAVEADDRESS)) {
880 if ((p = strpbrk (d, "%@")))
889 void mutt_safe_path (char *s, size_t l, ADDRESS * a)
893 mutt_save_path (s, l, a);
895 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
899 /* counts how many characters in s can be skipped while none of the
900 * characters of c appears */
901 int mutt_skipchars (const char *s, const char *c)
907 register const char *t = c;
917 return (mutt_strlen (p));
920 void mutt_FormatString (char *dest, /* output buffer */
921 size_t destlen, /* output buffer len */
922 const char *src, /* template string */
923 format_t * callback, /* callback for processing */
924 unsigned long data, /* callback data */
926 { /* callback flags */
927 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
928 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
929 size_t wlen, count, len, col, wid;
932 destlen--; /* save room for the terminal \0 */
933 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
936 while (*src && wlen < destlen) {
947 flags |= M_FORMAT_OPTIONAL;
951 flags &= ~M_FORMAT_OPTIONAL;
953 /* eat the format string */
956 while (count < sizeof (prefix) &&
957 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
966 break; /* bad format */
968 ch = *src++; /* save the character to switch on */
970 if (flags & M_FORMAT_OPTIONAL) {
972 break; /* bad format */
975 /* eat the `if' part of the string */
978 while (count < sizeof (ifstring) && *src && *src != '?'
985 /* eat the `else' part of the string (optional) */
987 src++; /* skip the & */
990 while (count < sizeof (elsestring) && *src && *src != '?') {
997 break; /* bad format */
999 src++; /* move past the trailing `?' */
1002 /* handle generic cases first */
1004 /* right justify to EOL */
1005 ch = *src++; /* pad char */
1006 /* calculate space left on line. if we've already written more data
1007 than will fit on the line, ignore the rest of the line */
1008 if (DrawFullLine || option (OPTSTATUSONTOP))
1009 count = (COLS < destlen ? COLS : destlen);
1012 ((COLS - SidebarWidth) <
1013 destlen ? (COLS - SidebarWidth) : destlen);
1015 count -= col; /* how many columns left on this line */
1016 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1017 wid = mutt_strlen (buf);
1019 count -= wid; /* how many chars to pad */
1020 memset (wptr, ch, count);
1024 if (wid + wlen > destlen)
1025 len = destlen - wlen;
1028 memcpy (wptr, buf, len);
1031 col += mutt_strwidth (buf);
1033 break; /* skip rest of input */
1035 else if (ch == '|') {
1040 if (destlen > wlen) {
1041 count = destlen - wlen;
1042 memset (wptr, ch, count);
1045 break; /* skip rest of input */
1051 while (ch == '_' || ch == ':') {
1060 /* use callback function to handle this case */
1062 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
1066 mutt_strlower (buf);
1075 if ((len = mutt_strlen (buf)) + wlen > destlen)
1076 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1078 memcpy (wptr, buf, len);
1081 col += mutt_strwidth (buf);
1084 else if (*src == '\\') {
1113 unsigned int bar = mutt_skipchars (src, "%\\");
1114 char *bar2 = safe_malloc (bar + 1);
1116 strfcpy (bar2, src, bar + 1);
1121 col += mutt_strwidth (bar2);
1128 if (flags & M_FORMAT_MAKEPRINT) {
1129 /* Make sure that the string is printable by changing all non-printable
1130 chars to dots, or spaces for non-printable whitespace */
1131 for (cp = dest; *cp; cp++)
1132 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1133 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1138 /* This function allows the user to specify a command to read stdout from in
1139 place of a normal file. If the last character in the string is a pipe (|),
1140 then we assume it is a commmand to run instead of a normal file. */
1141 FILE *mutt_open_read (const char *path, pid_t * thepid)
1146 int len = mutt_strlen (path);
1148 if (path[len - 1] == '|') {
1149 /* read from a pipe */
1151 char *s = safe_strdup (path);
1155 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1159 if (stat (path, &s) < 0)
1161 if (S_ISDIR (s.st_mode)) {
1165 f = fopen (path, "r");
1171 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1172 int mutt_save_confirm (const char *s, struct stat *st)
1174 char tmp[_POSIX_PATH_MAX];
1179 magic = mx_get_magic (s);
1182 if (magic == M_POP) {
1183 mutt_error _("Can't save message to POP mailbox.");
1190 if (magic == M_NNTP) {
1191 mutt_error _("Can't save message to newsserver.");
1197 if (stat (s, st) != -1) {
1199 mutt_error (_("%s is not a mailbox!"), s);
1203 if (option (OPTCONFIRMAPPEND) &&
1204 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1205 /* if we're appending to the trash, there's no point in asking */
1207 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1208 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1216 if (magic != M_IMAP)
1217 #endif /* execute the block unconditionally if we don't use imap */
1222 if (errno == ENOENT) {
1223 if (option (OPTCONFIRMCREATE)) {
1224 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1225 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1238 CLEARLINE (LINES - 1);
1242 void state_prefix_putc (char c, STATE * s)
1244 if (s->flags & M_PENDINGPREFIX) {
1247 i = strlen (Quotebuf);
1250 if (i == sizeof (Quotebuf) - 1 || c == '\n') {
1251 char buf[2 * SHORT_STRING];
1252 int j = 0, offset = 0;
1253 regmatch_t pmatch[1];
1255 state_reset_prefix (s);
1257 ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
1259 offset += pmatch->rm_eo;
1261 if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
1265 else if (option (OPTQUOTEQUOTED) && offset) {
1266 for (i = 0; i < offset; i++)
1267 if (Quotebuf[i] != ' ')
1269 strncpy (buf, Quotebuf, j + 1);
1270 strcpy (buf + j + 1, Quotebuf + j);
1273 snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
1275 state_puts (buf, s);
1282 state_set_prefix (s);
1287 int state_printf (STATE * s, const char *fmt, ...)
1293 rv = vfprintf (s->fpout, fmt, ap);
1299 void state_mark_attach (STATE * s)
1301 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1302 state_puts (AttachmentMarker, s);
1305 void state_attach_puts (const char *t, STATE * s)
1308 state_mark_attach (s);
1311 if (*t++ == '\n' && *t)
1313 state_mark_attach (s);
1317 void mutt_display_sanitize (char *s)
1325 void mutt_sleep (short s)
1334 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1335 * just initializes. Frees anything already in the buffer.
1337 * Disregards the 'destroy' flag, which seems reserved for caller.
1338 * This is bad, but there's no apparent protocol for it.
1340 BUFFER *mutt_buffer_init (BUFFER * b)
1343 b = safe_malloc (sizeof (BUFFER));
1348 safe_free (b->data);
1350 memset (b, 0, sizeof (BUFFER));
1355 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1356 * just initializes. Frees anything already in the buffer. Copies in
1359 * Disregards the 'destroy' flag, which seems reserved for caller.
1360 * This is bad, but there's no apparent protocol for it.
1362 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
1367 b = mutt_buffer_init (b);
1368 b->data = safe_strdup (seed);
1369 b->dsize = mutt_strlen (seed);
1370 b->dptr = (char *) b->data + b->dsize;
1374 void mutt_buffer_addstr (BUFFER * buf, const char *s)
1376 mutt_buffer_add (buf, s, mutt_strlen (s));
1379 void mutt_buffer_addch (BUFFER * buf, char c)
1381 mutt_buffer_add (buf, &c, 1);
1384 void mutt_buffer_free (BUFFER ** p)
1390 /* dptr is just an offset to data and shouldn't be freed */
1394 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1395 * Always one byte bigger than necessary for the null terminator, and
1396 * the buffer is always null-terminated */
1397 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
1401 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
1402 offset = buf->dptr - buf->data;
1403 buf->dsize += len < 128 ? 128 : len + 1;
1404 safe_realloc ((void **) &buf->data, buf->dsize);
1405 buf->dptr = buf->data + offset;
1407 memcpy (buf->dptr, s, len);
1409 *(buf->dptr) = '\0';
1412 /* Decrease a file's modification time by 1 second */
1414 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1416 struct utimbuf utim;
1421 if (stat (f, &_st) == -1)
1426 if ((mtime = st->st_mtime) == time (NULL)) {
1428 utim.actime = mtime;
1429 utim.modtime = mtime;
1436 const char *mutt_make_version (void)
1438 static char vstring[STRING];
1440 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1441 MUTT_VERSION, ReleaseDate);
1445 REGEXP *mutt_compile_regexp (const char *s, int flags)
1447 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1449 pp->pattern = safe_strdup (s);
1450 pp->rx = safe_calloc (sizeof (regex_t), 1);
1451 if (REGCOMP (pp->rx, NONULL (s), flags) != 0)
1452 mutt_free_regexp (&pp);
1457 void mutt_free_regexp (REGEXP ** pp)
1459 FREE (&(*pp)->pattern);
1460 regfree ((*pp)->rx);
1465 void mutt_free_rx_list (RX_LIST ** list)
1473 *list = (*list)->next;
1474 mutt_free_regexp (&p->rx);
1479 void mutt_free_spam_list (SPAM_LIST ** list)
1487 *list = (*list)->next;
1488 mutt_free_regexp (&p->rx);
1489 safe_free (&p->template);
1494 int mutt_match_rx_list (const char *s, RX_LIST * l)
1499 for (; l; l = l->next) {
1500 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0) {
1502 (debugfile, "mutt_match_rx_list: %s matches %s\n", s,
1511 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1513 static regmatch_t *pmatch = NULL;
1514 static int nmatch = 0;
1523 for (; l; l = l->next) {
1524 /* If this pattern needs more matches, expand pmatch. */
1525 if (l->nmatch > nmatch) {
1526 safe_realloc (&pmatch, l->nmatch * sizeof (regmatch_t));
1530 /* Does this pattern match? */
1532 (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch,
1535 (debugfile, "mutt_match_spam_list: %s matches %s\n", s,
1538 (debugfile, "mutt_match_spam_list: %d subs\n",
1539 l->rx->rx->re_nsub));
1541 /* Copy template into text, with substitutions. */
1542 for (p = l->template; *p;) {
1544 n = atoi (++p); /* find pmatch index */
1545 while (isdigit (*p))
1546 ++p; /* skip subst token */
1547 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1548 text[tlen++] = s[i];
1551 text[tlen++] = *p++;
1555 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));