b520c1d76d99da3b8a8cf9625a697e6b003a09a5
[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 /* Generic pointer type. */
27 #if defined (__STDC__)
28 #  define PTR_T void *
29 #else
30 #  define PTR_T char *
31 #endif
32
33 /* NULL pointer type. */
34 #if !defined (NULL)
35 #  if defined (__STDC__)
36 #    define NULL ((void *) 0)
37 #  else
38 #    define NULL 0x0
39 #  endif /* !__STDC__ */
40 #endif /* !NULL */
41
42 #if defined (HAVE_STRING_H)
43 #  include <string.h>
44 #else
45 #  include <strings.h>
46 #endif /* !HAVE_STRING_H */
47
48 #define pointer_to_int(x) (int)((long)(x))
49
50 extern char *xmalloc (), *xrealloc ();
51
52 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
53 extern char *strchr (), *strrchr ();
54 #endif
55
56 #if !defined (strcpy)
57 extern char *strcpy ();
58 #endif
59
60 #if !defined (savestring)
61 #  define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
62 #endif
63
64 #ifndef member
65 #  define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
66 #endif
67
68 #ifndef whitespace
69 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
70 #endif
71
72 #ifndef digit
73 #define digit(c)  ((c) >= '0' && (c) <= '9')
74 #endif
75
76 #ifndef isletter
77 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
78 #endif
79
80 #ifndef digit_value
81 #define digit_value(c) ((c) - '0')
82 #endif
83
84 /* Define exactly what a legal shell identifier consists of. */
85 #define legal_variable_starter(c) (isletter(c) || (c == '_'))
86 #define legal_variable_char(c)  (isletter (c) || digit (c) || c == '_')
87
88 /* Definitions used in subst.c and by the `read' builtin for field
89    splitting. */
90 #define spctabnl(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n')
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) \
109                         : (type)(list))
110
111 #if __GNUC__ > 1
112 #  define FASTCOPY(s, d, n)  __builtin_memcpy (d, s, n)
113 #else /* !__GNUC__ */
114 #  if !defined (HAVE_BCOPY)
115 #    if !defined (HAVE_MEMMOVE)
116 #      define FASTCOPY(s, d, n)  memcpy (d, s, n)
117 #    else
118 #      define FASTCOPY(s, d, n)  memmove (d, s, n)
119 #    endif /* !HAVE_MEMMOVE */
120 #  else /* HAVE_BCOPY */
121 #    define FASTCOPY(s, d, n)  bcopy (s, d, n)
122 #  endif /* HAVE_BCOPY */
123 #endif /* !__GNUC__ */
124
125 /* String comparisons that possibly save a function call each. */
126 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
127 #define STREQN(a, b, n) ((a)[0] == (b)[0] && strncmp(a, b, n) == 0)
128
129 /* More convenience definitions that possibly save system or libc calls. */
130 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
131 #define FREE(s)  do { if (s) free (s); } while (0)
132 #define MEMBER(c, s) (((c) && !(s)[1] && c == s[0]) || (member(c, s)))
133
134 /* A fairly hairy macro to check whether an allocated string has more room,
135    and to resize it using xrealloc if it does not.
136    STR is the string (char *)
137    CIND is the current index into the string (int)
138    ROOM is the amount of additional room we need in the string (int)
139    CSIZE is the currently-allocated size of STR (int)
140    SINCR is how much to increment CSIZE before calling xrealloc (int) */
141
142 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
143   do { \
144     if ((cind) + (room) >= csize) \
145       { \
146         while ((cind) + (room) >= csize) \
147           csize += (sincr); \
148         str = xrealloc (str, csize); \
149       } \
150   } while (0)
151
152 /* Function pointers can be declared as (Function *)foo. */
153 #if !defined (_FUNCTION_DEF)
154 #  define _FUNCTION_DEF
155 typedef int Function ();
156 typedef void VFunction ();
157 typedef char *CPFunction ();
158 typedef char **CPPFunction ();
159 #endif /* _FUNCTION_DEF */
160
161 #define NOW     ((time_t) time ((time_t *) 0))
162
163 /* Some defines for calling file status functions. */
164 #define FS_EXISTS         0x1
165 #define FS_EXECABLE       0x2
166 #define FS_EXEC_PREFERRED 0x4
167 #define FS_EXEC_ONLY      0x8
168 #define FS_DIRECTORY      0x10
169 #define FS_NODIRS         0x20
170
171 /* Declarations for functions defined in xmalloc.c */
172 extern char *xmalloc __P((size_t));
173 extern char *xrealloc __P((void *, size_t));
174 extern void xfree __P((char *));
175
176 /* Declarations for functions defined in general.c */
177 extern void posix_initialize __P((int));
178
179 extern char *itos __P((int));
180 extern long string_to_long __P((char *));
181
182 #if defined (RLIMTYPE)
183 extern RLIMTYPE string_to_rlimtype __P((char *));
184 extern void print_rlimtype __P((RLIMTYPE, int));
185 #endif
186
187 extern void timeval_to_secs ();
188 extern void print_timeval ();
189 extern void clock_t_to_secs ();
190 extern void print_time_in_hz ();
191
192 extern int all_digits __P((char *));
193 extern int legal_number __P((char *, long *));
194 extern int legal_identifier __P((char *));
195 extern int check_identifier __P((WORD_DESC *, int));
196
197 extern void unset_nodelay_mode __P((int));
198 extern void check_dev_tty __P((void));
199 extern int same_file ();        /* too many problems with prototype */
200 extern int move_to_high_fd __P((int, int));
201 extern int check_binary_file __P((unsigned char *, int));
202
203 extern char *canonicalize_pathname __P((char *));
204 extern char *make_absolute __P((char *, char *));
205 extern int absolute_pathname __P((char *));
206 extern int absolute_program __P((char *));
207 extern char *base_pathname __P((char *));
208 extern char *full_pathname __P((char *));
209 extern char *polite_directory_format __P((char *));
210
211 extern char *extract_colon_unit __P((char *, int *));
212
213 extern void tilde_initialize __P((void));
214 extern char *bash_tilde_expand __P((char *));
215
216 #endif  /* _GENERAL_H_ */