1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin
24da2e3ebdSchin /*
25da2e3ebdSchin * return the record format descriptor given a format string
26da2e3ebdSchin * e!=0 set to the first unrecognized char after the format
27da2e3ebdSchin * REC_N_TYPE() returned on error
28da2e3ebdSchin *
29da2e3ebdSchin * d[0xNN|delimiter] (delimited, newline default)
30da2e3ebdSchin * [f][+]size (fixed length)
31da2e3ebdSchin * v hN oN zN b|l i|n (variable length with size header)
32da2e3ebdSchin * h header size in bytes (ibm V 4)
33da2e3ebdSchin * o size offset in bytes (ibm V 0)
34da2e3ebdSchin * z size length in bytes (ibm V 2)
35da2e3ebdSchin * l|b little-endian or big-endian size (ibm V b (0))
36da2e3ebdSchin * i|n header included/not-included in size (ibm V i (1))
37da2e3ebdSchin */
38da2e3ebdSchin
39da2e3ebdSchin #include <recfmt.h>
40da2e3ebdSchin #include <ctype.h>
41da2e3ebdSchin
42da2e3ebdSchin Recfmt_t
recstr(register const char * s,char ** e)43da2e3ebdSchin recstr(register const char* s, char** e)
44da2e3ebdSchin {
45da2e3ebdSchin char* t;
46da2e3ebdSchin int n;
47da2e3ebdSchin long v;
48da2e3ebdSchin int a[6];
49da2e3ebdSchin
50da2e3ebdSchin while (*s == ' ' || *s == '\t' || *s == ',')
51da2e3ebdSchin s++;
52da2e3ebdSchin switch (*s)
53da2e3ebdSchin {
54da2e3ebdSchin case 'd':
55da2e3ebdSchin case 'D':
56da2e3ebdSchin if (!*s)
57da2e3ebdSchin n = '\n';
58da2e3ebdSchin else
59da2e3ebdSchin {
60da2e3ebdSchin if (*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))
61da2e3ebdSchin n = (int)strtol(s, &t, 0);
62da2e3ebdSchin else
63da2e3ebdSchin n = chresc(s, &t);
64da2e3ebdSchin s = (const char*)t;
65da2e3ebdSchin }
66da2e3ebdSchin if (e)
67da2e3ebdSchin *e = (char*)s;
68da2e3ebdSchin return REC_D_TYPE(n);
69da2e3ebdSchin case 'f':
70da2e3ebdSchin case 'F':
71da2e3ebdSchin while (*++s == ' ' || *s == '\t' || *s == ',');
72da2e3ebdSchin /*FALLTHROUGH*/
73da2e3ebdSchin case '+':
74da2e3ebdSchin case '0': case '1': case '2': case '3': case '4':
75da2e3ebdSchin case '5': case '6': case '7': case '8': case '9':
76da2e3ebdSchin n = strton(s, &t, NiL, 0);
77da2e3ebdSchin if (n > 0 && t > (char*)s)
78da2e3ebdSchin {
79da2e3ebdSchin if (e)
80da2e3ebdSchin *e = t;
81da2e3ebdSchin return REC_F_TYPE(n);
82da2e3ebdSchin }
83da2e3ebdSchin break;
84da2e3ebdSchin case 'm':
85da2e3ebdSchin case 'M':
86da2e3ebdSchin while (*++s == ' ' || *s == '\t' || *s == ',');
87da2e3ebdSchin for (t = (char*)s; *t && *t != ' ' && *t != '\t' && *t != ','; t++);
88da2e3ebdSchin if ((t - s) == 4)
89da2e3ebdSchin {
90da2e3ebdSchin if (strneq(s, "data", 4))
91da2e3ebdSchin {
92da2e3ebdSchin if (e)
93da2e3ebdSchin *e = t;
94da2e3ebdSchin return REC_M_TYPE(REC_M_data);
95da2e3ebdSchin }
96da2e3ebdSchin else if (strneq(s, "path", 4))
97da2e3ebdSchin {
98da2e3ebdSchin if (e)
99da2e3ebdSchin *e = t;
100da2e3ebdSchin return REC_M_TYPE(REC_M_path);
101da2e3ebdSchin }
102da2e3ebdSchin }
103da2e3ebdSchin
104da2e3ebdSchin /*
105da2e3ebdSchin * TBD: look up name in method libraries
106da2e3ebdSchin * and assign an integer index
107da2e3ebdSchin */
108da2e3ebdSchin
109da2e3ebdSchin break;
110da2e3ebdSchin case 'u':
111da2e3ebdSchin case 'U':
112da2e3ebdSchin while (*++s == ' ' || *s == '\t' || *s == ',');
113da2e3ebdSchin n = strtol(s, &t, 0);
114da2e3ebdSchin if (n < 0 || n > 15 || *t++ != '.')
115da2e3ebdSchin break;
116da2e3ebdSchin v = strtol(t, &t, 0);
117da2e3ebdSchin if (*t)
118da2e3ebdSchin break;
119da2e3ebdSchin if (e)
120da2e3ebdSchin *e = t;
121da2e3ebdSchin return REC_U_TYPE(n, v);
122da2e3ebdSchin case 'v':
123da2e3ebdSchin case 'V':
124da2e3ebdSchin a[0] = 0;
125da2e3ebdSchin a[1] = 4;
126da2e3ebdSchin a[2] = 0;
127da2e3ebdSchin a[3] = 2;
128da2e3ebdSchin a[4] = 0;
129da2e3ebdSchin a[5] = 1;
130da2e3ebdSchin n = 0;
131da2e3ebdSchin for (;;)
132da2e3ebdSchin {
133da2e3ebdSchin switch (*++s)
134da2e3ebdSchin {
135da2e3ebdSchin case 0:
136da2e3ebdSchin break;
137da2e3ebdSchin case 'm':
138da2e3ebdSchin case 'M':
139da2e3ebdSchin n = 0;
140da2e3ebdSchin continue;
141da2e3ebdSchin case 'h':
142da2e3ebdSchin case 'H':
143da2e3ebdSchin n = 1;
144da2e3ebdSchin continue;
145da2e3ebdSchin case 'o':
146da2e3ebdSchin case 'O':
147da2e3ebdSchin n = 2;
148da2e3ebdSchin continue;
149da2e3ebdSchin case 'z':
150da2e3ebdSchin case 'Z':
151da2e3ebdSchin n = 3;
152da2e3ebdSchin continue;
153da2e3ebdSchin case 'b':
154da2e3ebdSchin case 'B':
155da2e3ebdSchin n = 4;
156da2e3ebdSchin a[n++] = 0;
157da2e3ebdSchin continue;
158da2e3ebdSchin case 'l':
159da2e3ebdSchin case 'L':
160da2e3ebdSchin n = 4;
161da2e3ebdSchin a[n++] = 1;
162da2e3ebdSchin continue;
163da2e3ebdSchin case 'n':
164da2e3ebdSchin case 'N':
165da2e3ebdSchin n = 0;
166da2e3ebdSchin a[5] = 0;
167da2e3ebdSchin continue;
168da2e3ebdSchin case 'i':
169da2e3ebdSchin case 'I':
170da2e3ebdSchin n = 0;
171da2e3ebdSchin a[5] = 1;
172da2e3ebdSchin continue;
173da2e3ebdSchin case ' ':
174da2e3ebdSchin case '\t':
175da2e3ebdSchin case ',':
176da2e3ebdSchin case '-':
177da2e3ebdSchin case '+':
178da2e3ebdSchin continue;
179da2e3ebdSchin case '0': case '1': case '2': case '3': case '4':
180da2e3ebdSchin case '5': case '6': case '7': case '8': case '9':
1817c2fbfb3SApril Chin v = 0;
182da2e3ebdSchin a[n++] = strtol(s, &t, 0);
183da2e3ebdSchin s = (const char*)t - 1;
184da2e3ebdSchin continue;
185da2e3ebdSchin }
186da2e3ebdSchin break;
187da2e3ebdSchin }
188da2e3ebdSchin if (e)
189da2e3ebdSchin *e = (char*)s;
1907c2fbfb3SApril Chin if (a[3] > (a[1] - a[2]))
1917c2fbfb3SApril Chin a[3] = a[1] - a[2];
192da2e3ebdSchin return REC_V_RECORD(REC_V_TYPE(a[1], a[2], a[3], a[4], a[5]), a[0]);
193da2e3ebdSchin case '%':
194da2e3ebdSchin if (e)
195da2e3ebdSchin *e = (char*)s + 1;
196da2e3ebdSchin return REC_M_TYPE(REC_M_path);
197da2e3ebdSchin case '-':
198da2e3ebdSchin case '?':
199da2e3ebdSchin if (e)
200da2e3ebdSchin *e = (char*)s + 1;
201da2e3ebdSchin return REC_M_TYPE(REC_M_data);
202da2e3ebdSchin }
203da2e3ebdSchin if (e)
204da2e3ebdSchin *e = (char*)s;
205da2e3ebdSchin return REC_N_TYPE();
206da2e3ebdSchin }
207