Advertisement
micha_b

mb User-Shell

Apr 19th, 2023 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | Help | 0 0
  1. /*
  2.     Programme synchron/asynchron in Shell starten
  3.     (öffnet sich selbstständig, wenn erforderlich!)
  4. */
  5.  
  6. #include    <exec/types.h>
  7. #include    <exec/libraries.h>
  8. #include    <dos/dos.h>
  9. #include    <dos/dostags.h>
  10.  
  11. #include    <proto/exec.h>
  12. #include    <proto/dos.h>
  13. #include    <proto/intuition.h>
  14. #include    <proto/utility.h>
  15.  
  16. /* our function error codes for user shell functions */
  17. #define SYSTEMFAIL      (-1L)
  18. #define WINDOWFAIL      (-2L)
  19.  
  20. LONG beginCommand(UBYTE *command);              // asynchrone Ausgabe -> aufrufende Shell
  21. LONG doCommand(UBYTE *command, BPTR other);     // synchrone Ausgabe mit AutoCon
  22. VOID checkResult(UBYTE *command, LONG result);  // testet Rückgabewert der Shell
  23.  
  24. /*
  25.  * Synchronous external command (wait for return)
  26.  * Uses your Input/Output unless you supply other handle
  27.  * Result will be return code of the command, unless the System() call
  28.  * itself fails, in which case the result will be -1
  29.  */
  30. LONG doCommand(UBYTE *command, BPTR other)
  31. {
  32.     struct TagItem stags[4];
  33.  
  34.     stags[0].ti_Tag = SYS_Input;
  35.     stags[0].ti_Data = other ? other : Input();
  36.     stags[1].ti_Tag = SYS_Output;
  37.     stags[1].ti_Data = other ? NULL: Output();
  38.     stags[3].ti_Tag = TAG_END;
  39.     return System(command, stags);
  40. }
  41.  
  42.  
  43. /*
  44.  * Asynchronous external command started with its own autocon Input/Output
  45.  * This routine shows use of the SYS_UserShell tag as well.
  46.  * Result will only reflect whether System() call itself succeeded.
  47.  * If System() call fails, result will be -1L
  48.  * We are using -2L as result if our Open of CON: fails
  49.  */
  50. UBYTE *autocon="CON:60/94/640/150/My private Shell (burn after reading!)/shell/auto/close/wait";
  51. LONG beginCommand(UBYTE *command)
  52. {
  53.     struct TagItem stags[5];
  54.     BPTR file;
  55.  
  56.     if(file = Open(autocon, MODE_OLDFILE))
  57.         {
  58.         stags[0].ti_Tag = SYS_Input;
  59.         stags[0].ti_Data = file;
  60.         stags[1].ti_Tag = SYS_Output;
  61.         stags[1].ti_Data = NULL;
  62.         stags[2].ti_Tag = SYS_Asynch;
  63.         stags[2].ti_Data = TRUE;
  64.         stags[3].ti_Tag = SYS_UserShell;
  65.         stags[3].ti_Data = TRUE;
  66.         stags[4].ti_Tag = TAG_END;
  67.         return System(command, stags);
  68.         }
  69.     else return(WINDOWFAIL);
  70. }
  71.  
  72.  
  73. /*
  74.  * Demo routine outputs result of System
  75.  */
  76. VOID checkResult(UBYTE *command, LONG result)
  77. {
  78.     if(result == SYSTEMFAIL)
  79.         Printf("*** SystemTest: could not start process for command\n");
  80.     else if(result == WINDOWFAIL)
  81.         Printf("*** SystemTest: can't open con: for command\n");
  82.     else
  83.         Printf("*** SystemTest: command (if synchronous) returned %ld\n",result);
  84. }
  85.  
  86. /* -------- DEMO ---------------------------------------------------------------------- */
  87. int main()
  88. {
  89.     extern struct DosLibrary *DOSBase;
  90.     struct Screen *scr = NULL;
  91.     struct Window *win = NULL;
  92.     ULONG penspecterm = ~0;
  93.     LONG result;
  94.     BPTR file;
  95.     UBYTE *command;
  96.     UBYTE buf[128];
  97.    
  98.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39L)))
  99.     {
  100.         Printf("This example requires intuition.library V39 or higher\n");
  101.         return RETURN_FAIL;
  102.     }
  103.     //Printf("\f");
  104.    
  105.     // SYNCHRONE Ausgabe an aufrufende Shell:
  106.     Printf("\n*** SystemTest: Synchronous System call 'dir libs:':\n");
  107.     command = "dir libs:";
  108.     result = doCommand(command, NULL);
  109.     checkResult(command,result);
  110.    
  111.     Printf("\n*** SystemTest: Synchronous System call of nonexistant command:\n");
  112.     command = "badcommand";
  113.     result = doCommand(command,NULL);
  114.     checkResult(command,result);
  115.    
  116.     Printf("\n*** SystemTest: Synchronous System call 'ask \"...Answer y now\"':\n");
  117.     command = "ask \"Ready for Asynchronous demo? Answer y now (should return 5):\"";
  118.     result = doCommand(command,NULL);
  119.     checkResult(command,result);
  120.    
  121.     Printf("\n*** SystemTest: Synchronous startup of 'avail':\n");
  122.     command = "System:C/avail";
  123.     result = doCommand(command, NULL);
  124.     checkResult(command,result);
  125.    
  126.     /* ASYNCHRONOUS SYSTEM() WITH ON-DEMAND AUTO/WAIT CON:
  127.      */
  128.     Printf("\n*** SystemTest: Asynchronous startup of 'avail':\n");
  129.     command = "Stack";
  130.     result = beginCommand(command);
  131.     checkResult(command,result);
  132.      
  133.     Printf("\n*** SystemTest: Asynchronous startup of 'Sys:Utilities/Clock':\n");
  134.     command = "SYS:Utilities/Clock LEFT 2 TOP 15 WIDTH 150 HEIGHT 170 SECONDS DATE";
  135.     result = beginCommand(command);
  136.     checkResult(command,result);
  137.    
  138.     Printf("\n*** SystemTest: Asynchronous System call of nonexistant command:\n");
  139.     command = "badcommand";
  140.     result = beginCommand(command);
  141.     checkResult(command,result);
  142.    
  143.     Printf("\nSystemTest exiting. Close Clock and AutoCon window when you wish.\n");
  144.  
  145.     CloseLibrary((struct Library *)IntuitionBase);
  146.     return RETURN_OK;    
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement