* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: token.c,v 1.29 2006/01/14 04:37:19 paddy_s Exp $
+ * $Id: token.c,v 1.32 2006/07/19 17:41:15 martinea Exp $
*
* token bashing routines
*/
** Inspired by awk and a routine called splitter() that I snarfed from
** the net ages ago (original author long forgotten).
*/
-int split(str, token, toklen, sep)
-char *str; /* String to split */
-char **token; /* Array of token pointers */
-int toklen; /* Size of token[] */
-char *sep; /* Token separators - usually " " */
+int
+split(
+ char * str, /* String to split */
+ char ** token, /* Array of token pointers */
+ int toklen, /* Size of token[] */
+ char * sep) /* Token separators - usually " " */
{
register char *pi, *po;
register int fld;
if (*sep == '\0' || *str == '\0' || toklen == 1) return fld;
/* Calculate the length of the unquoted string. */
-
- len = 0;
- for (pi = str; *pi && *pi != '\n'; pi++) {
- switch(*pi) {
- case '\\': /* had better not be trailing... */
- pi++;
- if (*pi >= '0' && *pi <= '3') pi = pi + 2;
- len++;
- break;
- case '"': /* just ignore "'s */
- break;
- default:
- len++;
- }
- }
+ len = strlen(str);;
/* Allocate some space */
in_quotes = 0;
po = buf;
token[++fld] = po;
- for (pi = str; *pi && *pi != '\n'; pi++) {
- if (*pi == '\\') { /* escape */
- pi++;
- if (*pi >= '0' && *pi <= '3') {
- *po = ((*pi++ - '0') << 6);
- *po = *po + ((*pi++ - '0') << 3);
- *po = *po + ((*pi - '0') );
- }
- else *po = *pi;
- po++;
- }
- else if (*pi == '"') { /* quotes */
- in_quotes = !in_quotes;
- }
- else if (!in_quotes && strchr(sep, *pi)) { /* separator */
+ for (pi = str; *pi && *pi != '\0'; pi++) {
+ if (*pi == '\n' && !in_quotes)
+ break;
+
+ if (!in_quotes && strchr(sep, *pi)) {
+ /*
+ * separator
+ * Advance to next field.
+ */
*po = '\0'; /* end of token */
if (fld+1 >= toklen) return fld; /* too many tokens */
token[++fld] = po + 1;
po++;
+ continue;
}
- else {
- *po++ = *pi; /* normal */
+
+ if (*pi == '"') {
+ /*
+ * Start or end of quote
+ * Emit quote in either case
+ */
+ in_quotes = !in_quotes;
+ } else if (in_quotes && *pi == '\\' && (*(pi + 1) == '"')) {
+ /*
+ * Quoted quote.
+ * emit '/' - default will pick up '"'
+ */
+ *po++ = *pi++;
}
+ *po++ = *pi; /* Emit character */
}
*po = '\0';
/* Format the token */
arglist_start(argp, format);
- vsnprintf(linebuf, sizeof(linebuf), format, argp);
+ vsnprintf(linebuf, SIZEOF(linebuf), format, argp);
arglist_end(argp);
return quote(" ", linebuf);
/* Format the token */
arglist_start(argp, format);
- vsnprintf(linebuf, sizeof(linebuf), format, argp);
+ vsnprintf(linebuf, SIZEOF(linebuf), format, argp);
arglist_end(argp);
return quote(sep, linebuf);
}
-char *squote(str)
-char *str; /* the string to quote */
+char *squote(
+ char * str) /* the string to quote */
{
return quote(" ", str);
}
-char *quote(sepchr, str)
-char *sepchr; /* separators that also need quoting */
-char *str; /* the string to quote */
+char *
+quote(
+ char * sepchr, /* separators that also need quoting */
+ char * str) /* the string to quote */
{
register char *pi, *po;
register size_t len;
/* Calculate the length of the quoted token. */
- len = sep = 0;
+ sep = 0;
+ len = 0;
for (pi = str; *pi; pi++) {
if (*pi < ' ' || *pi > '~')
len = len + 4;
for (pi = str; *pi; pi++) {
if (*pi < ' ' || *pi > '~') {
*po++ = '\\';
- *po++ = ((*pi >> 6) & 07) + '0';
- *po++ = ((*pi >> 3) & 07) + '0';
- *po++ = ((*pi ) & 07) + '0';
+ *po++ = (char)(((*pi >> 6) & 07) + '0');
+ *po++ = (char)(((*pi >> 3) & 07) + '0');
+ *po++ = (char)(((*pi ) & 07) + '0');
}
else if (*pi == '\\' || *pi == '"') {
*po++ = '\\';
*po = '\0';
- assert(po - buf == len); /* Just checking! */
+ assert(po == (buf + len)); /* Just checking! */
return buf;
}
/* Quote a string so that it can be used as a regular expression */
-char *rxquote(str)
-char *str; /* the string to quote */
+char *
+rxquote(
+ char * str) /* the string to quote */
{
char *pi, *po;
size_t len;
*po = '\0';
- assert(po - buf == len); /* Just checking! */
+ assert(po == (buf + len)); /* Just checking! */
return buf;
}
#ifndef HAVE_SHQUOTE
/* Quote a string so that it can be safely passed to a shell */
-char *shquote(str)
-char *str; /* the string to quote */
+char *
+shquote(
+ char * str) /* the string to quote */
{
char *pi, *po;
size_t len;
*po = '\0';
- assert(po - buf == len); /* Just checking! */
+ assert(po == (buf + len)); /* Just checking! */
return buf;
}
/* Table lookup.
*/
-int table_lookup(table, str)
-table_t *table;
-char *str;
+int
+table_lookup(
+ table_t * table,
+ char * str)
{
while(table->word != (char *)0) {
- if (*table->word == *str &&
- strcmp(table->word, str) == 0) return table->value;
+ if (*table->word == *str && strcmp(table->word, str) == 0) {
+ return table->value;
+ }
table++;
}
/* Reverse table lookup.
*/
-char *table_lookup_r(table, val)
-table_t *table;
-int val;
+char *
+table_lookup_r(
+ /*@keep@*/ table_t * table,
+ int val)
{
while(table->word != (char *)0) {
- if (table->value == val) return table->word;
+ if (table->value == val) {
+ return table->word;
+ }
table++;
}
#ifdef TEST
-int main()
+int
+main(
+ int argc,
+ char ** argv)
{
char *str = NULL;
char *t[20];
safe_fd(-1, 0);
+ /* shut up compiler */
+ argc = argc;
+ argv = argv;
+
set_pname("token test");
+ dbopen(NULL);
+
/* Don't die when child closes pipe */
signal(SIGPIPE, SIG_IGN);
}
sr = squote(str);
printf("Quoted = \"%s\"\n", sr);
- strncpy(str,sr,sizeof(str)-1);
- str[sizeof(str)-1] = '\0';
+ strncpy(str,sr,SIZEOF(str)-1);
+ str[SIZEOF(str)-1] = '\0';
r = split(str, t, 20, " ");
if (r != 1) printf("split()=%d!\n", r);
printf("Unquoted = \"%s\"\n", t[1]);