]> git.ipfire.org Git - thirdparty/bash.git/blame - general.h
fix for SIGINT in sourced script
[thirdparty/bash.git] / general.h
CommitLineData
726f6388
JA
1/* general.h -- defines that everybody likes to use. */
2
a0c0a00f 3/* Copyright (C) 1993-2016 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)
f73dda09 65extern char *strcpy __P((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
95/* Bound on length of the string representing an integer value of type T.
96 Subtract one for the sign bit if T is signed;
97 302 / 1000 is log10 (2) rounded up;
98 add one for integer division truncation;
99 add one more for a minus sign if t is signed. */
100#define INT_STRLEN_BOUND(t) \
101 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
102 + 1 + TYPE_SIGNED (t))
726f6388 103
28ef6c31 104
ccc6cda3 105/* Define exactly what a legal shell identifier consists of. */
f73dda09
JA
106#define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
107#define legal_variable_char(c) (ISALNUM(c) || c == '_')
ccc6cda3 108
726f6388
JA
109/* Definitions used in subst.c and by the `read' builtin for field
110 splitting. */
111#define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
112
726f6388
JA
113/* All structs which contain a `next' field should have that field
114 as the first field in the struct. This means that functions
115 can be written to handle the general case for linked lists. */
116typedef struct g_list {
117 struct g_list *next;
118} GENERIC_LIST;
119
120/* Here is a generic structure for associating character strings
121 with integers. It is used in the parser for shell tokenization. */
122typedef struct {
123 char *word;
124 int token;
125} STRING_INT_ALIST;
126
a0c0a00f 127/* A macro to avoid making an unnecessary function call. */
726f6388 128#define REVERSE_LIST(list, type) \
7117c2d2 129 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
28ef6c31 130 : (type)(list))
726f6388
JA
131
132#if __GNUC__ > 1
95732b49 133# define FASTCOPY(s, d, n) __builtin_memcpy ((d), (s), (n))
726f6388 134#else /* !__GNUC__ */
ccc6cda3
JA
135# if !defined (HAVE_BCOPY)
136# if !defined (HAVE_MEMMOVE)
95732b49 137# define FASTCOPY(s, d, n) memcpy ((d), (s), (n))
726f6388 138# else
95732b49 139# define FASTCOPY(s, d, n) memmove ((d), (s), (n))
ccc6cda3
JA
140# endif /* !HAVE_MEMMOVE */
141# else /* HAVE_BCOPY */
95732b49 142# define FASTCOPY(s, d, n) bcopy ((s), (d), (n))
ccc6cda3 143# endif /* HAVE_BCOPY */
726f6388
JA
144#endif /* !__GNUC__ */
145
146/* String comparisons that possibly save a function call each. */
147#define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
bb70624e
JA
148#define STREQN(a, b, n) ((n == 0) ? (1) \
149 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
726f6388
JA
150
151/* More convenience definitions that possibly save system or libc calls. */
152#define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
153#define FREE(s) do { if (s) free (s); } while (0)
cce855bc 154#define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
726f6388 155
ccc6cda3
JA
156/* A fairly hairy macro to check whether an allocated string has more room,
157 and to resize it using xrealloc if it does not.
158 STR is the string (char *)
159 CIND is the current index into the string (int)
160 ROOM is the amount of additional room we need in the string (int)
161 CSIZE is the currently-allocated size of STR (int)
162 SINCR is how much to increment CSIZE before calling xrealloc (int) */
163
164#define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
165 do { \
166 if ((cind) + (room) >= csize) \
167 { \
168 while ((cind) + (room) >= csize) \
169 csize += (sincr); \
170 str = xrealloc (str, csize); \
171 } \
172 } while (0)
726f6388
JA
173
174/* Function pointers can be declared as (Function *)foo. */
ccc6cda3
JA
175#if !defined (_FUNCTION_DEF)
176# define _FUNCTION_DEF
726f6388
JA
177typedef int Function ();
178typedef void VFunction ();
f73dda09
JA
179typedef char *CPFunction (); /* no longer used */
180typedef char **CPPFunction (); /* no longer used */
726f6388
JA
181#endif /* _FUNCTION_DEF */
182
f73dda09
JA
183#ifndef SH_FUNCTION_TYPEDEF
184# define SH_FUNCTION_TYPEDEF
185
186/* Shell function typedefs with prototypes */
187/* `Generic' function pointer typedefs */
188
189typedef int sh_intfunc_t __P((int));
190typedef int sh_ivoidfunc_t __P((void));
191typedef int sh_icpfunc_t __P((char *));
192typedef int sh_icppfunc_t __P((char **));
193typedef int sh_iptrfunc_t __P((PTR_T));
194
195typedef void sh_voidfunc_t __P((void));
196typedef void sh_vintfunc_t __P((int));
197typedef void sh_vcpfunc_t __P((char *));
198typedef void sh_vcppfunc_t __P((char **));
199typedef void sh_vptrfunc_t __P((PTR_T));
200
201typedef int sh_wdesc_func_t __P((WORD_DESC *));
202typedef int sh_wlist_func_t __P((WORD_LIST *));
203
204typedef int sh_glist_func_t __P((GENERIC_LIST *));
205
206typedef char *sh_string_func_t __P((char *)); /* like savestring, et al. */
207
208typedef int sh_msg_func_t __P((const char *, ...)); /* printf(3)-like */
209typedef void sh_vmsg_func_t __P((const char *, ...)); /* printf(3)-like */
210
211/* Specific function pointer typedefs. Most of these could be done
212 with #defines. */
213typedef void sh_sv_func_t __P((char *)); /* sh_vcpfunc_t */
214typedef void sh_free_func_t __P((PTR_T)); /* sh_vptrfunc_t */
215typedef void sh_resetsig_func_t __P((int)); /* sh_vintfunc_t */
216
217typedef int sh_ignore_func_t __P((const char *)); /* sh_icpfunc_t */
218
495aee44
CR
219typedef int sh_assign_func_t __P((const char *));
220typedef int sh_wassign_func_t __P((WORD_DESC *, int));
f73dda09 221
a0c0a00f
CR
222typedef int sh_load_func_t __P((char *));
223typedef void sh_unload_func_t __P((char *));
224
f73dda09
JA
225typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
226
227#endif /* SH_FUNCTION_TYPEDEF */
228
726f6388
JA
229#define NOW ((time_t) time ((time_t *) 0))
230
231/* Some defines for calling file status functions. */
232#define FS_EXISTS 0x1
233#define FS_EXECABLE 0x2
234#define FS_EXEC_PREFERRED 0x4
235#define FS_EXEC_ONLY 0x8
ccc6cda3
JA
236#define FS_DIRECTORY 0x10
237#define FS_NODIRS 0x20
95732b49 238#define FS_READABLE 0x40
726f6388 239
7117c2d2
JA
240/* Default maximum for move_to_high_fd */
241#define HIGH_FD_MAX 256
242
f73dda09
JA
243/* The type of function passed as the fourth argument to qsort(3). */
244#ifdef __STDC__
245typedef int QSFUNC (const void *, const void *);
246#else
247typedef int QSFUNC ();
248#endif
249
28ef6c31
JA
250/* Some useful definitions for Unix pathnames. Argument convention:
251 x == string, c == character */
252
253#if !defined (__CYGWIN__)
254# define ABSPATH(x) ((x)[0] == '/')
255# define RELPATH(x) ((x)[0] != '/')
256#else /* __CYGWIN__ */
95732b49
JA
257# define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
258# define RELPATH(x) (ABSPATH(x) == 0)
28ef6c31
JA
259#endif /* __CYGWIN__ */
260
261#define ROOTEDPATH(x) (ABSPATH(x))
262
263#define DIRSEP '/'
95732b49
JA
264#if !defined (__CYGWIN__)
265# define ISDIRSEP(c) ((c) == '/')
266#else
267# define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
268#endif /* __CYGWIN__ */
28ef6c31
JA
269#define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
270
f73dda09 271#if 0
ccc6cda3 272/* Declarations for functions defined in xmalloc.c */
f73dda09
JA
273extern PTR_T xmalloc __P((size_t));
274extern PTR_T xrealloc __P((void *, size_t));
bb70624e 275extern void xfree __P((void *));
f73dda09 276#endif
726f6388 277
ccc6cda3
JA
278/* Declarations for functions defined in general.c */
279extern void posix_initialize __P((int));
726f6388 280
ccc6cda3
JA
281#if defined (RLIMTYPE)
282extern RLIMTYPE string_to_rlimtype __P((char *));
283extern void print_rlimtype __P((RLIMTYPE, int));
284#endif
726f6388 285
a0c0a00f 286extern int all_digits __P((const char *));
3185942a 287extern int legal_number __P((const char *, intmax_t *));
a0c0a00f
CR
288extern int legal_identifier __P((const char *));
289extern int importable_function_name __P((const char *, size_t));
290extern int exportable_function_name __P((const char *));
726f6388 291extern int check_identifier __P((WORD_DESC *, int));
a0c0a00f
CR
292extern int valid_nameref_value __P((const char *, int));
293extern int check_selfref __P((const char *, char *, int));
294extern int legal_alias_name __P((const char *, int));
b80f6443 295extern int assignment __P((const char *, int));
ccc6cda3 296
28ef6c31 297extern int sh_unset_nodelay_mode __P((int));
7117c2d2 298extern int sh_validfd __P((int));
495aee44 299extern int fd_ispipe __P((int));
ccc6cda3 300extern void check_dev_tty __P((void));
d166f048 301extern int move_to_high_fd __P((int, int, int));
a0c0a00f 302extern int check_binary_file __P((const char *, int));
f73dda09
JA
303
304#ifdef _POSIXSTAT_H_
a0c0a00f 305extern int same_file __P((const char *, const char *, struct stat *, struct stat *));
f73dda09 306#endif
ccc6cda3 307
3185942a
JA
308extern int sh_openpipe __P((int *));
309extern int sh_closepipe __P((int *));
310
a0c0a00f
CR
311extern int file_exists __P((const char *));
312extern int file_isdir __P((const char *));
313extern int file_iswdir __P((const char *));
ac50fbac 314extern int path_dot_or_dotdot __P((const char *));
f73dda09
JA
315extern int absolute_pathname __P((const char *));
316extern int absolute_program __P((const char *));
95732b49 317
a0c0a00f 318extern char *make_absolute __P((const char *, const char *));
726f6388
JA
319extern char *base_pathname __P((char *));
320extern char *full_pathname __P((char *));
726f6388 321extern char *polite_directory_format __P((char *));
3185942a 322extern char *trim_pathname __P((char *, int));
a0c0a00f 323extern char *printable_filename __P((char *, int));
726f6388 324
ccc6cda3 325extern char *extract_colon_unit __P((char *, int *));
726f6388 326
ccc6cda3 327extern void tilde_initialize __P((void));
95732b49 328extern char *bash_tilde_find_word __P((const char *, int, int *));
7117c2d2 329extern char *bash_tilde_expand __P((const char *, int));
726f6388 330
d166f048 331extern int group_member __P((gid_t));
d166f048 332extern char **get_group_list __P((int *));
b72432fd 333extern int *get_group_array __P((int *));
d166f048 334
a0c0a00f
CR
335extern char *conf_standard_path __P((void));
336
ccc6cda3 337#endif /* _GENERAL_H_ */