-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
88 lines (73 loc) · 1.88 KB
/
main.c
File metadata and controls
88 lines (73 loc) · 1.88 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
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
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <stdio.h>
#include "command_utility.h"
#include "command_stream.h"
#include "concurrent_commands.h"
static char const *program_name;
static char const *script_name;
static void
usage (void)
{
error (1, 0, "usage: %s [-ptC] SCRIPT-FILE", program_name);
}
static int
get_next_byte (void *stream)
{
return getc (stream);
}
int
main (int argc, char **argv)
{
int opt;
int command_number = 1;
int print_tree = 0;
int time_travel = 0;
int no_clobber = 0;
program_name = argv[0];
for (;;)
switch (getopt (argc, argv, "ptC"))
{
case 'p': print_tree = 1; break;
case 't': time_travel = 1; break;
case 'C': no_clobber = 1; break;
default: usage (); break;
case -1: goto options_exhausted;
}
options_exhausted:;
// There must be exactly one file argument.
if (optind != argc - 1)
usage ();
script_name = argv[optind];
FILE *script_stream = fopen (script_name, "r");
if (! script_stream)
error (1, errno, "%s: cannot open", script_name);
command_stream_t command_stream = create_command_stream (get_next_byte, script_stream);
command_t last_command = NULL;
command_t command;
int status;
if (time_travel)
status = parallelize_command_stream (command_stream, no_clobber);
else
{
while ((command = current_command (command_stream)))
{
if (print_tree)
{
printf ("# %d\n", command_number++);
print_command (command);
}
else
{
last_command = command;
execute_command (command, no_clobber);
}
next_command (command_stream);
}
status = print_tree || !last_command ? 0 : command_status (last_command);
}
fclose (script_stream);
destroy_command_stream (command_stream);
return status;
}