-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handle.c
138 lines (121 loc) · 4.5 KB
/
error_handle.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @author Harsh Rawat, harsh-rawat, hrawat2
* @author Sidharth Gurbani, gurbani, gurbani
*/
#include "error_handle.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* This method is called when we want to raise error for waitpid method failure
* */
void WaitPIDError(int line_index, char *line_str, int errorNo) {
fprintf(stderr, ERROR_TEMPLATE, line_index, strerror(errorNo), line_str);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for buffer overflow
* */
void BufferOverflowError(int index, char *line) {
fprintf(stderr, ERROR_TEMPLATE, index, "line size exceeds max buffer size", line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for receiving null byte in the string while parsing
* */
void NullByteInLineError(int index, char *line) {
fprintf(stderr, ERROR_TEMPLATE, index, "null byte found while parsing the line", line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error after detecting cycle in the execution graph
* */
void CycleInGraphError(int index, char *line) {
fprintf(stderr, ERROR_TEMPLATE, index, "cycle detected in makefile", line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error due to failure while parsing the target string.
* Such errors include - First character is not tab or character, etc
* */
void TargetParsingError(int index, char *line, char *error_msg) {
fprintf(stderr, ERROR_TEMPLATE, index, error_msg, line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for a command without target in makefile
* */
void InvalidCommandInMakefileError(int index, char *line) {
fprintf(stderr, ERROR_TEMPLATE, index, "command is not part of any target", line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for a duplicate target in the makefile
* */
void DuplicateRuleError(int index, char *line) {
fprintf(stderr, ERROR_TEMPLATE, index, "Duplicate rule found", line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for a failed execution of command
* */
void CommandExecutionFailedError(int index, char *line, int errNo) {
fprintf(stderr, "%d: command could not be executed. Error message is - %s: %s\n", index, strerror(errNo), line);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for a missing rule in the makefile
* */
void RuleNotFoundError(char *invalid_rule) {
fprintf(stderr, "%s: invalid target name\nExiting!\n", invalid_rule);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error to indicate invalid target dependency in the makefile.
* One such example is non-existent file in the dependency
* */
void InvalidTargetDependencyError(int line_index, char *line_str, char *dep_name) {
fprintf(stderr, "%d: Invalid target dependency of %s: %s\n", line_index, dep_name, line_str);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error since file could not be opened
* */
void FileOpenError(char *filename, int errorNo) {
fprintf(stderr, "%s: File could not be opened. Error message is - %s\n", filename, strerror(errorNo));
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error since the file doesn't exist
* */
void FileNotFoundError(char *filename, int line_index, char *line_str) {
if (line_str == NULL)
fprintf(stderr, "File not found error occured for %s\n", filename);
else
fprintf(stderr, "%d: File not found error occurred for %s: %s\n", line_index, filename, line_str);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error since we could not open the file stats
* */
void StatError(int line_index, char *line_str, char *filename, int errorNo) {
fprintf(stderr, "%d: Error finding file stats of %s. Error Message is %s: %s\n", line_index, filename,
strerror(errorNo), line_str);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for invalid arguments passed to main method
* */
void InvalidArgumentsError(char *error_msg) {
fprintf(stderr, "%s\n", error_msg);
exit(EXIT_FAILURE);
}
/**
* This method is called when we want to raise error for failure during memory allocation
* */
void ValidateMemoryAllocationError(void *arg) {
if (arg == NULL) {
fprintf(stderr, "Memory could not be allocated using malloc/calloc. Exiting!\n");
exit(EXIT_FAILURE);
}
}