-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty.c
82 lines (75 loc) · 1.82 KB
/
monty.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
#include "monty.h"
/* contains information about the current opcode being executed. */
opcode_i opcode_info;
/**
* main - entry of the monty interpreter 0.98.
* @ac: number of command line arguments passed.
* @av: the actual arguments list(array).
*
* Return: EXIT_SUCCESS (success), EXIT_FAILURE (error).
*/
int main(int ac, char **av)
{
char *line = NULL, *opcode = NULL;
unsigned int line_number = 1;
size_t len;
FILE *_file = NULL;
stack_t *stack = NULL;
op_stack(NULL, 0); /* setting default mode to stack */
check_main_args(ac, av);
opcode_info.file = _file = open_file(av[1], "rb");
opcode_info.stack = &stack;
for (line_number = 1; ; line_number++)
{
opcode_info.line_number = line_number;
if ((getline(&line, &len, _file)) == -1)
{
if (line != NULL)
free(line);
break;
}
opcode_info.opcode = opcode = handle_opcode_line(line);
if (opcode == NULL)
{
line = NULL;
continue;
}
execute_opcode(&stack, opcode, line_number);
line = NULL;
}
free_exit(EXIT_SUCCESS);
exit(EXIT_SUCCESS);
}
/**
* check_main_args - checks if valid argument number and valid argument
* is passed to main.
* @ac: number arguments.
* @av: the actual arguments.
*
* Return: if returns back to main (valid), else exit.
*/
void check_main_args(int ac, char **av)
{
FILE *_file;
if (ac == 2)
_file = open_file(av[1], "rb");
else
exit_error_msg(_MONTY_USAGE_ERR, NULL);
fclose(_file);
}
/**
* open_file - open a file and return FILE * to the file.
* @path: The path to the file.
* @flags: The flages to open the file with.
*
* Return: If it returns a FILE * to the file (success),
* else it will exit the program.
*/
FILE *open_file(char *path, char *flags)
{
FILE *_file = NULL;
_file = fopen(path, flags);
if (_file == NULL)
exit_error_msg(_MONTY_UNABLE_TO_READ_FILE, path);
return (_file);
}