2021-04-13 19:10:02 +00:00
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
* md2teach
|
|
|
|
*
|
|
|
|
* Created by Jeremy Rand on 2021-04-13.
|
2021-04-14 04:01:05 +00:00
|
|
|
* Copyright (c) 2021 Jeremy Rand. All rights reserved.
|
2021-04-13 19:10:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2021-04-15 03:12:04 +00:00
|
|
|
#include <stdlib.h>
|
2021-04-14 03:40:58 +00:00
|
|
|
#include <string.h>
|
2021-04-13 19:10:02 +00:00
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
#include "io.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "translate.h"
|
2021-04-24 20:25:32 +00:00
|
|
|
|
2021-04-13 19:10:02 +00:00
|
|
|
|
2021-04-14 03:40:58 +00:00
|
|
|
// GS_TODO - How big does the stack need to be? In looking over the code,
|
|
|
|
// I don't see massive stack frames due to large globals (other than the
|
|
|
|
// context which I made static). But I do see lots of arguments and if
|
|
|
|
// the call stack gets deep enough, we could have a problem.
|
|
|
|
//
|
|
|
|
// Testing looks pretty good though with a trivial input. The stack seems
|
2021-04-16 04:26:25 +00:00
|
|
|
// to reach just more than 512 bytes deep when the hook functions are called.
|
|
|
|
// I suspect things can get much worse with a complex document but this
|
2021-04-14 03:40:58 +00:00
|
|
|
// approach should let me measure the worst case stack with a complex
|
|
|
|
// document.
|
2021-04-14 04:01:05 +00:00
|
|
|
//
|
2021-05-05 04:23:28 +00:00
|
|
|
// Leaving the stack very big for now at 8K.
|
|
|
|
#pragma stacksize 8192
|
2021-04-14 03:40:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
|
2021-04-15 04:20:19 +00:00
|
|
|
char * commandName;
|
2021-04-19 04:05:45 +00:00
|
|
|
int debugEnabled = 0;
|
2021-04-29 23:33:15 +00:00
|
|
|
int debugIndentLevel = 0;
|
2021-05-06 01:58:48 +00:00
|
|
|
int generateRez = 0;
|
2021-04-15 04:20:19 +00:00
|
|
|
|
2021-04-23 04:20:27 +00:00
|
|
|
|
2021-04-14 03:40:58 +00:00
|
|
|
// Implementation
|
|
|
|
|
2021-04-19 04:05:45 +00:00
|
|
|
static void printUsage(void)
|
|
|
|
{
|
2021-05-06 01:58:48 +00:00
|
|
|
fprintf(stderr, "USAGE: %s [ -d -r ] inputfile outputfile\n", commandName);
|
2021-04-19 04:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int parseArgs(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
static int index;
|
|
|
|
static int charOffset;
|
|
|
|
static int optionLen;
|
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
commandName = argv[0];
|
|
|
|
|
2021-04-19 04:05:45 +00:00
|
|
|
for (index = 1; index < argc; index++) {
|
|
|
|
if (argv[index][0] != '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
optionLen = strlen(argv[index]);
|
|
|
|
for (charOffset = 1; charOffset < optionLen; charOffset++) {
|
|
|
|
switch (argv[index][charOffset]) {
|
|
|
|
case 'd':
|
|
|
|
debugEnabled = 1;
|
|
|
|
break;
|
2021-05-06 01:58:48 +00:00
|
|
|
case 'r':
|
|
|
|
generateRez = 1;
|
|
|
|
break;
|
2021-04-19 04:05:45 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
printUsage();
|
2021-04-24 20:15:43 +00:00
|
|
|
return -1;
|
2021-04-19 04:05:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index + 2 != argc) {
|
|
|
|
printUsage();
|
2021-04-24 20:15:43 +00:00
|
|
|
return -1;
|
2021-04-19 04:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2021-04-14 03:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
2021-04-13 19:10:02 +00:00
|
|
|
{
|
2021-04-15 04:20:19 +00:00
|
|
|
int result;
|
2021-04-24 20:15:43 +00:00
|
|
|
MD_SIZE inputFileLen;
|
|
|
|
MD_CHAR * inputBuffer;
|
|
|
|
int index;
|
2021-04-19 04:05:45 +00:00
|
|
|
|
|
|
|
index = parseArgs(argc, argv);
|
2021-04-24 20:15:43 +00:00
|
|
|
if (index < 0)
|
2021-04-15 03:12:04 +00:00
|
|
|
exit(1);
|
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
inputBuffer = readInputFile(argv[index], &inputFileLen);
|
|
|
|
if (inputBuffer == NULL)
|
2021-04-15 03:12:04 +00:00
|
|
|
exit(1);
|
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
if (openOutputFile(argv[index + 1]) != 0) {
|
|
|
|
releaseInputBuffer(inputBuffer);
|
2021-04-15 03:12:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
result = parse(inputBuffer, inputFileLen);
|
2021-04-29 04:24:27 +00:00
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
releaseInputBuffer(inputBuffer);
|
2021-04-23 00:21:14 +00:00
|
|
|
|
2021-04-19 04:05:45 +00:00
|
|
|
if (debugEnabled) {
|
|
|
|
fprintf(stderr, "Parser result: %d\n", result);
|
|
|
|
}
|
2021-04-14 03:40:58 +00:00
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
if (result != 0)
|
|
|
|
fprintf(stderr, "%s: Parser failed (%d)\n", commandName, result);
|
|
|
|
|
2021-04-29 04:24:27 +00:00
|
|
|
if (closeOutputFile() != 0)
|
|
|
|
result = 1;
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
remove(argv[index + 1]);
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
|
2021-05-05 04:23:28 +00:00
|
|
|
styleShutdown();
|
|
|
|
|
2021-04-24 20:15:43 +00:00
|
|
|
return result;
|
2021-04-13 19:10:02 +00:00
|
|
|
}
|