Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  <2017 Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  <2017
<== Date ==> <== Thread ==>

Subject: RE: Problem in macLib macExpandString?
From: Mark Rivers <rivers@cars.uchicago.edu>
To: EPICS Tech-Talk <tech-talk@aps.anl.gov>
Date: Sun, 16 Apr 2017 20:35:12 +0000
I've written a little test program that I think proves there is a bug in macExpandString:

Here is the program.  It defines the macros A and B.  It does not define the macro C.  It then calls macExpandString 3 times.  The first time it references only macros A and B, the second time A, B, C in that order, and the third time C, A, B in that order.

#####################################
#include <stdio.h>
#include <macLib.h>

int main(int argc, char *argv[])
{
  MAC_HANDLE *macHandle;
  char **macPairs;
  char expandOutput[100];
  const char *test1 = "$(A)$(B)";
  const char *test2 = "$(A)$(B)$(C)";
  const char *test3 = "$(C)$(B)$(A)";
  int status;

  macCreateHandle(&macHandle, 0);
  status = macParseDefns(macHandle, "A=1,B=2", &macPairs);
  status = macInstallMacros(macHandle, macPairs);

  status = macExpandString(macHandle, test1, expandOutput, sizeof(expandOutput));
  printf("Test1 input=%s, status=%d\n", test1, status);
  status = macExpandString(macHandle, test2, expandOutput, sizeof(expandOutput));
  printf("Test2 input=%s, status=%d\n", test2, status);
  status = macExpandString(macHandle, test3, expandOutput, sizeof(expandOutput));
  printf("Test3 input=%s, status=%d\n", test3, status);

  return 0;
}
#####################################

Here is the output:

corvette:ADCore/ADApp/ADSrc>../../bin/linux-x86_64/test_macExpandString
Test1 input=$(A)$(B), status=2
Test2 input=$(A)$(B)$(C), status=-16
Test3 input=$(C)$(B)$(A), status=16

The first 2 tests results are OK.  When only A and B are referenced it returns 2 characters as expected.  When the undefined C is the last macro in the string macExpandString returns -16, which is correct.  However, when the undefined C is not the last macro in the string macExpandString returns a positive number.  This is incorrect according to the documentation for macExpandString (http://www.aps.anl.gov/epics/base/R3-15/5-docs/AppDevGuide.pdf page 281), which says:

/*following returns #chars copied, <0 if any macros are undefined*/
long macExpandString(
    MAC_HANDLE *handle,    /* opaque handle */
    char *src,             /* source string */
    char *dest,            /* destination string */
    long maxlen            /* maximum number of characters to copy */
                           /* to destination string */
);

I'm surprised this has not been seen previously, so perhaps I am doing something wrong?

Thanks,
Mark





________________________________
From: Mark Rivers
Sent: Sunday, April 16, 2017 2:41 PM
To: EPICS Tech-Talk
Subject: Problem in macLib macExpandString?

I am trying to use macLib to do macro substitution in base 3.15.5.  It is generally working.  However sometimes when there is an undefined macro the return value from macExpandString is not negative when it should be.

This is the code I am using to call macExpandString, with some debugging printf calls.  It prints the value returned by macValueExpand, as well as the input and output strings.

        // Create a temporary buffer 10 times larger than input buffer
        int bufferSize = buffer.length() * 10;
        char *tmpBuffer = (char *)malloc(bufferSize);
printf("Calling macExpandString, input length=%d, output buffer size=%d\n", (int)buffer.length(), bufferSize);
        status = macExpandString(macHandle, buffer.c_str(), tmpBuffer, bufferSize);
printf("macExpandString returned %d\n", status);
printf("input:\n########\n%s\n########\n\n", buffer.c_str());
printf("output:\n########\n%s\n########\n\n", tmpBuffer);


Here is an example.  In this case the CAMERA macro is not defined.

#####################################################################################################################
Calling macExpandString, input length=1013, output buffer size=10130
macLib: macro CAMERA is undefined (expanding string <?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>
)
macExpandString returned 1027
input:
########
<?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>

########

output:
########
<?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA,undefined)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="ID13us:Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="ID13us:Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>

########

#####################################################################################################################

Note above that macExpand string is printing an error message as it should.  However, it is returning a positive integer 1037, while it should be returning a negative value because there was an undefined macro.


If instead of CAMERA being undefined I don't define ID instead I get this output:

#####################################################################################################################
Calling macExpandString, input length=1013, output buffer size=10130
macLib: macro ID is undefined (expanding string <?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>
)
macLib: macro ID is undefined (expanding string <?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>
)
macExpandString returned -1036
input:
########
<?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="$(CAMERA)AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>

########

output:
########
<?xml version="1.0" standalone="no" ?>
<!-- Attributes -->
<Attributes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:noNamespaceSchemaLocation="../../../../../ADCore/XML_schema/NDAttributes.xsd"
    >
    <Attribute name="AcquireTime"         type="EPICS_PV" source="13SIM1:cam1:AcquireTime"         dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
    <Attribute name="RingCurrent"         type="EPICS_PV" source="S:SRcurrentAI"                dbrtype="DBR_NATIVE"  description="Storage ring current"/>
    <Attribute name="RingCurrent_EGU"     type="EPICS_PV" source="S:SRcurrentAI.EGU"            dbrtype="DBR_NATIVE"  description="Storage ring current units"/>
    <Attribute name="ID_Energy"           type="EPICS_PV" source="$(ID,undefined)Energy"                  dbrtype="DBR_NATIVE"  description="Undulator energy"/>
    <Attribute name="ID_Energy_EGU"       type="EPICS_PV" source="$(ID,undefined)Energy.EGU"              dbrtype="DBR_NATIVE"  description="Undulator energy units"/>
</Attributes>

########
#####################################################################################################################

This is behaving correctly, macExpandString is returning a negative number, -1036 as it should.

Am I doing something wrong or is this a bug?


Thanks,
Mark


Replies:
RE: Problem in macLib macExpandString? Mark Rivers
References:
Problem in macLib macExpandString? Mark Rivers

Navigate by Date:
Prev: Problem in macLib macExpandString? Mark Rivers
Next: Re: CAS: partial (damaged?) UDP msg Michael Davidsaver
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  <2017
Navigate by Thread:
Prev: Problem in macLib macExpandString? Mark Rivers
Next: RE: Problem in macLib macExpandString? Mark Rivers
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  <2017
ANJ, 16 Apr 2017 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·