1 #include <../../nrnconf.h>
22 #include <fmt/format.h>
45 if (dup2(fileno(f1), 1) < 0) {
49 }
else if (
prev > -1) {
50 if (dup2(
prev, 1) < 0) {
123 for (cp1 =
s, cp2 = hs->
buf + begin; *cp1; ++cp1) {
124 if (*cp1 ==
'$' && cp1[1] ==
'(') {
128 for (cp3 =
buf; *cp1 && *cp1 !=
')'; ++cp1) {
134 if (strcmp(
buf,
"NEURONHOME") == 0) {
155 return hs->
buf + begin;
167 std::string fname{
name};
169 if (rcs[0] !=
'\0') {
172 std::string cmd{
"co -p"};
177 fname.append(1,
'-');
180 if (system(cmd.c_str()) != 0) {
193 while (errno == EINTR) {
195 constexpr
auto mode_str =
"r";
224 unlink(fname.c_str());
260 d = (int) strlen(
buf);
285 if (fscanf(fi,
"%255s", fs) == EOF) {
288 if (fs[0] ==
'i' || fs[0] ==
'n' || fs[0] ==
'I' || fs[0] ==
'N') {
291 if (sscanf(fs,
"%lf", &d) == 1) {
293 if (fscanf(fi,
"\n")) {
310 if (fs[0] ==
'i' || fs[0] ==
'n' || fs[0] ==
'I' || fs[0] ==
'N') {
313 if (sscanf(fs,
"%lf", &d) == 1) {
350 word = (int)
chkarg(2, 0., 1.);
370 char *pfmt, *pfrag, frag[120];
371 int convflag, lflag, didit;
378 convflag = lflag = didit = 0;
379 auto* pbuf = hs->
buf;
380 auto pbuf_size = hs->
size + 1;
385 auto const resize = [&pbuf, &pbuf_size](
HocStr* hs, std::size_t extra_size) {
386 auto const n = pbuf - hs->
buf;
387 auto const new_size =
n + extra_size;
390 pbuf_size = new_size + 1 -
n;
392 for (pfmt = fmt; *pfmt; pfmt++) {
407 pfrag[0] = pfrag[-1];
410 std::snprintf(pbuf, pbuf_size, frag, (
long long) *
getarg(argn));
412 std::snprintf(pbuf, pbuf_size, frag, (
int) *
getarg(argn));
418 std::snprintf(pbuf, pbuf_size, frag, (
char) *
getarg(argn));
425 std::snprintf(pbuf, pbuf_size, frag, *
getarg(argn));
435 resize(hs, std::strlen(cp) + 100);
436 std::snprintf(pbuf, pbuf_size, frag, cp);
442 std::strncpy(pbuf, frag, pbuf_size);
443 assert(pbuf[pbuf_size - 1] ==
'\0');
451 }
else if (*pfmt ==
'%') {
453 }
else if (pfrag - frag > 100) {
454 resize(hs, std::strlen(frag) + 100);
455 std::snprintf(pbuf, pbuf_size,
"%s", frag);
479 std::snprintf(pbuf, pbuf_size,
"%s", frag);
489 static constexpr
auto os_pathsep =
";";
491 static constexpr
auto os_pathsep =
":";
494 std::vector<std::string>
result;
495 std::size_t start = 0;
498 while ((end = input.find(os_pathsep, start)) != std::string::npos) {
500 result.push_back(input.substr(start, end - start));
506 if (start < input.size()) {
507 result.push_back(input.substr(start));
515 auto result = std::vector<std::string>({
"."});
518 auto hoc_library_path = getenv(
"HOC_LIBRARY_PATH");
519 if (hoc_library_path) {
520 auto paths =
split_paths(std::string(hoc_library_path));
521 std::copy(begin(paths), end(paths), back_inserter(
result));
525 auto home_path = fmt::format(
"{}/lib/hoc", std::string(
neuron_home));
526 result.push_back(home_path);
535 const std::vector<std::string>& paths) {
536 namespace fs = std::filesystem;
537 for (
const auto& path: paths) {
541 std::vector<std::string> paths_oc;
542 std::vector<std::string> paths_hoc;
543 for (
const auto& entry: fs::directory_iterator(path)) {
544 if (entry.is_regular_file() && entry.path().extension() ==
".oc") {
545 paths_oc.push_back(entry.path().string());
546 }
else if (entry.is_regular_file() && entry.path().extension() ==
".hoc") {
547 paths_hoc.push_back(entry.path().string());
550 std::sort(paths_oc.begin(), paths_oc.end(), std::locale());
551 std::sort(paths_hoc.begin(), paths_hoc.end(), std::locale());
553 std::vector<std::string>
result;
554 std::merge(paths_oc.begin(),
560 for (
const auto& entry:
result) {
561 auto file = std::ifstream(entry);
566 while (std::getline(file, line)) {
567 if (std::regex_search(line, pattern)) {
585 if (!sym || sym->
type == UNDEF) {
586 auto pattern = std::regex(fmt::format(
"^{} {}", stype,
s));
589 fprintf(stderr,
"Getting %s from %s\n",
s, file->c_str());
592 fprintf(stderr,
"Couldn't find a file that declares %s\n",
s);
618 i = (int)
chkarg(iarg, 0., 1.);
640 static std::vector<std::string> loaded;
654 for (
const std::string&
q: loaded) {
669 if ((base = strrchr(
name,
'/')) !=
NULL) {
671 path[base -
name] =
'\0';
673 f = fopen(
name,
"r");
678 f = fopen(base,
"r");
681 hlp = getenv(
"HOC_LIBRARY_PATH");
682 while (hlp && *hlp) {
683 char* cp = strchr(hlp,
':');
685 cp = strchr(hlp,
' ');
688 cp = hlp + strlen(hlp);
691 strncpy(path, hlp, cp - hlp);
692 path[cp - hlp] =
'\0';
711 Sprintf(path,
"$(NEURONHOME)/lib/hoc");
721 loaded.push_back(
name);
731 goback = (getcwd(old, 1000) != 0);
734 hoc_warning(
"Couldn't change directory to:", path);
746 "hoc_ac_ = execute1(\"{xopen(\\\"%s\\\")}\")\n",
755 if (path[0] && goback) {
757 hoc_warning(
"Couldn't change directory back to:", old);
773 hoc_execerror(
"getcwd failed. Perhaps the path length is > hoc_load_file_size_", (
char*) 0);
779 if (
buf[len - 1] !=
'/') {
793 gethostname(
buf, 20);
834 size = vsnprintf(
NULL, 0, fmt, ap);
838 constexpr
size_t maxsize = 512;
841 size = vsnprintf(
s, maxsize, fmt, ap);
double chkarg(int, double low, double high)
static auto default_search_paths()
void hoc_load_template(void)
static std::optional< std::string > search_hoc_files_regex(const std::regex &pattern, const std::vector< std::string > &paths)
static void hoc_load(const char *stype)
static int(* nrnpy_pass_callback)()
double hoc_fw_scan(NrnFILEWrap *fi)
int(* nrnpy_pr_stdoe_callback)(int, char *)
int nrn_is_python_extension
static int hoc_Load_file(int, const char *)
size_t hoc_xopen_file_size_
double hoc_scan(FILE *fi)
void hoc_machine_name(void)
char * hoc_back2forward(char *)
static auto split_paths(const std::string &input)
void nrnpy_set_pr_etal(int(*cbpr_stdoe)(int, char *), int(*cbpass)())
char * hoc_current_xopen(void)
static constexpr auto hoc_load_file_size_
int hoc_xopen1(const char *name, const char *rcs)
int hoc_is_object_arg(int narg)
void hoc_plprint(const char *)
void hoc_xopen_from_audit(const char *fname)
int hoc_retrieving_audit(void)
void hoc_pushstr(char **d)
void hoc_execerr_ext(const char *fmt,...)
printf style specification of hoc_execerror message.
void hoc_audit_from_xopen1(const char *fname, const char *rcs)
void hoc_assign_str(char **cpp, const char *buf)
const char * expand_env_var(const char *s)
int hoc_oc(const char *buf)
int hoc_is_double_arg(int narg)
int hoc_xopen_run(Symbol *sp, const char *str)
int hoc_chdir(const char *path)
char * hoc_object_name(Object *ob)
void hoc_sprint1(char **ppbuf, int argn)
Symbol * hoc_lookup(const char *)
char ** hoc_pgargstr(int narg)
char * fgets_unlimited(HocStr *s, NrnFILEWrap *f)
HocStr * hocstr_create(size_t size)
void hocstr_resize(HocStr *hs, size_t n)
Object ** hoc_objgetarg(int)
void hoc_execerror(const char *s1, const char *s2)
void * erealloc(void *ptr, size_t size)
static void * emalloc(size_t size)
void hoc_warning(const char *s1, const char *s2)
int Sprintf(char(&buf)[N], const char *fmt, Args &&... args)
Redirect sprintf to snprintf if the buffer size can be deduced.
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
#define nrn_fw_eq(fw, ff)
#define nrn_fw_set_stdin()
int const size_t const size_t n
static double resize(void *v)