1 /* general.h -- defines that everybody likes to use. */
3 /* Copyright (C) 1993-2004 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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 #if !defined (_GENERAL_H_)
26 #include "bashtypes.h"
28 #if defined (HAVE_SYS_RESOURCE_H) && defined (RLIMTYPE)
29 # if defined (HAVE_SYS_TIME_H)
30 # include <sys/time.h>
32 # include <sys/resource.h>
35 #if defined (HAVE_STRING_H)
39 #endif /* !HAVE_STRING_H */
41 #if defined (HAVE_LIMITS_H)
47 /* NULL pointer type. */
49 # if defined (__STDC__)
50 # define NULL ((void *) 0)
53 # endif /* !__STDC__ */
56 /* Hardly used anymore */
57 #define pointer_to_int(x) (int)((char *)x - (char *)0)
59 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
60 extern char *strchr (), *strrchr ();
63 #if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
64 extern char *strcpy __P((char *, const char *));
67 #if !defined (savestring)
68 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
72 # define member(c, s) ((c) ? ((char *)xstrchr ((s), (c)) != (char *)NULL) : 0)
76 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
80 # ifdef __CHAR_UNSIGNED__
81 # define CHAR_MAX 0xff
83 # define CHAR_MAX 0x7f
91 /* Nonzero if the integer type T is signed. */
92 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
94 /* Bound on length of the string representing an integer value of type T.
95 Subtract one for the sign bit if T is signed;
96 302 / 1000 is log10 (2) rounded up;
97 add one for integer division truncation;
98 add one more for a minus sign if t is signed. */
99 #define INT_STRLEN_BOUND(t) \
100 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
101 + 1 + TYPE_SIGNED (t))
104 /* Define exactly what a legal shell identifier consists of. */
105 #define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
106 #define legal_variable_char(c) (ISALNUM(c) || c == '_')
108 /* Definitions used in subst.c and by the `read' builtin for field
110 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
112 /* All structs which contain a `next' field should have that field
113 as the first field in the struct. This means that functions
114 can be written to handle the general case for linked lists. */
115 typedef struct g_list {
119 /* Here is a generic structure for associating character strings
120 with integers. It is used in the parser for shell tokenization. */
126 /* A macro to avoid making an uneccessary function call. */
127 #define REVERSE_LIST(list, type) \
128 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
132 # define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
133 #else /* !__GNUC__ */
134 # if !defined (HAVE_BCOPY)
135 # if !defined (HAVE_MEMMOVE)
136 # define FASTCOPY(s, d, n) memcpy (d, s, n)
138 # define FASTCOPY(s, d, n) memmove (d, s, n)
139 # endif /* !HAVE_MEMMOVE */
140 # else /* HAVE_BCOPY */
141 # define FASTCOPY(s, d, n) bcopy (s, d, n)
142 # endif /* HAVE_BCOPY */
143 #endif /* !__GNUC__ */
145 /* String comparisons that possibly save a function call each. */
146 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
147 #define STREQN(a, b, n) ((n == 0) ? (1) \
148 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
150 /* More convenience definitions that possibly save system or libc calls. */
151 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
152 #define FREE(s) do { if (s) free (s); } while (0)
153 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
155 /* A fairly hairy macro to check whether an allocated string has more room,
156 and to resize it using xrealloc if it does not.
157 STR is the string (char *)
158 CIND is the current index into the string (int)
159 ROOM is the amount of additional room we need in the string (int)
160 CSIZE is the currently-allocated size of STR (int)
161 SINCR is how much to increment CSIZE before calling xrealloc (int) */
163 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
165 if ((cind) + (room) >= csize) \
167 while ((cind) + (room) >= csize) \
169 str = xrealloc (str, csize); \
173 /* Function pointers can be declared as (Function *)foo. */
174 #if !defined (_FUNCTION_DEF)
175 # define _FUNCTION_DEF
176 typedef int Function ();
177 typedef void VFunction ();
178 typedef char *CPFunction (); /* no longer used */
179 typedef char **CPPFunction (); /* no longer used */
180 #endif /* _FUNCTION_DEF */
182 #ifndef SH_FUNCTION_TYPEDEF
183 # define SH_FUNCTION_TYPEDEF
185 /* Shell function typedefs with prototypes */
186 /* `Generic' function pointer typedefs */
188 typedef int sh_intfunc_t __P((int));
189 typedef int sh_ivoidfunc_t __P((void));
190 typedef int sh_icpfunc_t __P((char *));
191 typedef int sh_icppfunc_t __P((char **));
192 typedef int sh_iptrfunc_t __P((PTR_T));
194 typedef void sh_voidfunc_t __P((void));
195 typedef void sh_vintfunc_t __P((int));
196 typedef void sh_vcpfunc_t __P((char *));
197 typedef void sh_vcppfunc_t __P((char **));
198 typedef void sh_vptrfunc_t __P((PTR_T));
200 typedef int sh_wdesc_func_t __P((WORD_DESC *));
201 typedef int sh_wlist_func_t __P((WORD_LIST *));
203 typedef int sh_glist_func_t __P((GENERIC_LIST *));
205 typedef char *sh_string_func_t __P((char *)); /* like savestring, et al. */
207 typedef int sh_msg_func_t __P((const char *, ...)); /* printf(3)-like */
208 typedef void sh_vmsg_func_t __P((const char *, ...)); /* printf(3)-like */
210 /* Specific function pointer typedefs. Most of these could be done
212 typedef void sh_sv_func_t __P((char *)); /* sh_vcpfunc_t */
213 typedef void sh_free_func_t __P((PTR_T)); /* sh_vptrfunc_t */
214 typedef void sh_resetsig_func_t __P((int)); /* sh_vintfunc_t */
216 typedef int sh_ignore_func_t __P((const char *)); /* sh_icpfunc_t */
218 typedef int sh_assign_func_t __P((const char *)); /* sh_icpfunc_t */
220 typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
222 #endif /* SH_FUNCTION_TYPEDEF */
224 #define NOW ((time_t) time ((time_t *) 0))
226 /* Some defines for calling file status functions. */
227 #define FS_EXISTS 0x1
228 #define FS_EXECABLE 0x2
229 #define FS_EXEC_PREFERRED 0x4
230 #define FS_EXEC_ONLY 0x8
231 #define FS_DIRECTORY 0x10
232 #define FS_NODIRS 0x20
234 /* Default maximum for move_to_high_fd */
235 #define HIGH_FD_MAX 256
237 /* The type of function passed as the fourth argument to qsort(3). */
239 typedef int QSFUNC (const void *, const void *);
241 typedef int QSFUNC ();
244 /* Some useful definitions for Unix pathnames. Argument convention:
245 x == string, c == character */
247 #if !defined (__CYGWIN__)
248 # define ABSPATH(x) ((x)[0] == '/')
249 # define RELPATH(x) ((x)[0] != '/')
250 #else /* __CYGWIN__ */
251 # define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':' && (x)[2] == '/') || (x)[0] == '/')
252 # define RELPATH(x) (!(x)[0] || ((x)[1] != ':' && (x)[0] != '/'))
253 #endif /* __CYGWIN__ */
255 #define ROOTEDPATH(x) (ABSPATH(x))
258 #define ISDIRSEP(c) ((c) == '/')
259 #define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
262 /* Declarations for functions defined in xmalloc.c */
263 extern PTR_T xmalloc __P((size_t));
264 extern PTR_T xrealloc __P((void *, size_t));
265 extern void xfree __P((void *));
268 /* Declarations for functions defined in general.c */
269 extern void posix_initialize __P((int));
271 #if defined (RLIMTYPE)
272 extern RLIMTYPE string_to_rlimtype __P((char *));
273 extern void print_rlimtype __P((RLIMTYPE, int));
276 extern int all_digits __P((char *));
277 extern int legal_number __P((char *, intmax_t *));
278 extern int legal_identifier __P((char *));
279 extern int check_identifier __P((WORD_DESC *, int));
280 extern int legal_alias_name __P((char *, int));
281 extern int assignment __P((const char *, int));
283 extern int sh_unset_nodelay_mode __P((int));
284 extern int sh_validfd __P((int));
285 extern void check_dev_tty __P((void));
286 extern int move_to_high_fd __P((int, int, int));
287 extern int check_binary_file __P((char *, int));
290 extern int same_file __P((char *, char *, struct stat *, struct stat *));
293 extern int file_isdir __P((char *));
294 extern int file_iswdir __P((char *));
296 extern char *make_absolute __P((char *, char *));
297 extern int absolute_pathname __P((const char *));
298 extern int absolute_program __P((const char *));
299 extern char *base_pathname __P((char *));
300 extern char *full_pathname __P((char *));
301 extern char *polite_directory_format __P((char *));
303 extern char *extract_colon_unit __P((char *, int *));
305 extern void tilde_initialize __P((void));
306 extern char *bash_tilde_expand __P((const char *, int));
308 extern int group_member __P((gid_t));
309 extern char **get_group_list __P((int *));
310 extern int *get_group_array __P((int *));
312 #endif /* _GENERAL_H_ */