From: Pierre Habouzit Date: Sat, 19 May 2007 22:11:01 +0000 (+0200) Subject: I'm fed up with stupid defines when C can come up with *way* betted: bit-fields. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=bbaf5ab9813d18f9912e00fab83195e368e3f0d7;p=apps%2Fmadmutt.git I'm fed up with stupid defines when C can come up with *way* betted: bit-fields. Signed-off-by: Pierre Habouzit --- diff --git a/account.c b/account.c index ea1806a..044aa6c 100644 --- a/account.c +++ b/account.c @@ -44,12 +44,12 @@ int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2) user = NntpUser; #endif - if (a1->flags & a2->flags & M_ACCT_USER) - return (!m_strcmp(a1->user, a2->user)); - if (a1->flags & M_ACCT_USER) - return (!m_strcmp(a1->user, user)); - if (a2->flags & M_ACCT_USER) - return (!m_strcmp(a2->user, user)); + if (a1->has_user && a2->has_user) + return !m_strcmp(a1->user, a2->user); + if (a1->has_user) + return !m_strcmp(a1->user, user); + if (a2->has_user) + return !m_strcmp(a2->user, user); return 1; } @@ -64,15 +64,15 @@ int mutt_account_fromurl(ACCOUNT *account, ciss_url_t *url) if (url->user) { m_strcpy(account->user, sizeof(account->user), url->user); - account->flags |= M_ACCT_USER; + account->has_user = 1; } if (url->pass) { m_strcpy(account->pass, sizeof(account->pass), url->pass); - account->flags |= M_ACCT_PASS; + account->has_pass = 1; } if (url->port) { account->port = url->port; - account->flags |= M_ACCT_PORT; + account->has_port = 1; } return 0; @@ -90,34 +90,25 @@ void mutt_account_tourl (ACCOUNT * account, ciss_url_t * url) url->port = 0; if (account->type == M_ACCT_TYPE_IMAP) { - if (account->flags & M_ACCT_SSL) - url->scheme = U_IMAPS; - else - url->scheme = U_IMAP; + url->scheme = account->has_ssl ? U_IMAPS : U_IMAP; } if (account->type == M_ACCT_TYPE_POP) { - if (account->flags & M_ACCT_SSL) - url->scheme = U_POPS; - else - url->scheme = U_POP; + url->scheme = account->has_ssl ? U_POPS : U_POP; } #ifdef USE_NNTP if (account->type == M_ACCT_TYPE_NNTP) { - if (account->flags & M_ACCT_SSL) - url->scheme = U_NNTPS; - else - url->scheme = U_NNTP; + url->scheme = account->has_ssl ? U_NNTPS : U_NNTP; } #endif url->host = account->host; - if (account->flags & M_ACCT_PORT) + if (account->has_port) url->port = account->port; - if (account->flags & M_ACCT_USER) + if (account->has_user) url->user = account->user; - if (account->flags & M_ACCT_PASS) + if (account->has_pass) url->pass = account->pass; } @@ -127,7 +118,7 @@ int mutt_account_getuser (ACCOUNT * account) char prompt[STRING]; /* already set */ - if (account->flags & M_ACCT_USER) + if (account->has_user) return 0; else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser)) m_strcpy(account->user, sizeof(account->user), ImapUser); @@ -146,7 +137,7 @@ int mutt_account_getuser (ACCOUNT * account) return -1; } - account->flags |= M_ACCT_USER; + account->has_user = 1; return 0; } @@ -154,22 +145,22 @@ int mutt_account_getuser (ACCOUNT * account) int mutt_account_getlogin (ACCOUNT* account) { /* already set */ - if (account->flags & M_ACCT_LOGIN) + if (account->has_login) return 0; else if (account->type == M_ACCT_TYPE_IMAP) { if (!m_strisempty(ImapLogin)) { m_strcpy(account->login, sizeof(account->login), ImapLogin); - account->flags |= M_ACCT_LOGIN; + account->has_login = 1; } } - if (!(account->flags & M_ACCT_LOGIN)) { + if (!account->has_login) { mutt_account_getuser (account); m_strcpy(account->login, sizeof(account->login), account->user); } - account->flags |= M_ACCT_LOGIN; + account->has_login = 1; return 0; } @@ -179,7 +170,7 @@ int mutt_account_getpass (ACCOUNT * account) { char prompt[STRING]; - if (account->flags & M_ACCT_PASS) + if (account->has_pass) return 0; else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass)) m_strcpy(account->pass, sizeof(account->pass), ImapPass); @@ -191,7 +182,7 @@ int mutt_account_getpass (ACCOUNT * account) #endif else { snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "), - account->flags & M_ACCT_LOGIN ? account->login : account->user, + account->has_login ? account->login : account->user, account->host); account->pass[0] = '\0'; if (mutt_get_field_unbuffered(prompt, account->pass, @@ -199,12 +190,7 @@ int mutt_account_getpass (ACCOUNT * account) return -1; } - account->flags |= M_ACCT_PASS; + account->has_pass = 1; return 0; } - -void mutt_account_unsetpass (ACCOUNT * account) -{ - account->flags &= ~M_ACCT_PASS; -} diff --git a/account.h b/account.h index d9b5a55..0cb7122 100644 --- a/account.h +++ b/account.h @@ -14,29 +14,32 @@ #include -/* account types */ enum { - M_ACCT_TYPE_NONE = 0, - M_ACCT_TYPE_IMAP, - M_ACCT_TYPE_NNTP, - M_ACCT_TYPE_POP + M_ACCT_TYPE_NONE = 0, + M_ACCT_TYPE_IMAP, + M_ACCT_TYPE_NNTP, + M_ACCT_TYPE_POP }; -/* account flags */ -#define M_ACCT_PORT (1<<0) -#define M_ACCT_USER (1<<1) -#define M_ACCT_LOGIN (1<<2) -#define M_ACCT_PASS (1<<3) -#define M_ACCT_SSL (1<<4) - typedef struct { - char user[64]; - char login[64]; - char pass[64]; - char host[128]; - unsigned short port; - unsigned char type; - unsigned char flags; + union { + struct { + unsigned has_port : 1; + unsigned has_user : 1; + unsigned has_login : 1; + unsigned has_pass : 1; + unsigned has_ssl : 1; + }; + char flags; + }; + + char type; + int port; + + char user[64]; + char login[64]; + char pass[64]; + char host[128]; } ACCOUNT; int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * m2); @@ -45,6 +48,5 @@ void mutt_account_tourl (ACCOUNT * account, ciss_url_t * url); int mutt_account_getuser (ACCOUNT * account); int mutt_account_getlogin (ACCOUNT * account); int mutt_account_getpass (ACCOUNT * account); -void mutt_account_unsetpass (ACCOUNT * account); #endif /* _MUTT_ACCOUNT_H_ */ diff --git a/imap/imap.c b/imap/imap.c index c2cde32..a9f5788 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -341,7 +341,7 @@ IMAP_DATA *imap_conn_find (const ACCOUNT * account, int flags) if (!imap_authenticate (idata)) { idata->state = IMAP_AUTHENTICATED; } else { - mutt_account_unsetpass (&idata->conn->account); + idata->conn->account.has_pass = 0; } p_delete(&idata->capstr); diff --git a/imap/util.c b/imap/util.c index 4fe8ee8..db7e3af 100644 --- a/imap/util.c +++ b/imap/util.c @@ -64,11 +64,11 @@ int imap_parse_path (const char *path, IMAP_MBOX * mx) mx->mbox = m_strdup(url.path); if (url.scheme == U_IMAPS) - mx->account.flags |= M_ACCT_SSL; + mx->account.has_ssl = 1; p_delete(&c); - if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT)) + if (mx->account.has_ssl && !mx->account.has_port) mx->account.port = ImapsPort; return 0; diff --git a/lib-sys/mutt_socket.c b/lib-sys/mutt_socket.c index af88be0..0acee2a 100644 --- a/lib-sys/mutt_socket.c +++ b/lib-sys/mutt_socket.c @@ -230,7 +230,7 @@ CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account) if (Tunnel && *Tunnel) mutt_tunnel_socket_setup (conn); - else if (account->flags & M_ACCT_SSL) { + else if (account->has_ssl) { if (mutt_ssl_socket_setup (conn) < 0) { mutt_socket_free (conn); return NULL; diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 1a50b0d..4f2f249 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -274,7 +274,7 @@ int nntp_parse_url (const char *server, ACCOUNT * act, if (url.scheme == U_NNTP || url.scheme == U_NNTPS) { if (url.scheme == U_NNTPS) { - act->flags |= M_ACCT_SSL; + act->has_ssl = 1; act->port = NNTP_SSL_PORT; } diff --git a/nntp/nntp.c b/nntp/nntp.c index f5e7132..1f4462a 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -40,13 +40,13 @@ void nntp_sync_sidebar (NNTP_DATA* data) { * mailboxes command ;-((( FIXME */ buf[0] = '\0'; - snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s", - (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "", - NONULL (data->nserv->conn->account.user), - *data->nserv->conn->account.pass ? ":" : "", - *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "", - data->nserv->conn->account.host, - data->group); + snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s", + data->nserv->conn->account.has_ssl ? "s" : "", + NONULL(data->nserv->conn->account.user), + *data->nserv->conn->account.pass ? ":" : "", + *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "", + data->nserv->conn->account.host, + data->group); /* bail out if group not found via mailboxes */ if ((i = buffy_lookup (buf)) < 0) @@ -71,8 +71,8 @@ static int nntp_auth (NNTP_SERVER * serv) char buf[STRING]; unsigned char flags = conn->account.flags; - if (mutt_account_getuser (&conn->account) || !conn->account.user[0] || - mutt_account_getpass (&conn->account) || !conn->account.pass[0]) { + if (mutt_account_getuser(&conn->account) || !conn->account.user[0] || + mutt_account_getpass(&conn->account) || !conn->account.pass[0]) { conn->account.flags = flags; return -2; } @@ -152,7 +152,7 @@ static int nntp_connect_and_auth (NNTP_SERVER * serv) if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) return nntp_connect_error (serv); - if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) { + if (!conn->account.has_user && m_strncmp("480", buf, 3)) { serv->status = NNTP_OK; return 0; } diff --git a/pop.c b/pop.c index b0415aa..f173d6e 100644 --- a/pop.c +++ b/pop.c @@ -625,7 +625,7 @@ static int pop_parse_path (const char *path, ACCOUNT * act) if (url.scheme == U_POP || url.scheme == U_POPS) { if (url.scheme == U_POPS) { - act->flags |= M_ACCT_SSL; + act->has_ssl = 1; act->port = POP_SSL_PORT; }