-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_error.c
123 lines (116 loc) · 2.81 KB
/
handle_error.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "monty.h"
/**
* handle_error - Manages the printing of interpreter errors
* @errno: The error code to manage
* @opcode: The operation code to manage
* @line: The line on which the error occurred
* @buff: The reserved error line buffer
*
* Return: Nothing
*/
void handle_error(int errno, char *opcode, unsigned int line, char *buff)
{
if (errno >= 100 && errno < 200)
handle_cerror(errno, opcode, line);
else if (errno >= 200 && errno <= 210)
handle_uerror(errno, line);
else if (errno >= 211 && errno <= 220)
handle_more_uerror(errno, line);
else
return;
frees_stack();
if (buff)
free(buff);
exit(EXIT_FAILURE);
}
/**
* handle_cerror - Manages common interpreter errors
* @errno: The error code to manage
* @opcode: The operation code to manage
* @line: The line on which the error occurred
*
* Return: Nothing
*/
void handle_cerror(int errno, char *opcode, unsigned int line)
{
switch (errno)
{
case ERR_BAD_INST:
fprintf(stderr, "L%d: unknown instruction %s\n", line, opcode);
break;
case ERR_BAD_MALL:
fprintf(stderr, "Error: malloc failed\n");
break;
default:
break;
}
}
/**
* handle_uerror - Manages interpreter usage errors
* @errno: The error code to manage
* @line: The line on which the error occurred
*
* Return: Nothing
*/
void handle_uerror(int errno, unsigned int line)
{
switch (errno)
{
case ERR_ARG_USG:
fprintf(stderr, "USAGE: monty file\n");
break;
case ERR_PUSH_USG:
fprintf(stderr, "L%d: usage: push integer\n", line);
break;
case ERR_PINT_USG:
fprintf(stderr, "L%d: can't pint, stack empty\n", line);
break;
case ERR_POP_USG:
fprintf(stderr, "L%d: can't pop an empty stack\n", line);
break;
case ERR_SWAP_USG:
fprintf(stderr, "L%d: can't swap, stack too short\n", line);
break;
case ERR_ADD_USG:
fprintf(stderr, "L%d: can't add, stack too short\n", line);
break;
case ERR_SUB_USG:
fprintf(stderr, "L%d: can't sub, stack too short\n", line);
break;
case ERR_DIV_USG:
fprintf(stderr, "L%d: can't div, stack too short\n", line);
break;
case ERR_DIV_ZRO:
fprintf(stderr, "L%d: division by zero\n", line);
break;
case ERR_MUL_USG:
fprintf(stderr, "L%d: can't mul, stack too short\n", line);
break;
case ERR_MOD_USG:
fprintf(stderr, "L%d: can't mod, stack too short\n", line);
break;
default:
break;
}
}
/**
* handle_more_uerror - Manages interpreter usage errors
* @errno: The error code to manage
* @line: The line on which the error occurred
*
* Return: Nothing
*/
void handle_more_uerror(int errno, unsigned int line)
{
switch (errno)
{
case ERR_PCH_USG:
fprintf(stderr, "L%d: can't pchar, value out of range\n", line);
break;
case ERR_PCH_EMP:
fprintf(stderr, "L%d: can't pchar, stack empty\n", line);
break;
default:
break;
}
}