* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: token.c,v 1.22.8.3.2.2 2005/09/30 19:13:36 martinea 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);
- ap_vsnprintf(linebuf, sizeof(linebuf), format, argp);
+ g_vsnprintf(linebuf, SIZEOF(linebuf), format, argp);
arglist_end(argp);
return quote(" ", linebuf);
/* Format the token */
arglist_start(argp, format);
- ap_vsnprintf(linebuf, sizeof(linebuf), format, argp);
+ g_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;
}
-#endif /* HAVE_SHQUOTE */
+#endif
/* 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];
char *sr;
int i;
+ /*
+ * Configure program for internationalization:
+ * 1) Only set the message locale for now.
+ * 2) Set textdomain for all amanda related programs to "amanda"
+ * We don't want to be forced to support dozens of message catalogs
+ */
+ setlocale(LC_MESSAGES, "C");
+ textdomain("amanda");
+
safe_fd(-1, 0);
+ setlocale(LC_ALL, "C");
+
+ /* shut up compiler */
+ argc = argc;
+ argv = argv;
+
set_pname("token test");
+ dbopen(NULL);
+
+ /* Don't die when child closes pipe */
+ signal(SIGPIPE, SIG_IGN);
+
erroutput_type = ERR_INTERACTIVE;
- printf("Testing split() with \" \" token separator\n");
+ g_printf(_("Testing split() with \" \" token separator\n"));
while(1) {
- printf("Input string: ");
+ g_printf(_("Input string: "));
amfree(str);
if ((str = agets(stdin)) == NULL) {
- printf("\n");
+ g_printf("\n");
break;
}
r = split(str, t, 20, " ");
- printf("%d token%s:\n", r, (r == 1) ? "" : "s");
- for (i=0; i <= r; i++) printf("tok[%d] = \"%s\"\n", i, t[i]);
+ g_printf(plural(_("%d token:\n"), _("%d token:\n"), r), r);
+ for (i=0; i <= r; i++)
+ g_printf("tok[%d] = \"%s\"\n", i, t[i]);
}
amfree(str);
- printf("\n");
+ g_printf("\n");
- printf("Testing quote()\n");
+ g_printf(_("Testing quote()\n"));
while(1) {
- printf("Input string: ");
+ g_printf(_("Input string: "));
amfree(str);
if ((str = agets(stdin)) == NULL) {
- printf("\n");
+ g_printf("\n");
break;
}
sr = squote(str);
- printf("Quoted = \"%s\"\n", sr);
- strncpy(str,sr,sizeof(str)-1);
- str[sizeof(str)-1] = '\0';
+ g_printf(_("Quoted = \"%s\"\n"), sr);
+ 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]);
+ if (r != 1)
+ g_printf("split()=%d!\n", r);
+ g_printf(_("Unquoted = \"%s\"\n"), t[1]);
amfree(sr);
}
amfree(str);