Updated debug output.
[AROS.git] / workbench / fs / pipe / pipedebug.c
blob451da9330c3b45e970e9c292ec7b910f167a362c
1 /****************************************************************************
2 ** File: pipedebug.c
3 ** Program: pipe-handler - an AmigaDOS handler for named pipes
4 ** Version: 1.1
5 ** Author: Ed Puckett qix@mit-oz
6 **
7 ** Copyright 1987 by EpAc Software. All Rights Reserved.
8 **
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 /*---------------------------------------------------------------------------
27 ** pipedebug.c
28 ** -----------
29 ** This module contains debugging functions. In need only be included if the
30 ** other modules are compiled with DEBUG defined.
32 ** Visible Functions
33 ** -----------------
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 ** -----------------------
44 ** OS (s)
45 ** NL
46 ** OL (n)
48 ** Local Functions
49 ** ---------------
50 ** void DebugIO (Handler, Type, Arg1, Arg2, Arg3)
53 #ifdef BADDR
54 #define BPTRtoCptr(Bp) BADDR(Bp)
55 #define CptrtoBPTR(Cp) MKBADDR(Cp)
56 #else
57 #define BPTRtoCptr(Bp) ((char *) ((ULONG) (Bp) << 2))
58 #define CptrtoBPTR(Cp) ((BPTR) ((ULONG) (Cp) >> 2))
59 #endif
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;
69 BPTR DebugFH = 0;
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
79 ** the window.
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)
89 BYTE NodePri;
92 DebugDOSPort= NULL;
93 DebugMsg= NULL;
94 DebugPkt= NULL;
95 DebugFH= 0;
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) )
101 { CleanupDebugIO ();
102 return FALSE;
105 return TRUE;
110 /*---------------------------------------------------------------------------
111 ** Cleanup things allocated by InitDebugIO, and close the window.
114 void CleanupDebugIO ()
118 if (DebugFH != 0)
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
138 ** this function.
141 BPTR DebugOpen (name, mode)
143 char *name;
144 int mode;
147 BSTR Bname;
148 struct MsgPort *HandlerPID;
149 struct FileLock *Lock;
150 struct FileHandle *handle;
152 Bname = MKBADDR(AllocVec(strlen(name) + 1, MEMF_ANY));
153 if (!Bname)
154 return 0;
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));
162 return 0;
165 Lock= (struct FileLock *) IoErr ();
168 if ((handle= (struct FileHandle *) AllocMem (sizeof (struct FileHandle), MEMFLAGS)) == NULL) {
169 FreeVec(BADDR(Bname));
170 return 0;
173 handle->fh_Pos= -1;
174 handle->fh_End= -1;
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));
183 return 0;
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
194 ** this function.
197 void DebugClose (fh)
199 BPTR fh;
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
213 ** this function.
216 int DebugWrite (fh, buf, len)
218 BPTR fh;
219 BYTE *buf;
220 ULONG 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;
239 SIPTR Type;
240 SIPTR Arg1;
241 SIPTR Arg2;
242 SIPTR Arg3;
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)
269 char *str;
270 BPTR 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.
284 void OutLONG (n, fh)
286 ULONG n;
287 BPTR fh;
290 char buff[16];
291 int i = sizeof(buff)-1;
293 buff[i--] = 0;
294 for (buff[i] = '0'; n > 0; i--) {
295 buff[i] = "0123456789abcdef"[n & 0xf];
296 n >>= 4;
299 FPuts(fh, &buff[i]);