summaryrefslogtreecommitdiffstats
path: root/repl.c
blob: 15939095d8ddc99d3b7f61e1bbdbb50effaeeb1c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
/* Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
 * 
 * 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * As a special exception, the Free Software Foundation gives permission
 * for additional uses of the text contained in its release of GUILE.
 *
 * The exception is that, if you link the GUILE library with other files
 * to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the GUILE library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the
 * Free Software Foundation under the name GUILE.  If you copy
 * code from other Free Software Foundation releases into a copy of
 * GUILE, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for GUILE, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.  
 */

/* "repl.c" error, read-eval-print loop, read, write and load code.
   Author: Aubrey Jaffer */

#include "scm.h"
#include "setjump.h"
void	igc P((char *what, STACKITEM *stackbase));
void	unexec P((char *new_name, char *a_name, unsigned data_start,
		  unsigned bss_start, unsigned entry_address));

#ifdef ARM_ULIB
# include <termio.h>
int set_erase()
{
   struct termio tin;

   ioctl(0, TCGETA, &tin);
   tin.c_cc[VERASE] = '\010';

   ioctl(0, TCSETA,&tin);
   return(0);
}
#endif

unsigned char upcase[CHAR_CODE_LIMIT];
unsigned char downcase[CHAR_CODE_LIMIT];
unsigned char lowers[] = "abcdefghijklmnopqrstuvwxyz";
unsigned char uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
extern int verbose;
void init_tables()
{
  int i;
  for(i = 0;i<CHAR_CODE_LIMIT;i++) upcase[i] = downcase[i] = i;
  for(i = 0;i<sizeof lowers/sizeof(char);i++) {
    upcase[lowers[i]] = uppers[i];
    downcase[uppers[i]] = lowers[i];
  }
  verbose = 1;			/* Here so that monitor info won't be */
				/* printed while in init_storage. (BOOM) */
}

#ifdef EBCDIC
char *charnames[] = {
  "nul","soh","stx","etx", "pf", "ht", "lc","del",
   0   , 0   ,"smm", "vt", "ff", "cr", "so", "si",
  "dle","dc1","dc2","dc3","res", "nl", "bs", "il",
  "can", "em", "cc", 0   ,"ifs","igs","irs","ius",
   "ds","sos", "fs", 0   ,"byp", "lf","eob","pre",
   0   , 0   , "sm", 0   , 0   ,"enq","ack","bel",
   0   , 0   ,"syn", 0   , "pn", "rs", "uc","eot",
   0   , 0   , 0   , 0   ,"dc4","nak", 0   ,"sub",
  "space", s_newline, "tab", "backspace", "return", "page", "null"};
char charnums[] =
"\000\001\002\003\004\005\006\007\
\010\011\012\013\014\015\016\017\
\020\021\022\023\024\025\026\027\
\030\031\032\033\034\035\036\037\
\040\041\042\043\044\045\046\047\
\050\051\052\053\054\055\056\057\
\060\061\062\063\064\065\066\067\
\070\071\072\073\074\075\076\077\
 \n\t\b\r\f\0";
#endif /* def EBCDIC */
#ifdef ASCII
char *charnames[] = {
  "nul","soh","stx","etx","eot","enq","ack","bel",
   "bs", "ht", "nl", "vt", "np", "cr", "so", "si",
  "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
  "can", "em","sub","esc", "fs", "gs", "rs", "us",
  "space", s_newline, "tab", "backspace", "return", "page", "null", "del"};
char charnums[] =
"\000\001\002\003\004\005\006\007\
\010\011\012\013\014\015\016\017\
\020\021\022\023\024\025\026\027\
\030\031\032\033\034\035\036\037\
 \n\t\b\r\f\0\177";
#endif /* def ASCII */
char *isymnames[] = {
				/* Special Forms */
				/*  NUM_ISPCSYMS ISPCSYMS here */
  "#@and", "#@begin", "#@case", "#@cond", "#@do", "#@if", "#@lambda",
  "#@let", "#@let*", "#@letrec", "#@or", "#@quote", "#@set!",
  "#@define", "#@apply", "#@call-with-current-continuation",
  "#@farloc-car", "#@farloc-cdr", "#@delay", "#@quasiquote",
  "#@unquote", "#@unquote-splicing", "#@else", "#@=>",
				/* user visible ISYMS */
				/* other keywords */
				/* Flags */
  "#f", "#t", "#<undefined>", "#<eof>", "()", "#<unspecified>"
  };

static char	s_read_char[] = "read-char", s_peek_char[] = "peek-char";
char	s_read[] = "read", s_write[] = "write", s_newline[] = "newline";
static char	s_display[] = "display", s_write_char[] = "write-char";

static char	s_eofin[] = "end of file in ";
static char	s_unknown_sharp[] = "unknown # object";

static SCM lreadr P((SCM tok_buf, SCM port));
static SCM lreadparen P((SCM tok_buf, SCM port, char *name));
static sizet read_token P((int ic, SCM tok_buf, SCM port));

void intprint(n, radix, port)
     long n;
     int radix;
     SCM port;
{
  char num_buf[INTBUFLEN];
  lfwrite(num_buf, (sizet)sizeof(char), iint2str(n, radix, num_buf), port);
}

void ipruk(hdr, ptr, port)
     char *hdr;
     SCM ptr;
     SCM port;
{
  lputs("#<unknown-", port);
  lputs(hdr, port);
  if (scm_cell_p(ptr)) {
    lputs(" (0x", port);
    intprint(CAR(ptr), 16, port);
    lputs(" . 0x", port);
    intprint(CDR(ptr), 16, port);
    lputs(") @", port);
  }
  lputs(" 0x", port);
  intprint(ptr, 16, port);
  lputc('>', port);
}

void iprlist(hdr, exp, tlr, port, writing)
     char *hdr, tlr;
     SCM exp;
     SCM port;
     int writing;
{
  lputs(hdr, port);
  /* CHECK_INTS; */
  iprin1(CAR(exp), port, writing);
  exp = CDR(exp);
  for(;NIMP(exp);exp = CDR(exp)) {
    if (!scm_cell_p(~1L & exp)) break;
    if NECONSP(exp) break;
    lputc(' ', port);
    /* CHECK_INTS; */
    iprin1(CAR(exp), port, writing);
  }
  if NNULLP(exp) {
    lputs(" . ", port);
    iprin1(exp, port, writing);
  }
  lputc(tlr, port);
}
void iprin1(exp, port, writing)
     SCM exp;
     SCM port;
int writing;
{
  register long i;
taloop:
  switch (7 & (int)exp) {
  case 2:
  case 6:
    intprint(INUM(exp), 10, port);
    break;
  case 4:
    if ICHRP(exp) {
      i = ICHR(exp);
      if (writing) lputs("#\\", port);
      if (!writing) lputc((int)i, port);
      else if ((i <= ' ') && charnames[i]) lputs(charnames[i], port);
#ifndef EBCDIC
      else if (i=='\177')
	lputs(charnames[(sizeof charnames/sizeof(char *))-1], port);
#endif /* ndef EBCDIC */
      else if (i > '\177')
	intprint(i, 8, port);
      else lputc((int)i, port);
    }
    else if (IFLAGP(exp) && (ISYMNUM(exp)<(sizeof isymnames/sizeof(char *))))
      lputs(ISYMCHARS(exp), port);
    else if ILOCP(exp) {
      lputs("#@", port);
      intprint((long)IFRAME(exp), 10, port);
      lputc(ICDRP(exp)?'-':'+', port);
      intprint((long)IDIST(exp), 10, port);
    }
    else goto idef;
    break;
  case 1:			/* gloc */
    if (!scm_cell_p(exp-1)) {
      ipruk("gloc", exp, port);
      break;
    }
    lputs("#@", port);
    exp = CAR(exp-1);
    goto taloop;
  default:
  idef:
    ipruk("immediate", exp, port);
    break;
  case 0:
    if (!scm_cell_p(exp)) {
      ipruk("heap", exp, port);
      break;
    }
    switch TYP7(exp) {
    case tcs_cons_gloc:
    case tcs_cons_imcar:
    case tcs_cons_nimcar:
      iprlist("(", exp, ')', port, writing);
      break;
    case tcs_closures:
      exp = CODE(exp);
      iprlist("#<CLOSURE ", exp, '>', port, writing);
      break;
    case tc7_string:
      if (writing) {
	lputc('\"', port);
	for(i = 0;i<LENGTH(exp);++i) switch (CHARS(exp)[i]) {
	case '\"':
	case '\\':
	  lputc('\\', port);
	default:
	  lputc(CHARS(exp)[i], port);
	}
	lputc('\"', port);
	break;
      }
    case tcs_symbols:
      lfwrite(CHARS(exp), (sizet)sizeof(char), (sizet)LENGTH(exp), port);
      break;
    case tc7_vector:
      lputs("#(", port);
      for(i = 0;i+1<LENGTH(exp);++i) {
	/* CHECK_INTS; */
	iprin1(VELTS(exp)[i], port, writing);
	lputc(' ', port);
      }
      if (i<LENGTH(exp)) {
	/* CHECK_INTS; */
	iprin1(VELTS(exp)[i], port, writing);
      }
      lputc(')', port);
      break;
    case tc7_bvect:
    case tc7_ivect:
    case tc7_uvect:
    case tc7_fvect:
    case tc7_dvect:
    case tc7_cvect:
      raprin1(exp, port, writing);
      break;
    case tcs_subrs:
      lputs("#<primitive-procedure ", port);
      lputs(CHARS(SNAME(exp)), port);
      lputc('>', port);
      break;
#ifdef CCLO
    case tc7_cclo:
      lputs("#<compiled-closure ", port);
      iprin1(CCLO_SUBR(exp), port, writing);
      lputc('>', port);
      break;
#endif
    case tc7_contin:
      lputs("#<continuation ", port);
      intprint(LENGTH(exp), 10, port);
      lputs(" @ ", port);
      intprint((long)CHARS(exp), 16, port);
      lputc('>', port);
      break;
    case tc7_port:
      i = PTOBNUM(exp);
      if (i<numptob && ptobs[i].print && (ptobs[i].print)(exp, port, writing))
	break;
      goto punk;
    case tc7_smob:
      i = SMOBNUM(exp);
      if (i<numsmob && smobs[i].print && (smobs[i].print)(exp, port, writing))
	break;
      goto punk;
    default: punk: ipruk("type", exp, port);
    }
  }
}

#ifdef __IBMC__
# define MSDOS
#endif
#ifdef MSDOS
# ifndef GO32
#  include <io.h>
#  include <conio.h>
static int input_waiting(f)
     FILE *f;
{
  if (feof(f)) return 1;
  if (fileno(f)==fileno(stdin) && (isatty(fileno(stdin)))) return kbhit();
  return -1;
}
# endif
#else
# ifdef _DCC
#  include <ioctl.h>
# else
#  ifndef AMIGA
#   ifndef vms
#    ifdef MWC
#     include <sys/io.h>
#    else
#     ifndef THINK_C
#      ifndef ARM_ULIB
#       include <sys/ioctl.h>
#      endif
#     endif
#    endif
#   endif
#  endif
# endif

# ifdef HAVE_SELECT
#  ifdef HAVE_SYS_TIME_H
#   include <sys/time.h>
#  endif
# endif

static int input_waiting(f)
     FILE *f;
{
# ifdef HAVE_SELECT
  fd_set ifds;
  struct timeval tv;

  FD_ZERO(&ifds);
  FD_SET(fileno(f), &ifds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  select((fileno(f) + 1), &ifds, (fd_set *) NULL, (fd_set *) NULL, &tv);
  return FD_ISSET(fileno(f), &ifds);
# else
#  ifdef FIONREAD
  long remir;
  if (feof(f)) return 1;
  ioctl(fileno(f), FIONREAD, &remir);
  return remir;
#  else
  return -1;
#  endif
# endif
}
#endif
/* perhaps should undefine MSDOS from __IBMC__ here */
#ifndef GO32
static char s_char_readyp[]="char-ready?";
SCM char_readyp(port)
     SCM port;
{
  if UNBNDP(port) port = cur_inp;
  else ASSERT(NIMP(port) && OPINPORTP(port), port, ARG1, s_char_readyp);
  if (CRDYP(port) || !(BUF0 & CAR(port))) return BOOL_T;
  return input_waiting(STREAM(port)) ? BOOL_T : BOOL_F;
}
#endif

SCM eof_objectp(x)
     SCM x;
{
	return (EOF_VAL==x) ? BOOL_T : BOOL_F;
}

void lfflush(port)		/* internal SCM call */
     SCM port;
{
  sizet i = PTOBNUM(port);
  (ptobs[i].fflush)(STREAM(port));
}
static char	s_flush[] = "force-output";
SCM lflush(port)		/* user accessible as force-output */
     SCM port;
{
	if UNBNDP(port) port = cur_outp;
	else ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG1, s_flush);
	{
	  sizet i = PTOBNUM(port);
	  SYSCALL((ptobs[i].fflush)(STREAM(port)););
	  return UNSPECIFIED;
	}
}

SCM lwrite(obj, port)
     SCM obj, port;
{
	if UNBNDP(port) port = cur_outp;
	else ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG2, s_write);
	iprin1(obj, port, 1);
#ifdef HAVE_PIPE
# ifdef EPIPE
	if (EPIPE==errno) close_port(port);
# endif
#endif
	return UNSPECIFIED;
}
SCM display(obj, port)
     SCM obj, port;
{
	if UNBNDP(port) port = cur_outp;
	else ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG2, s_display);
	iprin1(obj, port, 0);
#ifdef HAVE_PIPE
# ifdef EPIPE
	if (EPIPE==errno) close_port(port);
# endif
#endif
	return UNSPECIFIED;
}
SCM newline(port)
     SCM port;
{
	if UNBNDP(port) port = cur_outp;
	else ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG1, s_newline);
	lputc('\n', port);
#ifdef HAVE_PIPE
# ifdef EPIPE
	if (EPIPE==errno) close_port(port);
	else
# endif
#endif
	  if (port==cur_outp) lfflush(port);
	return UNSPECIFIED;
}
SCM write_char(chr, port)
     SCM chr, port;
{
	if UNBNDP(port) port = cur_outp;
	else ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG2, s_write_char);
	ASSERT(ICHRP(chr), chr, ARG1, s_write_char);
	lputc((int)ICHR(chr), port);
#ifdef HAVE_PIPE
# ifdef EPIPE
	if (EPIPE==errno) close_port(port);
# endif
#endif
	return UNSPECIFIED;
}

FILE *trans = 0;
SCM trans_on(fil)
     SCM fil;
{
  transcript = open_file(fil, makfromstr("w", (sizet)sizeof(char)));
  if FALSEP(transcript) trans = 0;
  else trans = STREAM(transcript);
  return UNSPECIFIED;
}
SCM trans_off()
{
  if (!FALSEP(transcript)) close_port(transcript);
  transcript = BOOL_F;
  trans = 0;
  return UNSPECIFIED;
}

void lputc(c, port)
     int c;
     SCM port;
{
  sizet i = PTOBNUM(port);
  SYSCALL((ptobs[i].fputc)(c, STREAM(port)););
  if (trans && (port==def_outp || port==cur_errp))
    SYSCALL(fputc(c, trans););
}
void lputs(s, port)
     char *s;
     SCM port;
{
  sizet i = PTOBNUM(port);
  SYSCALL((ptobs[i].fputs)(s, STREAM(port)););
  if (trans && (port==def_outp || port==cur_errp))
    SYSCALL(fputs(s, trans););
}
int lfwrite(ptr, size, nitems, port)
     char *ptr;
     sizet size;
     sizet nitems;
     SCM port;
{
  int ret;
  sizet i = PTOBNUM(port);
  SYSCALL(ret = (ptobs[i].fwrite)
	  (ptr, size, nitems, STREAM(port)););
  if (trans && (port==def_outp || port==cur_errp))
    SYSCALL(fwrite(ptr, size, nitems, trans););
  return ret;
}

int lgetc(port)
  SCM port;
{
  FILE *f;
  int c;
  sizet i;
  /* One char may be stored in the high bits of (car port) orre@nada.kth.se. */
  if CRDYP(port)
    {
      c = CGETUN(port);
      CLRDY(port);		/* Clear ungetted char */
      return c;
    }
  f=STREAM(port);
  i = PTOBNUM(port);
#ifdef linux
  c = (ptobs[i].fgetc)(f);
#else
  SYSCALL(c = (ptobs[i].fgetc)(f););
#endif
  if (trans && (f==stdin)) SYSCALL(fputc(c, trans););
  return c;
}
void lungetc(c, port)
  int c;
  SCM port;
{
/*	ASSERT(!CRDYP(port), port, ARG2, "too many lungetc");*/
	CUNGET(c, port);
}

SCM scm_read_char(port)
     SCM port;
{
  int c;
  if UNBNDP(port) port = cur_inp;
  else ASSERT(NIMP(port) && OPINPORTP(port), port, ARG1, s_read_char);
  c = lgetc(port);
  if (EOF==c) return EOF_VAL;
  return MAKICHR(c);
}
SCM peek_char(port)
  SCM port;
{
	int c;
	if UNBNDP(port) port = cur_inp;
	else ASSERT(NIMP(port) && OPINPORTP(port), port, ARG1, s_peek_char);
	c = lgetc(port);
	if (EOF==c) return EOF_VAL;
	lungetc(c, port);
	return MAKICHR(c);
}

char *grow_tok_buf(tok_buf)
     SCM tok_buf;
{
  sizet len = LENGTH(tok_buf);
  len += len / 2;
  resizuve(tok_buf, (SCM)MAKINUM(len));
  return CHARS(tok_buf);
}

static int flush_ws(port)
     SCM port;
{
  register int c;
  while(1) switch (c = lgetc(port)) {
    case ';': lp: switch (c = lgetc(port)) {
      default: goto lp;
      case EOF: return c;
      case LINE_INCREMENTORS: break;
    }
    case LINE_INCREMENTORS: if (port==loadport) linum++;
    case WHITE_SPACES: break;
    case EOF:
    default:
      return c;
  }
}
SCM lread(port)
     SCM port;
{
	int c;
	SCM tok_buf;
	if UNBNDP(port) port = cur_inp;
	else ASSERT(NIMP(port) && OPINPORTP(port), port, ARG1, s_read);
	do {
	  c = flush_ws(port);
	  if (EOF==c) return EOF_VAL;
	  lungetc(c, port);
	  tok_buf = makstr(30L);
	} while (EOF_VAL==(tok_buf = lreadr(tok_buf, port)));
	return tok_buf;
}
static SCM lreadpr(tok_buf, port)
     SCM tok_buf;
     SCM port;
{
	int c;
	sizet j;
	SCM p;
tryagain:
	c = flush_ws(port);
	switch (c) {
	case EOF: return EOF_VAL;
#ifdef BRACKETS_AS_PARENS
	case '[':
#endif
	case '(': return lreadparen(tok_buf, port, s_list);
#ifdef BRACKETS_AS_PARENS
	case ']':
#endif
	case ')': return UNDEFINED; /* goto tryagain; */
	case '\'': return cons2(i_quote, lreadr(tok_buf, port), EOL);
	case '`': return cons2(i_quasiquote, lreadr(tok_buf, port), EOL);
	case ',':
		c = lgetc(port);
		if ('@'==c) p = i_uq_splicing;
		else {
			lungetc(c, port);
			p = i_unquote;
		}
		return cons2(p, lreadr(tok_buf, port), EOL);
	case '#':
		c = lgetc(port);
		switch (c) {
#ifdef BRACKETS_AS_PARENS
		case '[':
#endif
		case '(':
			p = lreadparen(tok_buf, port, s_vector);
			return NULLP(p) ? nullvect : vector(p);
		case 't': case 'T': return BOOL_T;
		case 'f': case 'F': return BOOL_F;
		case 'b': case 'B': case 'o': case 'O':
		case 'd': case 'D': case 'x': case 'X':
		case 'i': case 'I': case 'e': case 'E':
			lungetc(c, port);
			c = '#';
			goto num;
		case '*':
			j = read_token(c, tok_buf, port);
			p = istr2bve(CHARS(tok_buf)+1, (long)(j-1));
			if (NFALSEP(p)) return p;
			else goto unkshrp;
		case '\\':
			c = lgetc(port);
			j = read_token(c, tok_buf, port);
			if (j==1) return MAKICHR(c);
			if (c >= '0' && c < '8') {
			  p = istr2int(CHARS(tok_buf), (long)j, 8);
			  if (NFALSEP(p)) return MAKICHR(INUM(p));
			}
			for (c = 0;c<sizeof charnames/sizeof(char *);c++)
			  if (charnames[c]
			      && (0==strcmp(charnames[c], CHARS(tok_buf))))
			    return MAKICHR(charnums[c]);
			wta(UNDEFINED, "unknown # object: #\\", CHARS(tok_buf));
		case '|':
			j = 1;	/* here j is the comment nesting depth */
lp:			c = lgetc(port);
lpc:			switch (c) {
			case EOF:
			  wta(UNDEFINED, s_eofin, "balanced comment");
			case LINE_INCREMENTORS:
			  if (port==loadport) linum++;
			default:
			  goto lp;
			case '|':
			  if ('#' != (c = lgetc(port))) goto lpc;
			  if (--j) goto lp;
			  break;
			case '#':
			  if ('|' != (c = lgetc(port))) goto lpc;
			  ++j; goto lp;
			}
			goto tryagain;
		case '.':
			p = lreadr(tok_buf, port);
			return EVAL(p, (SCM)EOL);
		default: callshrp:
			p = CDR(intern("read:sharp", (sizeof "read:sharp")-1));
			if NIMP(p) {
			  p = apply(p, MAKICHR(c), acons(port, EOL, EOL));
			  if (UNSPECIFIED==p) goto tryagain;
			  return p;
			}
		      unkshrp: wta((SCM)MAKICHR(c), s_unknown_sharp, "");
		}
	case '\"':
		j = 0;
		while ('\"' != (c = lgetc(port))) {
			ASSERT(EOF != c, UNDEFINED, s_eofin, s_string);
			if (j+1 >= LENGTH(tok_buf)) grow_tok_buf(tok_buf);
			if (c=='\\') switch (c = lgetc(port)) {
			case '\n': continue;
			case '0': c = '\0'; break;
			case 'f': c = '\f'; break;
			case 'n': c = '\n'; break;
			case 'r': c = '\r'; break;
			case 't': c = '\t'; break;
			case 'a': c = '\007'; break;
			case 'v': c = '\v'; break;
			}
			CHARS(tok_buf)[j] = c;
			++j;
		}
		if (j==0) return nullstr;
		CHARS(tok_buf)[j] = 0;
		return makfromstr(CHARS(tok_buf), j);
	case DIGITS:
	case '.': case '-': case '+':
num:
		j = read_token(c, tok_buf, port);
		p = istring2number(CHARS(tok_buf), (long)j, 10L);
		if NFALSEP(p) return p;
	        if (c=='#') {
		  if ((j==2) && (lgetc(port)=='(')) {
		    lungetc('(', port);
		    c = CHARS(tok_buf)[1];
		    goto callshrp;
		  }
		  wta(UNDEFINED, s_unknown_sharp, CHARS(tok_buf));
		}
	        goto tok;
	default:
		j = read_token(c, tok_buf, port);
tok:
		p = intern(CHARS(tok_buf), j);
		return CAR(p);
	}
}
static SCM lreadr(tok_buf, port)
     SCM tok_buf;
     SCM port;
{
  SCM ans = lreadpr(tok_buf, port);
  switch (ans) {
  case UNDEFINED:
    warn("unexpected \")\"", "");
    return lreadpr(tok_buf, port);
  }
  return ans;
}

#ifdef _UNICOS
_Pragma("noopt");		/* # pragma _CRI noopt */
#endif
static sizet read_token(ic, tok_buf, port)
     int ic;
     SCM tok_buf;
     SCM port;
{
	register sizet j = 1;
	register int c = ic;
	register char *p = CHARS(tok_buf);
	p[0] = downcase[c];
	while(1) {
		if (j+1 >= LENGTH(tok_buf)) p = grow_tok_buf(tok_buf);
		switch (c = lgetc(port)) {
#ifdef BRACKETS_AS_PARENS
		case '[': case ']':
#endif
		case '(': case ')': case '\"': case ';':
		case ',': case '`':
		  /* case '#': */
		case WHITE_SPACES:
		case LINE_INCREMENTORS:
			lungetc(c, port);
		case EOF:
			p[j] = 0;
			return j;
		default:
			p[j++] = downcase[c];
		}
	}
}
#ifdef _UNICOS
_Pragma("opt");			/* # pragma _CRI opt */
#endif

static SCM lreadparen(tok_buf, port, name)
     SCM tok_buf;
     SCM port;
     char *name;
{
  SCM lst, fst, tmp = lreadpr(tok_buf, port);
  if (UNDEFINED==tmp) return EOL;
  if (i_dot==tmp) {
    fst = lreadr(tok_buf, port);
  closeit:
    tmp = lreadpr(tok_buf, port);
    if (UNDEFINED != tmp) wta(UNDEFINED, "missing close paren", "");
    return fst;
  }
  fst = lst = cons(tmp, EOL);
  while (UNDEFINED != (tmp = lreadpr(tok_buf, port))) {
    if (EOF_VAL==tmp) wta(lst, s_eofin, s_list);
    if (i_dot==tmp) {
      CDR(lst) = lreadr(tok_buf, port);
      goto closeit;
    }
    lst = (CDR(lst) = cons(tmp, EOL));
  }
  return fst;
}

/* These procedures implement synchronization primitives.  Processors
   with an atomic test-and-set instruction can use it here (and not
   DEFER_INTS). */
char s_tryarb[] = "try-arbiter";
char s_relarb[] = "release-arbiter";
long tc16_arbiter;
SCM tryarb(arb)
     SCM arb;
{
  ASSERT((TYP16(arb)==tc16_arbiter), arb, ARG1, s_tryarb);
  DEFER_INTS;
  if (CAR(arb) & (1L<<16))
    arb = BOOL_F;
  else {
    CAR(arb) = tc16_arbiter | (1L<<16);
    arb = BOOL_T;
  }
  ALLOW_INTS;
  return arb;
}
SCM relarb(arb)
     SCM arb;
{
  ASSERT((TYP16(arb)==tc16_arbiter), arb, ARG1, s_relarb);
  if (!(CAR(arb) & (1L<<16))) return BOOL_F;
  CAR(arb) = tc16_arbiter;
  return BOOL_T;
}
SCM makarb(name)
     SCM name;
{
  register SCM z;
  NEWCELL(z);
  CDR(z) = name;
  CAR(z) = tc16_arbiter;
  return z;
}
static int prinarb(exp, port, writing)
     SCM exp; SCM port; int writing;
{
  lputs("#<arbiter ", port);
  if (CAR(exp) & (1L<<16)) lputs("locked ", port);
  iprin1(CDR(exp), port, writing);
  lputc('>', port);
  return !0;
}

static char s_tryload[] = "try-load";
#define s_load (&s_tryload[4])

struct errdesc {char *msg;char *s_response;short parent_err;};
struct errdesc errmsgs[] = {
  {"Wrong number of args", 0, 0},
  {"numerical overflow", 0, FPE_SIGNAL},
  {"Argument out of range", 0, FPE_SIGNAL},
  {"Could not allocate", "out-of-storage", 0},
  {"EXIT", "end-of-program", -1},
  {"hang up", "hang-up", EXIT},
  {"user interrupt", "user-interrupt", 0},
  {"arithmetic error", "arithmetic-error", 0},
  {"bus error", 0, 0},
  {"segment violation", 0, 0},
  {"alarm", "alarm-interrupt", 0}
};

int errjmp_bad = 1, ints_disabled = 1, sig_deferred = 0, alrm_deferred;
SCM err_exp, err_env;
char *err_pos, *err_s_subr;
cell tmp_errobj = {(SCM)UNDEFINED, (SCM)EOL};
cell tmp_loadpath = {(SCM)BOOL_F, (SCM)EOL};
SCM *loc_errobj = (SCM *)&tmp_errobj;
SCM *loc_loadpath = (SCM *)&tmp_loadpath;
SCM loadport = UNDEFINED;
long linum = 1;
int verbose = 1;
long cells_allocated = 0, lcells_allocated = 0,
  mallocated = 0, lmallocated = 0,
  rt = 0, gc_rt, gc_time_taken;
long gc_cells_collected, gc_malloc_collected, gc_ports_collected;
long gc_syms_collected;
static void def_err_response P((void));

int handle_it(i)
     int i;
{
  char *name = errmsgs[i-WNA].s_response;
  SCM proc;
  if (errjmp_bad) return -1;	/* sends it to def_err_response */
  if (name) {
    NEWCELL(proc);		/* discard possibly-used cell */
    proc = CDR(intern(name, (sizet)strlen(name)));
    if NIMP(proc) {
      apply(proc, EOL, EOL);
      return i;
    }
  }
  return errmsgs[i-WNA].parent_err;
}
static char s_eval_string[] = "eval-string";
static char s_load_string[] = "load-string";
SCM scm_eval_string(str)
     SCM str;
{
  str = mkstrport(INUM0, str, OPN | RDNG, s_eval_string);
  str = lread(str);
  return EVAL(str, (SCM)EOL);
}
SCM scm_load_string(str)
     SCM str;
{
  ASSERT(NIMP(str) && (STRINGP(str) || SYMBOLP(str)), str, ARG1,
	 s_load_string);
  str = mkstrport(INUM0, str, OPN | RDNG, s_load_string);
  while(1) {
    SCM form = lread(str);
    if (EOF_VAL==form) break;
    SIDEVAL(form, EOL);
  }
  return BOOL_T;
}

SCM exitval = MAKINUM(EXIT_FAILURE); /* INUM return value */
extern char s_unexec[];
SCM repl_driver(initpath)
     char *initpath;
{
#ifdef _UNICOS
  int i;
#else
  long i;
#endif
  CONT(rootcont)->stkbse = (STACKITEM *)&i;
  i = setjump(CONT(rootcont)->jmpbuf);
#ifndef SHORT_INT
  if (i) i = UNCOOK(i);
#endif
  /* printf("repl_driver got %d\n", i); */
 drloop:
  switch ((int)i) {
  default: {
    char *name = errmsgs[i-WNA].s_response;
    if (name) {
      SCM proc = CDR(intern(name, (sizet)strlen(name)));
      if NIMP(proc) apply(proc, EOL, EOL);
    }
    if ((i = errmsgs[i-WNA].parent_err)) goto drloop;
    def_err_response();
    goto reset_toplvl;
  }
  case 0:
    exitval = MAKINUM(EXIT_SUCCESS);
    errjmp_bad = 0;
    errno = 0;
    alrm_deferred = 0;
    sig_deferred = 0;
    ints_disabled = 0;
    if (dumped) {
      lcells_allocated = cells_allocated;
      lmallocated = mallocated;
      rt = INUM(my_time());
      gc_time_taken = 0;
    }
    else if (scm_ldfile(initpath)) /* load Scheme init files */
      wta(*loc_errobj, "Could not open file", s_load);
    scm_evstr("(boot-tail)");	/* initialization tail-call */
  case -2:			/* abrt */
  reset_toplvl:
    errjmp_bad = 0;
    alrm_deferred = 0;
    sig_deferred = 0;
    ints_disabled = 0;

    /* Closing the loading file turned out to be a bad idea. */
    /* But I will leave the code here in case someone wants it. */
#ifdef CLOSE_LOADING_PORTS_ON_ABORT
    if (NIMP(loadport) && OPINPORTP(loadport)) {
      if (verbose > 1) {
	lputs("; Aborting load (closing): ", cur_errp);
	display(*loc_loadpath, cur_errp);
	newline(cur_errp);
      }
      close_port(loadport);	/* close loading file. */
    }
#endif
    *loc_loadpath = BOOL_F;
    loadport = UNDEFINED;
    repl();
    err_pos = (char *)EXIT;
    i = EXIT;
    goto drloop;		/* encountered EOF on stdin */
  case -1:			/* quit */
    return exitval;
  case -3:			/* restart. */
    return 0;
#ifdef CAN_DUMP
  case -4:			/* dump */
    igc(s_unexec, (STACKITEM *)0);
    dumped = 1;
    unexec(CHARS(*loc_errobj), execpath, 0, 0, 0);
    goto reset_toplvl;
#endif
  }
}

SCM line_num()
{
  return MAKINUM(linum);
}
SCM prog_args()
{
  return progargs;
}

extern char s_heap[];
extern sizet hplim_ind;
extern CELLPTR *hplims;
void growth_mon(obj, size, units)
     char *obj;
     long size;
     char *units;
{
  if (verbose>2)
    {
      lputs("; grew ", cur_errp);
      lputs(obj, cur_errp);
      lputs(" to ", cur_errp);
      intprint(size, 10, cur_errp);
      lputc(' ', cur_errp);
      lputs(units, cur_errp);
      if ((verbose>4) && (obj==s_heap)) heap_report();
      lputs("\n", cur_errp);
    }
}

void gc_start(what)
     char *what;
{
  if (verbose>3 && FPORTP(cur_errp)) {
    ALLOW_INTS;
    lputs(";GC(", cur_errp);
    lputs(what, cur_errp);
    lputs(")", cur_errp);
    lfflush(cur_errp);
    DEFER_INTS;
  }
  gc_rt = INUM(my_time());
  gc_cells_collected = 0;
  gc_malloc_collected = 0;
  gc_ports_collected = 0;
  gc_syms_collected = 0;
}
void gc_end()
{
  gc_rt = INUM(my_time()) - gc_rt;
  gc_time_taken = gc_time_taken + gc_rt;
  if (verbose>3) {
    ALLOW_INTS;
    if (!FPORTP(cur_errp)) lputs(";GC ", cur_errp);
    intprint(time_in_msec(gc_rt), 10, cur_errp);
    lputs(" cpu mSec, ", cur_errp);
    intprint(gc_cells_collected, 10, cur_errp);
    lputs(" cells, ", cur_errp);
    intprint(gc_malloc_collected, 10, cur_errp);
    lputs(" malloc, ", cur_errp);
    intprint(gc_syms_collected, 10, cur_errp);
    lputs(" syms, ", cur_errp);
    intprint(gc_ports_collected, 10, cur_errp);
    lputs(" ports collected\n", cur_errp);
    lfflush(cur_errp);
    DEFER_INTS;
  }
}
void repl_report()
{
  if (verbose>1) {
    lfflush(cur_outp);
    lputs(";Evaluation took ", cur_errp);
    intprint(time_in_msec(INUM(my_time())-rt), 10, cur_errp);
    lputs(" mSec (", cur_errp);
    intprint(time_in_msec(gc_time_taken), 10, cur_errp);
    lputs(" in gc) ", cur_errp);
    intprint(cells_allocated - lcells_allocated, 10, cur_errp);
    lputs(" cells work, ", cur_errp);
    intprint(mallocated - lmallocated, 10, cur_errp);
    lputs(" bytes other\n", cur_errp);
    lfflush(cur_errp);
  }
}
SCM lroom(args)
     SCM args;
{
  intprint(cells_allocated, 10, cur_errp);
  lputs(" out of ", cur_errp);
  intprint(heap_size, 10, cur_errp);
  lputs(" cells in use, ", cur_errp);
  intprint(mallocated, 10, cur_errp);
  lputs(" bytes allocated (of ", cur_errp);
  intprint(mtrigger, 10, cur_errp);
  lputs(")\n", cur_errp);
  if NIMP(args) {
    heap_report();
    lputs("\n", cur_errp);
    stack_report();
  }
  return UNSPECIFIED;
}
void heap_report()
{
  sizet i = 0;
  lputs("; heap segments:", cur_errp);
  while(i<hplim_ind) {
    lputs("\n; 0x", cur_errp);
    intprint((long)hplims[i++], 16, cur_errp);
    lputs(" - 0x", cur_errp);
    intprint((long)hplims[i++], 16, cur_errp);
  }
}
void exit_report()
{
  if (verbose>2) {
    lputs(";Totals: ", cur_errp);
    intprint(time_in_msec(INUM(my_time())), 10, cur_errp);
    lputs(" mSec my time, ", cur_errp);
    intprint(time_in_msec(INUM(your_time())), 10, cur_errp);
    lputs(" mSec your time\n", cur_errp);
  }
}

SCM prolixity(arg)
     SCM arg;
{
  int old = verbose;
  if (!UNBNDP(arg)) {
    if FALSEP(arg) verbose = 1;
    else verbose = INUM(arg);
  }
  return MAKINUM(old);
}

void repl()
{
  SCM x;
  repl_report();
  while(1) {
    if OPOUTPORTP(cur_inp) {	/* This case for curses window */
      lfflush(cur_outp);
      if (verbose) lputs(PROMPT, cur_inp);
      lfflush(cur_inp);
    }
    else {
      if (verbose) lputs(PROMPT, cur_outp);
      lfflush(cur_outp);
    }
    lcells_allocated = cells_allocated;
    lmallocated = mallocated;
    x = lread(cur_inp);
    rt = INUM(my_time());
    gc_time_taken = 0;
    if (EOF_VAL==x) break;
    if (!CRDYP(cur_inp))	/* assure newline read (and transcripted) */
      lungetc(lgetc(cur_inp), cur_inp);
#ifdef __TURBOC__
    if ('\n' != CGETUN(cur_inp))
      if OPOUTPORTP(cur_inp)	/* This case for curses window */
	{lfflush(cur_outp); newline(cur_inp);}
      else newline(cur_outp);
#endif
    x = EVAL(x, (SCM)EOL);
    repl_report();
    iprin1(x, cur_outp, 1);
    lputc('\n', cur_outp);
  }
}
SCM quit(n)
     SCM n;
{
  if (UNBNDP(n) || BOOL_T==n) n = MAKINUM(EXIT_SUCCESS);
  if INUMP(n) exitval = n;
  else exitval = MAKINUM(EXIT_FAILURE);
  if (errjmp_bad) exit(INUM(exitval));
  dowinds(EOL, ilength(dynwinds));
  longjump(CONT(rootcont)->jmpbuf, COOKIE(-1));
}
SCM abrt()
{
  if (errjmp_bad) exit(EXIT_FAILURE);
  dowinds(EOL, ilength(dynwinds));
#ifdef CAUTIOUS
  stacktrace = EOL;
#endif
  longjump(CONT(rootcont)->jmpbuf, COOKIE(-2));
}
char s_restart[] = "restart";
SCM restart()
{
  /* ASSERT(!dumped, UNDEFINED, "dumped can't", s_restart); */
  dowinds(EOL, ilength(dynwinds));
#ifdef CAUTIOUS
  stacktrace = EOL;
#endif
  longjump(CONT(rootcont)->jmpbuf, COOKIE(-3));
}

char s_no_ep[] = "no execpath";
#define s_execpath (s_no_ep+3)
SCM scm_execpath(newpath)
     SCM newpath;
{
  SCM retval = execpath ? makfrom0str(execpath) : BOOL_F;
  if (UNBNDP(newpath))
    return retval;
  if (FALSEP(newpath)) {
    if (execpath) free(execpath);
    execpath = 0;
    return retval;
  }
  ASSERT(NIMP(newpath) && STRINGP(newpath), newpath, ARG1, s_execpath);
  if (execpath) free(execpath);
  if ((execpath = (char *)malloc((sizet)(LENGTH(newpath) + 1))))
    strncpy(execpath, CHARS(newpath), LENGTH(newpath) + 1);
  return retval;
}

#ifdef CAN_DUMP
char s_unexec[] = "unexec";
SCM scm_unexec(newpath)
     SCM newpath;
{
  ASSERT(NIMP(newpath) && STRINGP(newpath), newpath, ARG1, s_unexec);
  ASSERT(execpath, UNSPECIFIED, s_no_ep, s_unexec);
  *loc_errobj = newpath;
# ifdef CAUTIOUS
  stacktrace = EOL;
# endif
  longjump(CONT(rootcont)->jmpbuf, COOKIE(-4));
}
#endif

#ifdef CAREFUL_INTS
void ints_viol(sense)
     int sense;
{
  fputs(";ints already ", stderr);
  fputs(sense ? "en" : "dis", stderr);
  fputs("abled\n", stderr);
}
#endif

void han_sig()
{
  sig_deferred = 0;
  if (INT_SIGNAL != handle_it(INT_SIGNAL))
    wta(UNDEFINED, (char *)INT_SIGNAL, "");
}
void han_alrm()
{
  alrm_deferred = 0;
  if (ALRM_SIGNAL != handle_it(ALRM_SIGNAL))
    wta(UNDEFINED, (char *)ALRM_SIGNAL, "");
}

#ifdef TAIL_RECURSIVE_LOAD
SCM tryload(filename)
     SCM filename;
{
  ASSERT(NIMP(filename) && STRINGP(filename), filename, ARG1, s_load);
  {
    SCM oloadpath = *loc_loadpath;
    SCM oloadport = loadport;
    long olninum = linum;
    SCM port, newform = BOOL_F;
    port = open_file(filename, makfromstr("r", (sizet)sizeof(char)));
    if FALSEP(port) return port;
    *loc_loadpath = filename;
    loadport = port;
    linum = 1;
    while(1) {
      SCM form = newform;
      newform = lread(port);
      if (EOF_VAL==newform) {
	close_port(port);
	linum = olninum;
	loadport = oloadport;
	*loc_loadpath = oloadpath;
	SIDEVAL(form, EOL);
	return BOOL_T;
      }
      SIDEVAL(form, EOL);
    }
  }
}
#else
SCM tryload(filename)
     SCM filename;
{
  ASSERT(NIMP(filename) && STRINGP(filename), filename, ARG1, s_load);
  {
    SCM oloadpath = *loc_loadpath;
    SCM oloadport = loadport;
    long olninum = linum;
    SCM form, port;
    port = open_file(filename, makfromstr("r", (sizet)sizeof(char)));
    if FALSEP(port) return port;
    *loc_loadpath = filename;
    loadport = port;
    linum = 1;
    while(1) {
      form = lread(port);
      if (EOF_VAL==form) break;
      SIDEVAL(form, EOL);
    }
    close_port(port);
    linum = olninum;
    loadport = oloadport;
    *loc_loadpath = oloadpath;
  }
  return BOOL_T;
}
#endif

#ifdef CAUTIOUS
void scm_print_stack(stk)
     SCM stk;
{
  switch (ilength(stk)) {
  case -1:
    lputs("\n; circular stacktrace!", cur_errp);
    return;
  case -2:
    lputs("\n; stacktrace not a list?", cur_errp);
    iprin1(stk, cur_errp, 1);
    return;
  default:
    while NNULLP(stk) {
      SCM ste = CAR(stk);
      lputc('\n', cur_errp);
      iprin1(ste, cur_errp, 1);
      stk = CDR(stk);
    }
  }
}
SCM scm_stack_trace()
{
  if (0==ilength(stacktrace)) return BOOL_F;
  scm_print_stack(stacktrace);
  return BOOL_T;
}
#endif

static void err_head(str)
     char *str;
{
  int oerrno = errno;
  exitval = MAKINUM(EXIT_FAILURE);
  if NIMP(cur_outp) lfflush(cur_outp);
  lputc('\n', cur_errp);
  if(BOOL_F != *loc_loadpath) {
    iprin1(*loc_loadpath, cur_errp, 1);
    lputs(", line ", cur_errp);
    intprint((long)linum, 10, cur_errp);
    lputs(": ", cur_errp);
  }
  lfflush(cur_errp);
  errno = oerrno;
  if (cur_errp==def_errp) {
    if (errno>0) perror(str);
    fflush(stderr);
    return;
  }
}
void warn(str1, str2)
     char *str1, *str2;
{
  err_head("WARNING");
  lputs("WARNING: ", cur_errp);
  lputs(str1, cur_errp);
  if (str2) {
    lputs(str2, cur_errp);
    lputc('\n', cur_errp);
    lfflush(cur_errp);
  }
}

SCM lerrno(arg)
     SCM arg;
{
  int old = errno;
  if (!UNBNDP(arg)) {
    if FALSEP(arg) errno = 0;
    else errno = INUM(arg);
  }
  return MAKINUM(old);
}
static char s_perror[] = "perror";
SCM lperror(arg)
     SCM arg;
{
  ASSERT(NIMP(arg) && STRINGP(arg), arg, ARG1, s_perror);
  err_head(CHARS(arg));
  return UNSPECIFIED;
}
static void def_err_response()
{
  SCM obj = *loc_errobj;
#ifdef CAUTIOUS
  SCM stk = stacktrace;
#endif
  DEFER_INTS;
  err_head("ERROR");
  lputs("ERROR: ", cur_errp);
  if (err_s_subr && *err_s_subr) {
    lputs(err_s_subr, cur_errp);
    lputs(": ", cur_errp);
  }
  if (err_pos==(char *)ARG1 && UNBNDP(*loc_errobj)) err_pos = (char *)WNA;
#ifdef nosve
  if ((~0x1fL) & (short)err_pos) lputs(err_pos, cur_errp);
  else if (WNA>(short)err_pos) {
    lputs("Wrong type in arg", cur_errp);
    lputc(err_pos ? '0'+(short)err_pos : ' ', cur_errp);
  }
#else
  if ((~0x1fL) & (long)err_pos) lputs(err_pos, cur_errp);
  else if (WNA>(long)err_pos) {
    lputs("Wrong type in arg", cur_errp);
    lputc(err_pos ? '0'+(int)err_pos : ' ', cur_errp);
  }
#endif
  else {
    lputs(errmsgs[((int)err_pos)-WNA].msg, cur_errp);
    goto outobj;
  }
  if (IMP(obj) || SYMBOLP(obj) || (TYP16(obj)==tc7_port)
      || (NFALSEP(procedurep(obj))) || (NFALSEP(numberp(obj)))) {
outobj:
    if (!UNBNDP(obj)) {
      lputs(((long)err_pos==WNA)?" given ":" ", cur_errp);
      iprin1(obj, cur_errp, 1);
    }
  }
  else lputs(" (see errobj)", cur_errp);
#ifdef CAUTIOUS
  if NNULLP(stk) scm_print_stack(stk);
#endif
  if UNBNDP(err_exp) goto getout;
  if NIMP(err_exp) {
    lputs("\n; in expression: ", cur_errp);
    if NCONSP(err_exp) iprin1(err_exp, cur_errp, 1);
    else if (UNDEFINED==CDR(err_exp))
      iprin1(CAR(err_exp), cur_errp, 1);
    else iprlist("(... ", err_exp, ')', cur_errp, 1);
  }
  if NULLP(err_env) lputs("\n; in top level environment.", cur_errp);
  else {
    SCM env = err_env;
    lputs("\n; in scope:", cur_errp);
    while NNULLP(env) {
      lputc('\n', cur_errp);
      lputs(";   ", cur_errp);
      iprin1(CAR(CAR(env)), cur_errp, 1);
      env = CDR(env);
    }
  }
 getout:
  lputc('\n', cur_errp);
  lfflush(cur_errp);
  err_exp = err_env = UNDEFINED;
  if (errjmp_bad) {
    iprin1(obj, cur_errp, 1);
    lputs("\nFATAL ERROR DURING CRITICAL CODE SECTION\n", cur_errp);
#ifdef vms
    exit(EXIT_FAILURE);
#else
    exit(errno? (long)errno : EXIT_FAILURE);
#endif
  }
  errno = 0;
  ALLOW_INTS;
}
void everr(exp, env, arg, pos, s_subr)
     SCM exp, env, arg;
     char *pos, *s_subr;
{
  err_exp = exp;
  err_env = env;
  *loc_errobj = arg;
  err_pos = pos;
  err_s_subr = s_subr;
#ifndef CAUTIOUS
  if (((~0x1fL) & (long)pos) || (WNA>(long)pos)
      || NIMP(dynwinds) || errjmp_bad)
#endif
    {
      def_err_response();
      dowinds(EOL, ilength(dynwinds));
      abrt();
    }
#ifndef CAUTIOUS
  /* We don't have to clear stacktrace because CAUTIOUS never gets here */
  /* We don't have to dowinds() because dynwinds is EOL */
  longjump(CONT(rootcont)->jmpbuf, COOKIE((int)pos));
  /* will do error processing at stack base */
#endif
}
void wta(arg, pos, s_subr)
     SCM arg;
char *pos, *s_subr;
{
 everr(UNDEFINED, EOL, arg, pos, s_subr);
}
SCM cur_input_port()
{
  return cur_inp;
}
SCM cur_output_port()
{
  return cur_outp;
}
SCM cur_error_port()
{
  return cur_errp;
}
char s_cur_inp[] = "set-current-input-port";
char s_cur_outp[] = "set-current-output-port";
char s_cur_errp[] = "set-current-error-port";
SCM set_inp(port)
     SCM port;
{
  SCM oinp = cur_inp;
  ASSERT(NIMP(port) && OPINPORTP(port), port, ARG1, s_cur_inp);
  cur_inp = port;
  return oinp;
}
SCM set_outp(port)
     SCM port;
{
  SCM ooutp = cur_outp;
  ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG1, s_cur_outp);
  cur_outp = port;
  return ooutp;
}
SCM set_errp(port)
     SCM port;
{
  SCM oerrp = cur_errp;
  ASSERT(NIMP(port) && OPOUTPORTP(port), port, ARG1, s_cur_errp);
  cur_errp = port;
  return oerrp;
}

static iproc subr0s[] = {
	{&s_cur_inp[4], cur_input_port},
	{&s_cur_outp[4], cur_output_port},
	{&s_cur_errp[4], cur_error_port},
	{"transcript-off", trans_off},
	{"program-arguments", prog_args},
	{"line-number", line_num},
	{"abort", abrt},
	{s_restart, restart},
#ifdef CAUTIOUS
	{"stack-trace", scm_stack_trace},
#endif
	{0, 0}};

static iproc subr1s[] = {
	{s_cur_inp, set_inp},
	{s_cur_outp, set_outp},
	{s_cur_errp, set_errp},
	{"transcript-on", trans_on},
	{s_tryload, tryload},
	{s_load_string, scm_load_string},
	{s_eval_string, scm_eval_string},
	{s_perror, lperror},
	{"make-arbiter", makarb},
	{s_tryarb, tryarb},
	{s_relarb, relarb},
	{0, 0}};

static iproc subr1os[] = {
	{s_read, lread},
	{s_read_char, scm_read_char},
	{s_peek_char, peek_char},
	{s_newline, newline},
	{s_flush, lflush},
#ifndef GO32
	{s_char_readyp, char_readyp},
#endif
	{"quit", quit},
	{"verbose", prolixity},
	{"errno", lerrno},
	{s_execpath, scm_execpath},
	{0, 0}};

static iproc subr2os[] = {
	{s_write, lwrite},
	{s_display, display},
	{s_write_char, write_char},
#ifdef CAN_DUMP
	{s_unexec, scm_unexec},
#endif
	{0, 0}};

static smobfuns arbsmob = {markcdr, free0, prinarb};
char s_ccl[] = "char-code-limit";

void init_repl( iverbose )
     int iverbose;
{
	sysintern(s_ccl, MAKINUM(CHAR_CODE_LIMIT));
	loc_errobj = &CDR(sysintern("errobj", UNDEFINED));
	loc_loadpath = &CDR(sysintern("*load-pathname*", BOOL_F));
	transcript = BOOL_F;
	trans = 0;
	linum = 1;
	verbose = iverbose;
	init_iprocs(subr0s, tc7_subr_0);
	init_iprocs(subr1os, tc7_subr_1o);
	init_iprocs(subr1s, tc7_subr_1);
	init_iprocs(subr2os, tc7_subr_2o);
	make_subr("room", tc7_lsubr, lroom);
#ifndef GO32
	add_feature(s_char_readyp);
#endif
#ifdef CAN_DUMP
	add_feature("dump");
	scm_ldstr("\
(define (dump file . thunk)\n\
  (cond ((null? thunk) (set! *interactive* #f) (set! *argv* #f))\n\
	((not (car thunk)) (set! *argv* #f))\n\
	((boolean? (car thunk)))\n\
	(else (set! boot-tail (car thunk))))\n\
  (set! restart exec-self)\n\
  (require #f)\n\
  (unexec file))\n\
");
#endif
#ifdef ARM_ULIB
	set_erase();
#endif
	tc16_arbiter = newsmob(&arbsmob);
}
void final_repl()
{
  loc_errobj = (SCM *)&tmp_errobj;
  loc_loadpath = (SCM *)&tmp_loadpath;
  loadport = UNDEFINED;
  transcript = BOOL_F;
  trans = 0;
  linum = 1;
}