-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.c
91 lines (83 loc) · 2.54 KB
/
eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eval.c
*
* evaluate (i.e. execute) the output of a command as a new command
*
* Copyright (C) 2011 Dirk Zimoch
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "iocsh.h"
#include "epicsStdioRedirect.h"
#include "epicsString.h"
#include "epicsExport.h"
int evalDebug = 0;
epicsExportAddress(int, evalDebug);
static void evalFunc (const iocshArgBuf *args)
{
#ifdef __linux__
char commandline[1024];
char* p = commandline;
unsigned int i;
FILE* orig_stdout;
FILE* bufferfile;
char* buffer;
size_t buffersize;
size_t len;
char *arg;
for (i = 0; i < args[1].aval.ac; i++) {
arg = args[1].aval.av[i];
len = strlen(arg);
if (p - commandline + len + 4 >= sizeof(commandline))
{
fprintf(stderr, "command line too long\n");
return;
}
p += sprintf(p, " \"%s\"", arg);
}
*p = 0;
bufferfile = open_memstream(&buffer, &buffersize);
orig_stdout = epicsGetThreadStdout();
if (evalDebug) fprintf(stderr, "iocshCmd:(%s)\n", commandline);
epicsSetThreadStdout(bufferfile);
iocshCmd(commandline);
epicsSetThreadStdout(orig_stdout);
fclose(bufferfile);
while (isspace(buffer[buffersize-1])) buffer[--buffersize] = 0;
if (evalDebug) {
fprintf(stderr, "result: %s\n", buffer);
}
iocshCmd(buffer);
free(buffer);
#else
fprintf(stderr, "not implemented\n");
#endif
}
static const iocshArg evalArg0 = { "command", iocshArgString };
static const iocshArg evalArg1 = { "arguments", iocshArgArgv };
static const iocshArg * const evalArgs[] = { &evalArg0, &evalArg1 };
static const iocshFuncDef evalDef = { "eval", 2, evalArgs };
static void evalRegister(void)
{
if (evalDebug)
fprintf(stderr, "registering 'eval' command\n");
iocshRegister (&evalDef, evalFunc);
}
epicsExportRegistrar(evalRegister);