improve of cmpl.
[bush.git] / mydoc / vname / general.h
blob4d16d827b08db897048eab6737a1af86d00f8a51
1 /* general.h -- defines that everybody likes to use. */
3 /* Copyright (C) 1993-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
21 #if !defined (_GENERAL_H_)
22 #define _GENERAL_H_
24 #include "stdc.h"
26 #include "bushtypes.h"
27 #include "chartypes.h"
29 #if defined (HAVE_SYS_RESOURCE_H) && defined (RLIMTYPE)
30 # if defined (HAVE_SYS_TIME_H)
31 # include <sys/time.h>
32 # endif
33 # include <sys/resource.h>
34 #endif
36 #if defined (HAVE_STRING_H)
37 # include <string.h>
38 #else
39 # include <strings.h>
40 #endif /* !HAVE_STRING_H */
42 #if defined (HAVE_LIMITS_H)
43 # include <limits.h>
44 #endif
46 #include "xmalloc.h"
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 */
57 /* Hardly used anymore */
58 #define pointer_to_int(x) (int)((char *)x - (char *)0)
60 #if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
61 extern char *strchr (), *strrchr ();
62 #endif
64 #if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
65 extern char *strcpy PARAMS((char *, const char *));
66 #endif
68 #if !defined (savestring)
69 # define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
70 #endif
72 #ifndef member
73 # define member(c, s) ((c) ? ((char *)mbschr ((s), (c)) != (char *)NULL) : 0)
74 #endif
76 #ifndef whitespace
77 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
78 #endif
80 #ifndef CHAR_MAX
81 # ifdef __CHAR_UNSIGNED__
82 # define CHAR_MAX 0xff
83 # else
84 # define CHAR_MAX 0x7f
85 # endif
86 #endif
88 #ifndef CHAR_BIT
89 # define CHAR_BIT 8
90 #endif
92 /* Nonzero if the integer type T is signed. */
93 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
95 /* The width in bits of the integer type or expression T.
96 Padding bits are not supported; this is checked at compile-time below. */
97 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
99 /* Bound on length of the string representing an unsigned integer
100 value representable in B bits. log10 (2.0) < 146/485. The
101 smallest value of B where this bound is not tight is 2621. */
102 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
104 /* Bound on length of the string representing an integer value of type T.
105 Subtract one for the sign bit if T is signed;
106 302 / 1000 is log10 (2) rounded up;
107 add one for integer division truncation;
108 add one more for a minus sign if t is signed. */
109 #define INT_STRLEN_BOUND(t) \
110 ((TYPE_WIDTH (t) - TYPE_SIGNED (t)) * 302 / 1000 \
111 + 1 + TYPE_SIGNED (t))
113 /* Updated version adapted from gnulib/intprops.h, not used right now.
114 Changes the approximation of log10(2) from 302/1000 to 146/485. */
115 #if 0
116 #define INT_STRLEN_BOUND(t) \
117 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - TYPE_SIGNED (t)) + TYPE_SIGNED(t))
118 #endif
120 /* Bound on buffer size needed to represent an integer type or expression T,
121 including the terminating null. */
122 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
124 /* Define exactly what a legal shell identifier consists of. */
125 #define org_legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
126 #define org_legal_variable_char(c) (ISALNUM(c) || c == '_')
128 #define legal_variable_starter(c) (ISALPHA(c) || (c == '_') || c == ':')
129 #define legal_variable_starter2(cp) ((*(cp) == ':') && (*(cp+1) == ':') && (*(++(cp))))
130 #define legal_variable_ender(c) (c == ':' || c == '.')
131 #define legal_variable_char(c) (ISALNUM(c) || c == '_' || c == ':' || c == '.')
132 #define legal_variable_char2(cp) ((*(cp) != ':') || (*(cp) == ':') && (*(cp+1) == ':') && (*(++(cp))))
133 #define legal_variable_char3(cp,idx) ((cp[idx] != ':') || (cp[idx] == ':') && (cp[idx+1] == ':') && cp[idx+=1])
135 /* Definitions used in subst.c and by the `read' builtin for field
136 splitting. */
137 #define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
139 /* All structs which contain a `next' field should have that field
140 as the first field in the struct. This means that functions
141 can be written to handle the general case for linked lists. */
142 typedef struct g_list {
143 struct g_list *next;
144 } GENERIC_LIST;
146 /* Here is a generic structure for associating character strings
147 with integers. It is used in the parser for shell tokenization. */
148 typedef struct {
149 char *word;
150 int token;
151 } STRING_INT_ALIST;
153 /* A macro to avoid making an unnecessary function call. */
154 #define REVERSE_LIST(list, type) \
155 ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
156 : (type)(list))
158 #if __GNUC__ > 1
159 # define FASTCOPY(s, d, n) __builtin_memcpy ((d), (s), (n))
160 #else /* !__GNUC__ */
161 # if !defined (HAVE_BCOPY)
162 # if !defined (HAVE_MEMMOVE)
163 # define FASTCOPY(s, d, n) memcpy ((d), (s), (n))
164 # else
165 # define FASTCOPY(s, d, n) memmove ((d), (s), (n))
166 # endif /* !HAVE_MEMMOVE */
167 # else /* HAVE_BCOPY */
168 # define FASTCOPY(s, d, n) bcopy ((s), (d), (n))
169 # endif /* HAVE_BCOPY */
170 #endif /* !__GNUC__ */
172 /* String comparisons that possibly save a function call each. */
173 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
174 #define STREQN(a, b, n) ((n == 0) ? (1) \
175 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
177 /* More convenience definitions that possibly save system or libc calls. */
178 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
179 #define FREE(s) do { if (s) free (s); } while (0)
180 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
182 /* A fairly hairy macro to check whether an allocated string has more room,
183 and to resize it using xrealloc if it does not.
184 STR is the string (char *)
185 CIND is the current index into the string (int)
186 ROOM is the amount of additional room we need in the string (int)
187 CSIZE is the currently-allocated size of STR (int)
188 SINCR is how much to increment CSIZE before calling xrealloc (int) */
190 #define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
191 do { \
192 if ((cind) + (room) >= csize) \
194 while ((cind) + (room) >= csize) \
195 csize += (sincr); \
196 str = xrealloc (str, csize); \
198 } while (0)
200 /* Function pointers can be declared as (Function *)foo. */
201 #if !defined (_FUNCTION_DEF)
202 # define _FUNCTION_DEF
203 typedef int Function ();
204 typedef void VFunction ();
205 typedef char *CPFunction (); /* no longer used */
206 typedef char **CPPFunction (); /* no longer used */
207 #endif /* _FUNCTION_DEF */
209 #ifndef SH_FUNCTION_TYPEDEF
210 # define SH_FUNCTION_TYPEDEF
212 /* Shell function typedefs with prototypes */
213 /* `Generic' function pointer typedefs */
215 typedef int sh_intfunc_t PARAMS((int));
216 typedef int sh_ivoidfunc_t PARAMS((void));
217 typedef int sh_icpfunc_t PARAMS((char *));
218 typedef int sh_icppfunc_t PARAMS((char **));
219 typedef int sh_iptrfunc_t PARAMS((PTR_T));
221 typedef void sh_voidfunc_t PARAMS((void));
222 typedef void sh_vintfunc_t PARAMS((int));
223 typedef void sh_vcpfunc_t PARAMS((char *));
224 typedef void sh_vcppfunc_t PARAMS((char **));
225 typedef void sh_vptrfunc_t PARAMS((PTR_T));
227 typedef int sh_wdesc_func_t PARAMS((WORD_DESC *));
228 typedef int sh_wlist_func_t PARAMS((WORD_LIST *));
230 typedef int sh_glist_func_t PARAMS((GENERIC_LIST *));
232 typedef char *sh_string_func_t PARAMS((char *)); /* like savestring, et al. */
234 typedef int sh_msg_func_t PARAMS((const char *, ...)); /* printf(3)-like */
235 typedef void sh_vmsg_func_t PARAMS((const char *, ...)); /* printf(3)-like */
237 /* Specific function pointer typedefs. Most of these could be done
238 with #defines. */
239 typedef void sh_sv_func_t PARAMS((char *)); /* sh_vcpfunc_t */
240 typedef void sh_free_func_t PARAMS((PTR_T)); /* sh_vptrfunc_t */
241 typedef void sh_resetsig_func_t PARAMS((int)); /* sh_vintfunc_t */
243 typedef int sh_ignore_func_t PARAMS((const char *)); /* sh_icpfunc_t */
245 typedef int sh_assign_func_t PARAMS((const char *));
246 typedef int sh_wassign_func_t PARAMS((WORD_DESC *, int));
248 typedef int sh_load_func_t PARAMS((char *));
249 typedef void sh_unload_func_t PARAMS((char *));
251 typedef int sh_builtin_func_t PARAMS((WORD_LIST *)); /* sh_wlist_func_t */
253 #endif /* SH_FUNCTION_TYPEDEF */
255 #define NOW ((time_t) time ((time_t *) 0))
256 #define GETTIME(tv) gettimeofday(&(tv), NULL)
258 /* Some defines for calling file status functions. */
259 #define FS_EXISTS 0x1
260 #define FS_EXECABLE 0x2
261 #define FS_EXEC_PREFERRED 0x4
262 #define FS_EXEC_ONLY 0x8
263 #define FS_DIRECTORY 0x10
264 #define FS_NODIRS 0x20
265 #define FS_READABLE 0x40
267 /* Default maximum for move_to_high_fd */
268 #define HIGH_FD_MAX 256
270 /* The type of function passed as the fourth argument to qsort(3). */
271 #ifdef __STDC__
272 typedef int QSFUNC (const void *, const void *);
273 #else
274 typedef int QSFUNC ();
275 #endif
277 /* Some useful definitions for Unix pathnames. Argument convention:
278 x == string, c == character */
280 #if !defined (__CYGWIN__)
281 # define ABSPATH(x) ((x)[0] == '/')
282 # define RELPATH(x) ((x)[0] != '/')
283 #else /* __CYGWIN__ */
284 # define ABSPATH(x) (((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
285 # define RELPATH(x) (ABSPATH(x) == 0)
286 #endif /* __CYGWIN__ */
288 #define ROOTEDPATH(x) (ABSPATH(x))
290 #define DIRSEP '/'
291 #if !defined (__CYGWIN__)
292 # define ISDIRSEP(c) ((c) == '/')
293 #else
294 # define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
295 #endif /* __CYGWIN__ */
296 #define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
298 #define DOT_OR_DOTDOT(s) (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
299 #if defined (HANDLE_MULTIBYTE)
300 #define WDOT_OR_DOTDOT(w) (w[0] == L'.' && (w[1] == L'\0' || (w[1] == L'.' && w[2] == L'\0')))
301 #endif
303 #if 0
304 /* Declarations for functions defined in xmalloc.c */
305 extern PTR_T xmalloc PARAMS((size_t));
306 extern PTR_T xrealloc PARAMS((void *, size_t));
307 extern void xfree PARAMS((void *));
308 #endif
310 /* Declarations for functions defined in general.c */
311 extern void posix_initialize PARAMS((int));
313 extern int num_posix_options PARAMS((void));
314 extern char *get_posix_options PARAMS((char *));
315 extern void set_posix_options PARAMS((const char *));
317 extern void save_posix_options PARAMS((void));
319 #if defined (RLIMTYPE)
320 extern RLIMTYPE string_to_rlimtype PARAMS((char *));
321 extern void print_rlimtype PARAMS((RLIMTYPE, int));
322 #endif
324 extern int all_digits PARAMS((const char *));
325 extern int legal_number PARAMS((const char *, intmax_t *));
326 extern int legal_identifier PARAMS((const char *));
327 extern int importable_function_name PARAMS((const char *, size_t));
328 extern int exportable_function_name PARAMS((const char *));
329 extern int check_identifier PARAMS((WORD_DESC *, int));
330 extern int valid_nameref_value PARAMS((const char *, int));
331 extern int check_selfref PARAMS((const char *, char *, int));
332 extern int legal_alias_name PARAMS((const char *, int));
333 extern int line_isblank PARAMS((const char *));
334 extern int assignment PARAMS((const char *, int));
336 extern int sh_unset_nodelay_mode PARAMS((int));
337 extern int sh_setclexec PARAMS((int));
338 extern int sh_validfd PARAMS((int));
339 extern int fd_ispipe PARAMS((int));
340 extern void check_dev_tty PARAMS((void));
341 extern int move_to_high_fd PARAMS((int, int, int));
342 extern int check_binary_file PARAMS((const char *, int));
344 #ifdef _POSIXSTAT_H_
345 extern int same_file PARAMS((const char *, const char *, struct stat *, struct stat *));
346 #endif
348 extern int sh_openpipe PARAMS((int *));
349 extern int sh_closepipe PARAMS((int *));
351 extern int file_exists PARAMS((const char *));
352 extern int file_isdir PARAMS((const char *));
353 extern int file_iswdir PARAMS((const char *));
354 extern int path_dot_or_dotdot PARAMS((const char *));
355 extern int absolute_pathname PARAMS((const char *));
356 extern int absolute_program PARAMS((const char *));
358 extern char *make_absolute PARAMS((const char *, const char *));
359 extern char *base_pathname PARAMS((char *));
360 extern char *full_pathname PARAMS((char *));
361 extern char *polite_directory_format PARAMS((char *));
362 extern char *trim_pathname PARAMS((char *, int));
363 extern char *printable_filename PARAMS((char *, int));
365 extern char *extract_colon_unit PARAMS((char *, int *));
367 extern void tilde_initialize PARAMS((void));
368 extern char *bush_tilde_find_word PARAMS((const char *, int, int *));
369 extern char *bush_tilde_expand PARAMS((const char *, int));
371 extern int group_member PARAMS((gid_t));
372 extern char **get_group_list PARAMS((int *));
373 extern int *get_group_array PARAMS((int *));
375 extern char *conf_standard_path PARAMS((void));
376 extern int default_columns PARAMS((void));
378 #endif /* _GENERAL_H_ */