]> git.ipfire.org Git - thirdparty/bash.git/blame - general.h
fix for crash without arrays built in; allow some job status changes while the jobs...
[thirdparty/bash.git] / general.h
CommitLineData
726f6388
JA
1/* general.h -- defines that everybody likes to use. */
2
e1dd98a1 3/* Copyright (C) 1993-2024 Free Software Foundation, Inc.
726f6388
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
2e4498b3
CR
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.
11
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.
16
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"
f5635ecd 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
e1dd98a1 46#include "unlocked-io.h"
f73dda09
JA
47#include "xmalloc.h"
48
7117c2d2
JA
49/* Hardly used anymore */
50#define pointer_to_int(x) (int)((char *)x - (char *)0)
726f6388 51
7117c2d2 52#if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
81e3a4fb 53extern char *strcpy (char *, const char *);
ccc6cda3
JA
54#endif
55
726f6388 56#if !defined (savestring)
726f6388
JA
57# define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
58#endif
59
ccc6cda3 60#ifndef member
d0ca3503 61# define member(c, s) ((c) ? ((char *)mbschr ((s), (c)) != (char *)NULL) : 0)
ccc6cda3
JA
62#endif
63
726f6388
JA
64#ifndef whitespace
65#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
66#endif
67
f73dda09
JA
68#ifndef CHAR_MAX
69# ifdef __CHAR_UNSIGNED__
70# define CHAR_MAX 0xff
71# else
72# define CHAR_MAX 0x7f
73# endif
726f6388
JA
74#endif
75
f73dda09
JA
76#ifndef CHAR_BIT
77# define CHAR_BIT 8
726f6388
JA
78#endif
79
f73dda09
JA
80/* Nonzero if the integer type T is signed. */
81#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
82
2afeb2af
CR
83/* The width in bits of the integer type or expression T.
84 Padding bits are not supported; this is checked at compile-time below. */
85#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
86
87/* Bound on length of the string representing an unsigned integer
88 value representable in B bits. log10 (2.0) < 146/485. The
89 smallest value of B where this bound is not tight is 2621. */
90#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
91
f73dda09
JA
92/* Bound on length of the string representing an integer value of type T.
93 Subtract one for the sign bit if T is signed;
94 302 / 1000 is log10 (2) rounded up;
95 add one for integer division truncation;
96 add one more for a minus sign if t is signed. */
97#define INT_STRLEN_BOUND(t) \
2afeb2af 98 ((TYPE_WIDTH (t) - TYPE_SIGNED (t)) * 302 / 1000 \
f73dda09 99 + 1 + TYPE_SIGNED (t))
726f6388 100
a078e04c
CR
101/* Updated version adapted from gnulib/intprops.h, not used right now.
102 Changes the approximation of log10(2) from 302/1000 to 146/485. */
103#if 0
104#define INT_STRLEN_BOUND(t) \
105 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - TYPE_SIGNED (t)) + TYPE_SIGNED(t))
106#endif
107
2afeb2af
CR
108/* Bound on buffer size needed to represent an integer type or expression T,
109 including the terminating null. */
110#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
28ef6c31 111
ccc6cda3 112/* Define exactly what a legal shell identifier consists of. */
f73dda09
JA
113#define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
114#define legal_variable_char(c) (ISALNUM(c) || c == '_')
ccc6cda3 115
726f6388
JA
116/* Definitions used in subst.c and by the `read' builtin for field
117 splitting. */
118#define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
119
726f6388
JA
120/* All structs which contain a `next' field should have that field
121 as the first field in the struct. This means that functions
122 can be written to handle the general case for linked lists. */
123typedef struct g_list {
124 struct g_list *next;
125} GENERIC_LIST;
126
127/* Here is a generic structure for associating character strings
128 with integers. It is used in the parser for shell tokenization. */
129typedef struct {
130 char *word;
131 int token;
132} STRING_INT_ALIST;
133
34ec1876 134/* A macro to avoid making an unnecessary function call. */
726f6388 135#define REVERSE_LIST(list, type) \
7117c2d2 136 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
28ef6c31 137 : (type)(list))
726f6388
JA
138
139#if __GNUC__ > 1
3ee6b87d 140# define FASTCOPY(s, d, n) __builtin_memcpy ((d), (s), (n))
726f6388 141#else /* !__GNUC__ */
ccc6cda3
JA
142# if !defined (HAVE_BCOPY)
143# if !defined (HAVE_MEMMOVE)
3ee6b87d 144# define FASTCOPY(s, d, n) memcpy ((d), (s), (n))
726f6388 145# else
3ee6b87d 146# define FASTCOPY(s, d, n) memmove ((d), (s), (n))
ccc6cda3
JA
147# endif /* !HAVE_MEMMOVE */
148# else /* HAVE_BCOPY */
3ee6b87d 149# define FASTCOPY(s, d, n) bcopy ((s), (d), (n))
ccc6cda3 150# endif /* HAVE_BCOPY */
726f6388
JA
151#endif /* !__GNUC__ */
152
153/* String comparisons that possibly save a function call each. */
877ff726
CR
154static inline int
155STREQ(const char *a, const char *b)
156{
157 return ((a)[0] == (b)[0] && strcmp(a, b) == 0);
158}
159
160static inline int
161STREQN(const char *a, const char *b, size_t n)
162{
511fef0f
CR
163 return ((n == 0) ||
164 (n == 1 && a[0] == b[0]) ||
165 ((a)[0] == (b)[0] && strncmp(a, b, n) == 0));
877ff726 166}
726f6388
JA
167
168/* More convenience definitions that possibly save system or libc calls. */
169#define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
170#define FREE(s) do { if (s) free (s); } while (0)
cce855bc 171#define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
726f6388 172
ccc6cda3
JA
173/* A fairly hairy macro to check whether an allocated string has more room,
174 and to resize it using xrealloc if it does not.
175 STR is the string (char *)
176 CIND is the current index into the string (int)
177 ROOM is the amount of additional room we need in the string (int)
178 CSIZE is the currently-allocated size of STR (int)
179 SINCR is how much to increment CSIZE before calling xrealloc (int) */
180
181#define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
182 do { \
183 if ((cind) + (room) >= csize) \
184 { \
185 while ((cind) + (room) >= csize) \
186 csize += (sincr); \
187 str = xrealloc (str, csize); \
188 } \
189 } while (0)
726f6388 190
f73dda09
JA
191#ifndef SH_FUNCTION_TYPEDEF
192# define SH_FUNCTION_TYPEDEF
193
194/* Shell function typedefs with prototypes */
195/* `Generic' function pointer typedefs */
196
81e3a4fb
CR
197typedef int sh_intfunc_t (int);
198typedef int sh_ivoidfunc_t (void);
199typedef int sh_icpfunc_t (char *);
200typedef int sh_icppfunc_t (char **);
201typedef int sh_iptrfunc_t (PTR_T);
f73dda09 202
81e3a4fb
CR
203typedef void sh_voidfunc_t (void);
204typedef void sh_vintfunc_t (int);
205typedef void sh_vcpfunc_t (char *);
206typedef void sh_vcppfunc_t (char **);
207typedef void sh_vptrfunc_t (PTR_T);
f73dda09 208
81e3a4fb
CR
209typedef int sh_wdesc_func_t (WORD_DESC *);
210typedef int sh_wlist_func_t (WORD_LIST *);
f73dda09 211
81e3a4fb
CR
212typedef int sh_glist_func_t (GENERIC_LIST *);
213typedef int sh_gcp_func_t (GENERIC_LIST *, char *);
f73dda09 214
81e3a4fb 215typedef char *sh_string_func_t (char *); /* like savestring, et al. */
f73dda09 216
81e3a4fb
CR
217typedef int sh_msg_func_t (const char *, ...); /* printf(3)-like */
218typedef void sh_vmsg_func_t (const char *, ...); /* printf(3)-like */
f73dda09
JA
219
220/* Specific function pointer typedefs. Most of these could be done
221 with #defines. */
b2613ad1 222typedef void sh_sv_func_t (const char *);
81e3a4fb
CR
223typedef void sh_free_func_t (PTR_T); /* sh_vptrfunc_t */
224typedef void sh_resetsig_func_t (int); /* sh_vintfunc_t */
f73dda09 225
81e3a4fb 226typedef int sh_ignore_func_t (const char *); /* sh_icpfunc_t */
f73dda09 227
81e3a4fb 228typedef int sh_assign_func_t (const char *);
b2613ad1 229typedef int sh_wassign_func_t (const WORD_DESC *, int);
f73dda09 230
81e3a4fb
CR
231typedef int sh_load_func_t (char *);
232typedef void sh_unload_func_t (char *);
e9eee9d4 233
81e3a4fb 234typedef int sh_builtin_func_t (WORD_LIST *); /* sh_wlist_func_t */
f73dda09
JA
235
236#endif /* SH_FUNCTION_TYPEDEF */
237
64b2b7c0 238#define NOW getnow()
f7ec6b1a 239#define GETTIME(tv) gettimeofday(&(tv), NULL)
726f6388
JA
240
241/* Some defines for calling file status functions. */
242#define FS_EXISTS 0x1
243#define FS_EXECABLE 0x2
244#define FS_EXEC_PREFERRED 0x4
245#define FS_EXEC_ONLY 0x8
ccc6cda3
JA
246#define FS_DIRECTORY 0x10
247#define FS_NODIRS 0x20
cdb32d45 248#define FS_READABLE 0x40
726f6388 249
7117c2d2
JA
250/* Default maximum for move_to_high_fd */
251#define HIGH_FD_MAX 256
252
f73dda09 253/* The type of function passed as the fourth argument to qsort(3). */
f73dda09 254typedef int QSFUNC (const void *, const void *);
f73dda09 255
28ef6c31
JA
256/* Some useful definitions for Unix pathnames. Argument convention:
257 x == string, c == character */
258
259#if !defined (__CYGWIN__)
260# define ABSPATH(x) ((x)[0] == '/')
261# define RELPATH(x) ((x)[0] != '/')
262#else /* __CYGWIN__ */
f5635ecd
CR
263# define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
264# define RELPATH(x) (ABSPATH(x) == 0)
28ef6c31
JA
265#endif /* __CYGWIN__ */
266
267#define ROOTEDPATH(x) (ABSPATH(x))
268
269#define DIRSEP '/'
f5635ecd
CR
270#if !defined (__CYGWIN__)
271# define ISDIRSEP(c) ((c) == '/')
272#else
273# define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
274#endif /* __CYGWIN__ */
28ef6c31
JA
275#define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
276
653d7134 277#define DOT_OR_DOTDOT(s) (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
da43077c 278
653d7134
CR
279#if defined (HANDLE_MULTIBYTE)
280#define WDOT_OR_DOTDOT(w) (w[0] == L'.' && (w[1] == L'\0' || (w[1] == L'.' && w[2] == L'\0')))
281#endif
282
f73dda09 283#if 0
ccc6cda3 284/* Declarations for functions defined in xmalloc.c */
81e3a4fb
CR
285extern PTR_T xmalloc (size_t);
286extern PTR_T xrealloc (void *, size_t);
287extern void xfree (void *);
f73dda09 288#endif
726f6388 289
ccc6cda3 290/* Declarations for functions defined in general.c */
81e3a4fb 291extern void posix_initialize (int);
726f6388 292
8fd8cd8f 293extern size_t num_posix_options (void);
81e3a4fb
CR
294extern char *get_posix_options (char *);
295extern void set_posix_options (const char *);
9d80be9a 296
81e3a4fb 297extern void save_posix_options (void);
091c6bc4 298
ccc6cda3 299#if defined (RLIMTYPE)
54f3ed22 300extern RLIMTYPE string_to_rlimtype (const char *, char **);
81e3a4fb 301extern void print_rlimtype (RLIMTYPE, int);
ccc6cda3 302#endif
726f6388 303
81e3a4fb 304extern int all_digits (const char *);
b3038907
CR
305extern int valid_number (const char *, intmax_t *);
306extern int valid_identifier (const char *);
81e3a4fb
CR
307extern int importable_function_name (const char *, size_t);
308extern int exportable_function_name (const char *);
309extern int check_identifier (WORD_DESC *, int);
310extern int valid_nameref_value (const char *, int);
311extern int check_selfref (const char *, char *, int);
b3038907 312extern int valid_alias_name (const char *, int);
de195f94 313extern int valid_function_name (const char *, int);
315095ad 314extern int valid_function_word (WORD_DESC *, int);
81e3a4fb
CR
315extern int line_isblank (const char *);
316extern int assignment (const char *, int);
317
318extern int sh_unset_nodelay_mode (int);
319extern int sh_setclexec (int);
320extern int sh_validfd (int);
321extern int fd_ispipe (int);
322extern void check_dev_tty (void);
323extern int move_to_high_fd (int, int, int);
324extern int check_binary_file (const char *, int);
f73dda09
JA
325
326#ifdef _POSIXSTAT_H_
81e3a4fb 327extern int same_file (const char *, const char *, struct stat *, struct stat *);
f73dda09 328#endif
ccc6cda3 329
81e3a4fb
CR
330extern int sh_openpipe (int *);
331extern int sh_closepipe (int *);
4ac1ff98 332
81e3a4fb
CR
333extern int file_exists (const char *);
334extern int file_isdir (const char *);
335extern int file_iswdir (const char *);
336extern int path_dot_or_dotdot (const char *);
337extern int absolute_pathname (const char *);
338extern int absolute_program (const char *);
ff247e74 339
81e3a4fb
CR
340extern char *make_absolute (const char *, const char *);
341extern char *base_pathname (char *);
342extern char *full_pathname (char *);
343extern char *polite_directory_format (char *);
344extern char *trim_pathname (char *, int);
345extern char *printable_filename (char *, int);
726f6388 346
81e3a4fb 347extern char *extract_colon_unit (char *, int *);
726f6388 348
81e3a4fb 349extern void tilde_initialize (void);
1cc5a8f7 350extern char *bash_tilde_find_word (const char *, int, size_t *);
81e3a4fb 351extern char *bash_tilde_expand (const char *, int);
726f6388 352
81e3a4fb
CR
353extern int group_member (gid_t);
354extern char **get_group_list (int *);
355extern int *get_group_array (int *);
d166f048 356
81e3a4fb
CR
357extern char *conf_standard_path (void);
358extern int default_columns (void);
1573ba78 359
ccc6cda3 360#endif /* _GENERAL_H_ */