forked from ncihnegn/miranda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteer.c
2230 lines (2128 loc) · 77.4 KB
/
steer.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
/* MIRANDA STEER */
/* 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 *
*------------------------------------------------------------------------*/
/* this stuff is to get the time-last-modified of files */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /* creat() */
/* #include <sys/wait.h> // seems not needed, oct 05 */
struct stat buf; /* see man(2) stat - gets file status */
#include "data.h"
#include "big.h"
#include "lex.h"
#include <float.h>
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;
char PRELUDE[pnlim+10],STDENV[pnlim+9];
/* if anyone complains, elasticate these buffers! */
#define DFLTSPACE 2500000l
#define DFLTDICSPACE 100000l
/* default values for size of heap, dictionary */
word SPACELIMIT=DFLTSPACE,DICSPACE=DFLTDICSPACE;
#ifdef CYGWIN
#define EDITOR "joe +!"
#else
#define 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 overriden by shell/environment variable EDITOR if present */
extern FILE *s_out;
int UTF8=0, UTF8OUT=0;
extern char *vdate, *host;
extern word version, ND;
extern word *dstack,*stackp;
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(char*);
static void editfile(char*,int);
static void ed_warn(void);
static void filecopy(char*);
static void filecp(char*,char*);
static void finger(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(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 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);
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 */
/* to allow a NOSTDENV directive _in_the_script_ we would need to
(i) replace isltmess() test in rules by eg is this a list of thing,
where thing is algebraic type originally defined in STDENV
(ii) arrange to pick up <stdenv> when current script not loaded
not implemented */
word baded=0; /* see fixeditor() */
char *miralib=NULL;
char *mirahdr,*lmirahdr;
char *promptstr="Miranda ";
char *obsuffix="x";
FILE *s_in=NULL;
word commandmode=0; /* 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];
word col;
char home_rc[pnlim+8];
char lib_rc[pnlim+8];
char *rc_error=NULL;
#define badval(x) (x<1||x>478000000)
#include <setjmp.h> /* for longjmp() - see man (3) setjmp */
jmp_buf env;
#ifdef sparc8
#include <ieeefp.h>
fp_except commonmask = FP_X_INV|FP_X_OFL|FP_X_DZ; /* invalid|ovflo|divzero */
#endif
int main(argc,argv) /* system initialisation, followed by call to YACC */
int argc;
char *argv[];
{ word manonly=0;
char *home, *prs;
int okhome_rc; /* flags valid HOME/.mirarc file present */
char *initscript;
int badlib=0;
extern int ARGC; extern char **ARGV;
extern word newtyps,algshfns;
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 */
if((logfilname=malloc((strlen(p)+9))))
sprintf(logfilname,"miralog/%s",p),
fil=fopen(logfilname,"a");
else mallocfail("logfile name");
/* 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; } else
if(strcmp(argv[1],"-man")==0){ manonly=1; break; } 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=EDITOR;
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 f=freeids;
while(f!=NIL)
{ word n=findid((char *)hd[hd[tl[hd[f]]]]);
id_type(n)=tl[tl[hd[f]]];
id_val(n)=the_val(hd[hd[f]]);
hd[f]=n;
f=tl[f]; }
f=freeids=typesfirst(freeids);
printf("\t%%free {\n");
while(f!=NIL)
putchar('\t'),
report_type(hd[f]),
putchar('\n'),
f=tl[f];
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 sparc8
fpsetmask(commonmask);
#elif defined sparc
ieee_handler("set","common",(sighandler)fpe_error);
#endif
#if !defined sparc | 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 */
}
int vstack[4]; /* record of miralib versions looked at */
char *mstack[4]; /* and where found */
int mvp=0;
int checkversion(m)
/* returns 1 iff m is directory with .version containing our version number */
char *m;
{ int v1,read=0,r=0;
FILE *f=fopen(strcat(strcpy(linebuf,m),"/.version"),"r");
if(f&&fscanf(f,"%u",&v1)==1)r= v1==version, read=1;
if(f)fclose(f);
if(read&&!r)mstack[mvp]=m,vstack[mvp++]=v1;
return r;
}
void libfails()
{ word i=0;
fprintf(stderr,"found");
for(;i<mvp;i++)fprintf(stderr,"\tversion %s at: %s\n",
strvers(vstack[i]),mstack[i]);
}
char *strvers(v)
int v;
{ static char vbuf[12];
if(v<0||v>999999)return "\?\?\?";
snprintf(vbuf,12,"%.3f",v/1000.0);
return vbuf;
}
char *mkabsolute(m) /* make sure m is an absolute pathname */
char *m;
{ if(m[0]=='/')return(m);
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;
dic_check();
return(m);
}
void missparam(s)
char *s;
{ fprintf(stderr,"mira: missing param after flag \"-%s\"\n",s);
exit(1); }
int oldversion=0;
#define colmax 400
#define spaces(s) for(j=s;j>0;j--)putchar(' ')
void announce()
{ extern char *vdate;
word w,j;
/*clrscr(); // clear screen on start up */
w=(twidth()-50)/2;
printf("\n\n");
spaces(w); printf(" T h e M i r a n d a S y s t e m\n\n");
spaces(w+5-strlen(vdate)/2);
printf(" version %s last revised %s\n\n",strvers(version),vdate);
spaces(w); printf("Copyright Research Software Ltd 1985-2020\n\n");
spaces(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(rcfile) /* get settings of system parameters from setup file */
char *rcfile;
{ FILE *in;
char z[20];
word h,d,v,s,r=0;
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,&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)!=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();
fclose(in);
return(r);
}
void fixeditor()
{ 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=rindex(editor,'/');
if(p==0)p=editor; else p++;
if(strcmp(p,"vi")==0)strcat(p," +!");
}
if(rindex(editor,'&'))rechecking=2;
listing=badeditor();
}
int badeditor() /* does editor know how to open file at line? */
{ char *p=index(editor,'!');
while(p&&p[-1]=='\\')p=index(p+1,'!');
return (baded = !p);
}
int getln(in,n,s) /* reads line (<=n chars) from in into s - returns 1 if ok */
FILE *in; /* the newline is discarded, and the result '\0' terminated */
word n;
char *s;
{ while(n--&&(*s=getc(in))!='\n')s++;
if(*s!='\n'||n<0)return(0);
*s='\0';
return(1);
}
void rc_write()
{ 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);
fclose(out);
}
word lastid=0; /* first inscope identifier of immediately preceding command */
word rv_expr=0;
void commandloop(initscript)
char* initscript;
{ int ch;
void reset();
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)
{ /*system(lb); */ /* always gives /bin/sh */
static char *shell=NULL;
sighandler oldsig;
word pid;
if(!shell)
{ shell=getenv("SHELL");
if(!shell)shell="/bin/sh"; }
oldsig= signal(SIGINT,SIG_IGN);
if((pid=fork()))
{ /* parent */
if(pid==-1)
perror("UNIX error - cannot create process");
while(pid!=wait(0));
(void)signal(SIGINT,oldsig); }
else execl(shell,shell,"-c",lb,(char *)0);
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(t,f,fil) /* parses next valid line of f at type t, returns EOF
if none found. See READVALS in reduce.c */
word t;
FILE *f;
word fil;
{ word t1,ch;
lastexp=UNDEF;
for(;;)
{ ch=getc(f);
while(ch==' '||ch=='\t'||ch=='\n')ch=getc(f);
if(ch=='|')
{ ch=getc(f);
if(ch=='|') /* leading comment */
{ while((ch=getc(f))!='\n'&&ch!=EOF);
if(ch!=EOF)continue; }
else ungetc(ch,f); }
if(ch==EOF)return(EOF);
ungetc(ch,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()
{ 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(f) /* time last modified of file f */
char *f;
{ return(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,-1))
word oldfiles=NIL; /* most recent set of sources, in case of interrupted or
failed compilation */
int src_update() /* 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() /* 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 */
#define checkeol if(getchar()!='\n')break;
int lose;
int normal(f) /* s has ".m" suffix */
char *f;
{ int n=strlen(f);
return n>=2&&strcmp(f+n-2,".m")==0;
}
void v_info(int full)
{ printf("%s last revised %s\n",strvers(version),vdate);
if(!full)return;
printf("%s",host);
printf("XVERSION %u\n",XVERSION);
}
void command()
{ char *t;
int ch,ch1;
switch(dicp[0])
{
case 'a': if(is("a")||is("aux"))
{ checkeol;
/* if(verbosity)clrscr(); */
(void)strcpy(linebuf,miralib);
(void)strcat(linebuf,"/auxfile");
filecopy(linebuf);
return; }
case 'c': if(is("count"))
{ checkeol; atcount=1; return; }
if(is("cd"))
{ char *d=token();
if(!d)d=getenv("HOME");
else d=addextn(0,d);
checkeol;
if(chdir(d)==-1)printf("cannot cd to %s\n",d);
else if(src_update())undump(current_script);
/* alternative: keep old script and recompute pathname
wrt new directory - LOOK INTO THIS LATER */
return; }
case 'd': if(is("dic"))
{ extern char *dic;
if(!token())
{ lose=getchar(); /* to eat \n */
printf("%ld chars",DICSPACE);
if(DICSPACE!=DFLTDICSPACE)
printf(" (default=%ld)",DFLTDICSPACE);
printf(" %ld in use\n",(long)(dicq-dic));
return; }
checkeol;
printf(
"sorry, cannot change size of dictionary while in use\n");
printf(
"(/q and reinvoke with flag: mira -dic %s ... )\n",dicp);
return; }
case 'e': if(is("e")||is("edit"))
{ char *mf=0;
if((t=token()))t=addextn(1,t);
else t=current_script;
checkeol;
if(stat(t,&buf)) /* new file */
{ if(!lmirahdr) /* lazy initialisation */
{ dicp=dicq;
(void)strcpy(dicp,getenv("HOME"));
if(strcmp(dicp,"/")==0)
dicp[0]=0; /* root is special case */
(void)strcat(dicp,"/.mirahdr");
lmirahdr=dicp;
dicq=dicp=dicp+strlen(dicp)+1; } /* ovflo check? */
if(!stat(lmirahdr,&buf))mf=lmirahdr;
if(!mf&&!mirahdr) /* lazy initialisation */
{ dicp=dicq;
(void)strcpy(dicp,miralib);
(void)strcat(dicp,"/.mirahdr");
mirahdr=dicp;
dicq=dicp=dicp+strlen(dicp)+1; }
if(!mf&&!stat(mirahdr,&buf))mf=mirahdr;
/*if(mf)printf("mf=%s\n",mf); // DEBUG*/
if(mf&&t!=current_script)
{ printf("open new script \"%s\"? [ny]",t);
ch1=ch=getchar();
while(ch!='\n'&&ch!=EOF)ch=getchar();
/*eat rest of line */
if(ch1!='y'&&ch1!='Y')return; }
if(mf)filecp(mf,t); }
editfile(t,strcmp(t,current_script)==0?errline:
errs&&strcmp(t,(char *)hd[errs])==0?tl[errs]:
geterrlin(t));
return; }
if(is("editor"))
{ char *hold=linebuf,*h;
if(!getln(stdin,pnlim-1,hold))break; /*reject if too long*/
if(!*hold)
{ // lose=getchar(); /* to eat newline */
printf("%s\n",editor);
return; }
h=hold+strlen(hold); /* remove trailing white space */
while(h[-1]==' '||h[-1]=='\t')*--h='\0';
if(*hold=='"'||*hold=='\'')
{ printf("please type name of editor without quotation marks\n");
return; }
printf("change editor to: \"%s\"? [ny]",hold);
ch1=ch=getchar();
while(ch!='\n'&&ch!=EOF)ch=getchar(); /* eat rest of line */
if(ch1!='y'&&ch1!='Y')
{ printf("editor not changed\n");
return; }
(void)strcpy(ebuf,hold);
editor=ebuf;
fixeditor(); /* reads "vi" as "vi +!" etc */
echoing=verbosity&listing;
rc_write();
printf("editor = %s\n",editor);
return; }
case 'f': if(is("f")||is("file"))
{ char *t=token();
checkeol;
if(t)t=addextn(1,t),keep(t);
/* could get multiple copies of filename in dictionary
- FIX LATER */
if(t)errs=errline=0; /* moved here from reset() */
if(t)if(strcmp(t,current_script)||(files==NIL&&okdump(t)))
{ extern word CLASHES;
CLASHES=NIL; /* normally done by load_script */
undump(t); /* does not always call load_script */
if(CLASHES!=NIL)/* pathological case, recompile */
loadfile(t); }
else loadfile(t); /* force recompilation */
else printf("%s%s\n",current_script,
files==NIL?" (not loaded)":"");
return; }
if(is("files")) /* info about internal state, not documented */
{ word f=files;
checkeol;
for(;f!=NIL;f=tl[f])
printf("(%s,%ld,%ld)",get_fil(hd[f]),fil_time(hd[f]),
fil_share(hd[f])),printlist("",fil_defs(hd[f]));
return; } /* DEBUG */
if(is("find"))
{ word i=0;
while(token())
{ word x=findid(dicp),y,f;
i++;
if(x!=NIL)
{ char *n=get_id(x);
for(y=primenv;y!=NIL;y=tl[y])
if(tag[hd[y]]==ID)
if(hd[y]==x||getaka(hd[y])==n)
finger(get_id(hd[y]));
for(f=files;f!=NIL;f=tl[f])
for(y=fil_defs(hd[f]);y!=NIL;y=tl[y])
if(tag[hd[y]]==ID)
if(hd[y]==x||getaka(hd[y])==n)
finger(get_id(hd[y])); }
}
ch=getchar(); /* '\n' */
if(i==0)printf("\7identifier needed after `/find'\n");
return; }
case 'g': if(is("gc"))
{ checkeol; atgc=1; return; }
case 'h': if(is("h")||is("help"))
{ checkeol;
/* if(verbosity)clrscr(); */
(void)strcpy(linebuf,miralib);
(void)strcat(linebuf,"/helpfile");
filecopy(linebuf);
return; }
if(is("heap"))
{ word x;
if(!token())
{ lose=getchar(); /* to eat \n */
printf("%ld cells",SPACELIMIT);
if(SPACELIMIT!=DFLTSPACE)
printf(" (default=%ld)",DFLTSPACE);
printf("\n");
return; }
checkeol;
if(sscanf(dicp,"%ld",&x)!=1||badval(x))
{ printf("illegal value (heap unchanged)\n"); return; }
if(x<trueheapsize())
printf("sorry, cannot shrink heap to %ld at this time\n",x);
else { if(x!=SPACELIMIT)
SPACELIMIT=x,resetheap();
printf("heaplimit = %ld cells\n",SPACELIMIT),
rc_write(); }
return; }
if(is("hush"))
{ checkeol; echoing=verbosity=0; return; }
case 'l': if(is("list"))
{ checkeol; listing=1; echoing=verbosity&listing;
rc_write(); return; }
case 'm': if(is("m")||is("man"))
{ checkeol; manaction(); return; }
if(is("miralib"))
{ checkeol; printf("%s\n",miralib); return; }
case 'n': /* if(is("namebuckets"))
{ int i,x;
extern word namebucket[];
checkeol;
for(i=0;i<128;i++)
if(x=namebucket[i])
{ printf("%d:",i);
while(x)
putchar(' '),out(stdout,hd[x]),x=tl[x];
putchar('\n'); }
return; } // DEBUG */
if(is("nocount"))
{ checkeol; atcount=0; return; }
if(is("nogc"))
{ checkeol; atgc=0; return; }
if(is("nohush"))
{ checkeol; echoing=listing; verbosity=1; return; }
if(is("nolist"))
{ checkeol; echoing=listing=0; rc_write(); return; }
if(is("norecheck"))
{ checkeol; rechecking=0; rc_write(); return; }
/* case 'o': if(is("object"))
{ checkeol; atobject=1; return; } // now done by flag -object */
case 'q': if(is("q")||is("quit"))
{ checkeol; if(verbosity)printf("miranda logout\n"); exit(0); }
case 'r': if(is("recheck"))
{ checkeol; rechecking=2; rc_write(); return; }
case 's': if(is("s")||is("settings"))
{ checkeol;
printf("*\theap %ld\n",SPACELIMIT);
printf("*\tdic %ld\n",DICSPACE);