Commit f765125d f765125dd064487abedac21273e872f65bebac7b by Sergey Poznyakoff

Use new string trimming functions in parsers.

* include/mailutils/cstr.h (mu_str_stripws): New function.
* mailbox/stripws.c: New file.
* mailbox/Makefile.am (libmailutils_la_SOURCES): Add stripws.c

* examples/nntpclient.c (stripwhite): Remove. Use mu_str_stripws instead.
(execute_line): Rewrite using new string functions.
* examples/pop3client.c: Likewise.
* mailbox/mailcap.c (stripwhite): Remove. Use mu_str_stripws instead.
* mailbox/mime.c (_strltrim, _strttrim, _strtrim): Remove. Use
mu_str_stripws instead.

* mail/mail.c: Use mu_str_stripws.
* mail/mail.h (util_stripwhite): Remove prototype.
* mail/util.c (util_stripwhite): Remove
* examples/pop3client.c: Likewise.
* imap4d/util.c: Use new string functions.
* maidag/forward.c: Likewise.
* maidag/lmtp.c: Likewise.
* mh/mhn.c: Likewise.

* libproto/imap/folder.c: Remove unused local.
* libproto/mailer/smtp.c (smtp_writeline): Minor optimization.
1 parent 2e70a57e
......@@ -44,6 +44,7 @@
#include <mailutils/errno.h>
#include <mailutils/mutil.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
/* A structure which contains information on the commands this program
can understand. */
......@@ -87,7 +88,6 @@ int com_stat (char *);
int com_verbose (char *);
void initialize_readline (void);
char *stripwhite (char *);
COMMAND *find_command (char *);
char *dupstr (const char *);
int execute_line (char *);
......@@ -274,7 +274,7 @@ main (int argc MU_ARG_UNUSED, char **argv)
/* Remove leading and trailing whitespace from the line.
Then, if there is anything left, add it to the history list
and execute it. */
s = stripwhite (line);
s = mu_str_stripws (line);
if (*s)
{
......@@ -294,45 +294,34 @@ main (int argc MU_ARG_UNUSED, char **argv)
int
execute_line (char *line)
{
register int i;
COMMAND *command;
char *word;
char *word, *arg;
/* Isolate the command word. */
i = 0;
while (line[i] && mu_isblank (line[i]))
i++;
word = line + i;
while (line[i] && !mu_isblank (line[i]))
i++;
if (line[i])
line[i++] = '\0';
word = mu_str_skip_class (line, MU_CTYPE_SPACE);
arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
if (*arg)
{
*arg++ = 0;
arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
}
command = find_command (word);
if (!command)
{
fprintf (stderr, "%s: No such command for %s.\n", word, progname);
return (-1);
mu_error ("%s: No such command.", word);
return 0;
}
/* Get argument to command, if any. */
while (mu_isblank (line[i]))
i++;
word = line + i;
/* Call the function. */
return ((*(command->func)) (word));
return ((*(command->func)) (arg));
}
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
COMMAND *
find_command (name)
char *name;
find_command (char *name)
{
register int i;
......@@ -343,27 +332,6 @@ find_command (name)
return ((COMMAND *) NULL);
}
/* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */
char *
stripwhite (char *string)
{
register char *s, *t;
for (s = string; mu_isblank (*s); s++)
;
if (*s == 0)
return (s);
t = s + strlen (s) - 1;
while (t > s && mu_isblank (*t))
t--;
*++t = '\0';
return s;
}
int
com_verbose (char *arg)
{
......
......@@ -46,6 +46,7 @@
#include <mailutils/vartab.h>
#include <mailutils/argcv.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
/* A structure which contains information on the commands this program
can understand. */
......@@ -80,7 +81,6 @@ int com_verbose (char *);
int com_prompt (char *);
void initialize_readline (void);
char *stripwhite (char *);
COMMAND *find_command (char *);
char *dupstr (const char *);
int execute_line (char *);
......@@ -322,7 +322,7 @@ main (int argc MU_ARG_UNUSED, char **argv)
/* Remove leading and trailing whitespace from the line.
Then, if there is anything left, add it to the history list
and execute it. */
s = stripwhite (line);
s = mu_str_stripws (line);
if (*s)
{
......@@ -342,22 +342,18 @@ main (int argc MU_ARG_UNUSED, char **argv)
int
execute_line (char *line)
{
register int i;
COMMAND *command;
char *word;
char *word, *arg;
/* Isolate the command word. */
i = 0;
while (line[i] && mu_isblank (line[i]))
i++;
word = line + i;
while (line[i] && !mu_isblank (line[i]))
i++;
if (line[i])
line[i++] = '\0';
word = mu_str_skip_class (line, MU_CTYPE_SPACE);
arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
if (*arg)
{
*arg++ = 0;
arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
}
command = find_command (word);
if (!command)
......@@ -366,21 +362,14 @@ execute_line (char *line)
return 0;
}
/* Get argument to command, if any. */
while (mu_isblank (line[i]))
i++;
word = line + i;
/* Call the function. */
return ((*(command->func)) (word));
return ((*(command->func)) (arg));
}
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
COMMAND *
find_command (name)
char *name;
find_command (char *name)
{
register int i;
......@@ -391,27 +380,6 @@ find_command (name)
return ((COMMAND *) NULL);
}
/* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */
char *
stripwhite (char *string)
{
register char *s, *t;
for (s = string; mu_isblank (*s); s++)
;
if (*s == 0)
return (s);
t = s + strlen (s) - 1;
while (t > s && mu_isblank (*t))
t--;
*++t = '\0';
return s;
}
int
com_verbose (char *arg)
{
......
......@@ -1363,13 +1363,11 @@ imap4d_readline (struct imap4d_tokbuf *tok)
char *p = mu_strcasestr (tok->buffer, "LOGIN");
if (p && p > tok->buffer && mu_isblank (p[-1]))
{
char *q = p + 5;
while (*q && mu_isblank (*q))
q++;
while (*q && !mu_isblank (*q))
q++;
char *q = mu_str_skip_class (p + 5, MU_CTYPE_SPACE);
q = mu_str_skip_class_comp (q, MU_CTYPE_SPACE);
len = q - tok->buffer;
mu_diag_output (MU_DIAG_DEBUG, "recv: %*.*s {censored}", len, len,
mu_diag_output (MU_DIAG_DEBUG,
"recv: %*.*s {censored}", len, len,
tok->buffer);
}
else
......
......@@ -38,7 +38,8 @@ char *mu_str_skip_cset (const char *str, const char *cset);
char *mu_str_skip_class_comp (const char *str, int class);
char *mu_str_skip_cset_comp (const char *str, const char *cset);
char *mu_str_stripws (char *string);
#ifdef __cplusplus
}
......
......@@ -1817,7 +1817,6 @@ imap_body (f_imap_t f_imap, char **ptr)
{
size_t len = sep - *ptr;
char *section = malloc (len + 1);
char *p;
if (!section)
return ENOMEM;
......
......@@ -1067,14 +1067,15 @@ smtp_writeline (smtp_t smtp, const char *format, ...)
smtp->ptr = smtp->buffer + len;
while (len > 0 && mu_isblank (smtp->buffer[len - 1]))
len--;
if ((smtp->state != SMTP_SEND && smtp->state != SMTP_SEND_DOT)
|| smtp->mailer->flags & MAILER_FLAG_DEBUG_DATA)
MU_DEBUG2 (smtp->mailer->debug, MU_DEBUG_PROT, "> %.*s\n", len,
smtp->buffer);
{
while (len > 0 && mu_isblank (smtp->buffer[len - 1]))
len--;
MU_DEBUG2 (smtp->mailer->debug, MU_DEBUG_PROT, "> %.*s\n", len,
smtp->buffer);
}
return 0;
}
......
......@@ -220,19 +220,11 @@ process_forward (mu_message_t msg, char *filename, const char *myname)
while (getline (&buf, &size, fp) > 0)
{
char *p, *q;
for (p = buf; *p && mu_isblank (*p); p++)
;
q = p + strlen (p);
if (q > p)
{
if (*--q == '\n')
q--;
for (; q > p && mu_isblank (*q); q--)
;
q[1] = 0;
}
char *p;
mu_rtrim_class (buf, MU_CTYPE_SPACE);
p = mu_str_skip_class (buf, MU_CTYPE_SPACE);
if (*p && *p != '#')
{
if (strchr (p, '@'))
......
......@@ -77,16 +77,6 @@ lmtp_reply (FILE *fp, char *code, char *enh, char *fmt, ...)
}
void
trimnl (char *arg)
{
size_t len = strlen (arg);
if (len > 0 && arg[len-1] == '\n')
arg[--len] = 0;
if (len > 0 && arg[len-1] == '\r')
arg[--len] = 0;
}
void
xlatnl (char *arg)
{
size_t len = strlen (arg);
......@@ -543,7 +533,7 @@ lmtp_loop (FILE *in, FILE *out, unsigned int timeout)
enum lmtp_command cmd = cp->cmd_code;
enum lmtp_state next_state = transtab[cmd][state];
trimnl (buf);
mu_rtrim_cset (sp, "\r\n");
if (lmtp_transcript)
mu_diag_output (MU_DIAG_INFO, "LMTP recieve: %s", buf);
......@@ -552,8 +542,7 @@ lmtp_loop (FILE *in, FILE *out, unsigned int timeout)
{
if (cp->cmd_fun)
{
while (*sp && mu_isblank (*sp))
sp++;
sp = mu_str_skip_class (sp, MU_CTYPE_SPACE);
if (cp->cmd_fun (out, sp))
continue;
}
......
......@@ -529,7 +529,7 @@ mail_mainloop (char *(*input) (void *, int),
command = buf;
len = strlen (command);
}
cmd = util_stripwhite (command);
cmd = mu_str_stripws (command);
util_do_command ("%s", cmd);
#ifdef WITH_READLINE
if (do_history && !(mu_isspace(cmd[0]) || cmd[0] == '#'))
......
......@@ -313,7 +313,6 @@ extern size_t util_range_msg (size_t low, size_t high, int flags,
msg_handler_t func, void *data);
extern function_t* util_command_get (const char *cmd);
extern char *util_stripwhite (char *string);
extern void *util_find_entry (void *table, size_t nmemb, size_t size,
const char *cmd);
......
......@@ -336,24 +336,6 @@ util_command_list (void *table, size_t nmemb, size_t size)
}
/*
* removes whitespace from the beginning and end of a string
*/
char *
util_stripwhite (char *string)
{
register char *s, *t;
for (s = string; mu_isspace ((unsigned)*s); s++)
;
if (*s == 0)
return s;
t = s + strlen (s) - 1;
while (t > s && mu_isspace ((unsigned)*t))
t--;
*++t = '\0';
return s;
}
/*
* Get the number of columns on the screen
* First try an ioctl() call not all shells set the COLUMNS environ.
*/
......
......@@ -123,6 +123,7 @@ libmailutils_la_SOURCES = \
stream.c\
strltrim.c\
strskip.c\
stripws.c\
strrtrim.c\
syslog.c\
system.c\
......
......@@ -50,7 +50,6 @@ struct _mu_mailcap
static int mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream);
static int mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer);
static char * stripwhite (char *string);
static char * tokenize (char *s, char **save_ptr);
int
......@@ -371,8 +370,8 @@ mu_mailcap_entry_get_value (mu_mailcap_entry_t entry, const char *key,
if (value != NULL)
{
value++; /* Pass the equal. */
/* Remove prepend space. */
for (; mu_isspace ((unsigned char)*value); value++)
/* Remove leading space. */
for (; mu_isspace (*value); value++)
;
len = strlen (value);
/* Strip surrounding double quotes */
......@@ -400,27 +399,6 @@ mu_mailcap_entry_get_value (mu_mailcap_entry_t entry, const char *key,
return status;
}
/* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */
static char *
stripwhite (char *string)
{
register char *s, *t;
for (s = string; mu_isspace ((unsigned char)*s); s++)
;
if (*s == 0)
return (s);
t = s + strlen (s) - 1;
while (t > s && mu_isspace (*t))
t--;
*++t = '\0';
return s;
}
/*
* break the line on ';'. Same as strtok() but
* check for escaped "\;"
......@@ -479,12 +457,12 @@ mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer)
{
/* The first entry in a mailcap line is the typefield. */
case 0:
entry->typefield = strdup (stripwhite (token));
entry->typefield = strdup (mu_str_stripws (token));
break;
/* The second entry in a mailcap line is the view-command. */
case 1:
entry->viewcommand = strdup (stripwhite(token));
entry->viewcommand = strdup (mu_str_stripws (token));
break;
/* The rest are the optional fields. */
......@@ -496,7 +474,8 @@ mu_mailcap_parse_entry (mu_mailcap_entry_t entry, char *buffer)
if (fields != NULL)
{
entry->fields = fields;
entry->fields[entry->fields_count] = strdup (stripwhite (token));
entry->fields[entry->fields_count] =
strdup (mu_str_stripws (token));
entry->fields_count++;
}
}
......@@ -643,7 +622,7 @@ mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t stream)
/* Parse the well-form mailcap line entry. */
if (previous == NULL) {
/* Nuke the trailing/prepend spaces. */
char *line = stripwhite(buffer);
char *line = mu_str_stripws (buffer);
/* Ignore comments or empty lines */
if (*line != '#' && *line != '\0')
{
......
......@@ -133,31 +133,7 @@ _mime_append_part (mu_mime_t mime, mu_message_t msg, int offset, int len, int li
return 0;
}
static char *
_strltrim (char *str)
{
char *p;
for (p = str; mu_isspace (*p) && *p != '\0'; ++p);
return ((p != str) ? memmove (str, p, strlen (p) + 1) : str);
}
static char *
_strttrim (char *str)
{
char *p;
for (p = str + strlen (str) - 1;
mu_isspace (*p) && p >= str; --p);
*++p = '\0';
return (str);
}
char *_strtrim (char *str);
#define _strtrim(str) _strltrim(_strttrim(str))
#define _ISSPECIAL(c) ( \
#define _ISSPECIAL(c) ( \
((c) == '(') || ((c) == ')') || ((c) == '<') || ((c) == '>') \
|| ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \
|| ((c) == '\\') || ((c) == '.') || ((c) == '[') \
......@@ -169,7 +145,7 @@ _mime_munge_content_header (char *field_body)
char *p, *e, *str = field_body;
int quoted = 0;
_strtrim (field_body);
mu_str_stripws (field_body);
if ((e = strchr (str, ';')) == NULL)
return;
......
/* This file is part of GNU Mailutils
Copyright (C) 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
/*
* removes whitespace from the beginning and end of a string
*/
char *
mu_str_stripws (char *string)
{
mu_rtrim_class (string, MU_CTYPE_SPACE);
return mu_str_skip_class (string, MU_CTYPE_SPACE);
}
......@@ -480,7 +480,7 @@ yylex ()
{
curp -= rest;
yylval.builtin = bp;
while (*curp && mu_isspace(*curp))
while (*curp && mu_isspace (*curp))
curp++;
return FUNCTION;
}
......
......@@ -617,8 +617,7 @@ mhn_compose_command (char *typestr, int *flags, char *file)
%s subtype */
obstack_init (&stk);
for (p = str; *p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (str, MU_CTYPE_SPACE);
if (*p == '|')
p++;
......@@ -661,12 +660,11 @@ mhn_compose_command (char *typestr, int *flags, char *file)
free (subtype);
str = obstack_finish (&stk);
for (p = str; *p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (str, MU_CTYPE_SPACE);
if (!*p)
str = NULL;
else
str = strdup (str);
str = strdup (p);
obstack_free (&stk, NULL);
return (char*) str;
......@@ -700,8 +698,7 @@ mhn_show_command (mu_message_t msg, msg_part_t part, int *flags,
%d content description */
obstack_init (&stk);
for (p = str; *p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (str, MU_CTYPE_SPACE);
if (*p == '|')
p++;
......@@ -777,12 +774,11 @@ mhn_show_command (mu_message_t msg, msg_part_t part, int *flags,
free (subtype);
str = obstack_finish (&stk);
for (p = str; *p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (str, MU_CTYPE_SPACE);
if (!*p)
str = NULL;
else
str = strdup (str);
str = strdup (p);
obstack_free (&stk, NULL);
return (char*) str;
......@@ -869,12 +865,11 @@ mhn_store_command (mu_message_t msg, msg_part_t part, char *name)
free (subtype);
str = obstack_finish (&stk);
for (p = str; *p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (str, MU_CTYPE_SPACE);
if (!*p)
str = NULL;
else
str = strdup (str);
str = strdup (p);
obstack_free (&stk, NULL);
return (char*) str;
......@@ -985,9 +980,8 @@ get_extbody_params (mu_message_t msg, char **content, char **descr)
&& mu_c_strncasecmp (buf, MU_HEADER_CONTENT_DESCRIPTION ":",
sizeof (MU_HEADER_CONTENT_DESCRIPTION)) == 0)
{
for (p = buf + sizeof (MU_HEADER_CONTENT_DESCRIPTION);
*p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (buf + sizeof (MU_HEADER_CONTENT_DESCRIPTION),
MU_CTYPE_SPACE);
*descr = strdup (p);
}
else if (content
......@@ -995,9 +989,8 @@ get_extbody_params (mu_message_t msg, char **content, char **descr)
sizeof (MU_HEADER_CONTENT_TYPE)) == 0)
{
char *q;
for (p = buf + sizeof (MU_HEADER_CONTENT_TYPE);
*p && mu_isspace (*p); p++)
;
p = mu_str_skip_class (buf + sizeof (MU_HEADER_CONTENT_TYPE),
MU_CTYPE_SPACE);
q = strchr (p, ';');
if (q)
*q = 0;
......@@ -1830,7 +1823,7 @@ parse_brace (char **pval, char **cmd, int c, struct compose_env *env)
}
#define isdelim(c) (mu_isspace (c) || strchr (";<[(", c))
#define skipws(ptr) do { while (*ptr && mu_isspace (*ptr)) ptr++; } while (0)
#define skipws(ptr) (ptr) = mu_str_skip_class (ptr, MU_CTYPE_SPACE)
int
parse_content_type (struct compose_env *env,
......@@ -2300,10 +2293,8 @@ edit_mime (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
rc = parse_type_command (&cmd, env, hdr);
if (rc)
return 1;
for (p = cmd + strlen (cmd) - 1; p > cmd && mu_isspace (*p); p--)
;
p[1] = 0;
mu_rtrim_class (cmd, MU_CTYPE_SPACE);
_get_content_type (hdr, &typestr, NULL);
shell_cmd = mhn_compose_command (typestr, &flags, cmd);
......@@ -2463,9 +2454,8 @@ mhn_edit (struct compose_env *env, int level)
finish_text_msg (env, &msg, ascii_buf);
/* Execute the directive */
tok = sp = buf;
while (*sp && !mu_isspace (*sp))
sp++;
tok = buf;
sp = mu_str_skip_class_comp (buf, MU_CTYPE_SPACE);
c = *sp;
*sp = 0;
......