Debian Codegraph @

src/shell/shell_cmds.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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_cmds.cpp,v 1.93 2009-09-21 21:04:25 h-a-l-9000 Exp $ */
20
21 #include "dosbox.h"
22 #include "shell.h"
23 #include "callback.h"
24 #include "regs.h"
25 #include "../dos/drives.h"
26 #include "support.h"
27 #include "control.h"
28 #include <cstring>
29 #include <cctype>
30 #include <cstdlib>
31 #include <vector>
32 #include <string>
33
34 static SHELL_Cmd cmd_list[]={
35 { "DIR", 0, &DOS_Shell::CMD_DIR, "SHELL_CMD_DIR_HELP"},
36 { "CHDIR", 1, &DOS_Shell::CMD_CHDIR, "SHELL_CMD_CHDIR_HELP"},
37 { "ATTRIB", 1, &DOS_Shell::CMD_ATTRIB, "SHELL_CMD_ATTRIB_HELP"},
38 { "CALL", 1, &DOS_Shell::CMD_CALL, "SHELL_CMD_CALL_HELP"},
39 { "CD", 0, &DOS_Shell::CMD_CHDIR, "SHELL_CMD_CHDIR_HELP"},
40 { "CHOICE", 1, &DOS_Shell::CMD_CHOICE, "SHELL_CMD_CHOICE_HELP"},
41 { "CLS", 0, &DOS_Shell::CMD_CLS, "SHELL_CMD_CLS_HELP"},
42 { "COPY", 0, &DOS_Shell::CMD_COPY, "SHELL_CMD_COPY_HELP"},
43 { "DEL", 0, &DOS_Shell::CMD_DELETE, "SHELL_CMD_DELETE_HELP"},
44 { "DELETE", 1, &DOS_Shell::CMD_DELETE, "SHELL_CMD_DELETE_HELP"},
45 { "ERASE", 1, &DOS_Shell::CMD_DELETE, "SHELL_CMD_DELETE_HELP"},
46 { "ECHO", 1, &DOS_Shell::CMD_ECHO, "SHELL_CMD_ECHO_HELP"},
47 { "EXIT", 0, &DOS_Shell::CMD_EXIT, "SHELL_CMD_EXIT_HELP"},
48 { "GOTO", 1, &DOS_Shell::CMD_GOTO, "SHELL_CMD_GOTO_HELP"},
49 { "HELP", 1, &DOS_Shell::CMD_HELP, "SHELL_CMD_HELP_HELP"},
50 { "IF", 1, &DOS_Shell::CMD_IF, "SHELL_CMD_IF_HELP"},
51 { "LOADHIGH", 1, &DOS_Shell::CMD_LOADHIGH, "SHELL_CMD_LOADHIGH_HELP"},
52 { "LH", 1, &DOS_Shell::CMD_LOADHIGH, "SHELL_CMD_LOADHIGH_HELP"},
53 { "MKDIR", 1, &DOS_Shell::CMD_MKDIR, "SHELL_CMD_MKDIR_HELP"},
54 { "MD", 0, &DOS_Shell::CMD_MKDIR, "SHELL_CMD_MKDIR_HELP"},
55 { "PATH", 1, &DOS_Shell::CMD_PATH, "SHELL_CMD_PATH_HELP"},
56 { "PAUSE", 1, &DOS_Shell::CMD_PAUSE, "SHELL_CMD_PAUSE_HELP"},
57 { "RMDIR", 1, &DOS_Shell::CMD_RMDIR, "SHELL_CMD_RMDIR_HELP"},
58 { "RD", 0, &DOS_Shell::CMD_RMDIR, "SHELL_CMD_RMDIR_HELP"},
59 { "REM", 1, &DOS_Shell::CMD_REM, "SHELL_CMD_REM_HELP"},
60 { "RENAME", 1, &DOS_Shell::CMD_RENAME, "SHELL_CMD_RENAME_HELP"},
61 { "REN", 0, &DOS_Shell::CMD_RENAME, "SHELL_CMD_RENAME_HELP"},
62 { "SET", 1, &DOS_Shell::CMD_SET, "SHELL_CMD_SET_HELP"},
63 { "SHIFT", 1, &DOS_Shell::CMD_SHIFT, "SHELL_CMD_SHIFT_HELP"},
64 { "SUBST", 1, &DOS_Shell::CMD_SUBST, "SHELL_CMD_SUBST_HELP"},
65 { "TYPE", 0, &DOS_Shell::CMD_TYPE, "SHELL_CMD_TYPE_HELP"},
66 { "VER", 0, &DOS_Shell::CMD_VER, "SHELL_CMD_VER_HELP"},
67 {0,0,0,0}
68 };
69
70 /* support functions */
71 static char empty_char = 0;
72 static char* empty_string = &empty_char;
73 static void StripSpaces(char*&args) {
74 while(args && *args && isspace(*reinterpret_cast<unsigned char*>(args)))
75 args++;
76 }
77
78 static void StripSpaces(char*&args,char also) {
79 while(args && *args && (isspace(*reinterpret_cast<unsigned char*>(args)) || (*args == also)))
80 args++;
81 }
82
83 static char* ExpandDot(char*args, char* buffer , size_t bufsize) {
84 if(*args == '.') {
85 if(*(args+1) == 0){
86 safe_strncpy(buffer, "*.*", bufsize);
87 return buffer;
88 }
89 if( (*(args+1) != '.') && (*(args+1) != '\\') ) {
90 buffer[0] = '*';
91 buffer[1] = 0;
92 if (bufsize > 2) strncat(buffer,args,bufsize - 1 /*used buffer portion*/ - 1 /*trailing zero*/ );
93 return buffer;
94 } else
95 safe_strncpy (buffer, args, bufsize);
96 }
97 else safe_strncpy(buffer,args, bufsize);
98 return buffer;
99 }
100
101
102
103 bool DOS_Shell::CheckConfig(char* cmd_in,char*line) {
104 Section* test = control->GetSectionFromProperty(cmd_in);
105 if(!test) return false;
106 if(line && !line[0]) {
107 std::string val = test->GetPropValue(cmd_in);
108 if(val != NO_SUCH_PROPERTY) WriteOut("%s\n",val.c_str());
109 return true;
110 }
111 char newcom[1024]; newcom[0] = 0; strcpy(newcom,"z:\\config ");
112 strcat(newcom,test->GetName()); strcat(newcom," ");
113 strcat(newcom,cmd_in);strcat(newcom,line);
114 DoCommand(newcom);
115 return true;
116 }
117
118 void DOS_Shell::DoCommand(char * line) {
119 /* First split the line into command and arguments */
120 line=trim(line);
121 char cmd_buffer[CMD_MAXLINE];
122 char * cmd_write=cmd_buffer;
123 while (*line) {
124 if (*line==32) break;
125 if (*line=='/') break;
126 if (*line=='\t') break;
127 if (*line=='=') break;
128 if ((*line=='.') ||(*line =='\\')) { //allow stuff like cd.. and dir.exe cd\kees
129 *cmd_write=0;
130 Bit32u cmd_index=0;
131 while (cmd_list[cmd_index].name) {
132 if (strcasecmp(cmd_list[cmd_index].name,cmd_buffer)==0) {
133 (this->*(cmd_list[cmd_index].handler))(line);
134 return;
135 }
136 cmd_index++;
137 }
138 }
139 *cmd_write++=*line++;
140 }
141 *cmd_write=0;
142 if (strlen(cmd_buffer)==0) return;
143 /* Check the internal list */
144 Bit32u cmd_index=0;
145 while (cmd_list[cmd_index].name) {
146 if (strcasecmp(cmd_list[cmd_index].name,cmd_buffer)==0) {
147 (this->*(cmd_list[cmd_index].handler))(line);
148 return;
149 }
150 cmd_index++;
151 }
152 /* This isn't an internal command execute it */
153 if(Execute(cmd_buffer,line)) return;
154 if(CheckConfig(cmd_buffer,line)) return;
155 WriteOut(MSG_Get("SHELL_EXECUTE_ILLEGAL_COMMAND"),cmd_buffer);
156 }
157
158 #define HELP(command) \
159 if (ScanCMDBool(args,"?")) { \
160 WriteOut(MSG_Get("SHELL_CMD_" command "_HELP")); \
161 const char* long_m = MSG_Get("SHELL_CMD_" command "_HELP_LONG"); \
162 WriteOut("\n"); \
163 if(strcmp("Message not Found!\n",long_m)) WriteOut(long_m); \
164 else WriteOut(command "\n"); \
165 return; \
166 }
167
168 void DOS_Shell::CMD_CLS(char * args) {
169 HELP("CLS");
170 reg_ax=0x0003;
171 CALLBACK_RunRealInt(0x10);
172 }
173
174 void DOS_Shell::CMD_DELETE(char * args) {
175 HELP("DELETE");
176 /* Command uses dta so set it to our internal dta */
177 RealPt save_dta=dos.dta();
178 dos.dta(dos.tables.tempdta);
179
180 char * rem=ScanCMDRemain(args);
181 if (rem) {
182 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
183 return;
184 }
185 /* If delete accept switches mind the space infront of them. See the dir /p code */
186
187 char full[DOS_PATHLENGTH];
188 char buffer[CROSS_LEN];
189 args = ExpandDot(args,buffer, CROSS_LEN);
190 StripSpaces(args);
191 if (!DOS_Canonicalize(args,full)) { WriteOut(MSG_Get("SHELL_ILLEGAL_PATH"));return; }
192 //TODO Maybe support confirmation for *.* like dos does.
193 bool res=DOS_FindFirst(args,0xffff & ~DOS_ATTR_VOLUME);
194 if (!res) {
195 WriteOut(MSG_Get("SHELL_CMD_DEL_ERROR"),args);
196 dos.dta(save_dta);
197 return;
198 }
199 //end can't be 0, but if it is we'll get a nice crash, who cares :)
200 char * end=strrchr(full,'\\')+1;*end=0;
201 char name[DOS_NAMELENGTH_ASCII];Bit32u size;Bit16u time,date;Bit8u attr;
202 DOS_DTA dta(dos.dta());
203 while (res) {
204 dta.GetResult(name,size,date,time,attr);
205 if (!(attr & (DOS_ATTR_DIRECTORY|DOS_ATTR_READ_ONLY))) {
206 strcpy(end,name);
207 if (!DOS_UnlinkFile(full)) WriteOut(MSG_Get("SHELL_CMD_DEL_ERROR"),full);
208 }
209 res=DOS_FindNext();
210 }
211 dos.dta(save_dta);
212 }
213
214 void DOS_Shell::CMD_HELP(char * args){
215 HELP("HELP");
216 bool optall=ScanCMDBool(args,"ALL");
217 /* Print the help */
218 if(!optall) WriteOut(MSG_Get("SHELL_CMD_HELP"));
219 Bit32u cmd_index=0,write_count=0;
220 while (cmd_list[cmd_index].name) {
221 if (optall || !cmd_list[cmd_index].flags) {
222 WriteOut("<\033[34;1m%-8s\033[0m> %s",cmd_list[cmd_index].name,MSG_Get(cmd_list[cmd_index].help));
223 if(!(++write_count%22)) CMD_PAUSE(empty_string);
224 }
225 cmd_index++;
226 }
227 }
228
229 void DOS_Shell::CMD_RENAME(char * args){
230 HELP("RENAME");
231 StripSpaces(args);
232 if(!*args) {SyntaxError();return;}
233 if((strchr(args,'*')!=NULL) || (strchr(args,'?')!=NULL) ) { WriteOut(MSG_Get("SHELL_CMD_NO_WILD"));return;}
234 char * arg1=StripWord(args);
235 char* slash = strrchr(arg1,'\\');
236 if(slash) {
237 slash++;
238 /* If directory specified (crystal caves installer)
239 * rename from c:\X : rename c:\abc.exe abc.shr.
240 * File must appear in C:\ */
241
242 char dir_source[DOS_PATHLENGTH]={0};
243 //Copy first and then modify, makes GCC happy
244 strcpy(dir_source,arg1);
245 char* dummy = strrchr(dir_source,'\\');
246 *dummy=0;
247
248 if((strlen(dir_source) == 2) && (dir_source[1] == ':'))
249 strcat(dir_source,"\\"); //X: add slash
250
251 char dir_current[DOS_PATHLENGTH + 1];
252 dir_current[0] = '\\'; //Absolute addressing so we can return properly
253 DOS_GetCurrentDir(0,dir_current + 1);
254 if(!DOS_ChangeDir(dir_source)) {
255 WriteOut(MSG_Get("SHELL_ILLEGAL_PATH"));
256 return;
257 }
258 DOS_Rename(slash,args);
259 DOS_ChangeDir(dir_current);
260 } else {
261 DOS_Rename(arg1,args);
262 }
263 }
264
265 void DOS_Shell::CMD_ECHO(char * args){
266 if (!*args) {
267 if (echo) { WriteOut(MSG_Get("SHELL_CMD_ECHO_ON"));}
268 else { WriteOut(MSG_Get("SHELL_CMD_ECHO_OFF"));}
269 return;
270 }
271 char buffer[512];
272 char* pbuffer = buffer;
273 safe_strncpy(buffer,args,512);
274 StripSpaces(pbuffer);
275 if (strcasecmp(pbuffer,"OFF")==0) {
276 echo=false;
277 return;
278 }
279 if (strcasecmp(pbuffer,"ON")==0) {
280 echo=true;
281 return;
282 }
283 if(strcasecmp(pbuffer,"/?")==0) { HELP("ECHO"); }
284
285 args++;//skip first character. either a slash or dot or space
286 size_t len = strlen(args); //TODO check input of else ook nodig is.
287 if(len && args[len - 1] == '\r') {
288 LOG(LOG_MISC,LOG_WARN)("Hu ? carriage return allready present. Is this possible?");
289 WriteOut("%s\n",args);
290 } else WriteOut("%s\r\n",args);
291 }
292
293
294 void DOS_Shell::CMD_EXIT(char * args) {
295 HELP("EXIT");
296 exit = true;
297 }
298
299 void DOS_Shell::CMD_CHDIR(char * args) {
300 HELP("CHDIR");
301 StripSpaces(args);
302 if (!*args) {
303 Bit8u drive=DOS_GetDefaultDrive()+'A';
304 char dir[DOS_PATHLENGTH];
305 DOS_GetCurrentDir(0,dir);
306 WriteOut("%c:\\%s\n",drive,dir);
307 } else if(strlen(args) == 2 && args[1]==':') {
308 WriteOut(MSG_Get("SHELL_CMD_CHDIR_HINT"),toupper(*reinterpret_cast<unsigned char*>(&args[0])));
309 } else if (!DOS_ChangeDir(args)) {
310 /* Changedir failed. Check if the filename is longer then 8 and/or contains spaces */
311
312 std::string temps(args),slashpart;
313 std::string::size_type separator = temps.find_first_of("\\/");
314 if(!separator) {
315 slashpart = temps.substr(0,1);
316 temps.erase(0,1);
317 }
318 separator = temps.find_first_of("\\/");
319 if(separator != std::string::npos) temps.erase(separator);
320 separator = temps.rfind('.');
321 if(separator != std::string::npos) temps.erase(separator);
322 separator = temps.find(' ');
323 if(separator != std::string::npos) {/* Contains spaces */
324 temps.erase(separator);
325 if(temps.size() >6) temps.erase(6);
326 temps += "~1";
327 WriteOut(MSG_Get("SHELL_CMD_CHDIR_HINT_2"),temps.insert(0,slashpart).c_str());
328 } else if (temps.size()>8) {
329 temps.erase(6);
330 temps += "~1";
331 WriteOut(MSG_Get("SHELL_CMD_CHDIR_HINT_2"),temps.insert(0,slashpart).c_str());
332 } else {
333 Bit8u drive=DOS_GetDefaultDrive()+'A';
334 if (drive=='Z') {
335 WriteOut(MSG_Get("SHELL_CMD_CHDIR_HINT_3"));
336 } else {
337 WriteOut(MSG_Get("SHELL_CMD_CHDIR_ERROR"),args);
338 }
339 }
340 }
341 }
342
343 void DOS_Shell::CMD_MKDIR(char * args) {
344 HELP("MKDIR");
345 StripSpaces(args);
346 char * rem=ScanCMDRemain(args);
347 if (rem) {
348 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
349 return;
350 }
351 if (!DOS_MakeDir(args)) {
352 WriteOut(MSG_Get("SHELL_CMD_MKDIR_ERROR"),args);
353 }
354 }
355
356 void DOS_Shell::CMD_RMDIR(char * args) {
357 HELP("RMDIR");
358 StripSpaces(args);
359 char * rem=ScanCMDRemain(args);
360 if (rem) {
361 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
362 return;
363 }
364 if (!DOS_RemoveDir(args)) {
365 WriteOut(MSG_Get("SHELL_CMD_RMDIR_ERROR"),args);
366 }
367 }
368
369 static void FormatNumber(Bitu num,char * buf) {
370 Bitu numm,numk,numb,numg;
371 numb=num % 1000;
372 num/=1000;
373 numk=num % 1000;
374 num/=1000;
375 numm=num % 1000;
376 num/=1000;
377 numg=num;
378 if (numg) {
379 sprintf(buf,"%d,%03d,%03d,%03d",numg,numm,numk,numb);
380 return;
381 };
382 if (numm) {
383 sprintf(buf,"%d,%03d,%03d",numm,numk,numb);
384 return;
385 };
386 if (numk) {
387 sprintf(buf,"%d,%03d",numk,numb);
388 return;
389 };
390 sprintf(buf,"%d",numb);
391 }
392
393 void DOS_Shell::CMD_DIR(char * args) {
394 HELP("DIR");
395 char numformat[16];
396 char path[DOS_PATHLENGTH];
397
398 std::string line;
399 if(GetEnvStr("DIRCMD",line)){
400 std::string::size_type idx = line.find('=');
401 std::string value=line.substr(idx +1 , std::string::npos);
402 line = std::string(args) + " " + value;
403 args=const_cast<char*>(line.c_str());
404 }
405
406 bool optW=ScanCMDBool(args,"W");
407 ScanCMDBool(args,"S");
408 bool optP=ScanCMDBool(args,"P");
409 if (ScanCMDBool(args,"WP") || ScanCMDBool(args,"PW")) {
410 optW=optP=true;
411 }
412 bool optB=ScanCMDBool(args,"B");
413 bool optAD=ScanCMDBool(args,"AD");
414 char * rem=ScanCMDRemain(args);
415 if (rem) {
416 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
417 return;
418 }
419 Bit32u byte_count,file_count,dir_count;
420 Bitu w_count=0;
421 Bitu p_count=0;
422 Bitu w_size = optW?5:1;
423 byte_count=file_count=dir_count=0;
424
425 char buffer[CROSS_LEN];
426 args = trim(args);
427 size_t argLen = strlen(args);
428 if (argLen == 0) {
429 strcpy(args,"*.*"); //no arguments.
430 } else {
431 switch (args[argLen-1])
432 {
433 case '\\': // handle \, C:\, etc.
434 case ':' : // handle C:, etc.
435 strcat(args,"*.*");
436 break;
437 default:
438 break;
439 }
440 }
441 args = ExpandDot(args,buffer,CROSS_LEN);
442
443 if (!strrchr(args,'*') && !strrchr(args,'?')) {
444 Bit16u attribute=0;
445 if(DOS_GetFileAttr(args,&attribute) && (attribute&DOS_ATTR_DIRECTORY) ) {
446 strcat(args,"\\*.*"); // if no wildcard and a directory, get its files
447 }
448 }
449 if (!strrchr(args,'.')) {
450 strcat(args,".*"); // if no extension, get them all
451 }
452
453 /* Make a full path in the args */
454 if (!DOS_Canonicalize(args,path)) {
455 WriteOut(MSG_Get("SHELL_ILLEGAL_PATH"));
456 return;
457 }
458 *(strrchr(path,'\\')+1)=0;
459 if (!optB) WriteOut(MSG_Get("SHELL_CMD_DIR_INTRO"),path);
460
461 /* Command uses dta so set it to our internal dta */
462 RealPt save_dta=dos.dta();
463 dos.dta(dos.tables.tempdta);
464 DOS_DTA dta(dos.dta());
465 bool ret=DOS_FindFirst(args,0xffff & ~DOS_ATTR_VOLUME);
466 if (!ret) {
467 if (!optB) WriteOut(MSG_Get("SHELL_CMD_FILE_NOT_FOUND"),args);
468 dos.dta(save_dta);
469 return;
470 }
471
472 do { /* File name and extension */
473 char name[DOS_NAMELENGTH_ASCII];Bit32u size;Bit16u date;Bit16u time;Bit8u attr;
474 dta.GetResult(name,size,date,time,attr);
475
476 /* Skip non-directories if option AD is present */
477 if(optAD && !(attr&DOS_ATTR_DIRECTORY) ) continue;
478
479 /* output the file */
480 if (optB) {
481 // this overrides pretty much everything
482 if (strcmp(".",name) && strcmp("..",name)) {
483 WriteOut("%s\n",name);
484 }
485 } else {
486 char * ext = empty_string;
487 if (!optW && (name[0] != '.')) {
488 ext = strrchr(name, '.');
489 if (!ext) ext = empty_string;
490 else *ext++ = 0;
491 }
492 Bit8u day = (Bit8u)(date & 0x001f);
493 Bit8u month = (Bit8u)((date >> 5) & 0x000f);
494 Bit16u year = (Bit16u)((date >> 9) + 1980);
495 Bit8u hour = (Bit8u)((time >> 5 ) >> 6);
496 Bit8u minute = (Bit8u)((time >> 5) & 0x003f);
497
498 if (attr & DOS_ATTR_DIRECTORY) {
499 if (optW) {
500 WriteOut("[%s]",name);
501 size_t namelen = strlen(name);
502 if (namelen <= 14) {
503 for (size_t i=14-namelen;i>0;i--) WriteOut(" ");
504 }
505 } else {
506 WriteOut("%-8s %-3s %-16s %02d-%02d-%04d %2d:%02d\n",name,ext,"<DIR>",day,month,year,hour,minute);
507 }
508 dir_count++;
509 } else {
510 if (optW) {
511 WriteOut("%-16s",name);
512 } else {
513 FormatNumber(size,numformat);
514 WriteOut("%-8s %-3s %16s %02d-%02d-%04d %2d:%02d\n",name,ext,numformat,day,month,year,hour,minute);
515 }
516 file_count++;
517 byte_count+=size;
518 }
519 if (optW) {
520 w_count++;
521 }
522 }
523 if (optP && !(++p_count%(22*w_size))) {
524 CMD_PAUSE(empty_string);
525 }
526 } while ( (ret=DOS_FindNext()) );
527 if (optW) {
528 if (w_count%5) WriteOut("\n");
529 }
530 if (!optB) {
531 /* Show the summary of results */
532 FormatNumber(byte_count,numformat);
533 WriteOut(MSG_Get("SHELL_CMD_DIR_BYTES_USED"),file_count,numformat);
534 Bit8u drive=dta.GetSearchDrive();
535 //TODO Free Space
536 Bitu free_space=1024*1024*100;
537 if (Drives[drive]) {
538 Bit16u bytes_sector;Bit8u sectors_cluster;Bit16u total_clusters;Bit16u free_clusters;
539 Drives[drive]->AllocationInfo(&bytes_sector,&sectors_cluster,&total_clusters,&free_clusters);
540 free_space=bytes_sector*sectors_cluster*free_clusters;
541 }
542 FormatNumber(free_space,numformat);
543 WriteOut(MSG_Get("SHELL_CMD_DIR_BYTES_FREE"),dir_count,numformat);
544 }
545 dos.dta(save_dta);
546 }
547
548 struct copysource {
549 std::string filename;
550 bool concat;
551 copysource(std::string filein,bool concatin):
552 filename(filein),concat(concatin){ };
553 copysource():filename(""),concat(false){ };
554 };
555
556
557 void DOS_Shell::CMD_COPY(char * args) {
558 HELP("COPY");
559 static char defaulttarget[] = ".";
560 StripSpaces(args);
561 /* Command uses dta so set it to our internal dta */
562 RealPt save_dta=dos.dta();
563 dos.dta(dos.tables.tempdta);
564 DOS_DTA dta(dos.dta());
565 Bit32u size;Bit16u date;Bit16u time;Bit8u attr;
566 char name[DOS_NAMELENGTH_ASCII];
567 std::vector<copysource> sources;
568 // ignore /b and /t switches: always copy binary
569 while(ScanCMDBool(args,"B")) ;
570 while(ScanCMDBool(args,"T")) ; //Shouldn't this be A ?
571 while(ScanCMDBool(args,"A")) ;
572 ScanCMDBool(args,"Y");
573 ScanCMDBool(args,"-Y");
574
575 char * rem=ScanCMDRemain(args);
576 if (rem) {
577 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
578 dos.dta(save_dta);
579 return;
580 }
581 // Gather all sources (extension to copy more then 1 file specified at commandline)
582 // Concatating files go as follows: All parts except for the last bear the concat flag.
583 // This construction allows them to be counted (only the non concat set)
584 char* source_p = NULL;
585 char source_x[DOS_PATHLENGTH+CROSS_LEN];
586 while ( (source_p = StripWord(args)) && *source_p ) {
587 do {
588 char* plus = strchr(source_p,'+');
589 if (plus) *plus++ = 0;
590 safe_strncpy(source_x,source_p,CROSS_LEN);
591 bool has_drive_spec = false;
592 size_t source_x_len = strlen(source_x);
593 if (source_x_len>0) {
594 if (source_x[source_x_len-1]==':') has_drive_spec = true;
595 }
596 if (!has_drive_spec) {
597 if (DOS_FindFirst(source_p,0xffff & ~DOS_ATTR_VOLUME)) {
598 dta.GetResult(name,size,date,time,attr);
599 if (attr & DOS_ATTR_DIRECTORY && !strstr(source_p,"*.*"))
600 strcat(source_x,"\\*.*");
601 }
602 }
603 sources.push_back(copysource(source_x,(plus)?true:false));
604 source_p = plus;
605 } while(source_p && *source_p);
606 }
607 // At least one source has to be there
608 if (!sources.size() || !sources[0].filename.size()) {
609 WriteOut(MSG_Get("SHELL_MISSING_PARAMETER"));
610 dos.dta(save_dta);
611 return;
612 };
613
614 copysource target;
615 // If more then one object exists and last target is not part of a
616 // concat sequence then make it the target.
617 if(sources.size()>1 && !sources[sources.size()-2].concat){
618 target = sources.back();
619 sources.pop_back();
620 }
621 //If no target => default target with concat flag true to detect a+b+c
622 if(target.filename.size() == 0) target = copysource(defaulttarget,true);
623
624 copysource oldsource;
625 copysource source;
626 Bit32u count = 0;
627 while(sources.size()) {
628 /* Get next source item and keep track of old source for concat start end */
629 oldsource = source;
630 source = sources[0];
631 sources.erase(sources.begin());
632
633 //Skip first file if doing a+b+c. Set target to first file
634 if(!oldsource.concat && source.concat && target.concat) {
635 target = source;
636 continue;
637 }
638
639 /* Make a full path in the args */
640 char pathSource[DOS_PATHLENGTH];
641 char pathTarget[DOS_PATHLENGTH];
642
643 if (!DOS_Canonicalize(const_cast<char*>(source.filename.c_str()),pathSource)) {
644 WriteOut(MSG_Get("SHELL_ILLEGAL_PATH"));
645 dos.dta(save_dta);
646 return;
647 }
648 // cut search pattern
649 char* pos = strrchr(pathSource,'\\');
650 if (pos) *(pos+1) = 0;
651
652 if (!DOS_Canonicalize(const_cast<char*>(target.filename.c_str()),pathTarget)) {
653 WriteOut(MSG_Get("SHELL_ILLEGAL_PATH"));
654 dos.dta(save_dta);
655 return;
656 }
657 char* temp = strstr(pathTarget,"*.*");
658 if(temp) *temp = 0;//strip off *.* from target
659
660 // add '\\' if target is a directoy
661 if (pathTarget[strlen(pathTarget)-1]!='\\') {
662 if (DOS_FindFirst(pathTarget,0xffff & ~DOS_ATTR_VOLUME)) {
663 dta.GetResult(name,size,date,time,attr);
664 if (attr & DOS_ATTR_DIRECTORY)
665 strcat(pathTarget,"\\");
666 }
667 };
668
669 //Find first sourcefile
670 bool ret = DOS_FindFirst(const_cast<char*>(source.filename.c_str()),0xffff & ~DOS_ATTR_VOLUME);
671 if (!ret) {
672 WriteOut(MSG_Get("SHELL_CMD_FILE_NOT_FOUND"),const_cast<char*>(source.filename.c_str()));
673 dos.dta(save_dta);
674 return;
675 }
676
677 Bit16u sourceHandle,targetHandle;
678 char nameTarget[DOS_PATHLENGTH];
679 char nameSource[DOS_PATHLENGTH];
680
681 while (ret) {
682 dta.GetResult(name,size,date,time,attr);
683
684 if ((attr & DOS_ATTR_DIRECTORY)==0) {
685 strcpy(nameSource,pathSource);
686 strcat(nameSource,name);
687 // Open Source
688 if (DOS_OpenFile(nameSource,0,&sourceHandle)) {
689 // Create Target or open it if in concat mode
690 strcpy(nameTarget,pathTarget);
691 if (nameTarget[strlen(nameTarget)-1]=='\\') strcat(nameTarget,name);
692
693 //Don't create a newfile when in concat mode
694 if (oldsource.concat || DOS_CreateFile(nameTarget,0,&targetHandle)) {
695 Bit32u dummy=0;
696 //In concat mode. Open the target and seek to the eof
697 if (!oldsource.concat || (DOS_OpenFile(nameTarget,OPEN_READWRITE,&targetHandle) &&
698 DOS_SeekFile(targetHandle,&dummy,DOS_SEEK_END))) {
699 // Copy
700 static Bit8u buffer[0x8000]; // static, otherwise stack overflow possible.
701 bool failed = false;
702 Bit16u toread = 0x8000;
703 do {
704 failed |= DOS_ReadFile(sourceHandle,buffer,&toread);
705 failed |= DOS_WriteFile(targetHandle,buffer,&toread);
706 } while (toread==0x8000);
707 failed |= DOS_CloseFile(sourceHandle);
708 failed |= DOS_CloseFile(targetHandle);
709 WriteOut(" %s\n",name);
710 if(!source.concat) count++; //Only count concat files once
711 } else {
712 DOS_CloseFile(sourceHandle);
713 WriteOut(MSG_Get("SHELL_CMD_COPY_FAILURE"),const_cast<char*>(target.filename.c_str()));
714 }
715 } else {
716 DOS_CloseFile(sourceHandle);
717 WriteOut(MSG_Get("SHELL_CMD_COPY_FAILURE"),const_cast<char*>(target.filename.c_str()));
718 }
719 } else WriteOut(MSG_Get("SHELL_CMD_COPY_FAILURE"),const_cast<char*>(source.filename.c_str()));
720 };
721 //On the next file
722 ret = DOS_FindNext();
723 };
724 }
725
726 WriteOut(MSG_Get("SHELL_CMD_COPY_SUCCESS"),count);
727 dos.dta(save_dta);
728 }
729
730 void DOS_Shell::CMD_SET(char * args) {
731 HELP("SET");
732 StripSpaces(args);
733 std::string line;
734 if (!*args) {
735 /* No command line show all environment lines */
736 Bitu count=GetEnvCount();
737 for (Bitu a=0;a<count;a++) {
738 if (GetEnvNum(a,line)) WriteOut("%s\n",line.c_str());
739 }
740 return;
741 }
742 char * p=strpbrk(args, "=");
743 if (!p) {
744 if (!GetEnvStr(args,line)) WriteOut(MSG_Get("SHELL_CMD_SET_NOT_SET"),args);
745 WriteOut("%s\n",line.c_str());
746 } else {
747 *p++=0;
748 /* parse p for envirionment variables */
749 char parsed[CMD_MAXLINE];
750 char* p_parsed = parsed;
751 while(*p) {
752 if(*p != '%') *p_parsed++ = *p++; //Just add it (most likely path)
753 else if( *(p+1) == '%') {
754 *p_parsed++ = '%'; p += 2; //%% => %
755 } else {
756 char * second = strchr(++p,'%');
757 if(!second) continue; *second++ = 0;
758 std::string temp;
759 if (GetEnvStr(p,temp)) {
760 std::string::size_type equals = temp.find('=');
761 if (equals == std::string::npos) continue;
762 strcpy(p_parsed,temp.substr(equals+1).c_str());
763 p_parsed += strlen(p_parsed);
764 }
765 p = second;
766 }
767 }
768 *p_parsed = 0;
769 /* Try setting the variable */
770 if (!SetEnv(args,parsed)) {
771 WriteOut(MSG_Get("SHELL_CMD_SET_OUT_OF_SPACE"));
772 }
773 }
774 }
775
776 void DOS_Shell::CMD_IF(char * args) {
777 HELP("IF");
778 StripSpaces(args,'=');
779 bool has_not=false;
780
781 while (strncasecmp(args,"NOT",3) == 0) {
782 if (!isspace(*reinterpret_cast<unsigned char*>(&args[3])) && (args[3] != '=')) break;
783 args += 3; //skip text
784 //skip more spaces
785 StripSpaces(args,'=');
786 has_not = !has_not;
787 }
788
789 if(strncasecmp(args,"ERRORLEVEL",10) == 0) {
790 args += 10; //skip text
791 //Strip spaces and ==
792 StripSpaces(args,'=');
793 char* word = StripWord(args);
794 if(!isdigit(*word)) {
795 WriteOut(MSG_Get("SHELL_CMD_IF_ERRORLEVEL_MISSING_NUMBER"));
796 return;
797 }
798
799 Bit8u n = 0;
800 do n = n * 10 + (*word - '0');
801 while (isdigit(*++word));
802 if(*word && !isspace(*word)) {
803 WriteOut(MSG_Get("SHELL_CMD_IF_ERRORLEVEL_INVALID_NUMBER"));
804 return;
805 }
806 /* Read the error code from DOS */
807 if ((dos.return_code>=n) ==(!has_not)) DoCommand(args);
808 return;
809 }
810
811 if(strncasecmp(args,"EXIST ",6) == 0) {
812 args += 6; //Skip text
813 StripSpaces(args);
814 char* word = StripWord(args);
815 if (!*word) {
816 WriteOut(MSG_Get("SHELL_CMD_IF_EXIST_MISSING_FILENAME"));
817 return;
818 }
819
820 { /* DOS_FindFirst uses dta so set it to our internal dta */
821 RealPt save_dta=dos.dta();
822 dos.dta(dos.tables.tempdta);
823 bool ret=DOS_FindFirst(word,0xffff & ~DOS_ATTR_VOLUME);
824 dos.dta(save_dta);
825 if (ret==(!has_not)) DoCommand(args);
826 }
827 return;
828 }
829
830 /* Normal if string compare */
831
832 char* word1 = args;
833 // first word is until space or =
834 while (*args && !isspace(*reinterpret_cast<unsigned char*>(args)) && (*args != '='))
835 args++;
836 char* end_word1 = args;
837
838 // scan for =
839 while (*args && (*args != '='))
840 args++;
841 // check for ==
842 if ((*args==0) || (args[1] != '=')) {
843 SyntaxError();
844 return;
845 }
846 args += 2;
847 StripSpaces(args,'=');
848
849 char* word2 = args;
850 // second word is until space or =
851 while (*args && !isspace(*reinterpret_cast<unsigned char*>(args)) && (*args != '='))
852 args++;
853
854 if (*args) {
855 *end_word1 = 0; // mark end of first word
856 *args++ = 0; // mark end of second word
857 StripSpaces(args,'=');
858
859 if ((strcmp(word1,word2)==0)==(!has_not)) DoCommand(args);
860 }
861 }
862
863 void DOS_Shell::CMD_GOTO(char * args) {
864 HELP("GOTO");
865 StripSpaces(args);
866 if (!bf) return;
867 if (*args &&(*args==':')) args++;
868 //label ends at the first space
869 char* non_space = args;
870 while (*non_space) {
871 if((*non_space == ' ') || (*non_space == '\t'))
872 *non_space = 0;
873 else non_space++;
874 }
875 if (!*args) {
876 WriteOut(MSG_Get("SHELL_CMD_GOTO_MISSING_LABEL"));
877 return;
878 }
879 if (!bf->Goto(args)) {
880 WriteOut(MSG_Get("SHELL_CMD_GOTO_LABEL_NOT_FOUND"),args);
881 return;
882 }
883 }
884
885 void DOS_Shell::CMD_SHIFT(char * args ) {
886 HELP("SHIFT");
887 if(bf) bf->Shift();
888 }
889
890 void DOS_Shell::CMD_TYPE(char * args) {
891 HELP("TYPE");
892 StripSpaces(args);
893 if (!*args) {
894 WriteOut(MSG_Get("SHELL_SYNTAXERROR"));
895 return;
896 }
897 Bit16u handle;
898 char * word;
899 nextfile:
900 word=StripWord(args);
901 if (!DOS_OpenFile(word,0,&handle)) {
902 WriteOut(MSG_Get("SHELL_CMD_FILE_NOT_FOUND"),word);
903 return;
904 }
905 Bit16u n;Bit8u c;
906 do {
907 n=1;
908 DOS_ReadFile(handle,&c,&n);
909 if (c==0x1a) break; // stop at EOF
910 DOS_WriteFile(STDOUT,&c,&n);
911 } while (n);
912 DOS_CloseFile(handle);
913 if (*args) goto nextfile;
914 }
915
916 void DOS_Shell::CMD_REM(char * args) {
917 HELP("REM");
918 }
919
920 void DOS_Shell::CMD_PAUSE(char * args){
921 HELP("PAUSE");
922 WriteOut(MSG_Get("SHELL_CMD_PAUSE"));
923 Bit8u c;Bit16u n=1;
924 DOS_ReadFile(STDIN,&c,&n);
925 if (c==0) DOS_ReadFile(STDIN,&c,&n); // read extended key
926 }
927
928 void DOS_Shell::CMD_CALL(char * args){
929 HELP("CALL");
930 this->call=true; /* else the old batchfile will be closed first */
931 this->ParseLine(args);
932 this->call=false;
933 }
934
935 void DOS_Shell::CMD_SUBST (char * args) {
936 /* If more that one type can be substed think of something else
937 * E.g. make basedir member dos_drive instead of localdrive
938 */
939 HELP("SUBST");
940 localDrive* ldp=0;
941 char mountstring[DOS_PATHLENGTH+CROSS_LEN+20];
942 char temp_str[2] = { 0,0 };
943 try {
944 strcpy(mountstring,"MOUNT ");
945 StripSpaces(args);
946 std::string arg;
947 CommandLine command(0,args);
948
949 if (command.GetCount() != 2) throw 0 ;
950 command.FindCommand(2,arg);
951 if((arg=="/D" ) || (arg=="/d")) throw 1; //No removal (one day)
952
953 command.FindCommand(1,arg);
954 if( (arg.size()>1) && arg[1] !=':') throw(0);
955 temp_str[0]=(char)toupper(args[0]);
956 if(Drives[temp_str[0]-'A'] ) throw 0; //targetdrive in use
957 strcat(mountstring,temp_str);
958 strcat(mountstring," ");
959
960 command.FindCommand(2,arg);
961 Bit8u drive;char fulldir[DOS_PATHLENGTH];
962 if (!DOS_MakeName(const_cast<char*>(arg.c_str()),fulldir,&drive)) throw 0;
963
964 if( ( ldp=dynamic_cast<localDrive*>(Drives[drive])) == 0 ) throw 0;
965 char newname[CROSS_LEN];
966 strcpy(newname, ldp->basedir);
967 strcat(newname,fulldir);
968 CROSS_FILENAME(newname);
969 ldp->dirCache.ExpandName(newname);
970 strcat(mountstring,"\"");
971 strcat(mountstring, newname);
972 strcat(mountstring,"\"");
973 this->ParseLine(mountstring);
974 }
975 catch(int a){
976 if(a == 0) {
977 WriteOut(MSG_Get("SHELL_CMD_SUBST_FAILURE"));
978 } else {
979 WriteOut(MSG_Get("SHELL_CMD_SUBST_NO_REMOVE"));
980 }
981 return;
982 }
983 catch(...) { //dynamic cast failed =>so no localdrive
984 WriteOut(MSG_Get("SHELL_CMD_SUBST_FAILURE"));
985 return;
986 }
987
988 return;
989 }
990
991 void DOS_Shell::CMD_LOADHIGH(char *args){
992 HELP("LOADHIGH");
993 Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
994 Bit8u umb_flag=dos_infoblock.GetUMBChainState();
995 Bit8u old_memstrat=(Bit8u)(DOS_GetMemAllocStrategy()&0xff);
996 if (umb_start==0x9fff) {
997 if ((umb_flag&1)==0) DOS_LinkUMBsToMemChain(1);
998 DOS_SetMemAllocStrategy(0x80); // search in UMBs first
999 this->ParseLine(args);
1000 Bit8u current_umb_flag=dos_infoblock.GetUMBChainState();
1001 if ((current_umb_flag&1)!=(umb_flag&1)) DOS_LinkUMBsToMemChain(umb_flag);
1002 DOS_SetMemAllocStrategy(old_memstrat); // restore strategy
1003 } else this->ParseLine(args);
1004 }
1005
1006 void DOS_Shell::CMD_CHOICE(char * args){
1007 HELP("CHOICE");
1008 static char defchoice[3] = {'y','n',0};
1009 char *rem = NULL, *ptr;
1010 bool optN = ScanCMDBool(args,"N");
1011 bool optS = ScanCMDBool(args,"S"); //Case-sensitive matching
1012 ScanCMDBool(args,"T"); //Default Choice after timeout
1013 if (args) {
1014 char *last = strchr(args,0);
1015 StripSpaces(args);
1016 rem = ScanCMDRemain(args);
1017 if (rem && *rem && (tolower(rem[1]) != 'c')) {
1018 WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem);
1019 return;
1020 }
1021 if (args == rem) args = strchr(rem,0)+1;
1022 if (rem) rem += 2;
1023 if(rem && rem[0]==':') rem++; /* optional : after /c */
1024 if (args > last) args = NULL;
1025 }
1026 if (!rem || !*rem) rem = defchoice; /* No choices specified use YN */
1027 ptr = rem;
1028 Bit8u c;
1029 if(!optS) while ((c = *ptr)) *ptr++ = (char)toupper(c); /* When in no case-sensitive mode. make everything upcase */
1030 if(args && *args ) {
1031 StripSpaces(args);
1032 size_t argslen = strlen(args);
1033 if(argslen>1 && args[0] == '"' && args[argslen-1] =='"') {
1034 args[argslen-1] = 0; //Remove quotes
1035 args++;
1036 }
1037 WriteOut(args);
1038 }
1039 /* Show question prompt of the form [a,b]? where a b are the choice values */
1040 if (!optN) {
1041 if(args && *args) WriteOut(" ");
1042 WriteOut("[");
1043 size_t len = strlen(rem);
1044 for(size_t t = 1; t < len; t++) {
1045 WriteOut("%c,",rem[t-1]);
1046 }
1047 WriteOut("%c]?",rem[len-1]);
1048 }
1049
1050 Bit16u n=1;
1051 do {
1052 DOS_ReadFile (STDIN,&c,&n);
1053 } while (!c || !(ptr = strchr(rem,(optS?c:toupper(c)))));
1054 c = optS?c:(Bit8u)toupper(c);
1055 DOS_WriteFile (STDOUT,&c, &n);
1056 dos.return_code = (Bit8u)(ptr-rem+1);
1057 }
1058
1059 void DOS_Shell::CMD_ATTRIB(char *args){
1060 HELP("ATTRIB");
1061 // No-Op for now.
1062 }
1063
1064 void DOS_Shell::CMD_PATH(char *args){
1065 HELP("PATH");
1066 if(args && *args && strlen(args)){
1067 char pathstring[DOS_PATHLENGTH+CROSS_LEN+20]={ 0 };
1068 strcpy(pathstring,"set PATH=");
1069 while(args && *args && (*args=='='|| *args==' '))
1070 args++;
1071 strcat(pathstring,args);
1072 this->ParseLine(pathstring);
1073 return;
1074 } else {
1075 std::string line;
1076 if(GetEnvStr("PATH",line)) {
1077 WriteOut("%s",line.c_str());
1078 } else {
1079 WriteOut("PATH=(null)");
1080 }
1081 }
1082 }
1083
1084 void DOS_Shell::CMD_VER(char *args) {
1085 HELP("VER");
1086 if(args && *args) {
1087 char* word = StripWord(args);
1088 if(strcasecmp(word,"set")) return;
1089 word = StripWord(args);
1090 dos.version.major = (Bit8u)(atoi(word));
1091 dos.version.minor = (Bit8u)(atoi(args));
1092 } else WriteOut(MSG_Get("SHELL_CMD_VER_VER"),VERSION,dos.version.major,dos.version.minor);
1093 }