DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/shell/shell_batch.cpp
00001 /*
00002  *  Copyright (C) 2002-2013  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 #include "shell.h"
00024 #include "support.h"
00025 
00026 BatchFile::BatchFile(DOS_Shell * host,char const * const resolved_name,char const * const entered_name, char const * const cmd_line) {
00027         location = 0;
00028         prev=host->bf;
00029         echo=host->echo;
00030         shell=host;
00031         char totalname[DOS_PATHLENGTH+4];
00032         DOS_Canonicalize(resolved_name,totalname); // Get fullname including drive specificiation
00033         cmd = new CommandLine(entered_name,cmd_line);
00034         filename = totalname;
00035 
00036         //Test if file is openable
00037         if (!DOS_OpenFile(totalname,128,&file_handle)) {
00038                 //TODO Come up with something better
00039                 E_Exit("SHELL:Can't open BatchFile %s",totalname);
00040         }
00041         DOS_CloseFile(file_handle);
00042 }
00043 
00044 BatchFile::~BatchFile() {
00045         delete cmd;
00046         shell->bf=prev;
00047         shell->echo=echo;
00048 }
00049 
00050 bool BatchFile::ReadLine(char * line) {
00051         //Open the batchfile and seek to stored postion
00052         if (!DOS_OpenFile(filename.c_str(),128,&file_handle)) {
00053                 LOG(LOG_MISC,LOG_ERROR)("ReadLine Can't open BatchFile %s",filename.c_str());
00054                 delete this;
00055                 return false;
00056         }
00057         DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_SET);
00058 
00059         Bit8u c=0;Bit16u n=1;
00060         char temp[CMD_MAXLINE];
00061 emptyline:
00062         char * cmd_write=temp;
00063         do {
00064                 n=1;
00065                 DOS_ReadFile(file_handle,&c,&n);
00066                 if (n>0) {
00067                         /* Why are we filtering this ?
00068                          * Exclusion list: tab for batch files 
00069                          * escape for ansi
00070                          * backspace for alien odyssey */
00071                         if (c>31 || c==0x1b || c=='\t' || c==8)
00072                                 *cmd_write++=(char)c;
00073                 }
00074         } while (c!='\n' && n);
00075         *cmd_write=0;
00076         if (!n && cmd_write==temp) {
00077                 //Close file and delete bat file
00078                 DOS_CloseFile(file_handle);
00079                 delete this;
00080                 return false;   
00081         }
00082         if (!strlen(temp)) goto emptyline;
00083         if (temp[0]==':') goto emptyline;
00084 
00085         /* Now parse the line read from the bat file for % stuff */
00086         cmd_write=line;
00087         char * cmd_read=temp;
00088         while (*cmd_read) {
00089                 if (*cmd_read=='%') {
00090                         cmd_read++;
00091                         if (cmd_read[0] == '%') {
00092                                 cmd_read++;
00093                                 *cmd_write++='%';
00094                                 continue;
00095                         }
00096                         if (cmd_read[0] == '0') {  /* Handle %0 */
00097                                 const char *file_name = cmd->GetFileName();
00098                                 cmd_read++;
00099                                 strcpy(cmd_write,file_name);
00100                                 cmd_write+=strlen(file_name);
00101                                 continue;
00102                         }
00103                         char next = cmd_read[0];
00104                         if(next > '0' && next <= '9') {  
00105                                 /* Handle %1 %2 .. %9 */
00106                                 cmd_read++; //Progress reader
00107                                 next -= '0';
00108                                 if (cmd->GetCount()<(unsigned int)next) continue;
00109                                 std::string word;
00110                                 if (!cmd->FindCommand((unsigned int)next,word)) continue;
00111                                 strcpy(cmd_write,word.c_str());
00112                                 cmd_write+=strlen(word.c_str());
00113                                 continue;
00114                         } else {
00115                                 /* Not a command line number has to be an environment */
00116                                 char * first=strchr(cmd_read,'%');
00117                                 /* No env afterall.Somewhat of a hack though as %% and % aren't handled consistent in dosbox. Maybe echo needs to parse % and %% as well. */
00118                                 if (!first) {*cmd_write++ = '%';continue;}
00119                                 *first++ = 0;
00120                                 std::string env;
00121                                 if (shell->GetEnvStr(cmd_read,env)) {
00122                                         const char * equals=strchr(env.c_str(),'=');
00123                                         if (!equals) continue;
00124                                         equals++;
00125                                         strcpy(cmd_write,equals);
00126                                         cmd_write+=strlen(equals);
00127                                 }
00128                                 cmd_read=first;
00129                         }
00130                 } else {
00131                         *cmd_write++=*cmd_read++;
00132                 }
00133         }
00134         *cmd_write=0;
00135         //Store current location and close bat file
00136         this->location = 0;
00137         DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_CUR);
00138         DOS_CloseFile(file_handle);
00139         return true;    
00140 }
00141 
00142 bool BatchFile::Goto(char * where) {
00143         //Open bat file and search for the where string
00144         if (!DOS_OpenFile(filename.c_str(),128,&file_handle)) {
00145                 LOG(LOG_MISC,LOG_ERROR)("SHELL:Goto Can't open BatchFile %s",filename.c_str());
00146                 delete this;
00147                 return false;
00148         }
00149 
00150         char cmd_buffer[CMD_MAXLINE];
00151         char * cmd_write;
00152 
00153         /* Scan till we have a match or return false */
00154         Bit8u c;Bit16u n;
00155 again:
00156         cmd_write=cmd_buffer;
00157         do {
00158                 n=1;
00159                 DOS_ReadFile(file_handle,&c,&n);
00160                 if (n>0) {
00161                         if (c>31)
00162                                 *cmd_write++=(char)c;
00163                 }
00164         } while (c!='\n' && n);
00165         *cmd_write++ = 0;
00166         char *nospace = trim(cmd_buffer);
00167         if (nospace[0] == ':') {
00168                 nospace++; //Skip :
00169                 //Strip spaces and = from it.
00170                 while(*nospace && (isspace(*reinterpret_cast<unsigned char*>(nospace)) || (*nospace == '=')))
00171                         nospace++;
00172 
00173                 //label is until space/=/eol
00174                 char* const beginlabel = nospace;
00175                 while(*nospace && !isspace(*reinterpret_cast<unsigned char*>(nospace)) && (*nospace != '=')) 
00176                         nospace++;
00177 
00178                 *nospace = 0;
00179                 if (strcasecmp(beginlabel,where)==0) {
00180                 //Found it! Store location and continue
00181                         this->location = 0;
00182                         DOS_SeekFile(file_handle,&(this->location),DOS_SEEK_CUR);
00183                         DOS_CloseFile(file_handle);
00184                         return true;
00185                 }
00186            
00187         }
00188         if (!n) {
00189                 DOS_CloseFile(file_handle);
00190                 delete this;
00191                 return false;   
00192         }
00193         goto again;
00194         return false;
00195 }
00196 
00197 void BatchFile::Shift(void) {
00198         cmd->Shift(1);
00199 }