home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CTASK.ZIP / CONOUT.C next >
C/C++ Source or Header  |  1989-12-20  |  3KB  |  136 lines

  1. /*
  2.     --- Version 2.0 89-12-13 17:41 ---
  3.  
  4.    Sample module for channeling console output through
  5.    a single task to avoid problems with non-reentrant output
  6.    routines.
  7. */
  8.  
  9. #include "tsk.h"
  10. #include "tsksup.h"
  11.  
  12. #include <conio.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15.  
  16. #define BUFSIZE   1024     /* Size of the console out buffer */
  17. #define STRLEN    256      /* Max. length of single output string */
  18. #define STACKSIZE 2048     /* Size of stack for output task */
  19.  
  20. local buffer conout_buf;
  21. local char conout_stack [STACKSIZE];
  22. local char conout_bufbuf [BUFSIZE];
  23. local char conout_str [STRLEN+1];
  24. local tcb conout_task;
  25.  
  26. /* -------------------------------------------------------------- */
  27.  
  28. /*
  29.    conout: The console output task. Reads strings from the buffer
  30.            and displays them on the console.
  31. */
  32.  
  33. local void far conout (void)
  34. {
  35.    int siz, i;
  36.    register int ch;
  37.  
  38.    while (1)
  39.       {
  40.       siz = read_buffer (&conout_buf, conout_str, STRLEN, 0L);
  41.       for (i = 0; i < siz; i++)
  42.          {
  43.          switch (ch = conout_str [i])
  44.             {
  45.             case '\n':  putch ('\r');
  46.                         putch ('\n');
  47.                         break;
  48.  
  49. #if (TURBO)
  50.             case 0x07:  sound (2000);
  51.                         t_delay (3L);
  52.                         nosound ();
  53.                         break;
  54. #endif
  55.  
  56.             default:    putch (ch);
  57.             }
  58.          }
  59.       }
  60. }
  61.  
  62.  
  63. /*
  64.    init_conout:   Creates buffer and task. Must be called
  65.                   before using any other routine from this module.
  66. */
  67.  
  68. void init_conout (void)
  69. {
  70.    create_buffer (&conout_buf, conout_bufbuf, BUFSIZE
  71. #if (TSK_NAMEPAR)
  72.                   ,"CONOUTBF"
  73. #endif
  74.                   );
  75.    create_task (&conout_task, conout, conout_stack, STACKSIZE, PRI_STD + 100, NULL
  76. #if (TSK_NAMEPAR)
  77.                 ,"CONOUT"
  78. #endif
  79.                 );
  80.    start_task (&conout_task);
  81. }
  82.  
  83.  
  84. /*
  85.    end_conout: Deletes task and buffer. Should be called before
  86.                terminating CTask.
  87. */
  88.  
  89. void end_conout (void)
  90. {
  91.    kill_task (&conout_task);
  92.    delete_buffer (&conout_buf);
  93. }
  94.  
  95. /* -------------------------------------------------------------- */
  96.  
  97. /*
  98.    tprintf:    Buffered replacement for printf/cprintf.
  99. */
  100.  
  101. int tprintf (format)
  102.    char *format;
  103. {
  104.    va_list argptr;
  105.    char buf [256];
  106.    int res;
  107.  
  108.    va_start (argptr, format);
  109.    if ((res = vsprintf (buf, format, argptr)) > 0)
  110.       if (write_buffer (&conout_buf, buf, res, 0L) < 0)
  111.          res = 0;
  112.    va_end (argptr);
  113.    return res;
  114. }
  115.  
  116.  
  117. /*
  118.    tputs:      Buffered replacement for puts.
  119. */
  120.  
  121. int tputs (char *buf)
  122. {
  123.    return (write_buffer (&conout_buf, buf, strlen (buf), 0L) < 0) ? -1 : 0;
  124. }
  125.  
  126.  
  127. /*
  128.    tputch:     Buffered replacement for putch.
  129. */
  130.  
  131. int tputch (int ch)
  132. {
  133.    return (write_buffer (&conout_buf, &ch, 1, 0L) < 0) ? EOF : ch;
  134. }
  135.  
  136.