xref: /openbsd/usr.bin/vi/common/util.h (revision d9a51c35)
1 /*	$OpenBSD: util.h,v 1.5 2022/12/26 19:16:03 jmc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  *
11  *	@(#)util.h	10.5 (Berkeley) 3/16/96
12  */
13 
14 /* Macros to init/set/clear/test flags. */
15 #define	FL_INIT(l, f)	(l) = (f)		/* Specific flags location. */
16 #define	FL_SET(l, f)	((l) |= (f))
17 #define	FL_CLR(l, f)	((l) &= ~(f))
18 #define	FL_ISSET(l, f)	((l) & (f))
19 
20 #define	LF_INIT(f)	FL_INIT(flags, (f))	/* Local variable flags. */
21 #define	LF_SET(f)	FL_SET(flags, (f))
22 #define	LF_CLR(f)	FL_CLR(flags, (f))
23 #define	LF_ISSET(f)	FL_ISSET(flags, (f))
24 
25 #define	F_INIT(p, f)	FL_INIT((p)->flags, (f))	/* Structure element flags. */
26 #define	F_SET(p, f)	FL_SET((p)->flags, (f))
27 #define	F_CLR(p, f)	FL_CLR((p)->flags, (f))
28 #define	F_ISSET(p, f)	FL_ISSET((p)->flags, (f))
29 
30 /* Offset to next column of stop size, e.g. tab offsets. */
31 #define	COL_OFF(c, stop)	((stop) - ((c) % (stop)))
32 
33 /* Busy message types. */
34 typedef enum { B_NONE, B_OFF, B_READ, B_RECOVER, B_SEARCH, B_WRITE } bmsg_t;
35 
36 /*
37  * Number handling defines and prototypes.
38  *
39  * NNFITS:	test for addition of two negative numbers under a limit
40  * NPFITS:	test for addition of two positive numbers under a limit
41  * NADD_SLONG:	test for addition of two signed longs
42  * NADD_USLONG:	test for addition of two unsigned longs
43  */
44 enum nresult { NUM_ERR, NUM_OK, NUM_OVER, NUM_UNDER };
45 #define	NNFITS(min, cur, add)						\
46 	(((long)(min)) - (cur) <= (add))
47 #define	NPFITS(max, cur, add)						\
48 	(((unsigned long)(max)) - (cur) >= (add))
49 #define	NADD_SLONG(v1, v2)						\
50 	((v1) < 0 ?							\
51 	    ((v2) < 0 &&						\
52 	    NNFITS(LONG_MIN, (v1), (v2))) ? NUM_UNDER : NUM_OK :	\
53 	 (v1) > 0 ?							\
54 	    (v2) > 0 &&							\
55 	    NPFITS(LONG_MAX, (v1), (v2)) ? NUM_OK : NUM_OVER :		\
56 	 NUM_OK)
57