From 980507583d3e9d67b33d81416ccc574c181738f1 Mon Sep 17 00:00:00 2001
From: Tony Farrell <tony.farrell@mq.edu.au>
Date: Thu, 24 Jan 2019 02:04:35 +1100
Subject: [PATCH] ditscmd add q option. ditsgetinfo doesnt output bad status if
 just looking for a task which isnt there.

        In ditsgetinfo, If checking for a task running, don't output
        the unknown task error if it is not
        just return bad status.  This makes
        creating scripts which check for tasks running easier

        Add -q option to ditscmd, a parameter get which does not use MsgOut
          for output, instead just stdout, for use in scripts.

        ditscmd monitor operations now set the Send-Current flag, so the
          current values are seen on startup.
---
 ditscmd.c     | 89 ++++++++++++++++++++++++++++++++++++++++++++-------
 ditsgetinfo.c | 25 ++++++++++++++-
 dmakefile     |  2 +-
 3 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/ditscmd.c b/ditscmd.c
index fdbc14c..44b88b2 100644
--- a/ditscmd.c
+++ b/ditscmd.c
@@ -81,8 +81,8 @@
         [-c]          Send control message.
 	[-p] 	      Send parameter monitor message.
 	[-l]	      Send multiple parameter get message (redunant, you
-                      should use -g).
-        [-v]          Report reponses verbosely using SdsList, not briefly 
+                      can use -g).
+        [-v]          Report responses verbosely using SdsList, not briefly 
                        using  ArgToString. Overides any -w option.
         [-w]          Write any returned Sds structure to the file ditscmd_out.sds.
                        Overrides any -v option. On successfully completion program
@@ -94,6 +94,11 @@
                         naming convention to using the specified 
                         character.  Only the first character of the value
                         is used.
+        [-q]          Special fetch of parameters.  Instead of outputing
+                        the result using MsgOut, it is directly output to stdout.  
+                         This makes it easier to fetch parameter values
+                         in scripts.  Is overriden by -w, -v and any
+                         flag which causes another message type to be sent.
         [-x]          If the sub-task exits, then don't return an error code from
                             this program.
         [--]          Stop processing options. You will need to use this if your
@@ -169,6 +174,9 @@
                            improve the error output.
                           Import error handling for success argument
                            handling failures.
+      23-Jan-2018 - TJF - Add -q option.
+                          For monitor messages, always set the SendCur
+                            flag as that is what I normally want to happen.
 
 
       {@change entry@}
@@ -253,6 +261,7 @@ static void *use_rcsId = (0 ? (void *)(&use_rcsId) : (void *) &rcsId);
 #define NAMES_PARAM_FLAG   2
 #define ALL_PARAM_FLAG 4
 #define WRITE_OUT_FLAG 8
+#define QUICK_PARAM_FLAG 16
 
 
 /*
@@ -340,7 +349,7 @@ Command options:\n\
         [-c]          Send control message.\n\
 	[-p] 	      Send parameter monitor message.\n\
 	[-l]	      Send multiple parameter get message (redunant, you\n\
-                      should use -g).\n\
+                      can just use -g).\n\
         [-v]          Report responses verbosely using SdsList, not briefly \n\
                        using  ArgToString.  Overrides any -w option.\n\
         [-w]          Write responses to the file ditscmd_out.sds (sorry, program\n\
@@ -354,6 +363,11 @@ Command options:\n\
                         naming convention to using the specified \n\
                         character.  Only the first character of the value\n\
                         is used.\n\
+        [-q]          Special fetch of a  parameters.  Instead of outputing\n\
+                        the result using MsgOut, it is directly output to stdout.\n\
+                        This makes it easier to fetch parameter values in scripts.\n\
+                        Is overriden by -w, -v and any flag which causes another\n\
+                        message type to be sent.\n\
         [-x]          If the sub-task exits, then don't return an error code\n\
                             from this program.\n\
         [--]          Stop processing options. You will need to use this if your\n\
@@ -414,7 +428,7 @@ DPUBLIC int main(int argc, char *argv[])
 /*
  *  Get program options
  */
-    while ((c = getopt (argc,argv,"a:b:m:n:z:okgscplvxhw")) != -1)
+    while ((c = getopt (argc,argv,"a:b:m:n:z:okgscplvxhwq")) != -1)
     {
         switch (c)
         {
@@ -459,6 +473,7 @@ DPUBLIC int main(int argc, char *argv[])
                  break;
              case 'p':              /* Send a control message   */
                  details.MsgType = DITS_MSG_MONITOR;
+                 details.SendCur = 1; /* Always send current parameter immediately*/
                  break;
              case 'g':              /* Send a get message       */
              case 'l':              /* Send mutiple parameter get message   */
@@ -466,11 +481,15 @@ DPUBLIC int main(int argc, char *argv[])
                  break;
              case 'v':              /* Output replies using SdsList   */
                  flags |= VERBOSE_FLAG;
-                 flags  &= ~WRITE_OUT_FLAG; /* Cancel write out flag */
+                 flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter
+                                                flag */
+                 flags &= ~WRITE_OUT_FLAG; /* Cancel write out flag */
                  break;
              case 'w':              /* Write output to ditscmd_out.sds   */
                  flags |= WRITE_OUT_FLAG;
                  flags  &= ~VERBOSE_FLAG; /* Cancel verbose flag */
+                 flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter
+                                                flag */
                  break;
              case 'z':
                  nameArgsSplitChar = optarg[0];
@@ -478,6 +497,12 @@ DPUBLIC int main(int argc, char *argv[])
              case 'x':
                  expectExit = 1;
                  break;
+             case 'q':
+                 details.MsgType = DITS_MSG_MGETPARAM;
+                 flags |= QUICK_PARAM_FLAG;
+                 flags  &= ~VERBOSE_FLAG; /* Cancel verbose flag */
+                 flags  &= ~WRITE_OUT_FLAG; /* Cancel write out flag */
+                break;                 
              case 'h':
                  Help();
                  exit(0);
@@ -487,6 +512,12 @@ DPUBLIC int main(int argc, char *argv[])
         }   /* switch */
     }       /* while  */
 
+    /* The quick param flag only makes sense for parameters */
+    if (details.MsgType != DITS_MSG_MGETPARAM)
+    {
+        flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter flag */
+    }
+        
 /*
  *  Get the task and action names
  */
@@ -670,15 +701,26 @@ extern int ditscmd(char *command)
                     break;
                 case 'p':
                     details.MsgType = DITS_MSG_MONITOR;
+                    details.SendCur = 1; /* Always send current parameter immediately*/
                     break;
                 case 'v':              /* Verbose output of reply.  */
                     flags |= VERBOSE_FLAG;
                     flags  &= ~WRITE_OUT_FLAG; /* Cancel write out flag */
+                    flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter
+                                                   flag */
                     break;
                 case 'w':              /* Write output to ditscmd_out.sds   */
                     flags |= WRITE_OUT_FLAG;
                     flags  &= ~VERBOSE_FLAG; /* Cancel verbose flag */
+                    flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter
+                                                flag */
                     break;
+                case 'q':
+                    details.MsgType = DITS_MSG_MGETPARAM;
+                    flags |= QUICK_PARAM_FLAG;
+                    flags  &= ~VERBOSE_FLAG; /* Cancel verbose flag */
+                    flags  &= ~WRITE_OUT_FLAG; /* Cancel write out flag */
+                    break;                 
                 case 'g':
                 case 'l':             /* Send mutiple parameter get message   */
                     details.MsgType = DITS_MSG_MGETPARAM;
@@ -760,6 +802,11 @@ extern int ditscmd(char *command)
         ErsRep(0, &status ,"You did not supply an Action/Parameter name");
         status = DITS__INVARG;
     }
+    /* The quick param flag only makes sense for parameters */
+    if (details.MsgType != DITS_MSG_MGETPARAM)
+    {
+        flags &= ~QUICK_PARAM_FLAG; /* Cancel quick parameter flag */
+    }
     /*
      * If we have more then one argument, then the NAMES_PARAM_FLAG and
      * ALL_PARAM_FLAGS must not be set.
@@ -817,8 +864,6 @@ extern int ditscmd(char *command)
         }
     }
 /*
- *  This is the only bit of the code common to both when main is needed and
- *  when it is not needed.
  */
     details.ErrorHandler = ErrorHandler;
     details.SuccessHandler = SuccessHandler;
@@ -958,7 +1003,10 @@ DPRIVATE int SuccessHandler(DuiDetailsType *details,  StatusType *status)
         else if (flags & NAMES_PARAM_FLAG)
         {
             /* borrow this function from Dui - to avoid duplication, but 
-             * it is not really worthy of a public interface
+             * it is not really worthy of a public interface.  Just 
+             * nicel outputs the values in the _NAMES_ parameter
+             * message reply.
+             * 
              */
             Dui___MessageParamNames(details->TaskName, ArgId, status);
         }
@@ -974,8 +1022,21 @@ DPRIVATE int SuccessHandler(DuiDetailsType *details,  StatusType *status)
                 char buffer[MSG_C_LEN];
                 ErsPush();
                 ArgToString(ArgId,sizeof(buffer),&length,buffer,status);
-            
-                buffer[length] = '\0';
+                /* 
+                 * If the last character is a space, this is normally
+                 * because this is a structured item (which happens
+                 * most of the time due to the way values are returned).
+                 * We want to remove that space.  
+                 */
+                if (length>0)
+                {
+                    if (buffer[length-1] == ' ')
+                        buffer[length-1] = '\0';
+                    else
+                        buffer[length] = '\0';
+                }
+                else
+                    buffer[0] = '\0';
                 if (*status == ARG__CNVERR)
                 {
                     ErsAnnul(status);
@@ -987,8 +1048,14 @@ DPRIVATE int SuccessHandler(DuiDetailsType *details,  StatusType *status)
                     ErsAnnul(status);
                     MsgOut(status,"Non scaler argument returned - suggest you consider the \"-v\" option to ditscmd");
                 }
+                else if ((*status== STATUS__OK)&&(flags&QUICK_PARAM_FLAG))
+                {
+                    printf("%s\n", buffer);
+                }
                 else if (*status == STATUS__OK)
+                {
                     MsgOut(status,"%s", buffer);
+                }
                 ErsPop();
             }
         }
@@ -1051,8 +1118,8 @@ DPRIVATE int TriggerHandler(DuiDetailsType *details,  StatusType *status)
              * Try to convert the argument returned to a string.
              */
             ArgToString(ArgId,sizeof(buffer),&length,buffer,status);
-            
             buffer[length] = '\0';
+            
             if (*status == ARG__CNVERR)
             {
                 ErsAnnul(status);
diff --git a/ditsgetinfo.c b/ditsgetinfo.c
index 289f669..3c5125c 100644
--- a/ditsgetinfo.c
+++ b/ditsgetinfo.c
@@ -40,7 +40,11 @@
 
       The following options are mutually exclusive and the default is -running.
 
-       [-running]           Just return a success status if the task is running.
+       [-running]           Just return a success status if the task
+                            is running.  If it is not, return error
+                            code 1 (but not other message unless some
+                            other error occured).
+
        [-type]              If task is running, output its type.
        [-descr]             If task is running, output its description.
        [-name]              If task is running, output its name.
@@ -64,6 +68,13 @@
       25-Mar-1998 - TJF - Register variable i now marked as int as the old
                           usage is not to be supported in a future version
                           of the C language.
+     23-Jan -2019 - TJF - IF checking for a task running, don't output
+                           the unknown task error if it is not
+                           just return bad status.  This makes
+                           creating scripts which check for tasks
+                           running easier.
+
+
       {@change entry@}
 
  *   @(#) $Id$
@@ -351,6 +362,18 @@ extern int ditscmdload(char *command)
         DuiTokenShut(&context);
 #   endif
 
+    /*
+     * If we were only interested in if the task is running and it
+     * is not, then don't return bad status, just return
+     * a bad exit status
+     */
+    if ((DetailsToGet == InfoRunning)&&
+        (status == DITS__UNKNTASK))
+    {
+        StatusType ignore = STATUS__OK;
+        DitsStop(thisTask, &ignore);
+        return 1;
+    }
     return(DitsStop(thisTask,&status));
     
 }
diff --git a/dmakefile b/dmakefile
index 46cb089..552b964 100644
--- a/dmakefile
+++ b/dmakefile
@@ -51,7 +51,7 @@
  
 #BeginConfig 
 #DramaSystem                    /* Indicates we are part of drama itself */
-ACMM_RELEASE=3_109$(ACMMBUILDVER)
+ACMM_RELEASE=3_110$(ACMMBUILDVER)
 RELEASE=r$(ACMM_RELEASE)
 SYSTEM=dits			/* System name (for release	*/
 /*
-- 
GitLab