source: trunk/src/ddraw/misc.c@ 521

Last change on this file since 521 was 521, checked in by hugh, 26 years ago

updated misc with FS wrappers

File size: 10.8 KB
Line 
1#define INCL_BASE
2#define INCL_WIN
3#define INCL_WINERRORS
4#define INCL_DOSFILEMGR
5#include <os2wrap.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdarg.h>
10#include "misc.h"
11#include <odincrt.h>
12
13
14//#define PMPRINTF
15#ifdef PMPRINTF
16
17/* ----- Customization variables ----- */
18#define PRINTFID ""
19#define PRINTFMAXLEN 300
20#define PRINTFLINELEN 100
21#define PRINTFTHREADS 54
22#define PRINTFQNAME "\\QUEUES\\PRINTF32"
23
24/* ----- Includes and externals ----- */
25#include <stddef.h> /* .. */
26#include <time.h> /* .. */
27
28extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
29
30/* ----- Local defines ----- */
31#define PRINTFIDSIZE sizeof(PRINTFID)
32#define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
33
34/* ----- Per-thread output buffer and current indices into line ---- */
35struct perthread
36{
37 LONG lineindex; /* where next char */
38 LONG tidemark; /* rightmost char */
39 int bell; /* TRUE if line has bell */
40 UCHAR line[PRINTFMAXBUF]; /* accumulator */
41};
42
43/* ----- Local static variables ----- */
44static ULONG ourpid=0; /* our process ID */
45static ULONG servepid=0; /* process IDs of the server */
46static HQUEUE qhandle=0; /* handle for the queue */
47static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
48
49/* ----- Local subroutine ----- */
50static int printf_(struct perthread *);
51
52/* ----------------------------------------------------------------- */
53/* The "printf" function. Note this has a variable number of */
54/* arguments. */
55/* ----------------------------------------------------------------- */
56int SYSTEM EXPORT WriteLog(char *f, ...)
57{
58 TIB *ptib; /* process/thread id structures */
59 PIB *ppib; /* .. */
60 TID ourtid; /* thread ID */
61 struct perthread *tp; /* pointer to per-thread data */
62 int rc; /* returncode */
63 ULONG urc; /* returncode */
64
65 urc=DosOpenQueue( &servepid,
66 &qhandle,
67 PRINTFQNAME); /* Open the Q */
68 /* Non-0 RC means Q does not exist or cannot be opened */
69 if (urc==343)
70 return 0; /* queue does not exist, so quit */
71 if (urc!=0)
72 return -1; /* report any other error */
73
74 /* First determine our thread ID (and hence get access to the */
75 /* correct per-thread data. If the per-thread data has not been */
76 /* allocated, then allocate it now. It is never freed, once */
77 /* allocated, as PRINTF is not notified of end-of-thread. */
78 DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
79 ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
80 if (ourtid>PRINTFTHREADS) /* too many threads .. */
81 return 0; /* .. so quit, quietly */
82 tp=tps[ourtid]; /* copy to local pointer */
83 if (tp==NULL)
84 {
85 /* uninitialized (NULL=0) */
86 /* allocate a per-thread structure */
87 tp=(struct perthread *)malloc(sizeof(struct perthread));
88 if (tp==NULL)
89 return -1; /* out of memory -- return error */
90 tps[ourtid]=tp; /* save for future calls */
91 strcpy(tp->line,PRINTFID); /* initialize: line.. */
92 tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
93 tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
94 tp->bell=FALSE; /* ..if line has bell */
95 if (ourpid==0)
96 ourpid=ppib->pib_ulpid; /* save PID for all to use */
97 }
98
99 { /* Block for declarations -- only needed if queue exists, etc. */
100 LONG count; /* count of characters formatted */
101 UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
102 LONG i, newind; /* work */
103 UCHAR ch; /* .. */
104 va_list argptr; /* -> variable argument list */
105
106 va_start(argptr, f); /* get pointer to argument list */
107 count=vsprintf(buffer, f, argptr);
108 va_end(argptr); /* done with variable arguments */
109
110 if (count<0)
111 return count-1000;/* bad start */
112
113 if (count>PRINTFMAXLEN)
114 {
115 /* Disaster -- we are probably "dead", but just in case we */
116 /* are not, carry on with truncated data. */
117 count=PRINTFMAXLEN;
118 }
119 buffer[count]='\0'; /* ensure terminated */
120 /* OK, ready to go with the data now in BUFFER */
121 /* We copy from the formatted string to the output (line) buffer, */
122 /* taking note of certain control characters and sending a line */
123 /* the queue whenever we see a LF control, or when the line */
124 /* fills (causing a forced break). */
125 for (i=0; ; i++)
126 {
127 ch=buffer[i]; if (!ch) break;
128 switch(ch)
129 {
130 case '\r': /* carriage return */
131 tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
132 break;
133 case '\n': /* new line */
134 case '\f': /* form feed */
135 rc=printf_(tp); /* print a line */
136 if (rc!=0) return rc; /* error */
137 break;
138 case '\t': /* tab */
139 newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
140 newind=tp->lineindex+5-newind%5; /* new index requested */
141 if (newind>=PRINTFMAXBUF)
142 newind=PRINTFMAXBUF; /* clamp */
143 for (; tp->lineindex<newind; tp->lineindex++)
144 {
145 if (tp->lineindex>tp->tidemark)
146 { /* beyond current end */
147 tp->line[tp->lineindex]=' '; /* add space */
148 tp->tidemark=tp->lineindex;
149 }
150 }
151 break;
152 case '\v': /* vertical tab */
153 /* ignore it */
154 break;
155 case '\b': /* backspace */
156 tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
157 break;
158 case '\a': /* alert (bell) */
159 tp->bell=TRUE;
160 break;
161 default: /* ordinary character */
162 tp->line[tp->lineindex]=ch;
163 if (tp->lineindex>tp->tidemark) /* is rightmost.. */
164 tp->tidemark=tp->lineindex;
165 tp->lineindex++; /* step for next */
166 } /* switch */
167 if (tp->lineindex>=PRINTFMAXBUF)
168 {
169 rc=printf_(tp); /* print a line */
170 if (rc!=0)
171 return rc; /* error */
172 }
173
174 } /* copy loop */
175 return count; /* all formatted data processed */
176 } /* block */
177} /* printf */
178
179/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
180/* A line has been completed (or overflowed): write it to the queue. */
181int printf_(struct perthread *tp) /* pointer to per-thread data */
182{
183 ULONG urc; /* unsigned returncode */
184 PSZ pszTo, pszFrom; /* character pointers */
185 PVOID addr; /* address of output data */
186 long size; /* total size of output data */
187 time_t timenow; /* holds current time */
188
189 tp->line[tp->tidemark+1]='\0'; /* add terminator */
190 size=tp->tidemark+2; /* total length of data */
191
192 /* Get some shared memory that can be given away */
193 urc=DosAllocSharedMem( &addr,
194 NULL,
195 (unsigned)size,
196 OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
197 /*knut: added flAllocMem */
198
199 if (urc!=0)
200 return -2; /* error */
201
202 pszTo=addr; /* copy for clarity */
203 pszFrom=&(tp->line[0]); /* pointer to source */
204 strcpy(pszTo,pszFrom); /* copy the string to shared memory */
205
206 if (ourpid!=servepid)
207 { /* (no giveaway needed if to self) */
208 urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
209 if (urc!=0)
210 return -3; /* error */
211 }
212
213 /* Write the selector, size, and timestamp to the queue */
214 if (tp->bell) size=-size; /* BELL passed by negation */
215 time(&timenow); /* optional - else use 0 */
216 urc=DosWriteQueue( qhandle, /* handle */
217 (unsigned)timenow, /* 'request' (timestamp) */
218 (unsigned)size, /* 'length' (length/bell) */
219 addr, /* 'address' (address) */
220 0); /* priority (FIFO if enabled) */
221 if (urc!=0)
222 return -4; /* error */
223 if (ourpid!=servepid)
224 { /* if given away.. */
225 urc=DosFreeMem(addr); /* .. *we* are done with it */
226 if (urc!=0)
227 return -5; /* error */
228 }
229 /* Reset the line buffer and indices */
230 tp->lineindex=PRINTFIDSIZE-1; /* where next char */
231 tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
232 tp->bell =FALSE; /* true if line has bell */
233 return 0; /* success! */
234} /* printf_ */
235#else
236
237
238#if 1 /*PLF Mon 97-09-08 20:04:28*/
239static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
240static BOOL init = FALSE;
241
242int SYSTEM EXPORT WriteLog(char *tekst, ...)
243{
244 ODIN_FS_BEGIN
245
246 va_list argptr;
247 if(!init)
248 {
249 init = TRUE;
250 if(!getenv("NODDRAWLOG"))
251 flog = fopen("DDRAW.log", "w");
252 }
253
254 if(flog)
255 {
256 va_start(argptr, tekst);
257 vfprintf(flog, tekst, argptr);
258 va_end(argptr);
259 fflush(flog);
260 }
261 ODIN_FS_END
262
263 return 1;
264}
265#else /*PLF Mon 97-09-08 20:04:26*/
266/******************************************************************************/
267static BOOL init = FALSE;
268static BOOL fLog = TRUE;
269/******************************************************************************/
270int SYSTEM EXPORT WriteLog(char *tekst, ...)
271{
272 ULONG Action, Wrote;
273 HFILE log;
274 APIRET rc;
275 char message[4096];
276 va_list argptr;
277 ULONG openFlags = OPEN_ACTION_CREATE_IF_NEW;
278
279 if(fLog == FALSE)
280 return(FALSE);
281
282 if(!init)
283 {
284 init = TRUE;
285 openFlags |= OPEN_ACTION_REPLACE_IF_EXISTS;
286 if(getenv("NOWIN32LOG"))
287 fLog = FALSE;
288 }
289 else
290 openFlags |= OPEN_ACTION_OPEN_IF_EXISTS;
291
292 rc = DosOpen( "win32os2.log",
293 &log, /* file handle returned */
294 &Action,
295 0L,
296 FILE_NORMAL,
297 openFlags,
298 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE,
299 (PEAOP2)NULL);
300
301 rc = DosSetFilePtr(log, 0, FILE_END, &Wrote);
302 va_start(argptr, tekst);
303 vsprintf(message, tekst, argptr);
304 va_end(argptr);
305
306 rc = DosWrite(log, message, strlen(message), &Wrote);
307
308 DosClose(log); /*PLF Mon 97-09-08 20:01:43*/
309 return(TRUE);
310}
311#endif /*PLF Mon 97-09-08 20:04:23*/
312/******************************************************************************/
313/******************************************************************************/
314
315
316#endif
Note: See TracBrowser for help on using the repository browser.