-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.pas
More file actions
117 lines (94 loc) · 1.73 KB
/
shell.pas
File metadata and controls
117 lines (94 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
program shell;
uses BaseUnix, Unix, strings;
type strArr = array[1..200] of string;
procedure prompt;
var i : integer;
begin
writeln;
i := BaseUnix.FpGetuid;
if i = 0 then begin
write ('# ')
end
else begin
write ('$ ');
end; //if
end; //prompt
procedure inLine(var l: string);
begin
readln(l);
end; //inLine
procedure getSeperateCommands(var str0 : string; var seperated : strArr; var argCount : integer;var ampersand : boolean );
var i,j : integer;
currStr : string;
begin
i := 1;
j := 1;
while i <> (length(str0) + 1) do
begin
if str0[i] <> ' ' then
begin
currStr := currStr + str0[i];
if (str0[i+1] = ' ') OR ((i+1) = (length(str0) + 1)) then
begin
seperated[j] := currStr;
inc(j);
currStr := '';
end;
end;
inc(i);
end;
argCount := j-1;
if seperated[argcount] = '&' then
begin
seperated[argCount] := '';
argCount := argCount - 1;
ampersand := true;
end;
end; //getSeperateCommands
procedure run(var commands: strArr; var argCount : integer);
var
PP : PPchar;
P0 : PChar;
i : integer;
begin
i:=1;
GetMem(PP,argCount*SizeOf(Pchar));
while i <= argCount do
begin
P0 := StrAlloc(length(commands[i])+1);
StrPCopy(P0, commands[i]);
PP[i-1] := P0;
inc(i);
end;
PP[i-1] := Nil;
Unix.FpExecVP(commands[1], PP);
end; //run
procedure main;
var
ampersand : boolean;
ln : string;
argCount : integer;
commands : strArr;
pid : longint;
begin
ampersand := false;
prompt;
inLine(ln);
getSeperateCommands(ln, commands, argCount, ampersand);
pid := BaseUnix.FpFork;
if pid = 0 then
begin
run(commands, argCount);
end
else
begin
if ampersand = false then
begin
FpWait(pid);
end
end;
main;
end;
begin
main;
end.