Imported from ../bash-2.03.tar.gz.
[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, 675 Mass Ave, Cambridge, MA 02139, 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 /* Generic pointer type. */
42 #if defined (__STDC__)
43 #  define PTR_T void *
44 #else
45 #  define PTR_T char *
46 #endif
47
48 /* NULL pointer type. */
49 #if !defined (NULL)
50 #  if defined (__STDC__)
51 #    define NULL ((void *) 0)
52 #  else
53 #    define NULL 0x0
54 #  endif /* !__STDC__ */
55 #endif /* !NULL */
56
57 #define pointer_to_int(x) (int)((long)(x))
58
59 extern char *xmalloc (), *xrealloc ();
60
61 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
62 extern char *strchr (), *strrchr ();
63 #endif
64
65 #if !defined (strcpy)
66 extern char *strcpy ();
67 #endif
68
69 #if !defined (savestring)
70 #  define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
71 #endif
72
73 #ifndef member
74 #  define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
75 #endif
76
77 #ifndef whitespace
78 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
79 #endif
80
81 #ifndef digit
82 #define digit(c)  ((c) >= '0' && (c) <= '9')
83 #endif
84
85 #ifndef isletter
86 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
87 #endif
88
89 #ifndef digit_value
90 #define digit_value(c) ((c) - '0')
91 #endif
92
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 == '_')
96
97 /* Definitions used in subst.c and by the `read' builtin for field
98    splitting. */
99 #define spctabnl(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n')
100
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 {
105   struct g_list *next;
106 } GENERIC_LIST;
107
108 /* Here is a generic structure for associating character strings
109    with integers.  It is used in the parser for shell tokenization. */
110 typedef struct {
111   char *word;
112   int token;
113 } STRING_INT_ALIST;
114
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) \
118                         : (type)(list))
119
120 #if __GNUC__ > 1
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)
126 #    else
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__ */
133
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) ((a)[0] == (b)[0] && strncmp(a, b, n) == 0)
137
138 /* More convenience definitions that possibly save system or libc calls. */
139 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
140 #define FREE(s)  do { if (s) free (s); } while (0)
141 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
142
143 /* A fairly hairy macro to check whether an allocated string has more room,
144    and to resize it using xrealloc if it does not.
145    STR is the string (char *)
146    CIND is the current index into the string (int)
147    ROOM is the amount of additional room we need in the string (int)
148    CSIZE is the currently-allocated size of STR (int)
149    SINCR is how much to increment CSIZE before calling xrealloc (int) */
150
151 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
152   do { \
153     if ((cind) + (room) >= csize) \
154       { \
155         while ((cind) + (room) >= csize) \
156           csize += (sincr); \
157         str = xrealloc (str, csize); \
158       } \
159   } while (0)
160
161 /* Function pointers can be declared as (Function *)foo. */
162 #if !defined (_FUNCTION_DEF)
163 #  define _FUNCTION_DEF
164 typedef int Function ();
165 typedef void VFunction ();
166 typedef char *CPFunction ();
167 typedef char **CPPFunction ();
168 #endif /* _FUNCTION_DEF */
169
170 #define NOW     ((time_t) time ((time_t *) 0))
171
172 /* Some defines for calling file status functions. */
173 #define FS_EXISTS         0x1
174 #define FS_EXECABLE       0x2
175 #define FS_EXEC_PREFERRED 0x4
176 #define FS_EXEC_ONLY      0x8
177 #define FS_DIRECTORY      0x10
178 #define FS_NODIRS         0x20
179
180 /* Declarations for functions defined in xmalloc.c */
181 extern char *xmalloc __P((size_t));
182 extern char *xrealloc __P((void *, size_t));
183 extern void xfree __P((char *));
184
185 /* Declarations for functions defined in general.c */
186 extern void posix_initialize __P((int));
187
188 #if defined (RLIMTYPE)
189 extern RLIMTYPE string_to_rlimtype __P((char *));
190 extern void print_rlimtype __P((RLIMTYPE, int));
191 #endif
192
193 extern void timeval_to_secs ();
194 extern void print_timeval ();
195 extern void clock_t_to_secs ();
196 extern void print_time_in_hz ();
197
198 extern int all_digits __P((char *));
199 extern int legal_number __P((char *, long *));
200 extern int legal_identifier __P((char *));
201 extern int check_identifier __P((WORD_DESC *, int));
202
203 extern void unset_nodelay_mode __P((int));
204 extern void check_dev_tty __P((void));
205 extern int same_file ();        /* too many problems with prototype */
206 extern int move_to_high_fd __P((int, int, int));
207 extern int check_binary_file __P((unsigned char *, int));
208
209 extern char *canonicalize_pathname __P((char *));
210 extern char *make_absolute __P((char *, char *));
211 extern int absolute_pathname __P((char *));
212 extern int absolute_program __P((char *));
213 extern char *base_pathname __P((char *));
214 extern char *full_pathname __P((char *));
215 extern char *polite_directory_format __P((char *));
216
217 extern char *extract_colon_unit __P((char *, int *));
218
219 extern void tilde_initialize __P((void));
220 extern char *bash_tilde_expand __P((char *));
221
222 extern int group_member __P((gid_t));
223 extern char **get_group_list __P((int *));
224 extern int *get_group_array __P((int *));
225
226 #endif  /* _GENERAL_H_ */