home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_02 / res.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  4KB  |  184 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 <io.h>
  22. #include <errno.h>
  23.  
  24. #include "tsk.h"
  25. #include "sio.h"
  26.  
  27. #define PORT   0x00     /* COM1, absolute */
  28. #define BAUD   9600L    /* Baudrate */
  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. sioptr siop;
  36.  
  37. byte fbuf[512];
  38. char fname[128];
  39. word rcvbuf [128];
  40. byte xmtbuf [128];
  41.  
  42. int ofile;
  43.  
  44. #if (TSK_TURBO)
  45. #define stayres(len)    keep (0, (len))
  46. #else
  47. #define stayres(len)    _dos_keep (0, (len))
  48. #define sound(x)
  49. #define nosound()
  50. #endif
  51.  
  52. struct mem_control {
  53.                    byte id;
  54.                    word owner;
  55.                    word paragraphs;
  56.                    };
  57.  
  58. void exit_resident (void)
  59. {
  60.    struct mem_control far *mem;
  61.  
  62.    mem = MK_FP (_psp - 1, 0);
  63.    stayres (mem->paragraphs);
  64. }
  65.  
  66.  
  67. void comprintf (char *fmt, ...)
  68. {
  69.    va_list argptr;
  70.    char buf [256];
  71.    int res, i;
  72.  
  73.    va_start (argptr, fmt);
  74.    res = vsprintf (buf, fmt, argptr);
  75.    for (i = 0; i < res; i++)
  76.       v24_send (siop, buf [i], 0L);
  77.    va_end (argptr);
  78. }
  79.  
  80.  
  81. /*
  82.    Task1 does something in the backgound.
  83. */
  84.  
  85. void far task1 (void)
  86. {
  87.   int retval;
  88.  
  89.    while (1) 
  90.       {
  91.       t_delay (36L);  /* 2 seconds = 36 */
  92.       if (fname [0]) 
  93.          {
  94.          if ((ofile = open (fname, O_RDONLY)) == -1) 
  95.             {
  96.             comprintf ("*** Open file ERROR %d ***\n\n", errno);
  97.             sound (800);
  98.             t_delay (2L);
  99.             nosound ();
  100.             }
  101.          else 
  102.             {
  103.             comprintf ("Open file OK\n");
  104.             while ((retval = read (ofile, fbuf, 512)) > 0) 
  105.                {
  106.                 t_delay (2L);
  107.                comprintf ("Read block OK\n");
  108.                }
  109.             if (retval == -1)
  110.                {
  111.                comprintf ("*** Read block ERROR %d ***\n", errno);
  112.                 sound (200);
  113.                t_delay (8L);
  114.                nosound ();
  115.                }
  116.             else 
  117.                {
  118.                comprintf ("Read File Complete\n\n");
  119.                sound (4000);
  120.                t_delay (1L);
  121.                nosound ();
  122.                }
  123.             close (ofile);
  124.             }
  125.          }
  126.       else 
  127.          {
  128.          sound (200);
  129.          t_delay (1L);
  130.          nosound ();
  131.          }
  132.       }
  133. }
  134.  
  135.  
  136. int main (int argc, char *argv [])
  137. {
  138.    char stack1 [STACKSIZE];
  139.    int port, i;
  140.    
  141.    if (argc >= 2) 
  142.       strcpy (fname, argv [1]);
  143.    else 
  144.       fname[0] = '\0';
  145.  
  146.    port = PORT;
  147.    if (argc > 2)
  148.       {
  149.       port = 0;
  150.       i = 0;
  151.       if (argv [2][0] == '-')
  152.          {
  153.          port = 0x80;
  154.          i = 1;
  155.          }
  156.       if (argv [2][i] >= '1' && argv [2][i] <= '3')
  157.          port |= argv [2][i] - '1';
  158.       }
  159.  
  160.    install_tasker (0, 0, IFL_STD, "Tsio");
  161.    siop = v24_install (port, 1, rcvbuf, sizeof (rcvbuf), 
  162.                        xmtbuf, sizeof (xmtbuf));
  163.  
  164.    if (siop == NULL)
  165.       {
  166.       remove_tasker ();
  167.       return 1;
  168.       }
  169.  
  170.    v24_change_baud (siop, BAUD);
  171.    v24_protocol (siop, XONXOFF, 40, 60);
  172.  
  173.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("SIO-TASK"));
  174.  
  175.    start_task (&tcb1);
  176.    set_priority (NULL, PRI_STD);
  177.    preempt_on ();
  178.    schedule ();
  179.    exit_resident ();
  180.    return 0; /* dummy */
  181. }
  182.  
  183.  
  184.