| 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: programs.cpp,v 1.37 2009-05-27 09:15:42 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <ctype.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include "programs.h" |
| 28 | #include "callback.h" |
| 29 | #include "regs.h" |
| 30 | #include "support.h" |
| 31 | #include "cross.h" |
| 32 | #include "control.h" |
| 33 | #include "shell.h" |
| 34 | |
| 35 | Bitu call_program; |
| 36 | |
| 37 | /* This registers a file on the virtual drive and creates the correct structure for it*/ |
| 38 | |
| 39 | static Bit8u exe_block[]={ |
| 40 | 0xbc,0x00,0x04, //MOV SP,0x400 decrease stack size |
| 41 | 0xbb,0x40,0x00, //MOV BX,0x040 for memory resize |
| 42 | 0xb4,0x4a, //MOV AH,0x4A Resize memory block |
| 43 | 0xcd,0x21, //INT 0x21 |
| 44 | //pos 12 is callback number |
| 45 | 0xFE,0x38,0x00,0x00, //CALLBack number |
| 46 | 0xb8,0x00,0x4c, //Mov ax,4c00 |
| 47 | 0xcd,0x21, //INT 0x21 |
| 48 | }; |
| 49 | |
| 50 | #define CB_POS 12 |
| 51 | |
| 52 | static std::vector<PROGRAMS_Main*> internal_progs; |
| 53 | |
| 54 | void PROGRAMS_MakeFile(char const * const name,PROGRAMS_Main * main) { |
| 55 | Bit8u * comdata=(Bit8u *)malloc(32); //MEM LEAK |
| 56 | memcpy(comdata,&exe_block,sizeof(exe_block)); |
| 57 | comdata[CB_POS]=(Bit8u)(call_program&0xff); |
| 58 | comdata[CB_POS+1]=(Bit8u)((call_program>>8)&0xff); |
| 59 | |
| 60 | /* Copy save the pointer in the vector and save it's index */ |
| 61 | if (internal_progs.size()>255) E_Exit("PROGRAMS_MakeFile program size too large (%d)",static_cast<int>(internal_progs.size())); |
| 62 | Bit8u index = (Bit8u)internal_progs.size(); |
| 63 | internal_progs.push_back(main); |
| 64 | |
| 65 | memcpy(&comdata[sizeof(exe_block)],&index,sizeof(index)); |
| 66 | Bit32u size=sizeof(exe_block)+sizeof(index); |
| 67 | VFILE_Register(name,comdata,size); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | static Bitu PROGRAMS_Handler(void) { |
| 73 | /* This sets up everything for a program start up call */ |
| 74 | Bitu size=sizeof(Bit8u); |
| 75 | Bit8u index; |
| 76 | /* Read the index from program code in memory */ |
| 77 | PhysPt reader=PhysMake(dos.psp(),256+sizeof(exe_block)); |
| 78 | HostPt writer=(HostPt)&index; |
| 79 | for (;size>0;size--) *writer++=mem_readb(reader++); |
| 80 | Program * new_program; |
| 81 | if(index >= internal_progs.size()) E_Exit("something is messing with the memory"); |
| 82 | PROGRAMS_Main * handler = internal_progs[index]; |
| 83 | (*handler)(&new_program); |
| 84 | new_program->Run(); |
| 85 | delete new_program; |
| 86 | return CBRET_NONE; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* Main functions used in all program */ |
| 91 | |
| 92 | |
| 93 | Program::Program() { |
| 94 | /* Find the command line and setup the PSP */ |
| 95 | psp = new DOS_PSP(dos.psp()); |
| 96 | /* Scan environment for filename */ |
| 97 | PhysPt envscan=PhysMake(psp->GetEnvironment(),0); |
| 98 | while (mem_readb(envscan)) envscan+=mem_strlen(envscan)+1; |
| 99 | envscan+=3; |
| 100 | CommandTail tail; |
| 101 | MEM_BlockRead(PhysMake(dos.psp(),128),&tail,128); |
| 102 | if (tail.count<127) tail.buffer[tail.count]=0; |
| 103 | else tail.buffer[126]=0; |
| 104 | char filename[256+1]; |
| 105 | MEM_StrCopy(envscan,filename,256); |
| 106 | cmd = new CommandLine(filename,tail.buffer); |
| 107 | } |
| 108 | |
| 109 | extern std::string full_arguments; |
| 110 | |
| 111 | void Program::ChangeToLongCmd() { |
| 112 | /* |
| 113 | * Get arguments directly from the shell instead of the psp. |
| 114 | * this is done in securemode: (as then the arguments to mount and friends |
| 115 | * can only be given on the shell ( so no int 21 4b) |
| 116 | * Securemode part is disabled as each of the internal command has already |
| 117 | * protection for it. (and it breaks games like cdman) |
| 118 | * it is also done for long arguments to as it is convient (as the total commandline can be longer then 127 characters. |
| 119 | * imgmount with lot's of parameters |
| 120 | * Length of arguments can be ~120. but switch when above 100 to be sure |
| 121 | */ |
| 122 | |
| 123 | if(/*control->SecureMode() ||*/ cmd->Get_arglength() > 100) { |
| 124 | CommandLine* temp = new CommandLine(cmd->GetFileName(),full_arguments.c_str()); |
| 125 | delete cmd; |
| 126 | cmd = temp; |
| 127 | } |
| 128 | full_arguments.assign(""); //Clear so it gets even more save |
| 129 | } |
| 130 | |
| 131 | void Program::WriteOut(const char * format,...) { |
| 132 | char buf[2048]; |
| 133 | va_list msg; |
| 134 | |
| 135 | va_start(msg,format); |
| 136 | vsnprintf(buf,2047,format,msg); |
| 137 | va_end(msg); |
| 138 | |
| 139 | Bit16u size = (Bit16u)strlen(buf); |
| 140 | for(Bit16u i = 0; i < size;i++) { |
| 141 | Bit8u out;Bit16u s=1; |
| 142 | if(buf[i] == 0xA && i > 0 && buf[i-1] !=0xD) { |
| 143 | out = 0xD;DOS_WriteFile(STDOUT,&out,&s); |
| 144 | } |
| 145 | out = buf[i]; |
| 146 | DOS_WriteFile(STDOUT,&out,&s); |
| 147 | } |
| 148 | |
| 149 | // DOS_WriteFile(STDOUT,(Bit8u *)buf,&size); |
| 150 | } |
| 151 | |
| 152 | void Program::WriteOut_NoParsing(const char * format) { |
| 153 | Bit16u size = (Bit16u)strlen(format); |
| 154 | char const* buf = format; |
| 155 | for(Bit16u i = 0; i < size;i++) { |
| 156 | Bit8u out;Bit16u s=1; |
| 157 | if(buf[i] == 0xA && i > 0 && buf[i-1] !=0xD) { |
| 158 | out = 0xD;DOS_WriteFile(STDOUT,&out,&s); |
| 159 | } |
| 160 | out = buf[i]; |
| 161 | DOS_WriteFile(STDOUT,&out,&s); |
| 162 | } |
| 163 | |
| 164 | // DOS_WriteFile(STDOUT,(Bit8u *)format,&size); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | bool Program::GetEnvStr(const char * entry,std::string & result) { |
| 169 | /* Walk through the internal environment and see for a match */ |
| 170 | PhysPt env_read=PhysMake(psp->GetEnvironment(),0); |
| 171 | |
| 172 | char env_string[1024+1]; |
| 173 | result.erase(); |
| 174 | if (!entry[0]) return false; |
| 175 | do { |
| 176 | MEM_StrCopy(env_read,env_string,1024); |
| 177 | if (!env_string[0]) return false; |
| 178 | env_read += (PhysPt)(strlen(env_string)+1); |
| 179 | char* equal = strchr(env_string,'='); |
| 180 | if (!equal) continue; |
| 181 | /* replace the = with \0 to get the length */ |
| 182 | *equal = 0; |
| 183 | if (strlen(env_string) != strlen(entry)) continue; |
| 184 | if (strcasecmp(entry,env_string)!=0) continue; |
| 185 | /* restore the = to get the original result */ |
| 186 | *equal = '='; |
| 187 | result = env_string; |
| 188 | return true; |
| 189 | } while (1); |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | bool Program::GetEnvNum(Bitu num,std::string & result) { |
| 194 | char env_string[1024+1]; |
| 195 | PhysPt env_read=PhysMake(psp->GetEnvironment(),0); |
| 196 | do { |
| 197 | MEM_StrCopy(env_read,env_string,1024); |
| 198 | if (!env_string[0]) break; |
| 199 | if (!num) { result=env_string;return true;} |
| 200 | env_read += (PhysPt)(strlen(env_string)+1); |
| 201 | num--; |
| 202 | } while (1); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | Bitu Program::GetEnvCount(void) { |
| 207 | PhysPt env_read=PhysMake(psp->GetEnvironment(),0); |
| 208 | Bitu num=0; |
| 209 | while (mem_readb(env_read)!=0) { |
| 210 | for (;mem_readb(env_read);env_read++) {}; |
| 211 | env_read++; |
| 212 | num++; |
| 213 | } |
| 214 | return num; |
| 215 | } |
| 216 | |
| 217 | bool Program::SetEnv(const char * entry,const char * new_string) { |
| 218 | PhysPt env_read = PhysMake(psp->GetEnvironment(),0); |
| 219 | |
| 220 | //Get size of environment. |
| 221 | DOS_MCB mcb(psp->GetEnvironment()-1); |
| 222 | Bit16u envsize = mcb.GetSize()*16; |
| 223 | |
| 224 | |
| 225 | PhysPt env_write = env_read; |
| 226 | PhysPt env_write_start = env_read; |
| 227 | char env_string[1024+1] = { 0 }; |
| 228 | do { |
| 229 | MEM_StrCopy(env_read,env_string,1024); |
| 230 | if (!env_string[0]) break; |
| 231 | env_read += (PhysPt)(strlen(env_string)+1); |
| 232 | if (!strchr(env_string,'=')) continue; /* Remove corrupt entry? */ |
| 233 | if ((strncasecmp(entry,env_string,strlen(entry))==0) && |
| 234 | env_string[strlen(entry)]=='=') continue; |
| 235 | MEM_BlockWrite(env_write,env_string,(Bitu)(strlen(env_string)+1)); |
| 236 | env_write += (PhysPt)(strlen(env_string)+1); |
| 237 | } while (1); |
| 238 | /* TODO Maybe save the program name sometime. not really needed though */ |
| 239 | /* Save the new entry */ |
| 240 | |
| 241 | //ensure room (0.74-3 5 because of the writed) |
| 242 | if (envsize <= (env_write-env_write_start) + strlen(entry) + 1 + strlen(new_string) + 5) return false; |
| 243 | |
| 244 | if (new_string[0]) { |
| 245 | std::string bigentry(entry); |
| 246 | for (std::string::iterator it = bigentry.begin(); it != bigentry.end(); ++it) *it = toupper(*it); |
| 247 | snprintf(env_string,1024+1,"%s=%s",bigentry.c_str(),new_string); |
| 248 | MEM_BlockWrite(env_write,env_string,(Bitu)(strlen(env_string)+1)); |
| 249 | env_write += (PhysPt)(strlen(env_string)+1); |
| 250 | } |
| 251 | /* Clear out the final piece of the environment */ |
| 252 | mem_writed(env_write,0); |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | class CONFIG : public Program { |
| 257 | public: |
| 258 | void Run(void); |
| 259 | }; |
| 260 | |
| 261 | void MSG_Write(const char *); |
| 262 | |
| 263 | void CONFIG::Run(void) { |
| 264 | FILE * f; |
| 265 | if (cmd->FindString("-writeconf",temp_line,true) |
| 266 | || cmd->FindString("-wc",temp_line,true)) { |
| 267 | /* In secure mode don't allow a new configfile to be created */ |
| 268 | if(control->SecureMode()) { |
| 269 | WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW")); |
| 270 | return; |
| 271 | } |
| 272 | f=fopen(temp_line.c_str(),"wb+"); |
| 273 | if (!f) { |
| 274 | WriteOut(MSG_Get("PROGRAM_CONFIG_FILE_ERROR"),temp_line.c_str()); |
| 275 | return; |
| 276 | } |
| 277 | fclose(f); |
| 278 | control->PrintConfig(temp_line.c_str()); |
| 279 | return; |
| 280 | } |
| 281 | if (cmd->FindString("-writelang",temp_line,true) |
| 282 | ||cmd->FindString("-wl",temp_line,true)) { |
| 283 | /* In secure mode don't allow a new languagefile to be created |
| 284 | * Who knows which kind of file we would overwriting. */ |
| 285 | if(control->SecureMode()) { |
| 286 | WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW")); |
| 287 | return; |
| 288 | } |
| 289 | f=fopen(temp_line.c_str(),"wb+"); |
| 290 | if (!f) { |
| 291 | WriteOut(MSG_Get("PROGRAM_CONFIG_FILE_ERROR"),temp_line.c_str()); |
| 292 | return; |
| 293 | } |
| 294 | fclose(f); |
| 295 | MSG_Write(temp_line.c_str()); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | /* Code for switching to secure mode */ |
| 300 | if(cmd->FindExist("-securemode",true)) { |
| 301 | control->SwitchToSecureMode(); |
| 302 | WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_ON")); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | /* Code for getting the current configuration. * |
| 307 | * Official format: config -get "section property" * |
| 308 | * As a bonus it will set %CONFIG% to this value as well */ |
| 309 | if(cmd->FindString("-get",temp_line,true)) { |
| 310 | std::string temp2 = ""; |
| 311 | cmd->GetStringRemain(temp2);//So -get n1 n2= can be used without quotes |
| 312 | if(temp2 != "") temp_line = temp_line + " " + temp2; |
| 313 | |
| 314 | std::string::size_type space = temp_line.find(" "); |
| 315 | if(space == std::string::npos) { |
| 316 | WriteOut(MSG_Get("PROGRAM_CONFIG_GET_SYNTAX")); |
| 317 | return; |
| 318 | } |
| 319 | //Copy the found property to a new string and erase from templine (mind the space) |
| 320 | std::string prop = temp_line.substr(space+1); temp_line.erase(space); |
| 321 | |
| 322 | Section* sec = control->GetSection(temp_line.c_str()); |
| 323 | if(!sec) { |
| 324 | WriteOut(MSG_Get("PROGRAM_CONFIG_SECTION_ERROR"),temp_line.c_str()); |
| 325 | return; |
| 326 | } |
| 327 | std::string val = sec->GetPropValue(prop.c_str()); |
| 328 | if(val == NO_SUCH_PROPERTY) { |
| 329 | WriteOut(MSG_Get("PROGRAM_CONFIG_NO_PROPERTY"),prop.c_str(),temp_line.c_str()); |
| 330 | return; |
| 331 | } |
| 332 | WriteOut("%s",val.c_str()); |
| 333 | first_shell->SetEnv("CONFIG",val.c_str()); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | |
| 339 | /* Code for the configuration changes * |
| 340 | * Official format: config -set "section property=value" * |
| 341 | * Accepted: without quotes and/or without -set and/or without section * |
| 342 | * and/or the "=" replaced by a " " */ |
| 343 | |
| 344 | if (cmd->FindString("-set",temp_line,true)) { //get all arguments |
| 345 | std::string temp2 = ""; |
| 346 | cmd->GetStringRemain(temp2);//So -set n1 n2=n3 can be used without quotes |
| 347 | if(temp2!="") temp_line = temp_line + " " + temp2; |
| 348 | } else if(!cmd->GetStringRemain(temp_line)) {//no set |
| 349 | WriteOut(MSG_Get("PROGRAM_CONFIG_USAGE")); //and no arguments specified |
| 350 | return; |
| 351 | }; |
| 352 | //Wanted input: n1 n2=n3 |
| 353 | char copy[1024]; |
| 354 | strcpy(copy,temp_line.c_str()); |
| 355 | //seperate section from property |
| 356 | const char* temp = strchr(copy,' '); |
| 357 | if((temp && *temp) || (temp=strchr(copy,'=')) ) copy[temp++ - copy]= 0; |
| 358 | else { |
| 359 | WriteOut(MSG_Get("PROGRAM_CONFIG_USAGE")); |
| 360 | return; |
| 361 | } |
| 362 | //if n1 n2 n3 then replace last space with = |
| 363 | const char* sign = strchr(temp,'='); |
| 364 | if(!sign) { |
| 365 | sign = strchr(temp,' '); |
| 366 | if(sign) { |
| 367 | copy[sign - copy] = '='; |
| 368 | } else { |
| 369 | //2 items specified (no space nor = between n2 and n3 |
| 370 | //assume that they posted: property value |
| 371 | //Try to determine the section. |
| 372 | Section* sec=control->GetSectionFromProperty(copy); |
| 373 | if(!sec){ |
| 374 | if(control->GetSectionFromProperty(temp)) return; //Weird situation:ignore |
| 375 | WriteOut(MSG_Get("PROGRAM_CONFIG_PROPERTY_ERROR"),copy); |
| 376 | return; |
| 377 | } //Hack to allow config ems true |
| 378 | char buffer[1024];strcpy(buffer,copy);strcat(buffer,"=");strcat(buffer,temp); |
| 379 | sign = strchr(buffer,' '); |
| 380 | if(sign) buffer[sign - buffer] = '='; |
| 381 | strcpy(copy,sec->GetName()); |
| 382 | temp = buffer; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* Input processed. Now the real job starts |
| 387 | * copy contains the likely "sectionname" |
| 388 | * temp contains "property=value" |
| 389 | * the section is destroyed and a new input line is given to |
| 390 | * the configuration parser. Then the section is restarted. |
| 391 | */ |
| 392 | char* inputline = const_cast<char*>(temp); |
| 393 | Section* sec = 0; |
| 394 | sec = control->GetSection(copy); |
| 395 | if(!sec) { WriteOut(MSG_Get("PROGRAM_CONFIG_SECTION_ERROR"),copy);return;} |
| 396 | sec->ExecuteDestroy(false); |
| 397 | sec->HandleInputline(inputline); |
| 398 | sec->ExecuteInit(false); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static void CONFIG_ProgramStart(Program * * make) { |
| 404 | *make=new CONFIG; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | void PROGRAMS_Init(Section* /*sec*/) { |
| 409 | /* Setup a special callback to start virtual programs */ |
| 410 | call_program=CALLBACK_Allocate(); |
| 411 | CALLBACK_Setup(call_program,&PROGRAMS_Handler,CB_RETF,"internal program"); |
| 412 | PROGRAMS_MakeFile("CONFIG.COM",CONFIG_ProgramStart); |
| 413 | |
| 414 | MSG_Add("PROGRAM_CONFIG_FILE_ERROR","Can't open file %s\n"); |
| 415 | MSG_Add("PROGRAM_CONFIG_USAGE","Config tool:\nUse -writeconf filename to write the current config.\nUse -writelang filename to write the current language strings.\n"); |
| 416 | MSG_Add("PROGRAM_CONFIG_SECURE_ON","Switched to secure mode.\n"); |
| 417 | MSG_Add("PROGRAM_CONFIG_SECURE_DISALLOW","This operation is not permitted in secure mode.\n"); |
| 418 | MSG_Add("PROGRAM_CONFIG_SECTION_ERROR","Section %s doesn't exist.\n"); |
| 419 | MSG_Add("PROGRAM_CONFIG_PROPERTY_ERROR","No such section or property.\n"); |
| 420 | MSG_Add("PROGRAM_CONFIG_NO_PROPERTY","There is no property %s in section %s.\n"); |
| 421 | MSG_Add("PROGRAM_CONFIG_GET_SYNTAX","Correct syntax: config -get \"section property\".\n"); |
| 422 | } |