]> git.ipfire.org Git - thirdparty/bash.git/blame - general.h
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / general.h
CommitLineData
726f6388
JA
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
bb70624e 19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
726f6388 20
ccc6cda3
JA
21#if !defined (_GENERAL_H_)
22#define _GENERAL_H_
726f6388
JA
23
24#include "stdc.h"
25
d166f048
JA
26#include "bashtypes.h"
27
cce855bc
JA
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
d166f048
JA
35#if defined (HAVE_STRING_H)
36# include <string.h>
37#else
38# include <strings.h>
39#endif /* !HAVE_STRING_H */
40
ccc6cda3
JA
41/* Generic pointer type. */
42#if defined (__STDC__)
43# define PTR_T void *
44#else
45# define PTR_T char *
726f6388
JA
46#endif
47
ccc6cda3 48/* NULL pointer type. */
726f6388
JA
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
726f6388
JA
57#define pointer_to_int(x) (int)((long)(x))
58
ccc6cda3
JA
59extern char *xmalloc (), *xrealloc ();
60
61#if defined (alpha) && defined (__GNUC__) && !defined (strchr) && !defined (__STDC__)
62extern char *strchr (), *strrchr ();
63#endif
64
65#if !defined (strcpy)
66extern char *strcpy ();
67#endif
68
726f6388 69#if !defined (savestring)
726f6388
JA
70# define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
71#endif
72
ccc6cda3
JA
73#ifndef member
74# define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
75#endif
76
726f6388
JA
77#ifndef whitespace
78#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
79#endif
80
81#ifndef digit
28ef6c31 82#define digit(c) (isdigit(c))
726f6388
JA
83#endif
84
85#ifndef isletter
28ef6c31 86#define isletter(c) (isalpha(c))
726f6388
JA
87#endif
88
89#ifndef digit_value
90#define digit_value(c) ((c) - '0')
91#endif
92
28ef6c31
JA
93#ifndef ISOCTAL
94#define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
95#endif
96
ccc6cda3
JA
97/* Define exactly what a legal shell identifier consists of. */
98#define legal_variable_starter(c) (isletter(c) || (c == '_'))
99#define legal_variable_char(c) (isletter (c) || digit (c) || c == '_')
100
726f6388
JA
101/* Definitions used in subst.c and by the `read' builtin for field
102 splitting. */
103#define spctabnl(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
104
726f6388
JA
105/* All structs which contain a `next' field should have that field
106 as the first field in the struct. This means that functions
107 can be written to handle the general case for linked lists. */
108typedef struct g_list {
109 struct g_list *next;
110} GENERIC_LIST;
111
112/* Here is a generic structure for associating character strings
113 with integers. It is used in the parser for shell tokenization. */
114typedef struct {
115 char *word;
116 int token;
117} STRING_INT_ALIST;
118
119/* A macro to avoid making an uneccessary function call. */
120#define REVERSE_LIST(list, type) \
ccc6cda3 121 ((list && list->next) ? (type)reverse_list ((GENERIC_LIST *)list) \
28ef6c31 122 : (type)(list))
726f6388
JA
123
124#if __GNUC__ > 1
125# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
126#else /* !__GNUC__ */
ccc6cda3
JA
127# if !defined (HAVE_BCOPY)
128# if !defined (HAVE_MEMMOVE)
726f6388
JA
129# define FASTCOPY(s, d, n) memcpy (d, s, n)
130# else
131# define FASTCOPY(s, d, n) memmove (d, s, n)
ccc6cda3
JA
132# endif /* !HAVE_MEMMOVE */
133# else /* HAVE_BCOPY */
726f6388 134# define FASTCOPY(s, d, n) bcopy (s, d, n)
ccc6cda3 135# endif /* HAVE_BCOPY */
726f6388
JA
136#endif /* !__GNUC__ */
137
138/* String comparisons that possibly save a function call each. */
139#define STREQ(a, b) ((a)[0] == (b)[0] && strcmp(a, b) == 0)
bb70624e
JA
140#define STREQN(a, b, n) ((n == 0) ? (1) \
141 : ((a)[0] == (b)[0] && strncmp(a, b, n) == 0))
726f6388
JA
142
143/* More convenience definitions that possibly save system or libc calls. */
144#define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
145#define FREE(s) do { if (s) free (s); } while (0)
cce855bc 146#define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
726f6388 147
ccc6cda3
JA
148/* A fairly hairy macro to check whether an allocated string has more room,
149 and to resize it using xrealloc if it does not.
150 STR is the string (char *)
151 CIND is the current index into the string (int)
152 ROOM is the amount of additional room we need in the string (int)
153 CSIZE is the currently-allocated size of STR (int)
154 SINCR is how much to increment CSIZE before calling xrealloc (int) */
155
156#define RESIZE_MALLOCED_BUFFER(str, cind, room, csize, sincr) \
157 do { \
158 if ((cind) + (room) >= csize) \
159 { \
160 while ((cind) + (room) >= csize) \
161 csize += (sincr); \
162 str = xrealloc (str, csize); \
163 } \
164 } while (0)
726f6388
JA
165
166/* Function pointers can be declared as (Function *)foo. */
ccc6cda3
JA
167#if !defined (_FUNCTION_DEF)
168# define _FUNCTION_DEF
726f6388
JA
169typedef int Function ();
170typedef void VFunction ();
171typedef char *CPFunction ();
172typedef char **CPPFunction ();
173#endif /* _FUNCTION_DEF */
174
175#define NOW ((time_t) time ((time_t *) 0))
176
177/* Some defines for calling file status functions. */
178#define FS_EXISTS 0x1
179#define FS_EXECABLE 0x2
180#define FS_EXEC_PREFERRED 0x4
181#define FS_EXEC_ONLY 0x8
ccc6cda3
JA
182#define FS_DIRECTORY 0x10
183#define FS_NODIRS 0x20
726f6388 184
28ef6c31
JA
185/* Some useful definitions for Unix pathnames. Argument convention:
186 x == string, c == character */
187
188#if !defined (__CYGWIN__)
189# define ABSPATH(x) ((x)[0] == '/')
190# define RELPATH(x) ((x)[0] != '/')
191#else /* __CYGWIN__ */
192# define ABSPATH(x) (((x)[0] && isalpha((x)[0]) && (x)[1] == ':' && (x)[2] == '/') || (x)[0] == '/')
193# define RELPATH(x) (!(x)[0] || ((x)[1] != ':' && (x)[0] != '/'))
194#endif /* __CYGWIN__ */
195
196#define ROOTEDPATH(x) (ABSPATH(x))
197
198#define DIRSEP '/'
199#define ISDIRSEP(c) ((c) == '/')
200#define PATHSEP(c) (ISDIRSEP(c) || (c) == 0)
201
ccc6cda3
JA
202/* Declarations for functions defined in xmalloc.c */
203extern char *xmalloc __P((size_t));
204extern char *xrealloc __P((void *, size_t));
bb70624e 205extern void xfree __P((void *));
726f6388 206
ccc6cda3
JA
207/* Declarations for functions defined in general.c */
208extern void posix_initialize __P((int));
726f6388 209
ccc6cda3
JA
210#if defined (RLIMTYPE)
211extern RLIMTYPE string_to_rlimtype __P((char *));
212extern void print_rlimtype __P((RLIMTYPE, int));
213#endif
726f6388 214
726f6388 215extern int all_digits __P((char *));
ccc6cda3 216extern int legal_number __P((char *, long *));
726f6388
JA
217extern int legal_identifier __P((char *));
218extern int check_identifier __P((WORD_DESC *, int));
ccc6cda3 219
28ef6c31 220extern int sh_unset_nodelay_mode __P((int));
ccc6cda3
JA
221extern void check_dev_tty __P((void));
222extern int same_file (); /* too many problems with prototype */
d166f048 223extern int move_to_high_fd __P((int, int, int));
ccc6cda3
JA
224extern int check_binary_file __P((unsigned char *, int));
225
726f6388
JA
226extern char *make_absolute __P((char *, char *));
227extern int absolute_pathname __P((char *));
228extern int absolute_program __P((char *));
229extern char *base_pathname __P((char *));
230extern char *full_pathname __P((char *));
726f6388 231extern char *polite_directory_format __P((char *));
726f6388 232
ccc6cda3 233extern char *extract_colon_unit __P((char *, int *));
726f6388 234
ccc6cda3
JA
235extern void tilde_initialize __P((void));
236extern char *bash_tilde_expand __P((char *));
726f6388 237
d166f048 238extern int group_member __P((gid_t));
d166f048 239extern char **get_group_list __P((int *));
b72432fd 240extern int *get_group_array __P((int *));
d166f048 241
ccc6cda3 242#endif /* _GENERAL_H_ */