diff --git a/.htmc-version b/.htmc-version new file mode 100644 index 0000000..27b184b --- /dev/null +++ b/.htmc-version @@ -0,0 +1 @@ +htmc version 24.10.21 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..384b746 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Alessandro Salerno + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a35945e --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# `latest-linux-bundle` +Here you can find build artifacts from the latest build of the `main` branch! + +These artifacts are for Linux only and include: + +| File | Location | Type | Description | +| - | - | - | - | +| `htmc` | `bin/htmc` | ELF executable | htmc executable program | +| `libhtmc.a` | `bin/libhtmc.a` | Static library archive | htmc in library format | +| `htmc-cgi-ws` | `bin/htmc-cgi-ws` | ELF executable | CGI Web Server for htmc | +| `libhtmc.h` | `include/libhtmc/libhtmc.h` | C header | header for libhtmc | +| `.htmc-version` | `.htmc-version` | Plain text file | htmc version file containing the output of `./bin/htmc --version` | diff --git a/bin/htmc b/bin/htmc new file mode 100755 index 0000000..6e08e20 Binary files /dev/null and b/bin/htmc differ diff --git a/bin/htmc-cgi-ws b/bin/htmc-cgi-ws new file mode 100755 index 0000000..1b9cbaa Binary files /dev/null and b/bin/htmc-cgi-ws differ diff --git a/bin/libhtmc.a b/bin/libhtmc.a new file mode 100644 index 0000000..97a9b09 Binary files /dev/null and b/bin/libhtmc.a differ diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..6b071a6 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-Iinclude/ +-std=c2x +-DHTMC_CGI_INTF +-DEXT_HTMC_BUILD="\"PLACEHOLDER\"" diff --git a/examples/index.htmc b/examples/index.htmc new file mode 100644 index 0000000..e19cfa0 --- /dev/null +++ b/examples/index.htmc @@ -0,0 +1,21 @@ + + + htmc test page + + + +

htmc test page

+

Following are the first n positive integers

+ +

+ +

+ + diff --git a/include/cli.h b/include/cli.h new file mode 100644 index 0000000..2f2ff09 --- /dev/null +++ b/include/cli.h @@ -0,0 +1,65 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include + +typedef struct { + const char *input_file; + const char *output_path; + bool stop_splash; + bool log_level_set; +} cli_info_t; + +typedef int (*cli_fcn_t)(cli_info_t *info, const char *next); +typedef int (*cli_exec_t)(cli_info_t info); + +typedef struct { + const char *short_option; + const char *full_option; + cli_fcn_t handler; + bool has_argument; + cli_exec_t exec_handler; +} cli_opt_desc_t; + +// Support functions +void print_program_version(); +void print_program_info(); + +// Handler functions (cli_fcn_t) +int flag_no_splash(cli_info_t *info, const char *next); +int flag_output(cli_info_t *info, const char *next); +int flag_log_level(cli_info_t *info, const char *next); + +// Setup for executable functions +int setup_cli_version(cli_info_t *info, const char *next); + +// Executable functions (cli_exec_t) +int cli_help(cli_info_t info); +int cli_license(cli_info_t info); +int cli_version(cli_info_t info); +int cli_translate(cli_info_t info); +int cli_compile(cli_info_t info); +int cli_run(cli_info_t info); +int cli_load_shared(cli_info_t info); +int cli_run(cli_info_t info); diff --git a/include/compile.h b/include/compile.h new file mode 100644 index 0000000..c9cf85a --- /dev/null +++ b/include/compile.h @@ -0,0 +1,25 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +int compile_c_output(const char *src_path, const char *dst_path); diff --git a/include/emit.h b/include/emit.h new file mode 100644 index 0000000..959fc4c --- /dev/null +++ b/include/emit.h @@ -0,0 +1,35 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include + +void emit_str(FILE *dst_file, const char *str); +void emit_base(FILE *dst_file); +void emit_end(FILE *dst_file); +void emit_html_base(FILE *dst_file); +void emit_html_end(FILE *dst_file); +void emit_html_block(FILE *dst_file); +void emit_html_block_end(FILE *dst_file); +void emit_char(FILE *dst_file, char chr); +void emit_char_escaped(FILE *dst_file, char chr); diff --git a/include/fscache.h b/include/fscache.h new file mode 100644 index 0000000..de69248 --- /dev/null +++ b/include/fscache.h @@ -0,0 +1,37 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include + +// These functions compare the "last modified" value of two files +// Return values: +// * negative integer if the left file is older (right is newer) +// * positive integer if the left file is newer (right is older) +// * zero if they are equal or if one does not exist +// The idea behind using zero as error is that, in any case, +// if the two files were changed at the same time, when checking +// for caching, it is safer to rebuild the file +double fscache_cmp_ff(FILE *f1, FILE *f2); +double fscache_cmp_pp(const char *p1, const char *p2); +double fscache_cmp_fp(FILE *f1, const char *f2); diff --git a/include/libhtmc/libhtmc-internals.h b/include/libhtmc/libhtmc-internals.h new file mode 100644 index 0000000..02dfb11 --- /dev/null +++ b/include/libhtmc/libhtmc-internals.h @@ -0,0 +1,42 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include +#include + +#include "libhtmc/libhtmc.h" + +int impl_debug_vprintf(htmc_handover_t *handover, + const char *fmt, + va_list args); +int impl_debug_puts(htmc_handover_t *handover, const char *s); +void *impl_debug_alloc(htmc_handover_t *handover, size_t nbytes); +void impl_debug_free(htmc_handover_t *handover, void *ptr); + +int impl_base_query_vscanf(htmc_handover_t *handover, + const char *fmt, + va_list args); +int impl_base_form_vscanf(htmc_handover_t *handover, + const char *fmt, + va_list args); diff --git a/include/libhtmc/libhtmc.h b/include/libhtmc/libhtmc.h new file mode 100644 index 0000000..d5d8998 --- /dev/null +++ b/include/libhtmc/libhtmc.h @@ -0,0 +1,60 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once +#include +#include +#include + +typedef enum htmc_handover_variant { + HTMC_BASE_HANDOVER +} htmc_handover_variant_t; + +typedef struct htmc_handover htmc_handover_t; +typedef struct htmc_handover { + const htmc_handover_variant_t variant_id; + int (*vprintf)(htmc_handover_t *handover, const char *fmt, va_list args); + int (*puts)(htmc_handover_t *handover, const char *s); + int (*query_vscanf)(htmc_handover_t *handover, const char *fmt, va_list args); + int (*form_vscanf)(htmc_handover_t *handover, const char *fmt, va_list args); + void *(*alloc)(htmc_handover_t *handover, size_t nbytes); + void (*free)(htmc_handover_t *handover, void *ptr); + + const char *request_method; + const char *query_string; + size_t content_length; + const char *content_type; + const char *request_body; +} htmc_handover_t; + +void htmc_bind(htmc_handover_t *handover); +int htmc_printf(const char *fmt, ...); +int htmc_vprintf(const char *fmt, va_list args); +int htmc_puts(const char *s); +int htmc_query_scanf(const char *fmt, ...); +int htmc_query_vscanf(const char *fmt, va_list args); +int htmc_form_scanf(const char *fmt, ...); +int htmc_form_vscafn(const char *fmt, va_list args); +void *htmc_alloc(size_t nbytes); +void htmc_free(void *ptr); +void htmc_error(const char *fmt, ...); +void htmc_verror(const char *fmt, va_list args); diff --git a/include/load.h b/include/load.h new file mode 100644 index 0000000..b7a1436 --- /dev/null +++ b/include/load.h @@ -0,0 +1,36 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include + +#include "libhtmc/libhtmc.h" + +#define HTMC_ENTRY_POINT_SYM "htmc_main" + +typedef int (*htmc_entry_point_t)(htmc_handover_t *); + +void *load_htmc_so(const char *so_file_path); +htmc_entry_point_t get_htmc_entry_point(void *so_handle); +int call_htmc_entry(htmc_entry_point_t entry_point, htmc_handover_t *handover); +int run_htmc_so(const char *so_file_path, htmc_handover_t *handover); diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..4473ae4 --- /dev/null +++ b/include/log.h @@ -0,0 +1,38 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +typedef enum { + HTMC_LOG_LEVEL_ALL, + HTMC_LOG_LEVEL_INFO, + HTMC_LOG_LEVEL_WARNING, + HTMC_LOG_LEVEL_ERROR, + HTMC_LOG_LEVEL_OFF, +} log_lvl_t; + +void log_fatal(const char *message); +void log_error(const char *message); +void log_info(const char *message); +void log_set_level(log_lvl_t lvl); +void log_set_safe(); +int log_translate_level(const char *lvl_str); diff --git a/include/parse.h b/include/parse.h new file mode 100644 index 0000000..7dffa4b --- /dev/null +++ b/include/parse.h @@ -0,0 +1,27 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include + +int parse_and_emit(FILE *src_file, FILE *dst_file); diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..7e78b51 --- /dev/null +++ b/include/util.h @@ -0,0 +1,25 @@ +// MIT License +// +// Copyright (c) 2024 Alessandro Salerno +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +void safe_free(void *ptr);