home *** CD-ROM | disk | FTP | other *** search
/ AMIGA PD 1 / AMIGA-PD-1.iso / Programme_zum_Heft / Anwendungen / Kurztests / GoldED / data / tools / GEDApp / main.c < prev    next >
C/C++ Source or Header  |  1995-02-19  |  8KB  |  406 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   GEDApp v1.0 - GoldED AppIcon handler, ©1995 Dietmar Eilert
  4.  
  5.   DICE:
  6.  
  7.   dcc main.c appIconA.a -// -proto -mRR -mi -pr -2.0 -o ram:GEDApp
  8.  
  9.   ------------------------------------------------------------------------------
  10. */
  11.  
  12. /// "includes"
  13.  
  14. #include <amiga20/exec/exec.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <amiga20/intuition/intuition.h>
  21. #include <amiga20/dos/dos.h>
  22. #include <amiga20/dos/dosextens.h>
  23. #include <amiga20/dos/rdargs.h>
  24. #include <amiga20/dos/dostags.h>
  25. #include <amiga20/workbench/startup.h>
  26. #include <amiga20/workbench/workbench.h>
  27. #include <amiga20/rexx/errors.h>
  28. #include <amiga20/rexx/rxslib.h>
  29.  
  30. #include <amiga20/clib/alib_protos.h>
  31. #include <amiga20/clib/dos_protos.h>
  32. #include <amiga20/clib/exec_protos.h>
  33. #include <amiga20/clib/icon_protos.h>
  34. #include <amiga20/clib/intuition_protos.h>
  35. #include <amiga20/clib/utility_protos.h>
  36. #include <amiga20/clib/rexxsyslib_protos.h>
  37. #include <amiga20/clib/wb_protos.h>
  38.  
  39. #ifdef PRAGMAS
  40.  
  41. #include "Pragmas/exec.h"
  42. #include "Pragmas/disk.h"
  43. #include "Pragmas/diskfont.h"
  44. #include "Pragmas/dynamic.h"
  45. #include "Pragmas/gadtools.h"
  46. #include "Pragmas/keymap.h"
  47. #include "Pragmas/graphics.h"
  48. #include "Pragmas/icon.h"
  49. #include "Pragmas/input.h"
  50. #include "Pragmas/intuition.h"
  51. #include "Pragmas/layers.h"
  52. #include "Pragmas/locale.h"
  53. #include "Pragmas/misc.h"
  54. #include "Pragmas/timer.h"
  55. #include "Pragmas/wb.h"
  56. #include "Pragmas/xpkmaster.h"
  57. #include "Pragmas/amigaguide.h"
  58. #include "Pragmas/reqtools.h"
  59.  
  60. #endif
  61.  
  62. #define Prototype    extern
  63. #define MAX_LEN      150
  64.  
  65. ///
  66. /// "prototypes"
  67.  
  68. Prototype void   main(ULONG, char **);
  69. Prototype int    wbmain(struct WBStartup *);
  70. Prototype void   MainLoop(void);
  71. Prototype char  *MakeFileName(char *, char *);
  72. Prototype char  *CompletePath(char *);
  73. Prototype char  *StartGED(void);
  74. Prototype struct RexxMsg *SendRexxCommand(char *, char *, struct MsgPort *);
  75. Prototype void   FreeRexxCommand (struct RexxMsg *);
  76. Prototype ULONG  WaitForAnswer(struct MsgPort *);
  77. Prototype char  *LookForGED(void);
  78. Prototype void   ReadWBCmd(ULONG, struct WBArg *);
  79.  
  80. extern struct Library *IconBase;
  81. extern struct Library *DOSBase;
  82. extern struct Library *SysBase;
  83. extern struct Library *IntuitionBase;
  84. extern struct Library *WorkbenchBase;
  85.  
  86. ///
  87. /// "entry points"
  88.  
  89. void
  90. main(argc, argv)
  91.  
  92. ULONG argc;
  93. char *argv[];
  94. {
  95.     MainLoop();
  96. }
  97.  
  98. int
  99. wbmain(struct WBStartup *wbs)
  100. {
  101.     MainLoop();
  102. }
  103.  
  104.  
  105. ///
  106. /// "main loop"
  107.  
  108. /* --------------------------------- MainLoop ----------------------------------
  109.  
  110.  Open AppIcon, handle incoming messages
  111.  
  112. */
  113.  
  114. void
  115. MainLoop()
  116. {
  117.     const char *version = "$VER: GEDApp 1.0 (26.1.95)";
  118.  
  119.     struct DiskObject *appDiskObject;
  120.  
  121.     if (!(appDiskObject = GetDiskObject("golded:config/AppIcon")))
  122.         appDiskObject = GetDefDiskObject(WBTOOL);
  123.  
  124.     if (appDiskObject) {
  125.  
  126.         struct MsgPort     *msgPort;
  127.         struct AppMessage  *amsg;
  128.         struct AppIcon     *appIcon;
  129.  
  130.         if (msgPort = CreateMsgPort()) {
  131.  
  132.             if (appIcon = AddAppIconA(0, NULL, "GED", msgPort, NULL, appDiskObject, TAG_END)) {
  133.  
  134.                 BOOL terminated = FALSE;
  135.  
  136.                 while (!terminated) {
  137.  
  138.                     WaitPort(msgPort);
  139.  
  140.                     while (amsg = (struct AppMessage *)GetMsg(msgPort)) {
  141.  
  142.                         if (amsg->am_NumArgs)
  143.                             ReadWBCmd(amsg->am_NumArgs, amsg->am_ArgList);
  144.                         else
  145.                             terminated = TRUE;
  146.  
  147.                         ReplyMsg((struct Message *)amsg);
  148.                     }
  149.                 }
  150.             }
  151.             else
  152.                 puts("Couldn't allocate AppIcon. Workbench closed ?!");
  153.  
  154.             RemoveAppIcon(appIcon);
  155.  
  156.             DeleteMsgPort(msgPort);
  157.         }
  158.         else
  159.             puts("Couldn't create message port ?!");
  160.  
  161.         FreeDiskObject(appDiskObject);
  162.     }
  163.     else
  164.         puts("Impossible d'allouer DiskObject ?!");
  165.  
  166.     exit(0);
  167. }
  168.  
  169.  
  170. ///
  171. /// "misc"
  172.  
  173. /* ------------------------------- MakeFileName --------------------------------
  174.  
  175.  Build fully qualified path from file/path names; return pointer to static copy.
  176.  
  177. */
  178.  
  179. char *
  180. MakeFileName(path, file)
  181.  
  182. char *path, *file;
  183. {
  184.     static char buffer[MAX_LEN + 1];
  185.  
  186.     strcpy(buffer, "\42");
  187.  
  188.     strcat(buffer, path);
  189.  
  190.     CompletePath(buffer);
  191.  
  192.     strcat(buffer, file);
  193.  
  194.     strcat(buffer, "\42");
  195.  
  196.     return(buffer);
  197. }
  198.  
  199. /* ------------------------------ CompletePath -----------------------------------
  200.  
  201.  Add '/' to path if missing so far
  202.  
  203. */
  204.  
  205. char *
  206. CompletePath(char *path)
  207. {
  208.     UWORD len;
  209.  
  210.     if (len = strlen(path))
  211.         if ((path[len - 1] != ':') && (path[len - 1] != '/'))
  212.             strcat(path, "/");
  213.  
  214.     return(path);
  215. }
  216.  
  217. /* ---------------------------------- ReadWBCmd --------------------------------
  218.  
  219.  Parse AppIcon message
  220.  
  221. */
  222.  
  223. void
  224. ReadWBCmd(numArgs, argList)
  225.  
  226. ULONG  numArgs;
  227. struct WBArg  *argList;
  228. {
  229.     char *host;
  230.     BOOL loadGED = !(host = LookForGED());
  231.  
  232.     if (loadGED)
  233.         host = StartGED();
  234.  
  235.     if (host) {
  236.  
  237.         struct MsgPort *replyPort;
  238.  
  239.         if (replyPort = CreateMsgPort()) {
  240.  
  241.             if (SendRexxCommand(host, "LOCK CURRENT", replyPort)) {
  242.  
  243.                 if (WaitForAnswer(replyPort) == RC_OK) {
  244.  
  245.                     UWORD count;
  246.                     char  path[MAX_LEN + 1], *command;
  247.  
  248.                     for (count = 0; numArgs--; count++) {
  249.  
  250.                         NameFromLock(argList[count].wa_Lock, path, MAX_LEN);
  251.  
  252.                         command = MakeFileName(path, argList[count].wa_Name);
  253.  
  254.                         strins(command, "OPEN SMART QUIET ");
  255.  
  256.                         if (SendRexxCommand(host, command, replyPort))
  257.  
  258.                             WaitForAnswer(replyPort);
  259.                     }
  260.                 }
  261.  
  262.                 if (SendRexxCommand(host, "UNLOCK", replyPort))
  263.  
  264.                     WaitForAnswer(replyPort);
  265.             }
  266.  
  267.             DeleteMsgPort(replyPort);
  268.         }
  269.     }
  270. }
  271.  
  272.  
  273. /* ----------------------------------- LookForGED ----------------------------
  274.  
  275.  Look for running GoldED task
  276.  
  277. */
  278.  
  279. char *
  280. LookForGED()
  281. {
  282.     static char host[] = "GOLDED.1";
  283.     UWORD  try;
  284.  
  285.     for (try = '1'; try <= '9'; try++) {
  286.  
  287.         host[7] = try;
  288.  
  289.         if (FindPort(host))
  290.             return(host);
  291.     } 
  292.  
  293.     return(NULL);
  294. }
  295.  
  296. /* ------------------------------------- StartGED -----------------------------
  297.  
  298.  Launch a new GoldED task. Return pointer to host name (or NULL).
  299.  
  300. */
  301.  
  302. char *
  303. StartGED()
  304. {
  305.     static char *host = "GOLDED.1";
  306.  
  307.     if (!SystemTags("GoldED:GoldED", SYS_Asynch, TRUE, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE))
  308.  
  309.         UWORD try;
  310.  
  311.         for (try = 50; try; try--, Delay(10))
  312.             if (FindPort(host))
  313.                 return(host);
  314.  
  315.     return(FALSE);
  316. }
  317.  
  318. ///
  319. /// "ARexx"
  320.  
  321. /* -------------------------------------- WaitForAnswer -----------------------
  322.  
  323.   Wait for answer on previously sent message. Free message afterwards. Primary
  324.   return code is returned.
  325.  
  326. */
  327.  
  328. ULONG
  329. WaitForAnswer(port)
  330.  
  331. struct MsgPort *port;
  332. {
  333.     struct RexxMsg *rexxMsg;
  334.     ULONG  result;
  335.  
  336.     do {
  337.         
  338.         WaitPort(port);
  339.  
  340.         if (rexxMsg = (struct RexxMsg *)GetMsg(port))
  341.             result = rexxMsg->rm_Result1;
  342.  
  343.     } while (!rexxMsg);
  344.  
  345.     FreeRexxCommand(rexxMsg);
  346.  
  347.     return(result);
  348. }
  349.  
  350. /* ------------------------------------- FreeRexxCommand ----------------------
  351.  
  352.  Free ARexx message
  353.  
  354. */
  355.  
  356. void
  357. FreeRexxCommand(rexxmessage)
  358.  
  359. struct RexxMsg *rexxmessage;
  360. {
  361.     if (rexxmessage->rm_Result1 == RC_OK) 
  362.         if (rexxmessage->rm_Result2)
  363.             DeleteArgstring((char *)rexxmessage->rm_Result2);
  364.  
  365.     DeleteArgstring((char *)ARG0(rexxmessage));
  366.  
  367.     DeleteRexxMsg(rexxmessage);
  368. }
  369.  
  370. /* ---------------------------------- SendRexxCommand -------------------------
  371.  
  372.  Send ARexx message
  373.  
  374. */
  375.  
  376. struct RexxMsg *
  377. SendRexxCommand(port, cmd, replyPort)
  378.  
  379. char   *cmd,   *port;
  380. struct MsgPort *replyPort;
  381. {
  382.     struct MsgPort *rexxport;
  383.     struct RexxMsg *rexx_command_message = NULL;
  384.  
  385.     Forbid();
  386.  
  387.     if (rexxport = FindPort(port)) {
  388.  
  389.         if (rexx_command_message = CreateRexxMsg(replyPort, NULL, NULL)) {
  390.  
  391.             if (rexx_command_message->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  392.  
  393.                 rexx_command_message->rm_Action = RXCOMM | RXFF_RESULT;
  394.  
  395.                 PutMsg(rexxport, &rexx_command_message->rm_Node);
  396.             }
  397.         }
  398.     }
  399.  
  400.     Permit();
  401.  
  402.     return(rexx_command_message);
  403. }
  404.  
  405. ///
  406.