dosbox-wii/src/shell/shell_batch.cpp

155 lines
3.8 KiB
C++
Raw Normal View History

2009-05-02 23:03:37 +02:00
/*
2009-05-03 00:28:34 +02:00
* Copyright (C) 2002-2007 The DOSBox Team
2009-05-02 23:03:37 +02:00
*
* 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
2009-05-03 00:02:15 +02:00
* GNU General Public License for more details.
2009-05-02 23:03:37 +02:00
*
* 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.
*/
2009-05-03 00:37:32 +02:00
/* $Id: shell_batch.cpp,v 1.24 2007/06/13 07:22:17 qbix79 Exp $ */
2009-05-03 00:02:15 +02:00
2009-05-02 23:03:37 +02:00
#include <stdlib.h>
#include <string.h>
2009-05-02 23:53:27 +02:00
#include "shell.h"
2009-05-03 00:18:08 +02:00
#include "support.h"
2009-05-02 23:03:37 +02:00
BatchFile::BatchFile(DOS_Shell * host,char * name, char * cmd_line) {
prev=host->bf;
echo=host->echo;
shell=host;
2009-05-03 00:18:08 +02:00
cmd=new CommandLine(name,cmd_line);
2009-05-02 23:43:00 +02:00
if (!DOS_OpenFile(name,128,&file_handle)) {
2009-05-02 23:03:37 +02:00
//TODO Come up with something better
E_Exit("SHELL:Can't open BatchFile");
}
};
BatchFile::~BatchFile() {
2009-05-02 23:20:05 +02:00
delete cmd;
2009-05-02 23:03:37 +02:00
DOS_CloseFile(file_handle);
shell->bf=prev;
shell->echo=echo;
}
bool BatchFile::ReadLine(char * line) {
2009-05-03 00:28:34 +02:00
Bit8u c=0;Bit16u n=1;
2009-05-02 23:03:37 +02:00
char temp[CMD_MAXLINE];
emptyline:
char * cmd_write=temp;
do {
n=1;
DOS_ReadFile(file_handle,&c,&n);
if (n>0) {
2009-05-03 00:18:08 +02:00
/* Why are we filtering this ?
* Exclusion list: tab for batch files
* escape for ansi
* backspace for alien odyssey */
if (c>31 || c==0x1b || c=='\t' || c==8)
2009-05-02 23:03:37 +02:00
*cmd_write++=c;
}
} while (c!='\n' && n);
2009-05-02 23:20:05 +02:00
*cmd_write=0;
if (!n && cmd_write==temp) {
2009-05-02 23:03:37 +02:00
delete this;
return false;
}
if (!strlen(temp)) goto emptyline;
if (temp[0]==':') goto emptyline;
/* Now parse the line read from the bat file for % stuff */
cmd_write=line;
char * cmd_read=temp;
char env_name[256];char * env_write;
while (*cmd_read) {
env_write=env_name;
if (*cmd_read=='%') {
2009-05-02 23:20:05 +02:00
cmd_read++;
2009-05-03 00:18:08 +02:00
if (cmd_read[0] == '%') {
2009-05-02 23:20:05 +02:00
cmd_read++;
*cmd_write++='%';
}
2009-05-03 00:18:08 +02:00
if (cmd_read[0] == '0') { /* Handle %0 */
const char *file_name = cmd->GetFileName();
cmd_read++;
strcpy(cmd_write,file_name);
cmd_write+=strlen(file_name);
continue;
}
size_t len=strspn(cmd_read,"123456789");
if (len) { /* Handle %1 %2 .. %9 */
2009-05-02 23:20:05 +02:00
memcpy(env_name,cmd_read,len);
env_name[len]=0;cmd_read+=len;
len=atoi(env_name);
if (cmd->GetCount()<len) continue;
std::string word;
if (!cmd->FindCommand(len,word)) continue;
strcpy(cmd_write,word.c_str());
cmd_write+=strlen(word.c_str());
continue;
} else {
/* Not a command line number has to be an environment */
char * first=strchr(cmd_read,'%');
if (!first) continue; *first++=0;
2009-05-03 00:37:32 +02:00
std::string env;
if (shell->GetEnvStr(cmd_read,env)) {
const char * equals=strchr(env.c_str(),'=');
2009-05-02 23:20:05 +02:00
if (!equals) continue;
equals++;
strcpy(cmd_write,equals);
cmd_write+=strlen(equals);
}
cmd_read=first;
}
2009-05-02 23:03:37 +02:00
} else {
*cmd_write++=*cmd_read++;
}
}
2009-05-02 23:20:05 +02:00
*cmd_write=0;
return true;
2009-05-02 23:03:37 +02:00
}
bool BatchFile::Goto(char * where) {
Bit32u pos=0;
2009-05-03 00:37:32 +02:00
char cmd_buffer[CMD_MAXLINE];
2009-05-02 23:03:37 +02:00
char * cmd_write;
DOS_SeekFile(file_handle,&pos,DOS_SEEK_SET);
2009-05-02 23:20:05 +02:00
/* Scan till we have a match or return false */
2009-05-02 23:03:37 +02:00
Bit8u c;Bit16u n;
again:
2009-05-03 00:37:32 +02:00
cmd_write=cmd_buffer;
2009-05-02 23:03:37 +02:00
do {
n=1;
DOS_ReadFile(file_handle,&c,&n);
if (n>0) {
if (c>31)
*cmd_write++=c;
}
} while (c!='\n' && n);
*cmd_write++=0;
2009-05-03 00:37:32 +02:00
char *nospace = trim(cmd_buffer);
2009-05-03 00:18:08 +02:00
if (nospace[0] == ':') {
char* nonospace = trim(nospace+1);
if (strcasecmp(nonospace,where)==0) return true;
2009-05-02 23:03:37 +02:00
}
if (!n) {
delete this;
return false;
}
goto again;
return false;
};
2009-05-03 00:28:34 +02:00
void BatchFile::Shift(void) {
cmd->Shift(1);
}