home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / pmprtf / printf.c < prev    next >
Text File  |  1992-08-18  |  15KB  |  280 lines

  1. /* -------------------- IBM Internal Use Only ---------------------- */
  2. /* PRINTF: diverts PRINTF calls to an OS/2 Named Queue               */
  3. /* Copyright (c) IBM Corporation, 1991,1992                          */
  4. /* ----------------------------------------------------------------- */
  5. /* This version for OS/2 2.x, 32-bit programs.        Mike Cowlishaw */
  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 and 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\PRINTF32'. */
  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 IBM C Set/2 compiler.  It    */
  49. /*      may need modification for other compilers.                   */
  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, and may   */
  58. /*      not apply to all compilers.                                  */
  59. /*   5. If the last PRINTF done by a thread does not end in '\n'     */
  60. /*      then the final part-line may be lost, or appear later.       */
  61. /*                                                                   */
  62. /* Protocol:                                                         */
  63. /*   PRINTF writes its data to the named queue using the following   */
  64. /*   protocol:                                                       */
  65. /*     Address -- Holds the address of the string to be sent. This   */
  66. /*                is a 0-terminated string) starting at offset 0.    */
  67. /*     Length  -- The length of the data, including terminator.      */
  68. /*                A negative length indicates a BELL in the data.    */
  69. /*     Request -- Timestamp (when queue was written) in C long       */
  70. /*                integer format (as returned by time()).            */
  71. /*                This may be 0L if not required.                    */
  72. /*                                                                   */
  73. /* Notes:                                                            */
  74. /*   1. PMPRINTF uses a queue and shared memory messages because:    */
  75. /*        (a) It makes collection at the receiving end very easy.    */
  76. /*        (b) I wanted to experiment with queues and shared memory.  */
  77. /*      This make not be the most cost-effective method.             */
  78. /*   2. Typical IBM C Set/2 compiler invocation:                     */
  79. /*        icc /c /Gm /O+ /Q /J /Kabgop                               */
  80. /*   3. PRINTF sends the timestamp across the queue as a GMT long    */
  81. /*      integer, the result from a call to the C function time().    */
  82. /*      This will only be correct if the environment variable TZ has */
  83. /*      been set (e.g., TZ=EST5EDT), or you are in the same time     */
  84. /*      zone as the default for your compiler.                       */
  85. /*      For more information, see the tzset() function description   */
  86. /*      in your C compiler manual.                                   */
  87.  
  88. /* ----- Customization variables ----- */
  89. #define PRINTFID      ""
  90. #define PRINTFMAXLEN  250
  91. #define PRINTFLINELEN 100
  92. #define PRINTFTHREADS  54
  93. #define PRINTFQNAME   "\\QUEUES\\PRINTF32"
  94.  
  95. /* ----- Includes and externals ----- */
  96. #include <stdlib.h>                /* standard C functions */
  97. #include <stddef.h>                /* .. */
  98. #include <string.h>                /* .. */
  99. #include <time.h>                  /* .. */
  100. #include <stdarg.h>                /* .. */
  101. #include <stdio.h>                 /* (needed to pick up real name) */
  102. #define INCL_DOS                   /* Operating system definitions */
  103. #include <os2.h>                   /* For OS/2 functions */
  104.  
  105. /* ----- Local defines ----- */
  106. #define PRINTFIDSIZE sizeof(PRINTFID)
  107. #define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
  108.  
  109. /* ----- Per-thread output buffer and current indices into line ---- */
  110. struct perthread {
  111.   LONG   lineindex;                /* where next char */
  112.   LONG   tidemark;                 /* rightmost char */
  113.   int    bell;                     /* TRUE if line has bell */
  114.   UCHAR  line[PRINTFMAXBUF];       /* accumulator */
  115.   };
  116.  
  117. /* ----- Local static variables ----- */
  118. static ULONG  ourpid=0;            /* our process ID */
  119. static ULONG  servepid=0;          /* process IDs of the server */
  120. static HQUEUE qhandle=0;           /* handle for the queue */
  121. static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
  122.  
  123. /* ----- Local subroutine ----- */
  124. static int printf_(struct perthread *);
  125.  
  126. /* ----------------------------------------------------------------- */
  127. /* The "printf" function.  Note this has a variable number of        */
  128. /* arguments.                                                        */
  129. /* ----------------------------------------------------------------- */
  130. int printf(const unsigned char *f)
  131.   {
  132.   TIB    *ptib;                    /* process/thread id structures */
  133.   PIB    *ppib;                    /* .. */
  134.   TID    ourtid;                   /* thread ID */
  135.   struct perthread *tp;            /* pointer to per-thread data */
  136.   int    rc;                       /* returncode */
  137.   ULONG  urc;                      /* returncode */
  138.  
  139.   urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME);  /* Open the Q */
  140.   /* Non-0 RC means Q does not exist or cannot be opened */
  141.   if (urc==343) return 0;          /* queue does not exist, so quit */
  142.   if (urc!=0)   return -1;         /* report any other error */
  143.  
  144.   /* First determine our thread ID (and hence get access to the      */
  145.   /* correct per-thread data.  If the per-thread data has not been   */
  146.   /* allocated, then allocate it now.  It is never freed, once       */
  147.   /* allocated, as PRINTF is not notified of end-of-thread.          */
  148.   DosGetInfoBlocks(&ptib,&ppib);   /* get process/thread info */
  149.   ourtid=ptib->tib_ptib2->tib2_ultid;   /* .. and copy TID */
  150.   if (ourtid>PRINTFTHREADS)        /* too many threads .. */
  151.     return 0;                      /* .. so quit, quietly */
  152.   tp=tps[ourtid];                  /* copy to local pointer */
  153.   if (tp==NULL) {                  /* uninitialized (NULL=0) */
  154.     /* allocate a per-thread structure */
  155.     tp=(struct perthread *)malloc(sizeof(struct perthread));
  156.     if (tp==NULL) return -1;       /* out of memory -- return error */
  157.     tps[ourtid]=tp;                /* save for future calls */
  158.     strcpy(tp->line,PRINTFID);     /* initialize: line.. */
  159.     tp->lineindex=PRINTFIDSIZE-1;  /* ..where next char */
  160.     tp->tidemark =PRINTFIDSIZE-2;  /* ..rightmost char */
  161.     tp->bell=FALSE;                /* ..if line has bell */
  162.     if (ourpid==0) ourpid=ppib->pib_ulpid;   /* save PID for all to use */
  163.     }
  164.  
  165.   { /* Block for declarations -- only needed if queue exists, etc. */
  166.     LONG  count;                   /* count of characters formatted */
  167.     UCHAR buffer[PRINTFMAXLEN+1];  /* formatting area */
  168.     LONG  i, newind;               /* work */
  169.     UCHAR ch;                      /* .. */
  170.     va_list argptr;                /* -> variable argument list */
  171.  
  172.     va_start(argptr, f);           /* get pointer to argument list */
  173.     count=vsprintf(buffer, f, argptr);
  174.     va_end(argptr);                /* done with variable arguments */
  175.  
  176.     if (count<0) return count-1000;/* bad start */
  177.  
  178.     if (count>PRINTFMAXLEN) {
  179.       /* Disaster -- we are probably "dead", but just in case we */
  180.       /* are not, carry on with truncated data. */
  181.       count=PRINTFMAXLEN;
  182.       }
  183.     buffer[count]='\0';            /* ensure terminated */
  184.     /* OK, ready to go with the data now in BUFFER                    */
  185.     /* We copy from the formatted string to the output (line) buffer, */
  186.     /* taking note of certain control characters and sending a line   */
  187.     /* the queue whenever we see a LF control, or when the line       */
  188.     /* fills (causing a forced break).                                */
  189.     for (i=0; ; i++) {
  190.       ch=buffer[i]; if (!ch) break;
  191.       switch(ch) {
  192.         case '\r':                 /* carriage return */
  193.           tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
  194.           break;
  195.         case '\n':                 /* new line */
  196.         case '\f':                 /* form feed */
  197.           rc=printf_(tp);          /* print a line */
  198.           if (rc!=0) return rc;    /* error */
  199.           break;
  200.         case '\t':                 /* tab */
  201.           newind=tp->lineindex-PRINTFIDSIZE+1;   /* offset into data */
  202.           newind=tp->lineindex+5-newind%5;    /* new index requested */
  203.           if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF;    /* clamp */
  204.           for (; tp->lineindex<newind; tp->lineindex++) {
  205.             if (tp->lineindex>tp->tidemark) {  /* beyond current end */
  206.               tp->line[tp->lineindex]=' ';              /* add space */
  207.               tp->tidemark=tp->lineindex;
  208.               }
  209.             }
  210.           break;
  211.         case '\v':                 /* vertical tab */
  212.           /* ignore it */
  213.           break;
  214.         case '\b':                 /* backspace */
  215.           tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
  216.           break;
  217.         case '\a':                 /* alert (bell) */
  218.           tp->bell=TRUE;
  219.           break;
  220.         default:                   /* ordinary character */
  221.           tp->line[tp->lineindex]=ch;
  222.           if (tp->lineindex>tp->tidemark)  /* is rightmost.. */
  223.             tp->tidemark=tp->lineindex;
  224.           tp->lineindex++;                 /* step for next */
  225.         } /* switch */
  226.       if (tp->lineindex>=PRINTFMAXBUF) {
  227.         rc=printf_(tp);            /* print a line */
  228.         if (rc!=0) return rc;      /* error */
  229.         }
  230.  
  231.       } /* copy loop */
  232.     return count;                  /* all formatted data processed */
  233.     } /* block */
  234.   } /* printf */
  235.  
  236. /* ----- printf_(tp) -- Local subroutine to send a line ------------ */
  237. /* A line has been completed (or overflowed): write it to the queue. */
  238. int printf_(struct perthread *tp)  /* pointer to per-thread data */
  239.   {
  240.   ULONG   urc;                     /* unsigned returncode */
  241.   PSZ     pszTo, pszFrom;          /* character pointers */
  242.   PVOID   addr;                    /* address of output data */
  243.   long    size;                    /* total size of output data */
  244.   time_t  timenow;                 /* holds current time */
  245.  
  246.   tp->line[tp->tidemark+1]='\0';   /* add terminator */
  247.   size=tp->tidemark+2;             /* total length of data */
  248.  
  249.   /* Get some shared memory that can be given away */
  250.   urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
  251.     OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT);
  252.   if (urc!=0) return -2;           /* error */
  253.  
  254.   pszTo=addr;                      /* copy for clarity */
  255.   pszFrom=&(tp->line[0]);          /* pointer to source */
  256.   strcpy(pszTo,pszFrom);           /* copy the string to shared memory */
  257.  
  258.   if (ourpid!=servepid) {          /* (no giveaway needed if to self) */
  259.     urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
  260.     if (urc!=0) return -3;}        /* error */
  261.  
  262.   /* Write the selector, size, and timestamp to the queue */
  263.   if (tp->bell) size=-size;        /* BELL passed by negation */
  264.   time(&timenow);                  /* optional - else use 0 */
  265.   urc=DosWriteQueue(qhandle,       /* handle */
  266.          (unsigned)timenow,        /* 'request' (timestamp) */
  267.          (unsigned)size,           /* 'length'  (length/bell) */
  268.                    addr,           /* 'address' (address) */
  269.                    0);             /* priority (FIFO if enabled) */
  270.   if (urc!=0) return -4;           /* error */
  271.   if (ourpid!=servepid) {          /* if given away.. */
  272.     urc=DosFreeMem(addr);          /* .. *we* are done with it */
  273.     if (urc!=0) return -5;}        /* error */
  274.   /* Reset the line buffer and indices */
  275.   tp->lineindex=PRINTFIDSIZE-1;    /* where next char */
  276.   tp->tidemark =PRINTFIDSIZE-2;    /* rightmost char */
  277.   tp->bell     =FALSE;             /* true if line has bell */
  278.   return 0;                        /* success! */
  279.   } /* printf_ */
  280.