24 #include "epicsStdlib.h"
25 #include "epicsString.h"
26 #include "epicsStdio.h"
28 #include "epicsMutex.h"
30 #include "dbStaticLib.h"
31 #include "dbFldTypes.h"
33 #include "dbAccessDefs.h"
34 #include <epicsTypes.h>
35 #include <epicsTime.h>
36 #include <epicsThread.h>
37 #include <epicsString.h>
38 #include <epicsTimer.h>
39 #include <epicsMutex.h>
46 #include <registryFunction.h>
48 #include <epicsExport.h>
54 char* val = getenv(var);
67 for(
char** cp = environ; *cp != NULL; ++cp)
69 char* str_tmp = strdup(*cp);
70 char* equals_loc = strchr(str_tmp,
'=');
71 if (equals_loc != NULL)
74 macPutValue(pmh, str_tmp, equals_loc + 1);
81 static void subMacros(std::string& new_macros,
const char* macros,
const char* loopVar)
83 char loopSubFrom[32], loopSubTo[32];
84 epicsSnprintf(loopSubFrom,
sizeof(loopSubFrom),
"\\$(%s)", loopVar);
85 epicsSnprintf(loopSubTo,
sizeof(loopSubTo),
"$(%s)", loopVar);
87 size_t start_pos = 0, lf = strlen(loopSubFrom), lt = strlen(loopSubTo);
88 while( (start_pos = new_macros.find(loopSubFrom, start_pos)) != std::string::npos )
90 new_macros.replace(start_pos, lf, loopSubTo);
93 epicsSnprintf(loopSubFrom,
sizeof(loopSubFrom),
"\\${%s}", loopVar);
94 epicsSnprintf(loopSubTo,
sizeof(loopSubTo),
"${%s}", loopVar);
96 while( (start_pos = new_macros.find(loopSubFrom, start_pos)) != std::string::npos )
98 new_macros.replace(start_pos, lf, loopSubTo);
123 epicsShareFunc
void iocshCmdLoop(
const char* command,
const char* macros,
const char* loopVar,
int start,
int stop,
int step)
135 std::string macros_s, command_s;
138 MAC_HANDLE* mh = NULL;
140 char macros_exp[1024], command_exp[1024];
141 for(
int i = start; i <= stop; i += step)
143 std::map<std::string,char*> old_values;
144 macCreateHandle(&mh, NULL);
146 epicsSnprintf(loopVal,
sizeof(loopVal),
"%d", i);
147 macPutValue(mh, loopVar, loopVal);
148 macExpandString(mh, command_s.c_str(), command_exp,
sizeof(command_exp));
149 macExpandString(mh, macros_s.c_str(), macros_exp,
sizeof(macros_exp));
151 macParseDefns(mh, macros_exp, &pairs);
153 for(
int j=0; pairs[j] != NULL; j += 2)
156 epicsEnvSet(pairs[j], pairs[j+1]);
160 epicsEnvSet(loopVar, loopVal);
161 std::ostringstream new_macros;
162 new_macros << macros_exp << (strlen(macros_exp) > 0 ?
"," :
"") << loopVar <<
"=" << loopVal;
163 std::cout <<
"--> (" << i <<
") iocshCmd(\"" << command_exp <<
"\") with: " << new_macros.str() << std::endl;
164 iocshCmd(command_exp);
166 for(std::map<std::string,char*>::iterator it = old_values.begin(); it != old_values.end(); ++it)
168 epicsEnvSet(it->first.c_str(), it->second);
194 epicsShareFunc
void iocshCmdList(
const char* command,
const char* macros,
const char* loopVar,
const char* list,
const char* sep)
196 static const char* default_sep =
";";
197 if (loopVar == NULL || list == NULL)
206 std::string macros_s, command_s;
209 char* saveptr = NULL;
210 char* list_tmp = strdup(list);
211 char* list_item = epicsStrtok_r(list_tmp, sep, &saveptr);
212 MAC_HANDLE* mh = NULL;
214 char macros_exp[1024], command_exp[1024];
215 while(list_item != NULL)
217 std::map<std::string,char*> old_values;
218 macCreateHandle(&mh, NULL);
220 macPutValue(mh, loopVar, list_item);
221 macExpandString(mh, command_s.c_str(), command_exp,
sizeof(command_exp));
222 macExpandString(mh, macros_s.c_str(), macros_exp,
sizeof(macros_exp));
224 macParseDefns(mh, macros_exp, &pairs);
225 for(
int j=0; pairs[j] != NULL; j += 2)
228 epicsEnvSet(pairs[j], pairs[j+1]);
232 epicsEnvSet(loopVar, list_item);
233 std::ostringstream new_macros;
234 new_macros << macros_exp << (strlen(macros_exp) > 0 ?
"," :
"") << loopVar <<
"=" << list_item;
235 std::cout <<
"--> (" << list_item <<
") iocshCmd(\"" << command_exp <<
"\") with: " << new_macros.str() << std::endl;
236 iocshCmd(command_exp);
237 for(std::map<std::string,char*>::iterator it = old_values.begin(); it != old_values.end(); ++it)
239 epicsEnvSet(it->first.c_str(), it->second);
243 list_item = epicsStrtok_r(NULL, sep, &saveptr);
275 iocshCmdLoop(args[0].sval, args[1].sval, args[2].sval, args[3].ival, args[4].ival, args[5].ival);
280 iocshCmdList(args[0].sval, args[1].sval, args[2].sval, args[3].sval, args[4].sval);
static const iocshFuncDef iocshCmdLoopDef
static void iocshCmdLoopInitCallFunc(const iocshArgBuf *args)
static const iocshArg iocshCmdLoopInitArg0
command string
static const iocshArg iocshCmdLoopInitArg1
macros to pass to command
static const iocshArg iocshCmdLoopInitArg2
loop macro variable name
static const iocshArg *const iocshCmdLoopInitArgs[]
static void iocshCmdFuncsRegister(void)
static void iocshCmdListInitCallFunc(const iocshArgBuf *args)
static const iocshArg iocshCmdListInitArg2
list macro variable name
static const iocshArg *const iocshCmdListInitArgs[]
static char * ioc_getenv(const char *var)
static const iocshArg iocshCmdLoopInitArg4
Loop end value.
static void loadMacEnviron(MAC_HANDLE *pmh)
static const iocshArg iocshCmdListInitArg4
list value separator character
epicsShareFunc void iocshCmdList(const char *command, const char *macros, const char *loopVar, const char *list, const char *sep)
Execute a command multiple times according to a list of items separated by known separator(s).
static const iocshArg iocshCmdLoopInitArg3
Loop start value.
static const iocshArg iocshCmdListInitArg0
command string
static const iocshArg iocshCmdLoopInitArg5
Loop step (default: 1)
static const iocshArg iocshCmdListInitArg1
macros to pass to command
static const iocshArg iocshCmdListInitArg3
list of values to substitute
static const iocshFuncDef iocshCmdListDef
epicsExportRegistrar(dbLoadRecordsFuncsRegister)
static void subMacros(std::string &new_macros, const char *macros, const char *loopVar)
look for e.g. $(I) and replace with so we can substitute later with macEnvExpand() ...
epicsShareFunc void iocshCmdLoop(const char *command, const char *macros, const char *loopVar, int start, int stop, int step)
Execute command multiple times substituting a specified macro according to a number range...