[Toxine-cvs] CVS: toxine/src common.h,1.29,1.30 script.c,1.4,1.5
Brought to you by:
f1rmb
From: Daniel Caujolle-B. <f1...@us...> - 2004-05-19 22:46:36
|
Update of /cvsroot/toxine/toxine/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21497 Modified Files: common.h script.c Log Message: rewrite script reading. Support multilines (using backslash) in script file). Index: common.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/common.h,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- common.h 17 May 2004 21:23:35 -0000 1.29 +++ common.h 19 May 2004 22:46:21 -0000 1.30 @@ -266,8 +266,9 @@ struct { int in_use; char *filename; - FILE *fd; - char buf[256]; + char **lines; + int current; + int num_lines; char *ln; } script; Index: script.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/script.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- script.c 17 May 2004 20:31:43 -0000 1.4 +++ script.c 19 May 2004 22:46:21 -0000 1.5 @@ -21,86 +21,133 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include "common.h" #include "commands.h" #include "utils.h" -/* - * Cleanup the EOL ('\n','\r',' ') - */ -static void toxine_script_clean_eol(toxine_t *tox) { - char *p; +static void toxine_script_free(toxine_t *tox) { + + if(tox->script.num_lines) { + while(tox->script.num_lines) { + toxine_free(tox->script.lines[(tox->script.num_lines - 1)]); + tox->script.num_lines--; + } + toxine_free(tox->script.lines); + } +} + +static int toxine_script_split_lines(toxine_t *tox, char *_buf) { + + if(_buf) { + char *buf, *p, *pp, *obuf; + + buf = strdup(_buf); + obuf = buf; + pp = buf; + + tox->script.num_lines = 0; + + while((p = strchr(pp, '\n'))) { + if(*(p - 1) == '\\') { + *(p - 1) = ' '; + *p = ' '; + } + else + pp++; + } + + while((p = xine_strsep(&buf, "\n"))) { - p = tox->script.ln; + if(p && (p <= buf) && (strlen(p))) { + + tox->script.lines = (char **) realloc(tox->script.lines, sizeof(char *) * (tox->script.num_lines + 1)); + + while((*(p + strlen(p) - 1) == '\n') || (*(p + strlen(p) - 1) == '\r')) + *(p + strlen(p) - 1) = '\0'; - if(p) { - while(*p != '\0') { - if(*p == '\n' || *p == '\r') { - *p = '\0'; - break; + tox->script.lines[tox->script.num_lines++] = strdup(p); } - - p++; } - while(p > tox->script.ln) { - --p; - - if(*p == ' ') - *p = '\0'; - else - break; - } + toxine_free(buf); } + + return (tox->script.num_lines > 0); } -/* - * Get next line from opened file. - */ static int toxine_script_get_next_line(toxine_t *tox) { - __get_next_line: - tox->script.ln = fgets(tox->script.buf, 255, tox->script.fd); - - while(tox->script.ln && (*tox->script.ln == ' ' || *tox->script.ln == '\t')) ++tox->script.ln; - - if(tox->script.ln) { - if((strncmp(tox->script.ln, "//", 2) == 0) || - (strncmp(tox->script.ln, "#", 1) == 0)) { - goto __get_next_line; - } + __again: + if(tox->script.current < (tox->script.num_lines - 1)) { + tox->script.current++; + tox->script.ln = tox->script.lines[tox->script.current]; + if(tox->script.ln && ((*(tox->script.ln) == '#') || + ((strlen(tox->script.ln) >= 2) && (*(tox->script.ln) == '/') && (*(tox->script.ln + 1) == '/')))) + goto __again; + return 1; } - - toxine_script_clean_eol(tox); - - if(tox->script.ln && !strlen(tox->script.ln)) - goto __get_next_line; - - return((tox->script.ln != NULL)); + return 0; } /* * Main loop in script mode. */ void toxine_handle_script(toxine_t *tox) { + struct stat st; + int fd; + char *buf; + + if(stat(tox->script.filename, &st) == 0) { + + if(st.st_size > 0) { + + if((fd = open(tox->script.filename, O_RDONLY)) != -1) { + off_t br; + + buf = (char *) xine_xmalloc(st.st_size + 1); + + if((br = read(fd, buf, st.st_size)) != st.st_size) { + close(fd); + toxine_free(buf); + fprintf(stderr, "read() returned wrong size (expected %lld, readed %lld)\n", st.st_size, br); + return; + } + + close(fd); + + buf[br] = '\0'; + tox->interactive = 0; + + if(toxine_script_split_lines(tox, buf)) { + + while(toxine_script_get_next_line(tox) && tox->running) { + + if(tox->script.ln) { + toxine_set_command_line(tox, tox->script.ln); + toxine_handle_command(tox, NULL); + } + + memset(&tox->command.command, 0, sizeof(tox->command.command)); + } + + toxine_free(buf); + toxine_script_free(tox); + } - if((tox->script.fd = fopen(tox->script.filename, "r")) != NULL) { - - tox->interactive = 0; - - while(toxine_script_get_next_line(tox) && tox->running) { - - if(tox->script.ln) { - toxine_set_command_line(tox, tox->script.ln); - toxine_handle_command(tox, NULL); } - - memset(&tox->command.command, 0, sizeof(tox->command.command)); + else + fprintf(stderr, "failed to open '%s': %s\n", tox->script.filename, strerror(errno)); + } - fclose(tox->script.fd); - } - else { - fprintf(stderr, "failed to open '%s': %s\n", tox->script.filename, strerror(errno)); + else + fprintf(stderr, "file is empty; '%s'\n", tox->script.filename); + } + else + fprintf(stderr, "unable to stat() '%s': %s\n", tox->script.filename, strerror(errno)); + } |