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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "clog.h"
// Error handlings sometimes uses the goto statement.
#define _strings_match(s1, s2) (strcmp((s1), (s2)) == 0)
#define ABORT(...) do { \
exit_code = EXIT_FAILURE; \
fprintf(stderr, "%s", this_executable); \
fprintf(stderr, __VA_ARGS__); \
goto end; \
} while(false)
int main(int argc, const char* argv[]) {
// Assume that the command is always called as:
// gcc -o «executable» «▒▒▒.c»
// This won't be perfect at first. We will walk through some pitfalls.
//
// THIS IS BUGGY (SO FAR)
const char* this_executable = argv[0];
int exit_code = EXIT_SUCCESS; // default is success!
if(argc != 4) { // because our specification is very narrow for this
ABORT("Need exactly 3 arguments\n");
goto end;
}
else {
const char* flag = argv[1];
const char* executable_path = argv[2];
const char* c_path = argv[3];
if(!_strings_match(flag, "-o")) {
ABORT("First argument must be \"-o\"\n");
goto end;
}
if(strlen(executable_path) == 0) {
ABORT("Second argument must not be blank.\n");
goto end;
}
// Make sure the .c file ends with .c
if(c_path[strlen(c_path) -2] != '.' || c_path[strlen(c_path) - 1] != 'c') {
ABORT("Third argument must end with .c\n");
goto end;
}
printf("# TODO: Compile %s to create executable %s.\n", argv[3], argv[2]);
}
end:
printf("Doing cleanup.\n");
printf("MY_GCC is exiting. Cleanup has been completed.\n");
return exit_code;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2020 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.