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 /* Generic pointer type. */
42 #if defined (__STDC__)
48 /* NULL pointer type. */
50 # if defined (__STDC__)
51 # define NULL ((void *) 0)
54 # endif /* !__STDC__ */
57 #define pointer_to_int(x) (int)((long)(x))
59 extern char *xmalloc (), *xrealloc ();
61 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
62 extern char *strchr (), *strrchr ();
66 extern char *strcpy ();
69 #if !defined (savestring)
70 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
74 # define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
78 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
82 #define digit(c) ((c) >= '0' && (c) <= '9')
86 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
90 #define digit_value(c) ((c) - '0')
93 /* Define exactly what a legal shell identifier consists of. */
94 #define legal_variable_starter(c) (isletter(c) || (c == '_'))
95 #define legal_variable_char(c) (isletter (c) || digit (c) || c == '_')
97 /* Definitions used in subst.c and by the `read' builtin for field
99 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
101 /* All structs which contain a `next' field should have that field
102 as the first field in the struct. This means that functions
103 can be written to handle the general case for linked lists. */
104 typedef struct g_list
{
108 /* Here is a generic structure for associating character strings
109 with integers. It is used in the parser for shell tokenization. */
115 /* A macro to avoid making an uneccessary function call. */
116 #define REVERSE_LIST(list, type) \
117 ((list && list->next) ? (type)reverse_list ((GENERIC_LIST *)list) \
121 # define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
122 #else /* !__GNUC__ */
123 # if !defined (HAVE_BCOPY)
124 # if !defined (HAVE_MEMMOVE)
125 # define FASTCOPY(s, d, n) memcpy (d, s, n)
127 # define FASTCOPY(s, d, n) memmove (d, s, n)
128 # endif /* !HAVE_MEMMOVE */
129 # else /* HAVE_BCOPY */
130 # define FASTCOPY(s, d, n) bcopy (s, d, n)
131 # endif /* HAVE_BCOPY */
132 #endif /* !__GNUC__ */
134 /* String comparisons that possibly save a function call each. */
135 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
136 #define STREQN(a, b, n) ((n == 0) ? (1) \
137 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
139 /* More convenience definitions that possibly save system or libc calls. */
140 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
141 #define FREE(s) do { if (s) free (s); } while (0)
142 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
144 /* A fairly hairy macro to check whether an allocated string has more room,
145 and to resize it using xrealloc if it does not.
146 STR is the string (char *)
147 CIND is the current index into the string (int)
148 ROOM is the amount of additional room we need in the string (int)
149 CSIZE is the currently-allocated size of STR (int)
150 SINCR is how much to increment CSIZE before calling xrealloc (int) */
152 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
154 if ((cind) + (room) >= csize) \
156 while ((cind) + (room) >= csize) \
158 str = xrealloc (str, csize); \
162 /* Function pointers can be declared as (Function *)foo. */
163 #if !defined (_FUNCTION_DEF)
164 # define _FUNCTION_DEF
165 typedef int Function ();
166 typedef void VFunction ();
167 typedef char *CPFunction ();
168 typedef char **CPPFunction ();
169 #endif /* _FUNCTION_DEF */
171 #define NOW ((time_t) time ((time_t *) 0))
173 /* Some defines for calling file status functions. */
174 #define FS_EXISTS 0x1
175 #define FS_EXECABLE 0x2
176 #define FS_EXEC_PREFERRED 0x4
177 #define FS_EXEC_ONLY 0x8
178 #define FS_DIRECTORY 0x10
179 #define FS_NODIRS 0x20
181 /* Declarations for functions defined in xmalloc.c */
182 extern char *xmalloc
__P((size_t));
183 extern char *xrealloc
__P((void *, size_t));
184 extern void xfree
__P((void *));
186 /* Declarations for functions defined in general.c */
187 extern void posix_initialize
__P((int));
189 #if defined (RLIMTYPE)
190 extern RLIMTYPE string_to_rlimtype
__P((char *));
191 extern void print_rlimtype
__P((RLIMTYPE
, int));
194 extern int all_digits
__P((char *));
195 extern int legal_number
__P((char *, long *));
196 extern int legal_identifier
__P((char *));
197 extern int check_identifier
__P((WORD_DESC
*, int));
199 extern int unset_nodelay_mode
__P((int));
200 extern void check_dev_tty
__P((void));
201 extern int same_file (); /* too many problems with prototype */
202 extern int move_to_high_fd
__P((int, int, int));
203 extern int check_binary_file
__P((unsigned char *, int));
205 extern char *canonicalize_pathname
__P((char *));
206 extern char *make_absolute
__P((char *, char *));
207 extern int absolute_pathname
__P((char *));
208 extern int absolute_program
__P((char *));
209 extern char *base_pathname
__P((char *));
210 extern char *full_pathname
__P((char *));
211 extern char *polite_directory_format
__P((char *));
213 extern char *extract_colon_unit
__P((char *, int *));
215 extern void tilde_initialize
__P((void));
216 extern char *bash_tilde_expand
__P((char *));
218 extern int group_member
__P((gid_t
));
219 extern char **get_group_list
__P((int *));
220 extern int *get_group_array
__P((int *));
222 #endif /* _GENERAL_H_ */