Skip to content

Commit 6fb5c24

Browse files
committed
configure.ac, m4/nut_check_bool.m4, include/nut_bool.h, tests/nutbooltest.c et al: introduce a NUT_CHECK_BOOL scriptlet and a header with nut_bool_t type [#1176]
Chose to use a unique type name to avoid conflicts with third-party headers which deliver a `bool_t` (currently commonly hacked into many NUT sources). Using lower-cased `true` and `false` values, hoping for maximum compatibility with C99 and newer language standards (if the compilers do implement them on whatever obscure platform NUT gets built on, and then our new `nut_bool_t` definition can be just an alias for what the language gives us). A test case was added to make sure it behaves as expected on different systems. Converting the sources from their custom type definitions and usages would be a separate step. Signed-off-by: Jim Klimov <[email protected]>
1 parent c4fd157 commit 6fb5c24

File tree

9 files changed

+555
-4
lines changed

9 files changed

+555
-4
lines changed

NEWS.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ during a NUT build.
183183
variable support was added to optionally disable this verification.
184184
Also the NUT daemons should request to double-check against their
185185
run-time process name (if it can be detected). [issue #2463]
186+
* introduced `m4` macros to check during `configure` phase for the
187+
platform, and a `nut_bool.h` header with `nut_bool_t` type to use
188+
during build, to avoid the numerous definitions of Boolean types
189+
and values (or macros) in the NUT codebase. [issue #1176, issue #31]
186190
187191
- various recipe, documentation and source files were revised to address
188192
respective warnings issued by the new generations of analysis tools.

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ AX_C_PRAGMAS
698698
AX_C___ATTRIBUTE__
699699
AX_C_PRINTF_STRING_NULL
700700

701+
dnl Check if the system provides a boolean type and how it is spelled
702+
NUT_CHECK_BOOL
703+
701704
dnl All current systems provide time.h; it need not be checked for.
702705
dnl Not all systems provide sys/time.h, but those that do, all allow
703706
dnl you to include it and time.h simultaneously.

docs/nut.dict

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
personal_ws-1.1 en 3190 utf-8
1+
personal_ws-1.1 en 3191 utf-8
22
AAC
33
AAS
44
ABI
@@ -1561,6 +1561,7 @@ bitmask
15611561
bitness
15621562
bitnesses
15631563
bn
1564+
bool
15641565
boolean
15651566
bootable
15661567
bp

include/Makefile.am

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
@NUT_AM_MAKE_CAN_EXPORT@@NUT_AM_EXPORT_CCACHE_PATH@export CCACHE_PATH=@CCACHE_PATH@
1010
@NUT_AM_MAKE_CAN_EXPORT@@NUT_AM_EXPORT_CCACHE_PATH@export PATH=@PATH_DURING_CONFIGURE@
1111

12-
dist_noinst_HEADERS = attribute.h common.h extstate.h proto.h \
13-
state.h str.h timehead.h upsconf.h nut_float.h nut_stdint.h nut_platform.h \
14-
nutstream.hpp nutwriter.hpp nutipc.hpp nutconf.hpp \
12+
dist_noinst_HEADERS = \
13+
attribute.h common.h extstate.h proto.h \
14+
state.h str.h timehead.h upsconf.h \
15+
nut_bool.h nut_float.h nut_stdint.h nut_platform.h \
16+
nutstream.hpp nutwriter.hpp nutipc.hpp nutconf.hpp \
1517
wincompat.h
1618

1719
# Optionally deliverable as part of NUT public API:

include/nut_bool.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* nut_bool.h - Network UPS Tools boolean type definitions
3+
* which should ensure a "nut_bool_t" name with
4+
* lower-case values "true" and "false"
5+
*
6+
* Inspired by earlier efforts and numerous definitions in NUT codebase.
7+
* Copyright (C) 2024 Jim Klimov <[email protected]>
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22+
*/
23+
24+
#ifndef NUT_BOOL_H_SEEN
25+
#define NUT_BOOL_H_SEEN 1
26+
27+
#ifdef __cplusplus
28+
/* *INDENT-OFF* */
29+
extern "C" {
30+
/* *INDENT-ON* */
31+
#endif
32+
33+
/* "config.h" is generated by autotools and lacks a header guard, so
34+
* we use an unambiguously named macro we know we must have, as one.
35+
* It must be the first header: be sure to know all about system config.
36+
*/
37+
#ifndef NUT_NETVERSION
38+
# include "config.h"
39+
#endif
40+
41+
/* See also https://en.cppreference.com/w/cpp/header/cstdbool for more
42+
* info about what should be available where per standard approach. */
43+
#ifdef __cplusplus
44+
# if defined HAVE_CSTDBOOL_H || defined HAVE_CSTDBOOL
45+
# include <cstdbool>
46+
# else
47+
# ifdef HAVE_STDBOOL_H
48+
# include <stdbool.h>
49+
# endif
50+
# endif
51+
#else
52+
/* plain C */
53+
# ifdef HAVE_STDBOOL_H
54+
# include <stdbool.h>
55+
# endif
56+
#endif
57+
58+
/* Is the goal achieved by the system headers or compiler itself,
59+
* so we can just alias to existing type and its values? */
60+
#if defined __bool_true_false_are_defined && __bool_true_false_are_defined
61+
typedef bool nut_bool_t;
62+
#elif defined FOUND__BOOL_TYPE && defined HAVE__BOOL_VALUE_LOWERCASE
63+
typedef FOUND__BOOL_TYPE nut_bool_t;
64+
#elif defined FOUND_BOOL_TYPE && defined HAVE_BOOL_VALUE_LOWERCASE
65+
typedef FOUND_BOOL_TYPE nut_bool_t;
66+
#elif defined FOUND_BOOLEAN_TYPE && defined HAVE_BOOLEAN_VALUE_LOWERCASE
67+
typedef FOUND_BOOLEAN_TYPE nut_bool_t;
68+
#elif defined FOUND_BOOL_T_TYPE && defined HAVE_BOOL_T_VALUE_LOWERCASE
69+
typedef FOUND_BOOL_T_TYPE nut_bool_t;
70+
#else
71+
/* Need a new type; can we use an enum with lower-case values? */
72+
# if (defined true && defined false) || defined HAVE__BOOL_VALUE_LOWERCASE || defined HAVE_BOOL_VALUE_LOWERCASE || defined HAVE_BOOLEAN_VALUE_LOWERCASE || defined HAVE_BOOL_T_VALUE_LOWERCASE
73+
/* Lower-case true/false are known */
74+
# if defined FOUND__BOOL_TYPE
75+
/* Got a C99 built-in mandated by the standard */
76+
typedef FOUND__BOOL_TYPE nut_bool_t;
77+
# else
78+
typedef int nut_bool_t;
79+
# endif
80+
# elif defined FOUND__BOOL_VALUE_TRUE && defined FOUND__BOOL_VALUE_FALSE
81+
typedef enum nut_bool_enum { false = FOUND__BOOL_VALUE_FALSE, true = FOUND__BOOL_VALUE_TRUE } nut_bool_t;
82+
# elif defined FOUND_BOOL_VALUE_TRUE && defined FOUND_BOOL_VALUE_FALSE
83+
typedef enum nut_bool_enum { false = FOUND_BOOL_VALUE_FALSE, true = FOUND_BOOL_VALUE_TRUE } nut_bool_t;
84+
# elif defined FOUND_BOOLEAN_VALUE_TRUE && defined FOUND_BOOLEAN_VALUE_FALSE
85+
typedef enum nut_bool_enum { false = FOUND_BOOLEAN_VALUE_FALSE, true = FOUND_BOOLEAN_VALUE_TRUE } nut_bool_t;
86+
# elif defined FOUND_BOOL_T_VALUE_TRUE && defined FOUND_BOOL_T_VALUE_FALSE
87+
typedef enum nut_bool_enum { false = FOUND_BOOL_T_VALUE_FALSE, true = FOUND_BOOL_T_VALUE_TRUE } nut_bool_t;
88+
# else
89+
typedef enum nut_bool_enum { false = 0, true = 1 } nut_bool_t;
90+
# endif
91+
#endif
92+
93+
#ifdef __cplusplus
94+
/* *INDENT-OFF* */
95+
}
96+
/* *INDENT-ON* */
97+
#endif
98+
99+
#endif /* NUT_BOOL_H_SEEN */

0 commit comments

Comments
 (0)