| 1 | /* $Id: misc.cpp,v 1.2 1999-10-26 17:53:43 sandervl Exp $ */ | 
|---|
| 2 | #define INCL_BASE | 
|---|
| 3 | #define INCL_WIN | 
|---|
| 4 | #define INCL_WINERRORS | 
|---|
| 5 | #define INCL_DOSFILEMGR | 
|---|
| 6 | #include <os2wrap.h> | 
|---|
| 7 | #include <stdio.h> | 
|---|
| 8 | #include <stdlib.h> | 
|---|
| 9 | #include <string.h> | 
|---|
| 10 | #include <stdarg.h> | 
|---|
| 11 | #include <misc.h> | 
|---|
| 12 | #include "initterm.h" | 
|---|
| 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 |  | 
|---|
| 28 | extern 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 ---- */ | 
|---|
| 35 | struct 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 ----- */ | 
|---|
| 44 | static ULONG  ourpid=0;            /* our process ID */ | 
|---|
| 45 | static ULONG  servepid=0;          /* process IDs of the server */ | 
|---|
| 46 | static HQUEUE qhandle=0;           /* handle for the queue */ | 
|---|
| 47 | static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */ | 
|---|
| 48 |  | 
|---|
| 49 | /* ----- Local subroutine ----- */ | 
|---|
| 50 | static int printf_(struct perthread *); | 
|---|
| 51 |  | 
|---|
| 52 | /* ----------------------------------------------------------------- */ | 
|---|
| 53 | /* The "printf" function.  Note this has a variable number of        */ | 
|---|
| 54 | /* arguments.                                                        */ | 
|---|
| 55 | /* ----------------------------------------------------------------- */ | 
|---|
| 56 | int 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. */ | 
|---|
| 181 | int 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 | /***************************************************************************** | 
|---|
| 239 | * Standard Version                                                          * | 
|---|
| 240 | *****************************************************************************/ | 
|---|
| 241 |  | 
|---|
| 242 | static FILE *flog = NULL;   /*PLF Mon  97-09-08 20:00:15*/ | 
|---|
| 243 | static BOOL init = FALSE; | 
|---|
| 244 | static BOOL fLogging = TRUE; | 
|---|
| 245 |  | 
|---|
| 246 | int SYSTEM EXPORT WriteLog(char *tekst, ...) | 
|---|
| 247 | { | 
|---|
| 248 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 249 | va_list argptr; | 
|---|
| 250 |  | 
|---|
| 251 | if(!init) | 
|---|
| 252 | { | 
|---|
| 253 | init = TRUE; | 
|---|
| 254 |  | 
|---|
| 255 | if(!getenv("NODSOUNDLOG")) { | 
|---|
| 256 | char logname[CCHMAXPATH]; | 
|---|
| 257 |  | 
|---|
| 258 | flog = fopen("DSOUND.log", "w"); | 
|---|
| 259 | if(flog == NULL) {//probably running exe on readonly device | 
|---|
| 260 | sprintf(logname, "%sdsound.log", dsoundPath); | 
|---|
| 261 | flog = fopen(logname, "w"); | 
|---|
| 262 | } | 
|---|
| 263 | } | 
|---|
| 264 | else | 
|---|
| 265 | fLogging = FALSE; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | if(fLogging && flog) | 
|---|
| 269 | { | 
|---|
| 270 | va_start(argptr, tekst); | 
|---|
| 271 | vfprintf(flog, tekst, argptr); | 
|---|
| 272 | va_end(argptr); | 
|---|
| 273 |  | 
|---|
| 274 | if(tekst[strlen(tekst)-1] != '\n') | 
|---|
| 275 | fprintf(flog, "\n"); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | SetFS(sel); | 
|---|
| 279 | return 1; | 
|---|
| 280 | } | 
|---|
| 281 | //****************************************************************************** | 
|---|
| 282 | //NOTE: No need to save/restore FS, as our FS selectors have already been | 
|---|
| 283 | //      destroyed and FS == 0x150B. | 
|---|
| 284 | //****************************************************************************** | 
|---|
| 285 | void CloseLogFile() | 
|---|
| 286 | { | 
|---|
| 287 | fclose(flog); | 
|---|
| 288 | flog = 0; | 
|---|
| 289 | } | 
|---|
| 290 | #endif | 
|---|