/
od.c
1613 lines (1543 loc) · 33 KB
/
od.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2013 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <glenn.s.fowler@gmail.com> *
* David Korn <dgkorn@gmail.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* David Korn
* AT&T Research
*
* od
*/
static const char usage[] =
"[-?\n@(#)$Id: od (AT&T Research) 2013-09-13 $\n]"
USAGE_LICENSE
"[+NAME?od - dump files in octal or other formats]"
"[+DESCRIPTION?\bod\b dumps the contents of the input files in various "
"formats on the standard output. The standard input is read if \b-\b or "
"no files are specified. Each output line contains the file offset of "
"the data in the leftmost column, followed by one or more columns in the "
"specified format. If more than one format is specified then the "
"subsequent lines are listed with the offset column blank. Second and "
"subsequent occurrences of a repeated output line are replaced by a "
"single line with `*' in the first data column.]"
"[+?\b--format=c\b, \b--format=C\b and \b--format=O\b interpret bytes as "
"characters in the LC_CTYPE locale category. The three types differ only "
"in the C style escape sequences recognized; \b--format=C\b explicitly "
"allows future extensions. The backslash character is written as `\\' "
"and NUL is written as `\\0'. Other non-printable characters are written "
"as one three-digit octal number for each byte in the character. "
"Printable multi-byte characters are written in the area corresponding "
"to the first byte of the character; the two-character sequence `**' is "
"written in the area corresponding to each remaining byte in the "
"character; the remaining bytes for \b-p\b are written as ` '. If "
"\b--skip\b or \b--count\b position the file into the middle of a "
"multibyte character then the entire multibyte character is silently "
"treated as non-character bytes.]"
"[+?If the output format is specified by one of the obsolete forms (not "
"\b--format\b) then the last file argument is interpreted as an offset "
"expression if it matches the extended regular expression "
"\b+?[0-9]]+\\.?[bkm]]?(ll|LL)?\b . In this case the first \aoffset\a "
"bytes of the file are skipped. The optional \bb\b means bytes, \bk\b "
"means Kb, and \bm\b means Mb. \bll\b and \bLL\b are ignored for "
"compatibility with some systems.]"
"[A:address-radix?The file offset radix.]:[radix:=o]"
"{"
"[+d?decimal]"
"[+o?octal]"
"[+x?hexadecimal]"
"[+n?none - do not print offset]"
"}"
"[B:swap?Swap input bytes according to the bit mask \aop\a, which is the "
"inclusive or of:]#[op]"
"{"
"[+01?swap 8-bit bytes]"
"[+02?swap 16-bit words]"
"[+04?swap 32-bit longs]"
"[+0?swap for big endian testing]"
"}"
"[j:skip-bytes?Skip \bbytes\b bytes into the data before "
"formatting.]#[bytes]"
"[N:count|read-bytes?Output only \bbytes\b bytes of data.]#[bytes]"
"[m:map?\b--printable\b and \b--format=m\b bytes are converted from "
"\acodeset\a to the native codeset. The codesets are:]:[codeset]"
"{\fcodesets\f} [p:printable?Output the printable bytes (after \b--map\b "
"if specified), in the last data column. Non-printable byte values are "
"printed as `.'.]"
"[z:strings?Output NUL terminated strings of at least \alength\a "
"bytes.]#? [length:=3]"
"[t:format|type?The data item output \aformat\a and \asize\a. A decimal byte "
"count or size code may follow all but the \ba\b, \bc\b, \bC\b, \bm\b "
"and \bO\b formats.]:[format[size]]:=o2]"
"{\ftypes\f}"
"[T:test?Enable internal implementation specific tests.]:[test]"
"{"
"[+b\an\a?Allocate a fixed input buffer of size \an\a.]"
"[+m\an\a?Set the mapped input buffer size to \an\a.]"
"[+n?Turn off the \bSF_SHARE\b input buffer flag.]"
"}"
"[v:all|output-duplicates?Output all data.]"
"[w:per-line|width?The number of items to format per output line. "
"\aper-line\a must be a multiple of the least common multiple of the "
"sizes of the format types.]#[per-line]"
"[a?Equivalent to \b-ta\b.]"
"[b?Equivalent to \b-toC\b.]"
"[c?Equivalent to \b-tO\b.]"
"[C?Equivalent to \b-tO\b.]"
"[d?Equivalent to \b-tuS\b.]"
"[D?Equivalent to \b-tuL\b.]"
"[f?Equivalent to \b-tfF\b.]"
"[F?Equivalent to \b-tfD\b.]"
"[h?Equivalent to \b-txS\b.]"
"[i?Equivalent to \b-tdS\b.]"
"[l?Equivalent to \b-tdL\b.]"
"[o?Equivalent to \b-toS\b.]"
"[O?Equivalent to \b-toL\b.]"
"[s?Equivalent to \b-tdS\b.]"
"[S?Equivalent to \b-tdL\b.]"
"[u?Equivalent to \b-tuS\b.]"
"[U?Equivalent to \b-tuL\b.]"
"[x?Equivalent to \b-txS\b.]"
"[X?Equivalent to \b-txL\b.]"
"\n"
"\n[ file ... ] [ [+]offset[.|b|k|m|ll|LL] ]\n"
"\n"
"[+SEE ALSO?\bsed\b(1), \bstrings\b(1), \bswap\b(3), \bascii\b(5)]"
;
#include <cmd.h>
#include <sig.h>
#include <swap.h>
#include <ccode.h>
#include <ctype.h>
#include <iconv.h>
#include <vmalloc.h>
#include <ast_float.h>
#if _hdr_wctype
#include <wchar.h>
#include <wctype.h>
#else
#define iswprint(w) isprint(w)
#endif
#define NEW (1<<0)
#define OLD (1<<1)
#define BASE_WIDTH 7
#define LINE_LENGTH 78
#define WIDTHINDEX(n) (((n)>1)+((n)>2)+((n)>4)+((n)>8))
#ifdef _ast_int8_t
#define QUAL "ll"
#else
#define QUAL "l"
#endif
#if _typ_long_double
#define double_max long double
#else
#define double_max double
#endif
struct State_s; typedef struct State_s State_t;
struct Format_s; typedef struct Format_s Format_t;
typedef void (*Format_f)(State_t*, Format_t*, Sfio_t*, unsigned char*);
typedef union Mem_u
{
char m_char[sizeof(intmax_t) + sizeof(double_max)];
float m_float;
double m_double;
#if _typ_long_double
long double m_long_double;
#endif
} Mem_t;
typedef struct Size_s
{
char* desc;
int name;
int map;
int dflt;
const char* qual;
int size;
int prefix;
int digits;
int exponent;
} Size_t;
typedef struct Type_s
{
char* desc;
char name;
char mb;
const char* fill;
Format_f fun;
const Size_t* size;
char width[5];
} Type_t;
struct Format_s
{
struct Format_s*next;
const Type_t* type;
Format_f fun;
char form[16];
struct
{
int external;
int internal;
} size;
int fp;
int width;
int us;
int per;
};
struct State_s
{
int addr;
char base[8];
int block;
struct
{
char* base;
size_t size;
int noshare;
} buffer;
size_t count;
Shbltin_t* context;
struct
{
char buf[1024];
char* data;
int mark;
size_t size;
} dup;
unsigned char* eob;
char* file;
Format_t* form;
Format_t* last;
unsigned char* map;
int mb;
int mbmax;
int mbp;
intmax_t offset;
struct
{
char* data;
size_t size;
} peek;
int printable;
int size;
size_t skip;
char* span;
int spansize;
int strings;
int style;
int swap;
size_t total;
int verbose;
int width;
Vmalloc_t* vm;
Mbstate_t q;
};
static const Size_t csize[] =
{
"char", 'C', 0, 1, 0, 1,0,0,0,
0
};
static const Size_t isize[] =
{
"char", 'C', 0, 0, 0, 1,0,0,0,
"short", 'S', 0, 0, 0, sizeof(short),0,0,0,
"int", 'I', 0, 1, 0, sizeof(int),0,0,0,
"long", 'L', 0, 0, "l", sizeof(long),0,0,0,
"long long", 'D', 0, 0,
#ifdef _ast_int8_t
"ll", sizeof(int64_t),0,0,0,
#else
0, sizeof(long),0,0,0,
#endif
0
};
static const Size_t fsize[] =
{
"float", 'F', 'e', 0, 0, sizeof(float),3,FLT_DIG,FLT_MAX_10_EXP,
"double", 'D', 'e', 1, 0, sizeof(double),3,DBL_DIG,DBL_MAX_10_EXP,
"long double", 'L', 'e', 0,
#if _typ_long_double
"L", sizeof(long double),3,LDBL_DIG,LDBL_MAX_10_EXP,
#else
0, sizeof(double),3,DBL_DIG,DBL_MAX_10_EXP,
#endif
0
};
static const Size_t asize[] =
{
"float", 'F', 'a', 0, 0, sizeof(float),5,FLT_DIG,FLT_MAX_EXP,
"double", 'D', 'a', 1, 0, sizeof(double),5,DBL_DIG,DBL_MAX_EXP,
"long double", 'L', 'a', 0,
#if _typ_long_double
"L", sizeof(long double),5,LDBL_DIG,LDBL_MAX_EXP,
#else
0, sizeof(double),5,DBL_DIG,DBL_MAX_EXP,
#endif
0
};
static void
aform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
register int c = *u;
static const char anames[] =
"nulsohstxetxeotenqackbel bs ht nl vt ff cr so si"
"dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us"
" sp ! \" # $ % & ' ( ) * + , - . /"
" 0 1 2 3 4 5 6 7 8 9 : ; < = > ?"
" @ A B C D E F G H I J K L M N O"
" P Q R S T U V W X Y Z [ \\ ] ^ _"
" ` a b c d e f g h i j k l m n o"
" p q r s t u v w x y z { | } ~del";
c = 3 * (c & 0177);
sfputc(op, anames[c]);
sfputc(op, anames[c+1]);
sfputc(op, anames[c+2]);
}
static void
bform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, "%08..2u", *u);
}
static void
cform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
unsigned char* v;
char* s;
wchar_t w;
int i;
int j;
char buf[2];
if (state->mb)
{
state->mb--;
sfputc(op, ' ');
sfputc(op, '*');
sfputc(op, '*');
return;
}
switch (*u)
{
case 0:
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, '0');
return;
case '\\':
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, '\\');
return;
}
buf[0] = *(v = u);
if ((w = mbtchar(&w, u, state->eob - u, &state->q)) > 0 && (i = u - v) > 1 && !iswprint(w))
{
state->mb = i - 1;
for (j = 3 - mbwidth(w); j > 0; j--)
sfputc(op, ' ');
while (i--)
sfputc(op, *v++);
return;
}
buf[1] = 0;
s = fmtesc(buf);
/*
* posix shalls are sometimes an impediment to progress
*/
if (*s != '\\')
{
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, *s);
return;
}
switch (*(s + 1))
{
case 'a':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'v':
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, *(s + 1));
return;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
sfputc(op, *++s);
sfputc(op, *++s);
sfputc(op, *++s);
return;
}
sfprintf(op, "%03o", *v);
}
static void
Cform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
unsigned char* v;
char* s;
wchar_t w;
int i;
int j;
char buf[2];
if (state->mb)
{
state->mb--;
sfputc(op, ' ');
sfputc(op, '*');
sfputc(op, '*');
return;
}
switch (*u)
{
case 0:
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, '0');
return;
case '\\':
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, '\\');
return;
}
buf[0] = *(v = u);
if ((w = mbtchar(&w, u, state->eob - u, &state->q)) > 0 && (i = u - v) > 1 && !iswprint(w))
{
state->mb = i - 1;
for (j = 3 - mbwidth(w); j > 0; j--)
sfputc(op, ' ');
while (i--)
sfputc(op, *v++);
return;
}
buf[1] = 0;
s = fmtesc(buf);
if (*s != '\\')
{
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, *s);
return;
}
if (isdigit(*(s + 1)))
{
sfputc(op, *++s);
sfputc(op, *++s);
sfputc(op, *++s);
return;
}
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, *(s + 1));
}
static void
mform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
register char* s;
char buf[2];
switch (buf[0] = ccmapchr(state->map, *u))
{
case 0:
sfputc(op, '0');
sfputc(op, '0');
return;
case '\\':
sfputc(op, ' ');
sfputc(op, '\\');
return;
}
buf[1] = 0;
s = fmtesc(buf);
if (*s != '\\')
{
sfputc(op, ' ');
sfputc(op, *s);
return;
}
if (isdigit(*(s + 1)))
sfprintf(op, "%02lx", strtol(s + 1, NiL, 8));
else
{
sfputc(op, '\\');
sfputc(op, *(s + 1));
}
}
static void
Oform(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
unsigned char* v;
char* s;
wchar_t w;
int i;
int j;
char buf[2];
if (state->mb)
{
state->mb--;
sfputc(op, ' ');
sfputc(op, '*');
sfputc(op, '*');
return;
}
switch (*u)
{
case 0:
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, '0');
return;
case '\\':
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, '\\');
return;
}
buf[0] = *(v = u);
if ((w = mbtchar(&w, u, state->eob - u, &state->q)) > 0 && (i = u - v) > 1 && !iswprint(w))
{
state->mb = i - 1;
for (j = 3 - mbwidth(w); j > 0; j--)
sfputc(op, ' ');
while (i--)
sfputc(op, *v++);
return;
}
buf[1] = 0;
s = fmtesc(buf);
/*
* posix shalls are sometimes an impediment to progress
*/
if (*s != '\\')
{
sfputc(op, ' ');
sfputc(op, ' ');
sfputc(op, *s);
return;
}
switch (*(s + 1))
{
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
sfputc(op, ' ');
sfputc(op, '\\');
sfputc(op, *(s + 1));
return;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
sfputc(op, *++s);
sfputc(op, *++s);
sfputc(op, *++s);
return;
}
sfprintf(op, "%03o", *v);
}
static const Type_t type[] =
{
{
"ISO/IEC 646:1991 named characters",
'a', 0, 0, aform, csize, 3, 0, 0, 0, 0
},
{
"hexadecimal floating point",
'A', 0, 0, 0, asize, 0, 0, 0, 0, 0
},
{
"binary character",
'b', 0, 0, bform, csize, 8, 0, 0, 0, 0
},
{
"locale character or backslash escape (\\a \\b \\f \\n \\r \\t \\v)",
'c', 0, 0, cform, csize, 3, 0, 0, 0, 0
},
{
"locale character or backslash escape (current and future escapes)",
'C', 0, 0, Cform, csize, 3, 0, 0, 0, 0
},
{
"signed decimal",
'd', 0, 0, 0, isize, 4, 6, 11, 21, 31
},
{
"floating point",
'f', 0, 0, 0, fsize, 0, 0, 0, 0, 0
},
{
"\b--map\b mapped character or hexadecimal value if not printable",
'm', 0, 0, mform, csize, 2, 0, 0, 0, 0
},
{
"octal",
'o', 0, "0", 0, isize, 3, 6, 11, 22, 33
},
{
"locale character or backslash escape (\\b \\f \\n \\r \\t)",
'O', 0, 0, Oform, csize, 3, 0, 0, 0, 0
},
{
"unsigned decimal",
'u', 0, 0, 0, isize, 3, 5, 10, 20, 30
},
{
"hexadecimal",
'x', 0, "0", 0, isize, 2, 4, 8, 16, 32
},
{
"printable bytes",
'z', 0, 0, 0, 0, 1, 0, 0, 0, 0
},
};
static void
form_int8(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, *u);
}
static void
form_int16(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (int16_t)swapget(state->swap, u, fp->size.external));
}
static void
form_int32(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (int32_t)swapget(state->swap, u, fp->size.external));
}
static void
form_intmax(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (intmax_t)swapget(state->swap, u, fp->size.external));
}
static void
form_uint8(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, *u);
}
static void
form_uint16(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (uint16_t)swapget(state->swap, u, fp->size.external));
}
static void
form_uint32(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (uint32_t)swapget(state->swap, u, fp->size.external));
}
static void
form_uintmax(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
sfprintf(op, fp->form, (uintmax_t)swapget(state->swap, u, fp->size.external));
}
static void
form_float(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
Mem_t mem;
swapmem(state->swap ^ int_swap, u, mem.m_char, fp->size.internal);
sfprintf(op, fp->form, mem.m_float);
}
static void
form_double(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
Mem_t mem;
swapmem(state->swap ^ int_swap, u, mem.m_char, fp->size.internal);
sfprintf(op, fp->form, mem.m_double);
}
#if _typ_long_double
static void
form_long_double(State_t* state, Format_t* fp, Sfio_t* op, unsigned char* u)
{
Mem_t mem;
swapmem(state->swap ^ int_swap, u, mem.m_char, fp->size.internal);
sfprintf(op, fp->form, mem.m_long_double);
}
#endif
/*
* add format type t to the format list
*/
static void
format(State_t* state, register char* t)
{
register int c;
register const Type_t* tp;
register const Size_t* zp;
register Format_t* fp;
const Size_t* xp;
uintmax_t m;
int prec;
int base;
int n;
int i;
char* e;
char* s;
char dig[256];
while (c = *t++)
{
if (isspace(c) || c == ',')
continue;
tp = type;
while (c != tp->name)
if (++tp >= &type[elementsof(type)])
{
error(2, "%c: invalid type name", c);
return;
}
if (!(zp = tp->size) && !tp->fun)
{
switch (tp->width[0])
{
case 1:
state->printable = 1;
break;
}
continue;
}
xp = 0;
if (isdigit(*t))
{
c = 0;
n = (int)strton(t, &e, NiL, 1);
t = e;
}
else
{
c = isupper(*t) ? *t++ : 0;
n = 0;
}
for (;;)
{
if (!zp->name)
{
if (c)
{
error(2, "%c: invalid size for type %c", c, tp->name);
return;
}
if (!(zp = xp) || zp->exponent)
{
error(2, "%d: invalid size for type %c", n, tp->name);
return;
}
break;
}
if (n)
{
if (n == zp->size)
break;
zp++;
if (n > (zp-1)->size && n < zp->size)
xp = zp;
}
else if (c == zp->name)
{
if (c != *t)
break;
t++;
zp++;
if (zp->name)
break;
}
else if (!c && !n && zp->dflt)
break;
else
zp++;
}
if (tp->mb && (i = mbmax()) > 1)
state->mbmax = i;
i = zp - tp->size;
if (!(fp = vmnewof(state->vm, 0, Format_t, 1, 0)))
{
error(ERROR_system(1), "out of space");
return;
}
if (state->last)
state->last = state->last->next = fp;
else
state->form = state->last = fp;
fp->type = tp;
fp->us = tp->name != 'd';
fp->size.internal = zp->size;
fp->size.external = n ? n : zp->size;
if (state->size < fp->size.external)
state->size = fp->size.external;
if (zp->exponent)
{
if (zp->map == 'a')
{
for (m = 0, i = zp->digits; i-- > 0; m = m * 10 + 9);
prec = sfsprintf(dig, sizeof(dig), "%I*x", sizeof(m), m);
}
else
prec = zp->digits;
base = sfsprintf(dig, sizeof(dig), "%u", zp->exponent);
fp->width = zp->prefix + prec + base + 2;
}
else
{
fp->width = tp->width[i = WIDTHINDEX(fp->size.internal)];
if (n > 1 && (n & (n - 1)))
{
c = (1 << i) - n;
n = (1 << i) - (1 << (i - 1));
fp->width -= ((fp->width - tp->width[i - 1] + n - 1) / n) * c;
}
prec = 0;
base = 0;
}
if (!(fp->fun = tp->fun))
{
e = fp->form;
*e++ = '%';
if (s = (char*)tp->fill)
while (*e = *s++)
e++;
e += sfsprintf(e, sizeof(fp->form) - (e - fp->form), "%d", fp->width);
if (prec)
e += sfsprintf(e, sizeof(fp->form) - (e - fp->form), ".%d", prec);
if (base)
e += sfsprintf(e, sizeof(fp->form) - (e - fp->form), "%s.%d", prec ? "" : ".", base);
if (s = (char*)zp->qual)
while (*e = *s++)
e++;
*e = zp->map ? zp->map : tp->name;
if (zp->exponent)
{
#if _typ_long_double
if (fp->size.internal == sizeof(long double))
fp->fun = form_long_double;
else
#endif
if (fp->size.internal == sizeof(double))
fp->fun = form_double;
else
fp->fun = form_float;
}
else if (fp->us)
switch (fp->size.internal)
{
case 1:
fp->fun = form_uint8;
break;
case 2:
fp->fun = form_uint16;
break;
case 4:
fp->fun = form_uint32;
break;
default:
fp->fun = form_uintmax;
break;
}
else
switch (fp->size.internal)
{
case 1:
fp->fun = form_int8;
break;
case 2:
fp->fun = form_int16;
break;
case 4:
fp->fun = form_int32;
break;
default:
fp->fun = form_intmax;
break;
}
}
}
}
static Sfio_t*
init(State_t* state, char*** p)
{
Sfio_t* ip;
intmax_t offset;
for (;;)
{
if (**p)
state->file = *((*p)++);
else if (state->file)
return 0;
if (!state->file || streq(state->file, "-"))
{
state->file = "/dev/stdin";
ip = sfstdin;
}
else if (!(ip = sfopen(NiL, state->file, "r")))
{
error(ERROR_system(0), "%s: cannot open", state->file);
error_info.errors = 1;
continue;
}
if (state->buffer.noshare)
sfset(ip, SF_SHARE, 0);
if (state->buffer.size)
sfsetbuf(ip, state->buffer.base, state->buffer.size);
if (state->skip)
{
if ((offset = sfseek(ip, (off_t)0, SEEK_END)) > 0)
{
if (offset <= state->skip)
{
state->skip -= offset;
state->offset += offset;
goto next;
}
if (sfseek(ip, state->skip, SEEK_SET) != state->skip)
{
error(ERROR_system(2), "%s: seek error", state->file);
goto next;
}
state->offset += state->skip;
state->skip = 0;
}
else
{
for (;;)
{
if (!(state->peek.data = sfreserve(ip, SF_UNBOUND, 0)))
{
if (sfvalue(ip))
error(ERROR_system(2), "%s: read error", state->file);
goto next;
}
state->peek.size = sfvalue(ip);
if (state->peek.size < state->skip)
state->skip -= state->peek.size;
else
{
state->peek.data += state->skip;
state->peek.size -= state->skip;
state->skip = 0;
break;
}