64a671be9c96283e913191d01a629f4abd0343e5
[platform/upstream/bash.git] / general.h
1 /* general.h -- defines that everybody likes to use. */
2
3 /* Copyright (C) 1993 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 #define pointer_to_int(x) (int)((long)(x))
57
58 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
59 extern char *strchr (), *strrchr ();
60 #endif
61
62 #if !defined (strcpy)
63 extern char *strcpy __P((char *, const char *));
64 #endif
65
66 #if !defined (savestring)
67 #  define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
68 #endif
69
70 #ifndef member
71 #  define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
72 #endif
73
74 #ifndef whitespace
75 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
76 #endif
77
78 #ifndef CHAR_MAX
79 #  ifdef __CHAR_UNSIGNED__
80 #    define CHAR_MAX    0xff
81 #  else
82 #    define CHAR_MAX    0x7f
83 #  endif
84 #endif
85
86 #ifndef CHAR_BIT
87 #  define CHAR_BIT 8
88 #endif
89
90 /* Nonzero if the integer type T is signed.  */
91 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
92
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))
101
102
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 == '_')
106
107 /* Definitions used in subst.c and by the `read' builtin for field
108    splitting. */
109 #define spctabnl(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n')
110
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 {
115   struct g_list *next;
116 } GENERIC_LIST;
117
118 /* Here is a generic structure for associating character strings
119    with integers.  It is used in the parser for shell tokenization. */
120 typedef struct {
121   char *word;
122   int token;
123 } STRING_INT_ALIST;
124
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) \
128                         : (type)(list))
129
130 #if __GNUC__ > 1
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)
136 #    else
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__ */
143
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))
148
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)))
153
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) */
161
162 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
163   do { \
164     if ((cind) + (room) >= csize) \
165       { \
166         while ((cind) + (room) >= csize) \
167           csize += (sincr); \
168         str = xrealloc (str, csize); \
169       } \
170   } while (0)
171
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 */
180
181 #ifndef SH_FUNCTION_TYPEDEF
182 #  define SH_FUNCTION_TYPEDEF
183
184 /* Shell function typedefs with prototypes */
185 /* `Generic' function pointer typedefs */
186
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));
192
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));
198
199 typedef int sh_wdesc_func_t __P((WORD_DESC *));
200 typedef int sh_wlist_func_t __P((WORD_LIST *));
201
202 typedef int sh_glist_func_t __P((GENERIC_LIST *));
203
204 typedef char *sh_string_func_t __P((char *));   /* like savestring, et al. */
205
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 */
208
209 /* Specific function pointer typedefs.  Most of these could be done
210    with #defines. */
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 */
214
215 typedef int sh_ignore_func_t __P((const char *));       /* sh_icpfunc_t */
216
217 typedef int sh_assign_func_t __P((const char *));       /* sh_icpfunc_t */
218
219 typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
220
221 #endif /* SH_FUNCTION_TYPEDEF */
222
223 #define NOW     ((time_t) time ((time_t *) 0))
224
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
232
233 /* The type of function passed as the fourth argument to qsort(3). */
234 #ifdef __STDC__
235 typedef int QSFUNC (const void *, const void *);
236 #else
237 typedef int QSFUNC ();
238 #endif 
239
240 /* Some useful definitions for Unix pathnames.  Argument convention:
241    x == string, c == character */
242
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__ */
250
251 #define ROOTEDPATH(x)   (ABSPATH(x))
252
253 #define DIRSEP  '/'
254 #define ISDIRSEP(c)     ((c) == '/')
255 #define PATHSEP(c)      (ISDIRSEP(c) || (c) == 0)
256
257 #if 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 *));
262 #endif
263
264 /* Declarations for functions defined in general.c */
265 extern void posix_initialize __P((int));
266
267 #if defined (RLIMTYPE)
268 extern RLIMTYPE string_to_rlimtype __P((char *));
269 extern void print_rlimtype __P((RLIMTYPE, int));
270 #endif
271
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));
276
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));
281
282 #ifdef _POSIXSTAT_H_
283 extern int same_file __P((char *, char *, struct stat *, struct stat *));
284 #endif
285
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 *));
292
293 extern char *extract_colon_unit __P((char *, int *));
294
295 extern void tilde_initialize __P((void));
296 extern char *bash_tilde_expand __P((const char *));
297
298 extern int group_member __P((gid_t));
299 extern char **get_group_list __P((int *));
300 extern int *get_group_array __P((int *));
301
302 #endif  /* _GENERAL_H_ */