From: Pietro Cerutti Date: Mon, 24 Jul 2017 12:08:15 +0000 (+0000) Subject: Convert userhdrs to STailQ X-Git-Tag: neomutt-20170907~35^2~19 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=79868b1f075f197e90f3c6f1cdb79ad2ee760f98;p=neomutt Convert userhdrs to STailQ Issue #374 --- diff --git a/envelope.h b/envelope.h index d6290793d..2ea10fdb5 100644 --- a/envelope.h +++ b/envelope.h @@ -59,7 +59,7 @@ struct Envelope struct Buffer *spam; struct STailQHead references; /**< message references (in reverse order) */ struct STailQHead in_reply_to; /**< in-reply-to header content */ - struct List *userhdrs; /**< user defined headers */ + struct STailQHead userhdrs; /**< user defined headers */ int kwtypes; bool irt_changed : 1; /**< In-Reply-To changed to link/break threads */ @@ -71,6 +71,7 @@ static inline struct Envelope *mutt_new_envelope(void) struct Envelope *e = safe_calloc(1, sizeof(struct Envelope)); STAILQ_INIT(&e->references); STAILQ_INIT(&e->in_reply_to); + STAILQ_INIT(&e->userhdrs); return e; } diff --git a/hcache/hcache.c b/hcache/hcache.c index 250f2c2c4..b33705b69 100644 --- a/hcache/hcache.c +++ b/hcache/hcache.c @@ -286,25 +286,6 @@ static void restore_address(struct Address **a, const unsigned char *d, int *off *a = NULL; } -static unsigned char *dump_list(struct List *l, unsigned char *d, int *off, int convert) -{ - unsigned int counter = 0; - unsigned int start_off = *off; - - d = dump_int(0xdeadbeef, d, off); - - while (l) - { - d = dump_char(l->data, d, off, convert); - l = l->next; - counter++; - } - - memcpy(d + start_off, &counter, sizeof(int)); - - return d; -} - static unsigned char *dump_stailq(struct STailQHead *l, unsigned char *d, int *off, int convert) { unsigned int counter = 0; @@ -324,23 +305,6 @@ static unsigned char *dump_stailq(struct STailQHead *l, unsigned char *d, int *o return d; } -static void restore_list(struct List **l, const unsigned char *d, int *off, int convert) -{ - unsigned int counter; - - restore_int(&counter, d, off); - - while (counter) - { - *l = safe_malloc(sizeof(struct List)); - restore_char(&(*l)->data, d, off, convert); - l = &(*l)->next; - counter--; - } - - *l = NULL; -} - static void restore_stailq(struct STailQHead *l, const unsigned char *d, int *off, int convert) { unsigned int counter; @@ -510,7 +474,7 @@ static unsigned char *dump_envelope(struct Envelope *e, unsigned char *d, int *o d = dump_stailq(&e->references, d, off, 0); d = dump_stailq(&e->in_reply_to, d, off, 0); - d = dump_list(e->userhdrs, d, off, convert); + d = dump_stailq(&e->userhdrs, d, off, convert); #ifdef USE_NNTP d = dump_char(e->xref, d, off, 0); @@ -552,7 +516,7 @@ static void restore_envelope(struct Envelope *e, const unsigned char *d, int *of restore_stailq(&e->references, d, off, 0); restore_stailq(&e->in_reply_to, d, off, 0); - restore_list(&e->userhdrs, d, off, convert); + restore_stailq(&e->userhdrs, d, off, convert); #ifdef USE_NNTP restore_char(&e->xref, d, off, 0); diff --git a/headers.c b/headers.c index fde5cc2ba..46841b482 100644 --- a/headers.c +++ b/headers.c @@ -54,7 +54,6 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg, struct Envelope *n = NULL; time_t mtime; struct stat st; - struct List *cur = NULL, **last = NULL, *tmp = NULL; mutt_mktemp(path, sizeof(path)); if ((ofp = safe_fopen(path, "w")) == NULL) @@ -99,7 +98,7 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg, } mutt_unlink(body); - mutt_free_list(&msg->env->userhdrs); + mutt_free_stailq(&msg->env->userhdrs); /* Read the temp file back in */ if ((ifp = fopen(path, "r")) == NULL) @@ -150,15 +149,14 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg, * fcc: or attach: or pgp: was specified */ - cur = msg->env->userhdrs; - last = &msg->env->userhdrs; - while (cur) + struct STailQNode *np, *tmp; + STAILQ_FOREACH_SAFE(np, &msg->env->userhdrs, entries, tmp) { keep = true; - if (fcc && (mutt_strncasecmp("fcc:", cur->data, 4) == 0)) + if (fcc && (mutt_strncasecmp("fcc:", np->data, 4) == 0)) { - p = skip_email_wsp(cur->data + 4); + p = skip_email_wsp(np->data + 4); if (*p) { strfcpy(fcc, p, fcclen); @@ -166,13 +164,13 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg, } keep = false; } - else if (mutt_strncasecmp("attach:", cur->data, 7) == 0) + else if (mutt_strncasecmp("attach:", np->data, 7) == 0) { struct Body *body2 = NULL; struct Body *parts = NULL; size_t l = 0; - p = skip_email_wsp(cur->data + 7); + p = skip_email_wsp(np->data + 7); if (*p) { for (; *p && *p != ' ' && *p != '\t'; p++) @@ -206,26 +204,19 @@ void mutt_edit_headers(const char *editor, const char *body, struct Header *msg, keep = false; } else if ((WithCrypto & APPLICATION_PGP) && - (mutt_strncasecmp("pgp:", cur->data, 4) == 0)) + (mutt_strncasecmp("pgp:", np->data, 4) == 0)) { - msg->security = mutt_parse_crypt_hdr(cur->data + 4, 0, APPLICATION_PGP); + msg->security = mutt_parse_crypt_hdr(np->data + 4, 0, APPLICATION_PGP); if (msg->security) msg->security |= APPLICATION_PGP; keep = false; } - if (keep) + if (!keep) { - last = &cur->next; - cur = cur->next; - } - else - { - tmp = cur; - *last = cur->next; - cur = cur->next; - tmp->next = NULL; - mutt_free_list(&tmp); + STAILQ_REMOVE(&msg->env->userhdrs, np, STailQNode, entries); + FREE(&np->data); + FREE(&np); } } } diff --git a/main.c b/main.c index 936dafb3e..27f1deb6d 100644 --- a/main.c +++ b/main.c @@ -688,7 +688,6 @@ int main(int argc, char **argv, char **env) struct Header *context_hdr = NULL; struct Envelope *opts_env = msg->env; struct stat st; - struct List *uh = NULL, **last_uhp = NULL; sendflags |= SENDDRAFTFILE; @@ -708,19 +707,18 @@ int main(int argc, char **argv, char **env) mutt_prepare_template(fin, NULL, msg, context_hdr, 0); /* Scan for mutt header to set OPT_RESUME_DRAFT_FILES */ - for (last_uhp = &msg->env->userhdrs, uh = *last_uhp; uh; uh = *last_uhp) + struct STailQNode *np, *tmp; + STAILQ_FOREACH_SAFE(np, &msg->env->userhdrs, entries, tmp) { - if (mutt_strncasecmp("X-Mutt-Resume-Draft:", uh->data, 20) == 0) + if (mutt_strncasecmp("X-Mutt-Resume-Draft:", np->data, 20) == 0) { if (option(OPT_RESUME_EDITED_DRAFT_FILES)) set_option(OPT_RESUME_DRAFT_FILES); - *last_uhp = uh->next; - uh->next = NULL; - mutt_free_list(&uh); + STAILQ_REMOVE(&msg->env->userhdrs, np, STailQNode, entries); + FREE(&np->data); + FREE(&np); } - else - last_uhp = &uh->next; } rfc822_append(&msg->env->to, opts_env->to, 0); diff --git a/muttlib.c b/muttlib.c index d387491ce..b5e081b3b 100644 --- a/muttlib.c +++ b/muttlib.c @@ -745,7 +745,7 @@ void mutt_free_envelope(struct Envelope **p) mutt_free_stailq(&(*p)->references); mutt_free_stailq(&(*p)->in_reply_to); - mutt_free_list(&(*p)->userhdrs); + mutt_free_stailq(&(*p)->userhdrs); FREE(p); } @@ -808,9 +808,9 @@ void mutt_merge_envelopes(struct Envelope *base, struct Envelope **extra) /* spam and user headers should never be hashed, and the new envelope may * have better values. Use new versions regardless. */ mutt_buffer_free(&base->spam); - mutt_free_list(&base->userhdrs); + mutt_free_stailq(&base->userhdrs); MOVE_ELEM(spam); - MOVE_ELEM(userhdrs); + MOVE_STAILQ(userhdrs); #undef MOVE_ELEM mutt_free_envelope(extra); diff --git a/parse.c b/parse.c index 2fb95afbb..281b13364 100644 --- a/parse.c +++ b/parse.c @@ -987,14 +987,9 @@ void mutt_parse_mime_message(struct Context *ctx, struct Header *cur) } int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line, - char *p, short user_hdrs, short weed, short do_2047, - struct List **lastp) + char *p, short user_hdrs, short weed, short do_2047) { int matched = 0; - struct List *last = NULL; - - if (lastp) - last = *lastp; switch (tolower(line[0])) { @@ -1345,25 +1340,16 @@ int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line, /* restore the original line */ line[strlen(line)] = ':'; - if (weed && option(OPT_WEED) && mutt_matches_ignore(line)) - goto done; - - if (last) + if (!(weed && option(OPT_WEED) && mutt_matches_ignore(line))) { - last->next = mutt_new_list(); - last = last->next; + struct STailQNode *np = calloc(1, sizeof(struct STailQNode)); + np->data = safe_strdup(line); + STAILQ_INSERT_TAIL(&e->userhdrs, np, entries); + if (do_2047) + rfc2047_decode(&np->data); } - else - last = e->userhdrs = mutt_new_list(); - last->data = safe_strdup(line); - if (do_2047) - rfc2047_decode(&last->data); } -done: - - if (lastp) - *lastp = last; return matched; } @@ -1384,7 +1370,6 @@ struct Envelope *mutt_read_rfc822_header(FILE *f, struct Header *hdr, short user_hdrs, short weed) { struct Envelope *e = mutt_new_envelope(); - struct List *last = NULL; char *line = safe_malloc(LONG_STRING); char *p = NULL; LOFF_T loc; @@ -1480,7 +1465,7 @@ struct Envelope *mutt_read_rfc822_header(FILE *f, struct Header *hdr, if (!*p) continue; /* skip empty header fields */ - mutt_parse_rfc822_line(e, hdr, line, p, user_hdrs, weed, 1, &last); + mutt_parse_rfc822_line(e, hdr, line, p, user_hdrs, weed, 1); } FREE(&line); diff --git a/postpone.c b/postpone.c index 0f0a3f835..e806a7edb 100644 --- a/postpone.c +++ b/postpone.c @@ -263,9 +263,6 @@ int mutt_get_postponed(struct Context *ctx, struct Header *hdr, { struct Header *h = NULL; int code = SENDPOSTPONED; - struct List *tmp = NULL; - struct List *last = NULL; - struct List *next = NULL; const char *p = NULL; int opt_delete; @@ -322,47 +319,29 @@ int mutt_get_postponed(struct Context *ctx, struct Header *hdr, FREE(&PostContext); - for (tmp = hdr->env->userhdrs; tmp;) + struct STailQNode *np, *tmp; + STAILQ_FOREACH_SAFE(np, &hdr->env->userhdrs, entries, tmp) { - if (mutt_strncasecmp("X-Mutt-References:", tmp->data, 18) == 0) + if (mutt_strncasecmp("X-Mutt-References:", np->data, 18) == 0) { if (ctx) { /* if a mailbox is currently open, look to see if the original message the user attempted to reply to is in this mailbox */ - p = skip_email_wsp(tmp->data + 18); + p = skip_email_wsp(np->data + 18); if (!ctx->id_hash) ctx->id_hash = mutt_make_id_hash(ctx); *cur = hash_find(ctx->id_hash, p); } - - /* Remove the X-Mutt-References: header field. */ - next = tmp->next; - if (last) - last->next = tmp->next; - else - hdr->env->userhdrs = tmp->next; - tmp->next = NULL; - mutt_free_list(&tmp); - tmp = next; if (*cur) code |= SENDREPLY; } - else if (mutt_strncasecmp("X-Mutt-Fcc:", tmp->data, 11) == 0) + else if (mutt_strncasecmp("X-Mutt-Fcc:", np->data, 11) == 0) { - p = skip_email_wsp(tmp->data + 11); + p = skip_email_wsp(np->data + 11); strfcpy(fcc, p, fcclen); mutt_pretty_mailbox(fcc, fcclen); - /* remove the X-Mutt-Fcc: header field */ - next = tmp->next; - if (last) - last->next = tmp->next; - else - hdr->env->userhdrs = tmp->next; - tmp->next = NULL; - mutt_free_list(&tmp); - tmp = next; /* note that x-mutt-fcc was present. we do this because we want to add a * default fcc if the header was missing, but preserve the request of the * user to not make a copy if the header field is present, but empty. @@ -371,70 +350,46 @@ int mutt_get_postponed(struct Context *ctx, struct Header *hdr, code |= SENDPOSTPONEDFCC; } else if ((WithCrypto & APPLICATION_PGP) && - ((mutt_strncmp("Pgp:", tmp->data, 4) == 0) /* this is generated - * by old mutt versions - */ - || (mutt_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0))) + ((mutt_strncmp("Pgp:", np->data, 4) == 0) /* this is generated + * by old mutt versions + */ + || (mutt_strncmp("X-Mutt-PGP:", np->data, 11) == 0))) { - hdr->security = mutt_parse_crypt_hdr(strchr(tmp->data, ':') + 1, 1, APPLICATION_PGP); + hdr->security = mutt_parse_crypt_hdr(strchr(np->data, ':') + 1, 1, APPLICATION_PGP); hdr->security |= APPLICATION_PGP; - - /* remove the pgp field */ - next = tmp->next; - if (last) - last->next = tmp->next; - else - hdr->env->userhdrs = tmp->next; - tmp->next = NULL; - mutt_free_list(&tmp); - tmp = next; } else if ((WithCrypto & APPLICATION_SMIME) && - (mutt_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0)) + (mutt_strncmp("X-Mutt-SMIME:", np->data, 13) == 0)) { - hdr->security = mutt_parse_crypt_hdr(strchr(tmp->data, ':') + 1, 1, APPLICATION_SMIME); + hdr->security = mutt_parse_crypt_hdr(strchr(np->data, ':') + 1, 1, APPLICATION_SMIME); hdr->security |= APPLICATION_SMIME; - - /* remove the smime field */ - next = tmp->next; - if (last) - last->next = tmp->next; - else - hdr->env->userhdrs = tmp->next; - tmp->next = NULL; - mutt_free_list(&tmp); - tmp = next; } #ifdef MIXMASTER - else if (mutt_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) + else if (mutt_strncmp("X-Mutt-Mix:", np->data, 11) == 0) { char *t = NULL; mutt_free_list(&hdr->chain); - t = strtok(tmp->data + 11, " \t\n"); + t = strtok(np->data + 11, " \t\n"); while (t) { hdr->chain = mutt_add_list(hdr->chain, t); t = strtok(NULL, " \t\n"); } - - next = tmp->next; - if (last) - last->next = tmp->next; - else - hdr->env->userhdrs = tmp->next; - tmp->next = NULL; - mutt_free_list(&tmp); - tmp = next; } #endif else { - last = tmp; - tmp = tmp->next; + // skip header removal + continue; } + + // remove the header + STAILQ_REMOVE(&hdr->env->userhdrs, np, STailQNode, entries); + FREE(&np->data); + FREE(&np); } if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT)) diff --git a/protos.h b/protos.h index 2ef06ec44..9b0290fcc 100644 --- a/protos.h +++ b/protos.h @@ -336,7 +336,7 @@ int mutt_parse_unmono(struct Buffer *buf, struct Buffer *s, unsigned long data, int mutt_parse_push(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); int mutt_parse_rc_line(/* const */ char *line, struct Buffer *token, struct Buffer *err); int mutt_parse_rfc822_line(struct Envelope *e, struct Header *hdr, char *line, char *p, - short user_hdrs, short weed, short do_2047, struct List **lastp); + short user_hdrs, short weed, short do_2047); int mutt_parse_score(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); int mutt_parse_unscore(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); int mutt_parse_unhook(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err); diff --git a/send.c b/send.c index 8ad30c0b2..cd7ed4ba6 100644 --- a/send.c +++ b/send.c @@ -364,11 +364,6 @@ static void process_user_recips(struct Envelope *env) static void process_user_header(struct Envelope *env) { struct List *uh = UserHeader; - struct List *last = env->userhdrs; - - if (last) - while (last->next) - last = last->next; for (; uh; uh = uh->next) { @@ -406,14 +401,9 @@ static void process_user_header(struct Envelope *env) (mutt_strncasecmp("subject:", uh->data, 8) != 0) && (mutt_strncasecmp("return-path:", uh->data, 12) != 0)) { - if (last) - { - last->next = mutt_new_list(); - last = last->next; - } - else - last = env->userhdrs = mutt_new_list(); - last->data = safe_strdup(uh->data); + struct STailQNode *np = safe_calloc(1, sizeof(struct STailQNode)); + np->data = safe_strdup(uh->data); + STAILQ_INSERT_TAIL(&env->userhdrs, np, entries); } } } diff --git a/sendlib.c b/sendlib.c index 9ea786805..26bcca61b 100644 --- a/sendlib.c +++ b/sendlib.c @@ -2000,7 +2000,6 @@ int mutt_write_rfc822_header(FILE *fp, struct Envelope *env, { char buffer[LONG_STRING]; char *p = NULL, *q = NULL; - struct List *tmp = env->userhdrs; bool has_agent = false; /* user defined user-agent header field exists */ if (mode == 0 && !privacy) @@ -2124,7 +2123,8 @@ int mutt_write_rfc822_header(FILE *fp, struct Envelope *env, } /* Add any user defined headers */ - for (; tmp; tmp = tmp->next) + struct STailQNode *tmp; + STAILQ_FOREACH(tmp, &env->userhdrs, entries) { if ((p = strchr(tmp->data, ':'))) { @@ -2164,18 +2164,19 @@ int mutt_write_rfc822_header(FILE *fp, struct Envelope *env, return (ferror(fp) == 0 ? 0 : -1); } -static void encode_headers(struct List *h) +static void encode_headers(struct STailQHead *h) { char *tmp = NULL; char *p = NULL; int i; - for (; h; h = h->next) + struct STailQNode *np; + STAILQ_FOREACH(np, h, entries) { - if (!(p = strchr(h->data, ':'))) + if (!(p = strchr(np->data, ':'))) continue; - i = p - h->data; + i = p - np->data; p = skip_email_wsp(p + 1); tmp = safe_strdup(p); @@ -2183,9 +2184,9 @@ static void encode_headers(struct List *h) continue; rfc2047_encode_string(&tmp); - safe_realloc(&h->data, mutt_strlen(h->data) + 2 + mutt_strlen(tmp) + 1); + safe_realloc(&np->data, mutt_strlen(np->data) + 2 + mutt_strlen(tmp) + 1); - sprintf(h->data + i, ": %s", NONULL(tmp)); + sprintf(np->data + i, ": %s", NONULL(tmp)); FREE(&tmp); } @@ -2657,15 +2658,16 @@ void mutt_prepare_envelope(struct Envelope *env, int final) { rfc2047_encode_string(&env->subject); } - encode_headers(env->userhdrs); + encode_headers(&env->userhdrs); } void mutt_unprepare_envelope(struct Envelope *env) { - struct List *item = NULL; - - for (item = env->userhdrs; item; item = item->next) + struct STailQNode *item; + STAILQ_FOREACH(item, &env->userhdrs, entries) + { rfc2047_decode(&item->data); + } rfc822_free_address(&env->mail_followup_to); diff --git a/url.c b/url.c index 8577ec51c..02c208be3 100644 --- a/url.c +++ b/url.c @@ -273,8 +273,6 @@ int url_parse_mailto(struct Envelope *e, char **body, const char *src) int rc = -1; - struct List *last = NULL; - if (!(t = strchr(src, ':'))) return -1; @@ -330,7 +328,7 @@ int url_parse_mailto(struct Envelope *e, char **body, const char *src) safe_asprintf(&scratch, "%s: %s", tag, value); scratch[taglen] = 0; /* overwrite the colon as mutt_parse_rfc822_line expects */ value = skip_email_wsp(&scratch[taglen + 1]); - mutt_parse_rfc822_line(e, NULL, scratch, value, 1, 0, 1, &last); + mutt_parse_rfc822_line(e, NULL, scratch, value, 1, 0, 1); FREE(&scratch); } }