]> git.ipfire.org Git - thirdparty/bash.git/blame - general.h
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / general.h
CommitLineData
726f6388
JA
1/* general.h -- defines that everybody likes to use. */
2
74091dd4 3/* Copyright (C) 1993-2021 Free Software Foundation, Inc.
726f6388
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
3185942a
JA
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.
726f6388 11
3185942a
JA
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.
726f6388 16
3185942a
JA
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/>.
19*/
726f6388 20
ccc6cda3
JA
21#if !defined (_GENERAL_H_)
22#define _GENERAL_H_
726f6388
JA
23
24#include "stdc.h"
25
d166f048 26#include "bashtypes.h"
95732b49 27#include "chartypes.h"
d166f048 28
cce855bc
JA
29#if defined (HAVE_SYS_RESOURCE_H) && defined (RLIMTYPE)
30# if defined (HAVE_SYS_TIME_H)
31# include <sys/time.h>
32# endif
33# include <sys/resource.h>
34#endif
35
d166f048
JA
36#if defined (HAVE_STRING_H)
37# include <string.h>
38#else
39# include <strings.h>
40#endif /* !HAVE_STRING_H */
41
f73dda09
JA
42#if defined (HAVE_LIMITS_H)
43# include <limits.h>
726f6388
JA
44#endif
45
f73dda09
JA
46#include "xmalloc.h"
47
ccc6cda3 48/* NULL pointer type. */
726f6388
JA
49#if !defined (NULL)
50# if defined (__STDC__)
51# define NULL ((void *) 0)
52# else
53# define NULL 0x0
54# endif /* !__STDC__ */
55#endif /* !NULL */
56
7117c2d2
JA
57/* Hardly used anymore */
58#define pointer_to_int(x) (int)((char *)x - (char *)0)
726f6388 59
ccc6cda3
JA
60#if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
61extern char *strchr (), *strrchr ();
62#endif
63
7117c2d2 64#if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
8868edaf 65extern char *strcpy PARAMS((char *, const char *));
ccc6cda3
JA
66#endif
67
726f6388 68#if !defined (savestring)
726f6388
JA
69# define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
70#endif
71
ccc6cda3 72#ifndef member
0001803f 73# define member(c, s) ((c) ? ((char *)mbschr ((s), (c)) != (char *)NULL) : 0)
ccc6cda3
JA
74#endif
75
726f6388
JA
76#ifndef whitespace
77#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
78#endif
79
f73dda09
JA
80#ifndef CHAR_MAX
81# ifdef __CHAR_UNSIGNED__
82# define CHAR_MAX 0xff
83# else
84# define CHAR_MAX 0x7f
85# endif
726f6388
JA
86#endif
87
f73dda09
JA
88#ifndef CHAR_BIT
89# define CHAR_BIT 8
726f6388
JA
90#endif
91
f73dda09
JA
92/* Nonzero if the integer type T is signed. */
93#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
94
d233b485
CR
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)
98
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)
103
f73dda09
JA
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) \
d233b485 110 ((TYPE_WIDTH (t) - TYPE_SIGNED (t)) * 302 / 1000 \
f73dda09 111 + 1 + TYPE_SIGNED (t))
726f6388 112
d233b485
CR
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. */
115#if 0
116#define INT_STRLEN_BOUND(t) \
117 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - TYPE_SIGNED (t)) + TYPE_SIGNED(t))
118#endif
119
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)
28ef6c31 123
ccc6cda3 124/* Define exactly what a legal shell identifier consists of. */
f73dda09
JA
125#define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
126#define legal_variable_char(c) (ISALNUM(c) || c == '_')
ccc6cda3 127
726f6388
JA
128/* Definitions used in subst.c and by the `read' builtin for field
129 splitting. */
130#define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
131
726f6388
JA
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. */
135typedef struct g_list {
136 struct g_list *next;
137} GENERIC_LIST;
138
139/* Here is a generic structure for associating character strings
140 with integers. It is used in the parser for shell tokenization. */
141typedef struct {
142 char *word;
143 int token;
144} STRING_INT_ALIST;
145
a0c0a00f 146/* A macro to avoid making an unnecessary function call. */
726f6388 147#define REVERSE_LIST(list, type) \
7117c2d2 148 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
28ef6c31 149 : (type)(list))
726f6388
JA
150
151#if __GNUC__ > 1
95732b49 152# define FASTCOPY(s, d, n) __builtin_memcpy ((d), (s), (n))
726f6388 153#else /* !__GNUC__ */
ccc6cda3
JA
154# if !defined (HAVE_BCOPY)
155# if !defined (HAVE_MEMMOVE)
95732b49 156# define FASTCOPY(s, d, n) memcpy ((d), (s), (n))
726f6388 157# else
95732b49 158# define FASTCOPY(s, d, n) memmove ((d), (s), (n))
ccc6cda3
JA
159# endif /* !HAVE_MEMMOVE */
160# else /* HAVE_BCOPY */
95732b49 161# define FASTCOPY(s, d, n) bcopy ((s), (d), (n))
ccc6cda3 162# endif /* HAVE_BCOPY */
726f6388
JA
163#endif /* !__GNUC__ */
164
165/* String comparisons that possibly save a function call each. */
166#define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
bb70624e
JA
167#define STREQN(a, b, n) ((n == 0) ? (1) \
168 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
726f6388
JA
169
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)
cce855bc 173#define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
726f6388 174
ccc6cda3
JA
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) */
182
183#define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
184 do { \
185 if ((cind) + (room) >= csize) \
186 { \
187 while ((cind) + (room) >= csize) \
188 csize += (sincr); \
189 str = xrealloc (str, csize); \
190 } \
191 } while (0)
726f6388
JA
192
193/* Function pointers can be declared as (Function *)foo. */
ccc6cda3
JA
194#if !defined (_FUNCTION_DEF)
195# define _FUNCTION_DEF
726f6388
JA
196typedef int Function ();
197typedef void VFunction ();
f73dda09
JA
198typedef char *CPFunction (); /* no longer used */
199typedef char **CPPFunction (); /* no longer used */
726f6388
JA
200#endif /* _FUNCTION_DEF */
201
f73dda09
JA
202#ifndef SH_FUNCTION_TYPEDEF
203# define SH_FUNCTION_TYPEDEF
204
205/* Shell function typedefs with prototypes */
206/* `Generic' function pointer typedefs */
207
8868edaf
CR
208typedef int sh_intfunc_t PARAMS((int));
209typedef int sh_ivoidfunc_t PARAMS((void));
210typedef int sh_icpfunc_t PARAMS((char *));
211typedef int sh_icppfunc_t PARAMS((char **));
212typedef int sh_iptrfunc_t PARAMS((PTR_T));
f73dda09 213
8868edaf
CR
214typedef void sh_voidfunc_t PARAMS((void));
215typedef void sh_vintfunc_t PARAMS((int));
216typedef void sh_vcpfunc_t PARAMS((char *));
217typedef void sh_vcppfunc_t PARAMS((char **));
218typedef void sh_vptrfunc_t PARAMS((PTR_T));
f73dda09 219
8868edaf
CR
220typedef int sh_wdesc_func_t PARAMS((WORD_DESC *));
221typedef int sh_wlist_func_t PARAMS((WORD_LIST *));
f73dda09 222
8868edaf 223typedef int sh_glist_func_t PARAMS((GENERIC_LIST *));
f73dda09 224
8868edaf 225typedef char *sh_string_func_t PARAMS((char *)); /* like savestring, et al. */
f73dda09 226
8868edaf
CR
227typedef int sh_msg_func_t PARAMS((const char *, ...)); /* printf(3)-like */
228typedef void sh_vmsg_func_t PARAMS((const char *, ...)); /* printf(3)-like */
f73dda09
JA
229
230/* Specific function pointer typedefs. Most of these could be done
231 with #defines. */
8868edaf
CR
232typedef void sh_sv_func_t PARAMS((char *)); /* sh_vcpfunc_t */
233typedef void sh_free_func_t PARAMS((PTR_T)); /* sh_vptrfunc_t */
234typedef void sh_resetsig_func_t PARAMS((int)); /* sh_vintfunc_t */
f73dda09 235
8868edaf 236typedef int sh_ignore_func_t PARAMS((const char *)); /* sh_icpfunc_t */
f73dda09 237
8868edaf
CR
238typedef int sh_assign_func_t PARAMS((const char *));
239typedef int sh_wassign_func_t PARAMS((WORD_DESC *, int));
f73dda09 240
8868edaf
CR
241typedef int sh_load_func_t PARAMS((char *));
242typedef void sh_unload_func_t PARAMS((char *));
a0c0a00f 243
8868edaf 244typedef int sh_builtin_func_t PARAMS((WORD_LIST *)); /* sh_wlist_func_t */
f73dda09
JA
245
246#endif /* SH_FUNCTION_TYPEDEF */
247
726f6388 248#define NOW ((time_t) time ((time_t *) 0))
8868edaf 249#define GETTIME(tv) gettimeofday(&(tv), NULL)
726f6388
JA
250
251/* Some defines for calling file status functions. */
252#define FS_EXISTS 0x1
253#define FS_EXECABLE 0x2
254#define FS_EXEC_PREFERRED 0x4
255#define FS_EXEC_ONLY 0x8
ccc6cda3
JA
256#define FS_DIRECTORY 0x10
257#define FS_NODIRS 0x20
95732b49 258#define FS_READABLE 0x40
726f6388 259
7117c2d2
JA
260/* Default maximum for move_to_high_fd */
261#define HIGH_FD_MAX 256
262
f73dda09
JA
263/* The type of function passed as the fourth argument to qsort(3). */
264#ifdef __STDC__
265typedef int QSFUNC (const void *, const void *);
266#else
267typedef int QSFUNC ();
268#endif
269
28ef6c31
JA
270/* Some useful definitions for Unix pathnames. Argument convention:
271 x == string, c == character */
272
273#if !defined (__CYGWIN__)
274# define ABSPATH(x) ((x)[0] == '/')
275# define RELPATH(x) ((x)[0] != '/')
276#else /* __CYGWIN__ */
95732b49
JA
277# define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
278# define RELPATH(x) (ABSPATH(x) == 0)
28ef6c31
JA
279#endif /* __CYGWIN__ */
280
281#define ROOTEDPATH(x) (ABSPATH(x))
282
283#define DIRSEP '/'
95732b49
JA
284#if !defined (__CYGWIN__)
285# define ISDIRSEP(c) ((c) == '/')
286#else
287# define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
288#endif /* __CYGWIN__ */
28ef6c31
JA
289#define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
290
8868edaf 291#define DOT_OR_DOTDOT(s) (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
74091dd4 292
8868edaf
CR
293#if defined (HANDLE_MULTIBYTE)
294#define WDOT_OR_DOTDOT(w) (w[0] == L'.' && (w[1] == L'\0' || (w[1] == L'.' && w[2] == L'\0')))
295#endif
296
f73dda09 297#if 0
ccc6cda3 298/* Declarations for functions defined in xmalloc.c */
8868edaf
CR
299extern PTR_T xmalloc PARAMS((size_t));
300extern PTR_T xrealloc PARAMS((void *, size_t));
301extern void xfree PARAMS((void *));
f73dda09 302#endif
726f6388 303
ccc6cda3 304/* Declarations for functions defined in general.c */
8868edaf
CR
305extern void posix_initialize PARAMS((int));
306
307extern int num_posix_options PARAMS((void));
308extern char *get_posix_options PARAMS((char *));
309extern void set_posix_options PARAMS((const char *));
726f6388 310
8868edaf 311extern void save_posix_options PARAMS((void));
d233b485 312
ccc6cda3 313#if defined (RLIMTYPE)
8868edaf
CR
314extern RLIMTYPE string_to_rlimtype PARAMS((char *));
315extern void print_rlimtype PARAMS((RLIMTYPE, int));
ccc6cda3 316#endif
726f6388 317
8868edaf
CR
318extern int all_digits PARAMS((const char *));
319extern int legal_number PARAMS((const char *, intmax_t *));
320extern int legal_identifier PARAMS((const char *));
321extern int importable_function_name PARAMS((const char *, size_t));
322extern int exportable_function_name PARAMS((const char *));
323extern int check_identifier PARAMS((WORD_DESC *, int));
324extern int valid_nameref_value PARAMS((const char *, int));
325extern int check_selfref PARAMS((const char *, char *, int));
326extern int legal_alias_name PARAMS((const char *, int));
327extern int line_isblank PARAMS((const char *));
328extern int assignment PARAMS((const char *, int));
329
330extern int sh_unset_nodelay_mode PARAMS((int));
331extern int sh_setclexec PARAMS((int));
332extern int sh_validfd PARAMS((int));
333extern int fd_ispipe PARAMS((int));
334extern void check_dev_tty PARAMS((void));
335extern int move_to_high_fd PARAMS((int, int, int));
336extern int check_binary_file PARAMS((const char *, int));
f73dda09
JA
337
338#ifdef _POSIXSTAT_H_
8868edaf 339extern int same_file PARAMS((const char *, const char *, struct stat *, struct stat *));
f73dda09 340#endif
ccc6cda3 341
8868edaf
CR
342extern int sh_openpipe PARAMS((int *));
343extern int sh_closepipe PARAMS((int *));
3185942a 344
8868edaf
CR
345extern int file_exists PARAMS((const char *));
346extern int file_isdir PARAMS((const char *));
347extern int file_iswdir PARAMS((const char *));
348extern int path_dot_or_dotdot PARAMS((const char *));
349extern int absolute_pathname PARAMS((const char *));
350extern int absolute_program PARAMS((const char *));
95732b49 351
8868edaf
CR
352extern char *make_absolute PARAMS((const char *, const char *));
353extern char *base_pathname PARAMS((char *));
354extern char *full_pathname PARAMS((char *));
355extern char *polite_directory_format PARAMS((char *));
356extern char *trim_pathname PARAMS((char *, int));
357extern char *printable_filename PARAMS((char *, int));
726f6388 358
8868edaf 359extern char *extract_colon_unit PARAMS((char *, int *));
726f6388 360
8868edaf
CR
361extern void tilde_initialize PARAMS((void));
362extern char *bash_tilde_find_word PARAMS((const char *, int, int *));
363extern char *bash_tilde_expand PARAMS((const char *, int));
726f6388 364
8868edaf
CR
365extern int group_member PARAMS((gid_t));
366extern char **get_group_list PARAMS((int *));
367extern int *get_group_array PARAMS((int *));
d166f048 368
8868edaf
CR
369extern char *conf_standard_path PARAMS((void));
370extern int default_columns PARAMS((void));
a0c0a00f 371
ccc6cda3 372#endif /* _GENERAL_H_ */