home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CTASK22.ZIP / CONOUT.C < prev    next >
C/C++ Source or Header  |  1990-10-12  |  3KB  |  137 lines

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