From eab38ff6950a0075741f24ac9fb0d9eaf7df8671 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 17 May 2007 00:04:37 +0200 Subject: [PATCH] use buffers instead of realloc-ed string. makes code shorter. Signed-off-by: Pierre Habouzit --- imap/auth.c | 7 +++--- imap/browse.c | 11 ++++----- imap/command.c | 46 +++++++++---------------------------- imap/imap.c | 55 ++++++++++++++------------------------------- imap/imap_private.h | 6 ++--- imap/message.c | 36 +++++++++++++---------------- imap/util.c | 17 +++++++------- 7 files changed, 65 insertions(+), 113 deletions(-) diff --git a/imap/auth.c b/imap/auth.c index c51c969..fd3b8ce 100644 --- a/imap/auth.c +++ b/imap/auth.c @@ -101,8 +101,9 @@ static int imap_auth_sasl(IMAP_DATA * idata, const char *method) goto bail; if (irc == IMAP_CMD_RESPOND) { - if (sasl_decode64(idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf, - LONG_STRING - 1, &len) != SASL_OK) { + if (sasl_decode64(idata->cmd.buf.data + 2, idata->cmd.buf.len - 2, buf, + LONG_STRING - 1, &len) != SASL_OK) + { goto bail; } } @@ -148,7 +149,7 @@ static int imap_auth_sasl(IMAP_DATA * idata, const char *method) if (rc != SASL_OK) goto bail; - if (imap_code (idata->cmd.buf)) { + if (imap_code(idata->cmd.buf.data)) { mutt_sasl_setup_conn (idata->conn, saslconn); return IMAP_AUTH_SUCCESS; } diff --git a/imap/browse.c b/imap/browse.c index 4ce8f30..a1c9a2d 100644 --- a/imap/browse.c +++ b/imap/browse.c @@ -112,7 +112,7 @@ int imap_browse (char *path, struct browser_state *state) mbox[n] = '\0'; } } - } while (m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN)); + } while (m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN)); } /* if we're descending a folder, mark it as current in browser_state */ @@ -298,7 +298,8 @@ int imap_mailbox_rename (const char *mailbox) } if (imap_rename_mailbox (idata, &mx, newname) < 0) { - mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf)); + mutt_error (_("Rename failed: %s"), + imap_get_qualifier(idata->cmd.buf.data)); mutt_sleep (1); goto fail; } @@ -345,7 +346,7 @@ static int browse_add_list_result (IMAP_DATA * idata, const char *cmd, imap_add_folder (idata->delim, name, noselect, noinferiors, state, isparent); } - } while ((m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0)); + } while ((m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN) != 0)); p_delete(&mx.mbox); return 0; @@ -438,7 +439,7 @@ static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen, if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) break; - s = imap_next_word (idata->cmd.buf); + s = imap_next_word (idata->cmd.buf.data); if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) { /* There are three sections to the response, User, Other, Shared, * and maybe more by extension */ @@ -541,7 +542,7 @@ static int browse_verify_namespace (IMAP_DATA * idata, &nsi->noinferiors, &delim) != 0) return -1; nsi->listable |= (name != NULL); - } while ((m_strncmp(idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0)); + } while ((m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN) != 0)); } return 0; diff --git a/imap/command.c b/imap/command.c index c33776b..e915320 100644 --- a/imap/command.c +++ b/imap/command.c @@ -85,58 +85,32 @@ int imap_cmd_start (IMAP_DATA * idata, const char *cmd) int imap_cmd_step (IMAP_DATA * idata) { IMAP_COMMAND *cmd = &idata->cmd; - unsigned int len = 0; - int c; if (idata->status == IMAP_FATAL) { cmd_handle_fatal (idata); return IMAP_CMD_BAD; } - /* read into buffer, expanding buffer as necessary until we have a full - * line */ - do { - if (len == cmd->blen) { - p_realloc(&cmd->buf, cmd->blen + IMAP_CMD_BUFSIZE); - cmd->blen = cmd->blen + IMAP_CMD_BUFSIZE; - } - - if (len) - len--; - - c = mutt_socket_readln (cmd->buf + len, cmd->blen - len, idata->conn); - if (c <= 0) { - /* cmd_handle_fatal (idata); */ - return IMAP_CMD_BAD; - } - - len += c; - } - /* if we've read all the way to the end of the buffer, we haven't read a - * full line (mutt_socket_readln strips the \r, so we always have at least - * one character free when we've read a full line) */ - while (len == cmd->blen); - - /* don't let one large string make cmd->buf hog memory forever */ - if ((cmd->blen > IMAP_CMD_BUFSIZE) && (len <= IMAP_CMD_BUFSIZE)) { - p_realloc(&cmd->buf, IMAP_CMD_BUFSIZE); - cmd->blen = IMAP_CMD_BUFSIZE; + buffer_reset(&cmd->buf); + if (mutt_socket_readln2(&cmd->buf, idata->conn) < 0) { + /* cmd_handle_fatal (idata); */ + return IMAP_CMD_BAD; } - idata->lastread = time (NULL); + idata->lastread = time(NULL); /* handle untagged messages. The caller still gets its shot afterwards. */ - if (!m_strncmp(cmd->buf, "* ", 2) && cmd_handle_untagged (idata)) + if (!m_strncmp(cmd->buf.data, "* ", 2) && cmd_handle_untagged (idata)) return IMAP_CMD_BAD; /* server demands a continuation response from us */ - if (cmd->buf[0] == '+') + if (cmd->buf.data[0] == '+') return IMAP_CMD_RESPOND; /* tagged completion code */ - if (!m_strncmp(cmd->buf, cmd->seq, SEQLEN)) { + if (!m_strncmp(cmd->buf.data, cmd->seq, SEQLEN)) { imap_cmd_finish (idata); - return imap_code (cmd->buf) ? IMAP_CMD_OK : IMAP_CMD_NO; + return imap_code(cmd->buf.data) ? IMAP_CMD_OK : IMAP_CMD_NO; } return IMAP_CMD_CONTINUE; @@ -279,7 +253,7 @@ static int cmd_handle_untagged (IMAP_DATA * idata) char *pn; int count; - s = imap_next_word (idata->cmd.buf); + s = imap_next_word (idata->cmd.buf.data); if ((idata->state == IMAP_SELECTED) && isdigit ((unsigned char) *s)) { pn = s; diff --git a/imap/imap.c b/imap/imap.c index 77830f9..c2cde32 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -241,7 +241,7 @@ static int imap_get_delim (IMAP_DATA * idata) if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) break; - s = imap_next_word (idata->cmd.buf); + s = imap_next_word (idata->cmd.buf.data); if (ascii_strncasecmp ("LIST", s, 4) == 0) { s = imap_next_word (s); s = imap_next_word (s); @@ -276,7 +276,7 @@ static int imap_check_acl (IMAP_DATA * idata) static int imap_check_capabilities (IMAP_DATA * idata) { if (imap_exec (idata, "CAPABILITY", 0) != 0) { - imap_error ("imap_check_capabilities", idata->cmd.buf); + imap_error ("imap_check_capabilities", idata->cmd.buf.data); return -1; } @@ -329,12 +329,8 @@ IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags) if (!idata) { /* The current connection is a new connection */ - if (!(idata = imap_new_idata ())) { - mutt_socket_free (conn); - return NULL; - } - - conn->data = idata; + idata = imap_new_idata(); + conn->data = idata; idata->conn = conn; new = 1; } @@ -376,7 +372,7 @@ int imap_open_connection (IMAP_DATA * idata) return -1; } - if (ascii_strncasecmp ("* OK", idata->cmd.buf, 4) == 0) { + if (ascii_strncasecmp ("* OK", idata->cmd.buf.data, 4) == 0) { /* TODO: Parse new tagged CAPABILITY data (* OK [CAPABILITY...]) */ if (imap_check_capabilities (idata)) goto bail; @@ -412,7 +408,7 @@ int imap_open_connection (IMAP_DATA * idata) goto err_close_conn; } } - else if (ascii_strncasecmp ("* PREAUTH", idata->cmd.buf, 9) == 0) { + else if (ascii_strncasecmp ("* PREAUTH", idata->cmd.buf.data, 9) == 0) { idata->state = IMAP_AUTHENTICATED; if (imap_check_capabilities (idata) != 0) goto bail; @@ -535,7 +531,7 @@ static int imap_open_mailbox (CONTEXT * ctx) if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) break; - pc = idata->cmd.buf + 2; + pc = idata->cmd.buf.data + 2; /* Obtain list of available flags here, may be overridden by a * PERMANENTFLAGS tag in the OK response */ @@ -577,7 +573,7 @@ static int imap_open_mailbox (CONTEXT * ctx) if (rc == IMAP_CMD_NO) { char *s; - s = imap_next_word (idata->cmd.buf); /* skip seq */ + s = imap_next_word (idata->cmd.buf.data); /* skip seq */ s = imap_next_word (s); /* Skip response */ mutt_error ("%s", s); mutt_sleep (2); @@ -589,7 +585,7 @@ static int imap_open_mailbox (CONTEXT * ctx) /* check for READ-ONLY notification */ if (!ascii_strncasecmp - (imap_get_qualifier (idata->cmd.buf), "[READ-ONLY]", 11) + (imap_get_qualifier (idata->cmd.buf.data), "[READ-ONLY]", 11) && !mutt_bit_isset (idata->capabilities, ACL)) { ctx->readonly = 1; } @@ -686,25 +682,8 @@ void imap_logout (IMAP_DATA * idata) idata->status = IMAP_BYE; imap_cmd_start (idata, "LOGOUT"); while (imap_cmd_step (idata) == IMAP_CMD_CONTINUE); - p_delete(&idata->cmd.buf); - p_delete(&idata); -} - -/* -int imap_close_connection (CONTEXT *ctx) -{ - if (CTX_DATA->status != IMAP_BYE) - { - mutt_message _("Closing connection to IMAP server..."); - imap_logout (CTX_DATA); - mutt_clear_error (); - } - mutt_socket_close (CTX_DATA->conn); - CTX_DATA->state = IMAP_DISCONNECTED; - CTX_DATA->conn->data = NULL; - return 0; + imap_free_idata(&idata); } -*/ /* imap_set_flag: append str to flags if we currently have permission * according to aclbit */ @@ -858,7 +837,7 @@ int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd, err_continue && (*err_continue != M_YES)) { *err_continue = imap_continue ("imap_sync_message: STORE failed", - idata->cmd.buf); + idata->cmd.buf.data); if (*err_continue != M_YES) return -1; } @@ -958,7 +937,7 @@ int imap_sync_mailbox (CONTEXT * ctx, int expunge, int *index_hint) /* Set expunge bit so we don't get spurious reopened messages */ idata->reopen |= IMAP_EXPUNGE_EXPECTED; if (imap_exec (idata, "EXPUNGE", 0) != 0) { - imap_error (_("imap_sync_mailbox: EXPUNGE failed"), idata->cmd.buf); + imap_error (_("imap_sync_mailbox: EXPUNGE failed"), idata->cmd.buf.data); rc = imap_reconnect (ctx); goto out; } @@ -1122,7 +1101,7 @@ int imap_mailbox_check (char *path, int new) if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) break; - s = imap_next_word (idata->cmd.buf); + s = imap_next_word (idata->cmd.buf.data); if (ascii_strncasecmp ("STATUS", s, 6) == 0) { s = imap_next_word (s); /* The mailbox name may or may not be quoted here. We could try to @@ -1296,7 +1275,7 @@ int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect, if (rc != IMAP_CMD_CONTINUE) return -1; - s = imap_next_word (idata->cmd.buf); + s = imap_next_word (idata->cmd.buf.data); if ((ascii_strncasecmp ("LIST", s, 4) == 0) || (ascii_strncasecmp ("LSUB", s, 4) == 0)) { *noselect = 0; @@ -1337,11 +1316,11 @@ int imap_parse_list_response (IMAP_DATA * idata, char **name, int *noselect, } s = imap_next_word (s); /* name */ if (s && *s == '{') { /* Literal */ - if (imap_get_literal_count (idata->cmd.buf, &bytes) < 0) + if (imap_get_literal_count (idata->cmd.buf.data, &bytes) < 0) return -1; if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE) return -1; - *name = idata->cmd.buf; + *name = idata->cmd.buf.data; } else *name = s; @@ -1533,7 +1512,7 @@ int imap_complete (char *dest, size_t dlen, char *path) { completions++; } } - while (m_strncmp(idata->cmd.seq, idata->cmd.buf, SEQLEN)); + while (m_strncmp(idata->cmd.seq, idata->cmd.buf.data, SEQLEN)); if (completions) { /* reformat output */ diff --git a/imap/imap_private.h b/imap/imap_private.h index be4ba69..cfa9b4d 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -114,10 +114,9 @@ typedef struct { /* IMAP command structure */ typedef struct { - char seq[SEQLEN + 1]; - char *buf; - unsigned int blen; int state; + char seq[SEQLEN + 1]; + buffer_t buf; } IMAP_COMMAND; typedef struct { @@ -138,6 +137,7 @@ typedef struct { unsigned int seqno; time_t lastread; /* last time we read a command for the server */ /* who knows, one day we may run multiple commands in parallel */ + IMAP_COMMAND cmd; /* The following data is all specific to the currently SELECTED mbox */ diff --git a/imap/message.c b/imap/message.c index cc339b7..df604d7 100644 --- a/imap/message.c +++ b/imap/message.c @@ -119,7 +119,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) break; if ((mfhrc = - msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1) + msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf.data, fp)) == -1) continue; else if (mfhrc < 0) break; @@ -212,7 +212,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) break; if ((mfhrc = - msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1) + msg_fetch_header (idata->ctx, &h, idata->cmd.buf.data, fp)) == -1) continue; else if (mfhrc < 0) break; @@ -391,7 +391,7 @@ int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) break; - pc = idata->cmd.buf; + pc = idata->cmd.buf.data; pc = imap_next_word (pc); pc = imap_next_word (pc); @@ -422,7 +422,7 @@ int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) /* pick up trailing line */ if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE) goto bail; - pc = idata->cmd.buf; + pc = idata->cmd.buf.data; fetched = 1; } @@ -451,7 +451,7 @@ int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) if (rc != IMAP_CMD_OK) goto bail; - if (!fetched || !imap_code (idata->cmd.buf)) + if (!fetched || !imap_code (idata->cmd.buf.data)) goto bail; /* Update the header information. Previously, we only downloaded a @@ -565,7 +565,7 @@ int imap_append_message (CONTEXT * ctx, MESSAGE * msg) if (rc != IMAP_CMD_RESPOND) { char *pc; - pc = vskipspaces(idata->cmd.buf + SEQLEN); + pc = vskipspaces(idata->cmd.buf.data + SEQLEN); pc = imap_next_word (pc); mutt_error ("%s", pc); mutt_sleep (1); @@ -596,10 +596,10 @@ int imap_append_message (CONTEXT * ctx, MESSAGE * msg) rc = imap_cmd_step (idata); while (rc == IMAP_CMD_CONTINUE); - if (!imap_code (idata->cmd.buf)) { + if (!imap_code (idata->cmd.buf.data)) { char *pc; - pc = vskipspaces(idata->cmd.buf + SEQLEN); + pc = vskipspaces(idata->cmd.buf.data + SEQLEN); pc = imap_next_word (pc); mutt_error ("%s", pc); mutt_sleep (1); @@ -704,8 +704,8 @@ int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete) if (rc == -2) { /* bail out if command failed for reasons other than nonexistent target */ if (ascii_strncasecmp - (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) { - imap_error ("imap_copy_messages", idata->cmd.buf); + (imap_get_qualifier (idata->cmd.buf.data), "[TRYCREATE]", 11)) { + imap_error ("imap_copy_messages", idata->cmd.buf.data); goto fail; } snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox); @@ -720,7 +720,7 @@ int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete) rc = imap_exec (idata, cmd.data, 0); } if (rc != 0) { - imap_error ("imap_copy_messages", idata->cmd.buf); + imap_error ("imap_copy_messages", idata->cmd.buf.data); goto fail; } @@ -743,18 +743,14 @@ int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete) } } - if (cmd.data) - p_delete(&cmd.data); - if (sync_cmd.data) - p_delete(&sync_cmd.data); + p_delete(&cmd.data); + p_delete(&sync_cmd.data); p_delete(&mx.mbox); return 0; fail: - if (cmd.data) - p_delete(&cmd.data); - if (sync_cmd.data) - p_delete(&sync_cmd.data); + p_delete(&cmd.data); + p_delete(&sync_cmd.data); p_delete(&mx.mbox); return -1; } @@ -880,7 +876,7 @@ static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf, if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE) return rc; - if (msg_parse_fetch (h, idata->cmd.buf) == -1) + if (msg_parse_fetch (h, idata->cmd.buf.data) == -1) return rc; } diff --git a/imap/util.c b/imap/util.c index d48101b..74b35e9 100644 --- a/imap/util.c +++ b/imap/util.c @@ -146,19 +146,20 @@ void imap_error (const char *where, const char *msg) * Returns NULL on failure (no mem) */ IMAP_DATA *imap_new_idata (void) { - return p_new(IMAP_DATA, 1); + IMAP_DATA *res = p_new(IMAP_DATA, 1); + buffer_init(&res->cmd.buf); + return res; } /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */ void imap_free_idata (IMAP_DATA ** idata) { - if (!idata) - return; - - p_delete(&(*idata)->capstr); - string_list_wipe(&(*idata)->flags); - p_delete(&((*idata)->cmd.buf)); - p_delete(idata); + if (*idata) { + p_delete(&(*idata)->capstr); + string_list_wipe(&(*idata)->flags); + buffer_wipe(&((*idata)->cmd.buf)); + p_delete(idata); + } } /* -- 2.20.1