picoc/picoc.c

88 lines
2.7 KiB
C
Raw Normal View History

/* picoc main program - this varies depending on your operating system and
* how you're using picoc */
/* platform-dependent code for running programs is in this file */
#if defined(UNIX_HOST) || defined(WIN32)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif
2015-06-06 23:37:28 -04:00
2015-06-16 23:53:32 -04:00
/* include only picoc.h here - should be able to use it with only the
external interfaces, no internals from interpreter.h */
#include "picoc.h"
#include "stats.h"
#if defined(UNIX_HOST) || defined(WIN32)
2015-06-16 23:53:32 -04:00
#include "LICENSE.h"
/* Override via STACKSIZE environment variable */
#define PICOC_STACK_SIZE (128000*4)
int main(int argc, char **argv)
{
int ParamCount = 1;
int DontRunMain = false;
int CollectStats = false;
int StatsType = 0;
int StackSize = getenv("STACKSIZE") ? atoi(getenv("STACKSIZE")) : PICOC_STACK_SIZE;
Picoc pc;
2015-06-06 23:37:28 -04:00
2015-06-16 23:49:54 -04:00
if (argc < 2 || strcmp(argv[ParamCount], "-h") == 0) {
2015-06-06 23:37:28 -04:00
printf(PICOC_VERSION " \n"
2015-06-16 23:53:32 -04:00
"Format:\n\n"
"> picoc <file1.c>... [- <arg1>...] : run a program, calls main() as the entry point\n"
"> picoc -s <file1.c>... [- <arg1>...] : run a script, runs the program without calling main()\n"
"> picoc -d[type] <file1.c>... [- <arg1>...] : run a program, outputting debugging stats\n"
"> picoc -i : interactive mode, Ctrl+d to exit\n"
"> picoc -c : copyright info\n"
"> picoc -h : this help message\n");
2015-06-16 23:49:54 -04:00
return 0;
}
2015-06-06 23:37:28 -04:00
if (strcmp(argv[ParamCount], "-c") == 0) {
2015-06-17 05:30:27 -04:00
printf("%s\n", (char*)&__LICENSE);
return 0;
}
2015-06-18 23:49:47 -04:00
PicocInitialize(&pc, StackSize);
2015-06-06 23:37:28 -04:00
2015-06-11 16:07:33 -04:00
if (strcmp(argv[ParamCount], "-s") == 0) {
DontRunMain = true;
PicocIncludeAllSystemHeaders(&pc);
ParamCount++;
} else if (strncmp(argv[ParamCount], "-d", 2) == 0) {
if (strlen(argv[ParamCount]) > 2) {
StatsType = atoi(&argv[ParamCount][2]);
}
CollectStats = true;
pc.CollectStats = true;
ParamCount++;
}
2015-06-06 23:37:28 -04:00
2015-06-07 00:40:08 -04:00
if (argc > ParamCount && strcmp(argv[ParamCount], "-i") == 0) {
PicocIncludeAllSystemHeaders(&pc);
PicocParseInteractive(&pc);
2015-06-07 00:40:08 -04:00
} else {
if (PicocPlatformSetExitPoint(&pc)) {
PicocCleanup(&pc);
return pc.PicocExitValue;
}
2015-06-06 23:37:28 -04:00
for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++)
PicocPlatformScanFile(&pc, argv[ParamCount]);
2015-06-06 23:37:28 -04:00
if (!DontRunMain)
PicocCallMain(&pc, argc - ParamCount, &argv[ParamCount]);
}
2015-06-06 23:37:28 -04:00
PicocCleanup(&pc);
if (CollectStats) {
stats_print_tokens(StatsType == 1);
}
return pc.PicocExitValue;
}
#endif