-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.cpp
106 lines (97 loc) · 2.95 KB
/
flags.cpp
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
#include <stdio.h>
#define DEFAULT_INPUTFILE "prog.asm"
#define DEFAULT_OUTPUTFILE "prog.hex"
#define MAX_LEN_FILENAME 100
#define HELP_TEXT "\
\
\n----------\n\
This is the help menu\n\
In case you haven't invoked this menu, it means your syntax is incorrect\n\
This is an educational software with a free GNU licencing, feel free to use, but mention the authors\n\
Authors: Lucas Pereira do Amaral\
Gabriela Barbosa Guedes Pereira\n\
\n----------\n\
Avalilable flags:\n\
-h: shows this help menu\n\
-f [filename]: indicate the path to the file to be compiled\n\
-o [filename]: indicate the path to the output hex file\n\
In case you have not specified input or output files, the defaults will ve used\n\
Default input file: prog.asm\n\
Default output file: prog.hex\n\
"
void print_help()
{
printf(HELP_TEXT);
return;
}
bool verify_filename(char *supposed_path, char **file_path)
{ // check path for safety
char *path = (char*)malloc(MAX_LEN_FILENAME*sizeof(char));
for (int i=0; i<MAX_LEN_FILENAME; i++)
{
path[i] = supposed_path[i];
if (path[i]=='\0') break;
if (i==MAX_LEN_FILENAME-1) path[0]='\0';
}
if (path[0]=='\0')
{
printf("[SET_FILENAME] Filename path might be too long or invalid\n");
return false;
}
printf("[SET_FILENAME] Path set as: %s\n", path);
*file_path = path;
return true;
}
int initializeFiles(int argc, char *argv[], char **inputfile, char **outputfile)
{
char *tmp1 = (char*)DEFAULT_INPUTFILE;
char *tmp2 = (char*)DEFAULT_OUTPUTFILE;
*inputfile = tmp1;
*outputfile = tmp2;
int args=1;
while (args < argc)
{
if (argv[args][0]=='-')
{
switch (argv[args][1])
{
case 'h':
print_help(); // print_help();
break;
case 'f':
args++;
if (args<argc)
{
if (!verify_filename(argv[args], inputfile)) return 403;
printf("[MANAGESTART] InputFile updated to %s\n", *inputfile);
}
else
{
printf("[MANAGESTART]Your [filename] argument is missing\n");
return 404;
}
break;
case 'o':
args++;
if (args<argc)
{
if (!verify_filename(argv[args], outputfile)) return 403;
printf("[MANAGESTART] OutputFile updated to %s\n", *outputfile);
}
else
{
printf("[MANAGESTART]Your [filename] argument is missing\n");
return 404;
}
break;
default:
print_help();
break;
}
}
else print_help();
args++;
}
printf("[MANAGESTART] Sucessful exit\n\n");
return 0;
}