Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions pdbApp/softMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "iocInit.h"
#include "iocsh.h"
#include "osiFileName.h"
#include "dbStaticPvt.h"

#include <pv/qsrv.h>

Expand Down Expand Up @@ -66,6 +67,8 @@ void usage(const char *arg0, const std::string& base_dbd) {
"\n"
" -D <dbd> If used, must come first. Specify the path to the softIocPVA.dbdfile."
" The compile-time install location is saved in the binary as a default.\n"
" If the compiled-in path cannot be found, the application will look relative\n"
" to EPICS_BASE."
"\n"
" -h Print this mesage and exit.\n"
"\n"
Expand Down Expand Up @@ -110,18 +113,29 @@ void errIf(int ret, const std::string& msg)

bool lazy_dbd_loaded;

void lazy_dbd(const std::string& dbd_file) {
void lazy_dbd(const std::string& dbd_file, const std::string& prefix) {
if(lazy_dbd_loaded) return;
lazy_dbd_loaded = true;

if (verbose)
std::cout<<"dbLoadDatabase(\""<<dbd_file<<"\")\n";
errIf(dbLoadDatabase(dbd_file.c_str(), NULL, NULL),
std::string("Failed to load DBD file: ")+dbd_file);

std::string loaded_file;
FILE *fp = NULL;
std::string dbd_relative_to_epics_base(DBD_FILE);
loaded_file = dbd_file;
dbOpenFile(&pdbbase,dbd_file.c_str(),&fp);
if(verbose)
std::cout<<"dbOpenFile(&pdbbase"<<dbd_file<<",&fp)"<<std::endl;
if((!fp) && dbd_file==prefix+DBD_FILE_REL){
loaded_file=dbd_relative_to_epics_base;
if(verbose)
std::cout<<"dbOpenFile(&pdbbase,"<<dbd_relative_to_epics_base<<",&fp)"<<std::endl;
dbOpenFile(&pdbbase,dbd_relative_to_epics_base.c_str(),&fp);
errIf(!fp, "failed to find softIocPVA.dbd");
}
if(fp && dbReadDatabaseFP(&pdbbase, fp, NULL, NULL))
std::cerr<<"Failed to read "<<loaded_file<<", but it was found and loaded."<<std::endl;
if (verbose)
std::cout<<"softIocPVA_registerRecordDeviceDriver(pdbbase)\n";
softIocPVA_registerRecordDeviceDriver(pdbbase);
errIf(softIocPVA_registerRecordDeviceDriver(pdbbase),
"Failed to initialize database");
registryFunctionAdd("exit", (REGISTRYFUNCTION) exitSubroutine);
}

Expand All @@ -137,26 +151,22 @@ int main(int argc, char *argv[])
bool interactive = true;
bool loadedDb = false;
bool ranScript = false;

#ifdef USE_EXECDIR
// attempt to compute relative paths
{
std::string prefix;
char *cprefix = epicsGetExecDir();
if(cprefix) {
try {
prefix = cprefix;
free(cprefix);
} catch(...) {
free(cprefix);
throw;
}

std::string prefix;
char *cprefix = epicsGetExecDir();
if(cprefix) {
try {
prefix = cprefix;
free(cprefix);
} catch(...) {
free(cprefix);
throw;
}

dbd_file = prefix + DBD_FILE_REL;
exit_file = prefix + EXIT_FILE_REL;
}
#endif

dbd_file = prefix + DBD_FILE_REL;
exit_file = prefix + EXIT_FILE_REL;

int opt;

Expand All @@ -172,7 +182,7 @@ int main(int argc, char *argv[])
epicsExit(2);
return 2;
case 'a':
lazy_dbd(dbd_file);
lazy_dbd(dbd_file, prefix);
if (!macros.empty()) {
if (verbose)
std::cout<<"asSetSubstitutions(\""<<macros<<"\")\n";
Expand All @@ -191,7 +201,7 @@ int main(int argc, char *argv[])
dbd_file = optarg;
break;
case 'd':
lazy_dbd(dbd_file);
lazy_dbd(dbd_file,prefix);
if (verbose) {
std::cout<<"dbLoadRecords(\""<<optarg<<"\"";
if(!macros.empty())
Expand All @@ -214,7 +224,7 @@ int main(int argc, char *argv[])
verbose = true;
break;
case 'x':
lazy_dbd(dbd_file);
lazy_dbd(dbd_file,prefix);
xmacro = "IOC=";
xmacro += optarg;
errIf(dbLoadRecords(exit_file.c_str(), xmacro.c_str()),
Expand All @@ -227,7 +237,7 @@ int main(int argc, char *argv[])
}
}

lazy_dbd(dbd_file);
lazy_dbd(dbd_file,prefix);

if(optind<argc) {
// run script
Expand Down Expand Up @@ -261,8 +271,10 @@ int main(int argc, char *argv[])

} else {
if (loadedDb || ranScript) {
epicsThreadExitMain();

// non-interactive IOC. spin forever
while(true) {
epicsThreadSleep(1000.0);
}
} else {
usage(argv[0], dbd_file);
std::cerr<<"Nothing to do!\n";
Expand Down