[r65]: Shell / convertLine / Shell.c  Maximize  Restore  History

Download this file

711 lines (564 with data), 14.9 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
Copyright (C) 1995-2010, The AROS Development Team. All rights reserved.
$Id: Shell.c 62 2010-09-29 23:26:20Z agreppin $
The shell program.
*/
/******************************************************************************
NAME
Shell
SYNOPSIS
COMMAND/K/F,FROM
LOCATION
C:
FUNCTION
Start a shell (interactive or background).
INPUTS
COMMAND -- command line to execute
FROM -- script to invoke before user interaction
RESULT
HISTORY
Sep 2010 - rewrite of the convertLine function.
Jul 2010 - improved handling of $ and `: things like cd SYS:Olle/$pelle
work now. Non-alphanumerical var-names must be enclosed in
braces.
Feb 2008 - initial support for .key/bra/ket/dot/dollar/default.
EXAMPLE
Shell FROM S:Startup-Sequence
Starts a shell and executes the startup script.
BUGS
SEE ALSO
Execute, NewShell
INTERNALS
The prompt support is not using SetCurrentDirName() as this function
has improper limitations. More or less the same goes for GetProgramName().
******************************************************************************/
/* TODO:
Alias [] support
Break support (and +(0L) before execution) -- CreateNewProc()?
*/
#include <dos/bptr.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/filesystem.h>
#include <dos/stdio.h>
#include <dos/var.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/lists.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <utility/tagitem.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define DEBUG 1
#include <aros/asmcall.h>
#include <aros/debug.h>
#define SH_GLOBAL_SYSBASE 1
#define SH_GLOBAL_DOSBASE 1
#include <aros/shcommands.h>
#include "Shell.h"
#include "Shell_inline.h" /* inline functions */
struct CommandLineInterface *cli;
AROS_SH1(Shell, 41.3,
AROS_SHA(STRPTR, , COMMAND, /F, NULL))
{
AROS_SHCOMMAND_INIT
struct Process *me = (struct Process *)FindTask(NULL);
STRPTR cmdline = SHArg(COMMAND);
struct InterpreterState is;
LONG error;
D(bug("[Shell] executing\n"));
cli = Cli();
setPath(NULL);
is.cliNumber = me->pr_TaskNum;
cliVarNum("process", is.cliNumber);
if (strcmp(me->pr_Task.tc_Node.ln_Name, "Boot Shell") == 0)
is.isBootShell = TRUE;
else
is.isBootShell = FALSE;
initDefaultInterpreterState(&is);
if (is.isBootShell)
SetPrompt("%N> ");
if (cmdline && cmdline[0] != '\0')
{
struct Redirection rd = {0};
struct CSource cs = { cmdline, cliLen(cmdline), 0 };
if ((error = Redirection_init(&rd)) == 0)
{
D(bug("[Shell] running command: %s\n", cmdline));
error = checkLine(&rd, &cs, &is);
Redirection_release(&rd);
}
}
else
error = interact(&is);
D(bug("[Shell] exiting, error = %d\n", error));
return error ? RETURN_FAIL : RETURN_OK;
AROS_SHCOMMAND_EXIT
}
/* First we execute the script, then we interact with the user */
LONG interact(struct InterpreterState *is)
{
LONG error = 0;
BOOL moreLeft = FALSE;
if (!cli->cli_Background)
{
SetVBuf(Output(), NULL, BUF_FULL, -1);
if (is->isBootShell)
{
PutStr
(
"AROS - The AROS Research Operating System\n"
"Copyright � 1995-2010, The AROS Development Team. "
"All rights reserved.\n"
"AROS is licensed under the terms of the "
"AROS Public License (APL),\n"
"a copy of which you should have received "
"with this distribution.\n"
"Visit http://www.aros.org/ for more information.\n"
);
}
else
Printf("New Shell process %ld\n", is->cliNumber);
SetVBuf(Output(), NULL, BUF_LINE, -1);
}
do {
struct CSource cs = { NULL, 0, 0 };
struct Redirection rd = {0};
if ((error = Redirection_init(&rd)) == 0)
{
cliPrompt(is);
moreLeft = readLine(&cs, cli->cli_CurrentInput);
if (cs.CS_Buffer)
error = checkLine(&rd, &cs, is);
if (!cli->cli_Interactive) /* stop script? */
{
if (error)
moreLeft = FALSE;
if (CheckSignal(SIGBREAKF_CTRL_D))
{
PutStr("SHELL: ***Break\n");
moreLeft = FALSE;
}
}
Redirection_release(&rd);
FreeCSource(&cs);
}
if (moreLeft)
continue;
popInterpreterState(is);
if (!cli->cli_Interactive)
{
Close(cli->cli_CurrentInput);
if (AROS_BSTR_strlen(cli->cli_CommandFile))
{
DeleteFile(AROS_BSTR_ADDR(cli->cli_CommandFile));
AROS_BSTR_setstrlen(cli->cli_CommandFile, 0);
}
if (!cli->cli_Background)
{
cli->cli_CurrentInput = cli->cli_StandardInput;
cli->cli_Interactive = TRUE;
moreLeft = TRUE;
Flush(Output());
Flush(Error());
}
}
} while (moreLeft);
if (cli->cli_Interactive)
{
Printf("Process %ld ending\n", is->cliNumber);
Flush(Output());
}
return error;
}
/* Take care of one command line */
LONG checkLine(struct Redirection *rd, struct CSource *cs,
struct InterpreterState *is)
{
/* The allocation is taken care of by appendString */
struct CSource filtered = { NULL, 0, 0 };
LONG result;
if ((result = convertLine(cs, &filtered, rd, is)) == 0)
{
/* Only a comment or dot command ? */
if (!rd->haveCommand)
{
result = 0;
goto exit;
}
/* stegerg: Set redirection to default in/out handles */
if (rd->haveOutRD || rd->haveAppRD)
{
LONG mode = rd->haveAppRD ? MODE_READWRITE : MODE_NEWFILE;
rd->newOut = Open(rd->outFileName, mode);
if (BADDR(rd->newOut) == NULL)
{
result = IoErr();
PrintFault(result, rd->outFileName);
goto exit;
}
if (rd->haveAppRD)
Seek(rd->newOut, 0, OFFSET_END);
rd->oldOut = SelectOutput(rd->newOut);
}
if (rd->haveInRD)
{
rd->newIn = Open(rd->inFileName, MODE_OLDFILE);
if (BADDR(rd->newIn) == NULL)
{
result = IoErr();
PrintFault(result, rd->inFileName);
goto exit;
}
SelectInput(rd->newIn);
}
cliEcho(rd, filtered.CS_Buffer);
/* OK, we've got a command. Let's execute it! */
result = executeLine(filtered.CS_Buffer, rd, is);
SelectInput(cli->cli_StandardInput);
SelectOutput(cli->cli_StandardOutput);
}
else
PrintFault(result, rd->haveCommand ? rd->commandStr : 0);
exit:
FreeCSource(&filtered);
if (cli->cli_Interactive)
{
Flush(Output());
Flush(Error());
}
return result;
}
BOOL readLine(struct CSource *cs, BPTR inputStream)
{
BOOL comment = FALSE, quoted = FALSE;
LONG letter = EOF, prev = EOF;
TEXT c;
while (inputStream)
{
/* TODO: better use FGets instead of FGetC -> less I/O requests */
letter = FGetC(inputStream);
if (letter == '\n' || letter == EOF)
break;
/* command line preprocessing */
switch (letter)
{
case '"':
if (quoted && prev == '*')
break;
else
quoted = !quoted;
break;
case ';':
comment = !quoted;
break;
default:
if (letter == ' ' || letter == '\t')
{
if (cs->CS_Length == 0) /* ignore leading spaces */
continue;
if (quoted)
break;
if (prev == ' ' || prev == '\t') /* ignore multiple spaces */
continue;
else
letter = ' ';
}
}
prev = letter;
if (comment) /* ignore */
continue;
c = (TEXT)letter;
appendString(cs, &c, 1);
}
#if DEBUG > 0
if (cs->CS_Buffer)
bug("[Shell] readLine: %s\n", cs->CS_Buffer);
#endif
return letter == EOF ? FALSE : TRUE;
}
void unloadCommand(BPTR commandSeg, struct ShellState *ss)
{
if (ss->homeDirChanged)
{
UnLock(SetProgramDir(ss->oldHomeDir));
ss->homeDirChanged = FALSE;
}
if (!cli->cli_Module)
return;
if (ss->residentCommand)
{
struct Segment *residentSeg = (struct Segment *)BADDR(commandSeg);
Forbid();
/* Decrease usecount */
if (residentSeg->seg_UC > 0)
residentSeg->seg_UC--;
Permit();
ss->residentCommand = FALSE;
}
else
UnLoadSeg(commandSeg);
}
BPTR loadCommand(STRPTR commandName, struct ShellState *ss)
{
BPTR oldCurDir;
BPTR commandSeg = NULL;
BPTR *paths;
struct Segment *residentSeg;
BOOL absolutePath = strpbrk(commandName, "/:") != NULL;
BPTR file;
/* We check the resident lists only if we do not have an absolute path */
if (!absolutePath)
{
Forbid();
/* Check regular list first... */
residentSeg = FindSegment(commandName, NULL, FALSE);
if (residentSeg == NULL)
{
/* ... then the system list */
residentSeg = FindSegment(commandName, NULL, TRUE);
}
if (residentSeg != NULL)
{
/* Can we use this command? */
if (residentSeg->seg_UC == CMD_INTERNAL || residentSeg->seg_UC >= 0)
{
if (residentSeg->seg_UC >= 0)
residentSeg->seg_UC++;
ss->residentCommand = TRUE;
Permit();
return MKBADDR(residentSeg);
}
}
Permit();
}
ss->residentCommand = FALSE;
oldCurDir = CurrentDir(NULL);
CurrentDir(oldCurDir);
file = Open(commandName, MODE_OLDFILE);
if (!file)
{
if
(
absolutePath || /* If this was an absolute path, we don't check the paths set by
'path' or the C: multiassign */
IoErr() == ERROR_OBJECT_IN_USE /* The object might be exclusively locked */
)
return NULL;
/* Search the command in the path */
for
(
paths = (BPTR *)BADDR(cli->cli_CommandDir);
file == NULL && paths != NULL;
paths = (BPTR *)BADDR(paths[0]) /* Go on with the next path */
)
{
CurrentDir(paths[1]);
file = Open(commandName, MODE_OLDFILE);
}
/* The last resort -- the C: multiassign */
if (!file)
{
commandName -= 2;
file = Open(commandName, MODE_OLDFILE);
}
}
if (file)
{
commandSeg = LoadSeg(commandName);
if (commandSeg)
{
BPTR lock = ParentOfFH(file);
if (lock)
{
ss->oldHomeDir = SetProgramDir(lock);
ss->homeDirChanged = TRUE;
}
}
else
{
struct FileInfoBlock fib;
if (Examine(file, &fib) && fib.fib_Protection & FIBF_SCRIPT)
{
commandSeg = LoadSeg("C:Execute");
if (commandSeg)
{
ss->script = TRUE;
ss->scriptLock = Lock(commandName, SHARED_LOCK);
}
}
else
SetIoErr(ERROR_FILE_NOT_OBJECT);
}
Close(file);
}
CurrentDir(oldCurDir);
return commandSeg;
}
/* Execute one command */
LONG executeLine(STRPTR commandArgs, struct Redirection *rd,
struct InterpreterState *is)
{
BPTR module;
LONG error = 0;
struct ShellState ss = {FALSE};
TEXT cmd[4096];
ss.script = FALSE;
D(bug("[Shell] executeLine: %s %s\n", rd->commandStr, commandArgs));
module = loadCommand(rd->commandStr, &ss);
/* Set command name even if we couldn't load the command to be able to
report errors correctly */
SetProgramName(rd->commandStr);
if (module != NULL)
{
struct Task *me = FindTask(NULL);
STRPTR oldtaskname = me->tc_Node.ln_Name;
BOOL __debug_mem;
LONG mem_before;
ULONG sig_before = ((struct Process *)me)->pr_Task.tc_SigAlloc;
ULONG sig_after;
BYTE sigbit;
ULONG sigmask;
BPTR seglist = ss.residentCommand ? ((struct Segment *)BADDR(module))->seg_Seg : module;
STRPTR dst = cmd, src;
LONG len = 0;
if (ss.script)
{
*dst++ = '"';
NameFromLock(ss.scriptLock, dst, sizeof(cmd));
while (*dst != '\0')
{
++dst;
++len;
}
*dst++ = '"';
*dst++ = ' ';
UnLock(ss.scriptLock);
len += 2;
}
src = commandArgs;
for (; src && *src != '\0'; ++dst, ++src, ++len)
*dst = *src;
*dst = '\0';
D(bug("[Shell] command loaded: len=%d, args=%s\n", len, cmd));
SetIoErr(0); /* Clear error before we execute this command */
SetSignal(0, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
cli->cli_Module = seglist;
me->tc_Node.ln_Name = rd->commandStr;
__debug_mem = FindVar("__debug_mem", LV_VAR) != NULL;
if (__debug_mem)
{
FreeVec(AllocVec((ULONG)(~0ul / 2), MEMF_ANY)); /* Flush memory */
mem_before = AvailMem(MEMF_ANY);
Printf("Available total memory before command execution: %10ld\n", mem_before);
}
cli->cli_ReturnCode = RunCommand(seglist, cli->cli_DefaultStack * CLI_DEFAULTSTACK_UNIT,
cmd, len);
/*
Check if running the command has changed signal bits of the Shell process.
If there is a difference the signals will be set or freed to avoid that
the Shell runs out of free signals.
*/
sig_after = ((struct Process *)me)->pr_Task.tc_SigAlloc;
if (sig_before != sig_after)
{
for (sigbit = 0; sigbit < 32; sigbit++)
{
sigmask = 1L << sigbit;
if ((sig_before & sigmask) && !(sig_after & sigmask))
{
/* Command has deleted signal => set it */
Printf("*** Command returned with freed signal 0x%lx\n", sigmask);
AllocSignal(sigbit);
}
else if (!(sig_before & sigmask) && (sig_after & sigmask))
{
/* Command has set signal => free it */
Printf("*** Command returned with unfreed signal 0x%lx\n", sigmask);
FreeSignal(sigbit);
}
}
}
if (__debug_mem)
{
LONG mem_after;
FreeVec(AllocVec((ULONG)(~0ul / 2), MEMF_ANY)); /* Flush memory */
mem_after = AvailMem(MEMF_ANY);
Printf("Available total memory after command execution: %10ld\n", mem_after);
Printf("Memory difference (before - after): %10ld\n", mem_before - mem_after);
}
me->tc_Node.ln_Name = oldtaskname;
D(bug("[Shell] returned from command: %s\n", rd->commandStr));
unloadCommand(module, &ss);
cli->cli_Result2 = IoErr();
}
else
{
/* Implicit CD ? */
if (rd->haveInRD || rd->haveOutRD || rd->haveAppRD)
return ERROR_TOO_MANY_ARGS;
/* SFS returns ERROR_INVALID_COMPONENT_NAME if you try to open "" */
error = IoErr();
if (error == ERROR_OBJECT_WRONG_TYPE ||
error == ERROR_OBJECT_NOT_FOUND ||
error == ERROR_INVALID_COMPONENT_NAME)
{
BPTR lock = Lock(rd->commandStr, SHARED_LOCK);
if (lock != NULL)
{
struct FileInfoBlock *fib = AllocDosObject(DOS_FIB, NULL);
if (fib != NULL)
{
if (Examine(lock, fib))
{
if (fib->fib_DirEntryType > 0)
{
setPath(lock);
lock = CurrentDir(lock);
}
else
SetIoErr(ERROR_OBJECT_WRONG_TYPE);
}
FreeDosObject(DOS_FIB, fib);
}
/* UnLock the old currentdir */
UnLock(lock);
}
}
if ((error = IoErr()))
{
cli->cli_Result2 = error;
PrintFault(error, rd->commandStr);
}
}
cliVarNum("RC", cli->cli_ReturnCode);
cliVarNum("Result2", cli->cli_Result2);
return error;
}
void setPath(BPTR lock)
{
BPTR dir;
STRPTR buf;
ULONG i;
if (lock == NULL)
dir = CurrentDir(NULL);
else
dir = lock;
for (i = 256; ; i += 256)
{
buf = AllocVec(i, MEMF_ANY);
if (buf == NULL)
break;
if (NameFromLock(dir, buf, i))
{
SetCurrentDirName(buf);
FreeVec(buf);
break;
}
FreeVec(buf);
}
if (lock == NULL)
CurrentDir(dir);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks