1 /****************************************************************************
3 ** Program: pipe-handler - an AmigaDOS handler for named pipes
5 ** Author: Ed Puckett qix@mit-oz
7 ** Copyright 1987 by EpAc Software. All Rights Reserved.
9 ** History: 05-Jan-87 Original Version (1.0)
11 #include <string.h> // For strlen
12 #include <stdio.h> // For snprintf
14 #include <libraries/dos.h>
15 #include <libraries/dosextens.h>
16 #include <exec/exec.h>
18 #include <proto/exec.h>
19 #include <proto/dos.h>
20 #include <proto/alib.h>
22 #include "pipedebug.h"
26 /*---------------------------------------------------------------------------
29 ** This module contains debugging functions. In need only be included if the
30 ** other modules are compiled with DEBUG defined.
34 ** int InitDebugIO (NodePri)
35 ** void CleanupDebugIO ()
36 ** BPTR DebugOpen (name, mode)
37 ** void DebugClose (fh)
38 ** int DebugWrite (fh, buf, len)
39 ** void OutStr (str, fh)
40 ** void OutLONG (n, fh)
42 ** Macros (in pipedebug.h)
43 ** -----------------------
50 ** void DebugIO (Handler, Type, Arg1, Arg2, Arg3)
54 #define BPTRtoCptr(Bp) BADDR(Bp)
55 #define CptrtoBPTR(Cp) MKBADDR(Cp)
57 #define BPTRtoCptr(Bp) ((char *) ((ULONG) (Bp) << 2))
58 #define CptrtoBPTR(Cp) ((BPTR) ((ULONG) (Cp) >> 2))
62 #define MEMFLAGS (MEMF_PUBLIC | MEMF_CLEAR)
66 static struct MsgPort
*DebugDOSPort
= NULL
;
67 static struct Message
*DebugMsg
= NULL
;
68 static struct DosPacket
*DebugPkt
= NULL
;
71 static void DebugIO(struct MsgPort
*Handler
, SIPTR Type
, SIPTR Arg1
, SIPTR Arg2
,SIPTR Arg3
);
75 /*---------------------------------------------------------------------------
76 ** InitDebugIO() allocates things for the debugging functions, and opens a
77 ** window for output. It MUST be called before any of the I/O operations
78 ** are used. CleanupDebugIO() frees the resources allocated here, and closes
80 ** The routines DebugOpen(), DebugClose(), and DebugWrite() mimic their
81 ** corresponding DOS functions, except they use a private reply port, not
82 ** the process' DOS port. DOS does bad things if a handler request comes in
83 ** while it is waiting or a reply to one of its requests made on your behalf.
84 ** The return value is nonzero iff no error occurred.
87 int InitDebugIO (NodePri
)
97 if ( ((DebugDOSPort
= CreatePort (NULL
, NodePri
)) == NULL
) ||
98 ((DebugMsg
= (struct Message
*) AllocMem (sizeof (struct Message
), MEMFLAGS
)) == NULL
) ||
99 ((DebugPkt
= (struct DosPacket
*) AllocMem (sizeof (struct DosPacket
), MEMFLAGS
)) == NULL
) ||
100 ((DebugFH
= DebugOpen (DEBUG_CON_NAME
, MODE_NEWFILE
)) == 0) )
110 /*---------------------------------------------------------------------------
111 ** Cleanup things allocated by InitDebugIO, and close the window.
114 void CleanupDebugIO ()
119 DebugClose (DebugFH
);
121 if (DebugPkt
!= NULL
)
122 FreeMem (DebugPkt
, sizeof (struct DosPacket
));
124 if (DebugMsg
!= NULL
)
125 FreeMem (DebugMsg
, sizeof (struct Message
));
127 if (DebugDOSPort
!= NULL
)
128 { FreeSignal (DebugDOSPort
->mp_SigBit
);
129 FreeMem (DebugDOSPort
, sizeof (struct MsgPort
));
135 /*---------------------------------------------------------------------------
136 ** DebugOpen() performs just like the DOS function Open().
137 ** InitDebugIO() MUST have been called and returned successful before calling
141 BPTR
DebugOpen (name
, mode
)
148 struct MsgPort
*HandlerPID
;
149 struct FileLock
*Lock
;
150 struct FileHandle
*handle
;
152 Bname
= MKBADDR(AllocVec(strlen(name
) + 1, MEMF_ANY
));
156 CopyMem(name
, AROS_BSTR_ADDR(Bname
), strlen(name
));
157 AROS_BSTR_setstrlen(Bname
, strlen(name
));
159 HandlerPID
= DeviceProc (name
);
160 if (HandlerPID
== NULL
) {
161 FreeVec(BADDR(Bname
));
165 Lock
= (struct FileLock
*) IoErr ();
168 if ((handle
= (struct FileHandle
*) AllocMem (sizeof (struct FileHandle
), MEMFLAGS
)) == NULL
) {
169 FreeVec(BADDR(Bname
));
175 handle
->fh_Type
= HandlerPID
;
178 DebugIO (HandlerPID
, mode
, (SIPTR
)CptrtoBPTR (handle
), (SIPTR
)CptrtoBPTR (Lock
), (SIPTR
)Bname
);
179 FreeVec(BADDR(Bname
));
181 if (DebugPkt
->dp_Res1
== 0)
182 { FreeMem (handle
, sizeof (struct FileHandle
));
186 return CptrtoBPTR (handle
);
191 /*---------------------------------------------------------------------------
192 ** DebugClose() performs just like the DOS function Close().
193 ** InitDebugIO() MUST have been called and returned successful before calling
201 { struct FileHandle
*handle
;
203 handle
= (struct FileHandle
*) BPTRtoCptr (fh
);
204 DebugIO (handle
->fh_Type
, 1007, handle
->fh_Arg1
, 0, 0);
205 FreeMem (handle
, sizeof (struct FileHandle
));
210 /*---------------------------------------------------------------------------
211 ** DebugWrite() performs just like the DOS function Write().
212 ** InitDebugIO() MUST have been called and returned successful before calling
216 int DebugWrite (fh
, buf
, len
)
222 { struct FileHandle
*handle
;
224 handle
= (struct FileHandle
*) BPTRtoCptr (fh
);
225 DebugIO (handle
->fh_Type
, ACTION_WRITE
, handle
->fh_Arg1
, (SIPTR
)buf
, len
);
226 return DebugPkt
->dp_Res1
;
231 /*---------------------------------------------------------------------------
232 ** DebugIO() sets up the DosPacket with the specified information, initiates
233 ** the request, and waits for the reply.
236 static void DebugIO (Handler
, Type
, Arg1
, Arg2
, Arg3
)
238 struct MsgPort
*Handler
;
245 DebugMsg
->mn_ReplyPort
= DebugDOSPort
;
246 DebugMsg
->mn_Node
.ln_Type
= NT_MESSAGE
;
247 DebugMsg
->mn_Node
.ln_Name
= (char *) DebugPkt
;
249 DebugPkt
->dp_Link
= DebugMsg
;
250 DebugPkt
->dp_Port
= DebugDOSPort
;
251 DebugPkt
->dp_Type
= Type
;
252 DebugPkt
->dp_Arg1
= Arg1
;
253 DebugPkt
->dp_Arg2
= Arg2
;
254 DebugPkt
->dp_Arg3
= Arg3
;
256 PutMsg (Handler
, DebugMsg
);
257 (void) WaitPort (DebugDOSPort
);
258 (void) GetMsg (DebugDOSPort
); /* assume it is DebugMsg */
263 /*---------------------------------------------------------------------------
264 ** OutStr() outputs the null-terminated string "str" to the filehandle "fh".
267 void OutStr (str
, fh
)
273 DebugWrite (fh
, str
, strlen (str
));
278 /*---------------------------------------------------------------------------
279 ** OutLONG() outputs the hex representaion of "n" to the filehandle "fh".
280 ** The conversion function stcu_d() is used -- this may not be available
281 ** on all systems. In that case, such a function will need to be written.
291 int i
= sizeof(buff
)-1;
294 for (buff
[i
] = '0'; n
> 0; i
--) {
295 buff
[i
] = "0123456789abcdef"[n
& 0xf];