Erythro/src/Delegate.c

123 lines
2.9 KiB
C
Raw Normal View History

/*************/
/*GEMWIRE */
/* ERYTHRO*/
/*************/
#include <Defs.h>
#include <Data.h>
char* Suffixate(char* String, char Suffix) {
char* Pos, *NewStr;
if((NewStr = strdup(String)) == NULL)
return NULL;
if((Pos = strrchr(NewStr, '.')) == NULL)
return NULL;
Pos++;
if(*Pos == '\0')
return NULL;
*Pos++ = Suffix;
*Pos = '\0';
return NewStr;
}
char* Compile(char* InputFile) {
char* OutputName;
OutputName = Suffixate(InputFile, 's');
if(OutputName == NULL) {
fprintf(stderr, "%s must have a suffix.\r\n", InputFile);
exit(1);
}
if((SourceFile = fopen(InputFile, "r")) == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", InputFile, strerror(errno));
exit(1);
}
if((OutputFile = fopen(OutputName, "w")) == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", OutputName, strerror(errno));
exit(1);
}
Line = 1;
Overread = '\n';
CurrentGlobal = 0;
CurrentLocal = SYMBOLS - 1;
if(OptVerboseOutput)
printf("Compiling %s\r\n", InputFile);
Tokenise(&CurrentToken);
AssemblerPreamble();
ParseGlobals();
fclose(OutputFile);
return OutputName;
}
char* Assemble(char* InputFile) {
char Command[TEXTLEN];
int Error;
char* OutputName;
OutputName = Suffixate(InputFile, 'o');
if(OutputName == NULL) {
fprintf(stderr, "%s must have a suffix.\r\n", InputFile);
exit(1);
}
snprintf(Command, TEXTLEN, "%s %s %s", "as -o ", OutputName, InputFile);
if(OptVerboseOutput)
printf("%s\n", Command);
Error = system(Command);
if(Error != 0) {
fprintf(stderr, "Assembling of %s failed with code %d\n", InputFile, Error);
exit(1);
}
return OutputName;
}
void Link(char* Output, char* Objects[]) {
int Count, Size = TEXTLEN, Error;
char Command[TEXTLEN], *CommandPtr;
CommandPtr = Command;
Count = snprintf(CommandPtr, Size, "%s %s ", "gcc -o ", OutputFileName);
CommandPtr += Count;
Size -= Count;
while(*Objects != NULL) {
Count = snprintf(CommandPtr, Size, "%s ", *Objects);
CommandPtr += Count;
Size -= Count;
Objects++;
}
if(OptVerboseOutput)
printf("%s\n", Command);
Error = system(Command);
if(Error != 0) {
fprintf(stderr, "Link failure\n");
exit(1);
}
}
void DisplayUsage(char* ProgName) {
fprintf(stderr, "Usage: %s -[vcST] {-o output} file [file ...]\n", ProgName);
fprintf(stderr, " -v: Verbose Output Level\n");
fprintf(stderr, " -c: Compile without Linking\n");
fprintf(stderr, " -S: Assemble without Linking\n");
fprintf(stderr, " -T: Dump AST\n");
fprintf(stderr, " -o: Name of the destination [executable/object/assembly] file.\n");
exit(1);
}