Commit 544e8ba3 authored by Thomas Roessler's avatar Thomas Roessler

Add (and use) a function mutt_str_replace, which essentially

replaces the sequence:

	safe_free (&s);
	s = safe_strdup (t);
parent c8e19c98
......@@ -65,8 +65,7 @@ int mutt_get_tmp_attachment (BODY *a)
if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w")))
{
mutt_copy_stream (fpin, fpout);
FREE(&a->filename);
a->filename = safe_strdup(tempfile);
mutt_str_replace (&a->filename, tempfile);
a->unlink = 1;
if(a->stamp >= st.st_mtime)
......
......@@ -932,10 +932,9 @@ void _mutt_select_file (char *f, size_t flen, int buffy,
}
else
{
safe_free ((void **) &Mask.pattern);
mutt_str_replace (&Mask.pattern, buf);
regfree (Mask.rx);
safe_free ((void **) &Mask.rx);
Mask.pattern = safe_strdup (buf);
Mask.rx = rx;
Mask.not = not;
......
......@@ -152,8 +152,8 @@ static int pgp_send_menu (int bits, int *redraw)
if ((p = pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN, PGP_PUBRING)))
{
snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p));
safe_free((void **) &PgpSignAs); PgpSignAs = safe_strdup (input_signas);
safe_free((void **) &PgpSignMicalg); PgpSignMicalg = safe_strdup (pgp_pkalg_to_mic (p->algorithm));
mutt_str_replace (&PgpSignAs, input_signas);
mutt_str_replace (&PgpSignMicalg, pgp_pkalg_to_mic (p->algorithm));
pgp_free_key (&p);
bits |= PGPSIGN;
......@@ -188,10 +188,7 @@ static int pgp_send_menu (int bits, int *redraw)
mutt_error _("Unknown MIC algorithm, valid ones are: pgp-md5, pgp-sha1, pgp-rmd160");
}
else
{
safe_free ((void **) &PgpSignMicalg);
PgpSignMicalg = safe_strdup (input_micalg);
}
mutt_str_replace (&PgpSignMicalg, input_micalg);
}
}
break;
......@@ -487,8 +484,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */
buf[0] = 0;
if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0)
{
safe_free ((void **) &msg->env->subject);
msg->env->subject = safe_strdup (buf);
mutt_str_replace (&msg->env->subject, buf);
move (HDR_SUBJECT, HDR_XOFFSET);
clrtoeol ();
if (msg->env->subject)
......@@ -825,8 +821,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */
/* header names should not be translated */
if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0)
{
safe_free ((void **) &idx[menu->current]->content->description);
idx[menu->current]->content->description = safe_strdup (buf);
mutt_str_replace (&idx[menu->current]->content->description, buf);
menu->redraw = REDRAW_CURRENT;
}
break;
......@@ -964,8 +959,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */
if(mutt_rename_file (idx[menu->current]->content->filename, fname))
break;
safe_free ((void **) &idx[menu->current]->content->filename);
idx[menu->current]->content->filename = safe_strdup (fname);
mutt_str_replace (&idx[menu->current]->content->filename, fname);
menu->redraw = REDRAW_CURRENT;
if(idx[menu->current]->content->stamp >= st.st_mtime)
......@@ -1036,8 +1030,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */
menu->max = idxlen;
idx[menu->current]->content->type = itype;
safe_free ((void **) &idx[menu->current]->content->subtype);
idx[menu->current]->content->subtype = safe_strdup (p);
mutt_str_replace (&idx[menu->current]->content->subtype, p);
idx[menu->current]->content->unlink = 1;
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
......
......@@ -918,12 +918,8 @@ int mutt_index_menu (void)
if (Context)
{
FREE (&LastFolder);
LastFolder = safe_strdup (Context->path);
}
mutt_str_replace (&LastFolder, Context->path);
if (Context)
{
if (mx_close_mailbox (Context) != 0)
{
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
......
......@@ -253,10 +253,7 @@ static void be_edit_header (ENVELOPE *e, int force)
addstr ("Subject: ");
strfcpy (tmp, e->subject ? e->subject: "", sizeof (tmp));
if (mutt_enter_string ((unsigned char *) tmp, sizeof (tmp), LINES-1, 9, 0) == 0)
{
safe_free ((void **) &e->subject);
e->subject = safe_strdup (tmp);
}
mutt_str_replace (&e->subject, tmp);
addch ('\n');
}
......@@ -391,8 +388,7 @@ int mutt_builtin_editor (const char *path, HEADER *msg, HEADER *cur)
addstr (_("missing filename.\n"));
break;
case 's':
safe_free ((void **) &msg->env->subject);
msg->env->subject = safe_strdup (p);
mutt_str_replace (&msg->env->subject, p);
break;
case 't':
msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
......
......@@ -79,8 +79,7 @@ void mutt_history_add (history_class_t hclass, const char *s)
if (prev < 0) prev = HistSize - 1;
if (!h->hist[prev] || mutt_strcmp (h->hist[prev], s) != 0)
{
safe_free ((void **) &h->hist[h->last]);
h->hist[h->last++] = safe_strdup (s);
mutt_str_replace (&h->hist[h->last++], s);
if (h->last > HistSize - 1)
h->last = 0;
}
......
......@@ -653,20 +653,16 @@ static void mutt_restore_default (struct option_t *p)
{
case DT_STR:
if (p->init)
{
FREE (p->data);
*((char **) p->data) = safe_strdup ((char *) p->init);
}
mutt_str_replace ((char **) p->data, (char *) p->init);
break;
case DT_PATH:
if (p->init)
{
char path[_POSIX_PATH_MAX];
FREE (p->data);
strfcpy (path, (char *) p->init, sizeof (path));
mutt_expand_path (path, sizeof (path));
*((char **) p->data) = safe_strdup (path);
mutt_str_replace ((char **) p->data, path);
}
break;
case DT_ADDR:
......
......@@ -143,6 +143,13 @@ char *safe_strdup (const char *s)
return (p);
}
void mutt_str_replace (char **p, const char *s)
{
if (*p)
free (*p);
*p = safe_strdup (s);
}
/* convert all characters in the string to lowercase */
char *mutt_strlower (char *s)
{
......
......@@ -396,8 +396,7 @@ int main (int argc, char **argv)
break;
case 'F':
FREE (&Muttrc);
Muttrc = safe_strdup (optarg);
mutt_str_replace (&Muttrc, optarg);
break;
case 'f':
......
......@@ -658,8 +658,7 @@ static int menu_search (MUTTMENU *menu, int op)
_("Reverse search for: "),
buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
return (-1);
safe_free ((void **) &menu->searchBuf);
menu->searchBuf = safe_strdup (buf);
mutt_str_replace (&menu->searchBuf, buf);
menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
}
else
......
......@@ -500,10 +500,7 @@ int maildir_commit_message (CONTEXT *ctx, MESSAGE *msg, HEADER *hdr)
if (safe_rename (msg->path, full) == 0)
{
if (hdr)
{
FREE (&hdr->path);
hdr->path = safe_strdup (path);
}
mutt_str_replace (&hdr->path, path);
FREE (&msg->path);
return 0;
}
......@@ -575,10 +572,7 @@ int mh_commit_message (CONTEXT *ctx, MESSAGE *msg, HEADER *hdr)
if (safe_rename (msg->path, path) == 0)
{
if (hdr)
{
FREE (&hdr->path);
hdr->path = safe_strdup (tmp);
}
mutt_str_replace (&hdr->path, tmp);
FREE (&msg->path);
return 0;
}
......@@ -648,10 +642,7 @@ static int mh_sync_message (CONTEXT *ctx, int msgno)
{
snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
if ((rc = safe_rename (newpath, oldpath)) == 0)
{
FREE (&h->path);
h->path = safe_strdup (partpath);
}
mutt_str_replace (&h->path, partpath);
}
}
else mx_close_message (&dest);
......@@ -719,8 +710,7 @@ static int maildir_sync_message (CONTEXT *ctx, int msgno)
mutt_perror ("rename");
return (-1);
}
safe_free ((void **) &h->path);
h->path = safe_strdup (partpath);
mutt_str_replace (&h->path, partpath);
}
return (0);
}
......@@ -928,10 +918,7 @@ int mh_check_mailbox(CONTEXT *ctx, int *index_hint)
ctx->hdrs[i]->read ? "R" : ""));
if(mutt_strcmp(ctx->hdrs[i]->path, p->h->path))
{
safe_free((void **) &ctx->hdrs[i]->path);
ctx->hdrs[i]->path = safe_strdup(p->h->path);
}
mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
if(modified)
{
......
......@@ -403,8 +403,7 @@ void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p
{
if (mutt_strcasecmp (attribute, q->attribute) == 0)
{
safe_free((void **) &q->value);
q->value = safe_strdup(value);
mutt_str_replace (&q->value, value);
return;
}
}
......
......@@ -348,8 +348,7 @@ static void parse_content_disposition (char *s, BODY *ct)
* have allocated space if a "name=filename" parameter was
* specified.
*/
safe_free ((void **) &ct->filename);
ct->filename = safe_strdup (s);
mutt_str_replace (&ct->filename, s);
}
if ((s = mutt_get_parameter ("name", parms)) != 0)
ct->form_name = safe_strdup (s);
......@@ -406,8 +405,7 @@ BODY *mutt_read_mime_header (FILE *fp, int digest)
parse_content_disposition (c, p);
else if (!mutt_strcasecmp ("description", line + 8))
{
safe_free ((void **) &p->description);
p->description = safe_strdup (c);
mutt_str_replace (&p->description, c);
rfc2047_decode (p->description, p->description, mutt_strlen (p->description) + 1);
}
}
......@@ -455,8 +453,7 @@ void mutt_parse_part (FILE *fp, BODY *b)
if (!b->parts)
{
b->type = TYPETEXT;
safe_free ((void **) &b->subtype);
b->subtype = safe_strdup ("plain");
mutt_str_replace (&b->subtype, "plain");
}
}
......@@ -988,8 +985,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
{
if (hdr)
{
safe_free ((void **) &hdr->content->description);
hdr->content->description = safe_strdup (p);
mutt_str_replace (&hdr->content->description, p);
rfc2047_decode (hdr->content->description,
hdr->content->description,
mutt_strlen (hdr->content->description) + 1);
......@@ -1008,8 +1004,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
case 'd':
if (!mutt_strcasecmp ("ate", line + 1))
{
safe_free((void **)&e->date);
e->date = safe_strdup(p);
mutt_str_replace (&e->date, p);
if (hdr)
hdr->date_sent = mutt_parse_date (p, hdr);
matched = 1;
......
......@@ -618,10 +618,7 @@ pgp_key_t *pgp_ask_for_key (char *tag, char *whatfor,
if (whatfor)
{
if (l)
{
safe_free ((void **) &l->dflt);
l->dflt = safe_strdup (resp);
}
mutt_str_replace (&l->dflt, resp);
else
{
l = safe_malloc (sizeof (struct pgp_cache));
......
......@@ -482,17 +482,11 @@ int mutt_parse_pgp_hdr (char *p, int set_signas)
}
if (set_signas || *pgp_sign_as)
{
safe_free((void **) &PgpSignAs);
PgpSignAs = safe_strdup(pgp_sign_as);
}
mutt_str_replace (&PgpSignAs, pgp_sign_as);
/* the micalg field must not be empty */
if (set_signas && *pgp_sign_micalg)
{
safe_free((void **) &PgpSignMicalg);
PgpSignMicalg = safe_strdup(pgp_sign_micalg);
}
mutt_str_replace (&PgpSignMicalg, pgp_sign_micalg);
return pgp;
}
......@@ -624,8 +618,7 @@ int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
return -1;
}
safe_free ((void *) &b->filename);
b->filename = safe_strdup (file);
mutt_str_replace (&b->filename, file);
b->unlink = 1;
if (mutt_is_text_type (b->type, b->subtype))
......
......@@ -194,6 +194,7 @@ void mutt_shell_escape (void);
void mutt_show_error (void);
void mutt_signal_init (void);
void mutt_stamp_attachment (BODY *a);
void mutt_str_replace (char **p, const char *s);
void mutt_tabs_to_spaces (char *);
void mutt_tag_set_flag (int, int);
void mutt_unblock_signals (void);
......
......@@ -88,10 +88,7 @@ void mutt_update_tree (ATTACHPTR **idx, short idxlen)
if (idx[x]->tree)
{
if (mutt_strcmp (idx[x]->tree, buf) != 0)
{
safe_free ((void **) &idx[x]->tree);
idx[x]->tree = safe_strdup (buf);
}
mutt_str_replace (&idx[x]->tree, buf);
}
else
idx[x]->tree = safe_strdup (buf);
......
......@@ -150,8 +150,7 @@ static int get_field_text (char *field, char **entry,
{
field++;
field = mutt_skip_whitespace (field);
safe_free ((void **) entry);
*entry = safe_strdup (field);
mutt_str_replace (entry, field);
}
return 1;
}
......
......@@ -259,15 +259,13 @@ void rfc2047_encode_adrlist (ADDRESS *addr)
if (ptr->personal)
{
rfc2047_encode_string (buffer, sizeof (buffer), (const unsigned char *)ptr->personal);
safe_free ((void **) &ptr->personal);
ptr->personal = safe_strdup (buffer);
mutt_str_replace (&ptr->personal, buffer);
}
#ifdef EXACT_ADDRESS
if (ptr->val)
{
rfc2047_encode_string (buffer, sizeof (buffer), (const unsigned char *)ptr->val);
safe_free ((void **) &ptr->val);
ptr->val = safe_strdup (buffer);
mutt_str_replace (&ptr->val, buffer);
}
#endif
ptr = ptr->next;
......
......@@ -234,8 +234,7 @@ static int edit_envelope (ENVELOPE *en)
mutt_message _("No subject, aborting.");
return (-1);
}
safe_free ((void **) &en->subject);
en->subject = safe_strdup (buf);
mutt_str_replace (&en->subject, buf);
return 0;
}
......@@ -914,8 +913,7 @@ static void encode_descriptions (BODY *b)
if (t->description)
{
rfc2047_encode_string (tmp, sizeof (tmp), (unsigned char *) t->description);
safe_free ((void **) &t->description);
t->description = safe_strdup (tmp);
mutt_str_replace (&t->description, tmp);
}
if (t->parts)
encode_descriptions (t->parts);
......
......@@ -1381,8 +1381,7 @@ static void encode_headers (LIST *h)
snprintf (tmp, sizeof (tmp), "%s: ", h->data);
len = mutt_strlen (tmp);
rfc2047_encode_string (tmp + len, sizeof (tmp) - len, (unsigned char *) p);
safe_free ((void **) &h->data);
h->data = safe_strdup (tmp);
mutt_str_replace (&h->data, tmp);
}
}
}
......@@ -1811,8 +1810,7 @@ void mutt_prepare_envelope (ENVELOPE *env)
{
rfc2047_encode_string (buffer, sizeof (buffer) - 1,
(unsigned char *) env->subject);
safe_free ((void **) &env->subject);
env->subject = safe_strdup (buffer);
mutt_str_replace (&env->subject, buffer);
}
encode_headers (env->userhdrs);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment