1 /* general.h -- defines that everybody likes to use. */
3 /* Copyright (C) 1993 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 #define pointer_to_int(x) (int)((long)(x))
58 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
59 extern char *strchr (), *strrchr ();
63 extern char *strcpy __P((char *, const char *));
66 #if !defined (savestring)
67 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
71 # define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
75 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
79 # ifdef __CHAR_UNSIGNED__
80 # define CHAR_MAX 0xff
82 # define CHAR_MAX 0x7f
90 /* Nonzero if the integer type T is signed. */
91 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
93 /* Bound on length of the string representing an integer value of type T.
94 Subtract one for the sign bit if T is signed;
95 302 / 1000 is log10 (2) rounded up;
96 add one for integer division truncation;
97 add one more for a minus sign if t is signed. */
98 #define INT_STRLEN_BOUND(t) \
99 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
100 + 1 + TYPE_SIGNED (t))
103 /* Define exactly what a legal shell identifier consists of. */
104 #define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
105 #define legal_variable_char(c) (ISALNUM(c) || c == '_')
107 /* Definitions used in subst.c and by the `read' builtin for field
109 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
111 /* All structs which contain a `next' field should have that field
112 as the first field in the struct. This means that functions
113 can be written to handle the general case for linked lists. */
114 typedef struct g_list {
118 /* Here is a generic structure for associating character strings
119 with integers. It is used in the parser for shell tokenization. */
125 /* A macro to avoid making an uneccessary function call. */
126 #define REVERSE_LIST(list, type) \
127 ((list && list->next) ? (type)reverse_list ((GENERIC_LIST *)list) \
131 # define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
132 #else /* !__GNUC__ */
133 # if !defined (HAVE_BCOPY)
134 # if !defined (HAVE_MEMMOVE)
135 # define FASTCOPY(s, d, n) memcpy (d, s, n)
137 # define FASTCOPY(s, d, n) memmove (d, s, n)
138 # endif /* !HAVE_MEMMOVE */
139 # else /* HAVE_BCOPY */
140 # define FASTCOPY(s, d, n) bcopy (s, d, n)
141 # endif /* HAVE_BCOPY */
142 #endif /* !__GNUC__ */
144 /* String comparisons that possibly save a function call each. */
145 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
146 #define STREQN(a, b, n) ((n == 0) ? (1) \
147 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
149 /* More convenience definitions that possibly save system or libc calls. */
150 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
151 #define FREE(s) do { if (s) free (s); } while (0)
152 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
154 /* A fairly hairy macro to check whether an allocated string has more room,
155 and to resize it using xrealloc if it does not.
156 STR is the string (char *)
157 CIND is the current index into the string (int)
158 ROOM is the amount of additional room we need in the string (int)
159 CSIZE is the currently-allocated size of STR (int)
160 SINCR is how much to increment CSIZE before calling xrealloc (int) */
162 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
164 if ((cind) + (room) >= csize) \
166 while ((cind) + (room) >= csize) \
168 str = xrealloc (str, csize); \
172 /* Function pointers can be declared as (Function *)foo. */
173 #if !defined (_FUNCTION_DEF)
174 # define _FUNCTION_DEF
175 typedef int Function ();
176 typedef void VFunction ();
177 typedef char *CPFunction (); /* no longer used */
178 typedef char **CPPFunction (); /* no longer used */
179 #endif /* _FUNCTION_DEF */
181 #ifndef SH_FUNCTION_TYPEDEF
182 # define SH_FUNCTION_TYPEDEF
184 /* Shell function typedefs with prototypes */
185 /* `Generic' function pointer typedefs */
187 typedef int sh_intfunc_t __P((int));
188 typedef int sh_ivoidfunc_t __P((void));
189 typedef int sh_icpfunc_t __P((char *));
190 typedef int sh_icppfunc_t __P((char **));
191 typedef int sh_iptrfunc_t __P((PTR_T));
193 typedef void sh_voidfunc_t __P((void));
194 typedef void sh_vintfunc_t __P((int));
195 typedef void sh_vcpfunc_t __P((char *));
196 typedef void sh_vcppfunc_t __P((char **));
197 typedef void sh_vptrfunc_t __P((PTR_T));
199 typedef int sh_wdesc_func_t __P((WORD_DESC *));
200 typedef int sh_wlist_func_t __P((WORD_LIST *));
202 typedef int sh_glist_func_t __P((GENERIC_LIST *));
204 typedef char *sh_string_func_t __P((char *)); /* like savestring, et al. */
206 typedef int sh_msg_func_t __P((const char *, ...)); /* printf(3)-like */
207 typedef void sh_vmsg_func_t __P((const char *, ...)); /* printf(3)-like */
209 /* Specific function pointer typedefs. Most of these could be done
211 typedef void sh_sv_func_t __P((char *)); /* sh_vcpfunc_t */
212 typedef void sh_free_func_t __P((PTR_T)); /* sh_vptrfunc_t */
213 typedef void sh_resetsig_func_t __P((int)); /* sh_vintfunc_t */
215 typedef int sh_ignore_func_t __P((const char *)); /* sh_icpfunc_t */
217 typedef int sh_assign_func_t __P((const char *)); /* sh_icpfunc_t */
219 typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
221 #endif /* SH_FUNCTION_TYPEDEF */
223 #define NOW ((time_t) time ((time_t *) 0))
225 /* Some defines for calling file status functions. */
226 #define FS_EXISTS 0x1
227 #define FS_EXECABLE 0x2
228 #define FS_EXEC_PREFERRED 0x4
229 #define FS_EXEC_ONLY 0x8
230 #define FS_DIRECTORY 0x10
231 #define FS_NODIRS 0x20
233 /* The type of function passed as the fourth argument to qsort(3). */
235 typedef int QSFUNC (const void *, const void *);
237 typedef int QSFUNC ();
240 /* Some useful definitions for Unix pathnames. Argument convention:
241 x == string, c == character */
243 #if !defined (__CYGWIN__)
244 # define ABSPATH(x) ((x)[0] == '/')
245 # define RELPATH(x) ((x)[0] != '/')
246 #else /* __CYGWIN__ */
247 # define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':' && (x)[2] == '/') || (x)[0] == '/')
248 # define RELPATH(x) (!(x)[0] || ((x)[1] != ':' && (x)[0] != '/'))
249 #endif /* __CYGWIN__ */
251 #define ROOTEDPATH(x) (ABSPATH(x))
254 #define ISDIRSEP(c) ((c) == '/')
255 #define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
258 /* Declarations for functions defined in xmalloc.c */
259 extern PTR_T xmalloc __P((size_t));
260 extern PTR_T xrealloc __P((void *, size_t));
261 extern void xfree __P((void *));
264 /* Declarations for functions defined in general.c */
265 extern void posix_initialize __P((int));
267 #if defined (RLIMTYPE)
268 extern RLIMTYPE string_to_rlimtype __P((char *));
269 extern void print_rlimtype __P((RLIMTYPE, int));
272 extern int all_digits __P((char *));
273 extern int legal_number __P((char *, long *));
274 extern int legal_identifier __P((char *));
275 extern int check_identifier __P((WORD_DESC *, int));
277 extern int sh_unset_nodelay_mode __P((int));
278 extern void check_dev_tty __P((void));
279 extern int move_to_high_fd __P((int, int, int));
280 extern int check_binary_file __P((char *, int));
283 extern int same_file __P((char *, char *, struct stat *, struct stat *));
286 extern char *make_absolute __P((char *, char *));
287 extern int absolute_pathname __P((const char *));
288 extern int absolute_program __P((const char *));
289 extern char *base_pathname __P((char *));
290 extern char *full_pathname __P((char *));
291 extern char *polite_directory_format __P((char *));
293 extern char *extract_colon_unit __P((char *, int *));
295 extern void tilde_initialize __P((void));
296 extern char *bash_tilde_expand __P((const char *));
298 extern int group_member __P((gid_t));
299 extern char **get_group_list __P((int *));
300 extern int *get_group_array __P((int *));
302 #endif /* _GENERAL_H_ */