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