home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PMPRINT.ZIP / PRINTF.C < prev    next >
Text File  |  1991-03-20  |  15KB  |  276 lines

  1. /* -------------------- IBM ESDTOOLS Use Only ---------------------- */
  2. /* PRINTF: diverts PRINTF calls to an OS/2 Named Queue               */
  3. /* Copyright (c) IBM Corporation, 1991                               */
  4. /* ----------------------------------------------------------------- */
  5. /*                                     Mike Cowlishaw, Jan-Mar 1991  */
  6. /*                                                                   */
  7. /* This routine, when linked into an .EXE instead of the usual C     */
  8. /* runtime, sends the edited result string to a named queue (if      */
  9. /* it exists).  If the queue does not exist, then all printf data    */
  10. /* are discarded (ignored).                                          */
  11. /*                                                                   */
  12. /* The result string is accumulated until a line feed (LF) character */
  13. /* is received; the whole line is then sent to the queue.  Lines are */
  14. /* automatically broken at a set (tailorable) length, if necessary.  */
  15. /*                                                                   */
  16. /* This routine may be tailored by altering the #defines at the      */
  17. /* top:                                                              */
  18. /*                                                                   */
  19. /*   PRINTFID      - An ID string that is prefixed to each line of   */
  20. /*                   data before being sent to the queue.  This      */
  21. /*                   can be any string, or the null string.          */
  22. /*   PRINTFMAXLEN  - Maximum length of string that can be formatted  */
  23. /*                   in a single call.                               */
  24. /*                   Results are unpredictable if this length is     */
  25. /*                   exceeded.  Default is 250.                      */
  26. /*   PRINTFLINELEN - Maximum length of a line that will be sent.     */
  27. /*                   This excludes the prefix an its blank.  If the  */
  28. /*                   calls to printf cause a line to be generated    */
  29. /*                   that is longer than this, the line will be      */
  30. /*                   broken at this point.                           */
  31. /*   PRINTFTHREADS - Maximum number of threads expected.  This may   */
  32. /*                   need to be increased if the process limitation  */
  33. /*                   is removed, or you can save a little storage    */
  34. /*                   by decreasing it.  PRINTFs from threads larger  */
  35. /*                   than this number are ignored.                   */
  36. /*   PRINTFQNAME   - The name of the public queue that the result    */
  37. /*                   is to be sent to.  Normally '\QUEUES\PMPRINTF'. */
  38. /*                   Note that the \QUEUES\ part is required.        */
  39. /*                                                                   */
  40. /* Returns:                                                          */
  41. /*   n: Count of data characters, if successfully received           */
  42. /*   0: If no queue existed (i.e., no server)                        */
  43. /*  <0: An error occurred (e.g., out of memory)                      */
  44. /*                                                                   */
  45. /* Restrictions:                                                     */
  46. /*   1. Total length of data (length of PRINTFID, + PRINTFMAXLEN)    */
  47. /*      must be less than 32K-1.                                     */
  48. /*   2. This has only been tested under Large model compilation.     */
  49. /*      It may need modification for other models.                   */
  50. /*   3. This version uses a static array to point to the per-thread  */
  51. /*      data.  The code could be made read-only by hanging this      */
  52. /*      array (and the other static information) off a system-owned  */
  53. /*      anchor of some kind.                                         */
  54. /*   4. To use PRINTF within other than the main thread in a         */
  55. /*      program, that thread must be started with _beginthread       */
  56. /*      (not DosCreateThread).  This restriction is a consequence of */
  57. /*      the use of C library routines (sprintf) in PRINTF <sigh>.    */
  58. /*   5. If the last PRINTF done by a thread does not end in '\n'     */
  59. /*      then the final part-line may be lost or appear later.        */
  60. /*                                                                   */
  61. /* Protocol:                                                         */
  62. /*   PRINTF writes its data to the named queue using the following   */
  63. /*   protocol:                                                       */
  64. /*     Request -- Holds the Selector of the shared memory segment    */
  65. /*                valid in the receiver's address space.  The        */
  66. /*                shared memory segment hold the character data      */
  67. /*                (a 0-terminated string) starting at offset 0.      */
  68. /*     Length  -- The length of the data, including terminator.      */
  69. /*                A negative length indicates a BELL in the data.    */
  70. /*     Address -- Timestamp (when queue was written) in C long       */
  71. /*                integer format (as returned by time()).            */
  72. /*                This may be 0L if not required.                    */
  73. /*                                                                   */
  74. /* Notes:                                                            */
  75. /*   1. PMPRINTF uses a queue and shared memory messages because:    */
  76. /*        (a) It makes collection at the receiving end very easy.    */
  77. /*        (b) I wanted to experiment with queues and shared memory.  */
  78. /*      This make not be the most cost-effective method.             */
  79. /*   2. Typical IBM C/2 compiler invocation:                         */
  80. /*        cc -nologo -G2s -W3 -Zpe printf.c;                         */
  81. /*      For multi-thread (and link with LLIBCMT):                    */
  82. /*        cl -c -nologo -G2s -W3 -Zpel Alfw printf.c                 */
  83.  
  84.  
  85. /* ----- Customization variables ----- */
  86. #define PRINTFID      ""
  87. #define PRINTFMAXLEN  250
  88. #define PRINTFLINELEN 100
  89. #define PRINTFTHREADS  54
  90. #define PRINTFQNAME   "\\QUEUES\\PMPRINTF"
  91.  
  92. /* ----- Includes and externals ----- */
  93. #include <stdlib.h>                /* standard C functions */
  94. #include <stddef.h>                /* (note NOT stdio.h, to avoid a */
  95. #include <string.h>                /*   warning of non-matching */
  96. #include <time.h>                  /*   PRINTF prototype) */
  97. #include <stdarg.h>                /* .. */
  98. #define INCL_DOS                   /* Operating system definitions */
  99. #include <os2.h>                   /* For OS/2 functions */
  100. /* Next line defines sprintf (from mt\stdio.h) */
  101. extern int far _CDECL vsprintf(char far *, const char far *, va_list);
  102.  
  103. /* ----- Local defines ----- */
  104. #define PRINTFIDSIZE sizeof(PRINTFID)
  105. #define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
  106.  
  107. /* ----- Per-thread output buffer and current indices into line ---- */
  108. struct perthread {
  109.   UCHAR  line[PRINTFMAXBUF];       /* accumulator */
  110.   SHORT  lineindex;                /* where next char */
  111.   SHORT  tidemark;                 /* rightmost char */
  112.   SHORT  bell;                     /* TRUE if line has bell */
  113.   };
  114.  
  115. /* ----- Local static variables ----- */
  116. static USHORT ourpid=0;            /* our process ID */
  117. static USHORT servepid=0;          /* process IDs of the server */
  118. static HQUEUE qhandle=0;           /* handle for the queue */
  119. static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
  120.  
  121. /* ----- Local subroutine ----- */
  122. int _CDECL printf_(struct perthread *);
  123.  
  124. /* ----------------------------------------------------------------- */
  125. /* The "printf" function.  Note this has a variable number of        */
  126. /* arguments.                                                        */
  127. /* ----------------------------------------------------------------- */
  128. int printf(f)
  129.   char f[];
  130.   {
  131.   USHORT  rc;                      /* returncode */
  132.   PIDINFO pids;                    /* process/thread id structure */
  133.   TID     ourtid;                  /* thread ID */
  134.   struct perthread *tp;            /* pointer to per-thread data */
  135.  
  136.   rc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME);  /* Open the Q */
  137.   /* Non-0 RC means Q does not exist or cannot be opened */
  138.   if (rc==343) return 0;           /* queue does not exist, so quit */
  139.   if (rc!=0) return -1;            /* report any other error */
  140.  
  141.   /* First determine our thread ID (and hence get access to the      */
  142.   /* correct per-thread data.  If the per-thread data has not been   */
  143.   /* allocated, then allocate it now.  It is never freed, once       */
  144.   /* allocated, as PRINTF is not notified of end-of-thread.          */
  145.   DosGetPID(&pids);                /* get process/thread info */
  146.   ourtid=pids.tid;                 /* .. and copy TID */
  147.   if (ourtid>PRINTFTHREADS)        /* too many threads .. */
  148.     return 0;                      /* .. so quit, quietly */
  149.   tp=tps[ourtid];                  /* copy to local pointer */
  150.   if (tp==NULL) {                  /* uninitialized (NULL=0) */
  151.     /* allocate a per-thread structure */
  152.     tp=(struct perthread *)malloc(sizeof(struct perthread));
  153.     if (tp==NULL) return -1;       /* out of memory -- return error */
  154.     tps[ourtid]=tp;                /* save for future calls */
  155.     strcpy(tp->line,PRINTFID);     /* initialize: line.. */
  156.     tp->lineindex=PRINTFIDSIZE-1;  /* ..where next char */
  157.     tp->tidemark =PRINTFIDSIZE-2;  /* ..rightmost char */
  158.     tp->bell=FALSE;                /* ..if line has bell */
  159.     if (ourpid==0) ourpid=pids.pid;/* save PID for all to use */
  160.     }
  161.  
  162.   { /* Block for declarations -- only needed if queue exists, etc. */
  163.     SHORT count;                   /* count of characters formatted */
  164.     SHORT formlen;                 /* length of formatted data */
  165.     UCHAR buffer[PRINTFMAXLEN+1];  /* formatting area */
  166.     SHORT i, newind;               /* work */
  167.     UCHAR ch;                      /* .. */
  168.     va_list argptr;                /* -> variable argument list */
  169.  
  170.     va_start(argptr, f);           /* get pointer to arg list */
  171.     count=vsprintf(buffer, f, argptr);
  172.     va_end(argptr);                /* done with variable arguments */
  173.  
  174.     if (count>PRINTFMAXLEN) {
  175.       /* Disaster -- we are probably "dead", but just in case we */
  176.       /* are not, carry on with truncated data. */
  177.       count=PRINTFMAXLEN;
  178.       }
  179.     buffer[count]='\0';            /* ensure terminated */
  180.     formlen=count+1;               /* length of whole string + \0 */
  181.     /* OK, ready to go with the data now in BUFFER                    */
  182.     /* We copy from the formatted string to the output (line) buffer, */
  183.     /* taking note of certain control characters and sending a line   */
  184.     /* the queue whenever we see a LF control, or when the line       */
  185.     /* fills (causing a forced break).                                */
  186.     for (i=0; ch=buffer[i]; i++) {
  187.       switch(ch) {
  188.         case '\r':                 /* carriage return */
  189.           tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
  190.           break;
  191.         case '\n':                 /* new line */
  192.         case '\f':                 /* form feed */
  193.           rc=printf_(tp);          /* print a line */
  194.           if (rc!=0) return rc;    /* error */
  195.           break;
  196.         case '\t':                 /* tab */
  197.           newind=tp->lineindex-PRINTFIDSIZE+1;   /* offset into data */
  198.           newind=tp->lineindex+5-newind%5;    /* new index requested */
  199.           if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF;    /* clamp */
  200.           for (; tp->lineindex<newind; tp->lineindex++) {
  201.             if (tp->lineindex>tp->tidemark) {  /* beyond current end */
  202.               tp->line[tp->lineindex]=' ';              /* add space */
  203.               tp->tidemark=tp->lineindex;
  204.               }
  205.             }
  206.           break;
  207.         case '\v':                 /* vertical tab */
  208.           /* ignore it */
  209.           break;
  210.         case '\b':                 /* backspace */
  211.           tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
  212.           break;
  213.         case '\a':                 /* alert (bell) */
  214.           tp->bell=TRUE;
  215.           break;
  216.         default:                   /* ordinary character */
  217.           tp->line[tp->lineindex]=ch;
  218.           if (tp->lineindex>tp->tidemark)  /* is rightmost.. */
  219.             tp->tidemark=tp->lineindex;
  220.           tp->lineindex++;                 /* step for next */
  221.         } /* switch */
  222.       if (tp->lineindex>=PRINTFMAXBUF) {
  223.         rc=printf_(tp);            /* print a line */
  224.         if (rc!=0) return rc;      /* error */
  225.         }
  226.  
  227.       } /* copy loop */
  228.     return count;                  /* all formatted data processed */
  229.     } /* block */
  230.   } /* printf */
  231.  
  232. /* ----- printf_(tp) -- Local subroutine to send a line ------------ */
  233. /* A line has been completed (or overflowed): write it to the queue. */
  234. int _CDECL printf_(tp)
  235.   struct perthread *tp;            /* pointer to per-thread data */
  236.   {
  237.   USHORT  rc;                      /* returncode */
  238.   PSZ     pszTo, pszFrom;          /* character pointers */
  239.   SEL     oursel, servesel;        /* selectors for shared segment */
  240.   USHORT  size;                    /* total size of output data */
  241.   long    timenow;                 /* holds current time */
  242.  
  243.   tp->line[tp->tidemark+1]='\0';   /* add terminator */
  244.   size=tp->tidemark+2;             /* total length of data */
  245.  
  246.   /* Get some shared memory that can be given away */
  247.   rc=DosAllocSeg(size, &oursel, SEG_GIVEABLE);
  248.   if (rc!=0) return -2;            /* error */
  249.   pszTo=MAKEP(oursel, 0);          /* make far ptr from selector */
  250.   pszFrom=&(tp->line[0]);          /* pointer to source */
  251.   while (*pszTo++=*pszFrom++);     /* copy the string to segment */
  252.  
  253.   if (ourpid==servepid) servesel=oursel; /* no giveaway needed */
  254.    else {
  255.     rc=DosGiveSeg(oursel, servepid, &servesel);
  256.     if (rc!=0) return -3;}         /* error */
  257.  
  258.   /* Write the selector, size, and timestamp to the queue */
  259.   if (tp->bell) size=-size;        /* BELL passed by negation */
  260.   time(&timenow);                  /* optional - else use 0 */
  261.   rc=DosWriteQueue(qhandle,        /* handle */
  262.                    servesel,       /* 'request' (selector) */
  263.                    size,           /* 'length'  (length/bell) */
  264.             (PBYTE)timenow,        /* 'address' (timestamp) */
  265.                    0);             /* priority (FIFO if enabled) */
  266.   if (rc!=0) return -4;            /* error */
  267.   if (ourpid!=servepid) {          /* if given away.. */
  268.     rc=DosFreeSeg(oursel);         /* .. *we* are done with it */
  269.     if (rc!=0) return -5;}         /* error */
  270.   /* Reset the line buffer and indices */
  271.   tp->lineindex=PRINTFIDSIZE-1;    /* where next char */
  272.   tp->tidemark =PRINTFIDSIZE-2;    /* rightmost char */
  273.   tp->bell     =FALSE;             /* true if line has bell */
  274.   return 0;                        /* success! */
  275.   } /* printf_ */
  276.