home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_02 / tprt.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  3KB  |  130 lines

  1.  
  2. /*
  3.    Test program for checking CTask printer buffering.
  4.  
  5.    The program will send the file given as parameter to the printer.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <process.h>
  11. #include <ctype.h>
  12.  
  13. #include "tsk.h"
  14. #include "prt.h"
  15. #include "tsksup.h"
  16.  
  17. #define PORT 0x80    /* LPT1 (relative) */
  18. #define POLL 0       /* Set to 1 to use polling */
  19.  
  20. #define STACKSIZE 2048
  21.  
  22. unsigned int _stklen = 3 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  23.  
  24. tcb tcb1, tcb2;
  25. flag halt, ready;
  26.  
  27. byte xmtbuf [1024];
  28. int endrun;
  29. int pport;
  30.  
  31. FILE *pfile;
  32.  
  33.  
  34. void far task1 (void)
  35. {
  36.    int i;
  37.  
  38.    printf ("Task 1 started\n");
  39.  
  40.    /* Copy file into printer pipe. Using a larger file buffer would be more
  41.       efficient, but this is only a demo.
  42.    */
  43.  
  44.    while (!endrun && ((i = getc (pfile)) != EOF))
  45.       prt_write (pport, (byte)i, 0L);
  46.  
  47.    if (endrun)
  48.       return;
  49.  
  50.    /* Wait for printer to complete output. */
  51.  
  52.    if (!prt_complete (pport))
  53.       puts ("Waiting for Printer");
  54.    prt_wait_complete (pport, 0L);
  55.  
  56.    /* Stop program */
  57.  
  58.    set_flag (&halt);
  59.    set_flag (&ready);
  60. }
  61.  
  62.  
  63. /*
  64.    Second task just for fun. Echoes keyboard characters, aborts printing
  65.    when 'e' is entered. Also outputs snapshot dump on 'd'.
  66. */
  67.  
  68. void far task2 (void)
  69. {
  70.    int ch;
  71.  
  72.    printf ("Task 2 started\n");
  73.    while (!endrun)
  74.       {
  75.       ch = t_read_key () & 0xff;
  76.       putch (ch);
  77.       ch = tolower (ch);
  78.       endrun = (ch == 'e');
  79. #if (TSK_NAMED)
  80.       if (ch == 'd')
  81.          snapshot (stdout);
  82. #endif
  83.       }
  84.  
  85.    set_flag (&halt);
  86. }
  87.  
  88.  
  89.  
  90. void main (int argc, char **argv)
  91. {
  92.    char stack1 [STACKSIZE];
  93.    char stack2 [STACKSIZE];
  94.  
  95.    if ((pfile = fopen (argv [1], "rb")) == NULL)
  96.       {
  97.       printf ("Error opening file '%s'\n", argv [1]);
  98.       exit (1);
  99.       }
  100.  
  101.    endrun = 0;
  102.    install_tasker (0, 0, IFL_STD, "TPRT");
  103.  
  104.    pport = prt_install (PORT, POLL, PRI_STD, xmtbuf, sizeof (xmtbuf));
  105.  
  106.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("TASK1"));
  107.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL TN("TASK2"));
  108.    create_flag (&halt TN("Halt"));
  109.    create_flag (&ready TN("Ready"));
  110.  
  111.    start_task (&tcb1);
  112.    start_task (&tcb2);
  113.  
  114.    preempt_on ();
  115.    wait_flag_set (&halt, 0L);
  116.  
  117.    endrun = 1;
  118.    puts ("******** Main Task *********");
  119.    wait_flag_set (&ready, 0L);
  120.  
  121.    puts ("******** End Run *********");
  122.    preempt_off ();
  123.    prt_remove (pport);
  124.    remove_tasker ();
  125.  
  126.    fclose (pfile);
  127. }
  128.  
  129.  
  130.