1 /*
2  * common definition and typedefs
3  */
4 
5 #ifndef _DEFINES_H_
6 #define _DEFINES_H_
7 
8 #if !defined(SYS_TIME_H) && !defined(_H_SYS_TIME)
9 #  include <sys/time.h>
10 #endif
11 
12 #ifdef AIX
13 #  include <sys/select.h>
14 #endif
15 
16 #define memzero(a,b)	memset((a), 0, (b))
17 
18 #ifdef USE_RANDOM
19 #  define get_random random
20 #  define init_random srandom
21 #else
22 #  define get_random lrand48
23 #  define init_random srand48
24 #endif
25 
26 #ifdef __STDC__
27 
28 # define VOLATILE volatile
29 # ifndef __P
30 #  define __P(args)					args
31 # endif
32 # define __P0(dummy)					(void)
33 # define __P1(t1,a1)					(t1 a1)
34 # define __P2(t1,a1,t2,a2)				(t1 a1, t2 a2)
35 # define __P3(t1,a1,t2,a2,t3,a3)			(t1 a1, t2 a2, t3 a3)
36 # define __P4(t1,a1,t2,a2,t3,a3,t4,a4)			(t1 a1, t2 a2, t3 a3, t4 a4)
37 # define __P5(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5)		(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)
38 # define __P6(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6)	(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6)
39 # define __P7(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7) (t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7)
40 
41 #else /* ! __STDC__ */
42 
43 # define VOLATILE
44 # ifndef __P
45 #  define __P(args)					()
46 # endif
47 # define __P0(dummy)					()
48 # define __P1(t1,a1)					(a1) t1 a1;
49 # define __P2(t1,a1,t2,a2)				(a1, a2) t1 a1; t2 a2;
50 # define __P3(t1,a1,t2,a2,t3,a3)			(a1, a2, a3) t1 a1; t2 a2; t3 a3;
51 # define __P4(t1,a1,t2,a2,t3,a3,t4,a4)			(a1, a2, a3, a4) t1 a1; t2 a2; t3 a3; t4 a4;
52 # define __P5(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5)		(a1, a2, a3, a4, a5) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5;
53 # define __P6(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6)	(a1, a2, a3, a4, a5, a6) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6;
54 # define __P7(t1,a1,t2,a2,t3,a3,t4,a4,t5,a5,t6,a6,t7,a7) (a1, a2, a3, a4, a5, a6, a7) t1 a1; t2 a2; t3 a3; t4 a4; t5 a5; t6 a6; t7 a7;
55 
56 #endif /* __STDC__ */
57 
58 #ifdef __GNUC__
59 # define INLINE static inline
60 #else
61 # define INLINE static
62 #endif
63 
64 #ifndef NULL
65 #  define NULL ((void *)0)
66 #endif
67 
68 #ifndef LONG_MAX
69 #  define LONG_MAX ( (long) ((~(unsigned long)0) >> 1) )
70 #endif
71 
72 #ifndef LONG_MIN
73 #  define LONG_MIN ( (long) (((~(unsigned long)0) >> 1) + 1) )
74 #endif
75 
76 #ifndef INT_MAX
77 #  define INT_MAX ( (int) ((~(unsigned int)0) >> 1) )
78 #endif
79 
80 #ifndef INT_MIN
81 #  define INT_MIN ( (int) (((~(unsigned int)0) >> 1) + 1) )
82 #endif
83 
84 #define uSEC_PER_SEC ((long)1000000)  /* microseconds in a second */
85 #define mSEC_PER_SEC ((long)1000)     /* milliseconds in a second */
86 #define uSEC_PER_mSEC ((long)1000)    /* microseconds in a millisecond */
87 
88 #undef MIN2
89 #undef MAX2
90 #undef ABS
91 #undef SIGN
92 #undef SWAP2
93 #define MIN2(a,b) ((a)<(b) ? (a) : (b))
94 #define MAX2(a,b) ((a)>(b) ? (a) : (b))
95 #define ABS(a)    ((a)> 0  ? (a) :(-a))
96 #define SIGN(a)   ((a)> 0  ?  1  : (a) ? -1 : 0)
97 #define SWAP2(a,b,c) ((c)=(b), (b)=(a), (a)=(c))
98 
99 /* macros to match parentheses */
100 #define ISRPAREN(c) ((c) == ')' || (c) == ']' || (c) == '}')
101 #define ISLPAREN(c) ((c) == '(' || (c) == '[' || (c) == '{')
102 #define LPAREN(c) ((c) == ')' ? '(' : ((c) == ']' ? '[' : '{'))
103 
104 #define ISODIGIT(c) ((c) >= '0' && (c) <= '7')
105 
106 #define PRINTF status(1), tty_printf
107 
108 #define INTLEN		(3*(1+(int)sizeof(int)))
109 				/* max length of a string representation
110 				 * of an int */
111 #define LONGLEN		(3*(1+(int)sizeof(long)))
112 				/* max length of a string representation
113 				 * of a long */
114 #define ESC		'\\'	/* special escape char */
115 #define STRESC		"\\"
116 #define ESC2		'`'	/* other special escape char */
117 #define STRESC2		"`"
118 #define CMDSEP		';'	/* command separator character */
119 #define SPECIAL_CHARS	"{}();\"=" /* specials chars needing escape */
120 #define MPI		"~$#E"	/* MUME protocol introducer */
121 #define MPILEN		4	/* strlen(MPI) */
122 
123 #ifdef NR_OPEN
124 # define MAX_FDSCAN	NR_OPEN
125 #else
126 # define MAX_FDSCAN	256	/* max number of fds */
127 #endif
128 
129 #define MAX_CONNECTS	32	/* max number of open connections. must fit in a byte */
130 
131 #define CAPLEN		20	/* max length of a terminal capability */
132 #define BUFSIZE		4096	/* general buffer size */
133 #define SOCKBUFSIZE	BUFSIZE	/* socket buffer size for read */
134 #define PARAMLEN	99	/* initial length of text strings */
135 #define MAX_MAPLEN	1000	/* maximum length of automapped path */
136 #define MIN_WORDLEN	3	/* the minimum length for history words */
137 #define MAX_WORDS	512	/* number of words kept for TAB-completion */
138 #define MAX_HIST	128	/* number of history lines kept */
139 #define LOG_MAX_HASH	7
140 #define MAX_HASH	(1<<LOG_MAX_HASH) /* max hash value, must be a power of 2 */
141 #define NUMPARAM	10	/* number of local unnamed params allowed
142 				 * (hardcoded, don't change) */
143 #define NUMVAR		50	/* number of global unnamed variables */
144 #define NUMTOT		(NUMVAR+NUMPARAM)
145 #define MAX_SUBOPT	256	/* max length of suboption string */
146 #define MAX_ARGS	16	/* max number of arguments to editor */
147 #define FLASHDELAY	500	/* time of parentheses flash in millisecs */
148 #define KBD_TIMEOUT	100	/* timeout for keyboard read in millisecs;
149 				 * hope it's enough also for very slow lines */
150 
151 #define MAX_STACK	100	/* maximum number of nested
152                                  * action, alias, #for or #while */
153 #define MAX_LOOP	10000	/* maximum number of iterations in
154                                  * #for or #while */
155 
156 #define ACTION_WEAK	0	/* GH: normal junk */
157 #define ACTION_REGEXP	1	/*     oh-so-mighty regexp */
158 #define ACTION_TYPES	(ACTION_REGEXP + 1)
159 
160 /* GH: the redefinable delimeters */
161 #define DELIM		(delim_list[delim_mode])
162 #define DELIM_LEN	(delim_len[delim_mode])
163 #define IS_DELIM(c)	(strchr(DELIM, (c)))
164 
165 #define DELIM_NORMAL	0	/* GH: normal word delimeters	*/
166 #define DELIM_PROGRAM	1	/*     ()[]{}.,;"'+/-*%		*/
167 #define DELIM_CUSTOM	2	/*     user-defined		*/
168 #define DELIM_MODES	(DELIM_CUSTOM + 1)
169 
170 
171 /* macros to find cursor position from input buffer position */
172 #define CURLINE(pos)	(((pos) + col0) / cols_1 + line0)
173 #define CURCOL(pos)	(((pos) + col0) % cols_1)
174 
175 #define CLIP(a, min, max) ((a)=(a)<(min) ? (min) : (a)>(max) ? (max) : (a))
176 
177 #define ISMARKWILDCARD(c) ((c) == '&' || (c) == '$')
178 
179 /*
180  * Attribute codes: bit 0-4 for foreground color, 5-9 for
181  * background color, 10-13 for effects.
182  * Color #16 is "none", so 0x0210 is "no attribute".
183  */
184 #define COLORS         16         /* number of colors on HFT terminal */
185 #define LOWCOLORS      8          /* number of ANSI colors */
186 #define NO_COLOR       COLORS     /* no color change, use default */
187 #define BITS_COLOR     5          /* bits used for a color entry
188                                      (16==none is a valid color) */
189 #define BITS_2COLOR    10         /* bits used for 2 color entries */
190 #define COLOR_MASK     0x1F       /* 5 (BITS_COLOR) bits set to 1, others 0 */
191 
192 #define ATTR_BOLD      0x01
193 #define ATTR_BLINK     0x02
194 #define ATTR_UNDERLINE 0x04
195 #define ATTR_INVERSE   0x08
196 
197 /*
198  * WARNING: colors and attributes are currently using 14 bits:
199  * 4 for attributes, 5 for foreground color and 5 for background.
200  * type used is int and -1 is used as 'invalid attribute'
201  * so in case ints are 16 bits, there is only 1 bit left unused.
202  * In case ints are 32 bits, no problem.
203  */
204 
205 /* constructors / accessors for attribute codes */
206 #define ATTRCODE(attr, fg, bg) \
207                 (((attr) << BITS_2COLOR) | ((bg) << BITS_COLOR) | (fg))
208 #define FOREGROUND(attrcode) ((attrcode) & COLOR_MASK)
209 #define BACKGROUND(attrcode) (((attrcode) >> BITS_COLOR) & COLOR_MASK)
210 #define ATTR(attrcode)       ((attrcode) >> BITS_2COLOR)
211 
212 #define NOATTRCODE ATTRCODE(0, NO_COLOR, NO_COLOR)
213 
214 /*
215  * NCSA telnet 2.2 doesn't reset the color when it receives "esc [ m",
216  * so we must know what the normal colors are in order to reset it.
217  * These colors can be changed with the #color command.
218  */
219 #ifdef BUG_TELNET
220 # define DEFAULTFG 7		/* make default white text */
221 # define DEFAULTBG 4		/* on blue background */
222 #endif
223 
224 #define LM_NOECHO 1	/* no local echo */
225 #define LM_CHAR 2	/* char-by-char mode (no line editing) */
226 
227 
228 
229 
230 
231 typedef unsigned char byte;
232 
233 typedef void (*function_any) ();	/* generic function pointer */
234 
235 typedef void (*function_int) __P ((int i));
236 
237 typedef function_int function_signal;
238 
239 typedef void (*function_str)	__P ((char *arg));
240 
241 typedef struct timeval vtime;   /* needs #include <sys/tyme.h> */
242 
243 #include "ptr.h"
244 
245 
246 
247 
248 /* generic linked list node (never actually created) */
249 typedef struct defnode {
250     struct defnode *next;
251     char *sortfield;
252 } defnode;
253 
254 /*
255  * twin linked list node: used to build pair of parallel lists,
256  * one sorted and one not, with the same nodes
257  */
258 typedef struct sortednode {
259     struct sortednode *next;
260     char *sortfield;
261     struct sortednode *snext;
262 } sortednode;
263 
264 /*
265  * linked list nodes: keep "next" first, then string to sort by,
266  * then (eventually) `snext'
267  */
268 typedef struct aliasnode {
269     struct aliasnode *next;
270     char *name;
271     struct aliasnode *snext;
272     char *subst;
273     char *group;
274     int active;
275 } aliasnode;
276 
277 typedef struct marknode {
278     struct marknode *next;
279     char *pattern;
280     int attrcode;
281     char *start, *end;
282     char mbeg;
283     char wild;
284 } marknode;
285 
286 typedef struct triggernode {
287     struct triggernode *next;
288     char *command, *label;
289     int active;
290     int type;				/* GH: allow regexp */
291     char *pattern;
292 #ifdef USE_REGEXP
293     void *regexp;			/* 0 if type == ACTION_WEAK */
294 #endif
295     char *group;
296 } triggernode;
297 
298 /*
299  * HACK WARNING :
300  * actionnode and promptnode must be the same type
301  * or search_action_or_prompt() in main.c won't work.
302  */
303 typedef triggernode actionnode;
304 typedef triggernode promptnode;
305 
306 typedef int  (*function_sort)	__P ((defnode *node1, defnode *node2));
307 
308 typedef struct keynode {
309     struct keynode *next;
310     char *name;			/* key name */
311     char *sequence;		/* escape sequence sent by terminal */
312     int seqlen;			/* GH: length of esc seq to allow \0 in seq */
313     function_str funct;		/* function called when key pressed */
314     char *call_data;		/* data passed to function */
315 } keynode;
316 
317 typedef struct delaynode {
318     struct delaynode *next;
319     char *name;
320     char *command;
321     vtime when;                 /* structure containing time when */
322 				/* command must be executed */
323 } delaynode;
324 
325 /* Variable struct definitions */
326 
327 typedef struct varnode {        /* for named variables */
328     struct varnode *next;
329     char *name;
330     struct varnode *snext;
331     int index;
332     long num;
333     ptr  str;
334 } varnode;
335 
336 typedef struct {                /* for unnamed vars */
337     long num;
338     ptr  str;
339 } unnamedvar;
340 
341 typedef struct {		/* stack of local vars */
342     unnamedvar p[MAX_STACK][NUMPARAM];
343     int curr;
344 } param_stack;
345 
346 typedef struct {		/* pointers to all variables */
347     long *num;
348     ptr  *str;
349 } vars;
350 
351 /* editing session control */
352 typedef struct editsess {
353     struct editsess *next;
354     unsigned int key;	/* session identifier */
355     int pid;		/* pid of child */
356     int fd;		/* MUD socket to talk with (-1 if non-MUD text) */
357     char *descr;  	/* short description of what we are editing */
358     char *file;		/* name of temporary file */
359     time_t ctime; 	/* time when temp file was created (upper bound) */
360     long oldsize; 	/* original file size */
361     char cancel;	/* 1 if cancelled */
362 } editsess;
363 
364 #endif /* _DEFINES_H_ */
365 
366