-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduef_args.c
More file actions
213 lines (200 loc) · 5.69 KB
/
duef_args.c
File metadata and controls
213 lines (200 loc) · 5.69 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "duef_args.h"
#include "duef_printing.h"
#include "duef_logger.h"
#include "duef.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Global variables for command line arguments
extern int g_is_verbose;
int g_print_mode_file = false;
int g_static_mode = false;
char *file_path = NULL;
void print_usage(const char *program_name)
{
printf("duef - Unreal Engine Crash File Decompressor\n\n");
printf("Usage: %s [OPTIONS] [file]\n\n", program_name);
printf("Options:\n");
printf(" -h, --help Show this help message and exit\n");
printf(" -v, --verbose Enable verbose output to stderr\n");
printf(" -f, --file FILE Specify .uecrash file to process\n");
printf(" -i Print individual file paths instead of directory path\n");
printf(" -s, --static Extract to a fixed 'static' directory instead of a crash-specific one\n");
printf(" --clean Remove all extracted files from ~/.duef directory\n\n");
printf("Examples:\n");
printf(" %s CrashReport.uecrash # Decompress crash file\n", program_name);
printf(" %s -v -f crash.uecrash # Decompress with verbose output\n", program_name);
printf(" %s -i crash.uecrash # Print individual file paths\n", program_name);
printf(" %s -s crash.uecrash # Extract to static directory\n", program_name);
printf(" %s --clean # Clean up extracted files\n\n", program_name);
printf("Output:\n");
printf(" On Unix: Files extracted to ~/.duef/<directory>/\n");
printf(" On Windows: Files extracted to %%LocalAppData%%\\duef\\<directory>\\\n");
printf(" Default file: CrashFile.uecrash (if no file specified)\n");
}
void process_file_option(int *i, int argc, char **argv)
{
if (*i + 1 < argc)
{
file_path = strdup(argv[++(*i)]);
print_verbose("File path set to: %s\n", file_path);
if (!file_path)
{
log_error("Memory allocation failed for file path\n");
exit(EXIT_FAILURE);
}
}
else
{
log_error("Option -f requires an argument\n\n");
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
void handle_single_short_option(char option, int *i, int argc, char **argv, bool *exit_j_loop)
{
switch (option)
{
case 'v':
g_is_verbose = true;
print_verbose("Verbose mode enabled.\n");
break;
case 'f':
process_file_option(i, argc, argv);
*exit_j_loop = true;
break;
case 'i':
g_print_mode_file = true;
print_verbose("Print mode file enabled.\n");
break;
case 's':
g_static_mode = true;
print_verbose("Static output directory enabled.\n");
break;
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
break;
default:
log_error("Unknown option: -%c\n\n", option);
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
void handle_short_options(char *arg, int *i, int argc, char **argv)
{
bool exit_j_loop = false;
for (int j = 1; arg[j] != '\0'; j++)
{
if (exit_j_loop) {
break;
}
handle_single_short_option(arg[j], i, argc, argv, &exit_j_loop);
}
}
void handle_verbose_option(void)
{
g_is_verbose = true;
print_verbose("Verbose mode enabled.\n");
}
void handle_file_long_option(int *i, int argc, char **argv)
{
if (*i + 1 < argc)
{
file_path = strdup(argv[++(*i)]);
print_verbose("File path set to: %s\n", file_path);
if (!file_path)
{
log_error("Memory allocation failed for file path\n");
exit(EXIT_FAILURE);
}
}
else
{
log_error("Option --file requires an argument\n\n");
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
void handle_clean_option(void)
{
delete_crash_collection_directory();
print_verbose("Crash collection directory cleared.\n");
exit(EXIT_SUCCESS);
}
void handle_long_options(char *arg, int *i, int argc, char **argv)
{
if (strcmp(arg, "--verbose") == 0)
{
handle_verbose_option();
}
else if (strcmp(arg, "--file") == 0)
{
handle_file_long_option(i, argc, argv);
}
else if (strcmp(arg, "--help") == 0)
{
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
else if (strcmp(arg, "--clean") == 0)
{
handle_clean_option();
}
else if (strcmp(arg, "--static") == 0)
{
g_static_mode = true;
print_verbose("Static output directory enabled.\n");
}
else
{
log_error("Unknown option: %s\n\n", arg);
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
void handle_positional_argument(char *arg)
{
if (file_path == NULL)
{
file_path = strdup(arg);
if (!file_path)
{
log_error("Memory allocation failed for file path\n");
exit(EXIT_FAILURE);
}
print_verbose("File path set to: %s\n", file_path);
}
else
{
log_error("Multiple file arguments provided. Only one file can be processed at a time.\n\n");
print_usage("duef");
exit(EXIT_FAILURE);
}
}
void parse_arguments(int argc, char **argv)
{
for (int i = 1; i < argc; i++)
{
if (argv[i][0] == '-' && argv[i][1] != '-')
{
handle_short_options(argv[i], &i, argc, argv);
}
else if (argv[i][0] == '-' && argv[i][1] == '-')
{
handle_long_options(argv[i], &i, argc, argv);
}
else
{
handle_positional_argument(argv[i]);
}
}
}
void cleanup_arguments(void)
{
if (file_path)
{
free(file_path);
file_path = NULL;
}
}