-
Notifications
You must be signed in to change notification settings - Fork 33
/
pi.c
418 lines (335 loc) · 8.5 KB
/
pi.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (c) 2018 Niklas Ekström
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/tasks.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <proto/alib.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "../a314device/a314.h"
#define PICMD_SERVICE_NAME "picmd"
#define ID_314_DISK (('3' << 24) | ('1' << 16) | ('4' << 8))
struct StartMsgHeader
{
UWORD length;
short rows;
short cols;
UBYTE component_count;
UBYTE arg_count;
};
static struct MsgPort *sync_mp;
static struct MsgPort *async_mp;
static struct A314_IORequest *read_ior;
static struct A314_IORequest *sync_ior;
static BPTR input_file;
static BPTR output_file;
static struct FileHandle *con;
static ULONG socket;
static UBYTE arbuf[256];
static struct StandardPacket sync_sp;
static struct StandardPacket wait_sp;
static BOOL pending_a314_read = FALSE;
static BOOL pending_con_wait = FALSE;
static BOOL stream_closed = FALSE;
static void put_con_sp(struct MsgPort *reply_port, struct StandardPacket *sp, LONG action, LONG arg1, LONG arg2, LONG arg3)
{
sp->sp_Msg.mn_Node.ln_Type = NT_MESSAGE;
sp->sp_Msg.mn_Node.ln_Pri = 0;
sp->sp_Msg.mn_Node.ln_Name = (char *)&(sp->sp_Pkt);
sp->sp_Msg.mn_Length = sizeof(struct StandardPacket);
sp->sp_Msg.mn_ReplyPort = reply_port;
sp->sp_Pkt.dp_Link = &(sp->sp_Msg);
sp->sp_Pkt.dp_Port = reply_port;
sp->sp_Pkt.dp_Type = action;
sp->sp_Pkt.dp_Arg1 = arg1;
sp->sp_Pkt.dp_Arg2 = arg2;
sp->sp_Pkt.dp_Arg3 = arg3;
PutMsg(con->fh_Type, &(sp->sp_Msg));
}
static LONG set_screen_mode(LONG mode)
{
put_con_sp(sync_mp, &sync_sp, ACTION_SCREEN_MODE, mode, 0, 0);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_sp.sp_Pkt.dp_Res1;
}
static void start_con_wait()
{
put_con_sp(async_mp, &wait_sp, ACTION_WAIT_CHAR, 100000, 0, 0);
pending_con_wait = TRUE;
}
static LONG con_read(char *s, int length)
{
return Read(input_file, s, length);
}
static LONG con_write(char *s, int length)
{
return Write(output_file, s, length);
}
static void start_a314_cmd(struct MsgPort *reply_port, struct A314_IORequest *ior, UWORD cmd, char *buffer, int length)
{
ior->a314_Request.io_Message.mn_ReplyPort = reply_port;
ior->a314_Request.io_Command = cmd;
ior->a314_Request.io_Error = 0;
ior->a314_Socket = socket;
ior->a314_Buffer = buffer;
ior->a314_Length = length;
SendIO((struct IORequest *)ior);
}
static BYTE a314_connect(char *name)
{
socket = time(NULL);
start_a314_cmd(sync_mp, sync_ior, A314_CONNECT, name, strlen(name));
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
static BYTE a314_write(char *buffer, int length)
{
start_a314_cmd(sync_mp, sync_ior, A314_WRITE, buffer, length);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
static BYTE a314_eos()
{
start_a314_cmd(sync_mp, sync_ior, A314_EOS, NULL, 0);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
static BYTE a314_reset()
{
start_a314_cmd(sync_mp, sync_ior, A314_RESET, NULL, 0);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
static void start_a314_read()
{
start_a314_cmd(async_mp, read_ior, A314_READ, arbuf, 255);
pending_a314_read = TRUE;
}
static void handle_con_wait_completed()
{
pending_con_wait = FALSE;
if (stream_closed)
return;
if (wait_sp.sp_Pkt.dp_Res1 == DOSFALSE)
{
start_con_wait();
}
else
{
unsigned char buf[64];
int len = con_read(buf, sizeof(buf));
if (len == 0 || len == -1)
{
a314_reset();
stream_closed = TRUE;
}
else
{
a314_write(buf, len);
start_con_wait();
}
}
}
static void handle_a314_read_completed()
{
pending_a314_read = FALSE;
if (stream_closed)
return;
int res = read_ior->a314_Request.io_Error;
if (res == A314_READ_OK)
{
UBYTE *p = read_ior->a314_Buffer;
int len = read_ior->a314_Length;
con_write(p, len);
start_a314_read();
}
else if (res == A314_READ_EOS)
{
a314_eos();
stream_closed = TRUE;
}
else if (res == A314_READ_RESET)
{
stream_closed = TRUE;
}
}
static void create_and_send_start_msg(BPTR current_dir, int argc, char **argv, short rows, short cols)
{
int buf_len = sizeof(struct StartMsgHeader);
int component_count = 0;
UBYTE *components[20];
if (current_dir != 0)
{
struct FileLock *fl = (struct FileLock *)BADDR(current_dir);
struct DeviceList *dl = (struct DeviceList *)BADDR(fl->fl_Volume);
if (dl->dl_DiskType == ID_314_DISK)
{
struct FileInfoBlock *fib = AllocMem(sizeof(struct FileInfoBlock), 0);
BPTR lock = DupLock(current_dir);
while (lock != 0)
{
if (Examine(lock, fib) == 0)
{
UnLock(lock);
break;
}
int n = strlen(fib->fib_FileName);
UBYTE *p = AllocMem(n + 1, 0);
p[0] = (UBYTE)n;
memcpy(&p[1], fib->fib_FileName, n);
components[component_count++] = p;
buf_len += n + 1;
BPTR child = lock;
lock = ParentDir(child);
UnLock(child);
}
FreeMem(fib, sizeof(struct FileInfoBlock));
}
}
for (int i = 1; i < argc; i++)
buf_len += strlen(argv[i]) + 1;
UBYTE *buffer = AllocMem(buf_len, 0);
struct StartMsgHeader *hdr = (struct StartMsgHeader *)buffer;
hdr->length = buf_len;
hdr->rows = rows;
hdr->cols = cols;
hdr->component_count = component_count;
hdr->arg_count = argc - 1;
UBYTE *p = buffer + sizeof(struct StartMsgHeader);
for (int i = 0; i < component_count; i++)
{
UBYTE *q = components[component_count - 1 - i];
int n = *q;
memcpy(p, q, n + 1);
p += n + 1;
FreeMem(q, n + 1);
}
for (int i = 1; i < argc; i++)
{
UBYTE *q = (UBYTE *)argv[i];
int n = strlen(q);
*p++ = (UBYTE)n;
memcpy(p, q, n);
p += n;
}
for (int i = 0; i < buf_len; i += 224)
{
int to_write = buf_len - i;
if (to_write > 224)
to_write = 224;
a314_write(buffer + i, to_write);
}
FreeMem(buffer, buf_len);
}
int main(int argc, char **argv)
{
struct Process *proc = (struct Process *)FindTask(NULL);
input_file = proc->pr_CIS;
output_file = proc->pr_COS;
if (!IsInteractive(input_file))
{
printf("Unable to handle redirected input\n");
goto fail0;
}
sync_mp = CreatePort(NULL, 0);
async_mp = CreatePort(NULL, 0);
sync_ior = (struct A314_IORequest *)CreateExtIO(sync_mp, sizeof(struct A314_IORequest));
read_ior = (struct A314_IORequest *)CreateExtIO(sync_mp, sizeof(struct A314_IORequest));
if (!sync_mp || !async_mp || !sync_ior || !read_ior)
{
printf("Unable to allocate enough memory\n");
goto fail1;
}
if (OpenDevice(A314_NAME, 0, (struct IORequest *)sync_ior, 0))
{
printf("Unable to open " A314_NAME "\n");
goto fail1;
}
memcpy(read_ior, sync_ior, sizeof(struct A314_IORequest));
if (a314_connect(PICMD_SERVICE_NAME) != A314_CONNECT_OK)
{
printf("Unable to connect to " PICMD_SERVICE_NAME " service\n");
goto fail2;
}
// The interactions with the console are described here:
// https://wiki.amigaos.net/wiki/Console_Device
con = (struct FileHandle *)BADDR(input_file);
set_screen_mode(DOSTRUE);
int rows = 25;
int cols = 80;
if (IsInteractive(output_file))
{
// Window Status Request
con_write("\x9b" "0 q", 4);
int len = con_read(arbuf, 32); // "\x9b" "1;1;33;77 r"
if (len < 10 || arbuf[len - 1] != 'r')
{
printf("Failure to receive window bounds report\n");
a314_reset();
goto fail3;
}
// Set Raw Events
con_write("\x9b" "12{", 4); // 12 = Window resized
int start = 5;
int ind = start;
while (arbuf[ind] != ';')
ind++;
arbuf[ind] = 0;
rows = atoi(arbuf + start);
ind++;
start = ind;
while (arbuf[ind] != ' ')
ind++;
arbuf[ind] = 0;
cols = atoi(arbuf + start);
}
create_and_send_start_msg(proc->pr_CurrentDir, argc, argv, (short)rows, (short)cols);
start_con_wait();
start_a314_read();
ULONG portsig = 1L << async_mp->mp_SigBit;
while (TRUE)
{
ULONG signal = Wait(portsig | SIGBREAKF_CTRL_C);
if (signal & portsig)
{
struct Message *msg;
while (msg = GetMsg(async_mp))
{
if (msg == (struct Message *)&wait_sp)
handle_con_wait_completed();
else if (msg == (struct Message *)read_ior)
handle_a314_read_completed();
}
}
if (stream_closed && !pending_a314_read && !pending_con_wait)
break;
}
fail3:
set_screen_mode(DOSFALSE);
fail2:
CloseDevice((struct IORequest *)sync_ior);
fail1:
if (read_ior)
DeleteExtIO((struct IORequest *)read_ior);
if (sync_ior)
DeleteExtIO((struct IORequest *)sync_ior);
if (async_mp)
DeletePort(async_mp);
if (sync_mp)
DeletePort(sync_mp);
fail0:
return 0;
}