/* ** Copyright (C) 2002-2004 Daniel Caujolle-Bert ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ** */ #include #include #include #include #include "common.h" #include "commands.h" #include "utils.h" extern int errno; /* * Cleanup the EOL ('\n','\r',' ') */ static void toxine_script_clean_eol(toxine_t *tox) { char *p; p = tox->script.ln; if(p) { while(*p != '\0') { if(*p == '\n' || *p == '\r') { *p = '\0'; break; } p++; } while(p > tox->script.ln) { --p; if(*p == ' ') *p = '\0'; else break; } } } /* * 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; } } toxine_script_clean_eol(tox); if(tox->script.ln && !strlen(tox->script.ln)) goto __get_next_line; return((tox->script.ln != NULL)); } /* * Main loop in script mode. */ void toxine_handle_script(toxine_t *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)); } fclose(tox->script.fd); } else { fprintf(stderr, "failed to open '%s': %s\n", tox->script.filename, strerror(errno)); } }