📄 printf.c
字号:
ULONG urc; /* returncode */
urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME); /* Open the Q */
/* Non-0 RC means Q does not exist or cannot be opened */
if (urc==343) return 0; /* queue does not exist, so quit */
if (urc!=0) return -1; /* report any other error */
/* First determine our thread ID (and hence get access to the */
/* correct per-thread data. If the per-thread data has not been */
/* allocated, then allocate it now. It is never freed, once */
/* allocated, as PRINTF is not notified of end-of-thread. */
DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
if (ourtid>PRINTFTHREADS) /* too many threads .. */
return 0; /* .. so quit, quietly */
tp=tps[ourtid]; /* copy to local pointer */
if (tp==NULL) { /* uninitialized (NULL=0) */
/* allocate a per-thread structure */
tp=(struct perthread *)malloc(sizeof(struct perthread));
if (tp==NULL) return -1; /* out of memory -- return error */
tps[ourtid]=tp; /* save for future calls */
strcpy(tp->line,PRINTFID); /* initialize: line.. */
tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
tp->bell=FALSE; /* ..if line has bell */
if (ourpid==0) ourpid=ppib->pib_ulpid; /* save PID for all to use */
}
{ /* Block for declarations -- only needed if queue exists, etc. */
LONG count; /* count of characters formatted */
UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
LONG i, newind; /* work */
UCHAR ch; /* .. */
va_list argptr; /* -> variable argument list */
va_start(argptr, f); /* get pointer to argument list */
count=vsprintf(buffer, f, argptr);
va_end(argptr); /* done with variable arguments */
if (count<0) return count-1000;/* bad start */
if (count>PRINTFMAXLEN) {
/* Disaster -- we are probably "dead", but just in case we */
/* are not, carry on with truncated data. */
count=PRINTFMAXLEN;
}
buffer[count]='\0'; /* ensure terminated */
/* OK, ready to go with the data now in BUFFER */
/* We copy from the formatted string to the output (line) buffer, */
/* taking note of certain control characters and sending a line */
/* the queue whenever we see a LF control, or when the line */
/* fills (causing a forced break). */
for (i=0; ; i++) {
ch=buffer[i]; if (!ch) break;
switch(ch) {
case '\r': /* carriage return */
tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
break;
case '\n': /* new line */
case '\f': /* form feed */
rc=printf_(tp); /* print a line */
if (rc!=0) return rc; /* error */
break;
case '\t': /* tab */
newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
newind=tp->lineindex+5-newind%5; /* new index requested */
if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF; /* clamp */
for (; tp->lineindex<newind; tp->lineindex++) {
if (tp->lineindex>tp->tidemark) { /* beyond current end */
tp->line[tp->lineindex]=' '; /* add space */
tp->tidemark=tp->lineindex;
}
}
break;
case '\v': /* vertical tab */
/* ignore it */
break;
case '\b': /* backspace */
tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
break;
case '\a': /* alert (bell) */
tp->bell=TRUE;
break;
default: /* ordinary character */
tp->line[tp->lineindex]=ch;
if (tp->lineindex>tp->tidemark) /* is rightmost.. */
tp->tidemark=tp->lineindex;
tp->lineindex++; /* step for next */
} /* switch */
if (tp->lineindex>=PRINTFMAXBUF) {
rc=printf_(tp); /* print a line */
if (rc!=0) return rc; /* error */
}
} /* copy loop */
return count; /* all formatted data processed */
} /* block */
} /* printf */
/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
/* A line has been completed (or overflowed): write it to the queue. */
int printf_(struct perthread *tp) /* pointer to per-thread data */
{
ULONG urc; /* unsigned returncode */
PSZ pszTo, pszFrom; /* character pointers */
PVOID addr; /* address of output data */
long size; /* total size of output data */
time_t timenow; /* holds current time */
tp->line[tp->tidemark+1]='\0'; /* add terminator */
size=tp->tidemark+2; /* total length of data */
/* Get some shared memory that can be given away */
urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT);
if (urc!=0) return -2; /* error */
pszTo=addr; /* copy for clarity */
pszFrom=&(tp->line[0]); /* pointer to source */
strcpy(pszTo,pszFrom); /* copy the string to shared memory */
if (ourpid!=servepid) { /* (no giveaway needed if to self) */
urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
if (urc!=0) return -3;} /* error */
/* Write the selector, size, and timestamp to the queue */
if (tp->bell) size=-size; /* BELL passed by negation */
time(&timenow); /* optional - else use 0 */
urc=DosWriteQueue(qhandle, /* handle */
(unsigned)timenow, /* 'request' (timestamp) */
(unsigned)size, /* 'length' (length/bell) */
addr, /* 'address' (address) */
0); /* priority (FIFO if enabled) */
if (urc!=0) return -4; /* error */
if (ourpid!=servepid) { /* if given away.. */
urc=DosFreeMem(addr); /* .. *we* are done with it */
if (urc!=0) return -5;} /* error */
/* Reset the line buffer and indices */
tp->lineindex=PRINTFIDSIZE-1; /* where next char */
tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
tp->bell =FALSE; /* true if line has bell */
return 0; /* success! */
} /* printf_ */
#endif // DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -