home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / wp_dtp / xdme1821.lha / XDME / drexx.c < prev    next >
C/C++ Source or Header  |  1993-03-30  |  12KB  |  561 lines

  1. /******************************************************************************
  2.  
  3.     MODUL
  4.     drexx.c
  5.  
  6.     DESCRIPTION
  7.     Another AREXX interface for the XDME. Uses HRexx.c, some tools, I
  8.     wrote for a easier AREXX-usage.
  9.  
  10.     This module implements the same commands as "rexx.c", so it works
  11.     with a minimum of changes in the real XDME sources.
  12.  
  13.     Because I want to implement a, which is ALWAYS calleble from the
  14.     outer world (other tasks), I was forced to made some minor changes
  15.     in main.c (marked with "/ * HD " as comment ). Further I had to add
  16.     an Activate Window to cmd1.c's QUIT command , because it dosen't
  17.     work proper if it was called with a deactivated window.
  18.  
  19.     In addition I added my new commands in the commandtable in
  20.     "command.c"
  21.  
  22.     NOTES
  23.  
  24.     BUGS
  25.  
  26.     TODO
  27.  
  28.     EXAMPLES
  29.  
  30.     SEE ALSO
  31.  
  32.     INDEX
  33.  
  34.     HISTORY
  35.     dirk    26. Dec 1991    created
  36.  
  37. ******************************************************************************/
  38.  
  39. /**************************************
  40.         Includes
  41. **************************************/
  42. #include "defs.h"
  43. #include "hrexx.h"
  44. #include "rexx/storage.h"
  45. #include "rexx/rxslib.h"
  46. #include "rexx/rexxio.h"
  47. #include "rexx/errors.h"
  48. #define  MYDEBUG    0
  49. #include "debug.h"
  50.  
  51.  
  52. /**************************************
  53.         Globale Variable
  54. **************************************/
  55. Prototype ULONG RexxMask;
  56. Prototype int foundcmd; /* control for implicit ARexx macro invocation     */
  57. Prototype int cmderr;    /* global command error flag for do_rexx()'s use */
  58.  
  59. Prototype void     openrexx         (void);
  60. Prototype void     closerexx         (void);
  61. Prototype void     do_rx             (void);
  62. Prototype void     do_rx1          (void);
  63. Prototype void     do_rx2          (void);
  64. Prototype long     do_rxImplied         (char *, char *);
  65. Prototype void     extern_rexx_command (void);
  66. Prototype void     to_port         (void);
  67. Prototype void     select_window         (void);
  68. Prototype void     project_info         (void);
  69. Prototype int     load_project         (void);
  70. Prototype void     save_project         (void);
  71. Prototype void     put_rexx_result     (void);
  72. Prototype char * get_rexx_result     (void);
  73. Prototype long     do_rexx         (const char * port, const char *fmt, ...);
  74.  
  75.  
  76. ULONG RexxMask;     /* ArexxPort signalmask for Wait        */
  77. int   foundcmd;     /* control for implicit ARexx macro invocation    */
  78. int   cmderr;        /* global command error flag for do_rexx's use  */
  79.  
  80. static char rexx_result[256];    /* hier wird das Rexxresult untergerbracht */
  81.  
  82.  
  83. /**************************************
  84.       Interne Defines & Strukturen
  85. **************************************/
  86.  
  87.  
  88. /**************************************
  89.         Interne Variable
  90. **************************************/
  91.  
  92.  
  93. /**************************************
  94.        Interne Prototypes
  95. **************************************/
  96. __stkargs static void in_rexx (struct RexxMsg *);
  97.  
  98.  
  99. /************************************************************************/
  100. /*               lokale  Funktionen                */
  101. /************************************************************************/
  102.  
  103. __stkargs static void in_rexx (struct RexxMsg *rmsg)
  104. /*
  105.  * this function is called with incomming Rexxmessages, while
  106.  * processing SyncRexxCommand
  107.  *
  108.  * --> rmsg    pointer to the RexxMessage
  109.  */
  110. {
  111.    long ret;
  112.    foundcmd = 0;
  113.  
  114.    *rexx_result = 0;
  115.  
  116. //   currentmsg = (MSG*)rmsg;           /* PATCH_RXADD */
  117.  
  118.    ret = !do_command ((char *)ARG0(rmsg));
  119.  
  120. //   currentmsg = NULL;         /* PATCH_RXADD */
  121.  
  122.    if (rexx_result[0])
  123.       ReplyRexxMsg (rmsg, ret, rexx_result);
  124.    else
  125.       ReplyRexxMsg (rmsg, ret, 0);
  126.  
  127.    *rexx_result= 0;
  128.  
  129.    do_command ("null");     /* reset foundcommand */
  130. }
  131.  
  132.  
  133. /************************************************************************/
  134. /*               globale Funktionen                */
  135. /************************************************************************/
  136.  
  137. long do_rexx (const char * port, const char *fmt,...)
  138. /*
  139.  * This command transmittes the command, printf-style, to a named
  140.  * port.
  141.  *
  142.  * --> port    name of port
  143.  * --> fmt    printf-like format string for the command
  144.  * --> ...    the args for the command
  145.  */
  146. {
  147.     va_list va;
  148.     char macro[256];
  149.  
  150.     va_start (va, fmt);
  151.     vsprintf (macro, fmt, va);
  152.     va_end (va);
  153.  
  154.     return (SyncRexxCommand (port, macro, rexx_result, sizeof(rexx_result), 0));
  155. }
  156.  
  157.  
  158. void put_rexx_result (void)
  159. /*
  160.     Copy a string to rexx_result
  161. */
  162. {
  163.     strcpy (rexx_result, av[1]);
  164. }
  165.  
  166.  
  167. char * get_rexx_result (void)
  168. /*
  169.     Get Contents of rexx_result
  170. */
  171. {
  172.     puts (rexx_result);
  173.  
  174.     return (rexx_result);
  175. }
  176.  
  177.  
  178. void openrexx (void)
  179. /* initalizes the Arexx-stuff */
  180. {
  181.    int number;
  182.  
  183.    number = 1;
  184.  
  185.    Forbid ();
  186.  
  187.    do
  188.    {
  189.     sprintf (RexxPortName, "XDME.%d", number ++);
  190.    } while (FindPort (RexxPortName) && number < 1000);
  191.  
  192.    Permit ();
  193.  
  194.    RexxMask = OpenRexx (RexxPortName, "XDME", in_rexx, 0, 0);
  195.  
  196.    if (!RexxMask)
  197.     exiterr ("Cannot open AREXX-Port");
  198. }
  199.  
  200.  
  201. void closerexx (void)
  202. /* shut down the Arexx-stuff */
  203. {
  204.    CloseRexx ();
  205. }
  206.  
  207.  
  208. void  do_rx (void)
  209. /*
  210.  *  explicit invocation interface between do_command() and do_rexx
  211.  *  for ARexx macros having NO arguments (i.e., for the "rx" command)
  212.  */
  213. {
  214.     do_rexx (0,"%s",av[1]);
  215. }
  216.  
  217.  
  218. void do_rx1 (void)
  219. /*
  220.  *  explicit invocation interface between do_command() and do_rexx
  221.  *  for ARexx macros having ONE argument (i.e., for the "rx1" command)
  222.  */
  223. {
  224.     do_rexx (0,"%s %s",av[1],av[2]);
  225. }
  226.  
  227.  
  228. void do_rx2 (void)
  229. /*
  230.  *  explicit invocation interface between do_command() and do_rexx
  231.  *  for ARexx macros having TWO arguments (i.e., for the "rx2" command)
  232.  */
  233. {
  234.     do_rexx (0,"%s %s %s",av[1],av[2],av[3]);
  235. }
  236.  
  237.  
  238. long do_rxImplied (char * cmd,char * args)
  239. /*
  240.  *  implicit invocation interface between do_command() and do_rexx
  241.  *  for ARexx macros implicitly called; arbitrary number of arguments
  242.  */
  243. {
  244.     return (do_rexx (0,"%s %s",cmd,args));
  245. }
  246.  
  247. /***********************************************************************/
  248. /***********           New Commands            *************/
  249. /***********************************************************************/
  250.  
  251. void extern_rexx_command (void)
  252. /*
  253.  * executes a command, comming in from an external port
  254.  */
  255. {
  256.     long         ret;
  257.     struct RexxMsg * rmsg;
  258.  
  259.     *rexx_result = 0;
  260.  
  261. //    if (Debug) printf("extern_rexx_command\n"); /* */
  262.  
  263.     rmsg = GetRexxMsg ();
  264.  
  265. //    currentmsg = (MSG*)rmsg;        /* PATCH_RXADD */
  266.  
  267.     ret = !do_command ((char *)ARG0(rmsg));
  268.  
  269. //    currentmsg = NULL;            /* PATCH_RXADD */
  270.  
  271.     if (rexx_result[0])
  272.     ReplyRexxMsg (rmsg, ret, rexx_result);
  273.     else
  274.     ReplyRexxMsg (rmsg, ret, 0);
  275.  
  276.     *rexx_result = 0;
  277. } /* extern_rexx_command */
  278.  
  279.  
  280. void to_port (void)
  281. /*
  282.  * simply passes a command to another named port
  283.  */
  284. {
  285.     do_rexx (av[1], "%s", av[2]);
  286. } /* to_port */
  287.  
  288.  
  289. void select_window (void)
  290. /*
  291.  * selects another window FIRST LAST NEXT PREVIOUS WINDOW=<NAME> SAVE LOAD
  292.  * every command can be abreviated by its first character:
  293.  *  "select window=cmd1.c" = "select w=cmd1.c"
  294.  */
  295. {
  296.     ED          * ed     = NULL;
  297.     static ED * sto_ed = NULL;
  298.  
  299.     switch (av[1][0])
  300.     {
  301.     case ('l') :
  302.     {
  303.         if (av[1][1] == 'a')
  304.         ed = (ED *)GetTail (&DBase);
  305.         else
  306.         ed = sto_ed;
  307.     break; } /* case ('l') */
  308.  
  309.     case ('f') :
  310.     {
  311.         ed = (ED *)GetHead (&DBase);
  312.     break; } /* case ('f') */
  313.  
  314.     case ('n') :
  315.     {
  316.         ed = GetPred (Ep);
  317.  
  318.         if (ed == NULL && globalflags.Windowcycling)
  319.         {  /* PATCH_NULL */
  320.         ed = (ED *)GetHead (&DBase);    /* PATCH_NULL */
  321.         } /* if */                /* PATCH_NULL */
  322.     break; } /* case ('n') */
  323.  
  324.     case ('p') :
  325.     {
  326.         ed = GetSucc (Ep);
  327.  
  328.         if (ed == NULL && globalflags.Windowcycling)
  329.         {  /* PATCH_NULL */
  330.         ed = (ED *)GetTail (&DBase);    /* PATCH_NULL */
  331.         } /* if */                /* PATCH_NULL */
  332.     break; } /* case ('p') */
  333.  
  334.     case ('a') :
  335.     {
  336.         ActivateWindow (Ep->win);
  337.  
  338.         if (globalflags.ActivateToFront)
  339.         {           /* PATCH_NULL */
  340.         WindowToFront (Ep->win);        /* PATCH_NULL */
  341.         } /* if */                /* PATCH_NULL */
  342.     break; } /* case ('a') */
  343.  
  344.     case ('s') :
  345.     {
  346.         sto_ed = Ep;
  347.         return;
  348.     break; } /* case ('s') */
  349.  
  350.     case ('w') :
  351.     {
  352.         char * ptr;
  353.  
  354.         ptr = av[1];
  355.  
  356.         while (*ptr && *ptr != '=')
  357.         ptr ++;
  358.  
  359.         if (*ptr)
  360.         ptr ++;
  361.         else
  362.         break;
  363.  
  364.         D(bug("looking for `%s'\n", ptr));
  365.  
  366.         for (ed=(ED *)GetHead(&DBase); ed; ed=GetSucc(ed))
  367.         if (!stricmp (ed->name, ptr))   /* found it */
  368.             break;
  369.  
  370.         D(bug("Found %08x\n", ed));
  371.  
  372.     break; } /* case ('w') */
  373.  
  374.     } /* switch (av[1][0]) */
  375.  
  376.     if (ed)
  377.     {
  378.     switch_ed (ed);
  379.  
  380.     ActivateWindow (ed->win);
  381.  
  382.     if (globalflags.ActivateToFront)
  383.     {           /* PATCH_NULL */
  384.         WindowToFront (ed->win);            /* PATCH_NULL */
  385.     } /* if */                /* PATCH_NULL */
  386.     } else
  387.     {
  388.     globalflags.Abortcommand = 1;
  389.     }
  390. } /* select_window */
  391.  
  392.  
  393. void project_info (void)
  394. /*
  395.  * builds the following string:
  396.  * path <name> <left> <top> <width> <height> <iconleft> <iconright>
  397.  * if Rexx called this string is return as resultstring
  398.  */
  399. {
  400.     char path[PATHSIZE];        /* Puffer für Pfad */
  401.  
  402.     getpathto (Ep->dirlock, NULL, path);
  403.  
  404.     /* Read values from Window-Struct */
  405.     if (Ep->iconmode)
  406.     {
  407.     Ep->config.iwinx     = Ep->win->LeftEdge;
  408.     Ep->config.iwiny     = Ep->win->TopEdge;
  409.     } else
  410.     {
  411.     Ep->config.winx      = Ep->win->LeftEdge;
  412.     Ep->config.winy      = Ep->win->TopEdge;
  413.     Ep->config.winwidth  = Ep->win->Width;
  414.     Ep->config.winheight = Ep->win->Height;
  415.     }
  416.  
  417.     sprintf (rexx_result, "\"%s\" \"%s\" %d %d %d %d %d %d %d",
  418.                path,    Ep->name,
  419.                      Ep->config.winx,
  420.                         Ep->config.winy,
  421.                            Ep->config.winwidth,
  422.                           Ep->config.winheight,
  423.                              Ep->config.iwinx,
  424.                             Ep->config.iwiny,
  425.                                Ep->line
  426.     );
  427. } /* project_info */
  428.  
  429.  
  430. void save_project (void)
  431. /*
  432.  * saves all opened files in the directory of the currently active
  433.  * editor to a file name XDMEArgs.projectfilename.
  434.  * The lines are in the same format as returned from project_info
  435.  */
  436. {
  437.     ED * ed;                /* Currenteditor */
  438.     ED * act_ed;            /* active Editor */
  439.     BPTR old_lock;            /* Active dirlock */
  440.     BPTR file;                /* our PRJ file */
  441.  
  442.     old_lock = CurrentDir (Ep->dirlock); /* change dir */
  443.     act_ed   = Ep;             /* save active Editor */
  444.  
  445.     file = Open (XDMEArgs.projectfilename, MODE_NEWFILE);
  446.  
  447.     if (file)
  448.     {
  449.     Write (file, "# path name x y w h ix iy line\n", 31L);
  450.  
  451.     for (ed=(ED *)GetHead(&DBase); ed; ed=(ED *)GetSucc((struct Node *)ed))
  452.     {
  453.         switch_ed (ed);              /* change current ed */
  454.  
  455.         project_info ();             /* build info text */
  456.  
  457.         Write (file, rexx_result, strlen (rexx_result));
  458.         Write (file, "\n", 1L);
  459.     }
  460.  
  461.     Close (file);
  462.     } /* if (file) */
  463.  
  464.     switch_ed (act_ed);                  /* restore actuall editor */
  465.  
  466.     CurrentDir (old_lock);               /* go back to old dir */
  467. } /* project_save */
  468.  
  469.  
  470. int load_project (void)
  471. {
  472.     FILE * file;
  473.     char   path[PATHSIZE];        /* Puffer für Pfad */
  474.     char   name[60];            /* file name */
  475.     char * ptr;
  476.     long   t, il, it, line;        /* window & icon & line */
  477.  
  478.     file = fopen (XDMEArgs.projectfilename, "r");  /* Open ProjectFile */
  479.  
  480.     if (file)
  481.     {
  482.     while (fgets (tmp_buffer, 256, file))
  483.     {
  484.         if (*tmp_buffer == '#')
  485.         continue;
  486.  
  487.         ptr = skip_whitespace (tmp_buffer);
  488.  
  489.         if (*ptr == '"')
  490.         {
  491.         ptr ++;
  492.  
  493.         for (t=0; *ptr != '"'; )
  494.             path[t ++] = *ptr ++;
  495.  
  496.         ptr ++;
  497.         } else
  498.         for (t=0; !isspace(*ptr); )
  499.             path[t ++] = *ptr ++;
  500.  
  501.         path[t] = 0;
  502.         ptr = skip_whitespace (ptr);
  503.  
  504.         if (*ptr == '"')
  505.         {
  506.         ptr ++;
  507.  
  508.         for (t=0; *ptr != '"'; )
  509.             name[t ++] = *ptr ++;
  510.  
  511.         ptr ++;
  512.         } else
  513.         for (t=0; !isspace(*ptr); )
  514.             name[t ++] = *ptr ++;
  515.  
  516.         name[t] = 0;
  517.  
  518.         av[1] = ptr;
  519.         do_openwindow ();
  520.  
  521.         av[1] = path;
  522.         do_cd ();
  523.  
  524.         av[0] = "newfile";
  525.         av[1] = name;
  526.         do_edit ();
  527.  
  528.         sscanf (ptr, " %d  %d  %d  %d  %d   %d   %d\n",
  529.                &t, &t, &t, &t, &il, &it, &line);
  530.  
  531.         if (line >= Ep->lines)
  532.         line = Ep->lines - 1;
  533.  
  534.         Ep->line = line;
  535.  
  536.         text_load ();
  537.         text_adjust (FALSE);
  538.  
  539.         Ep->config.iwinx = il;
  540.         Ep->config.iwiny = it;
  541.  
  542.         do_iconify ();
  543.     }
  544.  
  545.     fclose (file);
  546.  
  547.     t = TRUE;
  548.     } else
  549.     {
  550.     warn ("projectload: Cannot find file `%s'", XDMEArgs.projectfilename);
  551.     t = FALSE;
  552.     }
  553.  
  554.     return (t);
  555. } /* load_project */
  556.  
  557.  
  558. /******************************************************************************
  559. *****  ENDE drexx.c
  560. ******************************************************************************/
  561.