This repository was archived by the owner on Sep 3, 2019. It is now read-only.
forked from AsmusGerman/ABSShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacroUtils.pas
More file actions
561 lines (478 loc) · 12 KB
/
MacroUtils.pas
File metadata and controls
561 lines (478 loc) · 12 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
Unit MacroUtils;
interface
Uses
MiUtils, MiArchivo, MiTypes, MiResources, BaseUnix, Unix, crt,
strutils, SysUtils, DateUtils, users, unixutil, TDALista, keyboard;
procedure iniciarvariables;
procedure prompt;
procedure analizarEntrada(str: string);
procedure analizarSalida(pEntrada2: string);
function Descifrador(str: string; separator: string): salida;
procedure Lanzador (i : integer);
procedure lanzarExterno(pEntrada: string);
procedure guardarProceso(pEntrada: string; pPid: integer);
procedure waitshell;
procedure actualizarJobs;
procedure aPipe;
procedure dPipe;
procedure initSignal;
Implementation
{
name: iniciarvariables;
@param
@return
* El procedimiento inicializa las variables globales definidas en
* la unit MiTypes para su uso deliberado en ABS-Shell
}
procedure iniciarvariables;
begin
home:='/home';
olddir:=home;
usuarioActual:=fpgetenv('USER');
hostActual:=gethostname;
homeMasUsuarioActual:=(home+'/'+usuarioActual);
midir:=homeMasUsuarioActual;
Entrada2:=' ';
SetLength(LJobs,0);
jIndex:=-1;
bpipe:=false;
end;
{
name: prompt
@param
@return
* El procedimiento se encarga de mostrar el prompt
* en la salida estandar.
}
Procedure prompt;
Begin
write(usuarioActual,'@','-',hostActual,':');
if copy(midir,1,length(homeMasUsuarioActual)) = homeMasUsuarioActual then
write('~',copy(midir,length(homeMasUsuarioActual)+1,length(midir)))
else
write(midir);
if (usuarioActual = 'root') then
write('# ')
else
write('$ ');
end;
{
name: analizarEntrada
@param
* str : string
@return
* Se analiza la variable str en busca de un caracter especial y su
* posicion. En caso de existir alguno, se procede a dividir str
* según la posicion de la primera ocurrencia de dicho caracter.
* Todos los caracteres de posicion menor a la posicion del caracter
* especial son asignados a la variable Entrada.
* Todos los caracteres desde la posicion del caracter especial
* (incluido) hasta el final de str se asignan a la variable Entrada2.
}
procedure analizarEntrada(str: string);
var i,j:integer;
begin
i:=pos('|',str);
j:=pos('>',str);
if (i>0) or (j>0) then
begin
if ((i<j) and (i<>0)) or (j=0) then
begin
Entrada:=copy(str,1,i-2);
Entrada2:=copy(str,i,length(str));
if assignpipe(frente,atras)<>0 then
devolverMensaje('Error creando pipe')
else
bpipe:=true;
end
else
if ((j<i) and (j<>0)) or (i=0) then
begin
Entrada:=copy(str,1,j-2);
Entrada2:=copy(str,j,length(str));
end;
end
else
begin
Entrada:=str;
Entrada2:=' ';
end;
end;
{
name: analizarSalida
@param
* pEntrada2 : string
@return
*El procedimiento recibe la siguiente orden a ejecutar como
*pEntrada2,con la precondicion: pEntrada2 siempre distinto de vacio.
*Analiza el parametro y responde a la busqueda de simbolos especiales
*como: |, >>, > .
*Al finalizar actualiza las variables Entrada y Entrada2.
}
procedure analizarSalida(pEntrada2: string);
var
i:integer;
e:string;
begin
if pEntrada2 <> ' ' then
begin
if copy(pEntrada2,1,1)='|' then
begin
if devolverInterno(Entrada) <> -1 then
begin
if High(dat)>0 then
begin
for i:=low(dat) to High(dat) do
fpWrite(atras,dat[i],1);
setlength(dat,0);
fpclose(atras);
end;
end;
analizarEntrada(copy(pEntrada2,3,length(pEntrada2)));
end
else if (copy(pEntrada2,1,2)='>>') then
begin
e:=Entrada;
analizarEntrada(copy(pEntrada2,4,length(pEntrada2)));
if devolverInterno(e) <> -1 then
begin
escribirArchivo(copy(pEntrada2,4,length(pEntrada2)),dat);
setlength(dat,0);
end;
end
else if (copy(pEntrada2,1,1)='>') then
begin
if devolverInterno(e) <> -1 then
begin
reEscribirArchivo(copy(pEntrada2,3,length(pEntrada2)),dat);
setlength(dat,0);
end;
end;
end
else
begin
if (bpipe) and (devolverInterno(Entrada) <> -1) then
begin
setlength(dat,0);
for i:=low(dat) to High(dat) do
fpRead(frente,dat[i],1);
fpclose(frente);
end;
end;
analizarEntrada(copy(pEntrada2,3,length(pEntrada2)));
end;
{
name: Descifrador
@param
* str : string
* separator : string
@return
* Arreglo de cadenas de caracteres
* La funcion genera un arreglo de cadenas de caracteres obtenidos
* mediante la separación indicada por el parametro separador.
* Por ejemplo:
* Descifrador('mils -l';' ');
*
* En el ejemplo la funcion devolvería un arreglo de longitud dos. El
* primer elemento con 'mils' y el segundo con '-l'.
}
function Descifrador(str: string; separator: string): salida;
var
resultado: salida;
begin
SetLength(resultado,1);
if Pos(separator, str) > 0 then
resultado:= ConstArray(Copy(str, 1, Pos(separator, str) - 1),
Descifrador(Copy(str,Pos(separator, str)+1,Length(str)),' '))
else
resultado[Low(resultado)]:= str;
Descifrador:=resultado;
end;
{
name: Lanzador
@param
* i: integer
@return
* El parametro i representa el comando interno a lanzar.
* Por ejemplo si i = 1 representa mils -l.
* A la variable Entrada la transforma en un vector, si existe ' '
* la longitud del vector sera 2, de manera que
* en la primera posicion se ecuentra el numero del comando
* y en la segunda la ruta a la cual se quiere aplicar dicho comando.
* si no existe ' ', la longitud del vestor sera 1, significa que el
* comando se aplicara en el directorio actual.
* en el caso de que exista algun simbolo especial,
*realiza la operacion correspondiente.
}
procedure Lanzador(i : integer);
var
clave: salida;
begin
clave:=Descifrador(Entrada,' ');
case i of
//mils -l
1 :begin
if (Copy(Entrada2,1,1) = '>' ) or (Copy(Entrada2,1,1) = '|') then
begin
case High(clave) of
1:milsl(' ',true);
2:milsl(clave[2],true);
end;
end
else
case High(clave) of
1:milsl(' ',false);
2:milsl(clave[2],false);
end;
end;
//mils -a
2 :begin
if (Copy(Entrada2,1,1) = '>') or (Copy(Entrada2,1,1) = '|') then
case High(clave) of
1:milsa(' ',true);
2:milsa(clave[2],true);
end
else
case High(clave) of
1:milsa(' ',false);
2:milsa(clave[2],false);
end;
end;
//mils -fR
3 :begin
if (Copy(Entrada2,1,1) = '>') or (Copy(Entrada2,1,1) = '|') then
case High(clave) of
1:milsfR(' ');
2:milsfR(clave[2]);
end;
end;
//micat
4 : case High(clave) of
0:micat;
1:micat1(clave[1]);
2:micat2(clave[1],clave[2]);
end;
//micd
5 : case High(clave) of
0: micd(' ');
1: micd(clave[1]);
end;
//mipwd
6 : mipwd;
//mikill
7 : case High(clave) of
0:mikill('-1','-1');
1:mikill(clave[1],'-1');
2:mikill(clave[1],clave[2]);
end;
//mibg
8 : case High(clave) of
0: mibg('-1');
1: mibg(clave[1]);
end;
//mifg
9 : begin
case High(clave) of
0: mifg('-1');
1: mifg(clave[1]);
end;
mostrar(dat);
waitshell;
end;
//mijobs
10 : mijobs;
end;
end;
{
name: lanzarExterno
@param
* pEntada: string
@return
*Lanza un programa externo que se le pasa como string.
*en el caso de que exista algun simbolo especial,
*realiza la operacion correspondiente.
}
procedure aPipe;
begin
Fpclose(1);
fpdup(atras);
fpClose(frente);
fpClose(atras);
end;
procedure dPipe;
begin
Fpclose(0);
Fpdup(frente);
fpClose(atras);
fpClose(frente);
end;
Procedure DoSig (sig : cint); cdecl;
begin
chterm:=false;
end;
procedure initSignal;
begin
chterm:=true;
new (na);
na^.sa_handler:=sigActionHandler(@DoSig);
fillchar(na^.Sa_Mask,sizeof(na^.sa_mask),#0);
na^.sa_flags:=SA_RESTART or SA_NOCLDSTOP or SA_NOCLDWAIT;
na^.Sa_Restorer:=nil;
end;
procedure lanzarExterno(pEntrada: string);
var
programa: string;
PP: PPchar;
begin
programa:='cd '+midir+'; '+pEntrada;
PP:=CreateShellArgV(programa);
programa:=PP[0];
if Entrada2<>' ' then
begin
if copy(Entrada2,1,1) = '|' then aPipe
else if copy(Entrada2,1,2) = '>>' then
begin
if abrirArchivo('Salida.txt')< 0 then
devolverMensaje ('Error en el archivo!!!');
micat1('Salida.txt');
escribirArchivo(copy(Entrada2,4,length(Entrada2)),dat);
DeleteFile('Salida.txt');
analizarEntrada(copy(Entrada2,4,length(Entrada2)));
end
else if copy(Entrada2,1,1)='>' then
begin
if abrirArchivo(copy(Entrada2,3,length(Entrada2)))< 0 then
devolverMensaje ('Error en el archivo!!!');
analizarEntrada(copy(Entrada2,3,length(Entrada2)));
end;
end
else if bpipe=true then dPipe;
fpExecvp(programa, PP);
end;
{
name: guardarProceso
@param
* pEntrada: string
* pPid: integer
@return
*ABS-Shell guarda los procesos externos en un vector de registros.
*Cada uno de los registros mantienen el nombre, el pid y el estado
*del proceso en ejecucion.
}
procedure guardarProceso(pEntrada: string; pPid: integer);
Begin
setlength(Ljobs,length(Ljobs)+1);
with job do
begin
j_pid:=pPid;
j_estado:='Ejecutando';
if bbg then
j_nombre:=pEntrada+' &'
else
j_nombre:=pEntrada;
end;
Ljobs[high(Ljobs)]:=job;
End;
{
name: waitshell
@param
@return
*Este procedimiento impide al shell seguir el curso de la ejecucion
*normal, siempre que se ejecute un proceso externo en primer plano y
*hasta que el usuario genere una interrupcion mediante las teclas
*de funcion: Supr, Inicio, RePag, AvPag, Fin.
}
procedure waitshell;
var
k : TKeyEvent;
saux : string;
boo : boolean;
begin
InitKeyBoard;
boo:=true;
while (chterm) and (boo) and (not bbg) and (copy(Entrada2,1,1)<>'>')
and (copy(Entrada2,1,2)<>'>>') do
begin
if fpSigAction(SIGCHLD,na,nil) <> 0 then
devolverMensaje('Error de ejecucion en el proceso externo');
K:=PollKeyEvent;
if k<>0 then
begin
k := GetKeyEvent;
k := TranslateKeyEvent(K);
case k of
11544:
begin
str(LJobs[JIndex].j_pid,saux);
mikill(saux,'19');
boo:=false;
end;
11779:
begin
str(LJobs[JIndex].j_pid,saux);
mikill(saux,'9');
boo:=false;
end;
50367744:
begin
str(LJobs[JIndex].j_pid,saux);
mikill(saux,'19');
if JIndex = Low(LJobs) then
JIndex:= High(LJobs)
else
dec(JIndex);
mostrar(dat);
str(LJobs[JIndex].j_pid,saux);
mifg(saux);
mostrar(dat);
mijobs;
end;
50368768:
begin
str(LJobs[JIndex].j_pid,saux);
mikill(saux,'19');
if JIndex = High(LJobs) then
JIndex:= Low(LJobs)
else
inc(JIndex);
mostrar(dat);
str(LJobs[JIndex].j_pid,saux);
mifg(saux);
mostrar(dat);
mijobs;
end;
3849:
begin
str(LJobs[High(Ljobs)].j_pid,saux);
mibg(saux);
break;
end;
end;
end;
end;
DoneKeyboard;
end;
{
name: actualizarJobs
@param
@return
*El procedimiento invocado genera un cambio en el vector de procesos
* activos. Si encuentra un proceso en estado 'Terminado' procede a
* eliminarlo.
}
procedure actualizarJobs;
Var i,j:integer;
Begin
if not (chterm) then
LJobs[High(LJobs)].j_estado:= 'Terminado';
for i:= Low(LJobs) to Length(LJobs)-1 do
if LJobs[i].j_estado= 'Terminado' then
Begin
for j:=i to Length(LJobs)-1 do
if j<Length(LJobs)-1 then
LJobs[j]:=LJobs[j+1]
else
setLength(LJobs,Length(LJobs)-1);
end;
end;
END.