77 lines
2.1 KiB
C
77 lines
2.1 KiB
C
#include "link.h"
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static inline Args parse_args(int argc, char** argv);
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
Args args = parse_args(argc, argv);
|
|
FileIter input_files = { 1, argv, args.input_files };
|
|
|
|
switch (args.profile) {
|
|
case Profile_Linked:
|
|
case Profile_Shared:
|
|
fprintf(stderr,
|
|
"profile '%s' not implemented\n",
|
|
profile_strs[args.profile]);
|
|
break;
|
|
case Profile_Kern:
|
|
return link_kern(&args, input_files);
|
|
}
|
|
}
|
|
|
|
static inline Args parse_args(int argc, char** argv)
|
|
{
|
|
const char* output_file = NULL;
|
|
int input_files = 0;
|
|
Profile profile = Profile_Linked;
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (strcmp(argv[i], "-o") == 0) {
|
|
i += 1;
|
|
if (i >= argc) {
|
|
fprintf(stderr, "no filename given to -o\n");
|
|
exit(1);
|
|
}
|
|
output_file = argv[i];
|
|
} else if (strcmp(argv[i], "-p") == 0) {
|
|
i += 1;
|
|
if (i >= argc) {
|
|
fprintf(stderr, "no profile given to -p\n");
|
|
exit(1);
|
|
}
|
|
|
|
size_t strs_size = sizeof(profile_strs) / sizeof(profile_strs[0]);
|
|
bool found = false;
|
|
for (size_t si = 0; si < strs_size; ++si) {
|
|
if (strcmp(profile_strs[si], argv[i]) == 0) {
|
|
profile = (Profile)si;
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!found) {
|
|
fprintf(stderr, "invalid profile '%s'\n", argv[i]);
|
|
fprintf(stderr, "valid profiles: linked, shared, kern\n");
|
|
exit(1);
|
|
}
|
|
} else {
|
|
input_files += 1;
|
|
}
|
|
}
|
|
if (input_files == 0) {
|
|
fprintf(stderr, "no input files specified\n");
|
|
exit(1);
|
|
}
|
|
if (output_file == NULL) {
|
|
output_file = "a.out";
|
|
}
|
|
return (Args) {
|
|
output_file,
|
|
input_files,
|
|
profile,
|
|
};
|
|
}
|