home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Util / Starter / XDME_Starter.c < prev   
Encoding:
C/C++ Source or Header  |  1994-12-22  |  7.5 KB  |  369 lines

  1. /******************************************************************************
  2.  
  3.     MODUL
  4.  
  5.     HISTORY
  6.  
  7. ******************************************************************************/
  8.  
  9. /**************************************
  10.         Includes
  11. **************************************/
  12.  
  13. #include "all.h"
  14. #include <rexx/errors.h>
  15. #include <clib/rexxsyslib_protos.h>
  16. #include <pragmas/rexxsyslib_pragmas.h>
  17.  
  18. /**************************************
  19.         Globale Variable
  20. **************************************/
  21.  
  22. extern struct Library *IconBase,
  23.               *RexxSysBase;
  24.  
  25. /**************************************
  26.            Typedefs
  27. **************************************/
  28. typedef struct WBStartup    WBS;
  29. typedef struct DiskObject   DISKOBJ;
  30.  
  31. /**************************************
  32.         Interne Variable
  33. **************************************/
  34. static ULONG     newxdme;        /* force new xdme */
  35.  
  36. static const char default_startupfile[] = ".edrc";
  37. static const char default_projectfile[] = "XDME_Project_File";
  38.  
  39. static const char tmplate[] = "Files/M,"
  40.                   "Startup=-S/K,"
  41.                   "New=-N/S,"
  42.                   "Iconify=-I/S,"
  43.                   "Autoload=-A/S,"
  44.                   "PubScreen=-PS/K,"
  45.                   "ProjectFile=-PF/K";
  46.  
  47. struct __XDMEArgs XDMEArgs =
  48. {
  49.     NULL,        /* Files */
  50.     NULL,        /* Startup-Filename */
  51.     FALSE,        /* NEW */
  52.     FALSE,        /* Iconify */
  53.     FALSE,        /* Autoload */
  54.     NULL,        /* PublicScreen */
  55.     NULL,        /* ProjectFile */
  56.     /* ... */
  57. };
  58.  
  59. /**************************************
  60.        Interne Prototypes
  61. **************************************/
  62.  
  63.  
  64.  
  65. #ifdef _DCC
  66. extern int wbmain (WBS * wbs); /* !!! No Prototype !!! */
  67. int wbmain (WBS * wbs)
  68. {
  69.     return (main (0, (char **)wbs));
  70. } /* wbmain */
  71. #endif
  72.  
  73. #ifdef __SASC
  74. /* disable ^C checking */
  75. extern void __regargs __chkabort (void);
  76.        void __regargs __chkabort (void) { /* empty */ }
  77. #endif
  78.  
  79.  
  80. int do_rexx (char *port_name, char *command)
  81. {
  82.     struct MsgPort *rp, *port;
  83.     struct RexxMsg *rm, *msg;
  84.     int replied = 0;
  85.  
  86.     if ((rp = CreateMsgPort())) {
  87.  
  88.     if ((rm = CreateRexxMsg(rp, NULL, NULL))) {
  89.         rm->rm_Args[0] = command;
  90.         if (FillRexxMsg(rm, 1, 0)) {
  91.  
  92.         Forbid();
  93.  
  94.         port = FindPort( (UBYTE *) port_name );
  95.         if( port )
  96.             PutMsg( port, (struct Message *) rm );
  97.  
  98.         Permit();
  99.  
  100.         if (port) {
  101.             while (!replied) {
  102.             WaitPort (rp);
  103.             while (msg = (struct RexxMsg *)GetMsg (rp)) {
  104.                 if (msg != rm) {
  105.                 msg->rm_Result1 = RC_ERROR;
  106.                 msg->rm_Result2 = ERR10_008; /* ne bessere Idee ? */
  107.                 ReplyMsg ((struct Message *)msg);
  108.                 } else
  109.                 replied = 1;
  110.  
  111.             } /* while */
  112.             } /* while */
  113.         } /* if */
  114.  
  115.         ClearRexxMsg(rm, 1);
  116.         } /* if */
  117.         DeleteRexxMsg(rm);
  118.     } /* if */
  119.  
  120.     DeleteMsgPort(rp);
  121.     } /* if */
  122.  
  123.     return replied;
  124. } /* do_rexx */
  125.  
  126.  
  127.  
  128.  
  129. /* Parse WB-ToolTypes */
  130.  
  131. void ops (char ** toolarray)
  132. {
  133.     char * tooltype;
  134.  
  135.     if (tooltype = FindToolType (toolarray, "ICONIFY"))
  136.     XDMEArgs.iconify = MatchToolValue (tooltype, "YES");
  137.     else
  138.     XDMEArgs.iconify = FALSE;
  139.  
  140.     if (tooltype = FindToolType (toolarray, "AUTOLOAD"))
  141.     XDMEArgs.autoload = MatchToolValue (tooltype, "YES");
  142.     else
  143.     XDMEArgs.autoload = FALSE;
  144.  
  145.     if (tooltype = FindToolType (toolarray, "PUBSCREEN"))
  146.     XDMEArgs.publicscreenname = tooltype;    /* no strdup() here ! */
  147.  
  148.     if (tooltype = FindToolType (toolarray, "PROJECTFILE"))
  149.     XDMEArgs.projectfilename = tooltype;   /* no strdup() here ! */
  150.  
  151. #ifdef DEBUG
  152.     D(bug("sfile: %s\nnew %ld\niconify %ld\nauto %ld\n"
  153.         "PSName: `%s'\npf: `%s'\n", XDMEArgs.startupfile,
  154.         XDMEArgs.newxdme, XDMEArgs.iconify, XDMEArgs.autoload,
  155.         XDMEArgs.publicscreenname ? XDMEArgs.publicscreenname : "(nul)",
  156.         XDMEArgs.projectfilename ? XDMEArgs.projectfilename : "(nul)"));
  157. #endif
  158. } /* ops */
  159.  
  160.  
  161. BOOL loadfile (BPTR lock, char * name)
  162. {
  163.     char buf[512];
  164.  
  165.     /* Load project ? */
  166.     if (!stricmp (name, XDMEArgs.projectfilename))
  167.     {
  168.         strcpy (buf, "cd `");
  169.  
  170.         if (NameFromLock (lock, buf + 4, sizeof (buf) - 4))
  171.         {
  172.         strcat (buf, "' projectload");
  173.  
  174.         do_rexx ("XDME.1", buf);
  175.         }
  176.     } else
  177.     {
  178.         strcpy (buf, !newxdme ? "newwindow cd `" : "          cd `");
  179.         newxdme = 0;
  180.  
  181.         if (NameFromLock (lock, buf + 14, sizeof (buf) - 14))
  182.         {
  183.         sprintf (buf + strlen (buf), "' newfile `%s'%s",
  184.             name, (XDMEArgs.iconify ? " iconify" : ""));
  185.         do_rexx ("XDME.1", buf);
  186.         }
  187.     } /* No PROJECTFILE */
  188.  
  189.     return (TRUE);
  190. } /* loadfile */
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. int main (int mac, char ** mav)
  199. {
  200.     WBS *Wbs = NULL;
  201.     WORD   i;            /* temp. counter           */
  202.     ULONG  nf;            /* # files on command line       */
  203.     BPTR   origlock;        /* Start-Lock           */
  204.     PROC * proc        = (PROC *)FindTask(NULL);   /* our Task    */
  205.  
  206.     /* cd to dir of shell */
  207.     origlock = CurrentDir (DupLock (proc->pr_CurrentDir));
  208.  
  209.  
  210.     if (mac == 0)
  211.     {          /*  WORKBENCH STARTUP   */
  212.     Wbs = (WBS *)mav;
  213.     }
  214.  
  215. /* EYYYYY - how about that:
  216.     if there is not yet a XDME Process or if a "New"
  217.     Process is repested, we just call
  218.  
  219.     if (new_xdme_needed) {
  220.     if (!Wbs) {
  221.         sprintf (buffer "run XDME %s", args);
  222.         system  (buffer);
  223.     } else {
  224.         // aehm ... we would probably need something like this ...
  225.         WBStart ("XDME", Wbs);
  226.     }
  227.     return 0;
  228.     }
  229. */
  230.  
  231.     {
  232.     struct MsgPort * mp;
  233.  
  234.     Forbid ();
  235.     mp = FindPort ("XDME.1");
  236.     Permit ();
  237.  
  238.     if (!mp) {
  239.         system ("run XDME t:");
  240.  
  241.         Delay (20);
  242.  
  243.         Forbid ();
  244.         mp = FindPort ("XDME.1");
  245.         Permit ();
  246.  
  247.         if (!mp) {
  248.         puts("Cannot open XDME main process - exiting\n");
  249.         return 10;
  250.         } /* if */
  251.  
  252.         newxdme = 1;
  253.     } /* if */
  254.     }
  255.  
  256.  
  257.     nf = 0;
  258.     XDMEArgs.projectfilename = default_projectfile;
  259.     XDMEArgs.startupfile     = default_startupfile;
  260.  
  261.     /* WB-Startup or CLI ? */
  262.     if (Wbs)
  263.     {
  264.     /* Work on TOOLTypes */
  265.     if (Wbs->sm_ArgList[0].wa_Lock)
  266.     {
  267.         DISKOBJ     * dobj;    /* For WB-Startup */
  268.         struct WBArg * wbarg;
  269.         BPTR       current;
  270.  
  271.         for (i=0; i<Wbs->sm_NumArgs; i++)
  272.         {
  273.         wbarg = &Wbs->sm_ArgList[i];
  274.  
  275.         UnLock (CurrentDir (current = DupLock (wbarg->wa_Lock)));
  276.  
  277.         if (dobj = GetDiskObjectNew (wbarg->wa_Name))
  278.         {
  279.             /* Only process names for ToolTypes, not for the the
  280.                program itself ! */
  281.  
  282.             ops (dobj->do_ToolTypes);
  283.  
  284.             if (i)
  285.             {
  286.             if (loadfile (wbarg->wa_Lock, wbarg->wa_Name))
  287.                 nf ++;
  288.             else
  289.             {
  290.                 FreeDiskObject (dobj);
  291.                 break;
  292.             }
  293.             }
  294.  
  295.             FreeDiskObject (dobj);
  296.         }
  297.         }
  298.  
  299.         /* CD to new base-dir */
  300.         UnLock (CurrentDir (DupLock (Wbs->sm_ArgList[0].wa_Lock)));
  301.     }
  302.     } else
  303.     {
  304.     XDMEArgs.newxdme = newxdme;
  305.  
  306.     /* Parse CLI-args */
  307.     if ((XDMEArgs.ra = ReadArgs (tmplate, (LONG *)&XDMEArgs, NULL))
  308.         && XDMEArgs.files)
  309.     {
  310. #ifdef DEBUG
  311.         UWORD count = 0;
  312. #endif
  313.         char ** nameptr;
  314.  
  315.         for (nameptr=XDMEArgs.files; *nameptr; nameptr ++)
  316.         {
  317. #ifdef DEBUG
  318.         D(bug("Loading %ld %s ...\n", count ++, *nameptr));
  319. #endif
  320.  
  321.         if (loadfile (origlock, *nameptr))
  322.             nf ++;
  323.         else
  324.             break;
  325.         }
  326.     }
  327.     }
  328.  
  329. #ifdef DEBUG
  330.     D(bug("Parsed ARGS\n"));
  331. #endif
  332.  
  333.     /* Free args */
  334.     if (XDMEArgs.ra)
  335.     FreeArgs (XDMEArgs.ra);
  336.  
  337.     if (!nf)
  338.     {  /* no files to edit: Open simple empty window */
  339.     if (newxdme)
  340.     {
  341.         do_rexx ("XDME.1", "chfilename unnamed arpload");
  342.     } else
  343.     if (XDMEArgs.iconify)
  344.     {
  345.         do_rexx ("XDME.1", "newwindow iconify");
  346.     } else
  347.     {
  348.         do_rexx ("XDME.1", "newwindow");
  349.     }
  350.     } /* No files */
  351.  
  352.  
  353.     /* Free Lock */
  354.     UnLock (CurrentDir (origlock));
  355.  
  356. #ifdef DEBUG_MEM_H
  357.     _debug_FreeAllMem ();
  358. #endif /* DEBUG_MEM_H */
  359.  
  360.     /* All OK. */
  361.     return 0;
  362. } /* main */
  363.  
  364.  
  365. /******************************************************************************
  366. *****  ENDE main.c
  367. ******************************************************************************/
  368.  
  369.