Debian Codegraph @

src/shell/shell_misc.cpp

raw symbols SCIP
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_misc.cpp,v 1.54 2009-05-27 09:15:42 qbix79 Exp $ */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <algorithm> //std::copy
24 #include <iterator> //std::front_inserter
25 #include "shell.h"
26 #include "regs.h"
27 #include "callback.h"
28 #include "support.h"
29
30 void DOS_Shell::ShowPrompt(void) {
31 Bit8u drive=DOS_GetDefaultDrive()+'A';
32 char dir[DOS_PATHLENGTH];
33 dir[0] = 0; //DOS_GetCurrentDir doesn't always return something. (if drive is messed up)
34 DOS_GetCurrentDir(0,dir);
35 WriteOut("%c:\\%s>",drive,dir);
36 }
37
38 static void outc(Bit8u c) {
39 Bit16u n=1;
40 DOS_WriteFile(STDOUT,&c,&n);
41 }
42
43 void DOS_Shell::InputCommand(char * line) {
44 Bitu size=CMD_MAXLINE-2; //lastcharacter+0
45 Bit8u c;Bit16u n=1;
46 Bitu str_len=0;Bitu str_index=0;
47 Bit16u len=0;
48 bool current_hist=false; // current command stored in history?
49
50 line[0] = '\0';
51
52 std::list<std::string>::iterator it_history = l_history.begin(), it_completion = l_completion.begin();
53
54 while (size) {
55 dos.echo=false;
56 while(!DOS_ReadFile(input_handle,&c,&n)) {
57 Bit16u dummy;
58 DOS_CloseFile(input_handle);
59 DOS_OpenFile("con",2,&dummy);
60 LOG(LOG_MISC,LOG_ERROR)("Reopening the input handle.This is a bug!");
61 }
62 if (!n) {
63 size=0; //Kill the while loop
64 continue;
65 }
66 switch (c) {
67 case 0x00: /* Extended Keys */
68 {
69 DOS_ReadFile(input_handle,&c,&n);
70 switch (c) {
71
72 case 0x3d: /* F3 */
73 if (!l_history.size()) break;
74 it_history = l_history.begin();
75 if (it_history != l_history.end() && it_history->length() > str_len) {
76 const char *reader = &(it_history->c_str())[str_len];
77 while ((c = *reader++)) {
78 line[str_index ++] = c;
79 DOS_WriteFile(STDOUT,&c,&n);
80 }
81 str_len = str_index = (Bitu)it_history->length();
82 size = CMD_MAXLINE - str_index - 2;
83 line[str_len] = 0;
84 }
85 break;
86
87 case 0x4B: /* LEFT */
88 if (str_index) {
89 outc(8);
90 str_index --;
91 }
92 break;
93
94 case 0x4D: /* RIGHT */
95 if (str_index < str_len) {
96 outc(line[str_index++]);
97 }
98 break;
99
100 case 0x47: /* HOME */
101 while (str_index) {
102 outc(8);
103 str_index--;
104 }
105 break;
106
107 case 0x4F: /* END */
108 while (str_index < str_len) {
109 outc(line[str_index++]);
110 }
111 break;
112
113 case 0x48: /* UP */
114 if (l_history.empty() || it_history == l_history.end()) break;
115
116 // store current command in history if we are at beginning
117 if (it_history == l_history.begin() && !current_hist) {
118 current_hist=true;
119 l_history.push_front(line);
120 }
121
122 for (;str_index>0; str_index--) {
123 // removes all characters
124 outc(8); outc(' '); outc(8);
125 }
126 strcpy(line, it_history->c_str());
127 len = (Bit16u)it_history->length();
128 str_len = str_index = len;
129 size = CMD_MAXLINE - str_index - 2;
130 DOS_WriteFile(STDOUT, (Bit8u *)line, &len);
131 it_history ++;
132 break;
133
134 case 0x50: /* DOWN */
135 if (l_history.empty() || it_history == l_history.begin()) break;
136
137 // not very nice but works ..
138 it_history --;
139 if (it_history == l_history.begin()) {
140 // no previous commands in history
141 it_history ++;
142
143 // remove current command from history
144 if (current_hist) {
145 current_hist=false;
146 l_history.pop_front();
147 }
148 break;
149 } else it_history --;
150
151 for (;str_index>0; str_index--) {
152 // removes all characters
153 outc(8); outc(' '); outc(8);
154 }
155 strcpy(line, it_history->c_str());
156 len = (Bit16u)it_history->length();
157 str_len = str_index = len;
158 size = CMD_MAXLINE - str_index - 2;
159 DOS_WriteFile(STDOUT, (Bit8u *)line, &len);
160 it_history ++;
161
162 break;
163 case 0x53:/* DELETE */
164 {
165 if(str_index>=str_len) break;
166 Bit16u a=str_len-str_index-1;
167 Bit8u* text=reinterpret_cast<Bit8u*>(&line[str_index+1]);
168 DOS_WriteFile(STDOUT,text,&a);//write buffer to screen
169 outc(' ');outc(8);
170 for(Bitu i=str_index;i<str_len-1;i++) {
171 line[i]=line[i+1];
172 outc(8);
173 }
174 line[--str_len]=0;
175 size++;
176 }
177 break;
178 default:
179 break;
180 }
181 };
182 break;
183 case 0x08: /* BackSpace */
184 if (str_index) {
185 outc(8);
186 Bit32u str_remain=str_len - str_index;
187 size++;
188 if (str_remain) {
189 memmove(&line[str_index-1],&line[str_index],str_remain);
190 line[--str_len]=0;
191 str_index --;
192 /* Go back to redraw */
193 for (Bit16u i=str_index; i < str_len; i++)
194 outc(line[i]);
195 } else {
196 line[--str_index] = '\0';
197 str_len--;
198 }
199 outc(' '); outc(8);
200 // moves the cursor left
201 while (str_remain--) outc(8);
202 }
203 if (l_completion.size()) l_completion.clear();
204 break;
205 case 0x0a: /* New Line not handled */
206 /* Don't care */
207 break;
208 case 0x0d: /* Return */
209 outc('\n');
210 size=0; //Kill the while loop
211 break;
212 case'\t':
213 {
214 if (l_completion.size()) {
215 it_completion ++;
216 if (it_completion == l_completion.end()) it_completion = l_completion.begin();
217 } else {
218 // build new completion list
219 // Lines starting with CD will only get directories in the list
220 bool dir_only = (strncasecmp(line,"CD ",3)==0);
221
222 // get completion mask
223 char *p_completion_start = strrchr(line, ' ');
224
225 if (p_completion_start) {
226 p_completion_start ++;
227 completion_index = (Bit16u)(str_len - strlen(p_completion_start));
228 } else {
229 p_completion_start = line;
230 completion_index = 0;
231 }
232
233 char *path;
234 if ((path = strrchr(line+completion_index,'\\'))) completion_index = (Bit16u)(path-line+1);
235 if ((path = strrchr(line+completion_index,'/'))) completion_index = (Bit16u)(path-line+1);
236
237 // build the completion list
238 char mask[DOS_PATHLENGTH] = {0};
239 if (strlen(p_completion_start) + 3 >= DOS_PATHLENGTH) {
240 //Beep;
241 break;
242 }
243 if (p_completion_start) {
244 safe_strncpy(mask, p_completion_start,DOS_PATHLENGTH);
245 char* dot_pos=strrchr(mask,'.');
246 char* bs_pos=strrchr(mask,'\\');
247 char* fs_pos=strrchr(mask,'/');
248 char* cl_pos=strrchr(mask,':');
249 // not perfect when line already contains wildcards, but works
250 if ((dot_pos-bs_pos>0) && (dot_pos-fs_pos>0) && (dot_pos-cl_pos>0))
251 strncat(mask, "*",DOS_PATHLENGTH - 1);
252 else strncat(mask, "*.*",DOS_PATHLENGTH - 1);
253 } else {
254 strcpy(mask, "*.*");
255 }
256
257 RealPt save_dta=dos.dta();
258 dos.dta(dos.tables.tempdta);
259
260 bool res = DOS_FindFirst(mask, 0xffff & ~DOS_ATTR_VOLUME);
261 if (!res) {
262 dos.dta(save_dta);
263 break; // TODO: beep
264 }
265
266 DOS_DTA dta(dos.dta());
267 char name[DOS_NAMELENGTH_ASCII];Bit32u sz;Bit16u date;Bit16u time;Bit8u att;
268
269 std::list<std::string> executable;
270 while (res) {
271 dta.GetResult(name,sz,date,time,att);
272 // add result to completion list
273
274 char *ext; // file extension
275 if (strcmp(name, ".") && strcmp(name, "..")) {
276 if (dir_only) { //Handle the dir only case different (line starts with cd)
277 if(att & DOS_ATTR_DIRECTORY) l_completion.push_back(name);
278 } else {
279 ext = strrchr(name, '.');
280 if (ext && (strcmp(ext, ".BAT") == 0 || strcmp(ext, ".COM") == 0 || strcmp(ext, ".EXE") == 0))
281 // we add executables to the a seperate list and place that list infront of the normal files
282 executable.push_front(name);
283 else
284 l_completion.push_back(name);
285 }
286 }
287 res=DOS_FindNext();
288 }
289 /* Add excutable list to front of completion list. */
290 std::copy(executable.begin(),executable.end(),std::front_inserter(l_completion));
291 it_completion = l_completion.begin();
292 dos.dta(save_dta);
293 }
294
295 if (l_completion.size() && it_completion->length()) {
296 for (;str_index > completion_index; str_index--) {
297 // removes all characters
298 outc(8); outc(' '); outc(8);
299 }
300
301 strcpy(&line[completion_index], it_completion->c_str());
302 len = (Bit16u)it_completion->length();
303 str_len = str_index = completion_index + len;
304 size = CMD_MAXLINE - str_index - 2;
305 DOS_WriteFile(STDOUT, (Bit8u *)it_completion->c_str(), &len);
306 }
307 }
308 break;
309 case 0x1b: /* ESC */
310 //write a backslash and return to the next line
311 outc('\\');
312 outc('\n');
313 *line = 0; // reset the line.
314 if (l_completion.size()) l_completion.clear(); //reset the completion list.
315 this->InputCommand(line); //Get the NEW line.
316 size = 0; // stop the next loop
317 str_len = 0; // prevent multiple adds of the same line
318 break;
319 default:
320 if (l_completion.size()) l_completion.clear();
321 if(str_index < str_len && true) { //mem_readb(BIOS_KEYBOARD_FLAGS1)&0x80) dev_con.h ?
322 outc(' ');//move cursor one to the right.
323 Bit16u a = str_len - str_index;
324 Bit8u* text=reinterpret_cast<Bit8u*>(&line[str_index]);
325 DOS_WriteFile(STDOUT,text,&a);//write buffer to screen
326 outc(8);//undo the cursor the right.
327 for(Bitu i=str_len;i>str_index;i--) {
328 line[i]=line[i-1]; //move internal buffer
329 outc(8); //move cursor back (from write buffer to screen)
330 }
331 line[++str_len]=0;//new end (as the internal buffer moved one place to the right
332 size--;
333 };
334
335 line[str_index]=c;
336 str_index ++;
337 if (str_index > str_len){
338 line[str_index] = '\0';
339 str_len++;
340 size--;
341 }
342 DOS_WriteFile(STDOUT,&c,&n);
343 break;
344 }
345 }
346
347 if (!str_len) return;
348 str_len++;
349
350 // remove current command from history if it's there
351 if (current_hist) {
352 current_hist=false;
353 l_history.pop_front();
354 }
355
356 // add command line to history
357 l_history.push_front(line); it_history = l_history.begin();
358 if (l_completion.size()) l_completion.clear();
359 }
360
361 std::string full_arguments = "";
362 bool DOS_Shell::Execute(char * name,char * args) {
363 /* return true => don't check for hardware changes in do_command
364 * return false => check for hardware changes in do_command */
365 char fullname[DOS_PATHLENGTH+4]; //stores results from Which
366 char* p_fullname;
367 char line[CMD_MAXLINE];
368 if(strlen(args)!= 0){
369 if(*args != ' '){ //put a space in front
370 line[0]=' ';line[1]=0;
371 strncat(line,args,CMD_MAXLINE-2);
372 line[CMD_MAXLINE-1]=0;
373 }
374 else
375 {
376 safe_strncpy(line,args,CMD_MAXLINE);
377 }
378 }else{
379 line[0]=0;
380 };
381
382 /* check for a drive change */
383 if (((strcmp(name + 1, ":") == 0) || (strcmp(name + 1, ":\\") == 0)) && isalpha(*name))
384 {
385 if (!DOS_SetDrive(toupper(name[0])-'A')) {
386 WriteOut(MSG_Get("SHELL_EXECUTE_DRIVE_NOT_FOUND"),toupper(name[0]));
387 }
388 return true;
389 }
390 /* Check for a full name */
391 p_fullname = Which(name);
392 if (!p_fullname) return false;
393 strcpy(fullname,p_fullname);
394 const char* extension = strrchr(fullname,'.');
395
396 /*always disallow files without extension from being executed. */
397 /*only internal commands can be run this way and they never get in this handler */
398 if(extension == 0)
399 {
400 //Check if the result will fit in the parameters. Else abort
401 if(strlen(fullname) >( DOS_PATHLENGTH - 1) ) return false;
402 char temp_name[DOS_PATHLENGTH+4],* temp_fullname;
403 //try to add .com, .exe and .bat extensions to filename
404
405 strcpy(temp_name,fullname);
406 strcat(temp_name,".COM");
407 temp_fullname=Which(temp_name);
408 if (temp_fullname) { extension=".com";strcpy(fullname,temp_fullname); }
409
410 else
411 {
412 strcpy(temp_name,fullname);
413 strcat(temp_name,".EXE");
414 temp_fullname=Which(temp_name);
415 if (temp_fullname) { extension=".exe";strcpy(fullname,temp_fullname);}
416
417 else
418 {
419 strcpy(temp_name,fullname);
420 strcat(temp_name,".BAT");
421 temp_fullname=Which(temp_name);
422 if (temp_fullname) { extension=".bat";strcpy(fullname,temp_fullname);}
423
424 else
425 {
426 return false;
427 }
428
429 }
430 }
431 }
432
433 if (strcasecmp(extension, ".bat") == 0)
434 { /* Run the .bat file */
435 /* delete old batch file if call is not active*/
436 bool temp_echo=echo; /*keep the current echostate (as delete bf might change it )*/
437 if(bf && !call) delete bf;
438 bf=new BatchFile(this,fullname,name,line);
439 echo=temp_echo; //restore it.
440 }
441 else
442 { /* only .bat .exe .com extensions maybe be executed by the shell */
443 if(strcasecmp(extension, ".com") !=0)
444 {
445 if(strcasecmp(extension, ".exe") !=0) return false;
446 }
447 /* Run the .exe or .com file from the shell */
448 /* Allocate some stack space for tables in physical memory */
449 reg_sp-=0x200;
450 //Add Parameter block
451 DOS_ParamBlock block(SegPhys(ss)+reg_sp);
452 block.Clear();
453 //Add a filename
454 RealPt file_name=RealMakeSeg(ss,reg_sp+0x20);
455 MEM_BlockWrite(Real2Phys(file_name),fullname,(Bitu)(strlen(fullname)+1));
456
457 /* HACK: Store full commandline for mount and imgmount */
458 full_arguments.assign(line);
459
460 /* Fill the command line */
461 CommandTail cmdtail;
462 cmdtail.count = 0;
463 memset(&cmdtail.buffer,0,126); //Else some part of the string is unitialized (valgrind)
464 if (strlen(line)>126) line[126]=0;
465 cmdtail.count=(Bit8u)strlen(line);
466 memcpy(cmdtail.buffer,line,strlen(line));
467 cmdtail.buffer[strlen(line)]=0xd;
468 /* Copy command line in stack block too */
469 MEM_BlockWrite(SegPhys(ss)+reg_sp+0x100,&cmdtail,128);
470 /* Parse FCB (first two parameters) and put them into the current DOS_PSP */
471 Bit8u add;
472 FCB_Parsename(dos.psp(),0x5C,0x00,cmdtail.buffer,&add);
473 FCB_Parsename(dos.psp(),0x6C,0x00,&cmdtail.buffer[add],&add);
474 block.exec.fcb1=RealMake(dos.psp(),0x5C);
475 block.exec.fcb2=RealMake(dos.psp(),0x6C);
476 /* Set the command line in the block and save it */
477 block.exec.cmdtail=RealMakeSeg(ss,reg_sp+0x100);
478 block.SaveData();
479 #if 0
480 /* Save CS:IP to some point where i can return them from */
481 Bit32u oldeip=reg_eip;
482 Bit16u oldcs=SegValue(cs);
483 RealPt newcsip=CALLBACK_RealPointer(call_shellstop);
484 SegSet16(cs,RealSeg(newcsip));
485 reg_ip=RealOff(newcsip);
486 #endif
487 /* Start up a dos execute interrupt */
488 reg_ax=0x4b00;
489 //Filename pointer
490 SegSet16(ds,SegValue(ss));
491 reg_dx=RealOff(file_name);
492 //Paramblock
493 SegSet16(es,SegValue(ss));
494 reg_bx=reg_sp;
495 SETFLAGBIT(IF,false);
496 CALLBACK_RunRealInt(0x21);
497 /* Restore CS:IP and the stack */
498 reg_sp+=0x200;
499 #if 0
500 reg_eip=oldeip;
501 SegSet16(cs,oldcs);
502 #endif
503 }
504 return true; //Executable started
505 }
506
507
508
509
510 static const char * bat_ext=".BAT";
511 static const char * com_ext=".COM";
512 static const char * exe_ext=".EXE";
513 static char which_ret[DOS_PATHLENGTH+4];
514
515 char * DOS_Shell::Which(char * name) {
516 size_t name_len = strlen(name);
517 if(name_len >= DOS_PATHLENGTH) return 0;
518
519 /* Parse through the Path to find the correct entry */
520 /* Check if name is already ok but just misses an extension */
521
522 if (DOS_FileExists(name)) return name;
523 /* try to find .com .exe .bat */
524 strcpy(which_ret,name);
525 strcat(which_ret,com_ext);
526 if (DOS_FileExists(which_ret)) return which_ret;
527 strcpy(which_ret,name);
528 strcat(which_ret,exe_ext);
529 if (DOS_FileExists(which_ret)) return which_ret;
530 strcpy(which_ret,name);
531 strcat(which_ret,bat_ext);
532 if (DOS_FileExists(which_ret)) return which_ret;
533
534
535 /* No Path in filename look through path environment string */
536 char path[DOS_PATHLENGTH];std::string temp;
537 if (!GetEnvStr("PATH",temp)) return 0;
538 const char * pathenv=temp.c_str();
539 if (!pathenv) return 0;
540 pathenv=strchr(pathenv,'=');
541 if (!pathenv) return 0;
542 pathenv++;
543 Bitu i_path = 0;
544 while (*pathenv) {
545 /* remove ; and ;; at the beginning. (and from the second entry etc) */
546 while(*pathenv && (*pathenv ==';'))
547 pathenv++;
548
549 /* get next entry */
550 i_path = 0; /* reset writer */
551 while(*pathenv && (*pathenv !=';') && (i_path < DOS_PATHLENGTH) )
552 path[i_path++] = *pathenv++;
553
554 if(i_path == DOS_PATHLENGTH) {
555 /* If max size. move till next ; and terminate path */
556 while(*pathenv != ';')
557 pathenv++;
558 path[DOS_PATHLENGTH - 1] = 0;
559 } else path[i_path] = 0;
560
561
562 /* check entry */
563 if(size_t len = strlen(path)){
564 if(len >= (DOS_PATHLENGTH - 2)) continue;
565
566 if(path[len - 1] != '\\') {
567 strcat(path,"\\");
568 len++;
569 }
570
571 //If name too long =>next
572 if((name_len + len + 1) >= DOS_PATHLENGTH) continue;
573 strcat(path,name);
574
575 strcpy(which_ret,path);
576 if (DOS_FileExists(which_ret)) return which_ret;
577 strcpy(which_ret,path);
578 strcat(which_ret,com_ext);
579 if (DOS_FileExists(which_ret)) return which_ret;
580 strcpy(which_ret,path);
581 strcat(which_ret,exe_ext);
582 if (DOS_FileExists(which_ret)) return which_ret;
583 strcpy(which_ret,path);
584 strcat(which_ret,bat_ext);
585 if (DOS_FileExists(which_ret)) return which_ret;
586 }
587 }
588 return 0;
589 }