[Toxine-cvs] CVS: toxine/src commands.c,1.78,1.79 commands.h,1.6,1.7 common.h,1.34,1.35 main.c,1.39,
Brought to you by:
f1rmb
From: Daniel Caujolle-B. <f1...@us...> - 2004-07-18 20:31:51
|
Update of /cvsroot/toxine/toxine/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19802/src Modified Files: commands.c commands.h common.h main.c parse.c parse.h script.c script.h utils.c utils.h Log Message: -New command parser. -Fix stdin and -e bug. -Release all VOPs when leaving. -Fix interactive option (-i) -Support multiple variables usage in command arguments. -Rework script parsing code. Index: commands.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/commands.c,v retrieving revision 1.78 retrieving revision 1.79 diff -u -r1.78 -r1.79 --- commands.c 1 Jun 2004 21:49:51 -0000 1.78 +++ commands.c 18 Jul 2004 20:31:40 -0000 1.79 @@ -224,10 +224,7 @@ " - $audio_driver: audio driver name (*).\n" " - $mrl: current mrl (*).\n" " - $script_file: file script name (*).\n" - " (*): display 'UNSET' when it's unspecified/unavailable.\n" - "\n" - " variable name syntax can be $foo, $(foo) or ${foo}\n" - "" + " (*): display 'UNSET' when it's unspecified/unavailable." }, { "exit", NO_ARGS, do_quit, "Quit toxine.", @@ -716,24 +713,26 @@ do { - toxine_parse_command(tox); - - if(strlen(tox->command.line)) { + if(toxine_command_have_current(tox)) { + const char *command = toxine_command_get_current_command(tox); + + toxine_command_prepare_args(tox); /* * Is the command match with one available ? */ i = found = 0; while((commands[i].command != NULL) && (found == 0)) { - if(!strncasecmp(tox->command.command, commands[i].command, strlen(tox->command.command))) { + + if(!strncasecmp(command, commands[i].command, strlen(command))) { if((commands[i].argtype == REQUIRE_ARGS) - && (tox->command.num_args <= 0)) { + && (toxine_command_get_current_num_args(tox) <= 0)) { perr("Command '%s' require argument(s).\n", commands[i].command); if(tox->script.in_use || tox->stdin) exit(TOX_ERR_WRONG_SYNTAX); found++; } - else if((commands[i].argtype == NO_ARGS) && (tox->command.num_args > 0)) { + else if((commands[i].argtype == NO_ARGS) && (toxine_command_get_current_num_args(tox) > 0)) { perr("Command '%s' doesn't require argument.\n", commands[i].command); if(tox->script.in_use || tox->stdin) exit(TOX_ERR_WRONG_SYNTAX); @@ -752,7 +751,7 @@ } if(!found) { - perr("unhandled command '%s'.\n", tox->command.command); + perr("unhandled command '%s'.\n", command); if(tox->script.in_use || tox->stdin) exit(TOX_ERR_UNKNOWN_CMD); @@ -760,9 +759,9 @@ } - toxine_free(tox->command.line); - - } while((tox->command.remain != NULL)); + toxine_command_next(tox); + + } while(toxine_command_have_current(tox) || toxine_command_have_next(tox)); } /* @@ -798,18 +797,6 @@ return cmd; } -/* - * Called when we leave toxine. - */ -void toxine_stop(toxine_t *tox) { - - error_code_clear(tox); - if(tox->xine) { - toxine_set_command_line(tox, "xine_stop"); - toxine_handle_command(tox, NULL); - } -} - static void toxine_handle_stdin(toxine_t *tox) { int fd; char command[32768]; @@ -819,6 +806,9 @@ fd = STDIN_FILENO; + if(toxine_command_have_current(tox)) + toxine_handle_command(tox, NULL); + while(tox->running) { FD_ZERO(&set); @@ -848,7 +838,7 @@ } } - toxine_set_command_line(tox, command); + toxine_command_insert_command_line(tox, command); toxine_handle_command(tox, NULL); } } @@ -948,7 +938,11 @@ toxine_handle_stdin(tox); } else if(tox->script.in_use) { - toxine_handle_script(tox); + tox->interactive = 0; + + if(toxine_read_script(tox)) + toxine_handle_command(tox, NULL); + tox->video.running = tox->running = 0; } else { @@ -986,8 +980,8 @@ char *line; line = grabbed_line; - - toxine_set_command_line(tox, line); + + toxine_command_append_command_line(tox, line); toxine_handle_command(tox, NULL); add_history(toxine_atoa(line)); @@ -1020,8 +1014,16 @@ _xine_exit(NULL, tox, NULL); } - if(tox->video.last_plugin && tox->video.last_plugin->video_out_release) - tox->video.last_plugin->video_out_release(tox); + { /* Release vops */ + int i; + + for(i = 0; i < tox->video.plugins_num; i++) { + if(tox->video.plugins[i]->video_out_release) { + tox->video.last_plugin = tox->video.plugins[i]; + tox->video.plugins[i]->video_out_release(tox); + } + } + } } @@ -1104,7 +1106,7 @@ memset(&buffer, 0, sizeof(buffer)); sprintf(buffer, "%s %s", "xine_get_video_property", toxine_get_arg(tox, 3)); - toxine_set_command_line(tox, buffer); + toxine_command_insert_command_line(tox, buffer); toxine_handle_command(tox, NULL); } @@ -1114,7 +1116,7 @@ memset(&buffer, 0, sizeof(buffer)); sprintf(buffer, "%s %s", "xine_get_video_property_min_max", toxine_get_arg(tox, 3)); - toxine_set_command_line(tox, buffer); + toxine_command_insert_command_line(tox, buffer); toxine_handle_command(tox, NULL); } @@ -1174,7 +1176,7 @@ sprintf(buffer, "%s %s %s", "xine_set_param", toxine_get_arg(tox, 3), toxine_get_arg(tox, 4)); - toxine_set_command_line(tox, buffer); + toxine_command_insert_command_line(tox, buffer); toxine_handle_command(tox, NULL); } @@ -1240,7 +1242,7 @@ sprintf(buffer, "%s %d", "xine_set_param XINE_PARAM_AUDIO_VOLUME", tox->audio.mixer.volume_level); - toxine_set_command_line(tox, buffer); + toxine_command_insert_command_line(tox, buffer); toxine_handle_command(tox, NULL); } @@ -1248,7 +1250,7 @@ } else if(toxine_is_arg_contain(tox, 2, "mute")) { - toxine_set_command_line(tox, "xine_set_param XINE_PARAM_AUDIO_MUTE"); + toxine_command_insert_command_line(tox, "xine_set_param XINE_PARAM_AUDIO_MUTE"); toxine_handle_command(tox, NULL); } @@ -1303,8 +1305,8 @@ * leave toxine. */ static void do_quit(commands_t *command, toxine_t *tox, void *data) { - - if(tox->interactive) { + + if(tox->interactive == 1) { if((toxine_confirm(tox, "Do you really want to quit toxine ? [yes|no]: "))) tox->video.running = tox->running = 0; } @@ -1323,7 +1325,7 @@ return_if_no_stream(tox); error_code_clear(tox); - if(tox->command.num_args) + if(toxine_command_get_current_num_args(tox)) toxine_set_current_mrl(tox, (toxine_get_arg(tox, 1))); else if(!tox->current_mrl) toxine_set_current_mrl_from_cur(tox); @@ -1337,6 +1339,7 @@ start_watchdog(tox, "xine_open"); result = xine_open(tox->stream, tox->current_mrl); stop_watchdog(tox); + pinfo("-- returned %d\n", result); if(result) { tox->xine_state |= XINE_OPEN; _xine_play(command, tox, (void *)NO_ARGS); @@ -1471,10 +1474,11 @@ int retval; if(!tox->script.in_use) { - if(tox->command.num_args) { + int num_args = toxine_command_get_current_num_args(tox); + if(num_args) { int i; - for(i = 0; i < tox->command.num_args; i++) + for(i = 0; i < num_args; i++) sprintf(buffer, "%s %s", buffer, (toxine_get_arg(tox, (i+1)))); } else @@ -1539,7 +1543,7 @@ error_code_clear(tox); - if(!tox->command.num_args) { + if(!toxine_command_get_current_num_args(tox)) { int i = 0, j; int maxlen = 0; int curpos = 0; @@ -2159,7 +2163,7 @@ pinfo(" ident: '%s'\n", mmk.ident); pinfo(" mrl: '%s'\n", mmk.mrl); pinfo(" start @ %d, end @ %d.\n", mmk.start, mmk.end); - confirm = (tox->interactive) ? (toxine_confirm(tox, "Add mediamark ? [yes|no]: ")) : 1; + confirm = (tox->interactive == 1) ? (toxine_confirm(tox, "Add mediamark ? [yes|no]: ")) : 1; if(confirm) { playlist_add_entry(tox, mmk.mrl, mmk.ident, mmk.start, mmk.end); @@ -2234,7 +2238,7 @@ pinfo(" ident: '%s'\n", mmk.ident); pinfo(" mrl: '%s'\n", mmk.mrl); pinfo(" start @ %d, end @ %d.\n", mmk.start, mmk.end); - confirm = (tox->interactive) ? (toxine_confirm(tox, "Add mediamark ? [yes|no]: ")) : 1; + confirm = (tox->interactive == 1) ? (toxine_confirm(tox, "Add mediamark ? [yes|no]: ")) : 1; if(confirm) { playlist_add_entry(tox, mmk.mrl, mmk.ident, mmk.start, mmk.end); @@ -2460,7 +2464,7 @@ pinfo(" mrl: '%s'\n", mmk.mrl); pinfo(" start @ %d, end @ %d.\n", mmk.start, mmk.end); - confirm = (tox->interactive) ? (toxine_confirm(tox, "Replace mediamark ? [yes|no]: ")) : 1; + confirm = (tox->interactive == 1) ? (toxine_confirm(tox, "Replace mediamark ? [yes|no]: ")) : 1; if(confirm) { playlist_replace_entry(&tox->playlist.mmk[entry], @@ -2524,44 +2528,41 @@ int i; int nargs = toxine_is_args(tox); int len = 0; - const char *var, *arg; + const char *arg; error_code_clear(tox); - + i = 1; while(i <= nargs) { - arg = toxine_get_arg(tox, i++); - if((var = toxine_get_var(tox, arg)) != NULL) - len += strlen(var); - else - len += strlen(arg); + arg = toxine_get_arg(tox, i); + + len += strlen(arg); + + i++; } if(len) { len += nargs; { - char buffer[(len * 2)]; - - memset(&buffer, 0, sizeof(buffer)); + char *buffer = NULL; i = 1; while(i <= nargs) { - + if(i > 1) - strcat(buffer, " "); - - arg = toxine_get_arg(tox, i++); - - if((var = toxine_get_var(tox, arg)) != NULL) - strcat(buffer, var); - else - strcat(buffer, arg); - + toxine_strcat(&buffer, " "); + + arg = toxine_get_arg(tox, i); + toxine_strcat(&buffer, (char *)arg); + + i++; } - sprintf(buffer, "%s%c", buffer, '\n'); + + toxine_strcat(&buffer, "\n"); pecho(buffer); + toxine_free(buffer); } } @@ -2623,48 +2624,13 @@ if(nargs >= 2) { char *c_repeat; int repeat = 0; - char *f_command = NULL; c_repeat = (char *) toxine_get_arg(tox, 1); if((repeat = strtol(c_repeat, &c_repeat, 10))) { + toxine_command_t *rcmd; - if(toxine_is_arg_match(tox, 2, "{") && toxine_is_last_arg_match(tox, "}")) { - int i = 3; - - for(;i < tox->command.num_args; i++) { - char *arg = (char *) toxine_get_arg(tox, i); - int len = strlen(arg); - int f_len = (f_command ? strlen(f_command) : 0); - - if(!f_command) - f_command = strdup(arg); - else { - f_command = (char *) realloc(f_command, f_len + len + 2); - strcat(f_command, " "); - strcat(f_command, arg); - } - } - - } - else - f_command = strdup((char *)toxine_get_arg(tox, 2)); - - if(f_command && strlen(f_command)) { - char *r_command = NULL; - int i; - - r_command = (char *) xine_xmalloc(((strlen(f_command) + 1) * repeat) + 1); - for(i = 0; i < repeat; i++) { - strcat(r_command, f_command); - strcat(r_command, ";"); - } - - toxine_set_command_line(tox, r_command); - toxine_handle_command(tox, NULL); - toxine_free(r_command); - } - - toxine_free(f_command); + if((rcmd = toxine_command_scissor(tox, (char *) toxine_get_arg(tox, 2)))) + toxine_command_n_insert_commands(tox, repeat, rcmd); } } Index: commands.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/commands.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- commands.h 7 Apr 2004 18:21:07 -0000 1.6 +++ commands.h 18 Jul 2004 20:31:40 -0000 1.7 @@ -23,15 +23,10 @@ #include <xine.h> #include "common.h" -#define NO_ARGS 1 -#define REQUIRE_ARGS 2 -#define OPTIONAL_ARGS 3 - void toxine_event_listener(void *, const xine_event_t *); int toxine_video_out_init(toxine_t *); int toxine_audio_out_init(toxine_t *); void toxine_handle_command(toxine_t *, void *); -void toxine_stop(toxine_t *); void toxine_run(toxine_t *, int, char **); void *toxine_end_thread(void *); Index: common.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/common.h,v retrieving revision 1.34 retrieving revision 1.35 diff -u -r1.34 -r1.35 --- common.h 1 Jun 2004 22:15:46 -0000 1.34 +++ common.h 18 Jul 2004 20:31:40 -0000 1.35 @@ -33,6 +33,7 @@ typedef struct toxine_s toxine_t; #include "utils.h" +#include "parse.h" #include "vo_plugin.h" #include "errors.h" @@ -60,6 +61,10 @@ #define _cfg_entry_t xine_cfg_entry_t #endif +#define NO_ARGS 1 +#define REQUIRE_ARGS 2 +#define OPTIONAL_ARGS 3 + #define INFO_PREFIX " (I) " #define HELP_PREFIX " (?) " #define ECHO_PREFIX " (e) " @@ -95,7 +100,53 @@ } \ } while(0) -#define poutalign() do { \ +#if 0 +#define toxine_strcat(d, fmt, ags...) do { \ + if((d) && (fmt)) { \ + va_list vargs; \ + char *__buf; \ + int n, size = 100; \ + \ + if((__buf = xine_xmalloc(size))) { \ + while(1) { \ + va_start(vargs, fmt); \ + n = vsnprintf(__buf, size, fmt, vargs); \ + va_end(vargs); \ + \ + if(n > -1 && n < size) \ + break; \ + \ + if(n > -1) \ + size = n + 1; \ + else \ + size *= 2; \ + \ + if(!(__buf = realloc(__buf, size))) \ + break; \ + } \ + \ + if(__buf) { \ + int len = strlen((d)) + strlen(__buf) + 1; \ + (d) = (char *) realloc((d), len); \ + strcat((d), __buf); \ + free(__buf); \ + } \ + } \ + } \ + } while(0) +#endif +#if 0 +#define toxine_strcat(d, s) do { \ + if((d) && (s)) { \ + int len = ((d) ? strlen((d)) : 0); \ + \ + (d) = (char *) realloc((d), len + strlen((s)) + 1); \ + strcat((d), (s)); \ + } \ + } while(0) +#endif + +#define poutalign() do { \ if(!tox->stdin) { \ fprintf(stdout, "%s", EMPTY_PREFIX); \ fflush(stdout); \ @@ -314,11 +365,7 @@ struct { int execute; - char *remain; - char *line; - char *command; - int num_args; - char *args[256]; + toxine_command_t *current; } command; char *current_mrl; @@ -339,10 +386,6 @@ struct { int in_use; char *filename; - char **lines; - int current; - int num_lines; - char *ln; } script; int stdin; Index: main.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/main.c,v retrieving revision 1.39 retrieving revision 1.40 diff -u -r1.39 -r1.40 --- main.c 1 Jun 2004 22:15:46 -0000 1.39 +++ main.c 18 Jul 2004 20:31:40 -0000 1.40 @@ -248,14 +248,9 @@ toxine_free(tox->configfile); toxine_free((char *)tox->video.name); toxine_free((char *)tox->audio.name); - toxine_free(tox->command.line); - toxine_free(tox->command.remain); - toxine_free(tox->command.command); + toxine_command_free_commands(tox->command.current); // toxine_free(tox->last_result); - for(i = 0; i < 256; i++) - toxine_free(tox->command.args[i]); - if(tox->argc) { for(i = 0; i < tox->argc; i++) toxine_free(tox->argv[i]); @@ -295,7 +290,6 @@ int c = '?'; int option_index = 0; int interactive = 1; - int i; toxine_error_code_t last_error; pthread_t pth; @@ -317,9 +311,7 @@ tox->watchdog.enabled = 0; tox->watchdog.running = 0; tox->watchdog.timeout = 15; /* 15 secs timeout */ - tox->command.remain = NULL; - tox->command.line = NULL; - tox->command.command = NULL; + tox->command.current = NULL; tox->command.execute = 0; tox->xine_state = 0; tox->msg_fd = -1; @@ -355,7 +347,7 @@ case 'e': /* execute commands */ if(optarg != NULL) { tox->command.execute = 1; - tox->command.line = strdup(toxine_atoa(optarg)); + toxine_command_append_command_line(tox, toxine_atoa(optarg)); } break; @@ -465,13 +457,7 @@ tox->loop_mode = PLAYLIST_LOOP; tox->last_result = NULL; - for(i = 0; i < 256; i++) - tox->command.args[i] = (char *) xine_xmalloc(sizeof(char *) * 2048); - - if(interactive) - tox->interactive = 1; - else - tox->interactive = 0; + tox->interactive = interactive ? 1 : 0; toxine_run(tox, argc - optind, &argv[optind]); last_error = tox->last_error; Index: parse.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/parse.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- parse.c 24 May 2004 20:53:25 -0000 1.10 +++ parse.c 18 Jul 2004 20:31:40 -0000 1.11 @@ -25,305 +25,641 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <ctype.h> #include "common.h" #include "utils.h" -/* - * Push first command in remain queue to current command. - */ -static void toxine_parse_destock_remain(toxine_t *tox) { - char *p; +#undef TRACE +struct toxine_command_s { + char *command; + + int num_args; + char **args; - /* - * Remain and line are already filled, perhaps a forced command line - * happen here: don't handle it here - */ + toxine_command_t *next; +}; - if((tox->command.remain) && (tox->command.line)) - return; - - if((tox->command.remain == NULL) && - (tox->command.line && - ((p = strchr(tox->command.line, ';')) && (*(p - 1) != '\\')))) { - tox->command.remain = strdup(tox->command.line); - toxine_free(tox->command.line); +#define CLEAN_BUFFER_EOL() \ + do { \ + if(strlen(buffer)) { \ + char *p = buffer + (strlen(buffer) - 1); \ + \ + while(*p && (*p == ' ')) { \ + if(((p - 1) >= buffer) && (*(p - 1) == '\\')) \ + break; \ + *p = '\0'; \ + if((p - 1) >= buffer) \ + p--; \ + } \ + \ + if(strlen(buffer)) { \ + char *p = buffer + (strlen(buffer) - 1); \ + \ + while(*p && (*p == ';')) { \ + if(((p - 1) >= buffer) && (*(p - 1) == '\\')) \ + break; \ + *p = '\0'; \ + if((p - 1) >= buffer) \ + p--; \ + } \ + } \ + \ + if(!strlen(buffer)) \ + pd = NULL; \ + } \ + } while(0) + +#define _COMMAND_ADD() \ + do { \ + if(!top_command) \ + top_command = cur_command = _command_new(); \ + \ + else { \ + cur_command->next = _command_new(); \ + cur_command = cur_command->next; \ + } \ + _command_add(tox, cur_command, buffer); \ + } while(0) + +#ifdef TRACE +void _command_dump(toxine_t *tox, toxine_command_t *command) { + + while(command) { + pout("-> '%s' [%d]\n", command->command, command->num_args); + if(command->num_args) { + int i = 0; + + while(command->args[i]) { + pout(" + '%s'\n", command->args[i]); + i++; + } + } + pout("--\n"); + command = command->next; } +} +#endif - if(tox->command.remain && - ((tox->command.line == NULL) || - ((tox->command.line != NULL) && (strlen(tox->command.line) == 0)))) { - char *pbuf, *pp, *c, *commandline, *remaining; - int len; - - if(tox->command.line && (strlen(tox->command.line) > strlen(tox->command.remain))) - len = strlen(tox->command.line); - else - len = strlen(tox->command.remain); +static void _command_free(toxine_command_t *command) { + if(command) { + free(command->command); + if(command->num_args) { + int i = 0; + + while(command->args[i]) { + free(command->args[i]); + i++; + } + free(command->args); + } + free(command); + } +} - commandline = (char *) xine_xmalloc(sizeof(char) * (len + 1)); - remaining = (char *) xine_xmalloc(sizeof(char) * (len + 1)); +static toxine_command_t *_command_new(void) { + toxine_command_t *cmd; + + cmd = (toxine_command_t *) xine_xmalloc(sizeof(toxine_command_t)); + cmd->command = NULL; + cmd->num_args = 0; + cmd->args = NULL; + cmd->next = NULL; + + return cmd; +} + +static toxine_command_t *_command_clone(toxine_command_t *command) { + toxine_command_t *cmd = NULL; + + if(command) { + cmd = _command_new(); + cmd->command = strdup(command->command); + cmd->next = command->next; - if((p = strchr(tox->command.remain, ';')) && (*(p - 1) != '\\')) { + if((cmd->num_args = command->num_args)) { + int i = 0; - pp = tox->command.remain; - pbuf = commandline; + cmd->args = (char **) xine_xmalloc(sizeof(char *) * (command->num_args + 1)); - while(pp < p) { - *pbuf = *pp; - pp++; - pbuf++; + while(command->args[i]) { + cmd->args[i] = strdup(command->args[i]); + i++; } - *pbuf = '\0'; + } + } + + return cmd; +} + +static toxine_command_t *_command_n_copy(toxine_t *tox, toxine_command_t *command, unsigned int n) { + toxine_command_t *top = command; + + if(command && (n > 1)) { + toxine_command_t *cmd = top; + int count = 1; + int i, j; + + while(cmd->next) { + count++; + cmd = cmd->next; + } + + for(i = 0; i < (n - 1); i++) { + toxine_command_t *pcmd = top; - if(strlen(commandline)) { - c = &commandline[strlen(commandline)]; - while((*c == ';') && (c >= commandline)) { - *c = '\0'; - c--; - } + for(j = 0; j < count; j++) { + cmd->next = _command_clone(pcmd); + cmd = cmd->next; + cmd->next = NULL; + pcmd = pcmd->next; } + } + } + +#ifdef TRACE + _command_dump(tox, top); +#endif + return top; +} + +static void _command_add(toxine_t *tox, toxine_command_t *command, char *cmd) { + if(command && cmd) { + if(command->command) { + perr("INTERNAL ERROR: command '%s' is already sets to '%s'\n", cmd, command->command); + free(command->command); + } + command->command = strdup(cmd); + } +} + +static void _command_add_arg(toxine_t *tox, toxine_command_t *command, char *arg) { + if(command && arg && strlen(arg)) { + char *_arg = strdup(arg); + char *parg, *p; + + p = _arg; + while(p && ((*p == '{') || (*p == ' '))) + p++; + + parg = p; + + p = (_arg + (strlen(_arg) - 1)); + while(p && (p > parg) && ((*p == '}') || (*p == ' '))) + *p-- = '\0'; + + command->args = (char **) realloc(command->args, sizeof(char *) * (command->num_args + 2)); + command->args[command->num_args] = strdup(parg); + command->args[command->num_args + 1] = NULL; + command->num_args++; + free(_arg); + } +} + +toxine_command_t *toxine_command_scissor(toxine_t *tox, char *command) { + toxine_command_t *top_command = NULL; + + if(command && strlen(command)) { + toxine_command_t *cur_command = NULL; + char *cmd_line = strdup(command); + int in_brace = 0; + int in_bracket = 0; + int got_command = 0; + char quote = 0; + char *buffer;//[strlen(command) + 1]; + char *ps, *pe; + char *pc, *pd; + + buffer = (char *) xine_xmalloc(strlen(cmd_line) + 1); + + ps = cmd_line; + pe = cmd_line + strlen(cmd_line); + + while(*ps && (*ps == ' ' || *ps == ';')) + ps++; + + while((pe > ps) && ((*pe == ' ') && (*(pe - 1) != '\\'))) + *(pe--) = '\0'; + + /* Now extract command and arguments */ + pc = ps; + pd = buffer; + + while(*pc) { - c = toxine_atoa(commandline); - - if(*p == ';') - p++; - - if(p) - sprintf(remaining, "%s", (toxine_atoa(p))); - - tox->command.line = (char *) realloc(tox->command.line, sizeof(char *) * (strlen(c) + 1)); - - sprintf(tox->command.line, "%s", c); - - if(p) { - /* remove last ';' */ - if(strchr(remaining, ';')) { - p = &remaining[strlen(remaining)]; - while((*p == ';') && (p >= remaining)) { - *p = '\0'; - p--; + switch(*pc) { + + case '\n': + case '\r': + if((pd - 1) >= buffer) + pd--; + break; + + + case '\\': + *(pd++) = *pc; + *pd = *(++pc); + break; + + + case ' ': + if(in_brace || in_bracket) { + if(strlen(buffer)) + *pd = *pc; + } + else { + CLEAN_BUFFER_EOL(); + + if(strlen(buffer)) { + + if(!got_command) { + _COMMAND_ADD(); + pd = NULL; + got_command = 1; + } + else { + _command_add_arg(tox, cur_command, buffer); + pd = NULL; + } } } - if(strlen(remaining)) { - tox->command.remain = (char *) realloc(tox->command.remain, sizeof(char *) * (strlen(remaining) + 1)); - sprintf(tox->command.remain, "%s", remaining); + while(*pc && (*(pc + 1) == ' ')) + pc++; + break; + + + case ';': + if(in_brace) { + *pd = *pc; } else { - toxine_free(tox->command.remain); + if(got_command) { + CLEAN_BUFFER_EOL(); + _command_add_arg(tox, cur_command, buffer); + pd = NULL; + } + else { + + CLEAN_BUFFER_EOL(); + + if(strlen(buffer)) + _COMMAND_ADD(); + + pd = NULL; + } + + got_command = 0; } + break; + + + case '[': + in_bracket++; +#ifdef TRACE + if(in_bracket > 1) + perr("Wrong syntax\n"); +#endif + break; + + + case ']': + if(in_bracket) { + in_bracket--; + CLEAN_BUFFER_EOL(); + _command_add_arg(tox, cur_command, buffer); + pd = NULL; + } +#ifdef TRACE + else + perr("Bad syntax\n"); +#endif + break; + case '{': + in_brace++; + printf("BRACE %d: %s\n", in_brace, pc); + if(in_brace == 1) { + } + else + *pd = *pc; + break; + + + case '}': + if(in_brace) { + in_brace--; + + if(!in_brace) { + CLEAN_BUFFER_EOL(); + _command_add_arg(tox, cur_command, buffer); + pd = NULL; + } + else { + *pd = *pc; + } + } +#ifdef TRACE + else + perr("Something's wrong in wonderland...\n"); +#endif + break; + + + case '"': + quote = '"'; + case '\'': + { + char *opc; + + if(quote == 0) { + if(!strchr((pc + 1), '\'')) + goto __store_as_is; + quote = '\''; + } + + opc = ++pc; + + while(*pc && (*pc != quote)) + *(pd++) = *(pc++); + + if(opc != pc) + pd--; + + quote = 0; + } + break; + + + default: + __store_as_is: + *pd = *pc; + break; + } + + pc++; + + if(!pd) { + memset(buffer, 0, strlen(cmd_line) + 1); + pd = buffer; } else { - toxine_free(tox->command.remain); + if((pd > buffer) || ((pd == buffer) && (*pd != '\0'))) + pd++; } - } - else { /* no ';' in remain, copy AS IS remain to line */ - tox->command.line = (char *) realloc(tox->command.line, sizeof(char *) * (strlen(tox->command.remain) + 1)); - sprintf(tox->command.line, "%s", tox->command.remain); + + if(pd) { + CLEAN_BUFFER_EOL(); - toxine_free(tox->command.remain); + if(strlen(buffer)) { + + if(!got_command) + _COMMAND_ADD(); + else + _command_add_arg(tox, cur_command, buffer); + } } - - toxine_free(commandline); - toxine_free(remaining); + + free(buffer); + free(cmd_line); } + + return top_command; } /* - * handle multicommand line. + * return argument number. */ -static void toxine_parse_handle_multicommands(toxine_t *tox) { +int toxine_is_args(toxine_t *tox) { + if(!tox) + return -1; - if(tox->command.remain) - perr("Ooch, Unexpected state, remain isn't empty\n"); - else { - tox->command.remain = strdup(tox->command.line); - toxine_parse_destock_remain(tox); - } + return (tox->command.current ? + tox->command.current->num_args : 0); } /* - * parse command, extract arguments. + * return argumemnt <num>, NULL if there is not argument. */ -void toxine_parse_command(toxine_t *tox) { - char *p, *cmd, *cmdl; - int i = 0; - - toxine_parse_destock_remain(tox); +const char *toxine_get_arg(toxine_t *tox, int num) { + if((!tox || !tox->command.current) || (num < 1) || (tox->command.current->num_args < num)) + return NULL; - if((p = strchr(tox->command.line, ';'))) { - - if((*(p - 1) != '\\')) - toxine_parse_handle_multicommands(tox); - else { - char *pp = tox->command.line; + return(tox->command.current->args[num - 1]); +} - while((p = strchr(pp, ';')) && (*(p - 1) == '\\')) { - *(p - 1) = ' '; - pp = p + 1; - } - } +/* + * return 1 if *arg match with argument <pos> + */ +int toxine_is_arg_contain(toxine_t *tox, int pos, const char *arg) { + + if(tox && tox->command.current && pos && ((arg != NULL) && (strlen(arg))) && (tox->command.current->num_args >= pos)) { + if(!strncmp(tox->command.current->args[pos - 1], arg, strlen(tox->command.current->args[pos - 1]))) + return 1; + } + + return 0; +} +int toxine_is_arg_match(toxine_t *tox, int pos, const char *arg) { + + if(tox && tox->command.current && pos && ((arg != NULL) && (strlen(arg))) && (tox->command.current->num_args >= pos)) { + if(!strcmp(tox->command.current->args[pos - 1], arg)) + return 1; } + + return 0; +} - cmdl = tox->command.line; +/* + * return 1 if last arg match *arg + */ +int toxine_is_last_arg_contain(toxine_t *tox, const char *arg) { - if(tox->command.num_args) { - for(i = 0; i < tox->command.num_args; i++) - memset(tox->command.args[i], 0, sizeof(tox->command.args[i])); + if(tox && tox->command.current && ((arg != NULL) && (strlen(arg))) && tox->command.current->num_args) { + if(!strncmp(tox->command.current->args[tox->command.current->num_args - 1], arg, + strlen(tox->command.current->args[tox->command.current->num_args - 1]))) + return 1; } - tox->command.num_args = 0; + return 0; +} +int toxine_is_last_arg_match(toxine_t *tox, const char *arg) { - while((*cmdl == ' ') || (*cmdl == '\t')) - cmdl++; + if(tox && tox->command.current && ((arg != NULL) && (strlen(arg))) && tox->command.current->num_args) { + if(!strcmp(tox->command.current->args[tox->command.current->num_args - 1], arg)) + return 1; + } - cmd = cmdl; + return 0; +} - while(*cmd != '\0' && (*cmd != ' ' && *(cmd - 1) != '\\') && *cmd != '\t') - cmd++; +void toxine_command_n_insert_commands(toxine_t *tox, unsigned int n, toxine_command_t *command) { + if(tox && command) { + toxine_command_t *current = tox->command.current; + toxine_command_t *ncmd = _command_n_copy(tox, command, n); - if(cmd >= (cmdl + strlen(cmdl))) - cmd = NULL; + if(!current) + tox->command.current = ncmd; + else { + toxine_command_t *cmd = tox->command.current->next; - if(cmd) { - tox->command.command = (char *) realloc(tox->command.command, strlen(cmdl) - strlen(cmd) + 1); + current->next = ncmd; + + while(ncmd->next) + ncmd = ncmd->next; + + ncmd->next = cmd; + } + } +} - memset(tox->command.command, 0, sizeof(tox->command.command)); - snprintf(tox->command.command, (strlen(cmdl) - strlen(cmd))+1, "%s", cmdl); - - cmd = toxine_atoa(cmd); +void toxine_command_n_append_commands(toxine_t *tox, unsigned int n, toxine_command_t *command) { + if(tox && command) { + toxine_command_t *current = tox->command.current; + toxine_command_t *ncmd = _command_n_copy(tox, command, n); + + if(!current) { + tox->command.current = ncmd; + } + else { + toxine_command_t *pcmd = command; + + while(pcmd->next) + pcmd = pcmd->next; + + pcmd->next = ncmd; + } + } +} + +void toxine_command_insert_command_line(toxine_t *tox, char *line) { + if(tox && line && strlen(line)) + toxine_command_n_insert_commands(tox, 1, (toxine_command_scissor(tox, line))); +} + +void toxine_command_n_insert_command_line(toxine_t *tox, unsigned int n, char *line) { + if(tox && line && strlen(line)) + toxine_command_n_insert_commands(tox, n, (toxine_command_scissor(tox, line))); +} + +void toxine_command_append_command_line(toxine_t *tox, char *line) { + if(tox && line && strlen(line)) + toxine_command_n_append_commands(tox, 1, (toxine_command_scissor(tox, line))); +} + +void toxine_command_n_append_command_line(toxine_t *tox, unsigned int n, char *line) { + if(tox && line && strlen(line)) + toxine_command_n_append_commands(tox, n, (toxine_command_scissor(tox, line))); +} + +int toxine_command_have_current(toxine_t *tox) { + if(tox && tox->command.current && tox->command.current->command && strlen(tox->command.current->command)) + return 1; + + return 0; +} + +void toxine_command_free_commands(toxine_command_t *commands) { + if(commands) { + toxine_command_t *cmd = commands; - /* - * Extract and store args - */ - if(cmd < (cmdl + strlen(cmdl))) { - char *pcmd, *pb, buf[256]; - int nargs = 0; - int get_quote = 0, get_dbl_quote = 0; + while(cmd) { + toxine_command_t *pcmd = cmd; - pcmd = cmd; - memset(&buf, 0, sizeof(buf)); - pb = buf; + cmd = pcmd->next; - while(*(pcmd - 1) != '\0') { - - switch(*pcmd) { + _command_free(pcmd); + } + } +} - case '"': - case '\'': - if(*(pcmd - 1) != '\\') { - if(*pcmd == '\'') { - if(!get_dbl_quote) - get_quote++; - } - else if(*pcmd == '"') { - if(!get_quote) - get_dbl_quote++; - } +void toxine_command_next(toxine_t *tox) { + toxine_command_t *current; + + if(tox && (current = tox->command.current)) { + tox->command.current = tox->command.current->next; + _command_free(current); + } +} - if(get_quote == 2) - get_quote = 0; - else if(get_dbl_quote == 2) - get_dbl_quote = 0; - } - else { - pb--; - goto __store_char; - } - - break; - - case '\t': - if((!get_quote) && (!get_dbl_quote)) - goto __end_args; - else - goto __store_char; - break; - - case '\0': - goto __end_args; - break; - - case '$': - { - char *var; - char buffer[2048]; - char *p = buffer; - char *pp = pcmd; - - memset(&buffer, 0, sizeof(buffer)); +int toxine_command_have_next(toxine_t *tox) { + if(tox && tox->command.current) + return (tox->command.current->next ? 1 : 0); - while(pp && *pp && *pp != ' ') { - *p = *pp; - p++; - pp++; - } - p = '\0'; + return 0; +} + +const char *toxine_command_get_current_command(toxine_t *tox) { + if(tox && tox->command.current) + return tox->command.current->command; + + return NULL; +} + +int toxine_command_get_current_num_args(toxine_t *tox) { + if(tox && tox->command.current) + return tox->command.current->num_args; + + return 0; +} - if((var = toxine_get_var(tox, buffer))) { - p = var; +void toxine_command_prepare_args(toxine_t *tox) { + if(tox && tox->command.current && tox->command.current->num_args) { + int i = 0; + char *p; + + while((p = tox->command.current->args[i])) { + + if(strchr(p, '$')) { + char *pp = p; + char *newarg = NULL; + char *var = NULL; + + while(pp && (*pp != '\0')) { + + switch(*pp) { + case '$': + { + char *d = pp; + char *_var = NULL; - while(p && *p != '\0') { - *pb = *p; - pb++; - p++; + toxine_strcat(&_var, "%c", *d); + d++; + while(d && (*d != '\0') && ((*d != '$') && ((isalnum(*d)) || (*d == '_')))) { + toxine_strcat(&_var, "%c", *d); + d++; } - pcmd += strlen(buffer); - goto __end_args; - } - else - goto __store_char; - - } - break; + pp = d; - case ' ': - if((*(pcmd - 1) != '\\') && ((get_quote == 0) && (get_dbl_quote == 0))) { - while(*(pcmd + 1) == ' ') - pcmd++; + if((var = toxine_get_var(tox, _var))) + toxine_strcat(&newarg, var); + else + toxine_strcat(&newarg, _var); + toxine_free(_var); + } + break; - __end_args: - sprintf(tox->command.args[nargs], "%s", buf); - nargs++; - memset(&buf, 0, sizeof(buf)); - pb = buf; - } - else { - if(*(pcmd - 1) == '\\') - pb--; - goto __store_char; + default: + toxine_strcat(&newarg, "%c", *pp); + pp++; + break; } - break; - - default: - __store_char: - *pb = *pcmd; - pb++; - break; } - pcmd++; + + if(!var && !newarg) + toxine_strcat(&newarg, p); + + toxine_free(var); + + tox->command.current->args[i] = (char *) realloc(tox->command.current->args[i], strlen(newarg) + 1); + strcpy(tox->command.current->args[i], newarg); + + toxine_free(newarg); } - tox->command.num_args = nargs; + + i++; } } - else { - tox->command.command = (char *) realloc(tox->command.command, strlen(cmdl) + 1); - - memset(tox->command.command, 0, sizeof(tox->command.command)); - sprintf(tox->command.command, "%s", cmdl); - } - -#if 0 - { - int k; - pinfo("tox->command = '%s'\n", tox->command.command); - if(tox->command.num_args) - for(k = 0; k < tox->command.num_args; k++) - pinfo("tox->command_arg[%d] = '%s'\n", k, tox->command.args[k]); - } -#endif } Index: parse.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/parse.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- parse.h 7 Apr 2004 18:21:07 -0000 1.3 +++ parse.h 18 Jul 2004 20:31:40 -0000 1.4 @@ -22,6 +22,30 @@ #include "common.h" -void toxine_parse_command(toxine_t *tox); +typedef struct toxine_command_s toxine_command_t; + +int toxine_is_args(toxine_t *tox); +const char *toxine_get_arg(toxine_t *tox, int num); +int toxine_is_arg_contain(toxine_t *tox, int pos, const char *arg); +int toxine_is_arg_match(toxine_t *tox, int pos, const char *arg); +int toxine_is_last_arg_contain(toxine_t *tox, const char *arg); +int toxine_is_last_arg_match(toxine_t *tox, const char *arg); + +toxine_command_t *toxine_command_scissor(toxine_t *tox, char *command); +void toxine_command_n_insert_commands(toxine_t *tox, unsigned int n, toxine_command_t *command); +void toxine_command_n_append_commands(toxine_t *tox, unsigned int n, toxine_command_t *command); +void toxine_command_insert_command_line(toxine_t *tox, char *line); +void toxine_command_n_insert_command_line(toxine_t *tox, unsigned int n, char *line); +void toxine_command_append_command_line(toxine_t *tox, char *line); +void toxine_command_n_append_command_line(toxine_t *tox, unsigned int n, char *line); +void toxine_command_free_commands(toxine_command_t *commands); + +int toxine_command_have_current(toxine_t *tox); +void toxine_command_next(toxine_t *tox); +int toxine_command_have_next(toxine_t *tox); + +const char *toxine_command_get_current_command(toxine_t *tox); +int toxine_command_get_current_num_args(toxine_t *tox); +void toxine_command_prepare_args(toxine_t *tox); #endif Index: script.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/script.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- script.c 27 May 2004 00:27:39 -0000 1.6 +++ script.c 18 Jul 2004 20:31:40 -0000 1.7 @@ -29,115 +29,103 @@ #include "commands.h" #include "utils.h" -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 char *_script_prepare_buffer(char *buffer) { + char *buf = NULL; + char *p; -static int toxine_script_split_lines(toxine_t *tox, char *_buf) { - - if(_buf) { - char *buf, *p, *pp, *obuf; + if(buffer) { + char *pbuf; - buf = strdup(_buf); - obuf = buf; - pp = buf; - - tox->script.num_lines = 0; + buf = strdup(buffer); + pbuf = buf; - while((p = strchr(pp, '\n'))) { - if(*(p - 1) == '\\') { - *(p - 1) = ' '; - *p = ' '; - } - else - pp++; - } + while((p = xine_strsep(&pbuf, "\n"))) { + int no_space = 0; + + if(strlen(p)) { - while((p = xine_strsep(&buf, "\n"))) { + if(*p == '#') { + char *ps = p; + + while(ps && (*ps != '\0')) + *ps++ = ' '; + + } + else { + char *pp, *c; + + pp = p; + while(pp && (*pp != '\0')) { + if(*pp != ' ') { + no_space = 1; + break; + } + pp++; + } + + pp = p; + if((c = strchr(pp, '#'))) { + if(*(c - 1) == ' ') { + while(c && (*c != '\0')) + *c++ = ' '; + } + } - 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'; + } + } - tox->script.lines[tox->script.num_lines++] = strdup(p); + if(*(p + strlen(p) - 1) == '\\') { + *(p + strlen(p) - 1) = ' '; + *(p + strlen(p)) = ' '; + } + else { + if(!no_space) + *(p + strlen(p)) = ' '; + else { + if(*(p + strlen(p)) != ';') + *(p + strlen(p)) = ';'; + } } } - - toxine_free(buf); } - - return (tox->script.num_lines > 0); -} - -static int toxine_script_get_next_line(toxine_t *tox) { - - __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; - } - return 0; + + return buf; } -/* - * Main loop in script mode. - */ -void toxine_handle_script(toxine_t *tox) { +int toxine_read_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; + char *buf, *buffer; + 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); perr("read() returned wrong size (expected %lld, readed %lld)\n", st.st_size, br); - return; + return 0; } 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); + buf[br] = '\0'; + + /* Prepare the single line */ + if((buffer = _script_prepare_buffer(buf))) { + // printf("BUFFER: '%s'\n", buffer); + toxine_command_insert_command_line(tox, buffer); + toxine_free(buffer); } - + + toxine_free(buf); + return 1; } else { perr("Failed to open '%s': %s\n", tox->script.filename, strerror(errno)); @@ -156,4 +144,5 @@ error_code_set(tox, TOX_ERR_INTERNAL); } + return 0; } Index: script.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/script.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- script.h 7 Apr 2004 18:21:07 -0000 1.3 +++ script.h 18 Jul 2004 20:31:40 -0000 1.4 @@ -22,6 +22,6 @@ #include "common.h" -void toxine_handle_script(toxine_t *tox); +int toxine_read_script(toxine_t *tox); #endif Index: utils.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/utils.c,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- utils.c 13 Jun 2004 17:26:36 -0000 1.32 +++ utils.c 18 Jul 2004 20:31:40 -0000 1.33 @@ -29,6 +29,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> +#include <stdarg.h> #include "readline.h" #include "tilde.h" @@ -236,84 +237,6 @@ } /* - * return argument number. - */ -int toxine_is_args(toxine_t *tox) { - if(!tox) - return -1; - - return tox->command.num_args; -} - -/* - * return argumemnt <num>, NULL if there is not argument. - */ -const char *toxine_get_arg(toxine_t *tox, int num) { - if((tox == NULL) || (num < 1) || (tox->command.num_args < num)) - return NULL; - - return(tox->command.args[num - 1]); -} - -/* - * return 1 if *arg match with argument <pos> - */ -int toxine_is_arg_contain(toxine_t *tox, int pos, const char *arg) { - - if(tox && pos && ((arg != NULL) && (strlen(arg))) && (tox->command.num_args >= pos)) { - if(!strncmp(tox->command.args[pos - 1], arg, strlen(tox->command.args[pos - 1]))) - return 1; - } - - return 0; -} -int toxine_is_arg_match(toxine_t *tox, int pos, const char *arg) { - - if(tox && pos && ((arg != NULL) && (strlen(arg))) && (tox->command.num_args >= pos)) { - if(!strcmp(tox->command.args[pos - 1], arg)) - return 1; - } - - return 0; -} - -/* - * return 1 if last arg match *arg - */ -int toxine_is_last_arg_contain(toxine_t *tox, const char *arg) { - - if(tox && ((arg != NULL) && (strlen(arg))) && tox->command.num_args) { - if(!strncmp(tox->command.args[tox->command.num_args - 1], arg, strlen(tox->command.args[tox->command.num_args - 1]))) - return 1; - } - - return 0; -} -int toxine_is_last_arg_match(toxine_t *tox, const char *arg) { - - if(tox && ((arg != NULL) && (strlen(arg))) && tox->command.num_args) { - if(!strcmp(tox->command.args[tox->command.num_args - 1], arg)) - return 1; - } - - return 0; -} - -/* - * Set current command line from line. - */ -void toxine_set_command_line(toxine_t *tox, char *line) { - - if(tox && line && strlen(line)) { - - tox->command.line = (char *) realloc(tox->command.line, strlen(line) + 1); - memset(tox->command.line, 0, sizeof(tox->command.line)); - sprintf(tox->command.line, "%s", line); - - } -} - -/* * Look for first entry in playlist matching with 'mrl' */ static int toxine_lookup_in_playlist(toxine_t *tox, const char *mrl) { @@ -716,6 +639,7 @@ switch(info_type) { case XINE_STREAM_INFO_BITRATE: case XINE_STREAM_INFO_SEEKABLE: + case XINE_STREAM_INFO_VIDEO_HANDLED: case XINE_STREAM_INFO_AUDIO_HANDLED: case XINE_STREAM_INFO_HAS_CHAPTERS: case XINE_STREAM_INFO_HAS_VIDEO: @@ -738,7 +662,6 @@ case XINE_STREAM_INFO_VIDEO_CHANNELS: case XINE_STREAM_INFO_VIDEO_STREAMS: case XINE_STREAM_INFO_VIDEO_BITRATE: - case XINE_STREAM_INFO_VIDEO_HANDLED: case XINE_STREAM_INFO_FRAME_DURATION: case XINE_STREAM_INFO_AUDIO_CHANNELS: case XINE_STREAM_INFO_AUDIO_BITS: @@ -746,6 +669,9 @@ case XINE_STREAM_INFO_AUDIO_BITRATE: case XINE_STREAM_INFO_MAX_AUDIO_CHANNEL: case XINE_STREAM_INFO_MAX_SPU_CHANNEL: + case XINE_STREAM_INFO_AUDIO_MODE: + case XINE_STREAM_INFO_SKIPPED_FRAMES: + case XINE_STREAM_INFO_DISCARDED_FRAMES: pinfo("%d\n", iinfo); break; @@ -757,144 +683,162 @@ } char *toxine_get_var(toxine_t *tox, const char *var) { - static char buffer[32768]; - + char *buffer = NULL; + error_code_clear(tox); - memset(buffer, 0, sizeof(buffer)); if(tox && var) { char *variable; - variable = strchr(var, '$'); - - if(variable && (*variable == '$') && (*(variable + 1) != '$')) { - char varname[1024]; - int found = 0; + if((variable = strchr(var, '$'))) { - memset(varname, 0, sizeof(varname)); - - if(sscanf(variable, "$\(%[a-z-A-Z-0-9-_])", &varname[0]) == 1) - found = 1; - else if(sscanf(variable, "$\{%[a-z-A-Z-0-9-_]}", &varname[0]) == 1) - found = 1; - else if(sscanf(variable, "$%[a-z-A-Z-0-9-_]", &varname[0]) == 1) - found = 1; - - if(found) { - char *envvar; + if((*variable == '$') && (*(variable + 1) != '$')) { + char varname[1024]; + int found = 0; - if((envvar = getenv(varname))) { - snprintf(buffer, sizeof(buffer), "%s", envvar); - - return &buffer[0]; - } - } - - if(found) { - static const char day_names[] = "SunMonTueWedThuFriSat"; - static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; + memset(varname, 0, sizeof(varname)); - found = 0; - if(!strncasecmp(varname, "result", 6)) { - snprintf(buffer, sizeof(buffer), "%d", toxine_get_last_int_result(tox)); - found = 1; - } - else if(!strncasecmp(varname, "cresult", 7)) { - snprintf(buffer, sizeof(buffer), "%s", toxine_get_last_char_result(tox)); + if(sscanf(variable, "$\(%[a-z-A-Z-0-9-_])", &varname[0]) == 1) found = 1; - } - else if(!strncasecmp(varname, "cxresult", 7)) { - char *p = toxine_get_last_char_result(tox); - if(p) { - int val = strtol(p, &p, 10); - snprintf(buffer, sizeof(buffer), "0x%x", val); - } - else - snprintf(buffer, sizeof(buffer), "Nothing available"); + else if(sscanf(variable, "$\{%[a-z-A-Z-0-9-_]}", &varname[0]) == 1) found = 1; - } - else if(!strncasecmp(varname, "iresult", 7)) { - snprintf(buffer, sizeof(buffer), "%d", toxine_get_last_int_result(tox)); - found = 1; - } - else if(!strncasecmp(varname, "xresult", 7)) { - snprintf(buffer, sizeof(buffer), "0x%x", toxine_get_last_int_result(tox)); - found = 1; - } - else if(!strncasecmp(varname, "version", 7)) { - snprintf(buffer, sizeof(buffer), "%s", VERSION); - found = 1; - } - else if(!strncasecmp(varname, "date", 4)) { - time_t curtime; - struct tm *lt; - - time(&curtime); - lt = localtime(&curtime); - snprintf(buffer, sizeof(buffer), "%-3.3s %-3.3s %d %d", - day_names + 3 * lt->tm_wday, month_names + 3 * lt->tm_mon, - lt->tm_mday, (lt->tm_year + 1900)); + else if(sscanf(variable, "$%[a-z-A-Z-0-9-_]", &varname[0]) == 1) found = 1; + + if(found) { + char *envvar; + + if((envvar = getenv(varname))) { + toxine_strcat(&buffer, envvar); + return buffer; + } } - else if(!strncasecmp(varname, "time", 4)) { - time_t curtime; - struct tm *lt; + + if(found) { + static const char day_names[] = "SunMonTueWedThuFriSat"; + static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; - time(&curtime); - lt = localtime(&curtime); - snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec); - found = 1; - } - else if(!strncasecmp(varname, "config_file", 11)) { - snprintf(buffer, sizeof(buffer), "%s", (tox->configfile) ? tox->configfile : "UNSET"); - found = 1; - } - else if(!strncasecmp(varname, "video_driver", 12)) { - snprintf(buffer, sizeof(buffer), "%s", (tox->video.name) ? tox->video.name : "UNSET"); - found = 1; - } - else if(!strncasecmp(varname, "audio_driver", 12)) { - snprintf(buffer, sizeof(buffer), "%s", (tox->audio.name) ? tox->audio.name : "UNSET"); - found = 1; - } - else if(!strncasecmp(varname, "mrl", 3)) { - snprintf(buffer, sizeof(buffer), "%s", (tox->current_mrl) ? tox->current_mrl : "UNSET"); - found = 1; - } - else if(!strncasecmp(varname, "script_file", 11)) { - snprintf(buffer, sizeof(buffer), "%s", (tox->script.filename) ? tox->script.filename : "UNSET"); - found = 1; - } - else { - char *p = varname; - int isargv = 1; - - while(p && *p != '\0') { - if(!isdigit(*p)) { - isargv = 0; - break; + if(!strncasecmp(varname, "result", 6)) + toxine_strcat(&buffer, "%d", toxine_get_last_int_result(tox)); + else if(!strncasecmp(varname, "cresult", 7)) + toxine_strcat(&buffer, "%s", toxine_get_last_char_result(tox)); + else if(!strncasecmp(varname, "cxresult", 7)) { + char *p = toxine_get_last_char_result(tox); + if(p) { + int val = strtol(p, &p, 10); + + toxine_strcat(&buffer, "0x%x", val); } - p++; - } + else + toxine_strcat(&buffer, "Nothing available"); - if(isargv) { + } + else if(!strncasecmp(varname, "iresult", 7)) + toxine_strcat(&buffer, "%d", toxine_get_last_int_result(tox)); + else if(!strncasecmp(varname, "xresult", 7)) + toxine_strcat(&buffer, "0x%x", toxine_get_last_int_result(tox)); + else if(!strncasecmp(varname, "version", 7)) + toxine_strcat(&buffer, "%s", VERSION); + else if(!strncasecmp(varname, "date", 4)) { + time_t curtime; + struct tm *lt; + + time(&curtime); + lt = localtime(&curtime); + toxine_strcat(&buffer, "%-3.3s %-3.3s %d %d", + day_names + 3 * lt->tm_wday, month_names + 3 * lt->tm_mon, lt->tm_mday, (lt->tm_year + 1900)); + } + else if(!strncasecmp(varname, "time", 4)) { + time_t curtime; + struct tm *lt; + + time(&curtime); + lt = localtime(&curtime); + toxine_strcat(&buffer, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec); + } + else if(!strncasecmp(varname, "config_file", 11)) + toxine_strcat(&buffer, "%s", (tox->configfile) ? tox->configfile : "UNSET"); + else if(!strncasecmp(varname, "video_driver", 12)) + toxine_strcat(&buffer, "%s", (tox->video.name) ? tox->video.name : "UNSET"); + else if(!strncasecmp(varname, "audio_driver", 12)) + toxine_strcat(&buffer, "%s", (tox->audio.name) ? tox->audio.name : "UNSET"); + else if(!strncasecmp(varname, "mrl", 3)) + toxine_strcat(&buffer, "%s", (tox->current_mrl) ? tox->current_mrl : "UNSET"); + else if(!strncasecmp(varname, "script_file", 11)) + toxine_strcat(&buffer, "%s", (tox->script.filename) ? tox->script.filename : "UNSET"); + else { char *p = varname; - int val = strtol(p, &p, 10); + int isargv = 1; - if((val >= 0) && (val < tox->argc)) { - snprintf(buffer, sizeof(buffer), "%s", tox->argv[val]); - found = 1; + while(p && *p != '\0') { + if(!isdigit(*p)) { + isargv = 0; + break; + } + p++; + } + + if(isargv) { + char *p = varname; + int val = strtol(p, &p, 10); + + if((val >= 0) && (val < tox->argc)) + toxine_strcat(&buffer, "%s", tox->argv[val]); + } } } + } + } + } - if(found) - return &buffer[0]; + return buffer; +} +char *toxine_strcat(char **dest, char *fmt, ...) { + char *p = NULL; + + if(fmt) { + va_list vargs; + int n, size = 100; + char *buf; + + if((buf = xine_xmalloc(size))) { + + while(1) { + va_start(vargs, fmt); + n = vsnprintf(buf, size, fmt, vargs); + va_end(vargs); + + if(n > -1 && n < size) + break; + + if(n > -1) + size = n + 1; + else + size *= 2; + + if(!(buf = realloc(buf, size))) + break; + } + + if(buf) { + + if((*dest)) { + int len = ((*dest) ? strlen((*dest)) : 0) + strlen(buf) + 1; + + (*dest) = (char *) realloc((*dest), len); + p = strncat((*dest), buf, strlen(buf)); + } + else + p = (*dest) = strdup(buf); + + free(buf); } - } + else + printf("%s(): buf is NULL\n", __func__); + } } - - snprintf(buffer, sizeof(buffer), "%s", var); - return &buffer[0]; + + return p; } Index: utils.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/utils.h,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- utils.h 27 May 2004 00:27:39 -0000 1.16 +++ utils.h 18 Jul 2004 20:31:40 -0000 1.17 @@ -21,6 +21,7 @@ #define __TOXINE_UTILS_H__ #include "common.h" +#include <stdarg.h> char *toxine_get_yesno_string(uint32_t val); char *toxine_get_fourcc_string(uint32_t f); @@ -32,12 +33,6 @@ void toxine_set_command_line(toxine_t *tox, char *line); void toxine_set_current_mrl_from_cur(toxine_t *tox); void toxine_set_current_mrl(toxine_t *tox, const char *mrl); -int toxine_is_args(toxine_t *tox); -const char *toxine_get_arg(toxine_t *tox, int num); -int toxine_is_arg_contain(toxine_t *tox, int pos, const char *arg); -int toxine_is_arg_match(toxine_t *tox, int pos, const char *arg); -int toxine_is_last_arg_contain(toxine_t *tox, const char *arg); -int toxine_is_last_arg_match(toxine_t *tox, const char *arg); int toxine_mkdir_safe(toxine_t *tox, char *path); void toxine_draw_bar(toxine_t *tox, char *title, int min, int max, int val); @@ -61,4 +56,5 @@ char *toxine_get_var(toxine_t *tox, const char *var); +char *toxine_strcat(char **dest, char *fmt, ...); #endif |