-
Notifications
You must be signed in to change notification settings - Fork 2
/
sx_core.c
2582 lines (2047 loc) · 44.5 KB
/
sx_core.c
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sx_core.c
+------------------------------------+
| SamaruX - Unix like shell for CP/M |
+------------------------------------+
Copyright (c) 2007-2023 Miguel I. Garcia Lopez.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Contact:
www.floppysoftware.es
cpm-connections.blogspot.com
Revisions:
11-May-2007 : MGL / Initial version
28-Jul-2011 : MGL / Added support to UX (User numbers in file names...)
12-Nov-2013 : MGL / Added redirection output support with '>' in command line.
18-Nov-2013 : MGL / Uses the new UX
11-Dec-2013 : MGL / Added man command
03-Jun-2014 : MGL / LS rewritten.
08 Dec 2014 : Removed redirection code. Now uses stdio.h and redir.h
19 Dec 2014 : Removed ; stuff (multiple commands in one line - it was buggy).
19 Dec 2014 : Rewritten some commands as external c files: ascii, cd,
clear, dump, echo, ls, man, prompt, pwd.
19 Dec 2014 : Added cat command.
20 Dec 2014 : Added cp command.
21 Dec 2014 : Added rm command.
21 Dec 2014 : RtSortFcx was buggy, rewritten.
21 Dec 2014 : Added mv command.
22 Dec 2014 : Added env command and preliminary environment variables support.
23 Dec 2014 : Added chmod command.
24 Dec 2014 : Now Error() returns 1.
24 Dec 2014 : Added ver command.
25 Dec 2014 : Added batch support.
28 Dec 2014 : Added support for $variables in command line.
28 Dec 2014 : Rewritten commands as external c files: exit, login, history.
28 Dec 2014 : Now uses its own Redir().
29 Dec 2014 : Added command name in errors.
29 Dec 2014 : Added pipe support.
30 Dec 2014 : Now Error() returns -1.
30 Dec 2014 : Main shell functions re-written to support recursivity for batch.
31 Dec 2014 : Added more command.
31 Dec 2014 : Added comments support in command line with #.
02 Jan 2015 : Added profile execution on start.
02 Jan 2015 : Removed prompt command. Now prompt uses PROMPT environment variable.
03 Jan 2015 : Automatic console set-up.
03 Jan 2015 : Added escape sequences support to prompt.
04 Jan 2015 : Added MakeFcx function.
04 Jan 2015 : Added alias substitution.
04 Jan 2015 : Added cpm command.
04 Jan 2015 : Added -q command (quietly, no verbose) to support return from cpm command.
06 Jan 2015 : Added some error messages.
06 Jan 2015 : Added df command.
08 Jan 2015 : Added some debug code. Changed the name of some global variables.
16 Jan 2015 : Adapted to last changes in MESCC (SIZEOF_??? stuff).
16 Jan 2015 : Changed argument parsing code and # comment stuff.
19 Jan 2015 : Added command exit code control & pseudo-variable $?.
19 Jan 2015 : Added key table contol (for env & alias).
19 Jan 2015 : Now batch variables are not in the environment table.
20 Jan 2015 : Added read command. Added ErrorFindVar().
11 Feb 2015 : Removed login command. Reworked some code in profiling and the start message.
14 Feb 2015 : A lot of cosmethic changes.
15 Feb 2015 : Added if & goto commands. Added KeyFree(), ReadFile().
16 Feb 2015 : Do profiling no verbose.
18 Feb 2015 : Minor changes in ReadLine().
18 Feb 2015 : Work on temporary disk drive and pipe filenames (CP/M 3).
19 Feb 2015 : Added grep command. Added single quotes (') support to ParseArgs().
Added profile to CP/M mode.
23 Feb 2015 : Added support for $# pseudo-variable (batch). Added IsSpace().
26 Feb 2015 : Removed sv_tty_rows & sv_tty_cols. Added TermRows() & TermCols().
Added COLUMNS and LINES environment variables.
Added Ctrl-U, Ctrl-X and Ctrl-R support in ReadLine().
27 Feb 2015 : Added TmpFile(), TmpDir(). Added TMPDIR environment variable.
03 Mar 2015 : Added sort command.
04 Mar 2015 : Added UnParseEx(). Added wc command.
07 Mar 2015 : Added AddPathToFn().
09 Mar 2015 : Added AddTypeToFn().
15 Mar 2015 : Modified ReadFile() to support a last line without ending with CR/LF.
21 Mar 2015 : Now, all alias can be executed with redirection.
Now, alias will run even if both alias and command line have arguments.
22 Mar 2015 : Added HOME environment variable support for CD.
Solved bug in Execute(). In command lines like 'cat file | more ; echo text',
the input pipe was not logically closed after the command more.
30 Mar 2015 : Added external commands support (PRL). Added mem command.
v1.03
02 Apr 2015 : Added tee command.
07 Apr 2015 : Added date command.
09 Apr 2015 : Modified LoadStatus(), SaveStatus(), and restore mode.
Added BinFile(). Added BINDIR environment variable.
Solved bug in AddPathToFn() - now tests if filename has already a path.
Removed TmpDir().
v2.00
20 Aug 2015 : Removed sx_links.h, ExtStdIn(), ExtStdOut() and ExtStdErr(), not needed anymore.
Added version number control in external commands.
v2.01
21 Aug 2015 : Solved bug in version number control.
22 Aug 2015 : Added hook for exit(). Done some code optimizations.
v2.02
03 Sep 2015 : Added support for directory names (see cpm.h and CC_FCX_DIR).
Added diralias command.
Solved bug in Prompt() when user > 9.
Added PrintCWD().
v2.03
04 Sep 2015 : Modified AddPathToFn(): if the filename starts with ':', use
the current path.
v2.04
29 Nov 2015 : Added SX_SAMARUX def. for builtin / external commands.
24 Jan 2016 : Added better support for builtin / external commands.
v2.05
03 Jun 2016 : Added tail and whoami commands.
04 Jun 2016 : Added SX_MINIMAL to include only a minimal # of built-in commands.
05 Jun 2016 : SamaruX defines are now in sx.h.
06 Jun 2016 : Added head, true, and false commands.
14 Jun 2016 : Added tty command.
19 Jun 2016 : Added shift command.
v2.06
27 Nov 2016 : Added '>>' in output redirection. Optimized NULL comparisons.
28 Nov 2016 : Added SX_CMDCPM def. to support cpm command.
16 Dec 2016 : Added sleep command.
17 Dec 2016 : Included rand.h library. Added fortune command.
18 Dec 2016 : Solved bug in Execute().
v2.07
07 Sep 2021 : Fixed typo in error message in batch command.
Fixed issue history saving again command lines retrieved from history.
12 Sep 2021 : Added expr command.
19 Sep 2021 : Avoid output multiple error messages in ExeCommand().
20 Sep 2021 : Keep account of running processes. Added RunPush(), RunPop().
Added ps command.
21 Sep 2021 : Renamed to sx_core. Add CmdAdd().
v3.00
27 Dec 2023 : Added type and uname commands, store version data.
NOTES:
- Don't forget to modify samarux.h when you compile this file.
ISSUES:
- Alias defined with arguments can't be executed with pipes (single alias can).
TO-DO & IDEAS:
- Join mem and ver commands. See uname also.
- Add commands: who, getconf, unalias, dirname, basename, printf.
- Add environment variables: PWD - see command cd in POSIX specification.
- Implement setenv(), unsetenv(), getenv(), putenv() - see POSIX specification.
- Add options in compilation time to build for any CP/M, CP/M 2 or CP/M 3.
*/
/* DEFs FOR MESCC
--------------
*/
#define CC_NO_ARGS /* No command line arguments */
#define CC_FCX /* Enable FCX support (user numbers in file names) */
#define CC_FCX_DIR /* Enable named directories -- see DirToDrvUsr() below */
#define CC_STDIO /* Enable stdin, stdout and stderr */
#define CC_FOPEN_A /* To include "a" and "ab" modes for fopen() */
#define CC_FREAD /* To include fread() */
#define CC_FWRITE /* To include fwrite() */
#define CC_FGETS /* To include fgets() */
#define CC_CONIO_BIOS /* Direct console I/O */
/* DEFs FOR SamaruX
----------------
*/
//#define SX_DEBUG /* Debug */
//#define SX_EXTCMDS /* External commands support */
//#define SX_HKEXIT /* Hook for exit() */
//#define SX_MINIMAL /* Minimal # of built-in commands */
//#define SX_CMDCPM /* Support for cpm command */
/* MESCC LIBRARIES
---------------
*/
#include <mescc.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <mem.h>
#include <cpm.h>
#include <alloc.h>
#include <fileio.h>
#include <printf.h>
#include <fprintf.h>
#include <sprintf.h>
#include <clock.h>
#include <stdbool.h>
#include <rand.h>
#ifdef SX_HKEXIT
#include <setjmp.h>
#endif
/* SAMARUX LIBRARY
---------------
*/
#include "sx.h"
/* SAMARUX SYSTEM VARIABLES
------------------------
*/
int sv_name; /* SamaruX name */
int sv_ver_main; /* SamaruX main version */
int sv_ver_seq; /* SamaruX secondary version */
char *sv_ver_date; /* SamaruX version date */
int sv_cpmver; /* CP/M version */
int sv_drive; /* Current drive: 0 .. 15 */
int sv_user; /* Current user: 0 .. 15 */
/* char sv_tmp_du[FILENAME_MAX]; */ /* Temporary directory name */
char sv_tmp_fn[FILENAME_MAX]; /* Temporary file name */
int *sv_hist; /* char *sv_hist[] - Commands line history entries */
int *sv_env_name; /* char *sv_env_name[] - Name of environment variables */
int *sv_env_value; /* char *sv_env_value[] - Value of environment variables */
int *sv_alias_name; /* char *sv_alias_name[] - Name of aliases */
int *sv_alias_value; /* char *sv_alias_value[] - Value of aliases */
int *sv_dir_path; /* char *sv_dir_path[] - Path */
int *sv_dir_alias; /* char *sv_dir_alias[] - Alias */
int sv_flg_hist; /* NZ to not save last command in history */
int sv_flg_buf; /* NZ to not delete command line buffer */
int sv_flg_quit; /* NZ to quit shell */
int sv_flg_cpm; /* NZ if CP/M mode */
int sv_batch; /* NZ if BATCH is running */
BYTE sv_fcx[UX_FCX_SIZ]; /* FCX # 1 */
BYTE sv_fcx_two[UX_FCX_SIZ]; /* FCX # 2 */
BYTE *sv_dma; /* DMA */
BYTE *sv_fcxbuf; /* Buffer for file search */
int sv_cmd_max; /* # of built-in commands */
WORD sv_cmd_name[SX_MAX_CMD]; /* Array of built-in command names */
WORD sv_cmd_fun[SX_MAX_CMD]; /* Array of built-in command functions */
WORD *sv_run_name; /* char *sv_run_name[] - Name of running commands */
int sv_run_exit; /* Exit code of last executed command */
char sv_ed_buf[SX_LINESIZE]; /* Buffer for line editing */
char sv_pipe_in[FILENAME_MAX]; /* File name for input pipe */
char sv_pipe_out[FILENAME_MAX]; /* File name for output pipe */
#ifdef SX_HKEXIT
/* HOOK FOR exit()
---------------
*/
jmp_buf sx_exit_jpbf; /* jmp_buf for hook */
int sx_exit_code; /* exit code */
#endif
/* BUILT-IN COMMANDS
-----------------
*/
#define SX_SAMARUX
#ifdef SX_HKEXIT
#define exit HookExit
#endif
#include <sx_alias.c>
#ifndef SX_MINIMAL
#include <sx_ascii.c>
#endif
#include <sx_batch.c>
#include <sx_built.c>
#ifndef SX_MINIMAL
#include <sx_cat.c>
#endif
#include <sx_cd.c>
#ifndef SX_MINIMAL
#include <sx_chmod.c>
#endif
#include <sx_clear.c>
#ifndef SX_MINIMAL
#include <sx_cp.c>
#ifdef SX_CMDCPM
#include <sx_cpm.c>
#endif
#include <sx_date.c>
#include <sx_df.c>
#include <sx_diral.c>
#include <sx_dump.c>
#endif
#include <sx_echo.c>
#ifndef SX_MINIMAL
#include <sx_ed.c>
#endif
#include <sx_env.c>
#include <sx_exit.c>
#ifndef SX_MINIMAL
#include <sx_expr.c>
#endif
#include <sx_false.c>
#ifndef SX_MINIMAL
#include <sx_fortu.c>
#endif
#include <sx_goto.c>
#ifndef SX_MINIMAL
#include <sx_grep.c>
#include <sx_head.c>
#endif
#include <sx_hist.c>
#include <sx_if.c>
#ifndef SX_MINIMAL
#include <sx_ls.c>
#include <sx_man.c>
#endif
#include <sx_mem.c>
#ifndef SX_MINIMAL
#include <sx_more.c>
#include <sx_mv.c>
#include <sx_ps.c>
#endif
#include <sx_pwd.c>
#include <sx_read.c>
#ifndef SX_MINIMAL
#include <sx_rm.c>
#endif
#include <sx_shift.c>
#ifndef SX_MINIMAL
#include <sx_sleep.c>
#include <sx_sort.c>
#include <sx_tail.c>
#include <sx_tee.c>
#endif
#include <sx_true.c>
#ifndef SX_MINIMAL
#include <sx_tty.c>
#include <sx_type.c>
#include <sx_uname.c>
#include <sx_ver.c>
#include <sx_wc.c>
#include <sx_whoam.c>
#endif
#ifdef SX_HKEXIT
#undef exit
#endif
#undef SX_SAMARUX
/* MAIN PROGRAM
------------
*/
main()
{
char *ptr;
/* Setup version data */
sv_name = SX_APPNAME;
sv_ver_main = SX_VERSION;
sv_ver_seq = SX_RELEASE;
sv_ver_date = SX_APPDATE;
#undef SX_APPNAME
#undef SX_VERSION
#undef SX_RELEASE
#undef SX_APPDATE
/* Build commands table */
/*
sv_cmd_max = 0; *** Already initialized to 0
*/
#ifdef SX_ALIAS
CmdAdd("alias", AliasMain);
#endif
#ifdef SX_ASCII
CmdAdd("ascii", AsciiMain);
#endif
#ifdef SX_BATCH
CmdAdd("batch", BatchMain);
#endif
#ifdef SX_BUILTIN
CmdAdd("builtin", BuiltinMain);
#endif
#ifdef SX_CAT
CmdAdd("cat", CatMain);
#endif
#ifdef SX_CD
CmdAdd("cd", CdMain);
#endif
#ifdef SX_CHMOD
CmdAdd("chmod", ChmodMain);
#endif
#ifdef SX_CLEAR
CmdAdd("clear", ClearMain);
#endif
#ifdef SX_CP
CmdAdd("cp", CpMain);
#endif
#ifdef SX_CPM
CmdAdd("cpm", CpmMain);
#endif
#ifdef SX_DATE
CmdAdd("date", DateMain);
#endif
#ifdef SX_DF
CmdAdd("df", DfMain);
#endif
#ifdef SX_DIRALIAS
CmdAdd("diralias", DiralMain);
#endif
#ifdef SX_DUMP
CmdAdd("dump", DumpMain);
#endif
#ifdef SX_ECHO
CmdAdd("echo", EchoMain);
#endif
#ifdef SX_ED
CmdAdd("ed", EdMain);
#endif
#ifdef SX_ENV
CmdAdd("env", EnvMain);
#endif
#ifdef SX_EXIT
CmdAdd("exit", ExitMain);
#endif
#ifdef SX_EXPR
CmdAdd("expr", ExprMain);
#endif
#ifdef SX_FALSE
CmdAdd("false", FalseMain);
#endif
#ifdef SX_FORTUNE
CmdAdd("fortune", FortuneMain);
#endif
#ifdef SX_GOTO
CmdAdd("goto", GotoMain);
#endif
#ifdef SX_GREP
CmdAdd("grep", GrepMain);
#endif
#ifdef SX_HEAD
CmdAdd("head", HeadMain);
#endif
#ifdef SX_HISTORY
CmdAdd("history", HistMain);
#endif
#ifdef SX_IF
CmdAdd("if", IfMain);
#endif
#ifdef SX_LS
CmdAdd("ls", LsMain);
#endif
#ifdef SX_MAN
CmdAdd("man", ManMain);
#endif
#ifdef SX_MEM
CmdAdd("mem", MemMain);
#endif
#ifdef SX_MORE
CmdAdd("more", MoreMain);
#endif
#ifdef SX_MV
CmdAdd("mv", MvMain);
#endif
#ifdef SX_PS
CmdAdd("ps", PsMain);
#endif
#ifdef SX_PWD
CmdAdd("pwd", PwdMain);
#endif
#ifdef SX_READ
CmdAdd("read", ReadMain);
#endif
#ifdef SX_RM
CmdAdd("rm", RmMain);
#endif
#ifdef SX_SHIFT
CmdAdd("shift", ShiftMain);
#endif
#ifdef SX_SLEEP
CmdAdd("sleep", SleepMain);
#endif
#ifdef SX_SORT
CmdAdd("sort", SortMain);
#endif
#ifdef SX_TAIL
CmdAdd("tail", TailMain);
#endif
#ifdef SX_TEE
CmdAdd("tee", TeeMain);
#endif
#ifdef SX_TRUE
CmdAdd("true", TrueMain);
#endif
#ifdef SX_TTY
CmdAdd("tty", TtyMain);
#endif
#ifdef SX_TYPE
CmdAdd("type", TypeMain);
#endif
#ifdef SX_UNAME
CmdAdd("uname", UnameMain);
#endif
#ifdef SX_VER
CmdAdd("ver", VerMain);
#endif
#ifdef SX_WC
CmdAdd("wc", WcMain);
#endif
#ifdef SX_WHOAMI
CmdAdd("whoami", WhoamiMain);
#endif
/* Create some buffers and tables */
/* FCXs */
if(!(sv_fcxbuf = malloc(SX_MAX_FCX * 13)))
return ErrorMem();
/* Environment names */
if(!(sv_env_name = KeyAlloc(SX_MAX_ENV)))
return ErrorMem();
/* Environment values */
if(!(sv_env_value = KeyAlloc(SX_MAX_ENV)))
return ErrorMem();
/* Alias names */
if(!(sv_alias_name = KeyAlloc(SX_MAX_ALIAS)))
return ErrorMem();
/* Alias values */
if(!(sv_alias_value = KeyAlloc(SX_MAX_ALIAS)))
return ErrorMem();
/* Directory names */
if(!(sv_dir_path = KeyAlloc(SX_MAX_DIR)))
return ErrorMem();
/* Directory values */
if(!(sv_dir_alias = KeyAlloc(SX_MAX_DIR)))
return ErrorMem();
/* History */
if(!(sv_hist = KeyAlloc(SX_MAX_HIST)))
return ErrorMem();
/* Running commands */
if(!(sv_run_name = KeyAlloc(SX_MAX_RUN)))
return ErrorMem();
/* Set-up CP/M */
SetUpCPM();
/*
sv_flg_hist = sv_flg_buf = sv_flg_quit = sv_flg_cpm = sv_run_exit = sv_batch = 0; *** Already initialized to 0
*/
/* Check if there is a CP/M command line */
if(*(ptr = 0x0080))
{
#ifdef SX_CMDCPM
/* Check mode */
if(*ptr > 4 && ptr[1] == ' ' && ptr[2] == '-' && ptr[3] == 'R' && ptr[4] == ' ')
{
/* Restore mode: comeback from a CP/M command */
/* Set EOS */
ptr[1 + (*ptr)] = 0;
/* Load SamaruX status */
LoadStatus(ptr + 5);
}
else
{
#endif
/* CP/M mode: execute command and return to CP/M */
++sv_flg_cpm;
/* Profile */
Profile(SX_PROFCPM);
/* Cook the command line a bit */
ptr[*ptr + 1] = 0;
while(*++ptr)
*ptr = tolower(*ptr);
/* Execute the command line */
Execute(0x0081);
/* Return to CP/M */
return 0;
#ifdef SX_CMDCPM
}
#endif
}
else
{
/* Interactive mode: execute commands until user quits the shell */
printf("%s v%d.%02d / %s\n\n%s\n\n", sv_name, sv_ver_main, sv_ver_seq, sv_ver_date, SX_COPYRGT);
printf("CP/M v%d.%d\n\n", (sv_cpmver >> 4) & 0x0F, sv_cpmver & 0x0F);
printf("%d built-in commands\n\n", sv_cmd_max);
/* Profile */
Profile(SX_PROFILE);
}
/* Interactive shell */
while(1)
{
/* Re-use the buffer (from history)? */
if(sv_flg_buf)
{
sv_flg_buf = 0;
sv_flg_hist = 1; /* Don't re-save this command into history */
}
else
{
sv_ed_buf[0] = 0;
}
/* Show prompt */
Prompt();
/* Read the command line */
ReadLine(sv_ed_buf, SX_LINESIZE-1);
putchar('\n');
ptr = SkipSpaces(sv_ed_buf);
if(*ptr)
{
/* Execute the command line */
Execute(ptr);
/* Quit the shell? */
if(sv_flg_quit)
break;
/* Update history */
UpdateHist();
putchar('\n');
}
}
/* Return to CP/M */
return 0;
}
/* READ LINE
---------
*/
ReadLine(buf, width)
char *buf;
int width;
{
int len, ch;
putstr(buf); len = strlen(buf);
while(1)
{
switch(ch = getch())
{
case 0x08 : /* BS */
case 0x7F : /* DEL */
if(len)
{
putchar(8); putchar(' '); putchar(8);
--len;
}
break;
case 0x15 : /* Ctrl-U : Delete the line */
case 0x18 : /* Ctrl-X : Delete the line */
case 0x12 : /* Ctrl-R : Retype the line */
if(len)
{
putchar('^'); putchar('@' + ch); putchar('\n');
if(ch == 0x12)
{
buf[len] = 0;
putstr(buf);
}
else
*buf = len = 0;
}
break;
case '\r' : /* CR */
case '\n' : /* LF */
buf[len] = 0;
return;
default :
if(len < width && ch >= ' ')
putchar(buf[len++] = ch);
break;
}
}
}
/* ADD BUILT-IN COMMAND
--------------------
*/
CmdAdd(name, entry)
char *name; WORD entry;
{
if(sv_cmd_max < SX_MAX_CMD)
{
sv_cmd_name[sv_cmd_max] = name;
sv_cmd_fun[sv_cmd_max++] = entry;
}
else
{
ErrorFatal("Too many internal commands");
}
}
/* CHANGE ENVIRONMENT VARIABLES IN COMMAND LINE
--------------------------------------------
Return 0 on success, else -1.
*/
ExeVars(argc, argv)
int argc, argv[]; /* char *argv[] */
{
int i;
char *p, *val;
for(i = 0; i < argc; ++i)
{
p = argv[i];
if(*p == '$')
{
val = NULL;
if(p[1])
{
/* Normal environment variables */
val = EnvGet(p + 1);
/* Pseudo-environment variables: $?, $#, $0, $1, etc. */
if(!val && !p[2])
{
if(p[1] == '?')
val = (sv_run_exit ? "1" : "0");
else if(sv_batch)
{
if(p[1] == '#')
val = BatchArgc();
else if(isdigit(p[1]))
val = BatchArg(p[1] - '0');
}
}
}
if(val)
argv[i] = val;
else
return ErrorFindVar();
}
}
return 0;
}
/* COMMAND LINE REDIRECTION
------------------------
Return number of arguments on success, else -1.
*/
ExeRedir(argc, argv, pipe_in, pipe_out)
int argc, argv[], pipe_in, pipe_out; /* char *argv[] */
{
int i, k;
char *p, *fnin, *fnout;
bool fnout_append;
FILE *fp;
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir START]");
#endif
fnin = fnout = NULL;
for(i = 0; i < argc; ++i)
{
p = argv[i];
if(*p == '<' && !p[1])
{
if(i > 0 && i < argc - 1 && !fnin && !pipe_in)
fnin = argv[i + 1];
else
{
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir ERR <]");
#endif
return ErrorRedir();
}
}
else if(*p == '>' && (!p[1] || (p[1] == '>' && !p[2])))
{
if(i > 0 && i < argc - 1 && !fnout && !pipe_out)
{
fnout = argv[i + 1]; fnout_append = p[1];
}
else
{
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir ERR >]");
#endif
return ErrorRedir();
}
}
else
continue;
argc -= 2;
for(k = i--; k < argc; ++k)
argv[k] = argv[k + 2];
}
if(pipe_in)
fnin = sv_pipe_in;
if(pipe_out)
fnout = sv_pipe_out;
if(fnin)
{
if((fp = fopen(fnin, "r")))
stdin = fp;
else
{
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir ERR fnin %s]", fnin);
#endif
return ErrorRedir();
}
}
if(fnout)
{
if((fp = fopen(fnout, (fnout_append ? "a" : "w"))))
stdout = fp;
else
{
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir ERR fnout %s]", fnout);
#endif
return ErrorRedir();
}
}
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedir END]\n");
#endif
return argc;
}
/* STOP COMMAND LINE REDIRECTION
-----------------------------
*/
ExeRedirStop(pipe_in, pipe_out)
int pipe_in, pipe_out;
{
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedirStop START]");
#endif
if(stdin)
{
fclose(stdin);
if(pipe_in)
remove(sv_pipe_in);
}
if(stdout)
{
fclose(stdout);
if(pipe_out)
{
remove(sv_pipe_in);
rename(sv_pipe_out, sv_pipe_in);
}
}
stdin = stdout = NULL;
#ifdef SX_DEBUG
fprintf(stderr, "[ExeRedirStop END]\n");
#endif
}
/* EXECUTE SINGLE COMMAND
----------------------
Return command exit code on success, else -1.
*/