Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ie/io.h
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1984-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* David Korn <[email protected]> *
18
* Pat Sullivan *
19
* *
20
***********************************************************************/
21
/*
22
* UNIX shell
23
* S. R. Bourne
24
* rewritten by David Korn
25
*
26
*/
27
28
#include <ast.h>
29
#include <error.h>
30
31
#include "sh_config.h"
32
#include <sys/stat.h>
33
#ifdef _hdr_unistd
34
# include <unistd.h>
35
#endif /* _hdr_unistd */
36
#ifdef _hdr_fcntl
37
# include <fcntl.h>
38
#endif /* _hdr_fcntl */
39
#ifndef O_CREAT
40
# ifdef _sys_file
41
# include <sys/file.h>
42
# endif /* _sys_file */
43
#endif /* O_CREAT */
44
45
#ifndef S_ISDIR
46
# define S_ISDIR(m) (((m)&S_IFMT)==S_IFDIR)
47
#endif /* S_ISDIR */
48
#ifndef S_ISREG
49
# define S_ISREG(m) (((m)&S_IFMT)==S_IFREG)
50
#endif /* S_ISREG */
51
#ifndef S_ISCHR
52
# define S_ISCHR(m) (((m)&S_IFMT)==S_IFCHR)
53
#endif /* S_ISCHR */
54
#ifndef S_ISBLK
55
# define S_ISBLK(m) (((m)&S_IFMT)==S_IFBLK)
56
#endif /* S_ISBLK */
57
#ifdef S_IFIFO
58
# ifndef S_ISFIFO
59
# define S_ISFIFO(m) (((m)&S_IFMT)==S_IFIFO)
60
# endif /* S_ISFIFO */
61
#endif /* S_IFIFO */
62
#ifndef S_IRUSR
63
# define S_IRUSR (S_IREAD)
64
#endif /* S_IRUSR */
65
#ifndef S_IWUSR
66
# define S_IWUSR (S_IWRITE)
67
#endif /* S_IWUSR */
68
#ifndef S_IXUSR
69
# define S_IXUSR (S_IEXEC)
70
#endif /* S_IXUSR */
71
#ifndef S_IRGRP
72
# define S_IRGRP (S_IREAD>>3)
73
#endif /* S_IRGRP */
74
#ifndef S_IWGRP
75
# define S_IWGRP (S_IWRITE>>3)
76
#endif /* S_IWGRP */
77
#ifndef S_IXGRP
78
# define S_IXGRP (S_IEXEC>>3)
79
#endif /* S_IXGRP */
80
#ifndef S_IROTH
81
# define S_IROTH (S_IREAD>>6)
82
#endif /* S_IROTH */
83
#ifndef S_IWOTH
84
# define S_IWOTH (S_IWRITE>>6)
85
#endif /* S_IWOTH */
86
#ifndef S_IXOTH
87
# define S_IXOTH (S_IEXEC>>6)
88
#endif /* S_IXOTH */
89
#define RW_ALL (S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH)
90
91
#ifndef NFILE
92
# define NFILE 20
93
#endif /* NFILE */
94
#ifndef IOBSIZE
95
# define IOBSIZE 1024
96
#endif /* IOBSIZE */
97
#define EOF (-1)
98
#define MAXTRY 12
99
#ifdef SEVENBIT
100
# define STRIP 0177
101
#else
102
# define STRIP 0377
103
#endif /* SEVENBIT */
104
105
/* used for input and output of shell */
106
#define TMPSIZ 20
107
#define ERRIO 2
108
#define USERIO 10
109
#define FCIO (NFILE-1) /* history file */
110
#ifdef KSHELL
111
# define INIO (NFILE-2) /* saved standard ioput */
112
# define TMPIO (NFILE-3) /* used for command substitution */
113
# define CINPIPE (NFILE-4) /* default inpipe for co-process */
114
# define CINPIPE2 (NFILE-5) /* other end of inpipe for co-process */
115
# define COTPIPE (NFILE-6) /* default output pipe for co-process */
116
# define MAXFILES (NFILE-USERIO) /* maximum number of saved open files */
117
118
# define F_STRING ((unsigned char)NFILE) /* file number for incore files */
119
# define F_INFINITE 0x7fff /* effectively infinite */
120
#endif /* KSHELL */
121
122
/* SHELL file I/O structure */
123
struct fileblk
124
{
125
char *ptr;
126
char *base;
127
char *last;
128
off_t fseek;
129
int flag;
130
unsigned char fdes;
131
#ifdef KSHELL
132
char ftype;
133
int flast;
134
char **feval;
135
struct fileblk *fstak;
136
unsigned flin;
137
#endif /* KSHELL */
138
};
139
140
#define filenum(fp) ((int)(fp->fdes)) /* file number */
141
#define fnobuff(fp) ((fp)->flag&IONBF) /* file is unbuffered */
142
143
#define IOREAD 0001
144
#define IOWRT 0002
145
#define IONBF 0004
146
#define IOFREE 0010
147
#define IOEOF 0020
148
#define IOERR 0040
149
#define IORW 0100
150
#define IOSLOW 0200
151
#define IOEDIT 0400
152
153
extern struct fileblk *io_ftable[NFILE+USERIO];
154
155
#ifdef FNDELAY
156
# ifdef EAGAIN
157
# if EAGAIN!=EWOULDBLOCK
158
# undef EAGAIN
159
# define EAGAIN EWOULDBLOCK
160
# endif
161
# else
162
# define EAGAIN EWOULDBLOCK
163
# endif /* EAGAIN */
164
# ifndef O_NONBLOCK
165
# define O_NONBLOCK FNDELAY
166
# endif /* !O_NONBLOCK */
167
#endif /* FNDELAY */
168
#ifndef O_CREAT
169
# define O_CREAT 0400
170
# define O_TRUNC 01000
171
# define O_APPEND 010
172
#endif /* O_CREAT */
173
#ifndef O_RDWR
174
# define O_RDONLY 0
175
# define O_WRONLY 01
176
# define O_RDWR 02
177
#endif /* O_RDWR */
178
#ifdef NOFCNTL
179
# define open myopen
180
# define F_DUPFD 0
181
# define F_GETFD 1
182
# define F_SETFD 2
183
#endif /* F_DUPFD */
184
#ifndef R_OK
185
# define F_OK 0 /* does file exist */
186
# define X_OK 1 /* is it executable by caller */
187
# define W_OK 2 /* writable by caller */
188
# define R_OK 4 /* readable by caller */
189
#endif /* R_OK */
190
#ifndef SEEK_SET
191
# define SEEK_SET 0 /* absolute offset */
192
# define SEEK_CUR 1 /* relative offset */
193
# define SEEK_END 2 /* EOF offset */
194
#endif /* SEEK_SET */
195
196
197
/*io nodes*/
198
#define INPIPE 0
199
#define OTPIPE 1
200
#define DUPFLG 0100
201
202
/*
203
* The remainder of this file is only used when compiled with shell
204
*/
205
206
#ifdef KSHELL
207
/* possible values for ftype */
208
#define F_ISSTRING 1
209
#define F_ISFILE 2
210
#define F_ISALIAS 3
211
#define F_ISEVAL 4
212
#define F_ISEVAL2 5
213
214
215
/* The following union is used for argument to sh_eval */
216
union io_eval
217
{
218
int fd;
219
char **com;
220
};
221
222
#define io_unreadc(c) (st.peekn |= (c)|MARK)
223
#define input (st.curin)
224
#define output (sh.curout)
225
#define newline() p_char(NL)
226
#define fisopen(fd) (io_access(fd,F_OK)==0)
227
#define fiswrite(fd) (io_access(fd,W_OK)==0)
228
#define fisread(fd) (io_access(fd,R_OK)==0)
229
#define fiseof(fp) ((fp)->flag&IOEOF)
230
#define fiserror(fp) ((fp)->flag&IOERR)
231
#define nextchar(fp) (*((fp)->ptr))
232
#define finbuff(fp) ((fp)->last - (fp)->ptr)
233
#ifndef clearerr
234
# define clearerr(fp) ((fp)->flag &= ~(IOERR|IOEOF))
235
#endif
236
237
struct filesave
238
{
239
short org_fd;
240
short dup_fd;
241
};
242
243
244
#ifdef PROTO
245
extern void io_clear(struct fileblk*);
246
extern void io_fclose(int);
247
extern int io_getc(int);
248
extern void io_init(int,struct fileblk*,char*);
249
extern int io_intr(struct fileblk*);
250
extern void io_push(struct fileblk*);
251
extern int io_pop(int);
252
extern int io_mktmp(char*);
253
extern off_t io_seek(int,off_t,int);
254
extern int io_readbuff(struct fileblk*);
255
extern int io_readc(void);
256
extern int io_renumber(int,int);
257
extern void io_sync(void);
258
extern int io_movefd(int);
259
extern void io_popen(int[]);
260
extern void io_pclose(int[]);
261
extern void io_restore(int);
262
struct ionod;
263
extern int io_redirect(struct ionod*,int);
264
extern void io_save(int,int);
265
extern void io_linkdoc(struct ionod*);
266
extern void io_swapdoc(struct ionod*);
267
extern int io_fopen(const char*);
268
extern void io_sopen(char*);
269
extern int io_access(int,int);
270
extern int io_nextc(void);
271
extern int ispipe(int);
272
#else
273
extern void io_clear();
274
extern void io_fclose();
275
extern int io_getc();
276
extern void io_init();
277
extern int io_intr();
278
extern void io_push();
279
extern int io_pop();
280
extern int io_mktmp();
281
extern off_t io_seek();
282
extern int io_readbuff();
283
extern int io_readc();
284
extern int io_renumber();
285
extern void io_sync();
286
extern int io_movefd();
287
extern void io_popen();
288
extern void io_pclose();
289
extern void io_restore();
290
extern int io_redirect();
291
extern void io_save();
292
extern void io_rmtemp();
293
extern void io_linkdoc();
294
extern void io_swapdoc();
295
extern int io_fopen();
296
extern void io_sopen();
297
extern int io_access();
298
extern int io_nextc();
299
extern int ispipe();
300
#endif /* PROTO */
301
extern void io_settemp();
302
303
extern char _sibuf[];
304
extern char _sobuf[];
305
extern struct fileblk io_stdin;
306
extern struct fileblk io_stdout;
307
extern char io_tmpname[];
308
309
/* the following are readonly */
310
extern const char e_create[];
311
extern const char e_file[];
312
extern const char e_open[];
313
extern const char e_pipe[];
314
extern const char e_flimit[];
315
extern const char e_fexists[];
316
extern const char e_unknown[];
317
extern const char e_endoffile[];
318
extern const char e_devnull[];
319
extern const char e_profile[];
320
extern const char e_suidprofile[];
321
extern const char e_sysprofile[];
322
extern const char e_devfdNN[];
323
#ifdef SUID_EXEC
324
extern const char e_suidexec[];
325
#endif /* SUID_EXEC */
326
#endif /* KSHELL */
327
328