]> git.ipfire.org Git - thirdparty/bash.git/blob - general.h
commit bash-20040408 snapshot
[thirdparty/bash.git] / general.h
1 /* general.h -- defines that everybody likes to use. */
2
3 /* Copyright (C) 1993-2004 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 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
10 version.
11
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
15 for more details.
16
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. */
20
21 #if !defined (_GENERAL_H_)
22 #define _GENERAL_H_
23
24 #include "stdc.h"
25
26 #include "bashtypes.h"
27
28 #if defined (HAVE_SYS_RESOURCE_H) && defined (RLIMTYPE)
29 # if defined (HAVE_SYS_TIME_H)
30 # include <sys/time.h>
31 # endif
32 # include <sys/resource.h>
33 #endif
34
35 #if defined (HAVE_STRING_H)
36 # include <string.h>
37 #else
38 # include <strings.h>
39 #endif /* !HAVE_STRING_H */
40
41 #if defined (HAVE_LIMITS_H)
42 # include <limits.h>
43 #endif
44
45 #include "xmalloc.h"
46
47 /* NULL pointer type. */
48 #if !defined (NULL)
49 # if defined (__STDC__)
50 # define NULL ((void *) 0)
51 # else
52 # define NULL 0x0
53 # endif /* !__STDC__ */
54 #endif /* !NULL */
55
56 /* Hardly used anymore */
57 #define pointer_to_int(x) (int)((char *)x - (char *)0)
58
59 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
60 extern char *strchr (), *strrchr ();
61 #endif
62
63 #if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
64 extern char *strcpy __P((char *, const char *));
65 #endif
66
67 #if !defined (savestring)
68 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
69 #endif
70
71 #ifndef member
72 # define member(c, s) ((c) ? ((char *)xstrchr ((s), (c)) != (char *)NULL) : 0)
73 #endif
74
75 #ifndef whitespace
76 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
77 #endif
78
79 #ifndef CHAR_MAX
80 # ifdef __CHAR_UNSIGNED__
81 # define CHAR_MAX 0xff
82 # else
83 # define CHAR_MAX 0x7f
84 # endif
85 #endif
86
87 #ifndef CHAR_BIT
88 # define CHAR_BIT 8
89 #endif
90
91 /* Nonzero if the integer type T is signed. */
92 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
93
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))
102
103
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 == '_')
107
108 /* Definitions used in subst.c and by the `read' builtin for field
109 splitting. */
110 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
111
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 {
116 struct g_list *next;
117 } GENERIC_LIST;
118
119 /* Here is a generic structure for associating character strings
120 with integers. It is used in the parser for shell tokenization. */
121 typedef struct {
122 char *word;
123 int token;
124 } STRING_INT_ALIST;
125
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) \
129 : (type)(list))
130
131 #if __GNUC__ > 1
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)
137 # else
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__ */
144
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))
149
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)))
154
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) */
162
163 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
164 do { \
165 if ((cind) + (room) >= csize) \
166 { \
167 while ((cind) + (room) >= csize) \
168 csize += (sincr); \
169 str = xrealloc (str, csize); \
170 } \
171 } while (0)
172
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 */
181
182 #ifndef SH_FUNCTION_TYPEDEF
183 # define SH_FUNCTION_TYPEDEF
184
185 /* Shell function typedefs with prototypes */
186 /* `Generic' function pointer typedefs */
187
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));
193
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));
199
200 typedef int sh_wdesc_func_t __P((WORD_DESC *));
201 typedef int sh_wlist_func_t __P((WORD_LIST *));
202
203 typedef int sh_glist_func_t __P((GENERIC_LIST *));
204
205 typedef char *sh_string_func_t __P((char *)); /* like savestring, et al. */
206
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 */
209
210 /* Specific function pointer typedefs. Most of these could be done
211 with #defines. */
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 */
215
216 typedef int sh_ignore_func_t __P((const char *)); /* sh_icpfunc_t */
217
218 typedef int sh_assign_func_t __P((const char *)); /* sh_icpfunc_t */
219
220 typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
221
222 #endif /* SH_FUNCTION_TYPEDEF */
223
224 #define NOW ((time_t) time ((time_t *) 0))
225
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
233
234 /* Default maximum for move_to_high_fd */
235 #define HIGH_FD_MAX 256
236
237 /* The type of function passed as the fourth argument to qsort(3). */
238 #ifdef __STDC__
239 typedef int QSFUNC (const void *, const void *);
240 #else
241 typedef int QSFUNC ();
242 #endif
243
244 /* Some useful definitions for Unix pathnames. Argument convention:
245 x == string, c == character */
246
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__ */
254
255 #define ROOTEDPATH(x) (ABSPATH(x))
256
257 #define DIRSEP '/'
258 #define ISDIRSEP(c) ((c) == '/')
259 #define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
260
261 #if 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 *));
266 #endif
267
268 /* Declarations for functions defined in general.c */
269 extern void posix_initialize __P((int));
270
271 #if defined (RLIMTYPE)
272 extern RLIMTYPE string_to_rlimtype __P((char *));
273 extern void print_rlimtype __P((RLIMTYPE, int));
274 #endif
275
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));
282
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));
288
289 #ifdef _POSIXSTAT_H_
290 extern int same_file __P((char *, char *, struct stat *, struct stat *));
291 #endif
292
293 extern int file_isdir __P((char *));
294 extern int file_iswdir __P((char *));
295
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 *));
302
303 extern char *extract_colon_unit __P((char *, int *));
304
305 extern void tilde_initialize __P((void));
306 extern char *bash_tilde_expand __P((const char *, int));
307
308 extern int group_member __P((gid_t));
309 extern char **get_group_list __P((int *));
310 extern int *get_group_array __P((int *));
311
312 #endif /* _GENERAL_H_ */