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

  1.  
  2. /*
  3.    Minimal test program for checking CTask DOS spawn compatibility.
  4.  
  5.    NOTE: Task 1 does a "screensnap" every second. This is only useful
  6.          in a dual-monitor system.
  7.          The monitor to use is determined by the program parameters:
  8.             SPAWNxx   - Don't do screensnap.
  9.             SPAWNxx M - Use mono monitor
  10.             SPAWNxx C - Use colour monitor
  11.  
  12.    NOTE: Task 3 repeatedly opens and closes the file 'C:\STATE.RST'
  13.          to test background DOS operations. If this file does not
  14.          exist, or can't be opened, the program will beep (Turbo C)
  15.          or output an error message (MS C). You should have a file
  16.          with that name in the C: root directory, or change Task 3.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <process.h>
  23. #include <dos.h>
  24.  
  25. #include "tsk.h"
  26. #include "tsksup.h"
  27. #include "tskprf.h"
  28. #include "kbd.h"
  29.  
  30. #if (TSK_MSC)
  31. #define sound(x)
  32. #define nosound()
  33. #endif
  34.  
  35. #define SEROUT    1     /* If non-0, tasks will signal activity on
  36.                            COM1-Port. */
  37.  
  38. #if (SEROUT)
  39. #define outser(p,x)    tsk_outp(p,x)
  40. #else
  41. #define outser(p,x)
  42. #endif
  43.  
  44. #define STACKSIZE 1024
  45.  
  46. unsigned int _stklen = 3 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  47.  
  48. tcb tcb1, tcb3;
  49. int scrnl, cscreen;
  50. int endrun, xendrun;
  51. char cmdname [64];
  52. char *cmd;
  53. struct {
  54.          word environ;
  55.          farptr cmdtail;
  56.          farptr fcb1;
  57.          farptr fcb2;
  58.          } exepar;
  59.  
  60. byte cmdtail [2] = { 0, 0x0d };
  61. byte dumfcb [] = { "           " };
  62.  
  63. tlink hotkey_mono;
  64. tlink hotkey_colour;
  65. tlink hotkey_none;
  66. int hot_screen = 0;
  67.  
  68. void Taskfunc hotproc_mono (tlinkptr elem)
  69. {
  70.    hot_screen = 'm';
  71.    wake_task (&tcb1);
  72. }
  73.  
  74. void Taskfunc hotproc_colour (tlinkptr elem)
  75. {
  76.    hot_screen = 'c';
  77.    wake_task (&tcb1);
  78. }
  79.  
  80. void Taskfunc hotproc_none (tlinkptr elem)
  81. {
  82.    hot_screen = -1;
  83.    wake_task (&tcb1);
  84. }
  85.  
  86.  
  87. #if (TSK_TURBO)
  88.  
  89. int handler (int errval, int ax, int bp, int si)
  90. {
  91.    return 0;
  92. }
  93.  
  94. int cbreak (void)
  95. {
  96.    return 1;
  97. }
  98.  
  99. #endif
  100.  
  101. /*
  102.    Task1 will display a screen snap on the selected monitor every second.
  103. */
  104.  
  105. void far task1 (void)
  106. {
  107.    while (!endrun)
  108.       {
  109.         t_delay (18L);
  110.       if (hot_screen > 0 && hot_screen != cscreen)
  111.          switch (cscreen = hot_screen)
  112.             {
  113.             case 'c':   tsk_set_colour (25, 80);
  114.                         break;
  115.             case 'm':   tsk_set_mono (25, 80);
  116.                         break;
  117.             default:    hot_screen = 0;
  118.                         break;
  119.             }
  120.  
  121.       if (hot_screen >= 0 && (scrnl || hot_screen))
  122.          {
  123.          hot_screen = 0;
  124.          screensnap (25);
  125.          }
  126.       }
  127. }
  128.  
  129.  
  130. /*
  131.    Task3 does something in the backgound.
  132. */
  133.  
  134. void far task3 (void)
  135. {
  136.    static char far *fname = "c:/state.rst";
  137.    union REGS regs;
  138.    struct SREGS sregs;
  139.  
  140.    while (!endrun)
  141.       {
  142.       t_delay (18L);
  143.       regs.x.ax = 0x3d00;
  144.       regs.x.dx = TFP_OFF(fname);
  145.       sregs.ds = TFP_SEG(fname);
  146.       intdosx (®s, ®s, &sregs);
  147.  
  148.       if (regs.x.cflag != 0) 
  149.          {
  150.          outser (0x3f8, '!');
  151. #if (!TSK_MSC)
  152.          sound (200);
  153.          t_delay (2L);
  154.          nosound ();
  155. #endif
  156.          }
  157.       else
  158.             {
  159.          outser (0x3f8, '+');
  160.          regs.x.bx = regs.x.ax;
  161.          regs.h.ah = 0x3e;
  162.          intdos (®s, ®s);
  163.          }
  164.       }
  165. }
  166.  
  167.  
  168. int main (int argc, char *argv [])
  169. {
  170.    char stack1 [STACKSIZE];
  171.    char stack3 [STACKSIZE];
  172.    struct SREGS sregs;
  173.    union REGS regs;
  174.    int c;
  175.  
  176. #if(TSK_TURBO)
  177.    harderr (handler);
  178.    ctrlbrk (cbreak);
  179. #endif
  180.  
  181.    c = (argc < 2) ? 'x' : tolower (argv [1][0]);
  182.    cmd = getenv ("COMSPEC");
  183.    strcpy (cmdname, (cmd == NULL) ? "command.com" : cmd);
  184.  
  185.    exepar.cmdtail = cmdtail;
  186.    exepar.environ = 0;
  187.    exepar.fcb1 = dumfcb;
  188.    exepar.fcb2 = dumfcb;
  189.    regs.x.ax = 0x4b00;
  190.    regs.x.dx = TFP_OFF(cmdname);
  191.    sregs.ds = TFP_SEG(cmdname);
  192.    regs.x.bx = TFP_OFF(&exepar);
  193.    sregs.es = TFP_SEG(&exepar);
  194.  
  195.    endrun = xendrun = 0;
  196.    install_tasker (0, 0, IFL_INT8_DIR, "Spawn");
  197.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("SnapTask"));
  198.    create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL TN("DosCTask"));
  199.  
  200.    switch (c)
  201.       {
  202.       case 'c':   tsk_set_colour (25, 80);
  203.                   scrnl = cscreen = 'c';
  204.                   break;
  205.       case 'm':   tsk_set_mono (25, 80);
  206.                   scrnl = cscreen = 'm';
  207.                   break;
  208.       default:    scrnl = cscreen = 0;
  209.                   break;
  210.       }
  211.  
  212.    start_task (&tcb1);
  213.    start_task (&tcb3);
  214.    preempt_on ();
  215.  
  216.    create_hotkey_entry (&hotkey_mono, SCAN_M, 
  217.                         KF1_ALT | KF1_CONTROL | KF1_LEFTSHIFT, KF1_CONTROL | KF1_LEFTSHIFT,
  218.                         0, 0,
  219.                         0, 0,
  220.                         (farptr)hotproc_mono, TKIND_PROC, 1, LNULL);
  221.    create_hotkey_entry (&hotkey_colour, SCAN_C, 
  222.                         KF1_ALT | KF1_CONTROL | KF1_LEFTSHIFT, KF1_CONTROL | KF1_LEFTSHIFT,
  223.                         0, 0,
  224.                         0, 0,
  225.                         (farptr)hotproc_colour, TKIND_PROC, 1, LNULL);
  226.    create_hotkey_entry (&hotkey_none, SCAN_N, 
  227.                         KF1_ALT | KF1_CONTROL | KF1_LEFTSHIFT, KF1_CONTROL | KF1_LEFTSHIFT,
  228.                         0, 0,
  229.                         0, 0,
  230.                         (farptr)hotproc_none, TKIND_PROC, 1, LNULL);
  231.  
  232.    set_priority (NULL, PRI_STD);
  233.    tsk_printf ("Spawning...\n");
  234.    intdosx (®s, ®s, &sregs);
  235.    tsk_printf ("Returned from spawn, Carry = %d, AX = %04X\n", 
  236.                regs.x.cflag, regs.x.ax);
  237.  
  238.    endrun = 1;
  239.    tsk_puts ("******** Main Task *********");
  240.  
  241.    schedule ();
  242.  
  243.    xendrun = 1;
  244.    delete_hotkey (&hotkey_mono);
  245.    delete_hotkey (&hotkey_colour);
  246.    delete_hotkey (&hotkey_none);
  247.  
  248.    preempt_off ();
  249.    remove_tasker ();
  250.    tsk_puts ("******** End Run *********");
  251.    return 0;
  252. }
  253.  
  254.