-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_file.c
executable file
·90 lines (80 loc) · 2 KB
/
execute_file.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
#include "monty.h"
/**
* execute_file - handle command line from the file
* @inst_arr: array of instructions
*/
void execute_file(instruction_t *inst_arr)
{
unsigned int line_num = 0;
stack_t *head = NULL;
instruction_t *inst_help;
while ((fgets(monty.buf, MAX_LINE_LENGTH, monty.file)) != NULL)
{
monty.data = NULL;
monty.arg = NULL;
monty.found_arg = 0;
line_num++;
monty.arg = strtok(monty.buf, " \n\t\a\b");
if (!monty.arg && strchr(monty.buf, '\n') == NULL)
continue;
if (!monty.arg || *monty.arg == '#')
continue;
if (strcmp(monty.arg, "stack") == 0 || strcmp(monty.arg, "queue") == 0)
{
strcpy(monty.format_data, monty.arg);
continue;
}
inst_help = inst_arr;
check_opcode(inst_help, line_num, &head);
if (monty.found_arg == 0)
handle_err("unknown instruction", monty.arg, line_num, &head);
}
if (head)
free_list(&head);
}
/**
* check_is_digit - check if argument 2 include digit
* @token: string
* @line_num: unsigned int
* @head: the head of the linked list
*/
void check_is_digit(char *token, unsigned int line_num, stack_t **head)
{
int i;
if (!token)
handle_err("usage: push", "integer", line_num, head);
for (i = 0; token[i]; i++)
if (!isdigit(token[i]) && token[0] != '-')
handle_err("usage: push", "integer", line_num, head);
}
/**
* check_opcode - check opcode
* @inst_help: struct
* @line_num: unsigned int
* @head: the head of the linked list
*/
void check_opcode(instruction_t *inst_help,
unsigned int line_num, stack_t **head)
{
while ((inst_help->opcode))
{
if (strcmp(monty.arg, inst_help->opcode) == 0)
{
monty.found_arg = 1;
if (strcmp(monty.arg, "push") == 0)
{
monty.arg = strtok(NULL, " \n\t\a\b");
while ((!monty.arg && (strchr(monty.buf, '\n') == NULL)))
{
fgets(monty.buf, MAX_LINE_LENGTH, monty.file);
monty.arg = strtok(monty.buf, " \n\t\a\b");
}
check_is_digit(monty.arg, line_num, head);
monty.data = monty.arg;
}
inst_help->f(head, line_num);
break;
}
inst_help++;
}
}