From e07c290fb373d6c30c7640cdafc775ff85a864e2 Mon Sep 17 00:00:00 2001 From: Ville Voipio Date: Wed, 7 May 2008 09:19:36 +0000 Subject: [PATCH] Rewritten parse.c (removed strtok() for being evil and gettext() for being non-POSIX) git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1207 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- jtag/src/cmd/parse.c | 131 ++++++++++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 44 deletions(-) diff --git a/jtag/src/cmd/parse.c b/jtag/src/cmd/parse.c index 951ac7d8..08e43c60 100644 --- a/jtag/src/cmd/parse.c +++ b/jtag/src/cmd/parse.c @@ -20,6 +20,7 @@ * * Written by Marcel Telka , 2002, 2003. * Modified by Ajith Kumar P.C , 20/09/2006. + * Modified by Ville Voipio , 7-May-2008 * */ @@ -28,77 +29,119 @@ #include #include #include +#include #include "chain.h" #include "cmd.h" #include "jtag.h" +#define MAXINPUTLINE 100 /* Maximum input line length */ + -static char * -get_token( char *buf ) -{ - char *t = strtok( buf, " \f\n\r\t\v" ); - if (t && (*t == '#')) - return NULL; - return t; -} int jtag_parse_line( chain_t *chain, char *line ) { - char *t; - int l; - int n; + int l, i, r, tcnt; char **a; - int r; - - if (!line || !(strlen( line ) > 0)) + char *c, *d; + char *sline; + + if (line == NULL) + return 1; + l = strlen(line); + if (l == 0) return 1; - t = get_token( line ); - if (!t) + /* allocate as many chars as in the input line; this will be enough in all cases */ + sline = malloc((l+1) * sizeof(char)); + if (sline == NULL) { + printf( _("Out of memory\n") ); return 1; + } - n = 0; - l = 0; - a = NULL; - while (t) { - if (n + 2 > l) { - char **newa; - l = (l < 16) ? 16 : (l * 2); - newa = realloc( a, l * sizeof (char *) ); - if (!newa) { - free( a ); - printf( _("Out of memory\n") ); - return 1; - } - a = newa; + /* count and copy the tokens */ + c = line; + d = sline; + tcnt = 0; + while (1) { + /* eat up leading spaces */ + while (isspace(*c)) + c++; + + /* if the command ends here (either by NUL or comment) */ + if (*c == '\0' || *c == '#') + break; + + /* copy the meat (non-space, non-NUL) */ + while (!isspace(*c) && *c != '\0') { + *d++ = *c++; } - a[n++] = t; - a[n] = NULL; - - t = get_token( NULL ); + /* mark the end to the destination string */ + *d++ = '\0'; + tcnt++; + } + + if (tcnt == 0) { + free(sline); + return 1; + } + + /* allocate the token pointer table */ + a = malloc((tcnt + 1) * sizeof(char *)); + if (a == NULL) { + fprintf(stderr, _("Out of memory\n")); + return 1; } + /* find the starting points for the tokens */ + d = sline; + for (i = 0; i < tcnt; i++) + { + a[i] = d; + while (*d++ != '\0') + ; + } + a[tcnt] = NULL; + r = cmd_run( chain, a ); if(debug_mode & 1)printf("Return in jtag_parse_line r=%d\n",r); free( a ); + free(sline); return r; } + int jtag_parse_stream( chain_t *chain, FILE *f ) { - int go = 1; - char *line = NULL; - size_t n = 0; - - while (go && (getline( &line, &n, f ) != -1)) - if ((strlen(line) > 0) && (line[0] != '#')) - go = jtag_parse_line(chain, line); - - free(line); - + char inputline[MAXINPUTLINE + 1]; + int go = 1, i, c, lnr, clip; + + /* read the stream line-by-line until EOF or "quit" */ + lnr = 0; + do { + i = 0; + clip = 0; + + /* read stream until '\n' or EOF, copy at most MAXINPUTLINE-1 chars */ + while (1) { + c = fgetc(f); + if (c == EOF || c == '\n') + break; + if (i < sizeof(inputline) - 1) + inputline[i++] = c; + else + clip = 1; + } + inputline[i] = '\0'; + lnr++; + if (clip) + fprintf(stdout, "Warning: line %d exceeds %d characters, clipped\n", lnr, (int)sizeof(inputline) - 1); + go = jtag_parse_line(chain, inputline); + } + while (go && c != EOF); + return go; }