File: pipe.h

package info (click to toggle)
cssc 1.0.1-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,620 kB
  • ctags: 1,424
  • sloc: cpp: 13,500; sh: 4,759; ansic: 2,971; perl: 342; makefile: 342; awk: 11
file content (194 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * pipe.h: Part of GNU CSSC.
 * 
 * 
 *    Copyright (C) 1997,1998 Free Software Foundation, Inc. 
 * 
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 * 
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
 * 
 * CSSC was originally Based on MySC, by Ross Ridge, which was 
 * placed in the Public Domain.
 *
 *
 * Defines the class Pipe.
 */

#ifndef CSSC__PIPE_H__
#define CSSC__PIPE_H__

#include <stdio.h>

#ifdef __GNUC__
#pragma interface
#endif

/* One of two definitions of the class Pipe are used depending if the
   system supports pipes (and fork) or not.  If pipes are not supported
   then a temporary file used to emulate one.  The members must be
   called in order: constructor, write_stream(), write_close(),
   read_stream(), read_close(), destructor. */

/*
 * In order to use pipe(), we must have it and we must
 * also have fork().
 */
#define USE_PIPE
#ifndef HAVE_FORK
#undef  USE_PIPE
#endif
#ifndef HAVE_PIPE
#undef  USE_PIPE
#endif


/* We want the code for variour systems to be as consistent as 
 * possible so I think we should discard the fork-and-pipe method,
 * and use temporary files instead.   However the no-pipe code seems
 * not to work perfectly.  Perhaps I should rewrite it.
 */
/* #undef USE_PIPE */


#ifndef USE_PIPE

class Pipe : private cleanup {
	friend int run_diff(const char *gname, Pipe &in, Pipe &out);

	FILE *f;
	int fd;
	mystring name;

	void do_cleanup();

public:
	Pipe();

	FILE *write_stream() { return f; }
	void write_close() { fclose(f); }
	FILE *read_stream();
	int read_close();
	~Pipe() { ASSERT(f == NULL); }
};

#else /*  HAVE_PIPE */

/* Definition of the class wait_pid.  It handles the problem of
   other child processes exiting while waiting a for a specific
   child process to exit. */

class wait_pid {
	pid_t pid;
	int reaped;
	int status;
	class wait_pid *next;
	static class wait_pid *head;

	void
	link(pid_t p) {
		pid = p;
		if (p != (pid_t)-1 && p != 0) {
			reaped = 0;
			next = head;
			head = this;
		}
	}

	void unlink();

	void operator =(wait_pid const &);

public:
	wait_pid(): pid(-1) {};

	wait_pid(int p) { link(p); }

	int
	operator =(int p) {
		ASSERT(pid == (pid_t)-1);
		link(p);
		return p;
	}

	operator pid_t() {
		return pid;
	}

	int wait();

	~wait_pid() {
		if (pid != (pid_t)-1 && pid != 0) {
			unlink();
		}
	}
};

class Pipe {
	friend int run_diff(const char *gname, Pipe &in, Pipe &out);

	int fd;
	wait_pid pid;
	int child;
	FILE *f;

	static NORETURN _exit(int) POSTDECL_NORETURN;

public:
	Pipe();

	FILE *
	write_stream() {
		if (child) {
			return f;
		} else {
			return NULL;
		}
	}

	void
	write_close() {
		if (child) {
			fclose(f);
			_exit(0);
		}
	}

	FILE *
	read_stream() {
		ASSERT(!child);
		return f;
	}

	int
	read_close() {
		ASSERT(!child);
		fclose(f);
		f = NULL;
		return pid.wait();
	}


	~Pipe() {
		ASSERT(!child);
		ASSERT(f == NULL);
	}
};	

#endif /* HAVE_PIPE */

#endif /* __PIPE_H__ */

/* Local variables: */
/* mode: c++ */
/* End: */