aboutsummaryrefslogtreecommitdiffstats
path: root/packages/openslug-init/openslug-init-0.10/devio.c
blob: bfaace2da18c175af5e42061ebaf3f4ae7f4bd2c (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
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
/* vi: set sw=4 ts=4: */
/*
 * devio: correctly read a region of a device
 *
 * A dd like program designed to read correctly from mtd character
 * (and maybe block) devices.  Allows access to specific regions
 * of the device and allows output of numbers from specific locations.
 *
 * Copyright (C) 2005 John Bowler <jbowler@acm.org>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifndef S_ISSOCK
#define S_ISSOCK(fd) 0
#endif

/* Define to 0 to remove the detailed help. */
#ifndef HELP
#define HELP 1
#endif

#ifndef STR_MAX
#define STR_MAX 4096
#endif

/* common error-and-die functions - reduces code size slightly. */
static int error_level = 1; /* Increased by write operations. */

/* NDEBUG will only save about 600 bytes! */
/* The noreturn attribute helps reduce size by 300 bytes, and removes
 * warning messages
 */
#if NDEBUG
static void do_die(const unsigned char *why) __attribute__((noreturn));
static void do_die(const unsigned char *why) {
	fprintf(stderr, "devio: %s\n", why);
	exit(error_level);
}
#define die(a,b) do_die(a)
#else
static void do_die(const unsigned char *why, const unsigned char *infile)
    __attribute__((noreturn));
static void do_die(const unsigned char *why, const unsigned char *infile) {
	fprintf(stderr, "devio: %s: %s\n", infile, why);
	exit(error_level);
}
#define die(a,b) do_die((a),(b))
#endif

#if NDEBUG
static void do_pdie(const unsigned char *why) __attribute__((noreturn));
static void do_pdie(const unsigned char *why) {
	fprintf(stderr, "devio: %s: %s\n", why, strerror(errno));
	exit(error_level);
}
#define pdie(a,b) do_pdie(a)
#else
static void do_pdie(const unsigned char *why, const unsigned char *infile)
    __attribute__((noreturn));
static void do_pdie(const unsigned char *why, const unsigned char *infile) {
	fprintf(stderr, "devio: %s: %s: %s\n", infile, why, strerror(errno));
	exit(error_level);
}
#define pdie(a,b) do_pdie((a),(b))
#endif

/* This is a non-standard assert but it saves quite a lot of
 * space (1kbyte) over the OS version.
 */
#if NDEBUG
#define assert(condition) do;while(0)
#elif 0 /* Expensive string asserts (lots of space in strings). */
static void do_assert(const unsigned char *why)
    __attribute__((noreturn));
static void do_assert(const unsigned char *why) {
	fprintf(stderr, "devio: internal error: %s\n", why);
	exit(error_level);
}
#define assert(condition) do if (!(condition)) do_assert(#condition); while (0)
#else
static void do_assert(int line) __attribute__((noreturn));
static void do_assert(int line) {
	fprintf(stderr, "devio: internal error: %d\n", line);
	exit(error_level);
}
#define assert(condition) do if (!(condition)) do_assert(__LINE__); while (0)
#endif

/* This is a non-ANSI extension. */
unsigned char *my_strdup(const unsigned char *from) {
	size_t cb = strlen(from)+1;
	unsigned char *to = malloc(cb);
	if (to == 0)
		die("out of memory", from);
	memcpy(to, from, cb);
	return to;
}


/***********************************************************************
 * mtd_file
 *
 * Basic device safe IO.
 * Set mtd_seek to set the desired read or write point.
 * Use mtd_getb and mtd_putb to read/write a single byte.
 * Use mtd_readbytes and mtd_writebytes to move multiple bytes.
 *********************************************************************/
/* File structure, used for read and write operations.  stdio() should do
 * everything this does pretty much except that this allows for no-write
 * buffering and it is 'weird' in that it won't overwrite beyond the end
 * of the data. */
typedef struct mtd_file {
	unsigned char* pname;
	int            fwrite;
	int            fverify;  /* do not write, just verify */
	int            fwritten; /* something to do! */
	int            fchanged; /* something was done! */
	int            fd;
	struct stat    stat;
	size_t         cbbuf;
	/* The user pointer is at 'useroffset', the buffer contains data from
	 * 'bufferoffset' to 'deviceoffset' (exclusive - the buffer may be empty), so
	 * the file descriptor is pointing to 'deviceoffset', which may be just beyond
	 * the end of the file.
	 */
	off_t          useroffset;   /* Current user position */
	off_t          bufferoffset; /* Base of current buffer */
	off_t          deviceoffset; /* End of current buffer */
	off_t          endoffset;    /* Length of char or block device. */
	unsigned char* pbuf;
	unsigned char* pwritebuf;
} mtd_file;


/* Initialise an mtd structure. */
static void init_mtd(mtd_file *pfile) {
	pfile->pname = 0;
	pfile->fwrite = 0;
	pfile->fverify = 0;
	pfile->fwritten = 0;
	pfile->fchanged = 0;
	pfile->fd = (-1);
	memset(&pfile->stat, 0, sizeof pfile->stat);
	pfile->cbbuf = 0;
	pfile->useroffset = 0;
	pfile->bufferoffset  = 0;
	pfile->deviceoffset = 0;
	pfile->endoffset = (off_t)-1;
	pfile->pbuf = 0;
	pfile->pwritebuf = 0;
}


/* Return the size, in bytes. */
static size_t size_mtd(mtd_file *pfile) {
	if (S_ISCHR(pfile->stat.st_mode) || S_ISBLK(pfile->stat.st_mode)) {
		if (pfile->endoffset == (off_t)-1) {
			off_t len;
			assert(pfile->stat.st_size == 0);
			assert(pfile->stat.st_blocks == 0);
			assert(pfile->stat.st_blksize > 0);
			/* So seek to the end then come back here. */
			len = lseek(pfile->fd, -pfile->stat.st_blksize, SEEK_END);
			if (len == (off_t)-1)
				pdie("lseek(length)", pfile->pname);
			if (lseek(pfile->fd, pfile->deviceoffset, SEEK_SET) != pfile->deviceoffset)
				pdie("lseek(length reset)", pfile->pname);
			len += pfile->stat.st_blksize;
			pfile->endoffset = len;
		}
		return pfile->endoffset;
	} else if (S_ISDIR(pfile->stat.st_mode) || S_ISFIFO(pfile->stat.st_mode) ||
				S_ISSOCK(pfile->stat.st_mode))
		die("cannot find size of this device", pfile->pname);
	else
		return pfile->stat.st_size;
}


/* Open the named file for read or write, the structure is initialised
 * appropriately.  The name is copied. */
static void new_mtd(mtd_file *pfile, const char *pname, int fwrite, int fverify, int fd) {
	pfile->pname = my_strdup(pname);
	pfile->fwrite = fwrite;
	pfile->fverify = fverify;
	pfile->fwritten = 0;
	pfile->fchanged = 0;
	pfile->fd = fd;

	if (fstat(fd, &pfile->stat) != 0)
		pdie("fstat", pname);
	/* This can be made to work with fifos on read because it is possible
	 * to seek by reading so long as we only seek forward, but it really
	 * isn't worth spending time on this.
	 */
	if (S_ISDIR(pfile->stat.st_mode) || S_ISFIFO(pfile->stat.st_mode) ||
			S_ISSOCK(pfile->stat.st_mode))
		die("invalid device", pname);
	/* Allow writing to a file for testing - i.e. S_ISREG is fine above. */
	pfile->cbbuf = pfile->stat.st_blksize;
	if (pfile->cbbuf == 0)
		pfile->cbbuf = 4096;
	pfile->useroffset = 0;
	pfile->bufferoffset = 0;
	pfile->deviceoffset = 0;
	pfile->pbuf = 0;
	pfile->pwritebuf = 0;
}

static void open_mtd(mtd_file *pfile, const char *pname, int fwrite, int fverify) {
	int fd = open(pname, (fwrite && !fverify) ? O_RDWR : O_RDONLY);
	if (fd < 0)
		pdie("open", pname);
	else if (fd < 3)
		die("no standard streams", "-");
	new_mtd(pfile, pname, fwrite, fverify, fd);
}


/* Do the actual write.  Any pending write buffers are checked and output
 * to the device.  Happens on close and can be called before.  Does not
 * do an fsync.  The fwritten flag indicates that write_mtd needs to be
 * called, the fchanegd flag indicates that something has been written and
 * an fdatasync needs to happen before the close.
 */
static void write_mtd(mtd_file *pfile) {
	if (pfile->fwritten) {
		size_t count = pfile->deviceoffset - pfile->bufferoffset;
		unsigned char *pbuf = pfile->pwritebuf;

		assert(pfile->pbuf != 0);
		assert(pbuf != 0);
		assert(pfile->deviceoffset > pfile->bufferoffset);
		assert(pfile->deviceoffset <= pfile->bufferoffset + pfile->cbbuf);
		/* If it changed write it. */
		if (memcmp(pfile->pbuf, pbuf, count) != 0) {
			/* If verifying the verify just failed... */
			if (pfile->fverify)
				die("verification failed", pfile->pname);

			/* So write the whole of this buffer back.  Do not do a sync here
			 * because that would force a complete write of the flash erase
			 * block - not good.
			 */
			if (lseek(pfile->fd, pfile->bufferoffset, SEEK_SET) != pfile->bufferoffset)
				pdie("lseek(write)", pfile->pname);
			/* write, well, you have to keep doing it until it works, you
			 * also have to RTFM several times to get this write, so if
			 * this looks wrong please fix it.  No, not that, that was
			 * deliberate.
			 */
			do {
				ssize_t cb = write(pfile->fd, pbuf, count);
				assert(cb != 0);
				assert(cb <= count);
				if (cb < 0) switch (errno) {
				case EINTR:  /* shall we try that again then? */
					/* This is the common case - this does happen, it is
					 * necessary to deal with it and it is sufficient to
					 * try again.
					 */
					break;
				case EAGAIN: /* what, oh well, if at once you don't succeed. */
					/* We didn't say O_NONBLOCK above so this should never
					 * happen, however it has.  The code will therefore go into
					 * a tight loop in the manner of a certain Scottish nobleman.
					 */
					break;
				case EPIPE:  /* you don't love me any more. */
					/* This is a little difficult, it means we were squirting
					 * data down a pipe, so somehow someone has managed to work
					 * out both how to create a named pipe and how much fun to
					 * have by passing it to this program on the command line,
					 * then they have worked out how to make the shell ignore
					 * SIGPIPE in a spawned program (possible with some shells)
					 * then they want to see the really dumb message that comes
					 * out as a result.  We just say no.
					 */
					exit(1);
				default:
					pdie("write", pfile->pname);
				} else {
					count -= cb;
					/* It is now necessary to fdatasync this file descriptor
					 * to ensure that this data really does get to its final
					 * destination.  (Note that even this is probably not certain
					 * if the destination is a disk with a RAM buffer - which
					 * means *any* disk these days.)
					 */
					pfile->fchanged = 1;
					/* Something has been written to flash, but not everything
					 * has (necessarily) been written yet, so if something goes
					 * wrong after this point we are in deep, deep, trouble.
					 */
					error_level = 3;
				}
			} while (count > 0);

			/* So now the device matches the write buffer and the device
			 * pointer is back where it was before.
			 */
			memcpy(pfile->pbuf, pfile->pwritebuf, pfile->deviceoffset-pfile->bufferoffset);
		}
		/* Nothing remains to write from this buffer (hence nothing at all
		 * for this device.)
		 */
		pfile->fwritten = 0;
	}
}


/* Close the file, if anything was written out does an fsync.
 */
static void close_mtd(mtd_file *pfile) {
	write_mtd(pfile);
	assert(!pfile->fwritten);
	if (pfile->pbuf != 0) {
		free(pfile->pbuf);
		pfile->pbuf = 0;
	}
	if (pfile->pwritebuf != 0) {
		free(pfile->pwritebuf);
		pfile->pwritebuf = 0;
	}
	if (pfile->fd >= 0) {
		/* For a write file be very very careful.  For read ignore errors:
		 * it is more important to successfully write than to whine about
		 * strange close errors from a file we don't care about.  For a
		 * write file with nothing written we don't care either.
		 */
		if (pfile->fchanged) {
			/* This is the all important bit.  Doing the fdatasync is what
			 * flushes the data to the flash.  If this isn't done there is
			 * no guarantee that close will detect a write error, 'cause the
			 * flash may not have completed the write before the close
			 * returns.
			 */
			if (fdatasync(pfile->fd) != 0) {
				/* Trying an fdatasync on a pipe, etc, is silly, but we do
				 * it anyway.  EROFS means we just tried to write to a
				 * read only file system, safe but still an error.
				 */
				if (errno != EINVAL)
					pdie("sync", pfile->pname);
			}
			if (close(pfile->fd) != 0)
				pdie("close", pfile->pname);
		} else
			(void)close(pfile->fd);
		pfile->fd = (-1);
	}
	if (pfile->pname != 0) {
		free(pfile->pname);
		pfile->pname = 0;
	}
	init_mtd(pfile);
}


/* Obtain an input and, if necessary, an output buffer. */
static void buffer_mtd(mtd_file *pfile) {
	if (pfile->pbuf == 0) {
		size_t blksize = pfile->cbbuf;
		assert(blksize > 0);
		assert(pfile->pwritebuf == 0);

		/* Get blksize bytes (note: things could be speeded up by aligning
		 * the buffer but this really doesn't matter, all the time goes in
		 * read/write of the flash!)
		 */
		pfile->pbuf = malloc(blksize);
		if (pfile->fwrite)
			pfile->pwritebuf = malloc(blksize);
		if (pfile->pbuf == 0 || (pfile->fwrite && pfile->pwritebuf == 0))
			die("out of memory", pfile->pname);
	}
}


/* Read some data including the current user position.  This will also *write* data
 * if something is waiting to be written.
 *
 * NOTE: in the original design I conceived of some scheme whereby all the writes
 * would be buffered up for the end, but I can't see how this would actually help
 * anything because even if data has to be read from the device to determine read
 * locations it tends to happen before the relevant writes.  In the access patterns
 * I know (they are very simple - and that is important in itself) there is never
 * a need to read from a write device.
 */
static void read_mtd(mtd_file *pfile) {
	size_t cbread;
	int ioffset;

	/* 'useroffset' is where we need to read from, 'deviceoffset' is where we are
	 * at (sic) and 'bufferoffset'..'deviceoffset' is what we have already.
	 */
	if (pfile->useroffset >= pfile->bufferoffset &&
		pfile->useroffset < pfile->deviceoffset)
		return;

	if (pfile->useroffset < 0 || pfile->useroffset >= size_mtd(pfile))
		die("read outside file", pfile->pname);

	/* Make sure there is a buffer. */
	buffer_mtd(pfile);

	/* This is the maximum amount which can be read. */
	cbread = pfile->cbbuf;
	if (pfile->useroffset >= pfile->bufferoffset &&
		pfile->useroffset < pfile->bufferoffset + cbread) {
		/* Just fill the rest of the buffer. */
		ioffset = pfile->deviceoffset - pfile->bufferoffset;

		assert(pfile->deviceoffset < pfile->bufferoffset + cbread);
		cbread -= ioffset;
	} else {
		off_t base;

		/* We to move the buffer therefore  any pending write needs to be flushed. */
		write_mtd(pfile);
		assert(!pfile->fwritten);

		/* Seek to the aligned buffer boundary if necessary. */
		base = (pfile->useroffset / cbread) * cbread;
		if (base != pfile->deviceoffset) {
			if (lseek(pfile->fd, base, SEEK_SET) != base)
				pdie("lseek(read)", pfile->pname);
			pfile->deviceoffset = base;
		}
		pfile->bufferoffset = base;
		ioffset = 0;
	}

	/* Reading is like writing, EINTR can stop it succeeding but is a
	 * continuable error.
	 */
	assert(pfile->bufferoffset <= pfile->useroffset);
	assert(pfile->useroffset < pfile->bufferoffset + cbread);
	assert(pfile->deviceoffset <= pfile->useroffset);
	do {
		ssize_t cb = read(pfile->fd, pfile->pbuf+ioffset, cbread);
		if (cb < 0) switch (errno) {
		case EINTR:  /* simple restart */
			/* POSIX allows this to happen when something has been
			 * read.  Reset the file pointer just in case.
			 */
			if (lseek(pfile->fd, pfile->deviceoffset, SEEK_SET) != pfile->deviceoffset)
				pdie("lseek(read reset)", pfile->pname);
			break;
		case EAGAIN: /* O_NONBLOCK on the input? */
			break;
		default:
			pdie("read", pfile->pname);
		} else if (cb == 0) {
			die("unexpected end of file", pfile->pname);
		} else {
			/* Save a copy of the data so that it can be written out again
			 * by a write file.
			 */
			if (pfile->pwritebuf != 0)
				memcpy(pfile->pwritebuf+ioffset, pfile->pbuf+ioffset, cb);
			cbread -= cb;
			ioffset += cb;
			pfile->deviceoffset += cb;
		}
	} while (cbread > 0 && pfile->useroffset >= pfile->deviceoffset);

	assert(pfile->useroffset < pfile->deviceoffset);
}


/* Basic IO - these are the functions to use, not the internal read/write
 * functions above.
 */
/* Set the current read/write pointer on this file. */
#if 0 /*UNUSED*/
static void mtd_seek(mtd_file *pfile, off_t offset) {
	pfile->useroffset = offset;
}
#endif


/* Get a single byte (returned) and advance the read pointer by one. */
static unsigned char mtd_getb(mtd_file *pfile) {
	read_mtd(pfile);
	return (pfile->fwrite ? pfile->pwritebuf : pfile->pbuf)[
		pfile->useroffset++ - pfile->bufferoffset];
}


/* Store a single byte in a write file and advance the pointer by one. */
static void mtd_putb(mtd_file *pfile, unsigned long b) {
	if (!pfile->fwrite)
		die("file is not writeable", pfile->pname);
	read_mtd(pfile);
	if (b != pfile->pwritebuf[pfile->useroffset-pfile->bufferoffset]) {
		pfile->pwritebuf[pfile->useroffset-pfile->bufferoffset] = b;
		pfile->fwritten = 1;
	}
	++(pfile->useroffset);
}


/* Read a given number of bytes, which must exist in the file, and
 * advance the pointer by that amount.
 */
static void mtd_readbytes(mtd_file *pfile, unsigned char *pbuf, size_t cb) {
	if (pfile->useroffset+cb > size_mtd(pfile))
		die("read beyond end of file", pfile->pname);

	while (cb > 0) {
		int cbavail;

		read_mtd(pfile);
		cbavail = pfile->deviceoffset - pfile->useroffset;
		assert(cbavail > 0 && cbavail <= pfile->cbbuf);
		if (cbavail > cb)
			cbavail = cb;

		assert(pfile->useroffset >= pfile->bufferoffset);
		assert(pfile->useroffset < pfile->deviceoffset);
		assert(pfile->deviceoffset <= pfile->bufferoffset + pfile->cbbuf);

		memcpy(pbuf, (pfile->fwrite ? pfile->pwritebuf : pfile->pbuf) +
					(pfile->useroffset-pfile->bufferoffset), cbavail);
		pfile->useroffset += cbavail;
		pbuf += cbavail;
		cb -= cbavail;
	}
}


/* Write a given number of bytes and advance the pointer.  As with readbytes
 * the bytes must already exist in the file - mtd_file will never extend the
 * file only change existing bytes.
 */
static void mtd_writebytes(mtd_file *pfile, const unsigned char *pbuf, size_t cb) {
	if (!pfile->fwrite)
		die("file is not writeable", pfile->pname);
	if (pfile->useroffset+cb > size_mtd(pfile))
		die("write beyond end of file", pfile->pname);
	while (cb > 0) {
		int cbavail;

		/* This may look strange but it is correct - this code always reads
		 * before it writes to avoid unnecessary writes.
		 */
		read_mtd(pfile);
		cbavail = pfile->deviceoffset - pfile->useroffset;
		if (cbavail > cb)
			cbavail = cb;
		memcpy(pfile->pwritebuf + (pfile->useroffset-pfile->bufferoffset), pbuf, cbavail);
		pfile->fwritten = 1;
		pfile->useroffset += cbavail;
		pbuf += cbavail;
		cb -= cbavail;
	}
}


#if 0 /* Commented out because I don't think this is worth while. */
/* Copy bytes from the pointer in one file to the pointer in another
 * file (avoids an intermediate buffer compared to readbytes/writebytes.)
 */
static void mtd_copy(mtd_file *pto, mtd_file *pfrom, size_t cb) {
	int cbfrom, cbto;

	if (!pto->fwrite)
		die("file is not writeable", pto->pname);
	if (pto->useroffset+cb > size_mtd(pto))
		die("write beyond end of file", pto->pname);
	if (pfrom->useroffset+cb > size_mtd(pfrom))
		die("read beyond end of file", pfrom->pname);
	/* Copying from and to the same place has no effect. */
	if (pfrom == pto)
		return;

	cbfrom = cbto = 0;
	while (cb > 0) {
		int cbavail;

		if (cbfrom <= 0) {
			read_mtd(pfrom);
			cbfrom = pfrom->deviceoffset - pfrom->useroffset;
		 compared to readbytes/writebytes}
		if (cbto <= 0) {
			read_mtd(pto);
			cbto = pto->deviceoffset - pto->useroffset;
		}

		/* Take the smallest byte count and copy it. */
		cbavail = cbfrom;
		if (cbavail > cbto)
			cbavail = cbto;
		if (cbavail > cb)
			cbavail = cb;

		memcpy(pto->pwritebuf + (pto->useroffset-pto->bufferoffset),
				pfrom->pbuf + (pfrom->useroffset-pfrom->bufferoffset),
				cbavail);
		pto->fwritten = 1;

		pto->useroffset += cbavail;
		cbto -= cbavail;
		pfrom->useroffset += cbavail;
		cbfrom -= cbavail;

		cb -= cbavail;
	}
}
#endif


/***********************************************************************
 * parse
 *
 * Parse a command line option or a single line.  See the help below
 * for details...
 ***********************************************************************/
#define STACK_BASE 8
#define STACK_SIZE 256
#define NUM_FILES  16
typedef struct parse_buf {
	int           fverify; /* Just verifying, do no write. */
	int           cstack;
	int           fbreak;  /* Break in an expression. */
	mtd_file*     pfrom;
	mtd_file*     pto;

	/* The buffers. */
	unsigned long variables[256];
	unsigned long stack[STACK_SIZE];
	mtd_file      files[NUM_FILES];
} parse_buf;


/* Initialiser. */
static void init_parse(parse_buf *pp, int fverify) {
	int i;
	memset(pp, 0, sizeof *pp);
	pp->fverify = fverify;
	pp->cstack = STACK_BASE;
	pp->fbreak = 0;
	pp->pfrom = 0;
	pp->pto = 0;
	for (i=0; i<NUM_FILES; ++i)
		init_mtd(pp->files+i);
}


/* Terminator. */
static void quit(parse_buf *pp, int exit_code) __attribute__((noreturn));
static void quit(parse_buf *pp, int exit_code) {
	int i;
	/* Close all the files. */
	for (i=0; i<NUM_FILES; ++i)
		if (pp->files[i].pname != 0)
			close_mtd(pp->files+i);

	/* And make sure the output worked too. */
	if (fflush(stdout) == EOF || ferror(stdout) || fclose(stdout) == EOF)
		pdie("output failed", "stdout");

	exit(exit_code);
}


/* Input a single byte. */
static unsigned char inb(parse_buf *pp) {
	int b;
	if (pp->pfrom == 0) {
		b = getchar();
		if (b == EOF)
			pdie("read error", "stdin");
	} else {
		b = mtd_getb(pp->pfrom);
	}
	return b;
}


/* Output a single byte. */
static void outb(parse_buf *pp, unsigned long b) {
	if (pp->pto == 0) {
		if (putchar(b) == EOF)
			pdie("write error", "stdout");
	} else {
		mtd_putb(pp->pto, b);
	}
}


/* Output these bytes. */
static void outputbytes(parse_buf *pp, const char *pbuf, size_t cb) {
	if (pp->pto == 0) {
		if (fwrite(pbuf, cb, 1, stdout) != 1)
			pdie("write error", "stdout");
	} else
		mtd_writebytes(pp->pto, pbuf, cb);
}

/* Copy a stream of bytes. */
static void copybytes(parse_buf *pp, size_t cb) {
	while (cb > 0) {
		size_t cbavail = cb;
		unsigned char buf[1024];
		if (cbavail > sizeof buf)
			cbavail = sizeof buf;
		if (pp->pfrom == 0) {
			if (fread(buf, cbavail, 1, stdin) != 1)
				pdie("read error", "stdin");
		} else
			mtd_readbytes(pp->pfrom, buf, cbavail);

		outputbytes(pp, buf, cbavail);

		cb -= cbavail;
	}
}


/* Fill the output with a count of bytes of a given value. */
static void fillbytes(parse_buf *pp, unsigned long val, size_t cb) {
	unsigned char buf[1024];
	memset(buf, val, sizeof buf);

	while (cb > 0) {
		size_t cbavail = cb;
		if (cbavail > sizeof buf)
			cbavail = sizeof buf;

		outputbytes(pp, buf, cbavail);

		cb -= cbavail;
	}
}


/* Push a single numeric value onto the stack. */
static void push(parse_buf *pp, unsigned long num, const unsigned char *str) {
	if (pp->cstack >= STACK_SIZE)
		die("stack overflow", str);
	pp->stack[pp->cstack++] = num;
}


/* Pop one or move variables. */
static void pop(parse_buf *pp, int num, const unsigned char *str) {
	if (pp->cstack < STACK_BASE+num)
		die("stack underflow", str);
	pp->cstack -= num;
}

/* Return (and pop) the top of stack. */
static unsigned long top(parse_buf *pp, const unsigned char *str) {
	if (pp->cstack <= STACK_BASE)
		die("stack underflow", str);
	return pp->stack[--(pp->cstack)];
}


/* Store the result of an operator. */
static void op(parse_buf *pp, int numpop, unsigned long num,
		const unsigned char *str) {
	pop(pp, numpop, str);
	push(pp, num, str);
}


/* Parse a single expression, which may be empty.  The conditional execution
 * stuff is identical to that for a command except that (:?) are used instead
 * of $($:$?$)
 */
static int parse_expression(parse_buf *pp, const unsigned char *line, int Ac, int AcEnd) {
	int SP = 0, fnoexec = 0, test = 0;
	int stack[16];

	for (;Ac<AcEnd;++Ac) {
		const unsigned char *lp = line+Ac;
		unsigned char ch = *lp;
		switch (ch) {
			/* Control flow.  These operators have to explicitly check
			 * the fnoexec state because they manipulate it.
			 */
		case '(': /* if */
			if (SP >= 16)
				die("() stack overflow", lp);
			stack[SP++] = Ac;
			if (fnoexec) {
				fnoexec += 3;
			} else {
				fnoexec = top(pp, lp) == 0;
			}
			break;

		case '[': /* test start */
			/* If fnoexec >= 3 the whole block is disabled. */
			if (fnoexec <= 2) {
				/* Valid only inside an () block and there should only
				 * be one active at once.
				 */
				if (test != 0 || SP <= 0)
					goto badnest;
				/* Record the start of the test. */
				test = Ac+1;
				/* If the previous block executed record this. */
				if (fnoexec == 0)
					fnoexec = 2;
			}
			break;

		case ':': /* elif */
			/* fnoexec is 1 if nothing has executed in this block yet,
			 * and if the block itself is executing, it is 2 if something
			 * did execute, it is >2 for a non-executed block, including
			 * one with a break.
			 */
			if (fnoexec <= 2) {
				if (test == 0 || SP <= 0)
					goto badnest;

				assert(fnoexec > 0);
				assert(!pp->fbreak);
				/* 1: nothing has executed yet.
				 * 2: an if or elif has executed.
				 */
				if (fnoexec == 1) {
					/* Parse the test.  If this results in a break no
					 * condition is popped from the stack, otherwise
					 * the condition which the expression should push
					 * is popped.
					 */
					(void)parse_expression(pp, line, test, Ac);
					if (pp->fbreak) {
						fnoexec = 3;
						pp->fbreak = 0;
					} else
						fnoexec = top(pp, lp) == 0;
				}

				/* And the test has been consumed. */
				test = 0;
			}
			break;

		case ')': /* end+loop if */
			/* The stack must always be popped. */
			if (SP <= 0)
				goto badnest;
			--SP;
			/* If fnoexec>2 then this is a nested disabled block or, in
			 * the case of 3, a break.  In neither case is the expression
			 * evaluated and the test setting is for an enclosing block.
			 */
			if (fnoexec > 2) {
				fnoexec -= 3;
			} else {
				/* In this case there must be a test. */
				if (test == 0)
					goto badnest;

				/* Execution resumes regardless. */
				fnoexec = 0;
				assert(!pp->fbreak);

				/* So make the loop test now - this may cause a branch back
				 * to the ( and that will push the stack again.  Evaluate
				 * the test.
				 */
				(void)parse_expression(pp, line, test, Ac);
				if (pp->fbreak)
					pp->fbreak = 0;
				else if (top(pp, lp) != 0) {
					Ac = stack[SP]-1; /* Ac is incremented below */
				}

				/* And the test has been consumed. */
				test = 0;
			}
			break;

		badnest:
			die("bad [: or [) nesting", lp);
			break;

		case ';':
		case '\n':
			/* end of line terminates the loop, but Ac is stepped beyond the
			 * terminator.
			 */
			++Ac;
			goto end;

		case ' ':
		case '\f':
		case '\r':
		case '\t':
		case '\v':
		case ',':  /* Treat as a space */
			/* Skip other white space. */
			break;

			/* Everything else is glommed together because the fnoexec case
			 * can be simply handled by skipping character-by-character (because
			 * (:?); do not occur inside numbers!)
			 */
		default:
			if (fnoexec)
				break;

			if (isupper(ch))
				push(pp, pp->variables[ch], lp);
			else if (isdigit(ch)) {
				char *end = (char*)lp;
				unsigned long num;
				errno = 0;
				num = strtoul(lp, &end, 0);
				if (num == ULONG_MAX && (errno == EINVAL || errno == ERANGE))
					pdie("invalid number", lp);
				push(pp, num, lp);
				/* strotul returns a pointer to the first invalid character in
				 * end, so Ac becomes end-line-1, because it is incremented below.
				 */
				Ac = (const unsigned char*)end-line-1;
			} else {
				/* The operators are handled here.  An unrecognised character is an
				 * error at this point.  The left, right are always valid because
				 * the stack has 8 unused slots at the top...
				 */
				unsigned long left = pp->stack[pp->cstack - 2];
				unsigned long right = pp->stack[pp->cstack - 1];

				switch (ch) {
				case '?': /* break */
					/* break inside a condition is actually allowed, so this may
					 * happen with SP==0 while evaluating a condition.  For the
					 * moment ? is also allowed outside a loop, it terminates the
					 * processing of the whole expression.
					 */
					if (top(pp, lp) != 0) {
						/* break: skip to the ) and do not do the test on
						 * that either.
						 */
						fnoexec = 3;
					}
					break;

				#define DIOP(operator) op(pp, 2, left operator right, lp); break
				#define MONOP(operator) op(pp, 1, operator right, lp); break
					/* The C operators */
				case '*': DIOP(*);
				case '+': DIOP(+);
				case '-': DIOP(-);
				case '/': DIOP(/);
				case '%': DIOP(%);
				case '<': DIOP(<);
				case '>': DIOP(>);
				case '|': DIOP(|);
				case '&': DIOP(&);
				case '^': DIOP(^);
				case '~': MONOP(~);
				case '!': MONOP(!);
				case '=': /* equality */
					op(pp, 2, left == right, lp);
					break;
				case '{': /* shift left */
					op(pp, 2, left << right, lp);
					break;
				case '}': /* shift right */
					op(pp, 2, left >> right, lp);
					break;
				case 'r': /* rotate right */
					op(pp, 2, (left >> right) + (left << (32-right)), lp);
					break;
				case 'e': /* sign extend (right is number of valid bits). */
					op(pp, 2, (long)(left << (32-right)) >> (32-right), lp);
					break;
				case 'm': /* mask (right is number of valid bits). */
					op(pp, 2, (left << (32-right)) >> (32-right), lp);
					break;
				case '$': /* Size of input. */
					if (pp->pfrom == 0)
						die("size of input unknown", lp);
					push(pp, size_mtd(pp->pfrom), lp);
					break;
				case 'f': /* position of input ('from' pointer). */
					if (pp->pfrom == 0)
						die("position of input unknown", lp);
					push(pp, pp->pfrom->useroffset, lp);
					break;
				case '#': /* Size of output. */
					if (pp->pto == 0)
						die("size of output unknown", lp);
					push(pp, size_mtd(pp->pto), lp);
					break;
				case 't': /* position of output ('to' pointer). */
					if (pp->pto == 0)
						die("position of output unknown", lp);
					push(pp, pp->pto->useroffset, lp);
					break;
				case 'd': /* device number of the input device */
					if (pp->pfrom == 0)
						die("input device number unknown", lp);
					push(pp, pp->pfrom->stat.st_rdev == 0 ? pp->pfrom->stat.st_dev :
							pp->pfrom->stat.st_rdev, lp);
					break;
				case '@': /* one byte read. */
					push(pp, inb(pp), lp);
					break;
				case 'b': /* big endian 4 byte read. */
					#define P(st) (void)parse_expression(pp, st, 0, (sizeof st)-1)
					P("@8{@+8{@+8{@+;# 4 byte big-endian read");
					break;
				case 'l': /* little endian 4 byte read. */
					P("@@@@8{+8{+8{+;# 4 byte little-endian read");
					break;
				case '.': /* copy (dup) */
					push(pp, right, lp);
					break;
				case 'p': /* pop */
					pop(pp, 1, lp);
					break;
				case 's': /* swap (top two elements of the stack) */
					pop(pp, 2, lp);
					push(pp, right, lp);
					push(pp, left, lp);
					break;
				default:
					die("invalid operator", lp);
				}
			}
		}
	}

	/* Here on terminator or Ac==AcEnd. */
end:
	/* If SP>0 then there was some bad nesting going on - i.e. the brackets have
	 * not been closed.  fnoexec must be zero if SP is 0.
	 */
	if (SP > 0)
		die("unclosed ( )", line);
	assert(fnoexec == 0 || fnoexec == 3);
	if (fnoexec == 3)
		pp->fbreak = 1;
	return Ac;
}


/* Parse an expression and return numbers off the top of stack. */
static int need(parse_buf *pp, unsigned long arg[2],
		const unsigned char *line, int Ac, int AcEnd, int num) {
	int Acnew = parse_expression(pp, line, Ac, AcEnd);

	assert(num <= 2);
	if (pp->cstack < STACK_BASE+num)
		die("too few arguments", line+Ac);

	while (num > 0)
		arg[--num] = pp->stack[--(pp->cstack)];

	/* cancel a break at the top level. */
	pp->fbreak = 0;
	return Acnew;
}


/* Parse a string.  The string ends up in the buffer and is limited in
 * size to STR_MAX.  The API returns a null string for empty quoted
 * strings and for the unquoted case where there is only whitespace
 * in the rest of the command.  The result is the index of the first
 * character after the end of the command (*not* the end of the string.)
 */
static int string(unsigned char *buffer, const unsigned char *line, int Ac) {
	int start, end;

	while (isspace(line[Ac]) && line[Ac] != '\n') ++Ac;

	if (line[Ac] == '"' || line[Ac] == '\'') {
		const unsigned char quote = line[Ac];
		start = Ac;
		end = Ac+1;
		while (line[end] != 0 && line[end] != '\n' && line[end] != quote) ++end;
		if (line[end] != quote)
			die("unterminated quoted string", line+start);
		Ac = end+1;
		while (isspace(line[Ac]) && line[Ac] != '\n') ++Ac;
		if (line[Ac] != 0 && line[Ac] != ';' && line[Ac] != '\n')
			die("stuff on line after quoted string", line+start);
		++start;
	} else {
		start = Ac;
		while (line[Ac] != 0 && line[Ac] != ';' && line[Ac] != '\n') ++Ac;
		end = Ac;
	}

	end -= start;
	if (end >= STR_MAX)
		die("string too long", line+start);

	/* Copy out the string into the buffer and null terminate it. */
	memcpy(buffer, line+start, end);
	buffer[end] = 0;

	/* Skip over EOL, if present, and store the string pointer back. */
	if (line[Ac] != 0) ++Ac;
	return Ac;
}


/* Find the end of line, Ac always points to the start. */
static int eol(const unsigned char *line, int Ac) {
	unsigned char quote = 0;
	while (line[Ac] != '\n' && (line[Ac] != ';' || quote)) {
		if (line[Ac] == 0)
			return Ac;
		if (line[Ac] == '"' || line[Ac] == '\"') {
			if (quote == 0)
				quote = line[Ac];
			else if (quote == line[Ac])
				quote = 0;
		}
		++Ac;
	}
	return Ac+1;
}


/* Find a file given its name. */
static mtd_file *find_file(parse_buf *pp, const unsigned char *name) {
	int i;
	for (i=0; i<NUM_FILES; ++i) {
		if (pp->files[i].pname != 0 &&
			strcmp(name, pp->files[i].pname) == 0)
			return pp->files+i;
	}
	return 0;
}


/* Open a file for read or write. */
static mtd_file *open_file(parse_buf *pp, const unsigned char *name, int fwrite) {
	int i;
	for (i=0; i<NUM_FILES; ++i)
		if (pp->files[i].pname == 0)
			break;
	if (i >= NUM_FILES)
		die("no more files", name);
	open_mtd(pp->files+i, name, fwrite, pp->fverify);
	return pp->files+i;
}


/* Basic parse function.  Does all the work.  Simple line-by-line command
 * parser.  The input is a vector of strings (an argv), 'input' says where
 * it came from.
 */
static void parse(parse_buf *pp, int lines, const char **prog) {
	/* The program pointer is a line index Al and a character within the
	 * line Ac.  The exec stack just has those references (i.e. (Al,Ac)
	 * is a PC).
	 */
	int Al = 0, SP = 0, fnoexec = 0;
	struct stack {
		int Al, Ac;
	} stack[16];

	/* fnoexec has these values:
	 *    0: normal execution
	 *    1: within an if $($:$) and none of the tests have passed (so
	 *       nothing has been executed in this block.)
	 *    2: within an if and a test has passed (a previous block has
	 *       been executed.)
	 *    3: within an if and a break ($?) has succeeded, execute nothing
	 *       until *and including* the $) (i.e. do not evaluate the value
	 *       on that either.)
	 * This applies to nested if blocks too when the block is executable.
	 * If a block is nested within a non-executing environment the $(
	 * adds 3 to fnoexec.  The $) always subtracts 3 unless fnoexec is
	 * 0, 1, 2 or 3 thus the prior state is reliably restored.
	 */

	while (Al < lines) {
		int Ac = 0;
		const unsigned char *line = prog[Al];
		const int AcEnd = strlen(line);

		while (line[Ac]) {
			const unsigned char *lp = line+Ac;
			unsigned char ch = *lp;

			/* A command is two characters, except that ';' is end-of-line,
			 * skip these things until we see a command, then use the next
			 * two characters.  line[Ac] != 0 so the read of Ac+1 is safe.
			 */
			if (isspace(ch) || ch == ';') {
				++Ac;
			} else if ((ch != '$' && fnoexec) || ch == '#') {
				/* disabled execution and not a control flow command, or
				 * a comment.
				 */
				Ac = eol(line, Ac);
			} else switch ((ch << 8) + line[Ac+1]) {
				unsigned long arg[2];
				unsigned char buffer[STR_MAX];

				/* Control flow.  These commands have to explicitly check
				 * the fnoexec state because they manipulate it.
				 */
			#define CASE(l,r) case (((l)<<8)+(r))
			CASE('$','('): /* if */
				if (SP >= 16)
					die("exec stack overflow", lp);
				stack[SP].Al = Al;
				stack[SP++].Ac = Ac;
				if (fnoexec) {
					fnoexec += 3;
					goto noexec;
				}
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				fnoexec = arg[0] == 0;
				break;

			CASE('$',':'): /* elif */
				/* fnoexec is 1 if nothing has executed in this block yet,
				 * and if the block itself is executing.
				 */
				if (SP <= 0)
					goto underflow;
				if (fnoexec != 1) {
					/* If the previous block executed record this. */
					if (fnoexec == 0)
						fnoexec = 2;
					goto noexec;
				}
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				fnoexec = arg[0] == 0;
				break;

			CASE('$',')'): /* end+loop if */
				/* The stack must always be popped. */
				if (SP <= 0)
					goto underflow;
				--SP;
				/* If fnoexec>2 then this is a nested disabled block or, in
				 * the case of 3, a break.  In neither case is the expression
				 * evaluated.
				 */
				if (fnoexec > 2) {
					fnoexec -= 3;
					goto noexec;
				}
				fnoexec = 0;
				/* So make the loop test now - this may cause a branch back
				 * to the $( and that will push the stack again.
				 */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				if (arg[0] != 0) {
					Al = stack[SP].Al;
					Ac = stack[SP].Ac;
					/* Do not ever forget this, because we may not exit the
					 * immediately enclosing loop and line is set up outside
					 * it!
					 */
					line = prog[Al];
				}
				break;

			CASE('$','?'): /* break */
				if (SP <= 0)
					goto underflow;
				if (fnoexec)
					goto noexec;
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				if (arg[0] != 0) {
					/* break: skip to the $) and do not do the test on
					 * that either.
					 */
					fnoexec = 3;
				}
				break;

			underflow:
				die("exec stack underflow", lp);
			noexec:
				/* Control flow no-exec case. */
				Ac = eol(line, Ac);
				break;

				/* Program termination */
			CASE('!','?'): /* exit if non-zero */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				if (arg[0] != 0)
					quit(pp, arg[0]);
				break;

			CASE('!','!'): /* exit unconditionally */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				quit(pp, arg[0]);
				break;

				/* Input/output */
			CASE('>','>'): /* write to (no argument reverts to stdout) */
				Ac = string(buffer, line, Ac+2);
				if (buffer[0] != 0) {
					mtd_file *pfile = find_file(pp, buffer);
					if (pfile == 0)
						pfile = open_file(pp, buffer, 1);
					else if (!pfile->fwrite)
						die("attempt to open a read file for write", buffer);
					pp->pto = pfile;
				} else
					pp->pto = 0;
				break;

			CASE('<','<'): /* read from */
				Ac = string(buffer, line, Ac+2);
				if (buffer[0] != 0) {
					mtd_file *pfile = find_file(pp, buffer);
					if (pfile == 0)
						pfile = open_file(pp, buffer, 0);
					pp->pfrom = pfile;
				} else
					pp->pfrom = 0;
				break;

			CASE('<','>'): /* close */
			CASE('>','<'): /* synonym of close */
				Ac = string(buffer, line, Ac+2);
				if (buffer[0] != 0) {
					mtd_file *pfile = find_file(pp, buffer);
					if (pfile == 0)
						die("no such file to close", buffer);

					if (pfile == pp->pfrom)
						pp->pfrom = 0;
					if (pfile == pp->pto)
						pp->pto = 0;
					close_mtd(pfile);
				} else
					die("<> (close) requires an argument", lp);
				break;

			CASE('>','='): /* set to pointer */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				if (pp->pto == 0)
					die("no output file", lp);
				pp->pto->useroffset = arg[0];
				break;

			CASE('<','='): /* set from pointer */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				if (pp->pfrom == 0)
					die("no input file", lp);
				pp->pfrom->useroffset = arg[0];
				break;

				/* writing to output */
			CASE('w','b'): /* bigendian number bytes */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 2);
				switch (arg[1]) {
				case 4: outb(pp, arg[0] >> 24);
				case 3: outb(pp, arg[0] >> 16);
				case 2: outb(pp, arg[0] >>  8);
				case 1: outb(pp, arg[0]);
						break;
				default:die("byte count out of range", lp);
				}
				break;

			CASE('w','l'): /* little endian number bytes */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 2);
				switch (arg[1]) {
				case 4: outb(pp, arg[0]); arg[0] >>= 8;
				case 3: outb(pp, arg[0]); arg[0] >>= 8;
				case 2: outb(pp, arg[0]); arg[0] >>= 8;
				case 1: outb(pp, arg[0]);
						break;
				default:die("byte count out of range", lp);
				}
				break;

			CASE('w','s'): /* write string */
				Ac = string(buffer, line, Ac+2);
				outputbytes(pp, buffer, strlen(buffer));
				break;

			CASE('f','b'): /* fill bytes */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 2);

				/* The likely error is forgetting an argument, that may
				 * put the byte count into the fill value.
				 */
				if (arg[1] > 255)
					die("fill value out of range", lp);

				/* Now call fillbytes, arguments same order as memset, but
				 * the reverse from the command (fb number value).
				 */
				fillbytes(pp, arg[1], arg[0]);
				break;

			CASE('c','p'): /* copy bytes */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				copybytes(pp, arg[0]);
				break;

				/* writing to stdout */
			CASE('p','r'): /* printf("%lu\n") */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
				printf("%lu\n", arg[0]);
			stdout_check:
				if (ferror(stdout))
					pdie("write error", "stdout");
				break;

			CASE('p','f'): /* printf(string) */
				Ac = string(buffer, line, Ac+2);
				/* This flushes the whole stack, so "printf;" can be useful
				 * under some circumstances!
				 */
					{
					unsigned long *stack = pp->stack + pp->cstack - 1;
					pp->cstack = STACK_BASE;
					printf(buffer, stack[0], stack[-1], stack[-2], stack[-3],
									stack[-4], stack[-5], stack[-6], stack[-7]);
					}
				goto stdout_check;

			CASE('p','n'): /* printf("\n") */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 0);
				printf("\n");
				goto stdout_check;


			CASE('.','='): /* eval */
				Ac = need(pp, arg, line, Ac+2, AcEnd, 0);
				break;

			default:
				/* Assignment is handled here. */
				if (isupper(ch) && line[Ac+1] == '=') {
					/* Assignment. */
					Ac = need(pp, arg, line, Ac+2, AcEnd, 1);
					pp->variables[ch] = arg[0];
				} else
					die("unknown command", lp);
				break;
			}
		}

		/* end of line (line[Ac] is '\0'), move to the next line. */
		++Al;
	}

	/* If SP>0 then there was some bad nesting going on - i.e. the brackets have
	 * not been closed.  fnoexec must be zero if SP is 0.
	 */
	if (SP > 0)
		die("unclosed $( $)", "eof");
	assert(fnoexec == 0);
}

/* The command only understands two options, -v for verify and -h for
 * help.
 */
int main(int argc, const char **argv) {
	int i = 0;
	parse_buf p;

	/* the option must be the first argument. */
	if (argc < 2 || strcmp(argv[1], "-h") == 0) {
		fprintf(stderr, "%s: usage: %s ([-v] {comands} |-h [command])\n", argv[0], argv[0]);
#if HELP
		if (argc < 3) {
			fprintf(stderr, " options:\n"
							"  -v: do not write, just verify a previous write\n"
							"  -h: output this help\n"
							"  -h commands: output command help\n"
							"  -h <command>: output help for command <command>\n");
		} else {
			for (i=2; i<argc; ++i) {
				const unsigned char *p = argv[i];
				switch ((argv[i][0] << 8) + argv[i][1]) {
				CASE('a','d'):
					p = "Enter the keyword in [] contained after the command list\n"
						"for more information about those commands";
					break;
				CASE('c','o'):
					if (argv[i][2] != 'n')
						p = " commands are terminated by ';', newline or the end of input:\n"
						"  <command> {arguments} ;\n\n"
						" command: a two character command:       [additional info]\n"
						"  conditional execution: $( $: $? $)     [conditional]\n"
						"  program termination:   !? !!           [exit]\n"
						"  input/output control:  >> << <> >= <=  [io]\n"
						"  writing to the output: wb wl ws fb cp  [write]\n"
						"  writing to stdout:     pr pf pn        [messages]\n"
						"  assignment:            <C>= .=         [assign]\n"
						"  comment:               #\n\n"
						"  -h <command>: output the syntax of command <command>\n\n"
						" arguments: a string or a postfix numeric expression\n"
						"  -h string:   output the syntax of a string argument\n"
						"  -h number:   output the syntax of a numeric argument\n"
						"  -h operator: output a summary of the operators\n\n"
						" A command which starts with '#' is ignored (a comment).";
					else
						p = "conditional evalation\n"
						"  Conditional evalation has the general form:\n\n"
						"    $( cond; $? cond; $: cond; $) cond;  (command form)\n"
						"    cond(,  cond?,  [cond:,  [cond),     (operator from)\n\n"
						"  Conditional commands bracket other commands, conditional\n"
						"  operators bracket other numbers and operators in the expression\n"
						"  The conditions (cond) are intrepreted as boolean values, with 0\n"
						"  false and any other value true.  Notice that the numbers come off\n"
						"  the number stack so it is not necessary for them to be given with\n"
						"  the specific command or before the specifc operator but it can\n"
						"  be very confusing if this is not done.\n\n"
						"  The commands/operators behave exactly as the following ANSI C\n"
						"  syntax:\n\n"
						"    $( n       do if (n) {\n"
						"    $? n       if (n) break;\n"
						"    $: n       } else if (n) {\n"
						"    $) n       } while (n);\n\n"
						"  Thus a simple if/then/else sequence might be:\n\n"
						"    $( condition; $: 1; $) 0\n"
						"    condition( [1: [0)\n\n"
						"  And a simple do while loop:\n\n"
						"    $( 1; $) condition\n"
						"    1( [condition)\n\n"
						"  Notice that the operator syntax requires [cond: and [cond)";
					break;
				CASE('$','('): /* if */
					p = "conditional: $( condition;  do if (condition) {\n";
					break;
				CASE('$',':'): /* elif */
					p = "conditional: $( condition;  } else if (condition) {\n";
					break;
				CASE('$',')'): /* end+loop if */
					p = "conditional: $: condition;  } while (condition);\n";
					break;
				CASE('$','?'): /* break */
					p = "conditional: $: condition;  if (condition) break;\n";
					break;
				CASE('e','x'):
					p = "program termination commands:\n"
						"  !? and !! cause immediate termination of the program.\n"
						"  The currently opened files are closed (and flushed) on\n"
						"  termination and the program exits with the given code.\n"
						"  !? is condition - it only causes an exit if the code is\n"
						"  non zero.";
					break;
				CASE('!','?'): /* exit if non-zero */
					p = "exit: !? code;  if (code != 0) exit(code);\n";
					break;
				CASE('!','!'): /* exit unconditionally */
					p = "exit: !! code;  exit(code);\n";
					break;
				CASE('i','o'):
					p = "input/output control:\n"
						"  >> << allow files to be opened for write or read (respectively)\n"
						"  A file may be opened for both write and read, but in that case\n"
						"  the first open must be the write one.  Such a file has only one\n"
						"  position pointer.  Supplying an empty file name to the commands\n"
						"  causes the standard IO stream (stdout or stdin) to be used, but\n"
						"  the file remains open.  A file may be reselected by repeating\n"
						"  the open command.  The position pointer will not have changed.\n\n"
						"  <> closes a file.  If it is the current input or output then the\n"
						"  input or output, as appropriate, is redirected to the standard\n"
						"  streams.\n\n"
						"  <= >= set the input (from) or output (to) pointer.  The pointer is\n"
						"  changed on the current input or output device and is a property of\n"
						"  that device, not a global setting.";
					break;
				CASE('>','>'): /* write to (no argument reverts to stdout) */
					p = "io: >> file;  select file as the current output device\n"
						"    >> ;      select stdout as the current output device\n";
					break;
				CASE('<','<'): /* read from */
					p = "io: << file;  select file as the current input device\n"
						"    << ;      select stdin as the current input device\n";
					break;
				CASE('<','>'): /* close */
					p = "io: <> file;  close file\n";
					break;
				CASE('>','<'): /* synonym of close */
					p = "io: <> file;  close file\n";
					break;
				CASE('>','='): /* set to pointer */
					p = "io: >= offset;  set the 'to' pointer: output device file pointer\n";
					break;
				CASE('<','='): /* set from pointer */
					p = "io: <= offset;  set the 'from' pointer: input device file pointer\n";
					break;
				CASE('w','r'):
					p = "writing to the output:\n"
						"  wb, wl, ws, fb and cp write bytes to the current output device at\n"
						"  the current output position (which may be set using >=.)\n\n"
						"  See the individual command descriptions for more information.";
					break;
				CASE('w','b'): /* bigendian number bytes */
					p = "write: wb number bytes; write number in bytes big endian bytes\n";
					break;
				CASE('w','l'): /* little endian number bytes */
					p = "write: wl number bytes; write number in bytes little endian bytes\n";
					break;
				CASE('w','s'): /* write string */
					p = "write: wl string;       write string\n";
					break;
				CASE('f','b'): /* fill bytes */
					p = "write: fb number byte;  write number bytes of value 'byte'\n";
					break;
				CASE('c','p'): /* copy bytes */
					p = "write: cp number;       copy number bytes from input to output\n";
					break;
				CASE('m','e'):
					p = "writing to stdout:\n"
						"  pr pf and pn allow output to stdout ignoring the current output\n"
						"  device.  pf uses a C printf(3) format string and no checking is\n"
						"  made on the validity or number of parameters.  The program will\n"
						"  crash if a pointer format (like %s) is used.  This is by design.\n"
						"  Use of \\ escapes within the string will probably not produce the\n"
						"  expected result.  The only way to output a newline is via the\n"
						"  separate pn command.";
					break;
				CASE('p','r'): /* printf("%lu\n") */
					p = "message: pr number;   printf(\"%lu\\n\", number);\n";
					break;
				CASE('p','f'): /* printf(string) */
					p = "message: pf string;   printf(string, 1, 2, 3, 4, 5, 6, 7, 8)\n"
						"                      uses string as a format string for the\n"
						"                      top (up to) 8 arguments on the stack\n"
						"                      Empties the stack!\n";
					break;
				CASE('p','n'): /* printf("\n") */
					p = "message: pn;          prints a newline (printf(\"\\n\");)\n";
					break;
				CASE('.','='):
					p = ".= [number]\n"
						"  The number is evaluated and pushed to the stack";
				CASE('a','s'):
					p = "<C>= [number] (<C> is an upper case character in the range A-Z)\n"
						"  The number is assigned to the variable <C>.";
					break;
				CASE('s','t'):
					p = "string format\n"
						"  A string is either quoted or unquoted.  An unquoted string is\n"
						"  the rest of the line, terminated by ';' or newline (or the end\n"
						"  of the command).  A quoted string is enclosed in either single\n"
						"  or double quotes (which are removed) and must be the only thing\n"
						"  on the line apart from white space.";
					break;
				CASE('n','u'):
					p = "number format\n"
						"  A number is a post-fix expression consisting of numeric values,\n"
						"  variable values and operators.\n"
						"  A numeric value is anything starting with a digit.  0x prefixes a\n"
						"  hexadecimal value, 0 prefixes an octal value otherwise the value\n"
						"  is interpreted as decimal.\n"
						"  Variables are single upper case characters in the range A-Z,\n"
						"  thus 26 variables are available.\n"
						"  Numeric values and variables are pushed onto a stack which can\n"
						"  store up to 248 numbers.\n"
						"  Operators are single characters not identified as numeric values\n"
						"  or variables.  They operate on the stack combining elements.\n"
						"  Use -h operators for more information.";
					break;
				CASE('o','p'):
					p = "operators\n"
						"  The following operators are defined.  Operators take one or\n"
						"  two elements from the stack and push back up to two elements\n"
						"  The conditional operators (:?) are special, see the description\n"
						"  under -h conditionals for more information.  (Conditional\n"
						"  operators and the conditional commands behave identically.)\n\n"
						"  In the following descriptions 'left' is the last-but-one\n"
						"  number on the stack and 'right' is the last number pushed.\n"
						"  'new' is a new number pushed on to the top of the stack.\n\n"
						" arithmetic operators:\n"
						"     *: new := left * right        (multiplication)\n"
						"     +: new := left + right\n"
						"     -: new := left - right\n"
						"     /: new := left / right        (division)\n"
						"     %: new := left % right        (i.e. C style modulus)\n"
						"     <: new := left < right\n"
						"     >: new := left > right\n"
						"     =: new := left == right       (i.e. equality)\n"
						"     |: new := left | right        (bitwise or)\n"
						"     &: new := left | right        (bitwise and)\n"
						"     ^: new := left | right        (bitwise xor)\n"
						"     ~: new := ~right              (ones complement)\n"
						"     !: new := !right              (logical complement - right == 0)\n"
						"     {: new := left << right       (bitwise shift left by right bits)\n"
						"     }: new := left << right       (bitwise shift right by right bits)\n"
						"     r: new := left ROR right      (bitwise rotate right by right bits)\n"
						"     e: new := left SXT right      (right least significant bits of\n"
						"                                    left are sign extended to 32 bits)\n"
						"     m: new := left MASK right     (right least significant bits of left\n"
						"                                    are masked, upper bits become 0)\n"
						" operators to read bytes:\n"
						"     @: new := input-byte          (one byte is read from current input)\n"
						"     b: new := input-big-endian    (4 big endian bytes are read)\n"
						"     l: new := input-little-endian (4 little endian bytes)\n"
						" enquiry operators:\n"
						"     $: the size of the current input device\n"
						"     f: the current 'from' offset\n"
						"     #: the size of the current output device\n"
						"     t: the current 'to' offset\n"
						"     d: the full device number of the current input device\n"
						"        this returns the number of the underlying device for a file\n"
						" operators to manipulate the stack:\n"
						"     .: new := right,right         (i.e. right is duplicated)\n"
						"     p: pop - right is removed from the stack\n"
						"     s: swap - right and left are swapped on the stack\n"
						" conditional operators:\n"
						"     ( exec := right         (execution is stopped if right is 0)\n"
						"     : exec := else if right (execution is resumed or stopped)\n"
						"     ? if right break        (break out of if/loop if right)\n"
						"     ) loop if right         (end of current if/loop)";
					break;
				default:
					fprintf(stderr, "command '%s' not recognised\n", p);
					p = 0;
				}

				if (p != 0)
					fprintf(stderr, "%s\n", p);
			}
		}
#endif
		exit(1);
	} else if (strcmp(argv[1], "-v") == 0)
		i = 1;
		
	init_parse(&p, i);
	++i;
	parse(&p, argc-i, argv+i);
	quit(&p, 0);
}