ISIS Logo
UTILITIES
EPICS Utilities
iocname.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <math.h>
6 #include <exception>
7 #include <algorithm>
8 #include <stdexcept>
9 #include <iostream>
10 #include <map>
11 #include <list>
12 #include <string>
13 #include <time.h>
14 #include <sstream>
15 #include <fstream>
16 
17 #include "epicsStdlib.h"
18 #include "epicsString.h"
19 #include "dbDefs.h"
20 #include "epicsMutex.h"
21 #include "dbBase.h"
22 #include "dbStaticLib.h"
23 #include "dbFldTypes.h"
24 #include "dbCommon.h"
25 #include "dbAccessDefs.h"
26 #include <epicsTypes.h>
27 #include <epicsTime.h>
28 #include <epicsThread.h>
29 #include <epicsString.h>
30 #include <epicsTimer.h>
31 #include <epicsMutex.h>
32 #include <iocsh.h>
33 #include "envDefs.h"
34 #include "macLib.h"
35 #include "errlog.h"
36 
37 #include <string.h>
38 #include <registryFunction.h>
39 
40 #include <epicsExport.h>
41 
42 #include "utilities.h"
43 
44 epicsShareFunc std::string epicsShareAPI setIOCName(const char* iocName)
45 {
46  const char *iocBootDir = NULL, *old_iocName = macEnvExpand("$(IOCNAME=)");
47  if (iocName != NULL)
48  {
49  iocBootDir = iocName;
50  }
51  else if (old_iocName && *old_iocName)
52  {
53  iocBootDir = old_iocName;
54  }
55  else
56  {
57  iocBootDir = macEnvExpand("$(IOC)");
58  }
59  if (iocBootDir == NULL || *iocBootDir == '\0')
60  {
61  return "";
62  }
63  // The IOC name may be a boot directory, in which case strip of the initial ioc prefix
64  std::string iocBootDir_s = iocBootDir;
65  std::transform(iocBootDir_s.begin(), iocBootDir_s.end(), iocBootDir_s.begin(), ::toupper); // uppercase
66  std::replace(iocBootDir_s.begin(), iocBootDir_s.end(), '-', '_'); // hyphen to underscore
67  std::string ioc_name, ioc_group;
68  if (!iocBootDir_s.compare(0, 3, "IOC")) // are we a boot area (iocYYYY) rather than an ioc name
69  {
70  ioc_name = iocBootDir_s.substr(3);
71  }
72  else
73  {
74  ioc_name = iocBootDir_s;
75  }
76  // strip off -IOC-01 trailer to get IOC group, and remove -IOC from -IOC- to get ioc name (we want YY-IOC-01 to beceom YY_01
77  size_t pos = ioc_name.find("_IOC_");
78  if (pos != std::string::npos)
79  {
80  ioc_group = ioc_name.substr(0, pos);
81  ioc_name.erase(pos, strlen("_IOC")); // not _IOC_ as we want to leave an _ before the number
82  }
83  else if ( (pos = ioc_name.rfind("_")) != std::string::npos ) // old style nameing e.g. DEVICE32_01
84  {
85  if (atoi(ioc_name.substr(pos).c_str()) > 0)
86  {
87  ioc_group = ioc_name.substr(0, pos);
88  }
89  else
90  {
91  ioc_group = ioc_name;
92  }
93  }
94  else
95  {
96  ioc_group = ioc_name;
97  }
98  epicsEnvSet("IOCNAME", ioc_name.c_str());
99  epicsEnvSet("IOCGROUP", ioc_group.c_str());
100  return ioc_name;
101 }
102 
103 epicsShareFunc std::string epicsShareAPI getIOCName()
104 {
105  const char* iocName = macEnvExpand("$(IOCNAME=)");
106  if (iocName == NULL || *iocName == '\0')
107  {
108  setIOCName(NULL);
109  iocName = macEnvExpand("$(IOCNAME)");
110  }
111  return (iocName != NULL ? iocName : "");
112 }
113 
114 epicsShareFunc std::string epicsShareAPI getIOCGroup()
115 {
116  const char* iocGroup = macEnvExpand("$(IOCGROUP=)");
117  if (iocGroup == NULL || *iocGroup == '\0')
118  {
119  setIOCName(NULL);
120  iocGroup = macEnvExpand("$(IOCGROUP)");
121  }
122  return (iocGroup != NULL ? iocGroup : "");
123 }
124 
125 extern "C" {
126 
127 // EPICS iocsh shell commands
128 
129 static const iocshArg setInitArg0 = { "iocName", iocshArgString };
130 static const iocshArg * const setInitArgs[] = { &setInitArg0 };
131 
132 static const iocshFuncDef setInitFuncDef = {"setIOCName", sizeof(setInitArgs) / sizeof(iocshArg*), setInitArgs};
133 static const iocshFuncDef getInitFuncDef = {"getIOCName", 0, NULL};
134 static const iocshFuncDef groupInitFuncDef = {"getIOCGroup", 0, NULL};
135 
136 static void setInitCallFunc(const iocshArgBuf *args)
137 {
138  setIOCName(args[0].sval);
139 }
140 
141 static void getInitCallFunc(const iocshArgBuf *args)
142 {
143  std::string iocName = getIOCName();
144  std::cout << iocName;
145 }
146 
147 static void groupInitCallFunc(const iocshArgBuf *args)
148 {
149  std::string iocGroup = getIOCGroup();
150  std::cout << iocGroup;
151 }
152 
153 static void iocnameRegister(void)
154 {
155  iocshRegister(&setInitFuncDef, setInitCallFunc);
156  iocshRegister(&getInitFuncDef, getInitCallFunc);
157  iocshRegister(&groupInitFuncDef, groupInitCallFunc);
158 }
159 
160 epicsExportRegistrar(iocnameRegister); // need to be declared via registrar() in utilities.dbd too
161 
162 // asub callable functions - need to be in utilities.dbd as function()
163 
164 //epicsRegisterFunction(setIOCName);
165 //epicsRegisterFunction(getIOCName);
166 //epicsRegisterFunction(getIOCGroup);
167 
168 }
169 
static void groupInitCallFunc(const iocshArgBuf *args)
Definition: iocname.cpp:147
epicsShareFunc std::string epicsShareAPI getIOCName()
Definition: iocname.cpp:103
static void setInitCallFunc(const iocshArgBuf *args)
Definition: iocname.cpp:136
static void iocnameRegister(void)
Definition: iocname.cpp:153
static const iocshFuncDef getInitFuncDef
Definition: iocname.cpp:133
epicsShareFunc std::string epicsShareAPI setIOCName(const char *iocName)
Definition: iocname.cpp:44
static const iocshFuncDef groupInitFuncDef
Definition: iocname.cpp:134
static const iocshArg setInitArg0
The name of the ioc.
Definition: iocname.cpp:129
static const iocshArg *const setInitArgs[]
Definition: iocname.cpp:130
static const iocshFuncDef setInitFuncDef
Definition: iocname.cpp:132
epicsShareFunc std::string epicsShareAPI getIOCGroup()
Definition: iocname.cpp:114
static void getInitCallFunc(const iocshArgBuf *args)
Definition: iocname.cpp:141
epicsExportRegistrar(dbLoadRecordsFuncsRegister)
Copyright © 2013 Science and Technology Facilities Council | Generated by   doxygen 1.8.5