-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsteer.c
More file actions
2730 lines (2569 loc) · 103 KB
/
Copy pathsteer.c
File metadata and controls
2730 lines (2569 loc) · 103 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
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
/* Miranda steer.c */
/* Initialisation routines and assorted routines for I/O etc. */
/**************************************************************************
* Copyright (C) Research Software Limited 1985-90. All rights reserved. *
* The Miranda system is distributed as free software under the terms in *
* the file "COPYING" which is included in the distribution. *
* Revised to C11 standard and made 64bit compatible, January 2020 *
* Modernized for C11, improved readability and structure, June 2025 *
*------------------------------------------------------------------------*/
/* this stuff is to get the time-last-modified of files */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /* For creat() */
#include <stdio.h> /* For FILE, printf, fprintf, etc. */
#include <stdlib.h> /* For exit, getenv, malloc, free */
#include <string.h> /* For strcpy, strcat, strcmp, strlen, strstr, rindex, index */
#include <unistd.h> /* For isatty, chdir, fork, read, write, close, unlink, getcwd */
#include <signal.h> /* For signal */
#include <setjmp.h> /* For setjmp, longjmp */
#include <ctype.h> /* For isalpha */
#include <termios.h> /* For struct winsize, TIOCGWINSZ */
#include <sys/ioctl.h> /* For ioctl */
#if defined(sparc8) || defined(sparc)
#include <ieeefp.h>
#endif
#include <float.h> /* For DBL_MAX */
/* External declarations from other Miranda system files */
/* These are placeholders; actual content depends on your system. */
#include "data.h"
#include "big.h"
#include "lex.h"
/* It is assumed that 'word' is a type capable of holding pointers and
* integral values, and is correctly sized for the target architecture (e.g., uintptr_t).
* Without data.h, its exact definition is unknown. */
extern struct stat buf; /* see man(2) stat - gets file status */
word nill, Void;
word main_id; /* change to magic scripts 19.11.2013 */
word message, standardout;
word diagonalise, concat, indent_fn, outdent_fn, listdiff_fn;
word shownum1, showbool, showchar, showlist, showstring, showparen, showpair,
showvoid, showfunction, showabstract, showwhat;
/* These buffers might need dynamic allocation or larger static sizes if
* 'pnlim' (pathname limit) or 'BUFSIZE' (general buffer size) is very large.
* Consider using dynamic allocation with error checking for robust path handling. */
char PRELUDE[pnlim + 10];
char STDENV[pnlim + 9];
#define DFLTSPACE 2500000L
#define DFLTDICSPACE 100000L
/* default values for size of heap, dictionary */
word SPACELIMIT = DFLTSPACE;
word DICSPACE = DFLTDICSPACE;
#ifdef CYGWIN
#define DEFAULT_EDITOR "joe +!"
#else
#define DEFAULT_EDITOR "vi +!"
#endif
/* The name of whatever is locally considered to be the default editor - the
user will be able to override this using the `/editor' command.
It is also overridden by shell/environment variable EDITOR if present */
extern FILE *s_out;
int UTF8 = 0;
int UTF8OUT = 0;
extern char *vdate, *host;
extern word version, ND;
extern word *dstack, *stackp;
/* Forward declarations for static functions */
static void allnamescom(void);
static void announce(void);
static int badeditor(void);
static int checkversion(char *);
static void command(void);
static void commandloop(char *);
static void diagnose(const char *);
static void editfile(const char *, int);
static void ed_warn(void);
static void filecopy(const char *);
static void filecp(const char *, const char *);
static void finger(const char *);
static void fixeditor(void);
static void fixexports(void);
static int getln(FILE *, word, char *);
static word isfreeid(word);
static void libfails(void);
static void loadfile(char *);
static void makedump(void);
static void manaction(void);
static void mira_setup(void);
static void missparam(const char *);
static char *mkabsolute(char *);
static word mkincludes(word);
static word mktiny(void);
static void namescom(word);
static void primlib(void);
static word privatise(word);
static void privlib(void);
static word publicise(word);
static word rc_read(char *);
static void rc_write(void);
static int src_update(void);
static void stdlib(void);
static const char *strvers(int);
static int twidth(void);
static void undump(char *);
static int utf8test(void);
static void unfixexports(void);
static void unlinkx(char *);
static void unload(void);
static void v_info(int);
static void xschars(void);
/* Global Variables (consider reducing their scope if possible) */
char *editor = NULL;
word okprel = 0; /* set to 1 when prelude loaded */
word nostdenv = 0; /* if set to 1 mira does not load stdenv at startup */
enum BadEditorStatus {
EDITOR_OK = 0,
EDITOR_BAD = 1
};
enum BadEditorStatus baded = EDITOR_OK; /* see fixeditor() */
char *miralib = NULL;
char *mirahdr, *lmirahdr;
const char *promptstr = "Miranda> ";
const char *obsuffix = "x";
FILE *s_in = NULL;
extern word commandmode; /* true only when reading command-level expressions */
int atobject = 0, atgc = 0, atcount = 0, debug = 0;
word magic = 0; /* set to 1 means script will start with UNIX magic string */
word making = 0; /* set only for mira -make */
word mkexports = 0; /* set only for mira -exports */
word mksources = 0; /* set only for mira -sources */
word make_status = 0; /* exit status of -make */
int compiling = 1;
/* there are two types of MIRANDA process - compiling (the main process) and
subsidiary processes launched for each evaluation - the above flag tells
us which kind of process we are in */
int ideep = 0; /* depth of %include we are at, see mkincludes() */
word SYNERR = 0;
word initialising = 1;
word primenv = NIL;
char *current_script;
word lastexp = UNDEF; /* value of `$$' */
word echoing = 0, listing = 0, verbosity;
word strictif = 1, rechecking = 0;
word errline = 0; /* records position of last error, for editor */
word errs = 0; /* secondary error location, in inserted script, if relevant */
word *cstack;
extern word c;
extern char *dicp, *dicq;
char linebuf[BUFSIZE]; /* used for assorted purposes */
/* NB cannot share with linebuf in lex.c, or !! goes wrong */
static char ebuf[pnlim];
extern word col;
char home_rc[pnlim + 8];
char lib_rc[pnlim + 8];
char *rc_error = NULL;
#define badval(x) (x < 1 || x > 478000000)
jmp_buf env;
#ifdef sparc8
fp_except commonmask = FP_X_INV | FP_X_OFL | FP_X_DZ; /* invalid|ovflo|divzero */
#endif
/* Use _Noreturn for functions that do not return */
_Noreturn void dieclean(void);
_Noreturn void fpe_error(void);
_Noreturn void missparam(const char *s);
void printver(void);
/* Main program entry point */
int main(int argc, char *argv[]) {
word manonly = 0;
char *home, *prs;
int okhome_rc; /* flags valid HOME/.mirarc file present */
char *argv0 = argv[0];
char *initscript;
int badlib = 0;
extern int ARGC;
extern char **ARGV;
extern word newtyps, algshfns;
char *progname = strrchr(argv[0], '/');
if (!progname) {
progname = argv[0];
} else {
progname++;
}
cstack = &manonly;
/* used to indicate the base of the C stack for garbage collection purposes */
verbosity = isatty(0);
/* if(isatty(1)) */
setbuf(stdout, NULL); /* for unbuffered tty output */
if ((home = getenv("HOME"))) {
strcpy(home_rc, home);
if (strcmp(home_rc, "/") == 0) home_rc[0] = 0; /* root is special case */
strcat(home_rc, "/.mirarc");
okhome_rc = rc_read(home_rc);
}
/*setup policy:
if valid HOME/.mirarc found look no further, otherwise try
<miralib>/.mirarc
Complaints - if any .mirarc contained bad data, `announce' complains about
the last such looked at. */
UTF8OUT = UTF8 = utf8test();
while (argc > 1 && argv[1][0] == '-') /* strip off flags */
{
if (strcmp(argv[1], "-stdenv") == 0) nostdenv = 1;
else if (strcmp(argv[1], "-count") == 0) atcount = 1;
else if (strcmp(argv[1], "-list") == 0) listing = 1;
else if (strcmp(argv[1], "-nolist") == 0) listing = 0;
else if (strcmp(argv[1], "-nostrictif") == 0) strictif = 0;
else if (strcmp(argv[1], "-gc") == 0) atgc = 1;
else if (strcmp(argv[1], "-object") == 0) atobject = 1;
else if (strcmp(argv[1], "-lib") == 0) {
argc--, argv++;
if (argc == 1) missparam("lib");
else miralib = argv[1];
} else if (strcmp(argv[1], "-dic") == 0) {
argc--, argv++;
if (argc == 1) missparam("dic");
else if (sscanf(argv[1], "%ld", &DICSPACE) != 1 || badval(DICSPACE))
fprintf(stderr, "mira: bad value after flag \"-dic\"\n"), exit(1);
} else if (strcmp(argv[1], "-heap") == 0) {
argc--, argv++;
if (argc == 1) missparam("heap");
else if (sscanf(argv[1], "%ld", &SPACELIMIT) != 1 || badval(SPACELIMIT))
fprintf(stderr, "mira: bad value after flag \"-heap\"\n"), exit(1);
} else if (strcmp(argv[1], "-editor") == 0) {
argc--, argv++;
if (argc == 1) missparam("editor");
else editor = argv[1], fixeditor();
} else if (strcmp(argv[1], "-hush") == 0) verbosity = 0;
else if (strcmp(argv[1], "-nohush") == 0) verbosity = 1;
else if (strcmp(argv[1], "-exp") == 0 || strcmp(argv[1], "-log") == 0)
fprintf(stderr, "mira: obsolete flag \"%s\"\n"
"use \"-exec\" or \"-exec2\", see manual\n",
argv[1]), exit(1);
else if (strcmp(argv[1], "-exec") == 0) { /* replaces -exp 26.11.2019 */
ARGC = argc - 2, ARGV = argv + 2, magic = 1, verbosity = 0;
} else if (strcmp(argv[1], "-exec2") == 0) { /* version of -exec for debugging CGI scripts */
if (argc <= 2) fprintf(stderr, "incorrect use of -exec2 flag, missing filename\n"), exit(1);
char *logfilname, *p = strrchr(argv[2], '/');
FILE *fil = NULL;
if (!p) p = argv[2]; /* p now holds last component of prog name */
// Allocate enough space for "miralog/" + filename + null terminator
if ((logfilname = (char *)malloc(strlen(p) + 9))) {
sprintf(logfilname, "miralog/%s", p);
fil = fopen(logfilname, "a");
} else {
/* Error handling for malloc fail, assuming 'mallocfail' is external */
/* mallocfail("logfile name"); */
fprintf(stderr, "mira: failed to allocate memory for logfile name\n");
exit(1);
}
/* process requires write permission on local directory "miralog" */
if (fil) dup2(fileno(fil), 2); /* redirect stderr to log file */
else fprintf(stderr, "could not open %s\n", logfilname);
ARGC = argc - 2, ARGV = argv + 2, magic = 1, verbosity = 0;
if (logfilname) free(logfilname); /* Free dynamically allocated memory */
} else if (strcmp(argv[1], "-man") == 0) {
manonly = 1;
break;
} else if (strcmp(argv[1], "-v") == 0) {
printver();
exit(0);
} else if (strcmp(argv[1], "-version") == 0) {
v_info(0);
exit(0);
} else if (strcmp(argv[1], "-V") == 0) {
v_info(1);
exit(0);
} else if (strcmp(argv[1], "-make") == 0) {
making = 1;
verbosity = 0;
} else if (strcmp(argv[1], "-exports") == 0) {
making = mkexports = 1;
verbosity = 0;
} else if (strcmp(argv[1], "-sources") == 0) {
making = mksources = 1;
verbosity = 0;
} else if (strcmp(argv[1], "-UTF-8") == 0) UTF8 = 1;
else if (strcmp(argv[1], "-noUTF-8") == 0) UTF8 = 0;
else
fprintf(stderr, "mira: unknown flag \"%s\"\n", argv[1]), exit(1);
argc--, argv++;
}
if (argc > 2 && !magic && !making) fprintf(stderr, "mira: too many args\n"), exit(1);
if (!miralib) /* no -lib flag */
{
char *m;
/* note search order */
if ((m = getenv("MIRALIB"))) miralib = m;
else if (checkversion(m = "/usr/lib/miralib")) miralib = m;
else if (checkversion(m = "/usr/local/lib/miralib")) miralib = m;
else if (checkversion(m = "miralib")) miralib = m;
else badlib = 1;
}
if (badlib) {
fprintf(stderr, "fatal error: miralib version %s not found\n",
strvers(version));
libfails();
exit(1);
}
if (!okhome_rc) {
if (rc_error == lib_rc) rc_error = NULL;
(void)strcpy(lib_rc, miralib);
(void)strcat(lib_rc, "/.mirarc");
rc_read(lib_rc);
}
if (editor == NULL) { /* .mirarc was absent or unreadable */
editor = getenv("EDITOR");
if (editor == NULL) editor = (char *)DEFAULT_EDITOR; /* Cast for const string */
else strcpy(ebuf, editor), editor = ebuf, fixeditor();
}
if ((prs = getenv("MIRAPROMPT"))) promptstr = prs;
if (getenv("RECHECKMIRA") && !rechecking) rechecking = 1;
if (getenv("NOSTRICTIF")) strictif = 0;
setupdic(); /* used by mkabsolute */
s_in = stdin;
s_out = stdout;
miralib = mkabsolute(miralib); /* protection against "/cd" */
if (manonly) manaction(), exit(0);
(void)strcpy(PRELUDE, miralib);
(void)strcat(PRELUDE, "/prelude");
/* convention - change spelling of "prelude" at each release */
(void)strcpy(STDENV, miralib);
(void)strcat(STDENV, "/stdenv.m");
mira_setup();
if (verbosity) announce();
files = NIL;
undump(PRELUDE);
okprel = 1;
mkprivate(fil_defs(hd[files]));
files = NIL; /* don't wish unload() to unsetids on prelude */
if (!nostdenv) {
undump(STDENV);
while (files != NIL) { /* stdenv may have %include structure */
primenv = alfasort(append1(primenv, fil_defs(hd[files])));
files = tl[files];
}
primenv = alfasort(primenv);
newtyps = files = NIL; /* don't wish unload() to unsetids */
}
if (!magic) rc_write();
echoing = verbosity & listing;
initialising = 0;
if (mkexports) {
/* making=1, to say if recompiling, also to undump as for %include */
word f, argcount = argc - 1;
extern word exports, freeids;
char *s;
setjmp(env); /* will return here on blankerr (via reset) */
while (--argc) { /* where do error messages go?? */
word x = NIL;
s = addextn(1, *++argv);
if (s == dicp) keep(dicp);
undump(s); /* bug, recompile messages goto stdout - FIX LATER */
if (files == NIL || ND != NIL) continue;
if (argcount != 1) printf("%s\n", s);
if (exports != NIL) x = exports;
/* true (if ever) only if just recompiled */
else
for (f = files; f != NIL; f = tl[f]) x = append1(fil_defs(hd[f]), x);
/* method very clumsy, because exports not saved in dump */
if (freeids != NIL) {
word current_free_id = freeids;
while (current_free_id != NIL) {
word n = findid((char *)hd[hd[tl[hd[current_free_id]]]]);
id_type(n) = tl[tl[hd[current_free_id]]];
id_val(n) = the_val(hd[hd[current_free_id]]);
hd[current_free_id] = n;
current_free_id = tl[current_free_id];
}
current_free_id = freeids = typesfirst(freeids);
printf("\t%%free {\n");
while (current_free_id != NIL) {
putchar('\t');
report_type(hd[current_free_id]);
putchar('\n');
current_free_id = tl[current_free_id];
}
printf("\t}\n");
}
for (x = typesfirst(alfasort(x)); x != NIL; x = tl[x]) {
putchar('\t');
report_type(hd[x]);
putchar('\n');
}
}
exit(0);
}
if (mksources) {
extern word oldfiles;
char *s;
word f, x = NIL;
setjmp(env); /* will return here on blankerr (via reset) */
while (--argc)
if (stat((s = addextn(1, *++argv)), &buf) == 0) {
if (s == dicp) keep(dicp);
undump(s);
for (f = files == NIL ? oldfiles : files; f != NIL; f = tl[f])
if (!member(x, (word)get_fil(hd[f])))
x = cons((word)get_fil(hd[f]), x),
printf("%s\n", get_fil(hd[f]));
}
exit(0);
}
if (making) {
extern word oldfiles;
char *s;
setjmp(env); /* will return here on blankerr (via reset) */
while (--argc) { /* where do error messages go?? */
s = addextn(1, *++argv);
if (s == dicp) keep(dicp);
undump(s);
if (ND != NIL || (files == NIL && oldfiles != NIL)) {
if (make_status == 1) make_status = 0;
make_status = strcons(s, make_status);
}
/* keep list of source files with error-dumps */
}
if (tag[make_status] == STRCONS) {
word h = 0, maxw = 0, w, n;
printf("errors or undefined names found in:-\n");
while (make_status) { /* reverse to get original order */
h = strcons(hd[make_status], h);
w = strlen((char *)hd[h]);
if (w > maxw) maxw = w;
make_status = tl[make_status];
}
maxw++;
n = 78 / maxw;
w = 0;
while (h) {
printf("%*s%s", (int)maxw, (char *)hd[h], (++w % n) ? "" : "\n");
h = tl[h];
}
if (w % n) printf("\n");
make_status = 1;
}
exit(make_status);
}
initscript = (argc == 1) ? "script.m" : (magic ? argv[1] : addextn(1, argv[1]));
if (initscript == dicp) keep(dicp);
#if defined(sparc8)
fpsetmask(commonmask);
#elif defined(sparc)
ieee_handler("set", "common", (sighandler)fpe_error);
#endif
#if !defined(sparc) || defined(sparc8)
(void)signal(SIGFPE, (sighandler)fpe_error); /* catch arithmetic overflow */
#endif
(void)signal(SIGTERM, (sighandler)exit); /* flush buffers if killed */
commandloop(initscript);
/* parameter is file given as argument */
return 0; /* Should not be reached, commandloop contains infinite loop or exit */
}
int vstack[4]; /* record of miralib versions looked at */
char *mstack[4]; /* and where found */
int mvp = 0;
int checkversion(char *m)
/* returns 1 iff m is directory with .version containing our version number */
{
int v1, read_status = 0, r = 0;
// Ensure linebuf has enough space before strcat. pnlim is max path length.
if (strlen(m) + strlen("/.version") >= sizeof(linebuf)) {
fprintf(stderr, "Error: Path buffer too small in checkversion.\n");
return 0;
}
(void)strcpy(linebuf, m);
(void)strcat(linebuf, "/.version");
FILE *f = fopen(linebuf, "r");
if (f) {
if (fscanf(f, "%u", &v1) == 1) {
r = (v1 == version);
read_status = 1;
}
(void)fclose(f);
}
if (read_status && !r) {
if (mvp < 4) { // Prevent buffer overflow for mstack/vstack
mstack[mvp] = m;
vstack[mvp++] = v1;
}
}
return r;
}
void libfails(void) {
word i = 0;
fprintf(stderr, "found");
for (; i < mvp; i++) {
fprintf(stderr, "\tversion %s at: %s\n", strvers(vstack[i]), mstack[i]);
}
}
const char *strvers(int v) {
static char vbuf[12];
if (v < 0 || v > 999999) return "???";
snprintf(vbuf, sizeof(vbuf), "%.3f", v / 1000.0);
return vbuf;
}
char *mkabsolute(char *m) { /* make sure m is an absolute pathname */
if (m[0] == '/') return m;
// Ensure dicp has enough space for CWD + '/' + m + '\0'
// This assumes dicp is a large buffer managed externally.
// A safer approach might be to malloc here and pass ownership.
if (!getcwd(dicp, pnlim)) {
fprintf(stderr, "panic: cwd too long\n");
exit(1);
}
(void)strcat(dicp, "/");
(void)strcat(dicp, m);
m = dicp;
dicp = dicq += strlen(dicp) + 1; /* This assumes dicq is a pointer within dicp buffer */
dic_check(); /* Assumed to check dictionary bounds */
return m;
}
void missparam(const char *s) {
fprintf(stderr, "mira: missing param after flag \"-%s\"\n", s);
exit(1);
}
int oldversion = 0;
#define COL_MAX 400
static inline void print_spaces(int s) {
for (int j = 0; j < s; j++) {
putchar(' ');
}
}
void announce(void) {
extern char *vdate;
word w;
/* clrscr(); /* clear screen on start up (commented out in original) */
w = (twidth() - 50) / 2;
printf("\n\n");
print_spaces((int)w);
printf(" T h e M i r a n d a S y s t e m\n\n");
print_spaces((int)(w + 5 - (strlen(vdate) / 2)));
printf(" version %s last revised %s\n\n", strvers(version), vdate);
print_spaces((int)w);
printf("Copyright Research Software Ltd 1985-2020\n\n");
print_spaces((int)w);
printf(" World Wide Web: http://miranda.org.uk\n\n\n");
if (SPACELIMIT != DFLTSPACE) {
printf("(%ld cells)\n", SPACELIMIT);
}
if (!strictif) printf("(-nostrictif : deprecated!)\n");
/* printf("\t\t\t\t%dbit platform\n",__WORDSIZE); /* temporary */
if (oldversion < 1999) { /* pre release two */
printf("\
WARNING:\n\
a new release of Miranda has been installed since you last used\n\
the system - please read the `CHANGES' section of the /man pages !!!\n\n");
} else if (version > oldversion) {
printf("a new version of Miranda has been installed since you last\n");
printf("used the system - see under `CHANGES' in the /man pages\n\n");
}
if (version < oldversion) {
printf("warning - this is an older version of Miranda than the one\n");
printf("you last used on this machine!!\n\n");
}
if (rc_error) {
printf("warning: \"%s\" contained bad data (ignored)\n", rc_error);
}
}
word rc_read(char *rcfile) { /* get settings of system parameters from setup file */
FILE *in;
char z[20];
word h, d, v, s_val, r = 0; // Renamed 's' to 's_val' to avoid conflict with extern FILE* s_in
oldversion = version; /* default assumption */
in = fopen(rcfile, "r");
if (in == NULL || fscanf(in, "%19s", z) != 1) {
return 0; /* file not present, or not readable */
}
if (strncmp(z, "hdve", 4) == 0 /* current .mirarc format */
|| strcmp(z, "lhdve") == 0) { /* alternative format used at release one */
char *z1 = &z[3];
if (z[0] == 'l') listing = 1, z1++;
while (*++z1)
if (*z1 == 'l') listing = 1;
else if (*z1 == 's') /* ignore */;
else if (*z1 == 'r') rechecking = 2;
else rc_error = rcfile;
if (fscanf(in, "%ld%ld%ld%*c", &h, &d, &v) != 3 || !getln(in, pnlim - 1, ebuf)
|| badval(h) || badval(d) || badval(v)) {
rc_error = rcfile;
} else {
editor = ebuf;
SPACELIMIT = h;
DICSPACE = d;
r = 1;
oldversion = v;
}
} else if (strcmp(z, "ehdsv") == 0) { /* versions before 550 */
if (fscanf(in, "%19s%ld%ld%ld%ld", ebuf, &h, &d, &s_val, &v) != 5
|| badval(h) || badval(d) || badval(v)) {
rc_error = rcfile;
} else {
editor = ebuf;
SPACELIMIT = h;
DICSPACE = d;
r = 1;
oldversion = v;
}
} else if (strcmp(z, "ehds") == 0) { /* versions before 326, "s" was stacklimit (ignore) */
if (fscanf(in, "%s%ld%ld%ld", ebuf, &h, &d, &s_val) != 4
|| badval(h) || badval(d)) {
rc_error = rcfile;
} else {
editor = ebuf;
SPACELIMIT = h;
DICSPACE = d;
r = 1;
oldversion = 1;
}
} else {
rc_error = rcfile; /* unrecognised format */
}
if (editor) fixeditor();
(void)fclose(in);
return r;
}
void fixeditor(void) {
if (strcmp(editor, "vi") == 0) editor = "vi +!";
else if (strcmp(editor, "pico") == 0) editor = "pico +!";
else if (strcmp(editor, "nano") == 0) editor = "nano +!";
else if (strcmp(editor, "joe") == 0) editor = "joe +!";
else if (strcmp(editor, "jpico") == 0) editor = "jpico +!";
else if (strcmp(editor, "vim") == 0) editor = "vim +!";
else if (strcmp(editor, "gvim") == 0) editor = "gvim +! % &";
else if (strcmp(editor, "emacs") == 0) editor = "emacs +! % &";
else {
char *p = strrchr(editor, '/');
if (p == NULL) p = editor;
else p++;
if (strcmp(p, "vi") == 0) strcat(p, " +!");
}
if (strrchr(editor, '&')) rechecking = 2;
listing = badeditor();
}
int badeditor(void) { /* does editor know how to open file at line? */
char *p = strchr(editor, '!');
while (p && p[-1] == '\\') p = strchr(p + 1, '!');
return (baded = (enum BadEditorStatus)!p);
}
int getln(FILE *in, word n, char *s) { /* reads line (<=n chars) from in into s - returns 1 if ok */
/* the newline is discarded, and the result '\0' terminated */
for (word i = 0; i < n; i++) {
int c = getc(in);
if (c == EOF) {
*s = '\0'; // Ensure null termination even on EOF
return 0;
}
if (c == '\n') {
*s = '\0';
return 1;
}
*s++ = (char)c;
}
*s = '\0'; // Ensure null termination if n chars read without newline
// Consume rest of line if buffer limit reached
int c;
while ((c = getc(in)) != '\n' && c != EOF);
return 0; // Line too long or EOF before newline within n chars
}
void rc_write(void) {
FILE *out = fopen(home_rc, "w");
if (out == NULL) {
fprintf(stderr, "warning: cannot write to \"%s\"\n", home_rc);
return;
}
fprintf(out, "hdve");
if (listing) fputc('l', out);
if (rechecking == 2) fputc('r', out);
fprintf(out, " %ld %ld %ld %s\n", SPACELIMIT, DICSPACE, version, editor);
(void)fclose(out);
}
word lastid = 0; /* first inscope identifier of immediately preceding command */
word rv_expr = 0;
void commandloop(char *initscript) {
int ch;
extern word cook_stdin;
extern void obey(word);
char *lb;
if (setjmp(env) == 0) { /* returns here if interrupted, 0 means first time thru */
if (magic) {
undump(initscript); /* was loadfile() changed 26.11.2019
to allow dump of magic scripts in ".m"*/
if (files == NIL || ND != NIL || id_val(main_id) == UNDEF)
/* files==NIL=>script absent or has syntax errors
ND!=NIL=>script has type errors or undefined names
all reported by undump() or loadfile() on new compile */
{
if (files != NIL && ND == NIL && id_val(main_id) == UNDEF)
fprintf(stderr, "%s: main not defined\n", initscript);
fprintf(stderr, "mira: incorrect use of \"-exec\" flag\n");
exit(1);
}
magic = 0;
obey(main_id);
exit(0);
}
/* was obey(lastexp), change to magic scripts 19.11.2013 */
(void)signal(SIGINT, (sighandler)reset);
undump(initscript);
if (verbosity) printf("for help type /h\n");
}
for (;;) {
resetgcstats();
if (verbosity) printf("%s", promptstr);
ch = getchar();
if (rechecking && src_update()) loadfile(current_script);
/* modified behaviour for `2-window' mode */
while (ch == ' ' || ch == '\t') ch = getchar();
switch (ch) {
case '?':
ch = getchar();
if (ch == '?') {
word x;
char *aka = NULL;
if (!token() && !lastid) {
printf("\7identifier needed after `\?\?'\n");
ch = getchar(); /* '\n' */
break;
}
if (getchar() != '\n') {
xschars();
break;
}
if (baded) {
ed_warn();
break;
}
if (dicp[0]) x = findid(dicp);
else printf("??%s\n", get_id(lastid)), x = lastid;
if (x == NIL || id_type(x) == undef_t) {
diagnose(dicp[0] ? dicp : get_id(lastid));
lastid = 0;
break;
}
if (id_who(x) == NIL) {
/* nb - primitives have NIL who field */
printf("%s -- primitive to Miranda\n",
dicp[0] ? dicp : get_id(lastid));
lastid = 0;
break;
}
lastid = x;
x = id_who(x); /* get here info */
if (tag[x] == CONS) aka = (char *)hd[hd[x]], x = tl[x];
if (aka) printf("originally defined as \"%s\"\n",
aka);
editfile((char *)hd[x], tl[x]);
break;
}
ungetc(ch, stdin);
(void)token();
lastid = 0;
if (dicp[0] == '\0') {
if (getchar() != '\n') xschars();
else allnamescom();
break;
}
while (dicp[0]) finger(dicp), (void)token();
ch = getchar();
break;
case ':': /* add (silently) as kindness to Hugs users */
case '/':
(void)token();
lastid = 0;
command();
break;
case '!':
if (!(lb = rdline())) break; /* rdline returns NULL on failure */
lastid = 0;
if (*lb) {
static char *shell = NULL;
sighandler oldsig;
pid_t pid; /* Use pid_t for process IDs */
if (!shell) {
shell = getenv("SHELL");
if (!shell) shell = (char *)"/bin/sh";
}
oldsig = signal(SIGINT, SIG_IGN);
if ((pid = fork()) == -1) { // Check for fork failure
perror("UNIX error - cannot create process");
} else if (pid == 0) { // Child process
execl(shell, shell, "-c", lb, (char *)0);
// If execl returns, it means an error occurred
perror("execl failed");
_exit(127); // Exit child process if execl fails
} else { // Parent process
int status;
while (pid != wait(&status)); // Wait for child to finish
(void)signal(SIGINT, oldsig);
}
if (src_update()) loadfile(current_script);
} else
printf(
"No previous shell command to substitute for \"!\"\n");
break;
case '|': /* lines beginning "||" are comments */
if ((ch = getchar()) != '|')
printf("\7unknown command - type /h for help\n");
while (ch != '\n' && ch != EOF) ch = getchar();
case '\n':
break;
case EOF:
if (verbosity) printf("\nmiranda logout\n");
exit(0);
default:
ungetc(ch, stdin);
lastid = 0;
tl[hd[cook_stdin]] = 0; /* unset type of $+ */
rv_expr = 0;
c = EVAL;
echoing = 0;
polyshowerror = 0; /* gets set by wrong use of $+, readvals */
commandmode = 1;
yyparse();
if (SYNERR) SYNERR = 0;
else if (c != '\n') { /* APPARENTLY NEVER TRUE */
printf("syntax error\n");
while (c != '\n' && c != EOF)
c = getchar(); /* swallow syntax errors */
}
commandmode = 0;
echoing = verbosity & listing;
}
}
}
word parseline(word t, FILE *f, word fil) { /* parses next valid line of f at type t, returns EOF
if none found. See READVALS in reduce.c */
word t1, ch_val;
lastexp = UNDEF;
for (;;) {
ch_val = getc(f);
while (ch_val == ' ' || ch_val == '\t' || ch_val == '\n') ch_val = getc(f);
if (ch_val == '|') {
ch_val = getc(f);
if (ch_val == '|') { /* leading comment */
while ((ch_val = getc(f)) != '\n' && ch_val != EOF);
if (ch_val != EOF) continue;
} else
ungetc(ch_val, f);
}
if (ch_val == EOF) return (word)EOF;
ungetc(ch_val, f);
c = VALUE;
echoing = 0;
commandmode = 1;
s_in = f;
yyparse();
s_in = stdin;
if (SYNERR) SYNERR = 0, lastexp = UNDEF;
else if ((t1 = type_of(lastexp)) == wrong_t) lastexp = UNDEF;
else if (!subsumes(instantiate(t1), t)) {
printf("data has wrong type :: ");
out_type(t1);
printf("\nshould be :: ");
out_type(t);
putc('\n', stdout);
lastexp = UNDEF;
}
if (lastexp != UNDEF) return codegen(lastexp);
if (isatty(fileno(f))) printf("please re-enter data:\n");
else {
if (fil) fprintf(stderr, "readvals: bad data in file \"%s\"\n",
getstring(fil, 0));
else fprintf(stderr, "bad data in $+ input\n");
outstats();
exit(1);
}
}
}
void ed_warn(void) {
printf(
"The currently installed editor command, \"%s\", does not\n\
include a facility for opening a file at a specified line number. As a\n\
result the `\?\?' command and certain other features of the Miranda system\n\
are disabled. See manual section 31/5 on changing the editor for more\n\
information.\n",
editor);
}
word fm_time(const char *f) { /* time last modified of file f */
return (word)(stat(f, &buf) == 0 ? buf.st_mtime : 0);
/* non-existent file has conventional mtime of 0 */
} /* we assume time_t can be stored in a word */
#define same_file(x, y) (hd[fil_inodev(x)] == hd[fil_inodev(y)] && \
tl[fil_inodev(x)] == tl[fil_inodev(y)])
#define inodev(f) (stat(f, &buf) == 0 ? datapair(buf.st_ino, buf.st_dev) : \
datapair(0, (word)-1))
word oldfiles = NIL; /* most recent set of sources, in case of interrupted or
failed compilation */
int src_update(void) { /* any sources modified ? */
word ft, f = files == NIL ? oldfiles : files;
while (f != NIL) {
if ((ft = fm_time(get_fil(hd[f]))) != fil_time(hd[f])) {
if (ft == 0) unlinkx(get_fil(hd[f])); /* tidy up after eg `!rm %' */
return 1;
}
f = tl[f];
}
return 0;
}
int loading;
char *unlinkme; /* if set, is name of partially created obfile */
void reset(void) { /* interrupt catcher - see call to signal in commandloop */
extern word lineptr, ATNAMES, current_id;
extern int blankerr, collecting;
/*if(!making) /* see note below
(void)signal(SIGINT,SIG_IGN); /* dont interrupt me while I'm tidying up */
/*if(magic)exit(0); *//* signal now not set to reset in magic scripts */
if (collecting) gcpatch();
if (loading) {
if (!blankerr)
printf("\n<<compilation interrupted>>\n");
if (unlinkme) unlink(unlinkme);
/* stackp=dstack; /* add if undump() made interruptible later*/
oldfiles = files, unload(), current_id = ATNAMES = loading = SYNERR = lineptr = 0;
if (blankerr) blankerr = 0, makedump();
}
/* magic script cannot be literate so no guard needed on makedump */
else
printf("<<interrupt>>\n"); /* VAX, SUN, ^C does not cause newline */
reset_state(); /* see LEX */
if (collecting) collecting = 0, gc(); /* to mark stdenv etc as wanted */
if (making && !make_status) make_status = 1;
#ifdef SYSTEM5
else (void)signal(SIGINT, (sighandler)reset); /*ready for next interrupt*//*see note*/
#endif
/* during mira -make blankerr is only use of reset */
longjmp(env, 1);
} /* under BSD and Linux installed signal remains installed after interrupt
and further signals blocked until handler returns */