| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: shell_batch.cpp,v 1.36 2009-07-03 19:36:56 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "shell.h" |
| 25 | #include "support.h" |
| 26 | |
| 27 | BatchFile::BatchFile(DOS_Shell * host,char const * const resolved_name,char const * const entered_name, char const * const cmd_line) { |
| 28 | location = 0; |
| 29 | prev=host->bf; |
| 30 | echo=host->echo; |
| 31 | shell=host; |
| 32 | char totalname[DOS_PATHLENGTH+4]; |
| 33 | DOS_Canonicalize(resolved_name,totalname); // Get fullname including drive specificiation |
| 34 | cmd = new CommandLine(entered_name,cmd_line); |
| 35 | filename = totalname; |
| 36 | |
| 37 | //Test if file is openable |
| 38 | if (!DOS_OpenFile(totalname,(DOS_NOT_INHERIT|OPEN_READ),&file_handle)) { |
| 39 | //TODO Come up with something better |
| 40 | E_Exit("SHELL:Can't open BatchFile %s",totalname); |
| 41 | } |
| 42 | DOS_CloseFile(file_handle); |
| 43 | } |
| 44 | |
| 45 | BatchFile::~BatchFile() { |
| 46 | delete cmd; |
| 47 | shell->bf=prev; |
| 48 | shell->echo=echo; |
| 49 | } |
| 50 | |
| 51 | bool BatchFile::ReadLine(char * line) { |
| 52 | //Open the batchfile and seek to stored postion |
| 53 | if (!DOS_OpenFile(filename.c_str(),(DOS_NOT_INHERIT|OPEN_READ),&file_handle)) { |
| 54 | LOG(LOG_MISC,LOG_ERROR)("ReadLine Can't open BatchFile %s",filename.c_str()); |
| 55 | delete this; |
| 56 | return false; |
| 57 | } |
| 58 | DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_SET); |
| 59 | |
| 60 | Bit8u c=0;Bit16u n=1; |
| 61 | char temp[CMD_MAXLINE]; |
| 62 | emptyline: |
| 63 | char * cmd_write=temp; |
| 64 | do { |
| 65 | n=1; |
| 66 | DOS_ReadFile(file_handle,&c,&n); |
| 67 | if (n>0) { |
| 68 | /* Why are we filtering this ? |
| 69 | * Exclusion list: tab for batch files |
| 70 | * escape for ansi |
| 71 | * backspace for alien odyssey */ |
| 72 | if (c>31 || c==0x1b || c=='\t' || c==8) { |
| 73 | //Only add it if room for it (and trailing zero) in the buffer, but do the check here instead at the end |
| 74 | //So we continue reading till EOL/EOF |
| 75 | if (((cmd_write - temp) + 1) < (CMD_MAXLINE - 1)) |
| 76 | *cmd_write++ = c; |
| 77 | } |
| 78 | } |
| 79 | } while (c!='\n' && n); |
| 80 | *cmd_write=0; |
| 81 | if (!n && cmd_write==temp) { |
| 82 | //Close file and delete bat file |
| 83 | DOS_CloseFile(file_handle); |
| 84 | delete this; |
| 85 | return false; |
| 86 | } |
| 87 | if (!strlen(temp)) goto emptyline; |
| 88 | if (temp[0]==':') goto emptyline; |
| 89 | |
| 90 | /* Now parse the line read from the bat file for % stuff */ |
| 91 | cmd_write=line; |
| 92 | char * cmd_read=temp; |
| 93 | while (*cmd_read) { |
| 94 | if (*cmd_read == '%') { |
| 95 | cmd_read++; |
| 96 | if (cmd_read[0] == '%') { |
| 97 | cmd_read++; |
| 98 | if (((cmd_write - line) + 1) < (CMD_MAXLINE - 1)) |
| 99 | *cmd_write++ = '%'; |
| 100 | continue; |
| 101 | } |
| 102 | if (cmd_read[0] == '0') { /* Handle %0 */ |
| 103 | const char *file_name = cmd->GetFileName(); |
| 104 | cmd_read++; |
| 105 | size_t name_len = strlen(file_name); |
| 106 | if (((cmd_write - line) + name_len) < (CMD_MAXLINE - 1)) { |
| 107 | strcpy(cmd_write,file_name); |
| 108 | cmd_write += name_len; |
| 109 | } |
| 110 | continue; |
| 111 | } |
| 112 | char next = cmd_read[0]; |
| 113 | if(next > '0' && next <= '9') { |
| 114 | /* Handle %1 %2 .. %9 */ |
| 115 | cmd_read++; //Progress reader |
| 116 | next -= '0'; |
| 117 | if (cmd->GetCount()<(unsigned int)next) continue; |
| 118 | std::string word; |
| 119 | if (!cmd->FindCommand(next,word)) continue; |
| 120 | size_t name_len = strlen(word.c_str()); |
| 121 | if (((cmd_write - line) + name_len) < (CMD_MAXLINE - 1)) { |
| 122 | strcpy(cmd_write,word.c_str()); |
| 123 | cmd_write += name_len; |
| 124 | } |
| 125 | continue; |
| 126 | } else { |
| 127 | /* Not a command line number has to be an environment */ |
| 128 | char * first = strchr(cmd_read,'%'); |
| 129 | /* No env afterall. Ignore a single % */ |
| 130 | if (!first) {/* *cmd_write++ = '%';*/continue;} |
| 131 | *first++ = 0; |
| 132 | std::string env; |
| 133 | if (shell->GetEnvStr(cmd_read,env)) { |
| 134 | const char* equals = strchr(env.c_str(),'='); |
| 135 | if (!equals) continue; |
| 136 | equals++; |
| 137 | size_t name_len = strlen(equals); |
| 138 | if (((cmd_write - line) + name_len) < (CMD_MAXLINE - 1)) { |
| 139 | strcpy(cmd_write,equals); |
| 140 | cmd_write += name_len; |
| 141 | } |
| 142 | } |
| 143 | cmd_read = first; |
| 144 | } |
| 145 | } else { |
| 146 | if (((cmd_write - line) + 1) < (CMD_MAXLINE - 1)) |
| 147 | *cmd_write++ = *cmd_read++; |
| 148 | } |
| 149 | } |
| 150 | *cmd_write = 0; |
| 151 | //Store current location and close bat file |
| 152 | this->location = 0; |
| 153 | DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_CUR); |
| 154 | DOS_CloseFile(file_handle); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool BatchFile::Goto(char * where) { |
| 159 | //Open bat file and search for the where string |
| 160 | if (!DOS_OpenFile(filename.c_str(),(DOS_NOT_INHERIT|OPEN_READ),&file_handle)) { |
| 161 | LOG(LOG_MISC,LOG_ERROR)("SHELL:Goto Can't open BatchFile %s",filename.c_str()); |
| 162 | delete this; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | char cmd_buffer[CMD_MAXLINE]; |
| 167 | char * cmd_write; |
| 168 | |
| 169 | /* Scan till we have a match or return false */ |
| 170 | Bit8u c;Bit16u n; |
| 171 | again: |
| 172 | cmd_write=cmd_buffer; |
| 173 | do { |
| 174 | n=1; |
| 175 | DOS_ReadFile(file_handle,&c,&n); |
| 176 | if (n>0) { |
| 177 | if (c>31) { |
| 178 | if (((cmd_write - cmd_buffer) + 1) < (CMD_MAXLINE - 1)) |
| 179 | *cmd_write++ = c; |
| 180 | } |
| 181 | } |
| 182 | } while (c!='\n' && n); |
| 183 | *cmd_write++ = 0; |
| 184 | char *nospace = trim(cmd_buffer); |
| 185 | if (nospace[0] == ':') { |
| 186 | nospace++; //Skip : |
| 187 | //Strip spaces and = from it. |
| 188 | while(*nospace && (isspace(*reinterpret_cast<unsigned char*>(nospace)) || (*nospace == '='))) |
| 189 | nospace++; |
| 190 | |
| 191 | //label is until space/=/eol |
| 192 | char* const beginlabel = nospace; |
| 193 | while(*nospace && !isspace(*reinterpret_cast<unsigned char*>(nospace)) && (*nospace != '=')) |
| 194 | nospace++; |
| 195 | |
| 196 | *nospace = 0; |
| 197 | if (strcasecmp(beginlabel,where)==0) { |
| 198 | //Found it! Store location and continue |
| 199 | this->location = 0; |
| 200 | DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_CUR); |
| 201 | DOS_CloseFile(file_handle); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | if (!n) { |
| 207 | DOS_CloseFile(file_handle); |
| 208 | delete this; |
| 209 | return false; |
| 210 | } |
| 211 | goto again; |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | void BatchFile::Shift(void) { |
| 216 | cmd->Shift(1); |
| 217 | } |