home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / AmigaMail / System / SystemTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  6.5 KB  |  236 lines

  1. ;/* SystemTest.c - Execute me to compile me with Lattice 5.04
  2. ;   Demonstration of System(), AUTO CON, and custom screen CON
  3. LC -b1 -cfistq -v -y -j73 SystemTest.c
  4. Blink FROM LIB:c.o,SystemTest.o TO SystemTest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  5. quit
  6. */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/libraries.h>
  10. #include <dos/dos.h>
  11. #include <dos/dostags.h>
  12. #include <intuition/intuition.h>
  13. #include <graphics/displayinfo.h>
  14.  
  15. #ifdef LATTICE
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  23. int chkabort(void) { return(0); }  /* really */
  24. #endif
  25.  
  26. /* our function error codes */
  27. #define SYSTEMFAIL    (-1L)
  28. #define WINDOWFAIL    (-2L)
  29.  
  30. /* function prototypes */
  31. LONG beginCommand(UBYTE *command);
  32. LONG doCommand(UBYTE *command, BPTR other);
  33. VOID checkResult(UBYTE *command, LONG result);
  34.  
  35.  
  36. /* Formatted version string for the 2.0 VERSION command */ 
  37. UBYTE *vers = "\0$VER: SystemTest 37.1";
  38.  
  39. struct Library *IntuitionBase;
  40.  
  41.  
  42. struct Screen *OpenScreenTags(struct NewScreen *,ULONG tags, ...);
  43. struct Window *OpenWindowTags(struct NewWindow *,ULONG tags, ...);
  44.  
  45.  
  46. void main(int argc, char **argv)
  47.     {
  48.     extern struct Library *DOSBase;
  49.     struct Screen *scr = NULL;
  50.     struct Window *win = NULL;
  51.     ULONG penspecterm = ~0;
  52.     LONG result;
  53.     BPTR file;
  54.     UBYTE *command;
  55.     UBYTE buf[128];
  56.  
  57.     if(DOSBase->lib_Version < 36)
  58.     {
  59.         printf("This example requires dos.library V36 or higher\n");
  60.     exit(RETURN_FAIL);
  61.     }
  62.     if(!(IntuitionBase = OpenLibrary("intuition.library",36)))
  63.     {
  64.         printf("This example requires intuition.library V36 or higher\n");
  65.     exit(RETURN_FAIL);
  66.     }
  67.  
  68.     /* SYNCHRONOUS SYSTEM() WITH OUR INPUT/OUTPUT
  69.      */
  70.     printf("\n*** SystemTest: Synchronous System call 'dir libs:':\n");
  71.     command = "dir libs:";
  72.     result = doCommand(command,NULL);
  73.     checkResult(command,result);
  74.  
  75.     printf("\n*** SystemTest: Synchronous System call of nonexistant command:\n");
  76.     command = "badcommand";
  77.     result = doCommand(command,NULL);
  78.     checkResult(command,result);
  79.  
  80.     printf("\n*** SystemTest: Synchronous System call using the ASK command:\n");
  81.     command = "ask \"Ready for CON: on a Custom Screen? Answer y now (should return 5):\"";
  82.     result = doCommand(command,NULL);
  83.     checkResult(command,result);
  84.  
  85.  
  86.     /* SYNCHRONOUS SYSTEM() WITH CON: IN A CUSTOM SCREEN AND WINDOW
  87.      */
  88.     if(scr = OpenScreenTags(NULL,
  89.         SA_Width, 640,
  90.         SA_Height, 200,
  91.         SA_Depth, 3,
  92.         SA_DisplayID, HIRES_KEY,
  93.         SA_Pens, &penspecterm,    /* Give us New Look */
  94.         TAG_DONE))
  95.     {
  96.         if(win = OpenWindowTags(NULL,
  97.         WA_CustomScreen, scr,
  98.         WA_Flags, WINDOWDRAG|WINDOWCLOSE|ACTIVATE,
  99.         WA_IDCMP, CLOSEWINDOW,
  100.         WA_Top, 20,
  101.         WA_Height, scr->Height - 20,
  102.         WA_Title, "Custom Window",
  103.         WA_ScreenTitle, "Custom Screen",
  104.         TAG_DONE))        
  105.             {
  106.         sprintf(buf,"CON://///WINDOW0x%lx", win); /* adds window pointer */
  107.         if(file = Open(buf, MODE_OLDFILE))
  108.             {
  109.             command = "echo \"CLI commands on a custom screen!\"";
  110.             result = doCommand(command,file);
  111.             command = "dir libs:";
  112.             result = doCommand(command,file);
  113.             command = "echo \"( Click CLOSE gadget, or type CTRL-C )\"";
  114.             result = doCommand(command,file);
  115.         Wait(1 << win->UserPort->mp_SigBit | SIGBREAKF_CTRL_C);
  116.             Close(file);    /* Closes the window too */
  117.             }
  118.         else CloseWindow(win);
  119.         }
  120.     CloseScreen(scr);
  121.     }
  122.  
  123.     printf("\n*** SystemTest: Synchronous System call using the ASK command:\n");
  124.     command = "ask \"Ready for Asynchronous demo? Answer y now (should return 5):\"";
  125.     result = doCommand(command,NULL);
  126.     checkResult(command,result);
  127.  
  128.  
  129.     /* ASYNCHRONOUS SYSTEM() WITH ON-DEMAND AUTO/WAIT CON:
  130.      */
  131.     printf("\n*** SystemTest: Asynchronous startup of 'Sys:Utilities/Clock':\n");
  132.     command = "SYS:Utilities/Clock";
  133.     result = beginCommand(command);
  134.     checkResult(command,result);
  135.  
  136.     printf("\n*** SystemTest: Asynchronous startup of 'avail':\n");
  137.     command = "avail";
  138.     result = beginCommand(command);
  139.     checkResult(command,result);
  140.  
  141.     printf("\nSystemTest exiting. Close Clock and Autocon window when you wish.\n");
  142.  
  143.     CloseLibrary(IntuitionBase);
  144.     exit(RETURN_OK);
  145.     }
  146.  
  147.  
  148.  
  149. /*
  150.  * Synchronous external command (wait for return)
  151.  * Uses your Input/Output unless you supply other handle
  152.  * Result will be return code of the command, unless the System() call
  153.  * itself fails, in which case the result will be -1
  154.  */
  155. LONG doCommand(UBYTE *command, BPTR other)
  156.     {
  157.     struct TagItem stags[4];
  158.  
  159.     stags[0].ti_Tag = SYS_Input;
  160.     stags[0].ti_Data = other ? other : Input();
  161.     stags[1].ti_Tag = SYS_Output;
  162.     stags[1].ti_Data = other ? NULL: Output();
  163.     stags[3].ti_Tag = TAG_DONE;
  164.     return(System(command, stags));
  165.     }
  166.  
  167.  
  168. /*
  169.  * Asynchronous external command started with its own autocon Input/Output
  170.  * This routine shows use of the SYS_UserShell tag as well.
  171.  * Result will only reflect whether System() call itself succeeded.
  172.  * If System() call fails, result will be -1L, and we must close the window.
  173.  * On an asynchronous System such as this, failure will only reflect
  174.  *   whether System failed, not whether the command failed.
  175.  * We are using -2L as result if our Open of CON: fails
  176.  */
  177. UBYTE *autocon="CON:0/40/640/150/Auto CON Window Opens if Needed/auto/close/wait";
  178. LONG beginCommand(UBYTE *command)
  179.     {
  180.     struct TagItem stags[5];
  181.     LONG result = WINDOWFAIL;
  182.     BPTR file;
  183.  
  184.     if(file = Open(autocon, MODE_OLDFILE))
  185.     {
  186.     stags[0].ti_Tag = SYS_Input;
  187.     stags[0].ti_Data = file;
  188.     stags[1].ti_Tag = SYS_Output;
  189.     stags[1].ti_Data = NULL;
  190.     stags[2].ti_Tag = SYS_Asynch;
  191.     stags[2].ti_Data = TRUE;
  192.     stags[3].ti_Tag = SYS_UserShell;
  193.     stags[3].ti_Data = TRUE;
  194.     stags[4].ti_Tag = TAG_DONE;
  195.         result = System(command, stags);
  196.     if(result == -1L)
  197.         {
  198.         Close(file);
  199.         }
  200.     }
  201.     return(result);
  202.     }
  203.  
  204.  
  205. /*
  206.  * Demo routine outputs result of System
  207.  */
  208. VOID checkResult(UBYTE *command, LONG result)
  209.     {
  210.     if(result == SYSTEMFAIL)
  211.     printf("*** CheckResult: could not start process for command\n"); 
  212.     else if(result == WINDOWFAIL)
  213.     printf("*** CheckResult: can't open con: for command\n"); 
  214.     else
  215.     printf("*** CheckResult: command (if synchronous) returned %ld\n",result); 
  216.     }
  217.  
  218.  
  219. /*
  220.  * Stack based stubs for opening screen, window
  221.  */
  222. struct Screen *OpenScreenTags(ns, tags)
  223. struct NewScreen *ns;
  224. ULONG tags;
  225.     {
  226.     return (OpenScreenTagList(ns, (struct TagItem *)&tags));
  227.     }
  228.  
  229. struct Window *OpenWindowTags(nw, tags)
  230. struct NewWindow *nw;
  231. ULONG tags;
  232.     {
  233.     return (OpenWindowTagList(nw, (struct TagItem *)&tags));
  234.     }
  235.  
  236.