-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcshell.c
More file actions
53 lines (47 loc) · 1.61 KB
/
cshell.c
File metadata and controls
53 lines (47 loc) · 1.61 KB
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "execute.h"
#include "lexer.h"
#define MAX_LINE 1024
/* wanted to add cwd to the prompt, but doing so will
bork expectations for test output. for next time:
maybe ansi brightness. */
int main(void) {
if( isatty(STDIN_FILENO) ) {
/* this is the code used for an interactive shell; it prints a prompt, just
executes the commands on each line */
char * buffer;
while((buffer = readline ("cshell% "))) {
if(buffer[0] == 0) { continue; }
add_history(buffer);
yy_scan_string(buffer); /* set up bison to read from memory */
/* yyparse will invoke execute() on the parsed tree. */
/* execute should block until we're ready for the next prompt */
if (yyparse() != 0) {
fprintf(stderr, "Problem with command: '%s'\n", buffer);
continue; /* try again if parse error */
}
/* readline allocates the buffer, which must be freed. */
free(buffer);
}
} else {
/* this is the code used when testing; prints no prompt, just
executes the commands on each line */
char buffer[MAX_LINE];
while( fgets(buffer, MAX_LINE, stdin) != NULL) { /* exit on eof */
yy_scan_string(buffer); /* set up bison to read from memory */
if (yyparse() != 0) {
fprintf(stderr, "Problem with command: '%s'\n", buffer);
continue; /* try again if parse error */
}
}
}
return(0);
}
/* required by bison */
void yyerror (const char *s) {
fprintf (stderr, "Parse error: %s\n", s);
}