Initial commit
This commit is contained in:
parent
6fabc85fab
commit
13495d95e7
7 changed files with 233 additions and 0 deletions
82
src/main.c
Normal file
82
src/main.c
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* This code is part of the programming language flup
|
||||
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
|
||||
* Copyright (C) 2024 VegOwOtenks
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../include/utilitiec/argumentc/argumentc.h"
|
||||
|
||||
char* load_file_string(StringView path)
|
||||
{
|
||||
FILE* stream = fopen(path.source, "r");
|
||||
if (stream == NULL) {
|
||||
fprintf(stderr, "Fatal Error: Failed to open file at %*s\n", (int) path.length, path.source);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fseek(stream, 0, SEEK_END)) {
|
||||
perror("fseek");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
long length = ftell(stream);
|
||||
if (length == -1) {
|
||||
perror("ftell");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rewind(stream);
|
||||
|
||||
char* buffer = malloc(length + 1);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Fatal Error: Failed to allocate %li bytes\n", length);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread(buffer, 1, length + 1, stream);
|
||||
if (bytes_read != length) {
|
||||
fprintf(stderr, "Fatal Error: Failed read %li bytes from script file, got only %li\n", length, bytes_read);
|
||||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv [])
|
||||
{
|
||||
Argumentc arguments;
|
||||
Argumentc_Create(&arguments, argc, argv);
|
||||
|
||||
Option script_file = Argumentc_PopLongArgument(&arguments, StringView_FromString("file")).argument;
|
||||
if (script_file.type == OPTIONTYPE_NONE) {
|
||||
fprintf(stderr, "Usage: [program] --file path/to/script_file\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* script_string = load_file_string(script_file.content);
|
||||
if (script_string == NULL) return 1;
|
||||
|
||||
puts(script_string);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue