From 019c7dd845565f9993c8bace2102e7ecf3a2f18c Mon Sep 17 00:00:00 2001 From: Marek Kraus Date: Wed, 24 Aug 2022 12:06:39 +0200 Subject: [PATCH] Initial commit --- .clang-format | 58 +++++++++++++++++ .gitignore | 77 +++++++++++++++++++++++ .gitmodules | 3 + .idea/.gitignore | 8 +++ .idea/blisp.iml | 2 + .idea/codeStyles/Project.xml | 7 +++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ CMakeLists.txt | 11 ++++ src/cmd.h | 16 +++++ src/cmd/write.c | 47 ++++++++++++++ src/main.c | 94 ++++++++++++++++++++++++++++ vendor/argtable3 | 1 + 15 files changed, 347 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .idea/.gitignore create mode 100644 .idea/blisp.iml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 src/cmd.h create mode 100644 src/cmd/write.c create mode 100644 src/main.c create mode 160000 vendor/argtable3 diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..740bda5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,58 @@ +--- +Language: Cpp +BasedOnStyle: GNU +IndentWidth: 4 +AllowShortBlocksOnASingleLine: Empty +AllowShortFunctionsOnASingleLine: None +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: false + AfterCaseLabel: false + AfterEnum: false + AfterControlStatement: Never + AfterStruct : false + AfterUnion : false + AfterExternBlock : false + BeforeElse : false + BeforeWhile : false +AlignAfterOpenBracket: Align +AlignArrayOfStructures: Left +AlignEscapedNewlines: Right +AlignOperands: Align +AllowShortEnumsOnASingleLine: False +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine : false +AlwaysBreakAfterReturnType: AllDefinitions +AlignTrailingComments : true +AlignConsecutiveMacros: AcrossEmptyLines +KeepEmptyLinesAtTheStartOfBlocks : false +PointerAlignment : Left +QualifierAlignment : Left +ReferenceAlignment : Left +RemoveBracesLLVM : false +SpaceAfterCStyleCast : false +SpaceAfterLogicalNot : false +SpaceAfterTemplateKeyword : false +SpaceAroundPointerQualifiers : Before +SpaceBeforeAssignmentOperators : true +SpaceBeforeCaseColon : false +SpaceBeforeCpp11BracedList : false +SpaceBeforeParens : ControlStatements +SpaceBeforeParensOptions : + AfterControlStatements : true + AfterFunctionDeclarationName : false + AfterFunctionDefinitionName : false + AfterOverloadedOperator : false +SpaceBeforeRangeBasedForLoopColon : false +SpaceBeforeSquareBrackets : false +SpaceInEmptyBlock : false +SpaceInEmptyParentheses : false +SpacesInCStyleCastParentheses : false +SpacesInConditionalStatement : false +SpacesInContainerLiterals : false +SpacesInParentheses: false +SpacesInSquareBrackets : false +UseTab : Never + +BitFieldColonSpacing: None +BreakBeforeBinaryOperators: All \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d16bbbe --- /dev/null +++ b/.gitignore @@ -0,0 +1,77 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..7b360dc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/argtable3"] + path = vendor/argtable3 + url = https://github.com/argtable/argtable3 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/blisp.iml b/.idea/blisp.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/blisp.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..f603881 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c08044f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3d28e2e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.22) +project(blisp C) + +set(CMAKE_C_STANDARD 23) + +add_subdirectory(vendor/argtable3) + +add_executable(blisp src/main.c src/cmd/write.c) + +target_include_directories(blisp PRIVATE vendor/argtable3/src) +target_link_libraries(blisp PRIVATE argtable3) \ No newline at end of file diff --git a/src/cmd.h b/src/cmd.h new file mode 100644 index 0000000..9c971d8 --- /dev/null +++ b/src/cmd.h @@ -0,0 +1,16 @@ +#ifndef BLISP_CMD_H +#define BLISP_CMD_H + +#include + +struct cmd { + const char* name; + int8_t (*args_init)(); + uint8_t (*args_parse_exec)(int argc, char** argv); + void (*args_print_syntax)(); + void (*args_free)(); +}; + +extern struct cmd cmd_write; + +#endif // BLISP_CMD_H diff --git a/src/cmd/write.c b/src/cmd/write.c new file mode 100644 index 0000000..3c93385 --- /dev/null +++ b/src/cmd/write.c @@ -0,0 +1,47 @@ +#include "../cmd.h" +#include + +#define REG_EXTENDED 1 +#define REG_ICASE (REG_EXTENDED << 1) + +static struct arg_rex* cmd; +static struct arg_file* binary_to_write; +static struct arg_end* end; +static void* cmd_write_argtable[3]; + +int8_t +cmd_write_args_init() { + cmd_write_argtable[0] = cmd + = arg_rex1(NULL, NULL, "write", NULL, REG_ICASE, NULL); + cmd_write_argtable[1] = binary_to_write + = arg_file0(NULL, NULL, "", "Binary to write"); + cmd_write_argtable[2] = end = arg_end(10); + + if (arg_nullcheck(cmd_write_argtable) != 0) { + fprintf(stderr, "insufficient memory\n"); + return -1; + } +} + +uint8_t +cmd_write_parse_exec(int argc, char** argv) { + int errors = arg_parse(argc, argv, cmd_write_argtable); + if (errors == 0) { + printf("yeet\n"); + return 1; + } + return 0; +} + +void cmd_write_args_print_syntax() { + arg_print_syntax(stdout,cmd_write_argtable,"\n"); +} + +void +cmd_write_free() { + arg_freetable(cmd_write_argtable, + sizeof(cmd_write_argtable) / sizeof(cmd_write_argtable[0])); +} + +struct cmd cmd_write + = { "write", cmd_write_args_init, cmd_write_parse_exec, cmd_write_args_print_syntax, cmd_write_free }; \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..f521df1 --- /dev/null +++ b/src/main.c @@ -0,0 +1,94 @@ +#include "cmd.h" +#include +#include +#include +#include + +struct cmd* cmds[] = { + &cmd_write, +}; + +static struct arg_lit* help; +static struct arg_lit* version; +static struct arg_end* end; +static void* argtable[3]; + +int8_t args_init() { + argtable[0] = help = arg_lit0(NULL, "help", "print this help and exit"); + argtable[1] = version = arg_lit0(NULL, "version", "print version information and exit"); + argtable[2] = end = arg_end(20); + + if (arg_nullcheck(argtable) != 0) { + fprintf(stderr, "insufficient memory\n"); + return -1; + } +} + +void print_help() { + puts("Usage:"); + for (uint8_t i = 0; cmds[i] != NULL; i++) { + fputs("\tblisp", stdout); + cmds[i]->args_print_syntax(); + } + fputs("\tblisp", stdout); + arg_print_syntax(stdout, argtable,"\n"); +} + +int8_t args_parse_exec(int argc, char** argv) { + int error = arg_parse(argc, argv, argtable); + if (error == 0) { + if (help->count) { + print_help(); + return 1; + } else if (version->count) { + printf("blisp 1.0.0\n"); + printf("Copyright (C) 2022 Marek Kraus and PINE64 Community\n"); + return 1; + } + } + return 0; +} + +void args_free() { + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +int +main(int argc, char** argv) { + int exit_code = 0; + + if (args_init() != 0) { + exit_code = -1; + goto exit; + } + + for (uint8_t i = 0; cmds[i] != NULL; i++) { + if (cmds[i]->args_init() != 0) { + exit_code = -1; + goto exit; + } + } + + if (args_parse_exec(argc, argv)) { + goto exit; + } + + uint8_t command_found = false; + for (uint8_t i = 0; cmds[i] != NULL; i++) { + if (cmds[i]->args_parse_exec(argc, argv)) { + command_found = true; + break; + } + } + + if (!command_found) { + + } + +exit: + for (uint8_t i = 0; cmds[i] != NULL; i++) { + cmds[i]->args_free(); + } + args_free(); + return exit_code; +} diff --git a/vendor/argtable3 b/vendor/argtable3 new file mode 160000 index 0000000..6f0e40b --- /dev/null +++ b/vendor/argtable3 @@ -0,0 +1 @@ +Subproject commit 6f0e40bc44c99af353ced367c6fafca8705f5fca