-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.c
43 lines (34 loc) · 1.08 KB
/
batch.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
// batch.c
#include "batch.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_LENGTH 1024
int batch_mode(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
perror("Error opening file");
return -1;
}
char input_line[MAX_INPUT_LENGTH];
char tokens[MAX_NB_TOKENS][MAX_TOKEN_LEN];
CommandSequence command_sequence;
while (fgets(input_line, MAX_INPUT_LENGTH, file) != NULL) {
input_line[strcspn(input_line, "\n")] = 0; // Remove newline
int nb_tokens = tokenize(input_line, tokens); // Tokenize the input
if (nb_tokens == -1) {
fprintf(stderr, "Error: Failed to tokenize input\n");
continue;
}
if (parse(tokens, nb_tokens, &command_sequence) == -1) {
fprintf(stderr, "Error: Failed to parse command\n");
continue;
}
if (launch_command(&command_sequence) == -1) {
fprintf(stderr, "Error: Failed to execute command\n");
}
}
fclose(file);
return 0;
}