-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflex.h
executable file
·390 lines (331 loc) · 10.2 KB
/
flex.h
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
/*
Copyright (c) 1999-2011, Phillip Stanley-Marbell (author)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
enum
{
FLEX_ERRSTRLEN = 128
};
#ifdef INFERNO
# define FlexLock QLock
#else
# define FlexLock char
#endif
#ifdef INFERNO
# define FLEX_FLOATFMT "%f"
# define FLEX_DBLFMT "%E"
# define FLEX_INTFMT "%d"
# define FLEX_UINTFMT "%ud"
# define FLEX_UVLONGFMT "%llud"
# define FLEX_ULONGFMT "%lud"
# define FLEX_LONGFMT "%ld"
# define FLEX_UHLONGFMT "%lux"
# define FLEX_UHCLONGFMT "%luX"
# define FLEX_UVHCLONGFMT "%lluX"
# define FLEX_UH8LONGFMT "%08lux"
# define FLEX_UH2XFMT "%02X"
# define FLEX_STRINGFMT "%s"
# define FLEX_INTEGERFMT FLEX_INTFMT
# define FLEX_REALFMT FLEX_FLOATFMT
#else
# define FLEX_FLOATFMT "%f"
# define FLEX_DBLFMT "%E"
# define FLEX_INTFMT "%d"
# define FLEX_UINTFMT "%u"
# define FLEX_UVLONGFMT "%llu"
# define FLEX_ULONGFMT "%lu"
# define FLEX_LONGFMT "%ld"
# define FLEX_UHLONGFMT "%lx"
# define FLEX_UHCLONGFMT "%lX"
# define FLEX_UVHCLONGFMT "%llX"
# define FLEX_UH8LONGFMT "%08lx"
# define FLEX_UH2XFMT "%02X"
# define FLEX_STRINGFMT "%s"
# define FLEX_INTEGERFMT FLEX_INTFMT
# define FLEX_REALFMT FLEX_FLOATFMT
#endif
#if defined FLEX32
# define FLEX_PTRFMTH FLEX_UHCLONGFMT
# define FLEX_PTRFMTD FLEX_ULONGFMT
#elif defined FLEX64
# define FLEX_PTRFMTH FLEX_UVHCLONGFMT
# define FLEX_PTRFMTD FLEX_UVLONGFMT
#else
# error "You must define one of FLEX64/FLEX32/FLEX16/FLEX8"
#endif
#define USED(x) (void)(x)
/* WIN32 compiler doesn't always know 'inline', and already has min/max */
#ifdef _WIN32_WINNT
# define flexinline
#else
# define flexinline inline
# define max(x,y) ((x) > (y) ? (x) : (y))
# define min(x,y) ((x) < (y) ? (x) : (y))
#endif
typedef struct
{
char *data;
//obsolete... (delete after sflr migration to use flexlib (might be used there) long value;
int quoted;
int linenum;
int colnum;
void *next;
void *prev;
} Datum;
typedef struct
{
Datum *masthead;
Datum *head;
Datum *tail;
FlexLock lock;
} Input;
typedef struct
{
Datum *head;
Datum *tail;
FlexLock lock;
} Labels;
/* */
/* Flexp: flexible/buffering print routines */
/* */
enum
{
FLEX_CIRCBUFSZ = 65536*1024,
};
//obsolete typedef enum
//obsolete {
//obsolete Flexstdout = 1<<0, /* Print to stdout */
//obsolete Flexstderr = 1<<1, /* Print to stderr */
//obsolete Flexbuffer = 1<<2, /* Buffer (don't print) */
//obsolete Flexfd = 1<<3, /* Print to descriptor */
//obsolete } FlexPrintDest;
typedef struct
{
int silent; /* Flag to elide all action */
char *circbuf; /* String circular buffer */
int size; /* Allocated size of buffer */
int h2o; /* Fill level of circ buf */
int fd; /* For output to files, fd */
int off; /* Likewise, file offset */
//TODO: this has been factored out into the FlexErrState structure. Update flexmalloc.c and flextoken.c which is
//where it is used
// /* Called routines set or clear (i.e., [0]='\0') this: */
// char errstr[FLEX_ERRSTRLEN];
FlexLock lock;
} FlexPrintBuf;
typedef struct
{
FlexPrintBuf *fbs;
int nfbs;
FlexLock lock;
} FlexPrint;
/* */
/* This structure is passed in to all FlexLib functions, having */
/* previously being initialized. The callee sets the errlen */
/* upon an error, filling errstr with the selfsame number of */
/* chars (including terminating NUL) */
/* */
typedef struct
{
char errstr[FLEX_ERRSTRLEN];
/* Length of the current error */
int errlen;
} FlexErrState;
/* */
/* Flexm: flexible/monitoring allocator */
/* */
enum
{
FLEX_MAXIDSTRLEN = 64,
FLEX_MAXALLOCBLOCKS = 64,
FLEX_MAXALLOCS = 131072,
};
typedef struct
{
char id[FLEX_MAXIDSTRLEN];
uvlong *addrs;
int valid;
int allocs;
int frees;
int reallocs;
int mallocd;
FlexLock lock;
} FlexMblock;
/* */
/* This structure contains all the state needed by the FlexM */
/* routines. */
/* */
typedef struct
{
FlexMblock memblocks[FLEX_MAXALLOCBLOCKS];
int nmemblocks;
/* Enable alloc accounting: */
int debug;
char lock;
} FlexMstate;
typedef struct
{
int scanning;
Input istream;
Labels labellist;
FlexLock lock;
} FlexIstream;
//TODO: whereever these are used, e.g. in fn call args, check use type FlexFileMode, not int
typedef enum
{
FLEX_OTRUNCATE = 1 << 0,
FLEX_OREAD = 1 << 1,
FLEX_OWRITE = 1 << 2,
} FlexFileMode;
/*
TODO: april 2011: we should be able to replace uses of all three of the following
with GenericListItem which we already use in some places
typedef struct RealListItem RealListItem;
struct RealListItem
{
real value;
RealListItem *next;
};
typedef struct
{
RealListItem *hd;
RealListItem *tl;
int len;
} RealList;
typedef struct IntListItem IntListItem;
struct IntListItem
{
int value;
IntListItem *next;
};
typedef struct
{
IntListItem *hd;
IntListItem *tl;
int len;
} IntList;
typedef struct StringListItem StringListItem;
struct StringListItem
{
char *value;
StringListItem *next;
};
typedef struct
{
StringListItem *hd;
StringListItem *tl;
int len;
} StringList;
*/
/*
TODO: april 2011: remove these pre-determined GenericTypes, and leave it
up to the user of flexlib to define their own element types
typedef enum
{
GenericInteger,
GenericReal,
GenericString,
} GenericType;
*/
//TODO: rename this to FlexListItem, FlexList, FlexTupleListItem, and FlexTupleList.
// We currently use GenericList and TupleList in salsvm implementation and in Noisy
//
typedef struct GenericListItem GenericListItem;
struct GenericListItem
{
//GenericType type;
int type;
void *value;
GenericListItem *next;
};
typedef struct
{
GenericListItem *hd;
GenericListItem *tl;
int len;
} GenericList;
typedef struct TupleListItem TupleListItem;
struct TupleListItem
{
int cardinality;
GenericList *siblings;
TupleListItem *next;
};
typedef struct
{
TupleListItem *hd;
TupleListItem *tl;
int len;
} TupleList;
//TODO/BUG Until we actually rename structures, introduce aliases:
typedef GenericListItem FlexListItem;
typedef GenericList FlexList;
typedef TupleListItem FlexTupleListItem;
typedef TupleList FlexTupleList;
/* */
/* NOTE: the Flexm routines use the Flexp routines for printing. */
/* The Flexp (and thus the Flexm) routines will default to */
/* printing stdout if a NULL FlexPrintBuf is supplied. */
/* */
void* flexmalloc (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int size, char *id);
void* flexcalloc (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int nelem, int size, char *id);
void* flexrealloc (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, void *oldptr, int size, char *id);
void flexfree (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, void *ptr, char *id);
void flexmblocksdisplay (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P);
void flexincref (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, void *ptr);
void flexdecref (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, void *ptr);
void flexstreammunch (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, FlexIstream *S,
const char *sepchars, const char *stickies, const char *buf, int *curline, int *curcol);
void flexstreamclear (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, FlexIstream *I);
void flexstreamscan (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, FlexIstream *I);
void flexstreamchk (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, FlexIstream *I, int maxtokens, int fmtchars);
uvlong flexfsize (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int fd);
char* flexfgets (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, char *buf, int len, int fd);
int flexcreate (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, char *path, int omode);
int flexopen (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, char *path, int omode);
int flexread (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int fd, char* buf, int len);
int flexclose (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int fd);
FlexPrintBuf* flexbufalloc (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int circbufsz, char *filename, FlexFileMode mode);
void flexbufdealloc (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, FlexPrintBuf *moribund);
void flexbufreset (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P);
int flexwrite (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, int fd, char* buf, int len);
int flexchdir (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, char *path);
char* flexgetpwd (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P);
void flexprint (FlexErrState *E, FlexMstate *M, FlexPrintBuf *P, const char *fmt, ...);
/* */
/* Simple routines that do not need error and print buffers. */
/* */
void flexlock (FlexLock *l);
void flexunlock (FlexLock *l);
int flexsnprint (char *dst, int size, const char *fmt, ...);
void flexnsleep (ulong nsecs);
ulong flexusercputimeusecs (void);
ulong flexcputimeusecs (void);
ulong flexwallclockusecs (void);
ulong flextimeusecs (void);