Пример #1
0
static void addMacroReplacements(MAC_HANDLE *macPvt,char *pval)
{
    char **pairs;
    long status;

    status = macParseDefns(macPvt,pval,&pairs);
    if(!status) {
        fprintf(stderr,"Error macParseDefns error\n");
        usageExit();
    }
    status = macInstallMacros(macPvt,pairs);
    if(!status) {
        fprintf(stderr,"Error macInstallMacros error\n");
        usageExit();
    }
    free((void *)pairs);
}
Пример #2
0
static long dbReadCOM(DBBASE **ppdbbase,const char *filename, FILE *fp,
	const char *path,const char *substitutions)
{
    long	status;
    inputFile	*pinputFile = NULL;
    char	*penv;
    char	**macPairs;
    
    if(*ppdbbase == 0) *ppdbbase = dbAllocBase();
    pdbbase = *ppdbbase;
    if(path && strlen(path)>0) {
	dbPath(pdbbase,path);
    } else {
	penv = getenv("EPICS_DB_INCLUDE_PATH");
	if(penv) {
	    dbPath(pdbbase,penv);
	} else {
	    dbPath(pdbbase,".");
	}
    }
    my_buffer = dbCalloc(MY_BUFFER_SIZE,sizeof(char));
    freeListInitPvt(&freeListPvt,sizeof(tempListNode),100);
    if(substitutions) {
	if(macCreateHandle(&macHandle,NULL)) {
	    epicsPrintf("macCreateHandle error\n");
            status = -1;
	    goto cleanup;
	}
	macParseDefns(macHandle,(char *)substitutions,&macPairs);
	if(macPairs ==NULL) {
	    macDeleteHandle(macHandle);
	    macHandle = NULL;
	} else {
	    macInstallMacros(macHandle,macPairs);
	    free((void *)macPairs);
	    mac_input_buffer = dbCalloc(MY_BUFFER_SIZE,sizeof(char));
	}
    }
    pinputFile = dbCalloc(1,sizeof(inputFile));
    if(filename) {
	pinputFile->filename = macEnvExpand(filename);
    }
    if(!fp) {
	FILE	*fp1;

	if(pinputFile->filename) pinputFile->path = dbOpenFile(pdbbase,pinputFile->filename,&fp1);
	if(!pinputFile->filename || !fp1) {
	    errPrintf(0,__FILE__, __LINE__,
		"dbRead opening file %s",pinputFile->filename);
	    free((void *)pinputFile->filename);
	    free((void *)pinputFile);
            status = -1;
            goto cleanup;
	}
	pinputFile->fp = fp1;
    } else {
	pinputFile->fp = fp;
    }
    pinputFile->line_num = 0;
    pinputFileNow = pinputFile;
    my_buffer[0] = '\0';
    my_buffer_ptr = my_buffer;
    ellAdd(&inputFileList,&pinputFile->node);
    status = pvt_yy_parse();
    dbFreePath(pdbbase);
    if(!status) { /*add RTYP and VERS as an attribute */
	DBENTRY	dbEntry;
	DBENTRY	*pdbEntry = &dbEntry;
	long	localStatus;

	dbInitEntry(pdbbase,pdbEntry);
	localStatus = dbFirstRecordType(pdbEntry);
	while(!localStatus) {
	    localStatus = dbPutRecordAttribute(pdbEntry,"RTYP",
		dbGetRecordTypeName(pdbEntry));
	    if(!localStatus)  {
		localStatus = dbPutRecordAttribute(pdbEntry,"VERS",
		    "none specified");
	    }
	    if(localStatus) {
		fprintf(stderr,"dbPutRecordAttribute status %ld\n",status);
	    } else {
	        localStatus = dbNextRecordType(pdbEntry);
	    }
	}
	dbFinishEntry(pdbEntry);
    }
cleanup:
    if(macHandle) macDeleteHandle(macHandle);
    macHandle = NULL;
    if(mac_input_buffer) free((void *)mac_input_buffer);
    mac_input_buffer = NULL;
    if(freeListPvt) freeListCleanup(freeListPvt);
    freeListPvt = NULL;
    if(my_buffer) free((void *)my_buffer);
    my_buffer = NULL;
    freeInputFileList();
    return(status);
}
Пример #3
0
/** Create this driver's NDAttributeList (pAttributeList) by reading an XML file
  * This clears any existing attributes from this drivers' NDAttributeList and then creates a new list
  * based on the XML file.  These attributes can then be associated with an NDArray by calling asynNDArrayDriver::getAttributes()
  * passing it pNDArray->pAttributeList.
  * 
  * The following simple example XML file illustrates the way that both PVAttribute and paramAttribute attributes are defined.
  * <pre>
  * <?xml version="1.0" standalone="no" ?>
  * \<Attributes>
  * \<Attribute name="AcquireTime"         type="EPICS_PV" source="13SIM1:cam1:AcquireTime"      dbrtype="DBR_NATIVE"  description="Camera acquire time"/>
  * \<Attribute name="CameraManufacturer"  type="PARAM"    source="MANUFACTURER"                 datatype="STRING"     description="Camera manufacturer"/>
  * \</Attributes>
  * </pre>
  * Each NDAttribute (currently either an PVAttribute or paramAttribute, but other types may be added in the future) 
  * is defined with an XML <b>Attribute</b> tag.  For each attribute there are a number of XML attributes
  * (unfortunately there are 2 meanings of attribute here: the NDAttribute and the XML attribute).  
  * XML attributes have the syntax name="value".  The XML attribute names are case-sensitive and must be lower case, i.e. name="xxx", not NAME="xxx".  
  * The XML attribute values are specified by the XML Schema and are always uppercase for <b>datatype</b> and <b>dbrtype</b> attributes.
  * The XML attribute names are listed here:
  *
  * <b>name</b> determines the name of the NDAttribute.  It is required, must be unique, is case-insensitive, 
  * and must start with a letter.  It can include only letters, numbers and underscore. (No whitespace or other punctuation.)
  *
  * <b>type</b> determines the type of the NDAttribute.  "EPICS_PV" creates a PVAttribute, while "PARAM" creates a paramAttribute.
  * The default is EPICS_PV if this XML attribute is absent.
  *
  * <b>source</b> determines the source of the NDAttribute.  It is required. If type="EPICS_PV" then this is the name of the EPICS PV, which is
  * case-sensitive. If type="PARAM" then this is the drvInfo string that is used in EPICS database files (e.g. ADBase.template) to identify
  * this parameter.
  *
  * <b>dbrtype</b> determines the data type that will be used to read an EPICS_PV value with channel access.  It can be one of the standard EPICS
  * DBR types (e.g. "DBR_DOUBLE", "DBR_STRING", ...) or it can be the special type "DBR_NATIVE" which means to use the native channel access
  * data type for this PV.  The default is DBR_NATIVE if this XML attribute is absent.  Always use uppercase.
  *
  * <b>datatype</b> determines the parameter data type for type="PARAM".  It must match the actual data type in the driver or plugin
  * parameter library, and must be "INT", "DOUBLE", or "STRING".  The default is "INT" if this XML attribute is absent.   Always use uppercase.
  * 
  * <b>addr</b> determines the asyn addr (address) for type="PARAM".  The default is 0 if the XML attribute is absent.
  * 
  * <b>description</b> determines the description for this attribute.  It is not required, and the default is a NULL string.
  *
  */
asynStatus asynNDArrayDriver::readNDAttributesFile()
{
    const char *pName, *pSource, *pAttrType, *pDescription;
    xmlDocPtr doc;
    xmlNode *Attr, *Attrs;
    std::ostringstream buff;
    std::string buffer;
    std::ifstream infile;
    std::string attributesMacros;
    std::string fileName;
    MAC_HANDLE *macHandle;
    char **macPairs;
    int bufferSize;
    char *tmpBuffer = 0;
    int status;
    static const char *functionName = "readNDAttributesFile";
    
    getStringParam(NDAttributesFile, fileName);
    getStringParam(NDAttributesMacros, attributesMacros);

    /* Clear any existing attributes */
    this->pAttributeList->clear();
    if (fileName.length() == 0) return asynSuccess;

    infile.open(fileName.c_str());
    if (infile.fail()) {
        asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
            "%s::%s error opening file %s\n", 
            driverName, functionName, fileName.c_str());
        setIntegerParam(NDAttributesStatus, NDAttributesFileNotFound);
        return asynError;
    }
    buff << infile.rdbuf();
    buffer = buff.str();

    // We now have file in memory.  Do macro substitution if required
    if (attributesMacros.length() > 0) {
        macCreateHandle(&macHandle, 0);
        status = macParseDefns(macHandle, attributesMacros.c_str(), &macPairs);
        if (status < 0) { 
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s::%s, error parsing macros\n", driverName, functionName);
            goto done_macros;
        }
        status = macInstallMacros(macHandle, macPairs);
        if (status < 0) {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s::%s, error installed macros\n", driverName, functionName);
            goto done_macros;
        }
        // Create a temporary buffer 10 times larger than input buffer
        bufferSize = (int)(buffer.length() * 10);
        tmpBuffer = (char *)malloc(bufferSize);
        status = macExpandString(macHandle, buffer.c_str(), tmpBuffer, bufferSize);
        // NOTE: There is a bug in macExpandString up to 3.14.12.6 and 3.15.5 so that it does not return <0
        // if there is an undefined macro which is not the last macro in the string.
        // We work around this by testing also if the returned string contains ",undefined)".  This is
        // unlikely to occur otherwise.  Eventually we can remove this test.
        if ((status < 0)  || strstr(tmpBuffer, ",undefined)")) {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s::%s, error expanding macros\n", driverName, functionName);
            goto done_macros;
        }
        if (status >= bufferSize) {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s::%s, error macro buffer too small\n", driverName, functionName);
            goto done_macros;
        }
        buffer = tmpBuffer;
done_macros:
        macDeleteHandle(macHandle);
        free(tmpBuffer);
        if (status < 0) {
            setIntegerParam(NDAttributesStatus, NDAttributesMacroError);
            return asynError;
        } 
    }
    // Assume failure
    setIntegerParam(NDAttributesStatus, NDAttributesXMLSyntaxError);
    doc = xmlReadMemory(buffer.c_str(), (int)buffer.length(), "noname.xml", NULL, 0);
    if (doc == NULL) {
        asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
            "%s:%s: error creating doc\n", driverName, functionName);
        return asynError;
    }
    Attrs = xmlDocGetRootElement(doc);
    if ((!xmlStrEqual(Attrs->name, (const xmlChar *)"Attributes"))) {
        asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
            "%s:%s: cannot find Attributes element\n", driverName, functionName);
        return asynError;
    }
    for (Attr = xmlFirstElementChild(Attrs); Attr; Attr = xmlNextElementSibling(Attr)) {
        pName = (const char *)xmlGetProp(Attr, (const xmlChar *)"name");
        if (!pName) {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s:%s: name attribute not found\n", driverName, functionName);
            return asynError;
        }
        pDescription = (const char *)xmlGetProp(Attr, (const xmlChar *)"description");
        if (!pDescription) pDescription = "";
        pSource = (const char *)xmlGetProp(Attr, (const xmlChar *)"source");
        if (!pSource) {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s:%s: source attribute not found for attribute %s\n", driverName, functionName, pName);
            return asynError;
        }
        pAttrType = (const char *)xmlGetProp(Attr, (const xmlChar *)"type");
        if (!pAttrType) pAttrType = NDAttribute::attrSourceString(NDAttrSourceEPICSPV);
        if (strcmp(pAttrType, NDAttribute::attrSourceString(NDAttrSourceEPICSPV)) == 0) {
            const char *pDBRType = (const char *)xmlGetProp(Attr, (const xmlChar *)"dbrtype");
            int dbrType = DBR_NATIVE;
            if (pDBRType) {
                if      (!strcmp(pDBRType, "DBR_CHAR"))   dbrType = DBR_CHAR;
                else if (!strcmp(pDBRType, "DBR_SHORT"))  dbrType = DBR_SHORT;
                else if (!strcmp(pDBRType, "DBR_ENUM"))   dbrType = DBR_ENUM;
                else if (!strcmp(pDBRType, "DBR_INT"))    dbrType = DBR_INT;
                else if (!strcmp(pDBRType, "DBR_LONG"))   dbrType = DBR_LONG;
                else if (!strcmp(pDBRType, "DBR_FLOAT"))  dbrType = DBR_FLOAT;
                else if (!strcmp(pDBRType, "DBR_DOUBLE")) dbrType = DBR_DOUBLE;
                else if (!strcmp(pDBRType, "DBR_STRING")) dbrType = DBR_STRING;
                else if (!strcmp(pDBRType, "DBR_NATIVE")) dbrType = DBR_NATIVE;
                else {
                    asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                        "%s:%s: unknown dbrType = %s for attribute %s\n", driverName, functionName, pDBRType, pName);
                   return asynError;
                }
            }
            asynPrint(pasynUserSelf, ASYN_TRACEIO_DRIVER,
                "%s:%s: Name=%s, PVName=%s, pDBRType=%s, dbrType=%d, pDescription=%s\n",
                driverName, functionName, pName, pSource, pDBRType, dbrType, pDescription);
#ifndef EPICS_LIBCOM_ONLY
            PVAttribute *pPVAttribute = new PVAttribute(pName, pDescription, pSource, dbrType);
            this->pAttributeList->add(pPVAttribute);
#endif
        } else if (strcmp(pAttrType, NDAttribute::attrSourceString(NDAttrSourceParam)) == 0) {
            const char *pDataType = (const char *)xmlGetProp(Attr, (const xmlChar *)"datatype");
            if (!pDataType) pDataType = "int";
            const char *pAddr = (const char *)xmlGetProp(Attr, (const xmlChar *)"addr");
            int addr=0;
            if (pAddr) addr = strtol(pAddr, NULL, 0);
            asynPrint(pasynUserSelf, ASYN_TRACEIO_DRIVER,
                "%s:%s: Name=%s, drvInfo=%s, dataType=%s,pDescription=%s\n",
                driverName, functionName, pName, pSource, pDataType, pDescription); 
            paramAttribute *pParamAttribute = new paramAttribute(pName, pDescription, pSource, addr, this, pDataType);
            this->pAttributeList->add(pParamAttribute);
        } else if (strcmp(pAttrType, NDAttribute::attrSourceString(NDAttrSourceFunct)) == 0) {
            const char *pParam = (const char *)xmlGetProp(Attr, (const xmlChar *)"param");
            if (!pParam) pParam = epicsStrDup("");
            asynPrint(pasynUserSelf, ASYN_TRACEIO_DRIVER,
                "%s:%s: Name=%s, function=%s, pParam=%s, pDescription=%s\n",
                driverName, functionName, pName, pSource, pParam, pDescription); 
#ifndef EPICS_LIBCOM_ONLY
            functAttribute *pFunctAttribute = new functAttribute(pName, pDescription, pSource, pParam);
            this->pAttributeList->add(pFunctAttribute);
#endif
        } else {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s:%s: unknown attribute type = %s for attribute %s\n", driverName, functionName, pAttrType, pName);
            return asynError;
        }
    }
    setIntegerParam(NDAttributesStatus, NDAttributesOK);
    // Wait a short while for channel access callbacks on EPICS PVs
    epicsThreadSleep(0.5);
    // Get the initial values
    this->pAttributeList->updateValues();
    return asynSuccess;
}