1 /* +-------------------------------------------------------------------+ */
2 /* | Copyright 1991, David Koblas.                                     | */
3 /* |   Permission to use, copy, modify, and distribute this software   | */
4 /* |   and its documentation for any purpose and without fee is hereby | */
5 /* |   granted, provided that the above copyright notice appear in all | */
6 /* |   copies and that both that copyright notice and this permission  | */
7 /* |   notice appear in supporting documentation.  This software is    | */
8 /* |   provided "as is" without express or implied warranty.           | */
9 /* +-------------------------------------------------------------------+ */
10 
11 #ifndef _DEFS_H_
12 #define _DEFS_H_
13 
14 #include "config.h"
15 
16 #include <ctype.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 
21 #if HAVE_MALLOC == 0
22 # ifdef malloc
23 #  undef malloc
24 #  define rpl_malloc
25 # endif
26 # ifdef realloc
27 #  undef realloc
28 #  define rpl_realloc
29 # endif
30 #endif
31 #include <stdlib.h>
32 #if HAVE_MALLOC == 0
33 # ifdef rpl_malloc
34 #  undef rpl_malloc
35 #  define malloc	rpl_malloc
36 # endif
37 # ifdef rpl_realloc
38 #  undef rpl_realloc
39 #  define realloc	rpl_realloc
40 # endif
41 #endif
42 
43 #include <unistd.h>
44 
45 #ifdef TIME_WITH_SYS_TIME
46 # include <sys/time.h>
47 # include <time.h>
48 #else
49 # ifdef HAVE_SYS_TIME_H
50 #  include <sys/time.h>
51 # else
52 #  include <time.h>
53 # endif
54 #endif
55 
56 #ifdef HAVE_DIRENT_H
57 # include <dirent.h>
58 # define NAMLEN(dirent) strlen((dirent)->d_name)
59 #else
60 # define dirent direct
61 # define NAMLEN(dirent) (dirent)->d_namlen
62 # ifdef HAVE_SYS_NDIR_H
63 #  include <sys/ndir.h>
64 # endif
65 # ifdef HAVE_SYS_DIR_H
66 #  include <sys/dir.h>
67 # endif
68 # ifdef HAVE_NDIR_H
69 #  include <ndir.h>
70 # endif
71 #endif
72 
73 #include <sys/types.h>
74 #ifdef HAVE_SYS_WAIT_H
75 # include <sys/wait.h>
76 #endif
77 #ifndef WEXITSTATUS
78 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
79 #endif
80 #ifndef WIFEXITED
81 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
82 #endif
83 
84 #ifdef STDC_HEADERS
85 # include <string.h>
86 #else
87 # ifndef HAVE_STRCHR
88 #  define strchr index
89 #  define strrchr rindex
90 # endif
91 char *strchr(), *strrchr();
92 # ifndef HAVE_MEMCPY
93 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
94 #  define memmove(d, s, n) bcopy ((s), (d), (n))
95 # endif
96 #endif
97 
98 #ifdef HAVE_LIBBSD
99 #include <bsd/string.h>
100 #else
101 # ifndef HAVE_STRLCAT
102 size_t strlcat(char *dst, const char *src, size_t siz);
103 size_t strlcpy(char *dst, const char *src, size_t siz);
104 # endif
105 #endif
106 
107 #ifndef HAVE_VSNPRINTF
108 int snprintf(char *str, size_t size, const char *format, ...);
109 int vsnprintf(char *str, size_t size, const char *format, va_list arg);
110 #endif
111 
112 #ifndef MIN
113 #define MIN(a, b)	((a) < (b) ? (a) : (b))
114 #endif
115 
116 #define UNUSED(x) (void)(x)
117 
118 #ifdef malloc
119 void *rpl_malloc(size_t n);
120 #endif
121 #ifdef realloc
122 void *rpl_realloc(void *ptr, size_t n);
123 #endif
124 
125 typedef struct cmd_s {
126     char *name;
127     size_t nargs, nopts;
128     size_t margs, mopts;
129     char **args, **opts;
130     struct cmd_s *next;
131 } cmd_t;
132 
133 typedef struct var_s {
134     char *name, *value;
135     struct var_s *next;
136 } var_t;
137 
138 typedef struct array_s {
139     void **data;
140     size_t size, capacity;
141 } array_t;
142 
143 /* functions to manage a dynamically extensible array of pointers */
144 #define ARRAY_CHUNK	32
145 array_t *array_alloc(void);
146 void array_free(array_t * array);
147 array_t *array_free_contents(array_t * array);
148 void *array_push(array_t * array, void *object);
149 void *array_pop(array_t * array);
150 int array_extend(array_t * array, size_t capacity);
151 
152 char *savestr(const char *str);
153 cmd_t *Build(cmd_t * def, cmd_t * cmd);
154 cmd_t *BuildSingle(cmd_t * def, cmd_t * cmd);
155 
156 extern cmd_t *First;
157 extern var_t *Variables;
158 
159 /* cppcheck-suppress noreturn */
160 int logger(unsigned level, const char *format, ...);
161 void fatal(int logit, const char *format, ...);
162 char *strtolower(char *in);
163 /* NOLINTNEXTLINE(runtime/int) */
164 long strtolong(const char *str, int base);
165 
166 int ReadFile(const char *file);
167 int CountArgs(cmd_t * cmd);
168 
169 #define MAXSTRLEN	2048
170 #ifndef SYSCONFDIR
171 #define SYSCONFDIR	"/etc"
172 #endif
173 #define OP_ACCESS	SYSCONFDIR "/op.conf"
174 #define OP_ACCESS_DIR	SYSCONFDIR "/op.d"
175 
176 #define VAR_EXPAND_LEN	8192
177 #define	VAR_NAME_LEN	64
178 
179 #ifndef HOST_NAME_MAX
180 #define HOST_NAME_MAX	255
181 #endif
182 
183 #ifndef PASS_MAX
184 #define PASS_MAX	512
185 #endif
186 
187 #endif /* !_DEFS_H_ */
188