/// Load a db file multiple times substituting a specified macro according to a number range. /// /// The \a dbFile and \a macros arguments are like the normal dbLoadRecords() however /// it is possible to embed a macro within these whose value follows the range \a start to \a stop. /// You can either load the same \a dbFile multiple times with different macros, or even load /// different database files by using \a loopVar as part of the filename. If you want to use a list /// of (non-numeric) substitutions rather than an integer range see dbLoadRecordsList() /// /// The name of the macro to be used for substitution is contained in \a loopVar and needs to be /// reference in an \\ escaped way to make sure EPICS does not try to substitute it too soon. /// as well as the \a macros the \a dbFile is also passed the \a loopVar macro value /// @code /// dbLoadRecordsLoop("file\$(I).db", "P=1,Q=Hello\$(I)", "I", 1, 4) /// @endcode /// /// @param[in] dbFile @copydoc dbLoadRecordsLoopInitArg0 /// @param[in] macros @copydoc dbLoadRecordsLoopInitArg1 /// @param[in] loopVar @copydoc dbLoadRecordsLoopInitArg2 /// @param[in] start @copydoc dbLoadRecordsLoopInitArg3 /// @param[in] stop @copydoc dbLoadRecordsLoopInitArg4 /// @param[in] step @copydoc dbLoadRecordsLoopInitArg5 epicsShareFunc void dbLoadRecordsLoop(const char* dbFile, const char* macros, const char* loopVar, int start, int stop, int step) { char loopVal[32]; if (loopVar == NULL) { dbLoadRecords(dbFile, macros); return; } if (step <= 0) { step = 1; } std::string macros_s, dbFile_s; subMacros(macros_s, macros, loopVar); subMacros(dbFile_s, dbFile, loopVar); MAC_HANDLE* mh = NULL; char macros_exp[1024], dbFile_exp[1024]; macCreateHandle(&mh, NULL); loadMacEnviron(mh); for(int i = start; i <= stop; i += step) { macPushScope(mh); epicsSnprintf(loopVal, sizeof(loopVal), "%d", i); macPutValue(mh, loopVar, loopVal); macExpandString(mh, macros_s.c_str(), macros_exp, sizeof(macros_exp)); macExpandString(mh, dbFile_s.c_str(), dbFile_exp, sizeof(dbFile_exp)); std::ostringstream new_macros; new_macros << macros_exp << (strlen(macros_exp) > 0 ? "," : "") << loopVar << "=" << i; std::cout << "--> (" << i << ") dbLoadRecords(\"" << dbFile_exp << "\",\"" << new_macros.str() << "\")" << std::endl; dbLoadRecords(dbFile_exp, new_macros.str().c_str()); macPopScope(mh); } macDeleteHandle(mh); }
/// expand epics environment strings using previously saved environment /// based on EPICS macEnvExpand() char* lvDCOMInterface::envExpand(const char *str) { long destCapacity = 128; char *dest = NULL; int n; do { destCapacity *= 2; /* * Use free/malloc rather than realloc since there's no need to * keep the original contents. */ free(dest); dest = static_cast<char*>(mallocMustSucceed(destCapacity, "lvDCOMInterface::envExpand")); n = macExpandString(m_mac_env, str, dest, destCapacity); } while (n >= (destCapacity - 1)); if (n < 0) { free(dest); dest = NULL; } else { size_t unused = destCapacity - ++n; if (unused >= 20) dest = static_cast<char*>(realloc(dest, n)); } return dest; }
/// Load a db file multiple times according to a list of items separated by known separator(s). /// /// The \a dbFile and \a macros arguments are like the normal dbLoadRecords() however /// it is possible to embed a macro within these whose value takes a value from the \a list. /// You can either load the same \a dbFile multiple times with different macros, or even load /// different database files by using \a loopVar as part of the filename. If you want to use a /// pure numeric range see dbLoadRecordsLoop() /// /// The name of the macro to be used for substitution is contained in \a loopVar and needs to be /// reference in an \\ escaped way to make sure EPICS does not try to substitute it too soon. /// as well as the \a macros the \a dbFile is also passed the \a loopVar macro value /// @code /// dbLoadRecordsList("file\$(S).db", "P=1,Q=Hello\$(S)", "S", "A;B;C", ";") /// @endcode /// /// @param[in] dbFile @copydoc dbLoadRecordsListInitArg0 /// @param[in] macros @copydoc dbLoadRecordsListInitArg1 /// @param[in] loopVar @copydoc dbLoadRecordsListInitArg2 /// @param[in] list @copydoc dbLoadRecordsListInitArg3 /// @param[in] sep @copydoc dbLoadRecordsListInitArg4 epicsShareFunc void dbLoadRecordsList(const char* dbFile, const char* macros, const char* loopVar, const char* list, const char* sep) { static const char* default_sep = ";"; if (loopVar == NULL || list == NULL) { dbLoadRecords(dbFile, macros); return; } if (sep == NULL) { sep = default_sep; } std::string macros_s, dbFile_s; subMacros(macros_s, macros, loopVar); subMacros(dbFile_s, dbFile, loopVar); MAC_HANDLE* mh = NULL; char macros_exp[1024], dbFile_exp[1024]; macCreateHandle(&mh, NULL); loadMacEnviron(mh); char* saveptr = NULL; char* list_tmp = strdup(list); char* list_item = epicsStrtok_r(list_tmp, sep, &saveptr); while(list_item != NULL) { macPushScope(mh); macPutValue(mh, loopVar, list_item); macExpandString(mh, macros_s.c_str(), macros_exp, sizeof(macros_exp)); macExpandString(mh, dbFile_s.c_str(), dbFile_exp, sizeof(dbFile_exp)); std::ostringstream new_macros; new_macros << macros_exp << (strlen(macros_exp) > 0 ? "," : "") << loopVar << "=" << list_item; std::cout << "--> (" << list_item << ") dbLoadRecords(\"" << dbFile_exp << "\",\"" << new_macros.str() << "\")" << std::endl; dbLoadRecords(dbFile_exp, new_macros.str().c_str()); list_item = epicsStrtok_r(NULL, sep, &saveptr); macPopScope(mh); } free(list_tmp); macDeleteHandle(mh); }
static int db_yyinput(char *buf, int max_size) { int l,n; char *fgetsRtn; if(yyAbort) return(0); if(*my_buffer_ptr==0) { while(TRUE) { /*until we get some input*/ if(macHandle) { fgetsRtn = fgets(mac_input_buffer,MY_BUFFER_SIZE, pinputFileNow->fp); if(fgetsRtn) { n = macExpandString(macHandle,mac_input_buffer, my_buffer,MY_BUFFER_SIZE); if(n<0) { errPrintf(0,__FILE__, __LINE__, "macExpandString failed for file %s", pinputFileNow->filename); } } } else { fgetsRtn = fgets(my_buffer,MY_BUFFER_SIZE,pinputFileNow->fp); } if(fgetsRtn) break; if(fclose(pinputFileNow->fp)) errPrintf(0,__FILE__, __LINE__, "Closing file %s",pinputFileNow->filename); free((void *)pinputFileNow->filename); ellDelete(&inputFileList,(ELLNODE *)pinputFileNow); free((void *)pinputFileNow); pinputFileNow = (inputFile *)ellLast(&inputFileList); if(!pinputFileNow) return(0); } if(dbStaticDebug) fprintf(stderr,"%s",my_buffer); pinputFileNow->line_num++; my_buffer_ptr = &my_buffer[0]; } l = strlen(my_buffer_ptr); n = (l<=max_size ? l : max_size); memcpy(buf,my_buffer_ptr,n); my_buffer_ptr += n; return(n); }
static void makeSubstitutions(void *inputPvt,void *macPvt,char *templateName) { char *input; static char buffer[MAX_BUFFER_SIZE]; int n; static int unexpWarned = 0; inputBegin(inputPvt,templateName); while((input = inputNextLine(inputPvt))) { int expand=1; char *p; char *command = 0; p = input; /*skip whitespace at beginning of line*/ while(*p && (isspace(*p))) ++p; /*Look for i or s */ if(*p && (*p=='i' || *p=='s')) command = p; if(command) { char *pstart; char *pend; char *copy; int cmdind=-1; int i; for(i=0; i< NELEMENTS(cmdNames); i++) { if(strstr(command,cmdNames[i])) { cmdind = i; } } if(cmdind<0) goto endif; p = command + strlen(cmdNames[cmdind]); /*skip whitespace after command*/ while(*p && (isspace(*p))) ++p; /*Next character must be quote*/ if((*p==0) || (*p!='"')) goto endif; pstart = ++p; /*Look for end quote*/ while(*p && (*p!='"')) { /*allow escape for imbeded quote*/ if((*p=='\\') && *(p+1)=='"') { p += 2; continue; } else { if(*p=='"') break; } ++p; } pend = p; if(*p==0) goto endif; /*skip quote and any trailing blanks*/ while(*++p==' ') ; if(*p != '\n' && *p !=0) goto endif; copy = calloc(pend-pstart+1,sizeof(char)); strncpy(copy,pstart,pend-pstart); switch(cmdind) { case cmdInclude: inputNewIncludeFile(inputPvt,copy); break; case cmdSubstitute: addMacroReplacements(macPvt,copy); break; default: fprintf(stderr,"Logic Error: makeSubstitutions\n"); inputErrPrint(inputPvt); exit(1); } free(copy); expand = 0; } endif: if (expand) { n = macExpandString(macPvt,input,buffer,MAX_BUFFER_SIZE-1); fputs(buffer,stdout); if (!unexpWarned && n<0) { fprintf(stderr,"Warning: unexpanded macros in ouput\n"); unexpWarned++; } } } }
/** 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; }