1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2013 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *               Glenn Fowler <glenn.s.fowler@gmail.com>                *
18 *                    David Korn <dgkorn@gmail.com>                     *
19 *                     Phong Vo <phongvo@gmail.com>                     *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * standalone mini ast+sfio interface
25  */
26 
27 #ifndef _AST_H
28 #define _AST_H		1
29 
30 #include <ast_sa.h>
31 #include <ast_common.h>
32 
33 #if _PACKAGE_sfio
34 #include <sfio.h>
35 #else
36 #include <stdio.h>
37 #endif
38 
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 
43 #define FMT_EXP_CHAR	0x020		/* expand single byte chars	*/
44 #define FMT_EXP_LINE	0x040		/* expand \n and \r		*/
45 #define FMT_EXP_WIDE	0x080		/* expand \u \U \x wide chars	*/
46 #define FMT_EXP_NOCR	0x100		/* skip \r			*/
47 #define FMT_EXP_NONL	0x200		/* skip \n			*/
48 
49 #define STR_MAXIMAL	01		/* maximal match		*/
50 #define STR_LEFT	02		/* implicit left anchor		*/
51 #define STR_RIGHT	04		/* implicit right anchor	*/
52 #define STR_ICASE	010		/* ignore case			*/
53 #define STR_GROUP	020		/* (|&) inside [@|&](...) only	*/
54 
55 typedef int (*Error_f)(void*, void*, int, ...);
56 typedef int (*Qsortcmp_f)(const void*, const void*);
57 typedef int (*Qsortcmp_r_f)(const void*, const void*, void*);
58 
59 typedef struct
60 {
61 
62 	char*		id;
63 
64 	struct
65 	{
66 	unsigned int	serial;
67 	unsigned int	set;
68 	}		locale;
69 
70 	long		tmp_long;
71 	size_t		tmp_size;
72 	short		tmp_short;
73 	char		tmp_char;
74 	wchar_t		tmp_wchar;
75 
76 	int		(*collate)(const char*, const char*);
77 
78 	int		tmp_int;
79 	void*		tmp_pointer;
80 
81 	int		mb_cur_max;
82 	int		(*mb_len)(const char*, size_t);
83 	int		(*mb_towc)(wchar_t*, const char*, size_t);
84 	size_t		(*mb_xfrm)(char*, const char*, size_t);
85 	int		(*mb_width)(wchar_t);
86 	int		(*mb_conv)(char*, wchar_t);
87 
88 	unsigned int	env_serial;
89 
90 	char		pad[944];
91 
92 } _Ast_info_t;
93 
94 #define ast		_ast_info_
95 
96 #define elementsof(x)	(sizeof(x)/sizeof(x[0]))
97 #define integralof(x)	(((char*)(x))-((char*)0))
98 #define newof(p,t,n,x)	((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)calloc(1,sizeof(t)*(n)+(x)))
99 #define oldof(p,t,n,x)	((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)malloc(sizeof(t)*(n)+(x)))
100 #define pointerof(x)	((void*)((char*)0+(x)))
101 #define roundof(x,y)	(((x)+(y)-1)&~((y)-1))
102 
103 #ifndef offsetof
104 #define offsetof(type,member) ((unsigned long)&(((type*)0)->member))
105 #endif
106 
107 #if defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
108 #define NiL			0
109 #define NoP(x)			(void)(x)
110 #else
111 #define NiL			((char*)0)
112 #define NoP(x)			(&x,1)
113 #endif
114 
115 #define conformance(a,b)	"ast"
116 #define fmtident(s)		((char*)(s)+10)
117 #define mbchar(s)		(*s++)
118 #define setlocale(a,b)
119 
120 #define streq(a,b)		(*(a)==*(b)&&!strcmp(a,b))
121 #define strneq(a,b,n)		(*(a)==*(b)&&!strncmp(a,b,n))
122 #define strton(s,t,b,f)		strtol(s,t,0)
123 #define strtonll(s,t,b,f)	strtoll(s,t,0)
124 
125 #if !_PACKAGE_sfio
126 
127 #define Sfio_t		FILE
128 
129 #define sfstdin		stdin
130 #define sfstdout	stdout
131 #define sfstderr	stderr
132 
133 #define sfclose(f)	fclose(f)
134 #define sffileno(f)	fileno(f)
135 #define sfgetc(f)	fgetc(f)
136 #define sfopen(f,n,m)	fopen(n,m)
137 #define sfputc(f,c)	fputc(c,f)
138 #define sfread(f,b,n)	fread(b,n,1,f)
139 #define sfseek(f,p,w)	fseek(f,p,w)
140 #define sfset(f,v,n)
141 #define sfsync(f)	fflush(f)
142 #define sfwrite(f,b,n)	fwrite(b,n,1,f)
143 
144 #define sfprintf	fprintf
145 #define sfsprintf	snprintf
146 #define sfvprintf	vfprintf
147 
148 #define sfscanf		fscanf
149 
150 #define sfgetr		_sf_getr
151 
152 #include <sfstr.h>
153 
154 extern char*		sfgetr(Sfio_t*, int, int);
155 
156 #endif
157 
158 extern _Ast_info_t	ast;
159 
160 extern int		astwinsize(int, int*, int*);
161 extern int		chresc(const char*, char**);
162 extern char*		fmtbuf(size_t);
163 extern char*		fmtip4(uint32_t, int);
164 #define	qsort		_ast_qsort
165 extern void		qsort(void*, size_t, size_t, Qsortcmp_f);
166 extern char*		strcopy(char*, const char*);
167 extern int		strmatch(const char*, const char*);
168 extern int		strtoip4(const char*, char**, uint32_t*, unsigned char*);
169 
170 #endif
171