home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / ct22d / res.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-25  |  3.3 KB  |  157 lines

  1.  
  2. /*
  3.    Test program for checking CTask DOS TSR compatibility.
  4.  
  5.    This program goes TSR, and then repeatedly reads a file, and
  6.    outputs status messages to the serial port.
  7.  
  8.    The program is called with res [filename] [port]
  9.    where [port] is  "-"    output to COM1 (relative)
  10.                     "-1"   output to COM1 (relative)
  11.                     "-2"   output to COM2 (relative)
  12.                     "1"    output to COM1 (absolute)
  13.    If no filename is given, nothing is done in the background.
  14.    If no port is given, COM1 (absolute) is used.
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <fcntl.h>
  21. #include <dos.h>
  22. #include <errno.h>
  23.  
  24. #include "tsk.h"
  25. #include "tskprf.h"
  26.  
  27. #define PORT   1
  28. #define BAUD   1152
  29.  
  30. #define STACKSIZE 2048
  31.  
  32. unsigned int _stklen = 2 * STACKSIZE;  /* For Turbo C: One task + main task Stack */
  33.  
  34. tcb tcb1;
  35.  
  36. byte fbuf[512];
  37. char fname[128];
  38.  
  39. int ofile;
  40.  
  41. #if (TSK_TURBO)
  42. #define stayres(len)    keep (0, (len))
  43. #else
  44. #define stayres(len)    _dos_keep (0, (len))
  45. #define sound(x)
  46. #define nosound()
  47. #endif
  48.  
  49. struct mem_control {
  50.                    byte id;
  51.                    word owner;
  52.                    word paragraphs;
  53.                    };
  54.  
  55. void exit_resident (void)
  56. {
  57.    struct mem_control far *mem;
  58.  
  59.    mem = MK_FP (_psp - 1, 0);
  60.    stayres (mem->paragraphs);
  61. }
  62.  
  63.  
  64. /*
  65.    Task1 does something in the backgound.
  66. */
  67.  
  68. void far task1 (void)
  69. {
  70.   int retval;
  71.   unsigned nr;
  72.  
  73.    while (1) 
  74.       {
  75.       t_delay (36L);  /* 2 seconds = 36 */
  76.       if (fname [0]) 
  77.          {
  78.          if (_dos_open (fname, O_RDONLY, &ofile)) 
  79.             {
  80.             tsk_cprintf ("\n*OF ERR %d*\007\n", errno);
  81.             sound (800);
  82.             t_delay (2L);
  83.             nosound ();
  84.             }
  85.          else 
  86.             {
  87.             tsk_cprintf ("O");
  88.             while (!(retval = _dos_read (ofile, fbuf, 512, &nr)))
  89.                {
  90.                if (!nr)
  91.                   break;
  92.                 t_delay (2L);
  93.                tsk_cprintf ("r");
  94.                }
  95.             if (retval)
  96.                {
  97.                tsk_cprintf ("\n*Rd ERR %d*\007\n", errno);
  98.                 sound (200);
  99.                t_delay (8L);
  100.                nosound ();
  101.                }
  102.             else 
  103.                {
  104.                tsk_cprintf ("C");
  105.                sound (4000);
  106.                t_delay (1L);
  107.                nosound ();
  108.                }
  109.             _dos_close (ofile);
  110.             }
  111.          }
  112.       else 
  113.          {
  114.          sound (200);
  115.          t_delay (1L);
  116.          nosound ();
  117.          }
  118.       }
  119. }
  120.  
  121.  
  122. int main (int argc, char *argv [])
  123. {
  124.    char stack1 [STACKSIZE];
  125.    int port, i;
  126.    
  127.    if (argc >= 2) 
  128.       strcpy (fname, argv [1]);
  129.    else 
  130.       fname[0] = '\0';
  131.  
  132.    port = PORT;
  133.    if (argc > 2)
  134.       {
  135.       port = 1;
  136.       i = 0;
  137.       if (argv [2][0] == '-')
  138.          i = 1;
  139.       if (argv [2][i] >= '1' && argv [2][i] <= '4')
  140.          port = argv [2][i] - '0';
  141.       }
  142.  
  143.    install_tasker (0, 0, IFL_STD, "Tres");
  144.    tsk_cprint_init (port, BAUD);
  145.  
  146.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("SIO-TASK"));
  147.  
  148.    start_task (&tcb1);
  149.    set_priority (NULL, PRI_STD);
  150.    preempt_on ();
  151.    yield ();
  152.    exit_resident ();
  153.    return 0; /* dummy */
  154. }
  155.  
  156.  
  157.