From 6f3c27a1b151ff9cb72fb1e4154169cab783305f Mon Sep 17 00:00:00 2001
From: Fons Rademakers <Fons.Rademakers@cern.ch>
Date: Wed, 12 Dec 2007 10:15:51 +0000
Subject: [PATCH] Add extensive help to the .R command. Type .R -? or -h or
 --help to get a detailed description of the options. Also support for sourced
 scripts is added, as well as the old behaviour to execute a custom roots
 script, to for example start in a special mode or special binary. To source a
 script before starting roots do:

.R lxplus <go

to execute your own roots like script do:

.R lxplus myroots


git-svn-id: http://root.cern.ch/svn/root/trunk@21330 27541ba8-7e3a-0410-8455-c3a389f83636
---
 base/src/TApplication.cxx      | 41 +++++++++++++++++++++-------------
 net/src/TApplicationRemote.cxx | 20 ++++++++++++-----
 2 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/base/src/TApplication.cxx b/base/src/TApplication.cxx
index 6ded7c8b485..f34a2688756 100644
--- a/base/src/TApplication.cxx
+++ b/base/src/TApplication.cxx
@@ -555,13 +555,6 @@ Int_t TApplication::ParseRemoteLine(const char *ln,
    // The last argument 'script' allows to specify an alternative script to
    // be executed remotely to startup the session.
 
-   // Parse the content of a line starting with ".R" (already stripped-off)
-   // The format of teh remaining part is
-   //      hostdir [-l user] [-d dbg] [script]
-   // The variable 'hostdir' contains the host to connect to and the remote
-   // directory to be used as working dir.
-   // A username can also be included in hostdir in the usual form user@host.
-
    if (!ln || strlen(ln) <= 0)
       return 0;
 
@@ -598,10 +591,7 @@ Int_t TApplication::ParseRemoteLine(const char *ln,
             isScript = kTRUE;
          } else if (isScript) {
             // Add everything left
-            script = line;
-            Int_t itkn = script.Index(tkn);
-            if (itkn != kNPOS)
-               script.Remove(0, itkn);
+            script = tkn;
             script.Insert(0, "\"");
             script += "\"";
             isScript = kFALSE;
@@ -622,7 +612,7 @@ Long_t TApplication::ProcessRemote(const char *line, Int_t *)
 {
    // Process the content of a line starting with ".R" (already stripped-off)
    // The format is
-   //      [user@]host[:dir] [-l user] [-d dbg] [script]
+   //      [user@]host[:dir] [-l user] [-d dbg] [script] | [host] -close
    // The variable 'dir' is the remote directory to be used as working dir.
    // The username can be specified in two ways, "-l" having the priority
    // (as in ssh).
@@ -632,6 +622,27 @@ Long_t TApplication::ProcessRemote(const char *line, Int_t *)
 
    if (!line) return 0;
 
+   if (!strncmp(line, "-?", 2) || !strncmp(line, "-h", 2) ||
+       !strncmp(line, "--help", 6)) {
+       Info("ProcessRemote", "remote session help:");
+       Printf(".R [user@]host[:dir] [-l user] [-d dbg] [[<]script] | [host] -close");
+       Printf("Create a ROOT session on the specified remote host.");
+       Printf("The variable \"dir\" is the remote directory to be used as working dir.");
+       Printf("The username can be specified in two ways, \"-l\" having the priority");
+       Printf("(as in ssh). A \"dbg\" value > 0 gives increasing verbosity.");
+       Printf("The last argument \"script\" allows to specify an alternative script to");
+       Printf("be executed remotely to startup the session, \"roots\" being");
+       Printf("the default. If the script is preceeded by a \"<\" the script will be");
+       Printf("sourced, after which \"roots\" is executed. The sourced script can be ");
+       Printf("used to change the PATH and other variables, allowing an alternative");
+       Printf("\"roots\" script to be found.");
+       Printf("To close down a session do \".R host -close\".");
+       Printf("To switch between sessions do \".R host\", to switch to the local");
+       Printf("session do \".R\".");
+       Printf("To list all open sessions do \"gApplication->GetApplications()->Print()\".");
+       return 0;
+   }
+
    TString hostdir, user, script;
    Int_t dbg = 0;
    Int_t rc = ParseRemoteLine(line, hostdir, user, dbg, script);
@@ -647,7 +658,7 @@ Long_t TApplication::ProcessRemote(const char *line, Int_t *)
       return 1;
    } else if (rc == 1) {
       // close an existing remote application
-      TApplication *ap = Open(hostdir, 0, 0);
+      TApplication *ap = TApplication::Open(hostdir, 0, 0);
       if (ap) {
          TApplication::Close(ap);
          delete ap;
@@ -657,7 +668,7 @@ Long_t TApplication::ProcessRemote(const char *line, Int_t *)
    if (user.Length() > 0)
       hostdir.Insert(0,Form("%s@", user.Data()));
    const char *sc = (script.Length() > 0) ? script.Data() : 0;
-   TApplication *ap = Open(hostdir, dbg, sc);
+   TApplication *ap = TApplication::Open(hostdir, dbg, sc);
    if (ap) {
       fAppRemote = ap;
    }
@@ -1038,7 +1049,7 @@ TApplication *TApplication::Open(const char *url,
                                   Int_t debug, const char *script)
 {
    // Static function used to attach to an existing remote application
-   // or to start one
+   // or to start one.
 
    TApplication *ap = 0;
    TUrl nu(url);
diff --git a/net/src/TApplicationRemote.cxx b/net/src/TApplicationRemote.cxx
index c6f63507bdb..24b0bae8703 100644
--- a/net/src/TApplicationRemote.cxx
+++ b/net/src/TApplicationRemote.cxx
@@ -62,9 +62,9 @@ static const char *gScriptCmd = "\\\"%s %d localhost:%d/%s -d=%d\\\"";
 #ifndef WIN32
 static const char *gSshCmd = "ssh %s -f4 %s -R %d:localhost:%d sh -c \
    \"'(sh=\\`basename \'\\\\\\$SHELL\'\\`; \
-   if test xbash = x\'\\\\\\$sh\' -o xsh = x\'\\\\\\$sh\' -o xksh = x\'\\\\\\$sh\' -o xzsh = x\'\\\\\\$sh\' -o xdash = x\'\\\\\\$sh\'; then \
+   if test xbash = x\'\\\\\\$sh\' -o xsh = x\'\\\\\\$sh\' -o xzsh = x\'\\\\\\$sh\' -o xdash = x\'\\\\\\$sh\'; then \
       \'\\\\\\$SHELL\' -l -c %s; \
-   elif test xcsh = x\'\\\\\\$sh\' -o xtcsh = x\'\\\\\\$sh\'; then \
+   elif test xcsh = x\'\\\\\\$sh\' -o xtcsh = x\'\\\\\\$sh\' -o xksh = x\'\\\\\\$sh\'; then \
       \'\\\\\\$SHELL\' -c %s; \
    else \
       echo \\\"Unknown shell \'\\\\\\$SHELL\'\\\"; \
@@ -72,9 +72,9 @@ static const char *gSshCmd = "ssh %s -f4 %s -R %d:localhost:%d sh -c \
 #else
 static const char *gSshCmd = "ssh %s -f4 %s -R %d:localhost:%d sh -c \
    \"'(sh=`basename $SHELL`; \
-   if test xbash = x$sh -o xsh = x$sh -o xksh = x$sh -o xzsh = x$sh -o xdash = x$sh; then \
+   if test xbash = x$sh -o xsh = x$sh -o xzsh = x$sh -o xdash = x$sh; then \
       $SHELL -l -c %s; \
-   elif test xcsh = x$sh -o xtcsh = x$sh; then \
+   elif test xcsh = x$sh -o xtcsh = x$sh -o xksh = x$sh; then \
       $SHELL -c %s; \
    else \
       echo \"Unknown shell $SHELL\"; \
@@ -139,7 +139,17 @@ TApplicationRemote::TApplicationRemote(const char *url, Int_t debug,
 
    // Start the remote server
    Int_t rport = (port < fgPortUpper) ? port + 1 : port - 1;
-   TString sc(((script) ? script : gScript));
+   TString sc = gScript;
+   if (script && *script) {
+      // script is enclosed by " ", so ignore first " char
+      if (script[1] == '<') {
+         if (script[2])
+            sc.Form("source %s; %s", script+2, gScript);
+         else
+            Error("TApplicationRemote", "illegal script name <");
+      } else
+         sc = script;
+   }
    sc.ReplaceAll("\"","");
    TString userhost = fUrl.GetHost();
    if (strlen(fUrl.GetUser()) > 0)
-- 
GitLab