etc/protocols - sync with NetBSD-8
[minix.git] / bin / ksh / expand.h
blob5618af2228e06e22bef91c5925d9cbbae0c18986
1 /* $NetBSD: expand.h,v 1.5 2013/10/18 19:53:34 christos Exp $ */
3 /*
4 * Expanding strings
5 */
6 /* $Id: expand.h,v 1.5 2013/10/18 19:53:34 christos Exp $ */
8 #define X_EXTRA 8 /* this many extra bytes in X string */
10 #if 0 /* Usage */
11 XString xs;
12 char *xp;
14 Xinit(xs, xp, 128, ATEMP); /* allocate initial string */
15 while ((c = generate()) {
16 Xcheck(xs, xp); /* expand string if necessary */
17 Xput(xs, xp, c); /* add character */
19 return Xclose(xs, xp); /* resize string */
21 * NOTE:
22 * The Xcheck and Xinit macros have a magic + X_EXTRA in the lengths.
23 * This is so that you can put up to X_EXTRA characters in a XString
24 * before calling Xcheck. (See yylex in lex.c)
26 #endif /* 0 */
28 typedef struct XString {
29 char *end, *beg; /* end, begin of string */
30 size_t len; /* length */
31 Area *areap; /* area to allocate/free from */
32 } XString;
34 typedef char * XStringP;
36 /* initialize expandable string */
37 #define Xinit(xs, xp, length, area) do { \
38 (xs).len = length; \
39 (xs).areap = (area); \
40 (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
41 (xs).end = (xs).beg + (xs).len; \
42 xp = (xs).beg; \
43 __USE(xp); \
44 } while (0)
46 /* stuff char into string */
47 #define Xput(xs, xp, c) (*xp++ = (c))
49 /* check if there are at least n bytes left */
50 #define XcheckN(xs, xp, n) do { \
51 int more = ((xp) + (n)) - (xs).end; \
52 if (more > 0) \
53 xp = Xcheck_grow_(&xs, xp, more); \
54 } while (0)
56 /* check for overflow, expand string */
57 #define Xcheck(xs, xp) XcheckN(xs, xp, 1)
59 /* free string */
60 #define Xfree(xs, xp) afree((void*) (xs).beg, (xs).areap)
62 /* close, return string */
63 #define Xclose(xs, xp) (char*) aresize((void*)(xs).beg, \
64 (size_t)((xp) - (xs).beg), (xs).areap)
65 /* begin of string */
66 #define Xstring(xs, xp) ((xs).beg)
68 #define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */
69 #define Xlength(xs, xp) ((xp) - (xs).beg)
70 #define Xsize(xs, xp) ((xs).end - (xs).beg)
71 #define Xsavepos(xs, xp) ((xp) - (xs).beg)
72 #define Xrestpos(xs, xp, n) ((xs).beg + (n))
74 char * Xcheck_grow_ ARGS((XString *xsp, char *xp, int more));
77 * expandable vector of generic pointers
80 typedef struct XPtrV {
81 void **cur; /* next avail pointer */
82 void **beg, **end; /* begin, end of vector */
83 } XPtrV;
85 #define XPinit(x, n) do { \
86 register void **vp__; \
87 vp__ = (void**) alloc(sizeofN(void*, n), ATEMP); \
88 (x).cur = (x).beg = vp__; \
89 (x).end = vp__ + n; \
90 } while (0)
92 #define XPput(x, p) do { \
93 if ((x).cur >= (x).end) { \
94 int n = XPsize(x); \
95 (x).beg = (void**) aresize((void*) (x).beg, \
96 sizeofN(void*, n*2), ATEMP); \
97 (x).cur = (x).beg + n; \
98 (x).end = (x).cur + n; \
99 } \
100 *(x).cur++ = (p); \
101 } while (0)
103 #define XPptrv(x) ((x).beg)
104 #define XPsize(x) ((x).cur - (x).beg)
106 #define XPclose(x) (void**) aresize((void*)(x).beg, \
107 sizeofN(void*, XPsize(x)), ATEMP)
109 #define XPfree(x) afree((void*) (x).beg, ATEMP)