Imported from ../bash-1.14.7.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 /* just to make sure */
27 #if defined (HAVE_UNISTD_H)
28 #  ifdef CRAY
29 #    define word __word
30 #  endif
31 #  include <unistd.h>
32 #  ifdef CRAY
33 #    undef word
34 #  endif
35 #endif
36
37 #if !defined (NULL)
38 #  if defined (__STDC__)
39 #    define NULL ((void *) 0)
40 #  else
41 #    define NULL 0x0
42 #  endif /* !__STDC__ */
43 #endif /* !NULL */
44
45 #if defined (HAVE_STRING_H)
46 #  include <string.h>
47 #else
48 #  include <strings.h>
49 #endif /* !HAVE_STRING_H */
50
51 #define pointer_to_int(x) (int)((long)(x))
52
53 #if !defined (savestring)
54    extern char *xmalloc ();
55 #  if !defined (strcpy)
56    extern char *strcpy ();
57 #  endif
58 #  define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
59 #endif
60
61 #ifndef whitespace
62 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
63 #endif
64
65 #ifndef digit
66 #define digit(c)  ((c) >= '0' && (c) <= '9')
67 #endif
68
69 #ifndef isletter
70 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
71 #endif
72
73 #ifndef digit_value
74 #define digit_value(c) ((c) - '0')
75 #endif
76
77 /* Definitions used in subst.c and by the `read' builtin for field
78    splitting. */
79 #define spctabnl(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n')
80
81 #if !defined (__STDC__) && !defined (strchr)
82 extern char *strchr (), *strrchr ();
83 #endif /* !strchr */
84
85 #ifndef member
86 #  if defined (alpha) && defined (__GNUC__)     /* XXX */
87      extern char *strchr ();
88 #  endif
89 #  define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
90 #endif
91
92 /* All structs which contain a `next' field should have that field
93    as the first field in the struct.  This means that functions
94    can be written to handle the general case for linked lists. */
95 typedef struct g_list {
96   struct g_list *next;
97 } GENERIC_LIST;
98
99 /* Here is a generic structure for associating character strings
100    with integers.  It is used in the parser for shell tokenization. */
101 typedef struct {
102   char *word;
103   int token;
104 } STRING_INT_ALIST;
105
106 /* A macro to avoid making an uneccessary function call. */
107 #define REVERSE_LIST(list, type) \
108   ((list && list->next) ? (type)reverse_list ((GENERIC_LIST *)list) : (type)(list))
109
110 #if __GNUC__ > 1
111 #  define FASTCOPY(s, d, n)  __builtin_memcpy (d, s, n)
112 #else /* !__GNUC__ */
113 #  if defined (USG) && !defined (HAVE_BCOPY)
114 #    if defined (MEMMOVE_MISSING)
115 #      define FASTCOPY(s, d, n)  memcpy (d, s, n)
116 #    else
117 #      define FASTCOPY(s, d, n)  memmove (d, s, n)
118 #    endif /* !MEMMOVE_MISSING */
119 #  else
120 #    define FASTCOPY(s, d, n)  bcopy (s, d, n)
121 #  endif /* !USG || HAVE_BCOPY */
122 #endif /* !__GNUC__ */
123
124 /* String comparisons that possibly save a function call each. */
125 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
126 #define STREQN(a, b, n) ((a)[0] == (b)[0] && strncmp(a, b, n) == 0)
127
128 /* More convenience definitions that possibly save system or libc calls. */
129 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
130 #define FREE(s)  do { if (s) free (s); } while (0)
131 #define MEMBER(c, s) (((c) && !(s)[1] && c == s[0]) || (member(c, s)))
132
133 /* What type is a `generic' pointer?  This is used as the first argument
134    to xrealloc. */
135 #if defined (__STDC__)
136 typedef void *GENPTR;
137 #else
138 typedef char *GENPTR;
139 #endif
140
141 /* Function pointers can be declared as (Function *)foo. */
142 #if !defined (__FUNCTION_DEF)
143 #  define __FUNCTION_DEF
144 typedef int Function ();
145 typedef void VFunction ();
146 typedef char *CPFunction ();
147 typedef char **CPPFunction ();
148 #endif /* _FUNCTION_DEF */
149
150 #define NOW     ((time_t) time ((time_t *) 0))
151
152 /* Some defines for calling file status functions. */
153 #define FS_EXISTS         0x1
154 #define FS_EXECABLE       0x2
155 #define FS_EXEC_PREFERRED 0x4
156 #define FS_EXEC_ONLY      0x8
157
158 /* Posix and USG systems do not guarantee to restart a read () that is
159    interrupted by a signal. */
160 #if defined (USG) || defined (_POSIX_VERSION)
161 #  define NO_READ_RESTART_ON_SIGNAL
162 #endif /* USG || _POSIX_VERSION */
163
164 /* Here is a definition for set_signal_handler () which simply expands to
165    a call to signal () for non-Posix systems.  The code for set_signal_handler
166    in the Posix case resides in general.c. */
167
168 #if defined (VOID_SIGHANDLER)
169 #  define sighandler void
170 #else
171 #  define sighandler int
172 #endif /* !VOID_SIGHANDLER */
173
174 typedef sighandler SigHandler ();
175
176 #if !defined (_POSIX_VERSION)
177 #  define set_signal_handler(sig, handler) (SigHandler *)signal (sig, handler)
178 #else
179 extern SigHandler *set_signal_handler ();
180 #endif /* _POSIX_VERSION */
181
182 /* This function is defined in trap.c. */
183 extern SigHandler *set_sigint_handler __P((void));
184
185 /* Declarations for functions defined in general.c */
186 extern char *xmalloc __P((int));
187 extern char *xrealloc __P((void *, int));
188 extern void xfree __P((char *));
189 extern char *itos __P((int));
190 extern int all_digits __P((char *));
191 extern long string_to_long __P((char *));
192 extern int legal_identifier __P((char *));
193 extern int check_identifier __P((WORD_DESC *, int));
194 extern void unset_nodelay_mode __P((int));
195 extern void map_over_words __P((WORD_LIST *, Function *));
196
197 extern void map_over_list __P((GENERIC_LIST *, Function *));
198 extern GENERIC_LIST *reverse_list ();
199 extern GENERIC_LIST *delete_element ();
200 extern GENERIC_LIST *list_append ();
201 extern int list_length ();
202 extern int qsort_string_compare ();
203
204 extern int find_name_in_list __P((char *, char **));
205 extern int array_len __P((char **));
206 extern void free_array __P((char **));
207 extern char **copy_array __P((char **));
208 extern void strip_leading __P((char *));
209 extern void strip_trailing __P((char *, int));
210 extern char *canonicalize_pathname __P((char *));
211 extern char *make_absolute __P((char *, char *));
212 extern int absolute_pathname __P((char *));
213 extern int absolute_program __P((char *));
214 extern char *base_pathname __P((char *));
215 extern char *full_pathname __P((char *));
216 extern char *strindex __P((char *, char *));
217 extern void set_lines_and_columns __P((int, int));
218 extern void xbcopy __P((char *, char *, int));
219 extern char *polite_directory_format __P((char *));
220 extern void tilde_initialize __P((void));
221
222 #if !defined (strerror)
223 extern char *strerror __P((int));
224 #endif
225
226 #if defined (RLIMTYPE)
227 extern RLIMTYPE string_to_rlimtype __P((char *));
228 extern void print_rlimtype __P((RLIMTYPE, int));
229 #endif
230
231 #if !defined (HAVE_STRCASECMP)
232 extern int strnicmp __P((char *, char *, int));
233 extern int stricmp __P((char *, char *));
234 #else /* HAVE_STRCASECMP */
235 #  define stricmp strcasecmp
236 #  define strnicmp strncasecmp
237 #endif /* HAVE_STRCASECMP */
238
239 extern int dup2 __P((int, int));
240 extern char *getwd __P((char *));
241 extern int getdtablesize __P((void));
242
243 #if defined (USG) && !defined (HAVE_GETHOSTNAME)
244 extern int gethostname __P((char *, int));
245 #endif /* USG && !HAVE_GETHOSTNAME */
246
247 #endif  /* _GENERAL_H */