-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobweb-pipes.cob
293 lines (253 loc) · 8.93 KB
/
cobweb-pipes.cob
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
GCobol >>SOURCE FORMAT IS FREE
>>IF docpass NOT DEFINED
*> ***************************************************************
*>****R* cobweb/cobweb-pipes
*> AUTHOR
*> Brian Tiffin, Simon Sobisch
*> DATE
*> 20150216, 20150417 Brian Tiffin
*> creation of cobweb-pipes
*> 20150417 Simon Sobisch
*> Added WIN_NO_POSIX for use in native Windows environments
*> use STATIC clause for C library calls
*> 20160908 Simon Sobisch
*> Added missing implementation for characters-read,
*> reset pipe-pointer on pclose, initialize fields
*> 20170201 Brian Tiffin
*> Fix bugs with static call and by content
*> LICENSE
*> GNU Lesser General Public License, LGPL, 3.0 (or greater)
*> PURPOSE
*> cobweb-pipes program. Read OR Write on most POSIX systems
*> pipe-open, pipe-read, pipe-write, pipe-close
*> TECTONICS
*> cobc -x program.cob cobweb-pipes.cob -debug
*> or (for WIN_NO_POSIX)
*> cobc -x program.cob cobweb-pipes.cob -debug -DWIN_NO_POSIX
*> SOURCE
*> ***************************************************************
identification division.
program-id. cobweb-pipes.
procedure division.
*> cobcrun default, display the repository
display " *> cobweb-pipes function repository" end-display
display " function pipe-open" end-display
display " function pipe-read" end-display
display " function pipe-write" end-display
display " function pipe-close" end-display
goback.
end program cobweb-pipes.
*>****
*> ***************************************************************
*>****F* cobweb-pipes/pipe-open
*> PURPOSE
*> Open a pipe channel, Read or Write
*> INPUTS
*> shell command, pic x any
*> pipe open mode, "r" or "w", not both on POSIX, but maybe Mac
*> OUTPUTS
*> pipe record, first field pointer
*> SOURCE
*> ***************************************************************
identification division.
function-id. pipe-open.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
>>IF WIN_NO_POSIX NOT DEFINED
78 popen value "popen".
REPLACE ==:STATIC:== BY ====.
>>ELSE
78 popen value "_popen".
REPLACE ==:STATIC:== BY ==static==.
>>END-IF
linkage section.
01 pipe-command pic x any length.
01 pipe-mode pic x any length.
01 pipe-record.
05 pipe-pointer usage pointer.
05 pipe-return usage binary-long.
*> ***************************************************************
procedure division using
pipe-command
pipe-mode
returning pipe-record.
call :STATIC: popen using
by reference concatenate(trim(pipe-command), x"00")
by reference concatenate(trim(pipe-mode), x"00")
returning pipe-pointer
on exception
display "link error: popen" upon syserr end-display
set pipe-pointer to NULL
move 255 to pipe-return
goback
end-call
if pipe-pointer equal null then
display "exec error: popen" upon syserr end-display
move 255 to pipe-return
goback
end-if
goback.
end function pipe-open.
*>****
*> ***************************************************************
*>****F* cobweb-pipes/pipe-read
*> PURPOSE
*> Read from a pipe.
*> INPUTS
*> pipe record, first field pointer
*> line buffer, pic x any
*> OUTPUTS
*> pipe record
*> c string, pointer, possibly NULL
*> length, integer, possibly 0.
*> SOURCE
*> ***************************************************************
identification division.
function-id. pipe-read.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 line-buffer-length usage binary-long.
linkage section.
01 pipe-record-in.
05 pipe-pointer usage pointer.
05 filler usage binary-long.
01 line-buffer pic x any length.
01 pipe-record-out.
05 pipe-read-status usage pointer.
05 characters-read usage binary-long.
*> ***************************************************************
procedure division using
pipe-record-in
line-buffer
returning pipe-record-out.
move spaces to line-buffer
move 0 to characters-read
move length(line-buffer) to line-buffer-length
call :STATIC: "fgets" using
by reference line-buffer
by value line-buffer-length
by value pipe-pointer
returning pipe-read-status
on exception
display "link error: fgets" upon syserr end-display
goback
end-call
if pipe-read-status not = NULL
inspect line-buffer
tallying characters-read for characters before x'00'
end-if
goback.
end function pipe-read.
*>****
*> ***************************************************************
*>****F* cobweb-pipes/pipe-write
*> PURPOSE
*> Write to a pipe channel
*> INPUTS
*> pipe record, first field pointer
*> line buffer
*> OUTPUTS
*> pipe record, first field pointer
*> SOURCE
*> ***************************************************************
identification division.
function-id. pipe-write.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 line-buffer-length usage binary-long.
linkage section.
01 pipe-record-in.
05 pipe-pointer usage pointer.
05 filler usage binary-long.
01 line-buffer pic x any length.
01 pipe-record-out.
05 filler usage pointer.
05 pipe-write-status usage binary-long.
*> ***************************************************************
procedure division using
pipe-record-in
line-buffer
returning pipe-record-out.
call :STATIC: "fputs" using
by reference concatenate(trim(line-buffer), x"00")
by value pipe-pointer
returning pipe-write-status
on exception
display "link error: fputs" upon syserr end-display
move 255 to pipe-write-status
goback
end-call
goback.
end function pipe-write.
*>****
*> ***************************************************************
*>****F* cobweb-pipes/pipe-close
*> PURPOSE
*> Close a pipe channel
*> INPUTS
*> pipe record, first field pointer
*> OUTPUTS
*> pipe close status, integer
*> SOURCE
*> ***************************************************************
identification division.
function-id. pipe-close.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
>>IF WIN_NO_POSIX NOT DEFINED
78 pclose value "pclose".
>>ELSE
78 pclose value "_pclose".
>>END-IF
linkage section.
01 pipe-record.
05 pipe-pointer usage pointer.
05 filler usage binary-long.
01 pclose-status usage binary-long.
*> ***************************************************************
procedure division using pipe-record returning pclose-status.
call :STATIC: pclose using
by value pipe-pointer
returning pclose-status
on exception
display "link error: pclose" upon syserr end-display
move 255 to pclose-status
goback
end-call
set pipe-pointer to NULL
goback.
end function pipe-close.
*>****
>>ELSE
==================
cobweb-pipes usage
==================
Introduction
------------
POSIX ``popen`` pipes.
Pass a command to the shell, and either read from the result
or write to the pipe line.
Read from a pipe-open(command, "r")
And write to a pipe-open(command, "w") pipe channel
See :manpage:`popen(3)`
Source
------
.. code-include:: cobweb-pipes.cob
:language: cobol
>>END-IF