1 /********************************************
2 mawk.h
3 copyright 2008-2016,2019 Thomas E. Dickey
4 copyright 1991-1995,1996 Michael D. Brennan
5 
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
8 
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
12 
13 /*
14  * $MawkId: mawk.h,v 1.54 2019/02/02 01:00:14 tom Exp $
15  */
16 
17 /*  mawk.h  */
18 
19 #ifndef  MAWK_H
20 #define  MAWK_H
21 
22 #include "nstd.h"
23 
24 #include <stdio.h>
25 #include <stdarg.h>
26 
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 
31 #include <assert.h>
32 
33 #include "types.h"
34 
35 #ifndef GCC_NORETURN
36 #define GCC_NORETURN		/* nothing */
37 #endif
38 
39 #ifndef GCC_PRINTFLIKE
40 #define  GCC_PRINTFLIKE(fmt,var)	/* nothing */
41 #endif
42 
43 #ifndef GCC_UNUSED
44 #define GCC_UNUSED		/* nothing */
45 #endif
46 
47 #if defined(__GNUC__) && defined(_FORTIFY_SOURCE)
48 #define IGNORE_RC(func) ignore_unused = (int) func
49 extern int ignore_unused;
50 #else
51 #define IGNORE_RC(func) (void) func
52 #endif /* gcc workarounds */
53 
54 #ifdef   DEBUG
55 #define  YYDEBUG  1
56 extern int yydebug;		/* print parse if on */
57 extern int dump_RE;
58 #endif
59 
60 #if defined(MSDOS) || defined(__MINGW32__) || defined(_WINNT)
61 #define USE_BINMODE 1
62 #else
63 #define USE_BINMODE 0
64 #endif
65 
66 extern short posix_space_flag, interactive_flag;
67 
68 /*----------------
69  *  GLOBAL VARIABLES
70  *----------------*/
71 
72 /* a well known string */
73 extern STRING null_str;
74 
75 /* a useful scratch area */
76 extern char string_buff[SPRINTF_LIMIT];
77 
78 /* help with casts */
79 extern const int mpow2[];
80 
81  /* these are used by the parser, scanner and error messages
82     from the compile  */
83 
84 extern char *pfile_name;	/* program input file */
85 extern int current_token;
86 extern unsigned token_lineno;	/* lineno of current token */
87 extern unsigned compile_error_count;
88 extern int NR_flag;
89 extern int paren_cnt;
90 extern int brace_cnt;
91 extern int print_flag, getline_flag;
92 extern short mawk_state;
93 #define EXECUTION       1	/* other state is 0 compiling */
94 
95 #ifdef LOCALE
96 extern char decimal_dot;
97 #endif
98 
99 extern const char *progname;	/* for error messages */
100 extern unsigned rt_nr, rt_fnr;	/* ditto */
101 
102 /* macro to test the type of two adjacent cells */
103 #define TEST2(cp)  (mpow2[(cp)->type]+mpow2[((cp)+1)->type])
104 
105 /* macro to get at the string part of a CELL */
106 #define string(cp) ((STRING *)(cp)->ptr)
107 
108 #ifdef   DEBUG
109 #define cell_destroy(cp)  DB_cell_destroy(cp)
110 #else
111 /* Note: type is only C_STRING to C_MBSTRN */
112 #define cell_destroy(cp) \
113 	do { \
114 	    if ( (cp)->type >= C_STRING && \
115 	         (cp)->type <= C_MBSTRN ) { \
116 		free_STRING(string(cp));  \
117 	    } \
118 	} while (0)
119 #endif
120 
121 /*  prototypes  */
122 
123 extern void cast1_to_s(CELL *);
124 extern void cast1_to_d(CELL *);
125 extern void cast2_to_s(CELL *);
126 extern void cast2_to_d(CELL *);
127 extern void cast_to_RE(CELL *);
128 extern void cast_for_split(CELL *);
129 extern void check_strnum(CELL *);
130 extern void cast_to_REPL(CELL *);
131 extern Int d_to_I(double);
132 extern UInt d_to_U(double d);
133 
134 #define d_to_i(d)     ((int)d_to_I(d))
135 
136 extern int test(CELL *);	/* test for null non-null */
137 extern CELL *cellcpy(CELL *, CELL *);
138 extern CELL *repl_cpy(CELL *, CELL *);
139 extern void DB_cell_destroy(CELL *);
140 extern void overflow(const char *, unsigned);
141 extern void rt_overflow(const char *, unsigned);
142 extern void rt_error(const char *,...) GCC_NORETURN GCC_PRINTFLIKE(1,2);
143 extern void mawk_exit(int) GCC_NORETURN;
144 extern void da(INST *, FILE *);
145 extern INST *da_this(INST *, INST *, FILE *);
146 extern char *rm_escape(char *, size_t *);
147 extern char *re_pos_match(char *, size_t, PTR, size_t *, int);
148 extern int binmode(void);
149 
150 #ifndef  REXP_H
151 extern char *str_str(char *, size_t, const char *, size_t);
152 #endif
153 
154 extern void parse(void);
155 extern int yylex(void);
156 extern int yyparse(void);
157 extern void yyerror(const char *);
158 extern void scan_cleanup(void);
159 
160 extern void bozo(const char *) GCC_NORETURN;
161 extern void errmsg(int, const char *,...) GCC_PRINTFLIKE(2,3);
162 extern void compile_error(const char *,...) GCC_PRINTFLIKE(1,2);
163 
164 extern void execute(INST *, CELL *, CELL *);
165 extern const char *find_kw_str(int);
166 extern void da_string(FILE *fp, const char *, size_t);
167 
168 #ifdef HAVE_STRTOD_OVF_BUG
169 extern double strtod_with_ovf_bug(const char *, char **);
170 #define strtod  strtod_with_ovf_bug
171 #endif
172 
173 #if OPT_TRACE > 0
174 extern void Trace(const char *,...) GCC_PRINTFLIKE(1,2);
175 extern void TraceVA(const char *, va_list);
176 #define TRACE(params) Trace params
177 #if OPT_TRACE > 1
178 #define TRACE2(params) Trace params
179 #endif
180 #endif
181 
182 #ifndef TRACE
183 #define TRACE(params)		/* nothing */
184 #endif
185 
186 #ifndef TRACE2
187 #define TRACE2(params)		/* nothing */
188 #endif
189 
190 #if OPT_TRACE > 0
191 extern void TraceCell(CELL *);
192 extern void TraceString(STRING *);
193 extern void TraceString2(const char *, size_t);
194 #define TRACE_CELL(cp)		TraceCell(cp)
195 #define TRACE_STRING(cp)	TraceString(cp)
196 #define TRACE_STRING2(str,len)	TraceString2(str,len)
197 #else
198 #define TRACE_CELL(cp)		/* nothing */
199 #define TRACE_STRING(cp)	/* nothing */
200 #define TRACE_STRING2(str,len)	/* nothing */
201 #endif
202 
203 #if OPT_TRACE > 0
204 extern void TraceFunc(const char *, CELL *);
205 #define TRACE_FUNC(name,cp) TraceFunc(name,cp)
206 #else
207 #define TRACE_FUNC(name,cp)	/* nothing */
208 #endif
209 
210 #if OPT_TRACE > 0
211 extern void TraceInst(INST *, INST *);
212 #define TRACE_INST(cp,base) TraceInst(cp,base)
213 #else
214 #define TRACE_INST(cp,base)	/* nothing */
215 #endif
216 
217 #if OPT_TRACE > 0
218 extern const char *da_type_name(CELL *);
219 extern const char *da_op_name(INST *);
220 #endif
221 
222 #ifdef NO_LEAKS
223 
224 extern void free_cell_data(CELL *);
225 extern void free_codes(const char *, INST *, size_t);
226 extern void no_leaks_cell(CELL *);
227 extern void no_leaks_cell_ptr(CELL *);
228 extern void no_leaks_re_ptr(PTR);
229 
230 extern void array_leaks(void);
231 extern void bi_vars_leaks(void);
232 extern void cell_leaks(void);
233 extern void code_leaks(void);
234 extern void field_leaks(void);
235 extern void files_leaks(void);
236 extern void fin_leaks(void);
237 extern void hash_leaks(void);
238 extern void re_leaks(void);
239 extern void rexp_leaks(void);
240 extern void scan_leaks(void);
241 extern void trace_leaks(void);
242 extern void zmalloc_leaks(void);
243 
244 #else
245 
246 #define free_codes(tag, base, size) zfree(base, size)
247 #define no_leaks_cell(ptr)	/* nothing */
248 #define no_leaks_cell_ptr(ptr)	/* nothing */
249 #define no_leaks_re_ptr(ptr)	/* nothing */
250 
251 #endif
252 
253 /*
254  * Sometimes split_buff[] pointers are moved rather than copied.
255  * Optimize-out the assignment to clear the pointer in the array.
256  */
257 #ifdef NO_LEAKS
258 #define USED_SPLIT_BUFF(n) split_buff[n] = 0
259 #else
260 #define USED_SPLIT_BUFF(n)	/* nothing */
261 #endif
262 
263 #endif /* MAWK_H */
264