home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / ii-1 / systemtest.c < prev   
C/C++ Source or Header  |  1996-01-30  |  8KB  |  264 lines

  1. ;/* SystemTest.c - Execute me to compile me with SAS/C 6.56
  2. ;   Demonstration of System(), AUTO CON, and custom screen CON
  3. sc DATA=NEAR NMINC STRMERGE STREQ NOSTKCHK SAVEDS IGNORE=73 SystemTest.c
  4. slink FROM LIB:c.o,SystemTest.o TO SystemTest LIBRARY LIB:sc.lib,LIB:amiga.lib
  5. quit
  6. */
  7. /*
  8. Copyright (c) 1990 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga,
  11. Inc. for use with the Amiga Mail Volume II technical publication.
  12. Amiga Mail Volume II contains additional information on the correct
  13. usage of the techniques and operating system functions presented in
  14. these examples.  The source and executable code of these examples may
  15. only be distributed in free electronic form, via bulletin board or
  16. as part of a fully non-commercial and freely redistributable
  17. diskette.  Both the source and executable code (including comments)
  18. must be included, without modification, in any copy.  This example
  19. may not be published in printed form or distributed with any
  20. commercial product. However, the programming techniques and support
  21. routines set forth in these examples may be used in the development
  22. of original executable software products for Commodore Amiga
  23. computers.
  24.  
  25. All other rights reserved.
  26.  
  27. This example is provided "as-is" and is subject to change; no
  28. warranties are made.  All use is at your own risk. No liability or
  29. responsibility is assumed.
  30. */
  31.  
  32. #include <exec/types.h>
  33. #include <exec/libraries.h>
  34. #include <dos/dos.h>
  35. #include <dos/dostags.h>
  36. #include <intuition/intuition.h>
  37. #include <graphics/displayinfo.h>
  38.  
  39. #ifdef LATTICE
  40. #include <clib/exec_protos.h>
  41. #include <clib/dos_protos.h>
  42. #include <clib/intuition_protos.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  47. int chkabort(void) { return(0); }  /* really */
  48. #endif
  49.  
  50. /* our function error codes */
  51. #define SYSTEMFAIL      (-1L)
  52. #define WINDOWFAIL      (-2L)
  53.  
  54. /* function prototypes */
  55. LONG beginCommand(UBYTE *command);
  56. LONG doCommand(UBYTE *command, BPTR other);
  57. VOID checkResult(UBYTE *command, LONG result);
  58.  
  59.  
  60. /* Formatted version string for the 2.0 VERSION command */
  61. UBYTE *vers = "\0$VER: SystemTest 36.10";
  62.  
  63. struct Library *IntuitionBase;
  64.  
  65.  
  66. struct Screen *OpenScreenTags(struct NewScreen *,ULONG tags, ...);
  67. struct Window *OpenWindowTags(struct NewWindow *,ULONG tags, ...);
  68.  
  69.  
  70. void main(int argc, char **argv)
  71.     {
  72.     extern struct Library *DOSBase;
  73.     struct Screen *scr = NULL;
  74.     struct Window *win = NULL;
  75.     ULONG penspecterm = ~0;
  76.     LONG result;
  77.     BPTR file;
  78.     UBYTE *command;
  79.     UBYTE buf[128];
  80.  
  81.     if(DOSBase->lib_Version < 36)
  82.         {
  83.         printf("This example requires dos.library V36 or higher\n");
  84.         exit(RETURN_FAIL);
  85.         }
  86.     if(!(IntuitionBase = OpenLibrary("intuition.library",36)))
  87.         {
  88.         printf("This example requires intuition.library V36 or higher\n");
  89.         exit(RETURN_FAIL);
  90.         }
  91.  
  92.     /* SYNCHRONOUS SYSTEM() WITH OUR INPUT/OUTPUT
  93.      */
  94.     printf("\n*** SystemTest: Synchronous System call 'dir libs:':\n");
  95.     command = "dir libs:";
  96.     result = doCommand(command,NULL);
  97.     checkResult(command,result);
  98.     printf("\n*** SystemTest: Synchronous System call of nonexistant command:\n");
  99.     command = "badcommand";
  100.     result = doCommand(command,NULL);
  101.     checkResult(command,result);
  102.  
  103.     printf("\n*** SystemTest: Synchronous System call 'ask \"...Answer y now\"':\n");
  104.     command =
  105.         "ask \"Ready for CON: on a Custom Screen? Answer y now (should return 5):\"";
  106.     result = doCommand(command,NULL);
  107.     checkResult(command,result);
  108.  
  109.    /* SYNCHRONOUS SYSTEM() WITH CON: IN A CUSTOM SCREEN AND WINDOW
  110.      */
  111.     if(scr = OpenScreenTags(NULL,
  112.                 SA_Width, 640,
  113.                 SA_Height, 200,
  114.                 SA_Depth, 3,
  115.                 SA_DisplayID, HIRES_KEY,
  116.                 SA_Pens, &penspecterm,  /* Give us New Look */
  117.                 TAG_DONE))
  118.         {
  119.         if(win = OpenWindowTags(NULL,
  120.                 WA_CustomScreen, scr,
  121.                 WA_Flags, WINDOWDRAG|WINDOWCLOSE|ACTIVATE,
  122.                 WA_IDCMP, CLOSEWINDOW,
  123.                 WA_Top, 20,
  124.                 WA_Height, scr->Height - 20,
  125.                 WA_Title, "Custom Window",
  126.                 WA_ScreenTitle, "Custom Screen",
  127.                 TAG_DONE))
  128.             {
  129.             sprintf(buf,"CON://///WINDOW0x%lx", win); /* adds window pointer */
  130.             if(file = Open(buf, MODE_OLDFILE))
  131.                 {
  132.                 command = "echo \"CLI commands on a custom screen!\"";
  133.                 result = doCommand(command,file);
  134.                 command = "dir libs:";
  135.                 result = doCommand(command,file);
  136.                 command = "echo \"( Click CLOSE gadget, or type CTRL-C )\"";
  137.                 result = doCommand(command,file);
  138.                 Wait(1 << win->UserPort->mp_SigBit | SIGBREAKF_CTRL_C);
  139.                 Close(file);    /* Closes the window too */
  140.                 }
  141.             else CloseWindow(win);
  142.             }
  143.         CloseScreen(scr);
  144.         }
  145.  
  146.     printf("\n*** SystemTest: Synchronous System call 'ask \"...Answer y now\"':\n");
  147.     command = "ask \"Ready for Asynchronous demo? Answer y now (should return 5):\"";
  148.     result = doCommand(command,NULL);
  149.     checkResult(command,result);
  150.  
  151.  
  152.     /* ASYNCHRONOUS SYSTEM() WITH ON-DEMAND AUTO/WAIT CON:
  153.      */
  154.     printf("\n*** SystemTest: Asynchronous startup of 'Sys:Utilities/Clock':\n");
  155.     command = "SYS:Utilities/Clock";
  156.     result = beginCommand(command);
  157.     checkResult(command,result);
  158.  
  159.     printf("\n*** SystemTest: Asynchronous startup of 'avail':\n");
  160.     command = "avail";
  161.     result = beginCommand(command);
  162.     checkResult(command,result);
  163.  
  164.     printf("\nSystemTest exiting. Close Clock and Autocon window when you wish.\n");
  165.  
  166.     CloseLibrary(IntuitionBase);
  167.     exit(RETURN_OK);
  168.     }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /*
  175.  * Synchronous external command (wait for return)
  176.  * Uses your Input/Output unless you supply other handle
  177.  * Result will be return code of the command, unless the System() call
  178.  * itself fails, in which case the result will be -1
  179.  */
  180. LONG doCommand(UBYTE *command, BPTR other)
  181.     {
  182.     struct TagItem stags[4];
  183.  
  184.     stags[0].ti_Tag = SYS_Input;
  185.     stags[0].ti_Data = other ? other : Input();
  186.     stags[1].ti_Tag = SYS_Output;
  187.     stags[1].ti_Data = other ? NULL: Output();
  188.     stags[3].ti_Tag = TAG_DONE;
  189.     return(System(command, stags));
  190.     }
  191.  
  192.  
  193.  
  194.  
  195. /*
  196.  * Asynchronous external command started with its own autocon Input/Output
  197.  * This routine shows use of the SYS_UserShell tag as well.
  198.  * Result will only reflect whether System() call itself succeeded.
  199.  * If System() call fails, result will be -1L
  200.  * We are using -2L as result if our Open of CON: fails
  201.  */
  202. UBYTE *autocon="CON:0/40/640/150/Auto CON Window Opens if Needed/auto/close/wait";
  203. LONG beginCommand(UBYTE *command)
  204.     {
  205.     struct TagItem stags[5];
  206.     BPTR file;
  207.  
  208.     if(file = Open(autocon, MODE_OLDFILE))
  209.         {
  210.         stags[0].ti_Tag = SYS_Input;
  211.         stags[0].ti_Data = file;
  212.         stags[1].ti_Tag = SYS_Output;
  213.         stags[1].ti_Data = NULL;
  214.         stags[2].ti_Tag = SYS_Asynch;
  215.         stags[2].ti_Data = TRUE;
  216.         stags[3].ti_Tag = SYS_UserShell;
  217.         stags[3].ti_Data = TRUE;
  218.         stags[4].ti_Tag = TAG_DONE;
  219.         return(System(command, stags));
  220.         }
  221.     else return(WINDOWFAIL);
  222.     }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. /*
  229.  * Demo routine outputs result of System
  230.  */
  231. VOID checkResult(UBYTE *command, LONG result)
  232.     {
  233.     if(result == SYSTEMFAIL)
  234.         printf("*** SystemTest: could not start process for command\n");
  235.     else if(result == WINDOWFAIL)
  236.         printf("*** SystemTest: can't open con: for command\n");
  237.     else
  238.         printf("*** SystemTest: command (if synchronous) returned %ld\n",result);
  239.     }
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. /*
  247.  * Stack based stubs for opening screen, window
  248.  */
  249. struct Screen *OpenScreenTags(ns, tags)
  250. struct NewScreen *ns;
  251. ULONG tags;
  252.     {
  253.     return (OpenScreenTagList(ns, (struct TagItem *)&tags));
  254.     }
  255.  
  256. struct Window *OpenWindowTags(nw, tags)
  257. struct NewWindow *nw;
  258. ULONG tags;
  259.     {
  260.     return (OpenWindowTagList(nw, (struct TagItem *)&tags));
  261.     }
  262.  
  263.  
  264.