-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquish_run.c
123 lines (99 loc) · 2.15 KB
/
squish_run.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "squish_run.h"
#include "squish_tokenize.h"
#include "w_run.h"
int numPrompts = 0;
/**
* Print a prompt if the input is coming from a TTY
*/
static void
prompt(FILE *pfp, FILE *ifp)
{
if (isatty(fileno(ifp))) {
fputs(PROMPT_STRING, pfp);
}
}
int DLOBALi = 0;
/**
* Actually do the work
*/
int
execFullCommandLine(
FILE *ofp,
char ** const tokens,
int nTokens,
int verbosity)
{
if (verbosity > 0) {
fprintf(stderr, " + ");
fprintfTokens(stderr, tokens, 1);
}
/** Now actually do something with this command, or command set */
run(ofp, tokens, nTokens, verbosity);
// puts("+--------------+");
// puts("| |");
// puts("| |");
// printf("| %d |\n", DLOBALi);
// puts("| |");
// puts("| |");
// puts("+--------------+");
// DLOBALi++;
// for (int i = 0; i < DLOBALi %50; i++) {
// printf("⬛");
// }
return 0;
}
/**
* Load each line and perform the work for it
*/
int
runScript(
FILE *ofp, FILE *pfp, FILE *ifp,
const char *filename, int verbosity
)
{
char linebuf[LINEBUFFERSIZE];
char *tokens[MAXTOKENS];
int lineNo = 1;
int nTokens, executeStatus = 0;
fprintf(stderr, "SHELL PID %ld\n", (long) getpid());
prompt(pfp, ifp);
while ((nTokens = parseLine(ifp,
tokens, MAXTOKENS,
linebuf, LINEBUFFERSIZE, verbosity - 3)) > 0) {
lineNo++;
if (nTokens > 0) {
executeStatus = execFullCommandLine(ofp, tokens, nTokens, verbosity);
if (executeStatus < 0) {
fprintf(stderr, "Failure executing '%s' line %d:\n ",
filename, lineNo);
fprintfTokens(stderr, tokens, 1);
return executeStatus;
}
}
prompt(pfp, ifp);
}
return (0);
}
/**
* Open a file and run it as a script
*/
int
runScriptFile(FILE *ofp, FILE *pfp, const char *filename, int verbosity)
{
FILE *ifp;
int status;
ifp = fopen(filename, "r");
if (ifp == NULL) {
fprintf(stderr, "Cannot open input script '%s' : %s\n",
filename, strerror(errno));
return -1;
}
status = runScript(ofp, pfp, ifp, filename, verbosity);
fclose(ifp);
return status;
}