1/* general.h -- defines that everybody likes to use. */
2
3/* Copyright (C) 1993-2004 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21#if !defined (_GENERAL_H_)
22#define _GENERAL_H_
23
24#include "stdc.h"
25
26#include "bashtypes.h"
27#include "chartypes.h"
28
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
35
36#if defined (HAVE_STRING_H)
37#  include <string.h>
38#else
39#  include <strings.h>
40#endif /* !HAVE_STRING_H */
41
42#if defined (HAVE_LIMITS_H)
43#  include <limits.h>
44#endif
45
46#include "xmalloc.h"
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/* Hardly used anymore */
58#define pointer_to_int(x)	(int)((char *)x - (char *)0)
59
60#if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
61extern char *strchr (), *strrchr ();
62#endif
63
64#if !defined (strcpy) && (defined (HAVE_DECL_STRCPY) && !HAVE_DECL_STRCPY)
65extern char *strcpy __P((char *, const char *));
66#endif
67
68#if !defined (savestring)
69#  define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
70#endif
71
72#ifndef member
73#  define member(c, s) ((c) ? ((char *)xstrchr ((s), (c)) != (char *)NULL) : 0)
74#endif
75
76#ifndef whitespace
77#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
78#endif
79
80#ifndef CHAR_MAX
81#  ifdef __CHAR_UNSIGNED__
82#    define CHAR_MAX	0xff
83#  else
84#    define CHAR_MAX	0x7f
85#  endif
86#endif
87
88#ifndef CHAR_BIT
89#  define CHAR_BIT 8
90#endif
91
92/* Nonzero if the integer type T is signed.  */
93#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
94
95/* Bound on length of the string representing an integer value of type T.
96   Subtract one for the sign bit if T is signed;
97   302 / 1000 is log10 (2) rounded up;
98   add one for integer division truncation;
99   add one more for a minus sign if t is signed.  */
100#define INT_STRLEN_BOUND(t) \
101  ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
102   + 1 + TYPE_SIGNED (t))
103
104
105/* Define exactly what a legal shell identifier consists of. */
106#define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
107#define legal_variable_char(c)	(ISALNUM(c) || c == '_')
108
109/* Definitions used in subst.c and by the `read' builtin for field
110   splitting. */
111#define spctabnl(c)	((c) == ' ' || (c) == '\t' || (c) == '\n')
112#define posix_whitespace(c)   (spctabnl(c) || (c) == '\r' || (c) == '\f' || (c) == '\v')
113
114/* All structs which contain a `next' field should have that field
115   as the first field in the struct.  This means that functions
116   can be written to handle the general case for linked lists. */
117typedef struct g_list {
118  struct g_list *next;
119} GENERIC_LIST;
120
121/* Here is a generic structure for associating character strings
122   with integers.  It is used in the parser for shell tokenization. */
123typedef struct {
124  char *word;
125  int token;
126} STRING_INT_ALIST;
127
128/* A macro to avoid making an uneccessary function call. */
129#define REVERSE_LIST(list, type) \
130  ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \
131			: (type)(list))
132
133#if __GNUC__ > 1
134#  define FASTCOPY(s, d, n)  __builtin_memcpy ((d), (s), (n))
135#else /* !__GNUC__ */
136#  if !defined (HAVE_BCOPY)
137#    if !defined (HAVE_MEMMOVE)
138#      define FASTCOPY(s, d, n)  memcpy ((d), (s), (n))
139#    else
140#      define FASTCOPY(s, d, n)  memmove ((d), (s), (n))
141#    endif /* !HAVE_MEMMOVE */
142#  else /* HAVE_BCOPY */
143#    define FASTCOPY(s, d, n)  bcopy ((s), (d), (n))
144#  endif /* HAVE_BCOPY */
145#endif /* !__GNUC__ */
146
147/* String comparisons that possibly save a function call each. */
148#define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
149#define STREQN(a, b, n) ((n == 0) ? (1) \
150				  : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
151
152/* More convenience definitions that possibly save system or libc calls. */
153#define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
154#define FREE(s)  do { if (s) free (s); } while (0)
155#define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
156
157/* A fairly hairy macro to check whether an allocated string has more room,
158   and to resize it using xrealloc if it does not.
159   STR is the string (char *)
160   CIND is the current index into the string (int)
161   ROOM is the amount of additional room we need in the string (int)
162   CSIZE is the currently-allocated size of STR (int)
163   SINCR is how much to increment CSIZE before calling xrealloc (int) */
164
165#define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
166  do { \
167    if ((cind) + (room) >= csize) \
168      { \
169	while ((cind) + (room) >= csize) \
170	  csize += (sincr); \
171	str = xrealloc (str, csize); \
172      } \
173  } while (0)
174
175/* Function pointers can be declared as (Function *)foo. */
176#if !defined (_FUNCTION_DEF)
177#  define _FUNCTION_DEF
178typedef int Function ();
179typedef void VFunction ();
180typedef char *CPFunction ();		/* no longer used */
181typedef char **CPPFunction ();		/* no longer used */
182#endif /* _FUNCTION_DEF */
183
184#ifndef SH_FUNCTION_TYPEDEF
185#  define SH_FUNCTION_TYPEDEF
186
187/* Shell function typedefs with prototypes */
188/* `Generic' function pointer typedefs */
189
190typedef int sh_intfunc_t __P((int));
191typedef int sh_ivoidfunc_t __P((void));
192typedef int sh_icpfunc_t __P((char *));
193typedef int sh_icppfunc_t __P((char **));
194typedef int sh_iptrfunc_t __P((PTR_T));
195
196typedef void sh_voidfunc_t __P((void));
197typedef void sh_vintfunc_t __P((int));
198typedef void sh_vcpfunc_t __P((char *));
199typedef void sh_vcppfunc_t __P((char **));
200typedef void sh_vptrfunc_t __P((PTR_T));
201
202typedef int sh_wdesc_func_t __P((WORD_DESC *));
203typedef int sh_wlist_func_t __P((WORD_LIST *));
204
205typedef int sh_glist_func_t __P((GENERIC_LIST *));
206
207typedef char *sh_string_func_t __P((char *));	/* like savestring, et al. */
208
209typedef int sh_msg_func_t __P((const char *, ...));	/* printf(3)-like */
210typedef void sh_vmsg_func_t __P((const char *, ...));	/* printf(3)-like */
211
212/* Specific function pointer typedefs.  Most of these could be done
213   with #defines. */
214typedef void sh_sv_func_t __P((char *));	/* sh_vcpfunc_t */
215typedef void sh_free_func_t __P((PTR_T));	/* sh_vptrfunc_t */
216typedef void sh_resetsig_func_t __P((int));	/* sh_vintfunc_t */
217
218typedef int sh_ignore_func_t __P((const char *));	/* sh_icpfunc_t */
219
220typedef int sh_assign_func_t __P((const char *));	/* sh_icpfunc_t */
221typedef int sh_wassign_func_t __P((WORD_DESC *));
222
223typedef int sh_builtin_func_t __P((WORD_LIST *)); /* sh_wlist_func_t */
224
225#endif /* SH_FUNCTION_TYPEDEF */
226
227#define NOW	((time_t) time ((time_t *) 0))
228
229/* Some defines for calling file status functions. */
230#define FS_EXISTS	  0x1
231#define FS_EXECABLE	  0x2
232#define FS_EXEC_PREFERRED 0x4
233#define FS_EXEC_ONLY	  0x8
234#define FS_DIRECTORY	  0x10
235#define FS_NODIRS	  0x20
236#define FS_READABLE	  0x40
237
238/* Default maximum for move_to_high_fd */
239#define HIGH_FD_MAX	256
240
241/* The type of function passed as the fourth argument to qsort(3). */
242#ifdef __STDC__
243typedef int QSFUNC (const void *, const void *);
244#else
245typedef int QSFUNC ();
246#endif
247
248/* Some useful definitions for Unix pathnames.  Argument convention:
249   x == string, c == character */
250
251#if !defined (__CYGWIN__)
252#  define ABSPATH(x)	((x)[0] == '/')
253#  define RELPATH(x)	((x)[0] != '/')
254#else /* __CYGWIN__ */
255#  define ABSPATH(x)	(((x)[0] && ISALPHA((unsigned char)(x)[0]) && (x)[1] == ':') || ISDIRSEP((x)[0]))
256#  define RELPATH(x)	(ABSPATH(x) == 0)
257#endif /* __CYGWIN__ */
258
259#define ROOTEDPATH(x)	(ABSPATH(x))
260
261#define DIRSEP	'/'
262#if !defined (__CYGWIN__)
263#  define ISDIRSEP(c)	((c) == '/')
264#else
265#  define ISDIRSEP(c)	((c) == '/' || (c) == '\\')
266#endif /* __CYGWIN__ */
267#define PATHSEP(c)	(ISDIRSEP(c) || (c) == 0)
268
269#if 0
270/* Declarations for functions defined in xmalloc.c */
271extern PTR_T xmalloc __P((size_t));
272extern PTR_T xrealloc __P((void *, size_t));
273extern void xfree __P((void *));
274#endif
275
276/* Declarations for functions defined in general.c */
277extern void posix_initialize __P((int));
278
279#if defined (RLIMTYPE)
280extern RLIMTYPE string_to_rlimtype __P((char *));
281extern void print_rlimtype __P((RLIMTYPE, int));
282#endif
283
284extern int all_digits __P((char *));
285extern int legal_number __P((char *, intmax_t *));
286extern int legal_identifier __P((char *));
287extern int check_identifier __P((WORD_DESC *, int));
288extern int legal_alias_name __P((char *, int));
289extern int assignment __P((const char *, int));
290
291extern int sh_unset_nodelay_mode __P((int));
292extern int sh_validfd __P((int));
293extern void check_dev_tty __P((void));
294extern int move_to_high_fd __P((int, int, int));
295extern int check_binary_file __P((char *, int));
296
297#ifdef _POSIXSTAT_H_
298extern int same_file __P((char *, char *, struct stat *, struct stat *));
299#endif
300
301extern int file_isdir __P((char  *));
302extern int file_iswdir __P((char  *));
303extern int absolute_pathname __P((const char *));
304extern int absolute_program __P((const char *));
305
306extern char *make_absolute __P((char *, char *));
307extern char *base_pathname __P((char *));
308extern char *full_pathname __P((char *));
309extern char *polite_directory_format __P((char *));
310
311extern char *extract_colon_unit __P((char *, int *));
312
313extern void tilde_initialize __P((void));
314extern char *bash_tilde_find_word __P((const char *, int, int *));
315extern char *bash_tilde_expand __P((const char *, int));
316
317extern int group_member __P((gid_t));
318extern char **get_group_list __P((int *));
319extern int *get_group_array __P((int *));
320
321#endif	/* _GENERAL_H_ */
322