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