1 /* general.h -- defines that everybody likes to use. */
3 /* Copyright (C) 1993-2016 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 #if !defined (_GENERAL_H_)
26 #include "bashtypes.h"
27 #include "chartypes.h"
29 #if defined (HAVE_SYS_RESOURCE_H) && defined (RLIMTYPE)
30 # if defined (HAVE_SYS_TIME_H)
31 # include <sys/time.h>
33 # include <sys/resource.h>
36 #if defined (HAVE_STRING_H)
40 #endif /* !HAVE_STRING_H */
42 #if defined (HAVE_LIMITS_H)
48 /* NULL pointer type. */
50 # if defined (__STDC__)
51 # define NULL ((void *) 0)
54 # endif /* !__STDC__ */
57 /* Hardly used anymore */
58 #define pointer_to_int(x) (int)((char *)x - (char *)0)
60 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
61 extern char *strchr (), *strrchr ();
64 #if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
65 extern char *strcpy
__P((char *, const char *));
68 #if !defined (savestring)
69 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
73 # define member(c, s) ((c) ? ((char *)mbschr ((s), (c)) != (char *)NULL) : 0)
77 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
81 # ifdef __CHAR_UNSIGNED__
82 # define CHAR_MAX 0xff
84 # define CHAR_MAX 0x7f
92 /* Nonzero if the integer type T is signed. */
93 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
95 /* The width in bits of the integer type or expression T.
96 Padding bits are not supported; this is checked at compile-time below. */
97 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
99 /* Bound on length of the string representing an unsigned integer
100 value representable in B bits. log10 (2.0) < 146/485. The
101 smallest value of B where this bound is not tight is 2621. */
102 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
104 /* Bound on length of the string representing an integer value of type T.
105 Subtract one for the sign bit if T is signed;
106 302 / 1000 is log10 (2) rounded up;
107 add one for integer division truncation;
108 add one more for a minus sign if t is signed. */
109 #define INT_STRLEN_BOUND(t) \
110 ((TYPE_WIDTH (t) - TYPE_SIGNED (t)) * 302 / 1000 \
111 + 1 + TYPE_SIGNED (t))
113 /* Updated version adapted from gnulib/intprops.h, not used right now.
114 Changes the approximation of log10(2) from 302/1000 to 146/485. */
116 #define INT_STRLEN_BOUND(t) \
117 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - TYPE_SIGNED (t)) + TYPE_SIGNED(t))
120 /* Bound on buffer size needed to represent an integer type or expression T,
121 including the terminating null. */
122 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
124 /* Define exactly what a legal shell identifier consists of. */
125 #define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
126 #define legal_variable_char(c) (ISALNUM(c) || c == '_')
128 /* Definitions used in subst.c and by the `read' builtin for field
130 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
132 /* All structs which contain a `next' field should have that field
133 as the first field in the struct. This means that functions
134 can be written to handle the general case for linked lists. */
135 typedef struct g_list
{
139 /* Here is a generic structure for associating character strings
140 with integers. It is used in the parser for shell tokenization. */
146 /* A macro to avoid making an unnecessary function call. */
147 #define REVERSE_LIST(list, type) \
148 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
152 # define FASTCOPY(s, d, n) __builtin_memcpy ((d), (s), (n))
153 #else /* !__GNUC__ */
154 # if !defined (HAVE_BCOPY)
155 # if !defined (HAVE_MEMMOVE)
156 # define FASTCOPY(s, d, n) memcpy ((d), (s), (n))
158 # define FASTCOPY(s, d, n) memmove ((d), (s), (n))
159 # endif /* !HAVE_MEMMOVE */
160 # else /* HAVE_BCOPY */
161 # define FASTCOPY(s, d, n) bcopy ((s), (d), (n))
162 # endif /* HAVE_BCOPY */
163 #endif /* !__GNUC__ */
165 /* String comparisons that possibly save a function call each. */
166 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
167 #define STREQN(a, b, n) ((n == 0) ? (1) \
168 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
170 /* More convenience definitions that possibly save system or libc calls. */
171 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
172 #define FREE(s) do { if (s) free (s); } while (0)
173 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
175 /* A fairly hairy macro to check whether an allocated string has more room,
176 and to resize it using xrealloc if it does not.
177 STR is the string (char *)
178 CIND is the current index into the string (int)
179 ROOM is the amount of additional room we need in the string (int)
180 CSIZE is the currently-allocated size of STR (int)
181 SINCR is how much to increment CSIZE before calling xrealloc (int) */
183 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
185 if ((cind) + (room) >= csize) \
187 while ((cind) + (room) >= csize) \
189 str = xrealloc (str, csize); \
193 /* Function pointers can be declared as (Function *)foo. */
194 #if !defined (_FUNCTION_DEF)
195 # define _FUNCTION_DEF
196 typedef int Function ();
197 typedef void VFunction ();
198 typedef char *CPFunction (); /* no longer used */
199 typedef char **CPPFunction (); /* no longer used */
200 #endif /* _FUNCTION_DEF */
202 #ifndef SH_FUNCTION_TYPEDEF
203 # define SH_FUNCTION_TYPEDEF
205 /* Shell function typedefs with prototypes */
206 /* `Generic' function pointer typedefs */
208 typedef int sh_intfunc_t
__P((int));
209 typedef int sh_ivoidfunc_t
__P((void));
210 typedef int sh_icpfunc_t
__P((char *));
211 typedef int sh_icppfunc_t
__P((char **));
212 typedef int sh_iptrfunc_t
__P((PTR_T
));
214 typedef void sh_voidfunc_t
__P((void));
215 typedef void sh_vintfunc_t
__P((int));
216 typedef void sh_vcpfunc_t
__P((char *));
217 typedef void sh_vcppfunc_t
__P((char **));
218 typedef void sh_vptrfunc_t
__P((PTR_T
));
220 typedef int sh_wdesc_func_t
__P((WORD_DESC
*));
221 typedef int sh_wlist_func_t
__P((WORD_LIST
*));
223 typedef int sh_glist_func_t
__P((GENERIC_LIST
*));
225 typedef char *sh_string_func_t
__P((char *)); /* like savestring, et al. */
227 typedef int sh_msg_func_t
__P((const char *, ...)); /* printf(3)-like */
228 typedef void sh_vmsg_func_t
__P((const char *, ...)); /* printf(3)-like */
230 /* Specific function pointer typedefs. Most of these could be done
232 typedef void sh_sv_func_t
__P((char *)); /* sh_vcpfunc_t */
233 typedef void sh_free_func_t
__P((PTR_T
)); /* sh_vptrfunc_t */
234 typedef void sh_resetsig_func_t
__P((int)); /* sh_vintfunc_t */
236 typedef int sh_ignore_func_t
__P((const char *)); /* sh_icpfunc_t */
238 typedef int sh_assign_func_t
__P((const char *));
239 typedef int sh_wassign_func_t
__P((WORD_DESC
*, int));
241 typedef int sh_load_func_t
__P((char *));
242 typedef void sh_unload_func_t
__P((char *));
244 typedef int sh_builtin_func_t
__P((WORD_LIST
*)); /* sh_wlist_func_t */
246 #endif /* SH_FUNCTION_TYPEDEF */
248 #define NOW ((time_t) time ((time_t *) 0))
250 /* Some defines for calling file status functions. */
251 #define FS_EXISTS 0x1
252 #define FS_EXECABLE 0x2
253 #define FS_EXEC_PREFERRED 0x4
254 #define FS_EXEC_ONLY 0x8
255 #define FS_DIRECTORY 0x10
256 #define FS_NODIRS 0x20
257 #define FS_READABLE 0x40
259 /* Default maximum for move_to_high_fd */
260 #define HIGH_FD_MAX 256
262 /* The type of function passed as the fourth argument to qsort(3). */
264 typedef int QSFUNC (const void *, const void *);
266 typedef int QSFUNC ();
269 /* Some useful definitions for Unix pathnames. Argument convention:
270 x == string, c == character */
272 #if !defined (__CYGWIN__)
273 # define ABSPATH(x) ((x)[0] == '/')
274 # define RELPATH(x) ((x)[0] != '/')
275 #else /* __CYGWIN__ */
276 # define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
277 # define RELPATH(x) (ABSPATH(x) == 0)
278 #endif /* __CYGWIN__ */
280 #define ROOTEDPATH(x) (ABSPATH(x))
283 #if !defined (__CYGWIN__)
284 # define ISDIRSEP(c) ((c) == '/')
286 # define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
287 #endif /* __CYGWIN__ */
288 #define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
291 /* Declarations for functions defined in xmalloc.c */
292 extern PTR_T xmalloc
__P((size_t));
293 extern PTR_T xrealloc
__P((void *, size_t));
294 extern void xfree
__P((void *));
297 /* Declarations for functions defined in general.c */
298 extern void posix_initialize
__P((int));
300 extern int num_posix_options
__P((void));
301 extern char *get_posix_options
__P((char *));
302 extern void set_posix_options
__P((const char *));
304 #if defined (RLIMTYPE)
305 extern RLIMTYPE string_to_rlimtype
__P((char *));
306 extern void print_rlimtype
__P((RLIMTYPE
, int));
309 extern int all_digits
__P((const char *));
310 extern int legal_number
__P((const char *, intmax_t *));
311 extern int legal_identifier
__P((const char *));
312 extern int importable_function_name
__P((const char *, size_t));
313 extern int exportable_function_name
__P((const char *));
314 extern int check_identifier
__P((WORD_DESC
*, int));
315 extern int valid_nameref_value
__P((const char *, int));
316 extern int check_selfref
__P((const char *, char *, int));
317 extern int legal_alias_name
__P((const char *, int));
318 extern int line_isblank
__P((const char *));
319 extern int assignment
__P((const char *, int));
321 extern int sh_unset_nodelay_mode
__P((int));
322 extern int sh_setclexec
__P((int));
323 extern int sh_validfd
__P((int));
324 extern int fd_ispipe
__P((int));
325 extern void check_dev_tty
__P((void));
326 extern int move_to_high_fd
__P((int, int, int));
327 extern int check_binary_file
__P((const char *, int));
330 extern int same_file
__P((const char *, const char *, struct stat
*, struct stat
*));
333 extern int sh_openpipe
__P((int *));
334 extern int sh_closepipe
__P((int *));
336 extern int file_exists
__P((const char *));
337 extern int file_isdir
__P((const char *));
338 extern int file_iswdir
__P((const char *));
339 extern int path_dot_or_dotdot
__P((const char *));
340 extern int absolute_pathname
__P((const char *));
341 extern int absolute_program
__P((const char *));
343 extern char *make_absolute
__P((const char *, const char *));
344 extern char *base_pathname
__P((char *));
345 extern char *full_pathname
__P((char *));
346 extern char *polite_directory_format
__P((char *));
347 extern char *trim_pathname
__P((char *, int));
348 extern char *printable_filename
__P((char *, int));
350 extern char *extract_colon_unit
__P((char *, int *));
352 extern void tilde_initialize
__P((void));
353 extern char *bash_tilde_find_word
__P((const char *, int, int *));
354 extern char *bash_tilde_expand
__P((const char *, int));
356 extern int group_member
__P((gid_t
));
357 extern char **get_group_list
__P((int *));
358 extern int *get_group_array
__P((int *));
360 extern char *conf_standard_path
__P((void));
361 extern int default_columns
__P((void));
363 #endif /* _GENERAL_H_ */