Timo Teräs wrote:
> Vladimir Dronnikov wrote:
>> line 190 of your diff:
>>
>> + i += strcspn(&line[i], delims);
>>
>> I predict there will be problems if I pass "\0 \t" as delims, which is a
>> perfectly valid case for not having comment chars at all.
>
> You're right.
> Fixed as: "i += strcspn(&line[i], delims[0] ? delims : delims + 1);"
>
> There was also some problems with comment in the greedy last token.
> I think it should be right now.
>
> Please consider this for applying.
Urgh. Except that I forgot some debug printfs. This one has them
removed.
- Timo
Index: libbb/get_line_from_file.c
===================================================================
--- libbb/get_line_from_file.c (revision 23064)
+++ libbb/get_line_from_file.c (working copy)
@@ -16,7 +16,7 @@
* must be free'ed by the caller. If end is NULL '\n' isn't considered
* end of line. If end isn't NULL, length of the chunk read is stored in it.
* Return NULL if EOF/error */
-char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
+char* FAST_FUNC bb_get_chunk_from_file_wrapped(FILE *file, int *end, int *lineno)
{
int ch;
int idx = 0;
@@ -26,12 +26,20 @@
while ((ch = getc(file)) != EOF) {
/* grow the line buffer as necessary */
if (idx >= linebufsz) {
- linebufsz += 80;
+ linebufsz += 256;
linebuf = xrealloc(linebuf, linebufsz);
}
linebuf[idx++] = (char) ch;
- if (!ch || (end && ch == '\n'))
+ if (!ch)
break;
+ if (end && ch == '\n') {
+ if (lineno == NULL)
+ break;
+ (*lineno)++;
+ if (idx < 2 || linebuf[idx-2] != '\\')
+ break;
+ idx -= 2;
+ }
}
if (end)
*end = idx;
@@ -48,6 +56,11 @@
return linebuf;
}
+char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
+{
+ return bb_get_chunk_from_file_wrapped(file, end, NULL);
+}
+
/* Get line, including trailing \n if any */
char* FAST_FUNC xmalloc_fgets(FILE *file)
{
Index: libbb/parse_config.c
===================================================================
--- libbb/parse_config.c (revision 23064)
+++ libbb/parse_config.c (working copy)
@@ -123,137 +123,97 @@
#undef config_read
int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
{
- char *line, *q;
- char comment;
- int ii;
- int ntokens;
- int mintokens;
+ char *line;
+ int ntokens, mintokens;
+ int end, i, t;
- comment = *delims++;
ntokens = flags & 0xFF;
mintokens = (flags & 0xFF00) >> 8;
- again:
- memset(tokens, 0, sizeof(tokens[0]) * ntokens);
- if (!parser)
+ if (parser == NULL)
return 0;
+
+again:
+ memset(tokens, 0, sizeof(tokens[0]) * ntokens);
config_free_data(parser);
- while (1) {
-//TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc
- line = xmalloc_fgetline(parser->fp);
- if (!line)
- return 0;
+ /* Read one line (handling continuations with backslash) */
+ line = bb_get_chunk_from_file_wrapped(parser->fp, &end, &parser->lineno);
+ if (line == NULL)
+ return 0;
+ parser->line = line;
- parser->lineno++;
- // handle continuations. Tito's code stolen :)
- while (1) {
- ii = strlen(line);
- if (!ii)
- goto next_line;
- if (line[ii - 1] != '\\')
- break;
- // multi-line object
- line[--ii] = '\0';
-//TODO: add xmalloc_fgetline-like iface but with appending to existing str
- q = xmalloc_fgetline(parser->fp);
- if (!q)
- break;
- parser->lineno++;
- line = xasprintf("%s%s", line, q);
- free(q);
- }
- // discard comments
- if (comment) {
- q = strchrnul(line, comment);
- *q = '\0';
- ii = q - line;
- }
- // skip leading and trailing delimiters
- if (flags & PARSE_TRIM) {
- // skip leading
- int n = strspn(line, delims);
- if (n) {
- ii -= n;
- overlapping_strcpy(line, line + n);
- }
- // cut trailing
- if (ii) {
- while (strchr(delims, line[--ii]))
- continue;
- line[++ii] = '\0';
- }
- }
- // if something still remains -> return it
- if (ii)
- break;
+ /* Strip trailing line-feed if any */
+ if (end && line[end-1] == '\n')
+ line[--end] = '\0';
- next_line:
- // skip empty line
- free(line);
- }
- // non-empty line found, parse and return the number of tokens
+ /* Skip token in the start of line? */
+ if (flags & PARSE_TRIM)
+ line += strspn(line, delims + 1);
- // store line
- parser->line = line = xrealloc(line, ii + 1);
- if (flags & PARSE_KEEP_COPY) {
+ /* Empty line? */
+ if (line[0] == '\0' || line[0] == delims[0])
+ goto again;
+
+ if (flags & PARSE_KEEP_COPY)
parser->data = xstrdup(line);
- }
- // split line to tokens
- ntokens--; // now it's max allowed token no
- // N.B. non-empty remainder is also a token,
- // so if ntokens <= 1, we just return the whole line
- // N.B. if PARSE_GREEDY is set the remainder of the line is stuck to the last token
- ii = 0;
- while (*line && ii <= ntokens) {
- //bb_info_msg("L[%s]", line);
- // get next token
- // at last token and need greedy token ->
- if ((flags & PARSE_GREEDY) && (ii == ntokens)) {
- // skip possible delimiters
- if (flags & PARSE_COLLAPSE)
- line += strspn(line, delims);
- // don't cut the line
- q = line + strlen(line);
+ /* Tokenize the line */
+ for (i = t = 0; i < end && t < ntokens; t++) {
+ /* Did we hit a comment char? */
+ if (line[i] == delims[0])
+ break;
+
+ /* Pin token */
+ tokens[t] = &line[i];
+
+ /* Combine remaining arguments? */
+ if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
+ /* Vanilla token, find next delimiter */
+ i += strcspn(&line[i], delims[0] ? delims : delims + 1);
} else {
- // vanilla token. cut the line at the first delim
- q = line + strcspn(line, delims);
- if (*q) // watch out: do not step past the line end!
- *q++ = '\0';
+ /* Combining, find comment char if any */
+ i = strchrnul(&line[i], delims[0]) - line;
}
- // pin token
- if (!(flags & (PARSE_COLLAPSE | PARSE_TRIM)) || *line) {
- //bb_info_msg("N[%d] T[%s]", ii, line);
- tokens[ii++] = line;
- // process escapes in token
-#if 0 // unused so far
- if (flags & PARSE_ESCAPE) {
- char *s = line;
- while (*s) {
- if (*s == '\\') {
- s++;
- *line++ = bb_process_escape_sequence((const char **)&s);
- } else {
- *line++ = *s++;
- }
+
+ /* Token terminated by a comment char? */
+ if (line[i] == delims[0])
+ end = i;
+
+ /* Terminate it */
+ if (line[i] != '\0')
+ line[i++] = '\0';
+
+#if 0 /* unused so far */
+ if (flags & PARSE_ESCAPE) {
+ const char *from;
+ char *to;
+
+ from = to = tokens[t];
+ while (*from) {
+ if (*from == '\\') {
+ from++;
+ *to++ = bb_process_escape_sequence(&from);
+ } else {
+ *to++ = *from++;
}
- *line = '\0';
}
-#endif
+ *to = '\0';
}
- line = q;
- //bb_info_msg("A[%s]", line);
+#endif
+
+ /* Skip possible delimiters */
+ if (flags & PARSE_COLLAPSE)
+ i += strspn(&line[i], delims + 1);
}
- if (ii < mintokens) {
+ if (t < mintokens) {
bb_error_msg("bad line %u: %d tokens found, %d needed",
- parser->lineno, ii, mintokens);
+ parser->lineno, t, mintokens);
if (flags & PARSE_MIN_DIE)
xfunc_die();
- ntokens++;
goto again;
}
- return ii;
+ return t;
}
Index: include/libbb.h
===================================================================
--- include/libbb.h (revision 23064)
+++ include/libbb.h (working copy)
@@ -613,6 +613,7 @@
extern void xprint_and_close_file(FILE *file) FAST_FUNC;
extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC;
+extern char *bb_get_chunk_from_file_wrapped(FILE *file, int *end, int *lineno) FAST_FUNC;
/* Reads up to (and including) TERMINATING_STRING: */
extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC;
/* Chops off TERMINATING_STRING from the end: */
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox