From 938d83c21fbbcb6cf72bca29f5f2421853c091ad Mon Sep 17 00:00:00 2001 From: mingodad Date: Tue, 27 Sep 2022 13:37:57 +0200 Subject: [PATCH] Fixes to build amalgamation and compile with https://github.com/jart/cosmopolitan --- am.c | 8 +-- cmds.c | 6 +- complex_lib.c | 4 ++ help.c | 2 +- includes.h | 2 + main.c | 16 ++--- mk-mathomatic-amalgamation.lua | 120 +++++++++++++++++++++++++++++++++ mk-mathomatic.sh | 16 +++++ poly.c | 5 +- proto.h | 2 +- 10 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 mk-mathomatic-amalgamation.lua create mode 100755 mk-mathomatic.sh diff --git a/am.c b/am.c index 37403df..a57d651 100644 --- a/am.c +++ b/am.c @@ -155,8 +155,7 @@ error_bug(MathoMatic* mathomatic, const char *str) void check_err(MathoMatic* mathomatic) { - switch (errno) { - case EDOM: + if(errno == EDOM) { errno = 0; if (mathomatic->domain_check) { mathomatic->domain_check = false; @@ -164,12 +163,11 @@ check_err(MathoMatic* mathomatic) error(mathomatic, _("Domain error in constant.")); longjmp(mathomatic->jmp_save, 2); } - break; - case ERANGE: + } + if(errno == ERANGE) { errno = 0; error(mathomatic, _("Floating point constant out of range.")); longjmp(mathomatic->jmp_save, 2); - break; } } diff --git a/cmds.c b/cmds.c index 3bb62ab..687715d 100644 --- a/cmds.c +++ b/cmds.c @@ -193,6 +193,7 @@ plot_cmd(MathoMatic* mathomatic, char *cp) printf(_("Shell command-line = %s\n"), cl); } return true; +#undef APPEND } #endif @@ -3431,7 +3432,8 @@ code_cmd(MathoMatic* mathomatic, char *cp) } while (*cp); return displayed; } - +#ifndef VCMP_DEFINED +#define VCMP_DEFINED /*there is a copy elsewhere */ /* * Compare function for qsort(3). */ @@ -3448,7 +3450,7 @@ sort_type *p1, *p2; } return(p2->count - p1->count); } - +#endif /*VCMP_DEFINED*/ /* * The variables command. */ diff --git a/complex_lib.c b/complex_lib.c index 020af71..8b19467 100644 --- a/complex_lib.c +++ b/complex_lib.c @@ -27,8 +27,10 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA. #include "complex.h" #include +#ifndef true #define true 1 #define false 0 +#endif #define epsilon 0.00000000000005 /* a good value for doubles */ @@ -165,3 +167,5 @@ complexs a, b; complex_fixup(&r); return(r); } + +#undef epsilon diff --git a/help.c b/help.c index 7622d2a..10cf6de 100644 --- a/help.c +++ b/help.c @@ -917,7 +917,7 @@ display_repeat_command(MathoMatic* mathomatic) } int -read_examples(MathoMatic* mathomatic, char **cpp) +read_examples(MathoMatic* mathomatic, const char **cpp) { int i; char *cp; diff --git a/includes.h b/includes.h index 02164b8..c936c5d 100644 --- a/includes.h +++ b/includes.h @@ -24,8 +24,10 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA. */ +#ifndef true #define true 1 #define false 0 +#endif #if 0 #define _REENTRANT 1 /* Can be defined before including math.h for Mac OS X. Mac OS X allows a few re-entrant functions with this. iOS requires this commented out. */ diff --git a/main.c b/main.c index 8b09a25..2ba7d5a 100644 --- a/main.c +++ b/main.c @@ -113,9 +113,7 @@ usage(FILE *fp) } int -main(argc, argv) -int argc; -char **argv; +main(int argc, char *argv[]) { #if NO_GETOPT_H /* if no getopt.h is available */ extern char *optarg; /* set by getopt(3) */ @@ -426,8 +424,7 @@ main_io_loop(void) * Return zero on success, or a non-zero unsettable signal number on error. */ int -set_signals(time_out_seconds) -unsigned int time_out_seconds; +set_signals(unsigned int time_out_seconds) { int rv = 0; @@ -466,8 +463,7 @@ unsigned int time_out_seconds; * Floating point exceptions are currently ignored. */ void -fphandler(sig) -int sig; +fphandler(int sig) { #if DEBUG warning(mathomatic, "Floating point exception."); @@ -480,8 +476,7 @@ int sig; * If it can't, repeated calls terminate this program. */ void -inthandler(sig) -int sig; +inthandler(int sig) { matho_inc_abort_flag(mathomatic); switch (matho_get_abort_flag(mathomatic)) { @@ -505,8 +500,7 @@ int sig; * Alarm signal handler. */ void -alarmhandler(sig) -int sig; +alarmhandler(int sig) { printf(_("\nTimeout, quitting...\n")); exit_program(mathomatic, 1); diff --git a/mk-mathomatic-amalgamation.lua b/mk-mathomatic-amalgamation.lua new file mode 100644 index 0000000..f990be4 --- /dev/null +++ b/mk-mathomatic-amalgamation.lua @@ -0,0 +1,120 @@ +local base_dir = "./"; +local includes_base = {"lib/"} +local sq_sources = [==[ +globals.c +am.c +solve.c +help.c +parse.c +cmds.c +simplify.c +factor.c +super.c +unfactor.c +poly.c +diff.c +integrate.c +complex.c +complex_lib.c +list.c +gcd.c +factor_int.c +main.c +]==]; + +local included = {}; +local inc_sys = {}; +local inc_sys_count = 0; +local out = io.stdout + +function CopyWithInline(prefix, filename) + if included[filename] then return end + included[filename] = true + print('//--Start of', filename); + --if(filename:match("luac?.c")) + local inp = io.open(prefix .. filename, "r") + if not inp then + for idx in ipairs(includes_base) do + local sdir = includes_base[idx] + local fn = prefix .. sdir .. filename + --print(fn) + inp = io.open(fn, "r") + if inp then break end + end + end + if not inp then + if filename == "fzn_picat_sat_bc.h" then + print('//--End of', filename); + end + else + assert(inp) + for line in inp:lines() do + if line:match('#define LUA_USE_READLINE') then + out:write("//" .. line .. "\n") + else + local inc = line:match('#include%s+(["<].-)[">]') + if inc then + out:write("//" .. line .. "\n") + if inc:sub(1,1) == '"' or inc:match('[<"]sq') then + CopyWithInline(prefix, inc:sub(2)) + else + local fn = inc:sub(2) + if inc_sys[fn] == null then + inc_sys_count = inc_sys_count +1 + inc_sys[fn] = inc_sys_count + end + end + else + out:write(line .. "\n") + end + end + end + print('//--End of', filename); + end +end + +print([==[ +#ifdef WITH_COSMOPOLITAN + +STATIC_STACK_SIZE(0x400000); + +#endif + +#ifndef __COSMOPOLITAN__ +//gcc -Wall -DUNIX -DVERSION=\"16.0.5\" -o mathomatic mathomatic-am.c -lm +#include //7 +#include //21 +#include //3 +#include //8 +#include //11 +#include //27 +//#include //26 +#include //20 +#include //10 +#include //22 +#include //13 +//#include //25 +#include //9 +#include //4 +#include //24 +#include //16 +#include //19 +#include //18 +#include //17 +#include //12 +#include //2 +#include //23 +#include //14 +#include //15 +#include //1 +//#include //5 +#include //6 + +#endif +]==]) + +local prefix = base_dir; local src_files = sq_sources; +for filename in src_files:gmatch('([^\n]+)') do + CopyWithInline(prefix, filename); +end +--for k, v in pairs(inc_sys) do print("#include <" .. k .. "> //" .. v ) end diff --git a/mk-mathomatic.sh b/mk-mathomatic.sh new file mode 100755 index 0000000..50cb07e --- /dev/null +++ b/mk-mathomatic.sh @@ -0,0 +1,16 @@ +# run gcc compiler in freestanding mode +gcc -g -Os -static -fno-pie -no-pie -nostdlib -nostdinc \ + -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \ + -o mathomatic.com.dbg mathomatic-am.c \ + -DUNIX -DVERSION=\"16.0.5\" \ + -DWITH_COSMOPOLITAN \ + -Wl,--gc-sections -fuse-ld=bfd -Wl,--gc-sections \ + -Wl,-T,ape.lds -include cosmopolitan.h crt.o ape-no-modify-self.o cosmopolitan.a +objcopy -S -O binary mathomatic.com.dbg mathomatic.com + + +# NOTE: scp it to windows/mac/etc. *before* you run it! +# ~40kb static binary (can be ~16kb w/ MODE=tiny) +./ape.elf ./mathomatic.com +# -DSTACK_FRAME_UNLIMITED \ +# -fno-gcse -ffunction-sections -fdata-sections \ diff --git a/poly.c b/poly.c index 748eb8c..c544bc8 100644 --- a/poly.c +++ b/poly.c @@ -67,7 +67,8 @@ static int polydiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np static int pdiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int code); static int poly_div_sub(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2, long *vp); static int find_highest_count(token_type *p1, int n1, token_type *p2, int n2, long *vp1); - +#ifndef VCMP_DEFINED +#define VCMP_DEFINED /*there is a copy elsewhere */ /* * Compare function for qsort(3). */ @@ -84,7 +85,7 @@ sort_type *p1, *p2; } return(p2->count - p1->count); } - +#endif /*VCMP_DEFINED*/ /* * Return true if passed expression is strictly a single polynomial term in variable v. * The general form of a polynomial term is c*(v^d) diff --git a/proto.h b/proto.h index df9c0d0..31d5441 100644 --- a/proto.h +++ b/proto.h @@ -166,7 +166,7 @@ char *parse_var2(MathoMatic* mathomatic, long *vp, char *cp); int display_usage(MathoMatic* mathomatic, char *pstr, int i); int display_command(MathoMatic* mathomatic, int i); int display_repeat_command(MathoMatic* mathomatic); -int read_examples(MathoMatic* mathomatic, char **cpp); +int read_examples(MathoMatic* mathomatic, const char **cpp); void underline_title(MathoMatic* mathomatic, int count); int help_cmd(MathoMatic* mathomatic, char *cp); /* integrate.c */