SCP: add sim_get_tool_path to get the expanded path to a local command

This logic was previously within the static SCP function _get_tool_version
and is now exposed for use elsewhere.
This commit is contained in:
Mark Pizzolato 2021-08-28 04:27:41 -07:00
parent c808256e87
commit 6d04cf814a
2 changed files with 26 additions and 17 deletions

42
scp.c
View file

@ -6361,12 +6361,10 @@ void fprint_capac (FILE *st, DEVICE *dptr, UNIT *uptr)
fprintf (st, "%s", sprint_capac (dptr, uptr));
}
static const char *_get_tool_version (const char *tool)
const char *sim_get_tool_path (const char *tool)
{
char findcmd[PATH_MAX+1];
char toolpath[PATH_MAX+1];
char versioncmd[PATH_MAX+1];
static char toolversion[PATH_MAX+1];
static char toolpath[PATH_MAX+1];
FILE *f;
#if defined(_WIN32)
@ -6376,27 +6374,37 @@ FILE *f;
#else
#define FIND_CMD "which"
#endif
toolversion[0] = '\0';
memset (toolpath, 0, sizeof(toolpath));
snprintf (findcmd, sizeof (findcmd), "%s %s", FIND_CMD, tool);
if ((f = popen (findcmd, "r"))) {
memset (toolpath, 0, sizeof(toolpath));
do {
if (NULL == fgets (toolpath, sizeof(toolpath)-1, f))
break;
sim_trim_endspc (toolpath);
} while (toolpath[0] == '\0');
pclose (f);
if (toolpath[0]) {
snprintf (versioncmd, sizeof (versioncmd), "%s --version", tool);
if ((f = popen (versioncmd, "r"))) {
memset (toolversion, 0, sizeof(toolversion));
do {
if (NULL == fgets (toolversion, sizeof(toolversion)-1, f))
break;
sim_trim_endspc (toolversion);
} while (toolversion[0] == '\0');
pclose (f);
}
}
return toolpath;
}
static const char *_get_tool_version (const char *tool)
{
const char *toolpath;
char versioncmd[PATH_MAX+1];
static char toolversion[PATH_MAX+1];
FILE *f;
memset (toolversion, 0, sizeof(toolversion));
toolpath = sim_get_tool_path (tool);
if (toolpath[0]) {
snprintf (versioncmd, sizeof (versioncmd), "%s --version", tool);
if ((f = popen (versioncmd, "r"))) {
do {
if (NULL == fgets (toolversion, sizeof(toolversion)-1, f))
break;
sim_trim_endspc (toolversion);
} while (toolversion[0] == '\0');
pclose (f);
}
}
return toolversion;

1
scp.h
View file

@ -247,6 +247,7 @@ const char *sim_error_text (t_stat stat);
t_stat sim_string_to_stat (const char *cptr, t_stat *cond);
t_stat sim_sched_step (void);
t_stat sim_cancel_step (void);
const char *sim_get_tool_path (const char *tool);
void sim_printf (const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void sim_perror (const char *msg);
t_stat sim_messagef (t_stat stat, const char *fmt, ...) GCC_FMT_ATTR(2, 3);