Fixes to build amalgamation and compile with https://github.com/jart/cosmopolitan

This commit is contained in:
mingodad 2022-09-27 13:37:57 +02:00
parent 4425938f0b
commit 938d83c21f
10 changed files with 159 additions and 22 deletions

8
am.c
View File

@ -155,8 +155,7 @@ error_bug(MathoMatic* mathomatic, const char *str)
void void
check_err(MathoMatic* mathomatic) check_err(MathoMatic* mathomatic)
{ {
switch (errno) { if(errno == EDOM) {
case EDOM:
errno = 0; errno = 0;
if (mathomatic->domain_check) { if (mathomatic->domain_check) {
mathomatic->domain_check = false; mathomatic->domain_check = false;
@ -164,12 +163,11 @@ check_err(MathoMatic* mathomatic)
error(mathomatic, _("Domain error in constant.")); error(mathomatic, _("Domain error in constant."));
longjmp(mathomatic->jmp_save, 2); longjmp(mathomatic->jmp_save, 2);
} }
break; }
case ERANGE: if(errno == ERANGE) {
errno = 0; errno = 0;
error(mathomatic, _("Floating point constant out of range.")); error(mathomatic, _("Floating point constant out of range."));
longjmp(mathomatic->jmp_save, 2); longjmp(mathomatic->jmp_save, 2);
break;
} }
} }

6
cmds.c
View File

@ -193,6 +193,7 @@ plot_cmd(MathoMatic* mathomatic, char *cp)
printf(_("Shell command-line = %s\n"), cl); printf(_("Shell command-line = %s\n"), cl);
} }
return true; return true;
#undef APPEND
} }
#endif #endif
@ -3431,7 +3432,8 @@ code_cmd(MathoMatic* mathomatic, char *cp)
} while (*cp); } while (*cp);
return displayed; return displayed;
} }
#ifndef VCMP_DEFINED
#define VCMP_DEFINED /*there is a copy elsewhere */
/* /*
* Compare function for qsort(3). * Compare function for qsort(3).
*/ */
@ -3448,7 +3450,7 @@ sort_type *p1, *p2;
} }
return(p2->count - p1->count); return(p2->count - p1->count);
} }
#endif /*VCMP_DEFINED*/
/* /*
* The variables command. * The variables command.
*/ */

View File

@ -27,8 +27,10 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
#include "complex.h" #include "complex.h"
#include <math.h> #include <math.h>
#ifndef true
#define true 1 #define true 1
#define false 0 #define false 0
#endif
#define epsilon 0.00000000000005 /* a good value for doubles */ #define epsilon 0.00000000000005 /* a good value for doubles */
@ -165,3 +167,5 @@ complexs a, b;
complex_fixup(&r); complex_fixup(&r);
return(r); return(r);
} }
#undef epsilon

2
help.c
View File

@ -917,7 +917,7 @@ display_repeat_command(MathoMatic* mathomatic)
} }
int int
read_examples(MathoMatic* mathomatic, char **cpp) read_examples(MathoMatic* mathomatic, const char **cpp)
{ {
int i; int i;
char *cp; char *cp;

View File

@ -24,8 +24,10 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/ */
#ifndef true
#define true 1 #define true 1
#define false 0 #define false 0
#endif
#if 0 #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. */ #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. */

16
main.c
View File

@ -113,9 +113,7 @@ usage(FILE *fp)
} }
int int
main(argc, argv) main(int argc, char *argv[])
int argc;
char **argv;
{ {
#if NO_GETOPT_H /* if no getopt.h is available */ #if NO_GETOPT_H /* if no getopt.h is available */
extern char *optarg; /* set by getopt(3) */ 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. * Return zero on success, or a non-zero unsettable signal number on error.
*/ */
int int
set_signals(time_out_seconds) set_signals(unsigned int time_out_seconds)
unsigned int time_out_seconds;
{ {
int rv = 0; int rv = 0;
@ -466,8 +463,7 @@ unsigned int time_out_seconds;
* Floating point exceptions are currently ignored. * Floating point exceptions are currently ignored.
*/ */
void void
fphandler(sig) fphandler(int sig)
int sig;
{ {
#if DEBUG #if DEBUG
warning(mathomatic, "Floating point exception."); warning(mathomatic, "Floating point exception.");
@ -480,8 +476,7 @@ int sig;
* If it can't, repeated calls terminate this program. * If it can't, repeated calls terminate this program.
*/ */
void void
inthandler(sig) inthandler(int sig)
int sig;
{ {
matho_inc_abort_flag(mathomatic); matho_inc_abort_flag(mathomatic);
switch (matho_get_abort_flag(mathomatic)) { switch (matho_get_abort_flag(mathomatic)) {
@ -505,8 +500,7 @@ int sig;
* Alarm signal handler. * Alarm signal handler.
*/ */
void void
alarmhandler(sig) alarmhandler(int sig)
int sig;
{ {
printf(_("\nTimeout, quitting...\n")); printf(_("\nTimeout, quitting...\n"));
exit_program(mathomatic, 1); exit_program(mathomatic, 1);

View File

@ -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 <sys/resource.h> //7
#include <readline/history.h> //21
#include <unistd.h> //3
#include <limits.h> //8
#include <setjmp.h> //11
#include <getopt.h> //27
//#include <wincon.h> //26
#include <readline/readline.h> //20
#include <math.h> //10
#include <editline.h> //22
#include <string.h> //13
//#include <windows.h> //25
#include <float.h> //9
#include <libgen.h> //4
#include <termios.h> //24
#include <libintl.h> //16
#include <term.h> //19
#include <curses.h> //18
#include <locale.h> //17
#include <ctype.h> //12
#include <stdlib.h> //2
#include <sys/ioctl.h> //23
#include <errno.h> //14
#include <signal.h> //15
#include <stdio.h> //1
//#include <ieeefp.h> //5
#include <sys/time.h> //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

16
mk-mathomatic.sh Executable file
View File

@ -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 \

5
poly.c
View File

@ -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 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 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); 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). * Compare function for qsort(3).
*/ */
@ -84,7 +85,7 @@ sort_type *p1, *p2;
} }
return(p2->count - p1->count); return(p2->count - p1->count);
} }
#endif /*VCMP_DEFINED*/
/* /*
* Return true if passed expression is strictly a single polynomial term in variable v. * 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) * The general form of a polynomial term is c*(v^d)

View File

@ -166,7 +166,7 @@ char *parse_var2(MathoMatic* mathomatic, long *vp, char *cp);
int display_usage(MathoMatic* mathomatic, char *pstr, int i); int display_usage(MathoMatic* mathomatic, char *pstr, int i);
int display_command(MathoMatic* mathomatic, int i); int display_command(MathoMatic* mathomatic, int i);
int display_repeat_command(MathoMatic* mathomatic); 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); void underline_title(MathoMatic* mathomatic, int count);
int help_cmd(MathoMatic* mathomatic, char *cp); int help_cmd(MathoMatic* mathomatic, char *cp);
/* integrate.c */ /* integrate.c */