diff --git a/programs/develop/ktcc/trunk/bin/kos32-tcc b/programs/develop/ktcc/trunk/bin/kos32-tcc index 5fa83afe5e..5596e714a5 100755 Binary files a/programs/develop/ktcc/trunk/bin/kos32-tcc and b/programs/develop/ktcc/trunk/bin/kos32-tcc differ diff --git a/programs/develop/ktcc/trunk/bin/kos32-tcc.exe b/programs/develop/ktcc/trunk/bin/kos32-tcc.exe new file mode 100644 index 0000000000..fb44b69813 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/kos32-tcc.exe differ diff --git a/programs/develop/ktcc/trunk/bin/lib/box_lib.def b/programs/develop/ktcc/trunk/bin/lib/box_lib.def deleted file mode 100644 index 1badc805c9..0000000000 --- a/programs/develop/ktcc/trunk/bin/lib/box_lib.def +++ /dev/null @@ -1,98 +0,0 @@ -LIBRARY box_lib.obj - -EXPORTS -edit_box -edit_box_key -edit_box_mouse -edit_box_set_text -version_ed -init_checkbox2 -check_box_draw2 -check_box_mouse2 -version_ch2 -option_box_draw -option_box_mouse -version_op -scrollbar_v_draw -scrollbar_v_mouse -scrollbar_h_draw -scrollbar_h_mouse -version_scrollbar -dbutton_draw -dbutton_mouse -version_dbutton -menu_bar_draw -menu_bar_mouse -menu_bar_activate -version_menu_bar -FileBrowser_draw -FileBrowser_mouse -FileBrowser_key -version_FileBrowser -tl_data_init -tl_data_clear -tl_info_clear -tl_key -tl_mouse -tl_draw -tl_info_undo -tl_info_redo -tl_node_add -tl_node_set_data -tl_node_get_data -tl_node_delete -tl_cur_beg -tl_cur_next -tl_cur_perv -tl_node_close_open -tl_node_lev_inc -tl_node_lev_dec -tl_node_move_up -tl_node_move_down -tl_node_poi_get_info -tl_node_poi_get_next_info -tl_node_poi_get_data -tl_save_mem -tl_load_mem -tl_get_mem_size -version_tree_list -PathShow_prepare -PathShow_draw -version_PathShow -ted_but_sumb_upper -ted_but_sumb_lover -ted_but_convert_by_table -ted_can_save -ted_clear -ted_delete -ted_draw -ted_init -ted_init_scroll_bars -ted_init_syntax_file -ted_is_select -ted_key -ted_mouse -ted_open_file -ted_save_file -ted_text_add -ted_but_select_word -ted_but_cut -ted_but_copy -ted_but_paste -ted_but_undo -ted_but_redo -ted_but_reverse -ted_but_find -ted_but_replace -ted_text_colored -ted_go_to_position -version_text_edit -frame_draw -version_frame -progressbar_draw -progressbar_progress -tooltip_init -tooltip_delete -tooltip_test_show -tooltip_mouse -get_font_size diff --git a/programs/develop/ktcc/trunk/bin/lib/crt0.o b/programs/develop/ktcc/trunk/bin/lib/crt0.o index b857c80565..598dbd5fcc 100644 Binary files a/programs/develop/ktcc/trunk/bin/lib/crt0.o and b/programs/develop/ktcc/trunk/bin/lib/crt0.o differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libbox.a b/programs/develop/ktcc/trunk/bin/lib/libbox.a new file mode 100644 index 0000000000..cf3aa51b7c Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libbox.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libbuf2d.a b/programs/develop/ktcc/trunk/bin/lib/libbuf2d.a new file mode 100644 index 0000000000..30a0f85c7f Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libbuf2d.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libc.obj.a b/programs/develop/ktcc/trunk/bin/lib/libc.obj.a new file mode 100644 index 0000000000..d0cb67e74c Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libc.obj.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libck.a b/programs/develop/ktcc/trunk/bin/lib/libck.a new file mode 100644 index 0000000000..8ce0341010 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libck.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libdialog.a b/programs/develop/ktcc/trunk/bin/lib/libdialog.a new file mode 100644 index 0000000000..04a5772a1d Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libdialog.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libgb.a b/programs/develop/ktcc/trunk/bin/lib/libgb.a new file mode 100644 index 0000000000..b6680ec90d Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libgb.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libhttp.a b/programs/develop/ktcc/trunk/bin/lib/libhttp.a new file mode 100644 index 0000000000..80fdce442a Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libhttp.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libimg.a b/programs/develop/ktcc/trunk/bin/lib/libimg.a new file mode 100644 index 0000000000..c5f7806118 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libimg.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libinputbox.a b/programs/develop/ktcc/trunk/bin/lib/libinputbox.a new file mode 100644 index 0000000000..59de846216 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libinputbox.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libmsgbox.a b/programs/develop/ktcc/trunk/bin/lib/libmsgbox.a new file mode 100644 index 0000000000..a23e582777 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libmsgbox.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libnetwork.a b/programs/develop/ktcc/trunk/bin/lib/libnetwork.a new file mode 100644 index 0000000000..200b6f9f58 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/libnetwork.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/librasterworks.a b/programs/develop/ktcc/trunk/bin/lib/librasterworks.a new file mode 100644 index 0000000000..c7bc4b96c2 Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/lib/librasterworks.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/libtcc1.a b/programs/develop/ktcc/trunk/bin/lib/libtcc.a similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/libtcc1.a rename to programs/develop/ktcc/trunk/bin/lib/libtcc.a diff --git a/programs/develop/ktcc/trunk/bin/lib/msgbox.def b/programs/develop/ktcc/trunk/bin/lib/msgbox.def deleted file mode 100644 index eb37dca5e5..0000000000 --- a/programs/develop/ktcc/trunk/bin/lib/msgbox.def +++ /dev/null @@ -1,6 +0,0 @@ -LIBRARY msgbox.obj - -EXPORTS -mb_create -mb_reinit -mb_setfunctions diff --git a/programs/develop/ktcc/trunk/bin/lib/rasterworks.def b/programs/develop/ktcc/trunk/bin/lib/rasterworks.def deleted file mode 100644 index ca51de693a..0000000000 --- a/programs/develop/ktcc/trunk/bin/lib/rasterworks.def +++ /dev/null @@ -1,7 +0,0 @@ -LIBRARY rasterworks.obj - -EXPORTS -charsFit -cntUTF-8 -drawText -strWidth diff --git a/programs/develop/ktcc/trunk/bin/tcc b/programs/develop/ktcc/trunk/bin/tcc new file mode 100644 index 0000000000..1c7e416f6c Binary files /dev/null and b/programs/develop/ktcc/trunk/bin/tcc differ diff --git a/programs/develop/ktcc/trunk/bin/doc/en/Features.txt b/programs/develop/ktcc/trunk/kx/doc/en/Features.txt similarity index 100% rename from programs/develop/ktcc/trunk/bin/doc/en/Features.txt rename to programs/develop/ktcc/trunk/kx/doc/en/Features.txt diff --git a/programs/develop/ktcc/trunk/bin/doc/en/How to use.txt b/programs/develop/ktcc/trunk/kx/doc/en/How to use.txt similarity index 100% rename from programs/develop/ktcc/trunk/bin/doc/en/How to use.txt rename to programs/develop/ktcc/trunk/kx/doc/en/How to use.txt diff --git a/programs/develop/ktcc/trunk/bin/doc/ru/Features.txt b/programs/develop/ktcc/trunk/kx/doc/ru/Features.txt similarity index 100% rename from programs/develop/ktcc/trunk/bin/doc/ru/Features.txt rename to programs/develop/ktcc/trunk/kx/doc/ru/Features.txt diff --git a/programs/develop/ktcc/trunk/bin/doc/ru/History.txt b/programs/develop/ktcc/trunk/kx/doc/ru/History.txt similarity index 100% rename from programs/develop/ktcc/trunk/bin/doc/ru/History.txt rename to programs/develop/ktcc/trunk/kx/doc/ru/History.txt diff --git a/programs/develop/ktcc/trunk/bin/doc/ru/How to use.txt b/programs/develop/ktcc/trunk/kx/doc/ru/How to use.txt similarity index 100% rename from programs/develop/ktcc/trunk/bin/doc/ru/How to use.txt rename to programs/develop/ktcc/trunk/kx/doc/ru/How to use.txt diff --git a/programs/develop/ktcc/trunk/bin/lib/box.def b/programs/develop/ktcc/trunk/kx/lib/box.def similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/box.def rename to programs/develop/ktcc/trunk/kx/lib/box.def diff --git a/programs/develop/ktcc/trunk/bin/lib/console.def b/programs/develop/ktcc/trunk/kx/lib/console.def similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/console.def rename to programs/develop/ktcc/trunk/kx/lib/console.def diff --git a/programs/develop/ktcc/trunk/kx/lib/crt0.o b/programs/develop/ktcc/trunk/kx/lib/crt0.o new file mode 100644 index 0000000000..b857c80565 Binary files /dev/null and b/programs/develop/ktcc/trunk/kx/lib/crt0.o differ diff --git a/programs/develop/ktcc/trunk/bin/lib/dialog.def b/programs/develop/ktcc/trunk/kx/lib/dialog.def similarity index 88% rename from programs/develop/ktcc/trunk/bin/lib/dialog.def rename to programs/develop/ktcc/trunk/kx/lib/dialog.def index f292613608..eac5819e3a 100644 --- a/programs/develop/ktcc/trunk/bin/lib/dialog.def +++ b/programs/develop/ktcc/trunk/kx/lib/dialog.def @@ -2,7 +2,6 @@ LIBRARY proc_lib.obj EXPORTS ColorDialog_init -ColorDialog_start OpenDialog_start OpenDialog_init OpenDialog_set_file_ext diff --git a/programs/develop/ktcc/trunk/bin/lib/http.def b/programs/develop/ktcc/trunk/kx/lib/http.def similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/http.def rename to programs/develop/ktcc/trunk/kx/lib/http.def diff --git a/programs/develop/ktcc/trunk/bin/lib/img.def b/programs/develop/ktcc/trunk/kx/lib/img.def similarity index 87% rename from programs/develop/ktcc/trunk/bin/lib/img.def rename to programs/develop/ktcc/trunk/kx/lib/img.def index a8a0922d11..857a64cd0f 100644 --- a/programs/develop/ktcc/trunk/bin/lib/img.def +++ b/programs/develop/ktcc/trunk/kx/lib/img.def @@ -17,4 +17,4 @@ img_rotate img_rotate_layer img_scale img_to_rgb -img_to_rgb2 +img_to_rgb2 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/bin/lib/libc.def b/programs/develop/ktcc/trunk/kx/lib/libc.def similarity index 97% rename from programs/develop/ktcc/trunk/bin/lib/libc.def rename to programs/develop/ktcc/trunk/kx/lib/libc.def index b37a70843f..c36e2999fd 100644 --- a/programs/develop/ktcc/trunk/bin/lib/libc.def +++ b/programs/develop/ktcc/trunk/kx/lib/libc.def @@ -175,6 +175,3 @@ time localtime asctime difftime -;___LIBGEN___ -basename -dirname diff --git a/programs/develop/ktcc/trunk/kx/lib/libck.a b/programs/develop/ktcc/trunk/kx/lib/libck.a new file mode 100644 index 0000000000..96b3ff2ae4 Binary files /dev/null and b/programs/develop/ktcc/trunk/kx/lib/libck.a differ diff --git a/programs/develop/ktcc/trunk/kx/lib/libcryptal.a b/programs/develop/ktcc/trunk/kx/lib/libcryptal.a new file mode 100644 index 0000000000..59467f1660 Binary files /dev/null and b/programs/develop/ktcc/trunk/kx/lib/libcryptal.a differ diff --git a/programs/develop/ktcc/trunk/kx/lib/libtcc.a b/programs/develop/ktcc/trunk/kx/lib/libtcc.a new file mode 100644 index 0000000000..77e05da5e8 Binary files /dev/null and b/programs/develop/ktcc/trunk/kx/lib/libtcc.a differ diff --git a/programs/develop/ktcc/trunk/bin/lib/mbedtls.def b/programs/develop/ktcc/trunk/kx/lib/mbedtls.def similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/mbedtls.def rename to programs/develop/ktcc/trunk/kx/lib/mbedtls.def diff --git a/programs/develop/ktcc/trunk/bin/lib/network.def b/programs/develop/ktcc/trunk/kx/lib/network.def similarity index 100% rename from programs/develop/ktcc/trunk/bin/lib/network.def rename to programs/develop/ktcc/trunk/kx/lib/network.def diff --git a/programs/develop/ktcc/trunk/kx/src/crt/crt0.asm b/programs/develop/ktcc/trunk/kx/src/crt/crt0.asm new file mode 100644 index 0000000000..55bf4f6f69 --- /dev/null +++ b/programs/develop/ktcc/trunk/kx/src/crt/crt0.asm @@ -0,0 +1,127 @@ +; +; 2021, Edited by Coldy +; +; This module same as original crt0.asm, but cut: +; 1. virtual header block (hparams change to __app_params, hpath change to __app_path) +; 2. init heap of memory - not needed because 68.18 (68.19) init heap implicitly +; (it is does dll.obj) +; 3. loader (he lives in dll.obj) +; + +format ELF +section '.text' executable +public start +public start as '_start' + +extrn main +;include 'debug2.inc' +include '/../../../../../../proc32.inc' +include '/../../../../../../macros.inc' +__DEBUG__ = 0 + +__app_params equ 0x1C ; Pointer to program arguments +;__app_path equ 0x20 ; Pointer to program path + +start: +;DEBUGF 'Start programm\n' + + mov [argc], 0 + mov eax, [__app_params] + test eax, eax + jz .without_path + mov eax, path + cmp word ptr eax, 32fh ; '/#3' UTF8 + jne .without_path + mov word ptr eax, 12fh ; '/#1' fix to CP866 +.without_path: + mov esi, eax + call push_param +; retrieving parameters + mov esi, params + xor edx, edx ; dl - идёт параметр(1) или разделители(0) + ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) + mov ecx, 1 ; cl = 1 + ; ch = 0 просто ноль +.parse: + lodsb + test al, al + jz .run + test dl, dl + jnz .findendparam + ;{если был разделитель + cmp al, ' ' + jz .parse ;загружен пробел, грузим следующий символ + mov dl, cl ;начинается параметр + cmp al, '"' + jz @f ;загружены кавычки + mov dh, ch ;параметр без кавычек + dec esi + call push_param + inc esi + jmp .parse + + @@: + mov dh, cl ;параметр в кавычеках + call push_param ;если не пробел значит начинается какой то параметр + jmp .parse ;если был разделитель} + +.findendparam: + test dh, dh + jz @f ; без кавычек + cmp al, '"' + jz .clear + jmp .parse + @@: + cmp al, ' ' + jnz .parse + +.clear: + lea ebx, [esi - 1] + mov [ebx], ch + mov dl, ch + jmp .parse + +.run: + push argv + push [argc] + call main +.exit: + xor eax,eax + dec eax + int 0x40 + dd -1 +.crash: + jmp .exit +;============================ +push_param: +;============================ +;parameters +; esi - pointer +;description +; procedure increase argc +; and add pointer to array argv +; procedure changes ebx + mov ebx, [argc] + cmp ebx, max_parameters + jae .dont_add + mov [argv+4*ebx], esi + inc [argc] +.dont_add: + ret + + +;============================== +public argc as '__argc' +public params as '__argv' +public path as '__path' + +section '.bss' +buf_len = 0x400 +max_parameters=0x20 +argc rd 1 +argv rd max_parameters +path rb buf_len +params rb buf_len + +;section '.data' +;include_debug_strings ; ALWAYS present in data section diff --git a/programs/develop/ktcc/trunk/kx/src/tests/kxtest.c b/programs/develop/ktcc/trunk/kx/src/tests/kxtest.c new file mode 100644 index 0000000000..f53e59a567 --- /dev/null +++ b/programs/develop/ktcc/trunk/kx/src/tests/kxtest.c @@ -0,0 +1,18 @@ +// +// This sample show that KX extension supported or not + +#include + +int main(){ + + char* kx_ext = +#ifndef __KX__ + "not " +#endif + "supported" + ; + + printf("KX extension %s", kx_ext); + + return 0; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/kx/src/tests/kxtest.sh b/programs/develop/ktcc/trunk/kx/src/tests/kxtest.sh new file mode 100644 index 0000000000..94fb434702 --- /dev/null +++ b/programs/develop/ktcc/trunk/kx/src/tests/kxtest.sh @@ -0,0 +1,3 @@ +#SHS +../../../tcc ../src/tests/kxtest.c -o kxtest -I../../include -llibc +exit \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/boxlib/Makefile b/programs/develop/ktcc/trunk/lib/boxlib/Makefile new file mode 100644 index 0000000000..599c114e04 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/boxlib/Makefile @@ -0,0 +1,10 @@ +all: + fasm boxlib.asm + ar -crs libbox.a boxlib.o + +clean: + rm -f *.a + rm -f *.o + +install: + mv libbox.a ../../bin/lib diff --git a/programs/develop/ktcc/trunk/lib/boxlib/boxlib.asm b/programs/develop/ktcc/trunk/lib/boxlib/boxlib.asm new file mode 100644 index 0000000000..0392ca3853 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/boxlib/boxlib.asm @@ -0,0 +1,230 @@ +; writed by maxcodehack +; adaptation of clayer for ktcc +format elf +use32 ; Tell compiler to use 32 bit instructions +; ELF section +section '.text' executable + + +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +purge section,mov,add,sub + +include '../../../../../dll.inc' + + +public init_boxlib as 'kolibri_boxlib_init' + +proc init_boxlib +local retval dd ? + mov [retval], eax + pusha + mcall 68, 11 + test eax, eax + jnz @f + mov [retval], -1 + jmp exit_init_boxlib +@@: + stdcall dll.Load, @IMPORT + test eax, eax + jz exit_init_boxlib + mov [retval], -1 +exit_init_boxlib: + popa + mov eax, [retval] + ret +endp + +;; Wrapper to handle edit_box_key function for editboxes. +;; Call this baby from C (refer kolibri_editbox.h for details) +public editbox_key_thunk as 'edit_box_key' ; renamed due to ambiguity +;; replaced by siemargl as inline ASM in C wrapper +editbox_key_thunk: + mov eax, [esp+8] + mov [oldebp], ebp ;Save ebp because GCC is crazy for it otherwise. + pop ebp ;Save return address in ebp. Stack top is param now. + ;mov eax, dword [press_key] + call [edit_box_key] ; The pointer we passed should be on the stack already. + push ebp ;push the return address back to stack + mov ebp, [oldebp] + ret +oldebp dd ? + +section '.data' writeable + +@IMPORT: +library lib_boxlib, 'box_lib.obj' + +import lib_boxlib, \ + edit_box_draw, 'edit_box' , \ + edit_box_key, 'edit_box_key' , \ + edit_box_mouse, 'edit_box_mouse', \ + edit_box_set_text, 'edit_box_set_text' , \ + init_checkbox2, 'init_checkbox2' , \ + check_box_draw2, 'check_box_draw2' , \ + check_box_mouse2, 'check_box_mouse2' , \ + option_box_draw, 'option_box_draw' , \ + option_box_mouse, 'option_box_mouse' , \ + scrollbar_v_draw, 'scrollbar_v_draw' , \ + scrollbar_v_mouse, 'scrollbar_v_mouse' , \ + scrollbar_h_draw, 'scrollbar_h_draw' , \ + scrollbar_h_mouse, 'scrollbar_h_mouse' , \ + dynamic_button_draw, 'dbutton_draw' , \ + dynamic_button_mouse, 'dbutton_mouse' , \ + menu_bar_draw, 'menu_bar_draw' , \ + menu_bar_mouse, 'menu_bar_mouse' , \ + menu_bar_activate, 'menu_bar_activate' , \ + fb_draw_panel, 'FileBrowser_draw' , \ + fb_mouse, 'FileBrowser_mouse' , \ + fb_key, 'FileBrowser_key' , \ + tl_data_init, 'tl_data_init' , \ + tl_data_clear, 'tl_data_clear' , \ + tl_info_clear, 'tl_info_clear' , \ + tl_key, 'tl_key' , \ + tl_mouse, 'tl_mouse' , \ + tl_draw, 'tl_draw' , \ + tl_info_undo, 'tl_info_undo' , \ + tl_info_redo, 'tl_info_redo' , \ + tl_node_add, 'tl_node_add' , \ + tl_node_set_data, 'tl_node_set_data' , \ + tl_node_get_data, 'tl_node_get_data' , \ + tl_node_delete, 'tl_node_delete' , \ + tl_cur_beg, 'tl_cur_beg' , \ + tl_cur_next, 'tl_cur_next' , \ + tl_cur_perv, 'tl_cur_perv' , \ + tl_node_close_open, 'tl_node_close_open' , \ + tl_node_lev_inc, 'tl_node_lev_inc' , \ + tl_node_lev_dec, 'tl_node_lev_dec' , \ + tl_node_move_up, 'tl_node_move_up' , \ + tl_node_move_down, 'tl_node_move_down' , \ + tl_node_poi_get_info, 'tl_node_poi_get_info' , \ + tl_node_poi_get_next_info, 'tl_node_poi_get_next_info' , \ + tl_node_poi_get_data, 'tl_node_poi_get_data' , \ + tl_save_mem, 'tl_save_mem' , \ + tl_load_mem, 'tl_load_mem' , \ + tl_get_mem_size, 'tl_get_mem_size' , \ + path_show_prepare, 'PathShow_prepare' , \ + path_show_draw, 'PathShow_draw' , \ + ted_but_sumb_upper, 'ted_but_sumb_upper' , \ + ted_but_sumb_lover, 'ted_but_sumb_lover' , \ + ted_but_convert_by_table, 'ted_but_convert_by_table' , \ + ted_can_save, 'ted_can_save' , \ + ted_clear, 'ted_clear' , \ + ted_delete, 'ted_delete' , \ + ted_draw, 'ted_draw' , \ + ted_init, 'ted_init' , \ + ted_init_scroll_bars, 'ted_init_scroll_bars' , \ + ted_init_syntax_file, 'ted_init_syntax_file' , \ + ted_is_select, 'ted_is_select' , \ + ted_key, 'ted_key' , \ + ted_mouse, 'ted_mouse' , \ + ted_open_file, 'ted_open_file' , \ + ted_save_file, 'ted_save_file' , \ + ted_text_add, 'ted_text_add' , \ + ted_but_select_word, 'ted_but_select_word' , \ + ted_but_cut, 'ted_but_cut' , \ + ted_but_copy, 'ted_but_copy' , \ + ted_but_paste, 'ted_but_paste' , \ + ted_but_undo, 'ted_but_undo' , \ + ted_but_redo, 'ted_but_redo' , \ + ted_but_reverse, 'ted_but_reverse' , \ + ted_but_find, 'ted_but_find' , \ + ted_but_replace, 'ted_but_replace' , \ + ted_text_colored, 'ted_text_colored' , \ + ted_go_to_position, 'ted_go_to_position' , \ + frame_draw, 'frame_draw' , \ + progressbar_draw,'progressbar_draw' , \ + progressbar_progress, 'progressbar_progress' + + +public edit_box_draw as 'edit_box_draw' +;public edit_box_key as 'edit_box_key' + +public edit_box_mouse as 'edit_box_mouse' +public edit_box_set_text as 'edit_box_set_text' + +public check_box_draw2 as 'check_box_draw2' +public check_box_mouse2 as 'check_box_mouse2' +public init_checkbox2 as 'init_checkbox2' + +public progressbar_draw as 'progressbar_draw' +public progressbar_progress as 'progressbar_progress' + +public frame_draw as 'frame_draw' + +public scrollbar_v_draw as 'scrollbar_v_draw' +public scrollbar_v_mouse as 'scrollbar_v_mouse' +public scrollbar_h_draw as 'scrollbar_h_draw' +public scrollbar_h_mouse as 'scrollbar_h_mouse' + +public option_box_draw as 'option_box_draw' +public option_box_mouse as 'option_box_mouse' + +public menu_bar_draw as 'menu_bar_draw' +public menu_bar_mouse as 'menu_bar_mouse' +public menu_bar_activate as 'menu_bar_activate' + +public dynamic_button_draw as 'dynamic_button_draw' +public dynamic_button_mouse as 'dynamic_button_mouse' + +public path_show_prepare as 'path_show_prepare' +public path_show_draw as 'path_show_draw' + +public fb_draw_panel as 'filebrowse_draw' +public fb_mouse as 'filebrowse_mouse' +public fb_key as 'filebrowse_key' + +public ted_but_sumb_upper as 'ted_but_sumb_upper' +public ted_but_sumb_lover as 'ted_but_sumb_lover' +public ted_but_convert_by_table as 'ted_but_convert_by_table' +public ted_can_save as 'ted_can_save' +public ted_clear as 'ted_clear' +public ted_delete as 'ted_delete' +public ted_draw as 'ted_draw' +public ted_init as 'ted_init' +public ted_init_scroll_bars as 'ted_init_scroll_bars' +public ted_init_syntax_file as 'ted_init_syntax_file' +public ted_is_select as 'ted_is_select' +public ted_key as 'ted_key_asm' +public ted_mouse as 'ted_mouse' +public ted_open_file as 'ted_open_file_asm' +public ted_save_file as 'ted_save_file_asm' +public ted_text_add as '_ted_text_add' +public ted_but_select_word as 'ted_but_select_word' +public ted_but_cut as 'ted_but_cut' +public ted_but_copy as 'ted_but_copy' +public ted_but_paste as 'ted_but_paste' +public ted_but_undo as 'ted_but_undo' +public ted_but_redo as 'ted_but_redo' +public ted_but_reverse as 'ted_but_reverse' +public ted_but_find as 'ted_but_find' +public ted_but_replace as 'ted_but_replace' +public ted_text_colored as 'ted_text_colored_asm' +public ted_go_to_position as 'ted_go_to_position' + +public tl_data_init as 'tl_data_init' +public tl_data_clear as 'tl_data_clear' +public tl_info_clear as 'tl_info_clear' +public tl_key as 'tl_key_asm' +public tl_mouse as 'tl_mouse' +public tl_draw as 'tl_draw' +public tl_info_undo as 'tl_info_undo' +public tl_info_redo as 'tl_info_redo' +public tl_node_add as 'tl_node_add' +public tl_node_set_data as 'tl_node_set_data' +public tl_node_get_data as 'tl_node_get_data' +public tl_node_delete as 'tl_node_delete' +public tl_cur_beg as 'tl_cur_beg' +public tl_cur_next as 'tl_cur_next' +public tl_cur_perv as 'tl_cur_perv' +public tl_node_close_open as 'tl_node_close_open' +public tl_node_lev_inc as 'tl_node_lev_inc' +public tl_node_lev_dec as 'tl_node_lev_dec' +public tl_node_move_up as 'tl_node_move_up' +public tl_node_move_down as 'tl_node_move_down' +public tl_node_poi_get_info as 'tl_node_poi_get_info' +public tl_node_poi_get_next_info as 'tl_node_poi_get_next_info' +public tl_node_poi_get_data as 'tl_node_poi_get_data' +public tl_save_mem as 'tl_save_mem_asm' +public tl_load_mem as 'tl_load_mem_asm' +public tl_get_mem_size as 'tl_get_mem_size_asm' diff --git a/programs/develop/ktcc/trunk/lib/buf2d/Makefile b/programs/develop/ktcc/trunk/lib/buf2d/Makefile new file mode 100644 index 0000000000..eee026f811 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/buf2d/Makefile @@ -0,0 +1,10 @@ +all: + fasm buf2d.asm buf2d.o + ar -csr libbuf2d.a buf2d.o + +clean: + rm -f *.a + rm -f *.o + +install: + mv *.a ../../bin/lib diff --git a/programs/develop/ktcc/trunk/lib/buf2d/buf2d.asm b/programs/develop/ktcc/trunk/lib/buf2d/buf2d.asm new file mode 100644 index 0000000000..27a149b0b5 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/buf2d/buf2d.asm @@ -0,0 +1,90 @@ +format elf +use32 ; Tell compiler to use 32 bit instructions + +section '.text' executable + +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +purge section,mov,add,sub + +include '../../../../../dll.inc' + +;public lib_init as 'kolibri_buf2d_init' +;;; Returns 0 on success. -1 on failure. +public init_buf2d as 'kolibri_buf2d_init' + +proc init_buf2d + pusha + mcall 68,11 + stdcall dll.Load, @IMPORT + popa + ret +endp + +section '.data' writeable + +@IMPORT: +library lib_buf2d, 'buf2d.obj' + +import lib_buf2d, \ + libbuf2d_init, 'lib_init' , \ + buf2d_create, 'buf2d_create' , \ + buf2d_clear, 'buf2d_clear' , \ + buf2d_draw, 'buf2d_draw' , \ + buf2d_delete, 'buf2d_delete', \ + buf2d_rotate, 'buf2d_rotate', \ + buf2d_resize, 'buf2d_resize', \ + buf2d_line, 'buf2d_line', \ + buf2d_line_sm, 'buf2d_line_sm', \ + buf2d_rect_by_size, 'buf2d_rect_by_size', \ + buf2d_filled_rect_by_size, 'buf2d_filled_rect_by_size', \ + buf2d_circle, 'buf2d_circle', \ + buf2d_img_hdiv2, 'buf2d_img_hdiv2', \ + buf2d_img_wdiv2, 'buf2d_img_wdiv2', \ + buf2d_conv_24_to_8, 'buf2d_conv_24_to_8', \ + buf2d_conv_24_to_32, 'buf2d_conv_24_to_32', \ + buf2d_bit_blt, 'buf2d_bit_blt', \ + buf2d_bit_blt_transp, 'buf2d_bit_blt_transp', \ + buf2d_bit_blt_alpha, 'buf2d_bit_blt_alpha', \ + buf2d_curve_bezier, 'buf2d_curve_bezier', \ + buf2d_convert_text_matrix, 'buf2d_convert_text_matrix', \ + buf2d_draw_text, 'buf2d_draw_text', \ + buf2d_crop_color, 'buf2d_crop_color', \ + buf2d_offset_h, 'buf2d_offset_h', \ + buf2d_flood_fill, 'buf2d_flood_fill', \ + buf2d_set_pixel, 'buf2d_set_pixel', \ + buf2d_get_pixel, 'buf2d_get_pixel', \ + buf2d_flip_h, 'buf2d_flip_h', \ + buf2d_flip_v, 'buf2d_flip_v', \ + buf2d_filter_dither, 'buf2d_filter_dither' + +public libbuf2d_init as 'libimg_init' +public buf2d_create as 'buf2d_create_asm' +public buf2d_clear as 'buf2d_clear' +public buf2d_draw as 'buf2d_draw' +public buf2d_delete as 'buf2d_delete' +public buf2d_rotate as 'buf2d_rotate' +public buf2d_resize as 'buf2d_resize' +public buf2d_line as 'buf2d_line' +public buf2d_line_sm as 'buf2d_line_sm' +public buf2d_rect_by_size as 'buf2d_rect_by_size' +public buf2d_filled_rect_by_size as 'buf2d_filled_rect_by_size' +public buf2d_circle as 'buf2d_circle' +public buf2d_img_hdiv2 as 'buf2d_img_hdiv2' +public buf2d_img_wdiv2 as 'buf2d_img_wdiv2' +public buf2d_conv_24_to_8 as 'buf2d_conv_24_to_8' +public buf2d_conv_24_to_32 as 'buf2d_conv_24_to_32' +public buf2d_bit_blt as 'buf2d_bit_blt' +public buf2d_bit_blt_transp as 'buf2d_bit_blt_transp' +public buf2d_bit_blt_alpha as 'buf2d_bit_blt_alpha' +public buf2d_curve_bezier as 'buf2d_curve_bezier_asm' +public buf2d_convert_text_matrix as 'buf2d_convert_text_matrix' +public buf2d_draw_text as 'buf2d_draw_text' +public buf2d_crop_color as 'buf2d_crop_color' +public buf2d_offset_h as 'buf2d_offset_h' +public buf2d_flood_fill as 'buf2d_flood_fill' +public buf2d_set_pixel as 'buf2d_set_pixel' +public buf2d_get_pixel as 'buf2d_get_pixel' +public buf2d_flip_h as 'buf2d_flip_h' +public buf2d_flip_v as 'buf2d_flip_v' +public buf2d_filter_dither as 'buf2d_filter_dither' diff --git a/programs/develop/ktcc/trunk/lib/http/Makefile b/programs/develop/ktcc/trunk/lib/http/Makefile new file mode 100644 index 0000000000..8cdf824dac --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/Makefile @@ -0,0 +1,11 @@ +FASM = fasm + +OBJS = get.o head.o __lib__.o post.o receive.o send.o free.o + +%.o : %.asm + $(FASM) $< + +all: $(OBJS) + ar -rsc libhttp.a *.o + mv -f libhttp.a ../../bin/lib + rm -f *.o diff --git a/programs/develop/ktcc/trunk/lib/http/__lib__.asm b/programs/develop/ktcc/trunk/lib/http/__lib__.asm new file mode 100644 index 0000000000..eac1985673 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/__lib__.asm @@ -0,0 +1,9 @@ +format ELF + +include '__lib__.inc' + +section '.text' + +public lib_name + +lib_name db 0x55, 0xAA, lib_name_str, 0 diff --git a/programs/develop/ktcc/trunk/lib/http/__lib__.inc b/programs/develop/ktcc/trunk/lib/http/__lib__.inc new file mode 100644 index 0000000000..879a487980 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/__lib__.inc @@ -0,0 +1,2 @@ +lib_name equ @http.obj +lib_name_str equ '/sys/lib/http.obj' \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/http/free.asm b/programs/develop/ktcc/trunk/lib/http/free.asm new file mode 100644 index 0000000000..77e7ee108e --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/free.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_free +fun_str equ 'free' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/http/get.asm b/programs/develop/ktcc/trunk/lib/http/get.asm new file mode 100644 index 0000000000..d2a1132553 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/get.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_get +fun_str equ 'get' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/http/head.asm b/programs/develop/ktcc/trunk/lib/http/head.asm new file mode 100644 index 0000000000..5b7ac8b942 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/head.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_head +fun_str equ 'head' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/http/post.asm b/programs/develop/ktcc/trunk/lib/http/post.asm new file mode 100644 index 0000000000..cedf139378 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/post.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_post +fun_str equ 'post' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/http/receive.asm b/programs/develop/ktcc/trunk/lib/http/receive.asm new file mode 100644 index 0000000000..5bc616828c --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/receive.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_receive +fun_str equ 'receive' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/http/send.asm b/programs/develop/ktcc/trunk/lib/http/send.asm new file mode 100644 index 0000000000..3adfe2bb80 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/http/send.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ http_send +fun_str equ 'send' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/inputbox/InputBox.asm b/programs/develop/ktcc/trunk/lib/inputbox/InputBox.asm new file mode 100644 index 0000000000..deebcffe29 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/inputbox/InputBox.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ InputBox +fun_str equ 'InputBox' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/inputbox/__lib__.asm b/programs/develop/ktcc/trunk/lib/inputbox/__lib__.asm new file mode 100644 index 0000000000..eac1985673 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/inputbox/__lib__.asm @@ -0,0 +1,9 @@ +format ELF + +include '__lib__.inc' + +section '.text' + +public lib_name + +lib_name db 0x55, 0xAA, lib_name_str, 0 diff --git a/programs/develop/ktcc/trunk/lib/inputbox/__lib__.inc b/programs/develop/ktcc/trunk/lib/inputbox/__lib__.inc new file mode 100644 index 0000000000..d87c630bb6 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/inputbox/__lib__.inc @@ -0,0 +1,2 @@ +lib_name equ @inputbox.obj +lib_name_str equ '/sys/lib/inputbox.obj' \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/inputbox/make.bat b/programs/develop/ktcc/trunk/lib/inputbox/make.bat new file mode 100644 index 0000000000..1a12411771 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/inputbox/make.bat @@ -0,0 +1,5 @@ +fasm __lib__.asm +fasm InputBox.asm +kos32-ar -ru libinputbox.a *.o +del *.o +pause \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libimg/Makefile b/programs/develop/ktcc/trunk/lib/libimg/Makefile new file mode 100644 index 0000000000..25caaa1a7f --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libimg/Makefile @@ -0,0 +1,9 @@ +all: + fasm loadlibimg.asm + ar -csr libimg.a loadlibimg.o + +install: + mv libimg.a ../../bin/lib/libimg.a + +clean: + rm -f *.o *.a diff --git a/programs/develop/ktcc/trunk/lib/libimg/loadlibimg.asm b/programs/develop/ktcc/trunk/lib/libimg/loadlibimg.asm new file mode 100644 index 0000000000..ad2ddf73b6 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libimg/loadlibimg.asm @@ -0,0 +1,88 @@ +format elf +use32 ; Tell compiler to use 32 bit instructions + +; ELF section +section '.text' executable + + +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +purge section,mov,add,sub + +include '../../../../../dll.inc' + + +public init_libimg as 'kolibri_libimg_init' +;;; Returns 0 on success. -1 on failure. + +proc init_libimg +local retval dd ? + mov [retval], eax + pusha + mcall 68, 11 + test eax, eax + jnz @f + mov [retval], -1 + jmp exit_init_libimg +@@: + stdcall dll.Load, @IMPORT + test eax, eax + jz exit_init_libimg + mov [retval], -1 +exit_init_libimg: + popa + mov eax, [retval] + ret +endp + +; ELF section +section '.data' writeable + +@IMPORT: +library lib_libimg, 'libimg.obj' + +import lib_libimg, \ + libimg_init, 'lib_init' , \ + img_is_img, 'img_is_img' , \ + img_info, 'img_info' , \ + img_from_file, 'img_from_file', \ + img_to_file, 'img_to_file', \ + img_from_rgb, 'img_from_rgb', \ + img_to_rgb, 'img_to_rgb', \ + img_to_rgb2, 'img_to_rgb2', \ + img_decode, 'img_decode', \ + img_encode, 'img_encode', \ + img_create, 'img_create', \ + img_destroy, 'img_destroy', \ + img_destroy_layer, 'img_destroy_layer', \ + img_count, 'img_count', \ + img_lock_bits, 'img_lock_bits', \ + img_unlock_bits, 'img_unlock_bits', \ + img_flip, 'img_flip', \ + img_flip_layer, 'img_flip_layer', \ + img_rotate, 'img_rotate', \ + img_rotate_layer, 'img_rotate_layer', \ + img_draw, 'img_draw', \ + img_blend, 'img_blend', \ + img_convert, 'img_convert', \ + img_resize_data, 'img_resize_data', \ + img_scale, 'img_scale' + +public libimg_init as 'libimg_init' +public img_to_rgb as 'img_to_rgb' +public img_to_rgb2 as 'img_to_rgb2' +public img_decode as 'img_decode' +public img_encode as 'img_encode' +public img_create as 'img_create' +public img_destroy as 'img_destroy' +public img_destroy_layer as 'img_destroy_layer' +public img_count as 'img_count' +public img_flip as 'img_flip' +public img_flip_layer as 'img_flip_layer' +public img_rotate as 'img_rotate' +public img_rotate_layer as 'img_rotate_layer' +public img_draw as 'img_draw' +public img_blend as 'img_blend' +public img_convert as 'img_convert' +public img_resize_data as 'img_resize_data' +public img_scale as 'img_scale' diff --git a/programs/develop/ktcc/trunk/lib/libnetwork/Makefile b/programs/develop/ktcc/trunk/lib/libnetwork/Makefile new file mode 100644 index 0000000000..688aaa7184 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libnetwork/Makefile @@ -0,0 +1,7 @@ +all: + fasm network.asm + ar -csr libnetwork.a network.o +clean: + rm -f *.o *.a +install: + cp libnetwork.a ../../bin/lib diff --git a/programs/develop/ktcc/trunk/lib/libnetwork/network.asm b/programs/develop/ktcc/trunk/lib/libnetwork/network.asm new file mode 100644 index 0000000000..58da472707 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libnetwork/network.asm @@ -0,0 +1,50 @@ +format elf +use32 ; Tell compiler to use 32 bit instructions +; ELF section +section '.text' executable + + +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +purge section,mov,add,sub + +include '../../../../../dll.inc' + + +public lib_init as 'networklib_init' + + +proc lib_init +local retval dd ? + mov [retval], eax + pusha + mcall 68, 11 + test eax, eax + jnz @f + mov [retval], -1 + jmp exit_init_networklib +@@: + stdcall dll.Load, @IMPORT + test eax, eax + jz exit_init_networklib + mov [retval], -1 +exit_init_networklib: + popa + mov eax, [retval] + ret +endp + +section '.data' writeable +@IMPORT: +library networklib, 'network.obj' + +import networklib, \ + inet_addr, 'inet_addr', \ + inet_ntoa, 'inet_ntoa', \ + getaddrinfo, 'getaddrinfo', \ + freeaddrinfo, 'freeaddrinfo' + +public inet_addr as 'inet_addr' +public inet_ntoa as 'inet_ntoa' +public getaddrinfo as 'getaddrinfo' +public freeaddrinfo as 'freeaddrinfo' diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.asm b/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.asm new file mode 100644 index 0000000000..eac1985673 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.asm @@ -0,0 +1,9 @@ +format ELF + +include '__lib__.inc' + +section '.text' + +public lib_name + +lib_name db 0x55, 0xAA, lib_name_str, 0 diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.inc b/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.inc new file mode 100644 index 0000000000..c53f9e5523 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/__lib__.inc @@ -0,0 +1,2 @@ +lib_name equ @RASTERWORKS.OBJ +lib_name_str equ '/sys/lib/RASTERWORKS.OBJ' diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/charsFit.asm b/programs/develop/ktcc/trunk/lib/librasterworks/charsFit.asm new file mode 100644 index 0000000000..97dc8a6a58 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/charsFit.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ charsFit +fun_str equ 'charsFit' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/cntUTF-8.asm b/programs/develop/ktcc/trunk/lib/librasterworks/cntUTF-8.asm new file mode 100644 index 0000000000..6720eac7c0 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/cntUTF-8.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ countUTF8Z +fun_str equ 'cntUTF-8' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/drawText.asm b/programs/develop/ktcc/trunk/lib/librasterworks/drawText.asm new file mode 100644 index 0000000000..9662bc0372 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/drawText.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ drawText +fun_str equ 'drawText' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/make.bat b/programs/develop/ktcc/trunk/lib/librasterworks/make.bat new file mode 100644 index 0000000000..c70d398a69 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/make.bat @@ -0,0 +1,7 @@ +fasm __lib__.asm +fasm mb_create.asm +fasm mb_reinit.asm +fasm mb_setfunctions.asm +kos32-ar -ru libmsgbox.a *.o +del *.o +pause diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/makefile b/programs/develop/ktcc/trunk/lib/librasterworks/makefile new file mode 100644 index 0000000000..078c46ff73 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/makefile @@ -0,0 +1,9 @@ +all: + fasm __lib__.asm + fasm charsFit.asm + fasm cntUTF-8.asm + fasm drawText.asm + fasm strWidth.asm + ar -crs librasterworks.a *.o +clean: + rm -f *.o diff --git a/programs/develop/ktcc/trunk/lib/librasterworks/strWidth.asm b/programs/develop/ktcc/trunk/lib/librasterworks/strWidth.asm new file mode 100644 index 0000000000..81a3d7e553 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/librasterworks/strWidth.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ strWidth +fun_str equ 'strWidth' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/msgbox/__lib__.asm b/programs/develop/ktcc/trunk/lib/msgbox/__lib__.asm new file mode 100644 index 0000000000..eac1985673 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/__lib__.asm @@ -0,0 +1,9 @@ +format ELF + +include '__lib__.inc' + +section '.text' + +public lib_name + +lib_name db 0x55, 0xAA, lib_name_str, 0 diff --git a/programs/develop/ktcc/trunk/lib/msgbox/__lib__.inc b/programs/develop/ktcc/trunk/lib/msgbox/__lib__.inc new file mode 100644 index 0000000000..eef5773a7d --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/__lib__.inc @@ -0,0 +1,2 @@ +lib_name equ @msgbox.obj +lib_name_str equ '/sys/lib/msgbox.obj' diff --git a/programs/develop/ktcc/trunk/lib/msgbox/make.bat b/programs/develop/ktcc/trunk/lib/msgbox/make.bat new file mode 100644 index 0000000000..c70d398a69 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/make.bat @@ -0,0 +1,7 @@ +fasm __lib__.asm +fasm mb_create.asm +fasm mb_reinit.asm +fasm mb_setfunctions.asm +kos32-ar -ru libmsgbox.a *.o +del *.o +pause diff --git a/programs/develop/ktcc/trunk/lib/msgbox/makefile b/programs/develop/ktcc/trunk/lib/msgbox/makefile new file mode 100644 index 0000000000..13ccbefce7 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/makefile @@ -0,0 +1,8 @@ +all: + fasm __lib__.asm + fasm mb_create.asm + fasm mb_reinit.asm + fasm mb_setfunctions.asm + kos32-ar -ru libmsgbox.a *.o +clean: + rm -f *.o diff --git a/programs/develop/ktcc/trunk/lib/msgbox/mb_create.asm b/programs/develop/ktcc/trunk/lib/msgbox/mb_create.asm new file mode 100644 index 0000000000..a94f017c9b --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/mb_create.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ msgbox_create +fun_str equ 'mb_create' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/msgbox/mb_reinit.asm b/programs/develop/ktcc/trunk/lib/msgbox/mb_reinit.asm new file mode 100644 index 0000000000..2cf8a0db27 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/mb_reinit.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ msgbox_reinit +fun_str equ 'mb_reinit' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/msgbox/mb_setfunctions.asm b/programs/develop/ktcc/trunk/lib/msgbox/mb_setfunctions.asm new file mode 100644 index 0000000000..20ae112cbf --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/msgbox/mb_setfunctions.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ msgbox_setfunctions +fun_str equ 'mb_setfunctions' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/lib/proc_lib/Makefile b/programs/develop/ktcc/trunk/lib/proc_lib/Makefile new file mode 100644 index 0000000000..0e373d6650 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/proc_lib/Makefile @@ -0,0 +1,7 @@ +all: + fasm loadproclib.asm + ar -csr libdialog.a loadproclib.o +install: + mv libdialog.a ../../bin/lib +clean: + rm -f *.o *.a diff --git a/programs/develop/ktcc/trunk/lib/proc_lib/loadproclib.asm b/programs/develop/ktcc/trunk/lib/proc_lib/loadproclib.asm new file mode 100644 index 0000000000..d60a8c7842 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/proc_lib/loadproclib.asm @@ -0,0 +1,39 @@ + +format elf +use32 ; Tell compiler to use 32 bit instructions + +section '.text' executable ; Keep this line before includes or GCC messes up call addresses + +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +purge section,mov,add,sub + +include '../../../../../dll.inc' + +public init_proclib as 'kolibri_dialog_init' +;;; Returns 0 on success. -1 on failure. + +proc init_proclib + pusha + mcall 68,11 + stdcall dll.Load, @IMPORT + popa + ret +endp + +section '.data' writeable + +@IMPORT: +library lib_boxlib, 'proc_lib.obj' + +import lib_boxlib, \ + OpenDialog_init, 'OpenDialog_init' , \ + OpenDialog_start, 'OpenDialog_start' , \ + ColorDialog_init, 'ColorDialog_init' , \ + ColorDialog_start, 'ColorDialog_start' + +public OpenDialog_init as 'OpenDialog_init' +public OpenDialog_start as 'OpenDialog_start' + +public ColorDialog_init as 'ColorDialog_init' +public ColorDialog_start as 'ColorDialog_start' diff --git a/programs/develop/ktcc/trunk/lib/sample/__lib__.asm b/programs/develop/ktcc/trunk/lib/sample/__lib__.asm new file mode 100644 index 0000000000..eac1985673 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/sample/__lib__.asm @@ -0,0 +1,9 @@ +format ELF + +include '__lib__.inc' + +section '.text' + +public lib_name + +lib_name db 0x55, 0xAA, lib_name_str, 0 diff --git a/programs/develop/ktcc/trunk/lib/sample/__lib__.inc b/programs/develop/ktcc/trunk/lib/sample/__lib__.inc new file mode 100644 index 0000000000..96b5a49c17 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/sample/__lib__.inc @@ -0,0 +1,2 @@ +lib_name equ @sample.obj +lib_name_str equ '/sys/lib/sample.obj' \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/sample/howto.txt b/programs/develop/ktcc/trunk/lib/sample/howto.txt new file mode 100644 index 0000000000..cfd9ed8851 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/sample/howto.txt @@ -0,0 +1,13 @@ +Create new library wrapper + +1. Redefine lib_name and lib_name_str in __lib__.inc +2. Create file for each symbol, basing on sample_symbol.asm and for each: +2.1. Redefine fun to *your_symbol_name* +2.2. Redefine fun_name to '*your_symbol_name*' (with quotes, this is ASCII string, no trailing zero need) +3. Change make.bat as appropriate + +ACHTUNG: lib_name need to be unique in project, this is why I started it with "@" and put "." into. +ACHTUNG: lib_name_str must be at least 5 symbols long, if is not, fix this adding padding to the path. + +Now you can create header file and declare extern variables pointers-to-functions, and then link your app with -llibname (put .a file in lib folder). +For examples of such header files, look at include/kos/inputbox.h, include/kos/console.h and include/kos/http.h. IMO, first one is the simplest. diff --git a/programs/develop/ktcc/trunk/lib/sample/make.bat b/programs/develop/ktcc/trunk/lib/sample/make.bat new file mode 100644 index 0000000000..4b82311b24 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/sample/make.bat @@ -0,0 +1,5 @@ +fasm __lib__.asm +fasm sample_symbol.asm +kos32-ar -ru libsample.a *.o +del *.o +pause \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/sample/sample_symbol.asm b/programs/develop/ktcc/trunk/lib/sample/sample_symbol.asm new file mode 100644 index 0000000000..613ad69a91 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/sample/sample_symbol.asm @@ -0,0 +1,18 @@ +format ELF + +include "__lib__.inc" + +fun equ sample_symbol +fun_str equ 'sample_symbol' + +section '.text' + +fun_name db fun_str, 0 + +section '.data' + +extrn lib_name +public fun + +fun dd fun_name +lib dd lib_name diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/boxlib.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/boxlib.h index 9476a2f96d..3162f1b361 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/boxlib.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/boxlib.h @@ -3,12 +3,10 @@ #ifndef KOLIBRI_BOXLIB_H #define KOLIBRI_BOXLIB_H -#include "sys/ksys.h" -#include -#include - typedef unsigned color_t; +extern int kolibri_boxlib_init(void); + /* flags meaning */ #define ed_figure_only 0b1000000000000000 // РѕРґРЅРё символы #define ed_always_focus 0b100000000000000 // всегда СЃ РєСѓСЂСЃРѕСЂРѕРј (фокусом) @@ -68,10 +66,10 @@ typedef struct { uint32_t ar_offset; } __attribute__ ((__packed__)) scrollbar; -extern void __stdcall (*scrollbar_h_draw)(scrollbar*); -extern void __stdcall (*scrollbar_h_mouse)(scrollbar*); -extern void __stdcall (*scrollbar_v_draw)(scrollbar*); -extern void __stdcall (*scrollbar_v_mouse)(scrollbar*); +extern void (*scrollbar_h_draw __attribute__((__stdcall__)))(scrollbar*); +extern void (*scrollbar_h_mouse __attribute__((__stdcall__)))(scrollbar*); +extern void (*scrollbar_v_draw __attribute__((__stdcall__)))(scrollbar*); +extern void (*scrollbar_v_mouse __attribute__((__stdcall__)))(scrollbar*); // CHECKBOX typedef struct { @@ -88,9 +86,9 @@ typedef struct { unsigned int size_of_str; }check_box; -extern void __stdcall (*check_box_draw2)(check_box *); -extern void __stdcall (*check_box_mouse2)(check_box *); -extern void __stdcall (*init_checkbox2)(check_box *); +extern void (*check_box_draw2 __attribute__((__stdcall__)))(check_box *); +extern void (*check_box_mouse2 __attribute__((__stdcall__)))(check_box *); +extern void (*init_checkbox2 __attribute__((__stdcall__)))(check_box *); // DBUTTON typedef struct { @@ -110,8 +108,8 @@ typedef struct { uint32_t click; // clicked - 1, zero it after tested } pict_button; -extern void __stdcall (*dynamic_button_draw)(pict_button *); -extern void __stdcall (*dynamic_button_mouse)(pict_button *); +extern void (*dynamic_button_draw __attribute__((__stdcall__)))(pict_button *); +extern void (*dynamic_button_mouse __attribute__((__stdcall__)))(pict_button *); // EDITBOX @@ -143,20 +141,10 @@ typedef struct edit_box_t { }edit_box; #pragma pack(pop) -extern void __stdcall (*edit_box_draw)(edit_box *) __asm__("edit_box"); -extern void __stdcall (*edit_box_key)(edit_box *, unsigned int key_val); -extern void __stdcall (*edit_box_mouse)(edit_box *); -extern void __stdcall (*edit_box_set_text)(edit_box *, char *); - -void edit_box_key_safe(edit_box *e, ksys_oskey_t ch) { - int a=3; - asm_inline ( - "pushl %2 \n\t" - "call *%1 \n\t" - ::"a"(ch.val), "m"(edit_box_key), "m"(e) - :"memory" - ); -} +extern void (*edit_box_draw __attribute__((__stdcall__)))(edit_box *); +extern void edit_box_key (edit_box *, unsigned int key_val)__attribute__((__stdcall__)); +extern void (*edit_box_mouse __attribute__((__stdcall__)))(edit_box *); +extern void (*edit_box_set_text __attribute__((__stdcall__)))(edit_box *, char *); // FRAME typedef struct { @@ -174,7 +162,7 @@ typedef struct { color_t font_bg_color; }frame; -extern void __stdcall (*frame_draw)(frame *); +extern void (*frame_draw)(frame *); // MENUBAR typedef struct @@ -215,9 +203,9 @@ typedef struct uint32_t get_mouse_flag; } menubar; -extern void __stdcall (*menu_bar_draw)(menubar *); -extern void __stdcall (*menu_bar_mouse)(menubar *); -extern void __stdcall (*menu_bar_activate)(menubar *); +extern void (*menu_bar_draw)(menubar *); +extern void (*menu_bar_mouse)(menubar *); +extern void (*menu_bar_activate)(menubar *); // OPTIONBOX typedef struct option_box_t { @@ -234,8 +222,8 @@ typedef struct option_box_t { uint32_t flags; } __attribute__ ((__packed__)) option_box; -extern void __stdcall (*option_box_draw)(option_box **); -extern void __stdcall (*option_box_mouse)(option_box **); +extern void (*option_box_draw __attribute__((__stdcall__)))(option_box **); +extern void (*option_box_mouse __attribute__((__stdcall__)))(option_box **); // PATHSHOW typedef struct { @@ -252,8 +240,8 @@ typedef struct { uint32_t temp_text_length; } __attribute__ ((__packed__)) pathview; -extern void __stdcall (*path_show_prepare)(pathview *); -extern void __stdcall (*path_show_draw)(pathview *); +extern void (*path_show_prepare __attribute__((__stdcall__)))(pathview *); +extern void (*path_show_draw __attribute__((__stdcall__)))(pathview *); // PROGRESSBAR typedef struct { @@ -270,7 +258,8 @@ typedef struct { unsigned int frame_color; } progressbar; -extern void __stdcall (*progressbar_draw)(progressbar *); -extern void __stdcall (*progressbar_progress)(progressbar *); +extern void (*progressbar_draw __attribute__((__stdcall__)))(progressbar *); +extern void (*progressbar_progress __attribute__((__stdcall__)))(progressbar *); + #endif /* KOLIBRI_BOXLIB_H */ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/buf2d.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/buf2d.h index cdba64648a..74b986d2b1 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/buf2d.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/buf2d.h @@ -2,8 +2,11 @@ #define KOLIBRI_BUF2D_H #include -#include -#include +/*ToDo + * voxel function + */ + +extern int kolibri_buf2d_init(void); typedef struct { unsigned int *buf_pointer; @@ -13,7 +16,7 @@ typedef struct { unsigned int height; unsigned int bgcolor; uint8_t color_bit; -} __attribute__ ((__packed__))buf2d_struct; +} __attribute__ ((__packed__))buf2d_struct; enum BUF2D_ALGORITM_FILTR { SIERRA_LITE, @@ -30,12 +33,12 @@ enum BUF2D_OPT_CROP { BUF2D_OPT_CROP_RIGHT = 8 }; -extern void __stdcall (*buf2d_create_asm)(buf2d_struct *); -extern void __stdcall (*buf2d_curve_bezier_asm)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_create_asm __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_curve_bezier_asm __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); buf2d_struct* buf2d_create(uint16_t tlx, uint16_t tly, unsigned int sizex, unsigned int sizey, unsigned int font_bgcolor, uint8_t color_bit) { - buf2d_struct *new_buf2d_struct = (buf2d_struct *)_ksys_alloc(sizeof(buf2d_struct)); + buf2d_struct *new_buf2d_struct = (buf2d_struct *)malloc(sizeof(buf2d_struct)); new_buf2d_struct -> left = tlx; new_buf2d_struct -> top = tly; new_buf2d_struct -> width = sizex; @@ -51,31 +54,30 @@ void buf2d_curve_bezier(buf2d_struct *buf, unsigned int p0_x, unsigned int p0_y, buf2d_curve_bezier_asm(buf, (p0_x<<16)+p0_y, (p1_x<<16)+p1_y, (p2_x<<16)+p2_y, color); } -extern void __stdcall (*buf2d_draw)(buf2d_struct *); -extern void __stdcall (*buf2d_clear)(buf2d_struct *, unsigned int); -extern void __stdcall (*buf2d_delete)(buf2d_struct *); -extern void __stdcall (*buf2d_rotate)(buf2d_struct *, unsigned int); -extern void __stdcall (*buf2d_resize)(buf2d_struct *, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_line)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_line_sm)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_rect_by_size)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_filled_rect_by_size)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_circle)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_img_hdiv2)(buf2d_struct *); -extern void __stdcall (*buf2d_img_wdiv2)(buf2d_struct *); -extern void __stdcall (*buf2d_conv_24_to_8)(buf2d_struct *, unsigned int); -extern void __stdcall (*buf2d_conv_24_to_32)(buf2d_struct *, unsigned int); -extern void __stdcall (*buf2d_bit_blt_transp)(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); -extern void __stdcall (*buf2d_bit_blt_alpha)(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); -extern void __stdcall (*buf2d_convert_text_matrix)(buf2d_struct *); -extern void __stdcall (*buf2d_draw_text)(buf2d_struct *, buf2d_struct *, const char *, unsigned int, unsigned int); -extern void __stdcall (*buf2d_crop_color)(buf2d_struct *, unsigned int, unsigned int); -extern void __stdcall (*buf2d_offset_h)(buf2d_struct *, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_flood_fill)(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); -extern void __stdcall (*buf2d_set_pixel)(buf2d_struct *, unsigned int, unsigned int, unsigned int); -extern unsigned __stdcall (*buf2d_get_pixel)(buf2d_struct *, unsigned int, unsigned int); -extern void __stdcall (*buf2d_flip_h)(buf2d_struct *); -extern void __stdcall (*buf2d_flip_v)(buf2d_struct *); -extern void __stdcall (*buf2d_filter_dither)(buf2d_struct *, unsigned int); - +extern void (*buf2d_draw __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_clear __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_delete __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_rotate __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_resize __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_line __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_line_sm __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_rect_by_size __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_filled_rect_by_size __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_circle __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_img_hdiv2 __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_img_wdiv2 __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_conv_24_to_8 __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_conv_24_to_32 __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_bit_blt_transp __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); +extern void (*buf2d_bit_blt_alpha __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); +extern void (*buf2d_convert_text_matrix __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_draw_text __attribute__((__stdcall__)))(buf2d_struct *, buf2d_struct *, const char *, unsigned int, unsigned int); +extern void (*buf2d_crop_color __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int); +extern void (*buf2d_offset_h __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_flood_fill __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_set_pixel __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern unsigned int (*buf2d_get_pixel __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int); +extern void (*buf2d_flip_h __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_flip_v __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_filter_dither __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); #endif /* KOLIBRI_BUF2D_H */ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/dialog.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/dialog.h index 5d81066403..3cb7759e0c 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/dialog.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/dialog.h @@ -2,7 +2,6 @@ #define KOLIBRI_DIALOG_H #include - #define NOT_SUCCESS 0 #define SUCCESS 1 @@ -116,10 +115,12 @@ color_dialog* kolibri_new_color_dialog(unsigned int type, unsigned short tlx, un return new_colordialog; } -extern void __stdcall (*OpenDialog_init)(open_dialog *); -extern void __stdcall (*OpenDialog_start)(open_dialog *); +extern void kolibri_dialog_init(); -extern void __stdcall (*ColorDialog_init)(color_dialog *); -extern void __stdcall (*ColorDialog_start)(color_dialog *); +extern void (*OpenDialog_init __attribute__((__stdcall__)))(open_dialog *); +extern void (*OpenDialog_start __attribute__((__stdcall__)))(open_dialog *); + +extern void (*ColorDialog_init __attribute__((__stdcall__)))(color_dialog *); +extern void (*ColorDialog_start __attribute__((__stdcall__)))(color_dialog *); #endif diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/gb.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/gb.h index 564bbd7d6c..7e0ae75bdb 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/gb.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/gb.h @@ -1,13 +1,11 @@ -#ifndef KOLIBRI_GB_H -#define KOLIBRI_GB_H #pragma pack(push, 1) typedef struct { - int w; - int h; - char *bmp; - char *alpha; +int w; +int h; +char *bmp; +char *alpha; } GB_BMP; #pragma pack(pop) @@ -19,5 +17,3 @@ void gb_bar(GB_BMP *b, int x, int y, int w, int h, unsigned c); void gb_circle(GB_BMP *b, int x, int y, int r, unsigned c); void gb_image_set(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h); void gb_image_set_t(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h, unsigned c); - -#endif // KOLIBRI_GB_H \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/http.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/http.h index 78b0001c43..ac296576e3 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/http.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/http.h @@ -5,10 +5,11 @@ Adapted for TCC's dynamic API by Magomed Kostoev, 2020 */ -#ifndef KOLIBRI_HTTP_H -#define KOLIBRI_HTTP_H +#ifndef _HTTP_H_ +#define _HTTP_H_ -#include +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) // Bitflags for http_msg.flags // status @@ -79,7 +80,7 @@ typedef struct http_msg_s { Initiates a HTTP connection, using 'GET' method. Returns NULL on error, identifier otherwise. */ -extern http_msg * __stdcall (*http_get)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); +extern http_msg * stdcall (*http_get)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); /* url = pointer to ASCIIZ URL @@ -90,7 +91,7 @@ extern http_msg * __stdcall (*http_get)(const char *url, http_msg *identifier, u Initiate a HTTP connection, using 'HEAD' method. Returns NULL on error, identifier otherwise. */ -extern http_msg * __stdcall (*http_head)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); +extern http_msg * stdcall (*http_head)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); /* url = pointer to ASCIIZ URL @@ -105,7 +106,7 @@ extern http_msg * __stdcall (*http_head)(const char *url, http_msg *identifier, using system function 75, 6. Returns 0 on error, identifier otherwise */ -extern http_msg * __stdcall (*http_post)(const char *url, http_msg *identifier, unsigned flags, const char *add_header, +extern http_msg * stdcall (*http_post)(const char *url, http_msg *identifier, unsigned flags, const char *add_header, const char *content_type, unsigned content_length); /* @@ -127,7 +128,7 @@ extern http_msg * __stdcall (*http_post)(const char *url, http_msg *identifier, In content_length you'll find the length of the content. In content_received, you'll find the number of content bytes already received. */ -extern int __stdcall (*http_receive)(http_msg *identifier); +extern int stdcall (*http_receive)(http_msg *identifier); /* identifier = identifier which one of the previous functions returned @@ -136,17 +137,17 @@ extern int __stdcall (*http_receive)(http_msg *identifier); This procedure can be used to send data to the server (POST) Returns number of bytes sent, -1 on error */ -extern int __stdcall (*http_send)(http_msg *identifier, void *dataptr, unsigned datalength); +extern int stdcall (*http_send)(http_msg *identifier, void *dataptr, unsigned datalength); /* Sometimes the http_receive function receives incomplete data. If you have the same problem then a macro can help you: */ -extern int __stdcall (*http_free)(http_msg *identifier); +extern int stdcall (*http_free)(http_msg *identifier); /* Free unused data */ #define http_long_receive(x) while(http_receive(x)){}; -#endif // KOLIBRI_HTTP_H +#endif // _HTTP_H_ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/inputbox.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/inputbox.h index 3144341a2d..1b2a6c2041 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/inputbox.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/inputbox.h @@ -5,12 +5,13 @@ Adapted for TCC's dynamic API by Magomed Kostoev, 2020 */ -#ifndef KOLIBRI_INPUTBOX_H -#define KOLIBRI_INPUTBOX_H +#ifndef __KOS__INPUTBOX__H________ +#define __KOS__INPUTBOX__H________ -#include +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) -extern unsigned __stdcall (*InputBox)(void* Buffer, char* Caption, char* Prompt, char* Default, +extern unsigned stdcall (*InputBox)(void* Buffer, char* Caption, char* Prompt, char* Default, unsigned long Flags, unsigned long BufferSize, void* RedrawProc); -#endif // KOLIBRI_INPUTBOX_H +#endif // __KOS__INPUTBOX__H________ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/libimg.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/libimg.h index 355fd84ba4..3b41978be2 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/libimg.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/libimg.h @@ -6,6 +6,10 @@ #include #include +extern int kolibri_libimg_init(void); + +#define _stdcall __attribute__((__stdcall__)) + //list of format id's #define LIBIMG_FORMAT_BMP 1 #define LIBIMG_FORMAT_ICO 2 @@ -95,23 +99,23 @@ typedef struct{ #define ROTATE_90_CCW ROTATE_270_CW #define ROTATE_270_CCW ROTATE_90_CW -extern Image* __stdcall (*img_decode)(void* file_data, uint32_t size, uint32_t b_color); -extern Image* __stdcall (*img_encode)(Image* img, uint32_t length, uint32_t option); -extern Image* __stdcall (*img_create)(uint32_t width, uint32_t height, uint32_t type); -extern void __stdcall (*img_to_rgb2)(Image* img, void *rgb_data); -extern Image* __stdcall (*img_to_rgb)(Image* img); -extern bool __stdcall (*img_flip)(Image* img, uint32_t flip); -extern bool __stdcall (*img_flip_layer)(Image *img, uint32_t flip); -extern bool __stdcall (*img_rotate)(Image *img, uint32_t rotate); -extern bool __stdcall (*img_rotate_layer)(Image* data, uint32_t rotate); -extern void __stdcall (*img_draw)(Image *img, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t xoff, uint32_t yoff); -extern int32_t __stdcall (*img_count)(Image *img); -extern bool __stdcall (*img_destroy)(Image *img); -extern bool __stdcall (*img_destroy_layer)(Image* img); -extern Image* __stdcall (*img_blend)(Image* dst, Image* src, uint32_t out_x, uint32_t out_y, uint32_t in_x, uint32_t in_y, uint32_t width, uint32_t height); -extern Image* __stdcall (*img_convert)(Image *src, Image *dst, uint32_t dst_type, uint32_t, uint32_t); -extern Image* __stdcall (*img_resize_data)(Image *src, uint32_t width, uint32_t height); -extern Image* __stdcall (*img_scale)(Image* src, uint32_t crop_x, uint32_t crop_y, uint32_t crop_width, uint32_t crop_height, Image* dst, uint32_t scale_type, uint32_t inter, uint32_t new_width, uint32_t new_height); +extern Image* (*img_decode _stdcall)(void* file_data, uint32_t size, uint32_t b_color); +extern Image* (*img_encode _stdcall)(Image* img, uint32_t length, uint32_t option); +extern Image* (*img_create _stdcall)(uint32_t width, uint32_t height, uint32_t type); +extern void (*img_to_rgb2 _stdcall)(Image* img, void *rgb_data); +extern Image* (*img_to_rgb _stdcall)(Image* img); +extern bool (*img_flip _stdcall)(Image* img, uint32_t flip); +extern bool (*img_flip_layer _stdcall)(Image *img, uint32_t flip); +extern bool (*img_rotate _stdcall)(Image *img, uint32_t rotate); +extern bool (*img_rotate_layer _stdcall)(Image* data, uint32_t rotate); +extern void (*img_draw _stdcall)(Image *img, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t xoff, uint32_t yoff); +extern int32_t (*img_count _stdcall)(Image *img); +extern bool (*img_destroy _stdcall)(Image *img); +extern bool (*img_destroy_layer _stdcall)(Image* img); +extern Image* (*img_blend _stdcall)(Image* dst, Image* src, uint32_t out_x, uint32_t out_y, uint32_t in_x, uint32_t in_y, uint32_t width, uint32_t height); +extern Image* (*img_convert _stdcall)(Image *src, Image *dst, uint32_t dst_type, uint32_t, uint32_t); +extern Image* (*img_resize_data _stdcall)(Image *src, uint32_t width, uint32_t height); +extern Image* (*img_scale _stdcall)(Image* src, uint32_t crop_x, uint32_t crop_y, uint32_t crop_width, uint32_t crop_height, Image* dst, uint32_t scale_type, uint32_t inter, uint32_t new_width, uint32_t new_height); void img_fill_color(Image* img, uint32_t width, uint32_t height, uint32_t color){ for (uint32_t i = 0; i < width*height; i++) { diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/msgbox.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/msgbox.h index c08d9be086..b0efe61f69 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/msgbox.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/msgbox.h @@ -1,11 +1,11 @@ #ifndef KOLIBRI_MSGBOX_H #define KOLIBRI_MSGBOX_H - #include #include #include #include + typedef struct { uint8_t retval; // 0 - win closed, 1 to n - button num, also default button on start uint8_t reserv; @@ -16,16 +16,16 @@ typedef struct { typedef void (*msgbox_callback)(void); -extern void __stdcall (*mb_create)(msgbox *, void *thread); // clears callbacks, ! if fix lib, we can return eax as of Fn51 -extern void __stdcall (*mb_setfunctions)(msgbox_callback*); // must be called immediately after create, zero-ended array -extern void __stdcall (*mb_reinit)(msgbox *); // recalc sizes when structure changes, called auto when MsgBoxCreate +extern void (*msgbox_create __attribute__((__stdcall__)))(msgbox *, void *thread); // clears callbacks, ! if fix lib, we can return eax as of Fn51 +extern void (*msgbox_setfunctions __attribute__((__stdcall__)))(msgbox_callback*); // must be called immediately after create, zero-ended array +extern void (*msgbox_reinit __attribute__((__stdcall__)))(msgbox *) ; // recalc sizes when structure changes, called auto when MsgBoxCreate static inline msgbox* kolibri_new_msgbox(char* title, char* text, int def_but, ...) /// text can be multilined by code 13 = "\r" /// def_but - highlighted and used on Enter (if zero - default is [X]), user may use Tabs or Arrows /// last params are buttons text, max 8. last must set as NULL { - va_list vl = 0; + va_list vl=0; va_start(vl, def_but); msgbox* box = calloc(sizeof(msgbox), 1); box->retval = (uint8_t)def_but; @@ -48,8 +48,8 @@ static inline msgbox* kolibri_new_msgbox(char* title, char* text, int def_but, . static inline void kolibri_start_msgbox(msgbox* box, msgbox_callback cb[]) { - mb_create(box, &box->top_stack); - if (cb) mb_setfunctions(cb); + (*msgbox_create)(box, &box->top_stack); + if (cb) (*msgbox_setfunctions)(cb); } #endif diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/network.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/network.h index 0f48e4ba7a..3ac45bdebf 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/network.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/network.h @@ -1,9 +1,11 @@ -#ifndef KOLIBRI_NETWORK_H -#define KOLIBRI_NETWORK_H +#ifndef __NETWORK_H +#define __NETWORK_H #include #include +#define stdcall __attribute__ ((stdcall)) + #define EAI_ADDRFAMILY 1 #define EAI_AGAIN 2 #define EAI_BADFLAGS 3 @@ -46,9 +48,10 @@ struct addrinfo { }; #pragma pack(pop) -extern int __stdcall (*inet_addr)(const char* hostname); -extern char* __stdcall (*inet_ntoa)(int ip_addr); -extern int __stdcall (*getaddrinfo)(const char* hostname, const char* servname, const struct addrinfo* hints, struct addrinfo** res); -extern void __stdcall (*freeaddrinfo)(struct addrinfo* ai); +extern int networklib_init (); +extern int stdcall (*inet_addr)(const char* hostname); +extern char* stdcall (*inet_ntoa)(int ip_addr); +extern int stdcall (*getaddrinfo)(const char* hostname, const char* servname, const struct addrinfo* hints, struct addrinfo** res); +extern void stdcall (*freeaddrinfo)(struct addrinfo* ai); #endif diff --git a/programs/develop/ktcc/trunk/libc.obj/include/clayer/rasterworks.h b/programs/develop/ktcc/trunk/libc.obj/include/clayer/rasterworks.h index 7b842a9fcb..4c0aea79d2 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/clayer/rasterworks.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/clayer/rasterworks.h @@ -1,11 +1,11 @@ #ifndef KOLIBRI_RASTERWORKS_H #define KOLIBRI_RASTERWORKS_H -#include +//extern int kolibri_rasterworks_init(void); -extern void __stdcall (*drawText)(void *canvas, int x, int y, const char *string, int charQuantity, int fontColor, int params); -extern int __stdcall (*countUTF8Z)(const char *string, int byteQuantity) __asm__("cntUTF-8"); -extern int __stdcall (*charsFit)(int areaWidth, int charHeight); -extern int __stdcall (*strWidth)(int charQuantity, int charHeight); +extern void (*drawText __attribute__((__stdcall__)))(void *canvas, int x, int y, const char *string, int charQuantity, int fontColor, int params); +extern int (*countUTF8Z __attribute__((__stdcall__)))(const char *string, int byteQuantity); +extern int (*charsFit __attribute__((__stdcall__)))(int areaWidth, int charHeight); +extern int (*strWidth __attribute__((__stdcall__)))(int charQuantity, int charHeight); -#endif /* KOLIBRI_RASTERWORKS_H */ \ No newline at end of file +#endif /* KOLIBRI_RASTERWORKS_H */ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/conio.h b/programs/develop/ktcc/trunk/libc.obj/include/conio.h index 03fda9a338..92c125d981 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/conio.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/conio.h @@ -11,6 +11,9 @@ Adapted for tcc by Siemargl, 2016 #include +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) + /* console.obj exports the following functions */ @@ -35,25 +38,25 @@ typedef unsigned short word; /* 16-bit unsigned integer */ extern int _FUNC(con_init)(void); extern int _FUNC(con_init_opt)(dword wnd_width, dword wnd_height, dword scr_width, dword scr_height, const char* title); -extern void __stdcall _FUNC((*con_exit))(int bCloseWindow); -extern void __stdcall _FUNC((*con_set_title))(const char* title); -extern void __stdcall _FUNC((*con_write_asciiz))(const char* str); -extern void __stdcall _FUNC((*con_write_string))(const char* str, dword length); -extern int __cdecl _FUNC((*con_printf))(const char* format, ...); -extern dword __stdcall _FUNC((*con_get_flags))(void); -extern dword __stdcall _FUNC((*con_set_flags))(dword new_flags); -extern int __stdcall _FUNC((*con_get_font_height))(void); -extern int __stdcall _FUNC((*con_get_cursor_height))(void); -extern int __stdcall _FUNC((*con_set_cursor_height))(int new_height); -extern int __stdcall _FUNC((*con_getch))(void); -extern word __stdcall _FUNC((*con_getch2))(void); -extern int __stdcall _FUNC((*con_kbhit))(void); -extern char* __stdcall _FUNC((*con_gets))(char* str, int n); -typedef int __stdcall _FUNC(* con_gets2_callback)(int keycode, char** pstr, int* pn, int* ppos); -extern char* __stdcall _FUNC((*con_gets2))(con_gets2_callback callback, char* str, int n); -extern void __stdcall _FUNC((*con_cls))(); -extern void __stdcall _FUNC((*con_get_cursor_pos))(int* px, int* py); -extern void __stdcall _FUNC((*con_set_cursor_pos))(int x, int y); +extern void stdcall _FUNC((*con_exit))(int bCloseWindow); +extern void stdcall _FUNC((*con_set_title))(const char* title); +extern void stdcall _FUNC((*con_write_asciiz))(const char* str); +extern void stdcall _FUNC((*con_write_string))(const char* str, dword length); +extern int cdecl _FUNC((*con_printf))(const char* format, ...); +extern dword stdcall _FUNC((*con_get_flags))(void); +extern dword stdcall _FUNC((*con_set_flags))(dword new_flags); +extern int stdcall _FUNC((*con_get_font_height))(void); +extern int stdcall _FUNC((*con_get_cursor_height))(void); +extern int stdcall _FUNC((*con_set_cursor_height))(int new_height); +extern int stdcall _FUNC((*con_getch))(void); +extern word stdcall _FUNC((*con_getch2))(void); +extern int stdcall _FUNC((*con_kbhit))(void); +extern char* stdcall _FUNC((*con_gets))(char* str, int n); +typedef int (stdcall _FUNC(* con_gets2_callback))(int keycode, char** pstr, int* pn, int* ppos); +extern char* stdcall _FUNC((*con_gets2))(con_gets2_callback callback, char* str, int n); +extern void stdcall _FUNC((*con_cls))(); +extern void stdcall _FUNC((*con_get_cursor_pos))(int* px, int* py); +extern void stdcall _FUNC((*con_set_cursor_pos))(int x, int y); extern int _FUNC(__con_is_load); #endif diff --git a/programs/develop/ktcc/trunk/libc.obj/include/stddef.h b/programs/develop/ktcc/trunk/libc.obj/include/stddef.h index 3de65b8da9..34f3f74ca4 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/stddef.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/stddef.h @@ -34,12 +34,4 @@ typedef uint64_t uintmax_t; #define offsetof(type, field) ((size_t)&((type *)0)->field) -#ifndef __stdcall - #define __stdcall __attribute__((stdcall)) -#endif - -#ifndef __cdecl - #define __cdecl __attribute__((cdecl)) -#endif - -#endif /* _STDDEF_H_ */ +#endif /* _STDDEF_H_ */ \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h b/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h index 1a9245adba..a107ac94cf 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h @@ -218,8 +218,8 @@ enum KSYS_FILE_ENCODING{ KSYS_FILE_UTF8 = 3 }; -static inline -int __strcmp(const char * s1, const char * s2 ) +static inline +int _ksys_strcmp(const char * s1, const char * s2 ) { while ((*s1) && (*s1 == *s2)){ ++s1; ++s2; } return(*(unsigned char*)s1 - *(unsigned char *)s2); @@ -1163,7 +1163,7 @@ void* _ksys_dlsym(ksys_dll_t *table, const char* fun_name){ if (!(table+i)->func_name){ break; }else{ - if (!__strcmp(fun_name, (table+i)->func_name)){ + if (!_ksys_strcmp(fun_name, (table+i)->func_name)){ return (table+i)->func_ptr; } } diff --git a/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o b/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o new file mode 100644 index 0000000000..598dbd5fcc Binary files /dev/null and b/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o differ diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile index e480e37589..3bbd6bf93b 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile +++ b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile @@ -1,40 +1,41 @@ -KTCC=../../bin/kos32-tcc +KTCC=kos32-tcc FASM= fasm KPACK = kpack - CFLAGS = -I../include -I../../../../../../contrib/sdk/sources/SDL-1.2.2_newlib/include -LIBS = -lbox_lib -lshell -lSDL -lsound -lnetwork -lrasterworks -limg -ldialog -lmsgbox +LDFLAGS = -nostdlib -L../../bin/lib ../../bin/lib/crt0.o + +BIN= stdio_test.kex \ +basic_gui.kex \ +http_tcp_demo.kex \ +math_test.kex \ +string_test.kex \ +whois.kex \ +file_io.kex \ +tmpdisk_work.kex \ +consoleio.kex \ +assert_test.kex \ +fasm/sprintf_test.kex \ +clayer/rasterworks.kex \ +clayer/libimg.kex \ +clayer/dialog.kex \ +clayer/msgbox.kex \ +clayer/boxlib.kex \ +thread_work.kex \ +sdltest.kex \ +shell_test.kex \ +libc_test.kex + +LIBS= -lSDL -lshell -ltcc -lsound -ldialog -lrasterworks -limg -lbox -lmsgbox -lnetwork -lc.obj -BIN = stdio_test.kex \ - basic_gui.kex \ - http_tcp_demo.kex \ - math_test.kex \ - string_test.kex \ - whois.kex \ - file_io.kex \ - tmpdisk_work.kex \ - consoleio.kex \ - assert_test.kex \ - clayer/rasterworks.kex \ - clayer/libimg.kex \ - clayer/dialog.kex \ - clayer/msgbox.kex \ - clayer/boxlib.kex \ - thread_work.kex \ - sdltest.kex \ - shell_test.kex \ - libc_test.kex \ - defgen.kex - all: $(BIN) %.kex : %.c $(KTCC) $(CFLAGS) $(LDFLAGS) $< -o $@ $(LIBS) $(KPACK) --nologo $@ - --%.kex : %.asm + +%.kex : %.asm $(FASM) $< $@ $(KPACK) --nologo $@ - + clean: rm *.kex clayer/*.kex diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh index cddad56b51..7d6cfeda49 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh @@ -1,24 +1,24 @@ #SHS mkdir /tmp0/1/tcc_samples -../tcc file_io.c -o /tmp0/1/tcc_samples/file_io -../tcc whois.c -o /tmp0/1/tcc_samples/whois -lnetwork -../tcc stdio_test.c -o /tmp0/1/tcc_samples/stdio_test -../tcc basic_gui.c -o /tmp0/1/tcc_samples/basic_gui -../tcc consoleio.c -o /tmp0/1/tcc_samples/consoleio -../tcc dir_example.c -o /tmp0/1/tcc_samples/ls_dir -../tcc http_tcp_demo.c -o /tmp0/1/tcc_samples/http_tcp_demo -lnetwork -../tcc math_test.c -o /tmp0/1/tcc_samples/math_test -../tcc string_test.c -o /tmp0/1/tcc_samples/string_test -../tcc tmpdisk_work.c -o /tmp0/1/tcc_samples/tmpdisk_work -../tcc clayer/boxlib.c -o /tmp0/1/tcc_samples/boxlib -lbox_lib -../tcc clayer/dialog.c -o /tmp0/1/tcc_samples/dialog -ldialog +../tcc file_io.c -o /tmp0/1/tcc_samples/file_io -lc.obj +../tcc whois.c -o /tmp0/1/tcc_samples/whois -ltcc -lnetwork -lc.obj +../tcc stdio_test.c -o /tmp0/1/tcc_samples/stdio_test -lc.obj +../tcc basic_gui.c -o /tmp0/1/tcc_samples/basic_gui -ltcc -lc.obj +../tcc consoleio.c -o /tmp0/1/tcc_samples/consoleio -lc.obj +../tcc dir_example.c -o /tmp0/1/tcc_samples/ls_dir -lc.obj +../tcc http_tcp_demo.c -o /tmp0/1/tcc_samples/http_tcp_demo -ltcc -lnetwork -lc.obj +../tcc math_test.c -o /tmp0/1/tcc_samples/math_test -ltcc -lc.obj +../tcc string_test.c -o /tmp0/1/tcc_samples/string_test -ltcc -lc.obj +../tcc tmpdisk_work.c -o /tmp0/1/tcc_samples/tmpdisk_work -ltcc -lc.obj +../tcc clayer/boxlib.c -o /tmp0/1/tcc_samples/boxlib -ltcc -lbox -lc.obj +../tcc clayer/dialog.c -o /tmp0/1/tcc_samples/dialog -ltcc -ldialog -lc.obj cp clayer/logo.png /tmp0/1/tcc_samples/logo.png -../tcc clayer/libimg.c -o /tmp0/1/tcc_samples/libimg -limg -../tcc clayer/msgbox.c -o /tmp0/1/tcc_samples/msgbox -lmsgbox -../tcc clayer/rasterworks.c -o /tmp0/1/tcc_samples/rasterworks -lrasterworks -../tcc thread_work.c -o /tmp0/1/tcc_samples/thread_work -ltcc -lc -../tcc -I../include/SDL sdltest.c -o /tmp0/1/tcc_samples/sdltest -lSDL -lsound -../tcc shell_test.c -o /tmp0/1/tcc_samples/shell_test -lshell -../tcc libc_test.c -o /tmp0/1/tcc_samples/libc_test +../tcc clayer/libimg.c -o /tmp0/1/tcc_samples/libimg -ltcc -limg -lc.obj +../tcc clayer/msgbox.c -o /tmp0/1/tcc_samples/msgbox -ltcc -lmsgbox -lc.obj +../tcc clayer/rasterworks.c -o /tmp0/1/tcc_samples/rasterworks -ltcc -lrasterworks -lc.obj +../tcc thread_work.c -o /tmp0/1/tcc_samples/thread_work -ltcc -lc.obj +../tcc -I../include/SDL sdltest.c -o /tmp0/1/tcc_samples/sdltest -lSDL -lsound -ltcc -lc.obj +../tcc shell_test.c -o /tmp0/1/tcc_samples/shell_test -lshell -ltcc -lc.obj +../tcc libc_test.c -o /tmp0/1/tcc_samples/libc_test -ltcc -lc.obj "/sys/File managers/Eolite" /tmp0/1/tcc_samples exit diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/boxlib.c b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/boxlib.c index be81481182..6298a96308 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/boxlib.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/boxlib.c @@ -42,8 +42,10 @@ void draw_window(){ _ksys_end_draw(); } + int main() { + kolibri_boxlib_init(); init_checkbox2(&output_off); _ksys_set_event_mask(KSYS_EVM_REDRAW + KSYS_EVM_KEY + KSYS_EVM_BUTTON + KSYS_EVM_MOUSE+ KSYS_EVM_MOUSE_FILTER); while(1){ @@ -53,7 +55,7 @@ int main() break; case KSYS_EVENT_KEY: - edit_box_key_safe(&ed, _ksys_get_key()); + edit_box_key(&ed, _ksys_get_key().val); draw_window(); break; @@ -87,4 +89,4 @@ int main() } } return 0; -} +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/dialog.c b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/dialog.c index 8051ef2bc4..9d84bc9f67 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/dialog.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/dialog.c @@ -4,6 +4,7 @@ int main() { + kolibri_dialog_init(); // dialog init open_dialog *dlg_open = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320); // create opendialog struct OpenDialog_init(dlg_open); // Initializing an open dialog box. OpenDialog_start(dlg_open); // Show open dialog box diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/libimg.c b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/libimg.c index e30d85e69d..3d09442167 100755 --- a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/libimg.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/libimg.c @@ -44,13 +44,18 @@ void* load_img(char* fname, uint32_t* read_sz){ // Image file upload function void draw_gui(){ _ksys_start_draw(); - _ksys_create_window(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34); - img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0); // Draw blended image to window + _ksys_create_window(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34); + img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0); // Draw blended image to window _ksys_end_draw(); } int main(){ - _ksys_get_system_colors(&sys_color_table); // Get system colors theme + if (kolibri_libimg_init() == -1){ + printf("Error loading lib_img.obj\n"); + exit(0); + } + + _ksys_get_system_colors(&sys_color_table); // Get system colors theme _ksys_set_event_mask(0xC0000027); uint32_t img_size; diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/defgen.c b/programs/develop/ktcc/trunk/libc.obj/samples/defgen.c deleted file mode 100644 index 61a8ccaa7e..0000000000 --- a/programs/develop/ktcc/trunk/libc.obj/samples/defgen.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "stddef.h" -#include -#include -#include -#include -#include - -#define ARGC_VALID 3 - -enum ARGV_FILE { - IN = 1, - OUT = 2 -}; - -void show_help(void){ - puts("Usage: defgen [lib.obj] [lib.def]"); -} - -int main(int argc, char** argv){ - - if(argc!=ARGC_VALID){ - show_help(); - return 0; - } - - ksys_dll_t *obj_dll = _ksys_dlopen(argv[IN]); - FILE* outfile = fopen(argv[OUT], "w"); - - if(!obj_dll){ - printf("File '%s' not found!\n", argv[IN]); - return 1; - } - - if(!outfile){ - printf("Unable to create file:'%s'!\n", argv[OUT]); - return 2; - } - - fprintf(outfile, "LIBRARY %s\n\n", basename(argv[IN])); - fputs("EXPORTS\n", outfile); - - int i=0; - while(obj_dll[i].func_name){ - fprintf(outfile,"%s\n", obj_dll[i].func_name); - i++; - } - fclose(outfile); - return 0; -} diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c b/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c index 5423a69818..0b79b38006 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c @@ -3,7 +3,11 @@ #include #include +int errno; + int main() { + networklib_init(); + char *host = "kolibrios.org"; int port = 80; printf("Connecting to %s on port %d\n", host, port); diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c index c4b03cacfa..5bf99c36f2 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c @@ -8,7 +8,7 @@ int main(int argc, char** argv){ char hello1[]="Hello, KolibriOS!"; char hello2[20]; memcpy(hello1, hello2, strlen(hello1)); - if(!__strcmp(hello1, hello2)){ + if(!_ksys_strcmp(hello1, hello2)){ printf("memcpy: Successfully!\n"); return 0; } else{ diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/whois.c b/programs/develop/ktcc/trunk/libc.obj/samples/whois.c index fcbb51b96e..5d29a63fdb 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/whois.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/whois.c @@ -5,6 +5,8 @@ https://www.binarytides.com/whois-client-code-in-c-with-linux-sockets/ */ #include +int errno; + #include "sys/ksys.h" #include #include @@ -40,7 +42,8 @@ char* str_copy(char*); int main(int argc , char *argv[]) -{ +{ + networklib_init(); char *domain , *data = NULL; int f_flag=0; diff --git a/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm index 399a9d69cf..7237073af7 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm +++ b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm @@ -1,36 +1,39 @@ -; -; 2021, Edited by Coldy -; -; This module same as original crt0.asm, but cut: -; 1. virtual header block (hparams change to __app_params, hpath change to __app_path) -; 2. init heap of memory - not needed because 68.18 (68.19) init heap implicitly -; (it is does dll.obj) -; 3. loader (he lives in dll.obj) -; - format ELF section '.text' executable public start public start as '_start' - +;extrn mf_init extrn main -;include 'debug2.inc' -include '../../../../../../proc32.inc' -include '../../../../../../macros.inc' -__DEBUG__ = 0 +include '../../../../../proc32.inc' +include '../../../../../macros.inc' +include '../../../../../dll.inc' +;include '../../../../../debug.inc' -__app_params equ 0x1C ; Pointer to program arguments -;__app_path equ 0x20 ; Pointer to program path +;start_: +virtual at 0 + db 'MENUET01' ; 1. Magic number (8 bytes) + dd 0x01 ; 2. Version of executable file + dd start ; 3. Start address +imgsz dd 0x0 ; 4. Size of image + dd 0x100000 ; 5. Size of needed memory + dd 0x100000 ; 6. Pointer to stack +hparams dd 0x0 ; 7. Pointer to program arguments +hpath dd 0x0 ; 8. Pointer to program path +end virtual start: ;DEBUGF 'Start programm\n' + ;init heap of memory + mov eax,68 + mov ebx,11 + int 0x40 mov [argc], 0 - mov eax, [__app_params] + mov eax, [hparams] test eax, eax - jz .without_path + jz .without_path mov eax, path - cmp word ptr eax, 32fh ; '/#3' UTF8 + cmp word ptr eax, 32fh ; '/#3' UTF8 jne .without_path mov word ptr eax, 12fh ; '/#1' fix to CP866 .without_path: @@ -39,21 +42,21 @@ start: ; retrieving parameters mov esi, params xor edx, edx ; dl - идёт параметр(1) или разделители(0) - ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) + ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) mov ecx, 1 ; cl = 1 - ; ch = 0 просто ноль + ; ch = 0 просто ноль .parse: lodsb test al, al - jz .run + jz .run test dl, dl jnz .findendparam - ;{если был разделитель + ;{если был разделитель cmp al, ' ' - jz .parse ;загружен пробел, грузим следующий символ + jz .parse ;загружен пробел, грузим следующий символ mov dl, cl ;начинается параметр cmp al, '"' - jz @f ;загружены кавычки + jz @f ;загружены кавычки mov dh, ch ;параметр без кавычек dec esi call push_param @@ -67,9 +70,9 @@ start: .findendparam: test dh, dh - jz @f ; без кавычек + jz @f ; без кавычек cmp al, '"' - jz .clear + jz .clear jmp .parse @@: cmp al, ' ' @@ -82,6 +85,7 @@ start: jmp .parse .run: + call load_imports push argv push [argc] call main @@ -89,7 +93,7 @@ start: xor eax,eax dec eax int 0x40 - dd -1 + dd -1 .crash: jmp .exit ;============================ @@ -108,9 +112,101 @@ push_param: inc [argc] .dont_add: ret - - ;============================== + +;============================== +load_imports: +;============================== +;parameters +; none +;description +; imports must be located at end of image (but before BSS sections) +; the address of end of imports (next byte after imports) is located in imgsz +; look at each import from that address up to illegal import +; legal import is such that: +; first pointer points to procedure name +; and is smaller than imgsz +; second pointer points lo library name, starting with 0x55, 0xAA +; and is smaller than imgsz +; each library should be initialized as appropriate, once +; so as library is initialized, its name will be replaced 0x00 + mov ebx, [imgsz] ; byte after imports +.handle_next_import: + sub ebx, 4 ; ebx = pointer to pointer to library name + mov esi, dword[ebx] ; esi = pointer to library name + push ebx + push esi + call load_library ; eax = pointer to library exports + pop esi + pop ebx + test eax, eax + jz .done + sub ebx, 4 ; ebx = pointer to pointer to symbol name + push ebx + stdcall dll.GetProcAddress, eax, dword[ebx] + pop ebx + test eax, eax + jz .fail + mov dword[ebx], eax + jmp .handle_next_import +.done: + ;DEBUGF 1, "Library: %s not loaded!\n", esi + ;mcall -1 + ret +.fail: + ret + +;============================== + +;============================== +load_library: +;============================== +;parameters +; ebx: library name address +;description +; each library should be initialized as appropriate, once +; so as library is initialized, its name will be replaced 0x00 +; and 4 next bytes will be set to address of library + ; first two bytes of library name must be 0x55, 0xAA (is like a magic) + cld ; move esi further, not back + cmp esi, [imgsz] + ja .fail + lodsb ; al = first byte of library name + cmp al, 0x55 + jne .fail + lodsb ; al = second byte of library name + cmp al, 0xAA + jne .fail + lodsb ; al = third byte of library name (0x00 if the library is already loaded) + test al, al + jnz .load + lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes + ; now eax contains address of library + ret +.load: + dec esi ; we checked on 0 before, let's go back + mov eax, 68 + mov ebx, 19 + mov ecx, esi + int 0x40 ; eax = address of exports + mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name + mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library + ; call lib_init + stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init + test eax, eax + jz .ret + stdcall dll.Init, eax +.ret: + mov eax, [esi + 1] ; put address of library into eax + ret +.fail: + mov eax, 0 + ret + +;============================== + +lib_init_str db 'lib_init', 0 + public argc as '__argc' public params as '__argv' public path as '__path' @@ -118,10 +214,8 @@ public path as '__path' section '.bss' buf_len = 0x400 max_parameters=0x20 -argc rd 1 -argv rd max_parameters -path rb buf_len -params rb buf_len +argc rd 1 +argv rd max_parameters +path rb buf_len +params rb buf_len -;section '.data' -;include_debug_strings ; ALWAYS present in data section diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c index 0aa0954817..c99924ecaa 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c @@ -7,25 +7,31 @@ static char* __con_dllname = "/sys/lib/console.obj"; int __con_is_load = 0; -void __stdcall (*__con_init_hidden)(int wnd_width, unsigned wnd_height, int scr_width, int scr_height, const char* title); -void __stdcall (*con_exit)(int); -void __stdcall (*con_set_title)(const char* title); -void __stdcall (*con_write_asciiz)(const char* str); -void __stdcall (*con_write_string)(const char* str, dword length); -int __cdecl (*con_printf)(const char* format, ...); -dword __stdcall (*con_get_flags)(void); -dword __stdcall (*con_set_flags)(dword new_flags); -int __stdcall (*con_get_font_height)(void); -int __stdcall (*con_get_cursor_height)(void); -int __stdcall (*con_set_cursor_height)(int new_height); -int __stdcall (*con_getch)(void); -word __stdcall (*con_getch2)(void); -int __stdcall (*con_kbhit)(void); -char* __stdcall (*con_gets)(char* str, int n); -char* __stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n); -void __stdcall (*con_cls)(); -void __stdcall (*con_get_cursor_pos)(int* px, int* py); -void __stdcall (*con_set_cursor_pos)(int x, int y); +void stdcall (*__con_init_hidden)(int wnd_width, unsigned wnd_height, int scr_width, int scr_height, const char* title); +void stdcall (*con_exit)(int); +void stdcall (*con_set_title)(const char* title); +void stdcall (*con_write_asciiz)(const char* str); +void stdcall (*con_write_string)(const char* str, dword length); +int cdecl (*con_printf)(const char* format, ...); +dword stdcall (*con_get_flags)(void); +dword stdcall (*con_set_flags)(dword new_flags); +int stdcall (*con_get_font_height)(void); +int stdcall (*con_get_cursor_height)(void); +int stdcall (*con_set_cursor_height)(int new_height); +int stdcall (*con_getch)(void); +word stdcall (*con_getch2)(void); +int stdcall (*con_kbhit)(void); +char* stdcall (*con_gets)(char* str, int n); +char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n); +void stdcall (*con_cls)(); +void stdcall (*con_get_cursor_pos)(int* px, int* py); +void stdcall (*con_set_cursor_pos)(int x, int y); + +/*static void __con_panic(char* func_name) +{ + debug_printf("In console.obj %s=NULL!\n", func_name); + _ksys_exit(); +}*/ static void __con_lib_link(ksys_dll_t *exp) { diff --git a/programs/develop/ktcc/trunk/libc/KOSfuncs_inc_status.txt b/programs/develop/ktcc/trunk/libc/KOSfuncs_inc_status.txt new file mode 100644 index 0000000000..1f5c6af764 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/KOSfuncs_inc_status.txt @@ -0,0 +1,292 @@ + +; KolibriOS system functions: ++SF_TERMINATE_PROCESS=-1 // kos_exit() ++SF_CREATE_WINDOW=0 ; define and draw the window //sys_create_window() +SF_PUT_PIXEL=1 ; draw pixel to the window ++SF_GET_KEY=2 ; get code of the pressed key +SF_GET_SYS_TIME=3 ++SF_DRAW_TEXT=4 //draw_text_sys() ++SF_SLEEP=5 ; pause process // delay() ++SF_PUT_IMAGE=7 ; draw image to the window //draw_bitmap() ++SF_DEFINE_BUTTON=8 ; define/delete the button ++SF_THREAD_INFO=9 ; information on execution thread // get_proc_info() ++SF_WAIT_EVENT=10 ; wait for event // get_os_event() ++SF_CHECK_EVENT=11 ; check for event and return // check_os_event() +SF_REDRAW=12 ++ SSF_BEGIN_DRAW=1 ++ SSF_END_DRAW=2 ++SF_DRAW_RECT=13 ; draw rectangle to the window // draw_bar() +SF_GET_SCREEN_SIZE=14 ; get screen resolution +SF_BACKGROUND_SET=15 + SSF_SIZE_BG=1 + SSF_PIXEL_BG=2 + SSF_REDRAW_BG=3 + SSF_MODE_BG=4 + SSF_IMAGE_BG=5 + SSF_MAP_BG=6 ; map background image to the address space of the process + SSF_UNMAP_BG=7 + SSF_LAST_DRAW=8 ; get coordinates of the last draw to the background + SSF_REDRAW_RECT=9 ; redraws a rectangular part of the background +SF_RD_TO_FLOPPY=16 ; save ramdisk on the floppy ++SF_GET_BUTTON=17 ; get ID of the pressed button // get_os_button() +SF_SYSTEM=18 + SSF_UNFOCUS_WINDOW=1 ; take focus from the window of the given thread + SSF_TERMINATE_THREAD=2 ; terminate process/thread by the slot number ++ SSF_FOCUS_WINDOW=3 ; give focus to the window of the given thread + SSF_GET_IDLE_COUNT=4 ; get counter of idle cycles per second + SSF_GET_CPU_REQUENCY=5 ; get CPU clock rate + SSF_RD_TO_HDD=6 ; save ramdisk to the file on hard disk + SSF_GET_ACTIVE_WINDOW=7 ; get slot number of the active window + SSF_SPEAKER=8 + SSSF_GET_STATE=1 + SSSF_TOGGLE=2 + SSF_SHUTDOWN=9 ; system shutdown/reboot + SSF_MINIMIZE_WINDOW=10 ; minimize active window + SSF_INFO_DISC_SYS=11 ; get disk subsystem information + SSF_KERNEL_VERSION=13 ; get kernel version + SSF_WAIT_RETRACE=14 ; wait for screen retrace + SSF_CURSOR_CENTER=15 ; center mouse cursor on the screen + SSF_GET_FREE_RAM=16 ; get size of free RAM + SSF_GET_TOTAL_RAM=17 ; get total amount of RAM + SSF_TERMINATE_THREAD_ID=18 ; Terminate process/thread by the ID + SSF_MOUSE_SETTINGS=19 + SSSF_GET_SPEED=0 + SSSF_SET_SPEED=1 + SSSF_GET_SPEEDUP=2 + SSSF_SET_SPEEDUP=3 ; set mouse acceleration + SSSF_SET_POS=4 ; set mouse pointer position + SSSF_SET_BUTTON=5 ; simulate state of mouse buttons + SSSF_GET_DOUBLE_CLICK_DELAY=6 + SSSF_SET_DOUBLE_CLICK_DELAY=7 + SSF_GET_RAM_INFO=20 ; get information on RAM ++ SSF_GET_THREAD_SLOT=21 ; get slot number of process/thread by the ID + SSF_FOREIGN_WINDOW=22 ; operations with window of another thread by slot/ID + SSSF_MINIMIZE=0 + SSSF_MINIMIZE_ID=1 + SSSF_RESTORE=2 + SSSF_RESTORE_ID=3 + SSF_MINIMIZE_ALL=23 + SSF_SET_SCREEN_LIMITS=24 + SSF_WINDOW_BEHAVIOR=25 ; window focus relation with other windows + SSSF_GET_WB=1 + SSSF_SET_WB=2 +SF_MIDI=20 + SSF_RESET=1 + SSF_OUTPUT=2 +SF_SYSTEM_SET=21 + SSF_MPU_MIDI_BASE=1 + SSF_KEYBOARD_LAYOUT=2 + SSF_SYS_LANG=5 + SSF_ACCESS_HD_LBA=11 ; setting of low-level access to HD + SSF_ACCESS_PCI=12 ; setting of low-level access to PCI +SF_SET_TIME_DATE=22 ++SF_WAIT_EVENT_TIMEOUT=23; wait for event with timeout // wait_for_event() +SF_CD=24 + SSF_EJECT_TRAY=4 + SSF_INSERT_TRAY=5 +SF_SCREEN_PUT_IMAGE=25 ; put image on the background layer +SF_SYSTEM_GET=26 + ; Same as SF_SYSTEM_SET, plus: ++ SSF_TIME_COUNT=9 // get_tick_count() ++ SSF_TIME_COUNT_PRO=10 ; get value of the high precision time counter // get_ns_count() +SF_GET_SYS_DATE=29 +SF_CURRENT_FOLDER=30 ++ SSF_SET_CF=1 ; set current folder for the thread // set_current_folder() ++ SSF_GET_CF=2 // get_current_folder() + SSF_ADD_SYS_FOLDER=3 ; install the add.system directory for the kernel +SF_GET_PIXEL_OWNER=34 ; get slot number of the screen pixel owner +SF_GET_PIXEL=35 ; read the screen pixel color +SF_GET_IMAGE=36 ; read the screen area +SF_MOUSE_GET=37 ++ SSF_SCREEN_POSITION=0 // get_mouse_pos(POS_SCREEN) ++ SSF_WINDOW_POSITION=1 // get_mouse_pos(POS_WINDOW) ++ SSF_BUTTON=2 ; states of the mouse buttons //get_mouse_buttons() ++ SSF_BUTTON_EXT=3 ; states and events of the mouse buttons // get_mouse_eventstate() ++ SSF_LOAD_CURSOR=4 ++ SSF_SET_CURSOR=5 ++ SSF_DEL_CURSOR=6 // destroy_cursor() ++ SSF_SCROLL_DATA=7 //get_mouse_wheels() ++SF_DRAW_LINE=38 +SF_BACKGROUND_GET=39 + ;SSF_SIZE_BG=1 + ;SSF_PIXEL_BG=2 + ;SSF_MODE_BG=4 ++SF_SET_EVENTS_MASK=40 ; turn on/off desired events +SF_PORT_IN_OUT=43 ; input/output to a port +SF_SET_PORTS=46 ; reserve/free a group of input/output ports ++SF_DRAW_NUMBER=47 ; draw number to the window // draw_number_sys() +SF_STYLE_SETTINGS=48 + SSF_APPLY=0 ; apply screen settings + SSF_SET_BUTTON_STYLE=1 + SSF_SET_COLORS=2 ++ SSF_GET_COLORS=3 ; get standard window colors // get_system_colors() ++ SSF_GET_SKIN_HEIGHT=4 + SSF_GET_SCREEN_AREA=5 ; get screen working area + SSF_SET_SCREEN_AREA=6 + SSF_GET_SKIN_MARGINS=7 + SSF_SET_SKIN=8 + SSF_GET_FONT_SMOOTH=9 + SSF_SET_FONT_SMOOTH=10 + SSF_GET_FONT_SIZE=11 + SSF_SET_FONT_SIZE=12 +SF_APM=49 +SF_SET_WINDOW_SHAPE=50 ++SF_CREATE_THREAD=51 // start_thread() +SF_CLIPBOARD=54 ++ SSF_GET_SLOT_COUNT=0 ; get the number of slots in the clipboard // kol_clip_num() ++ SSF_READ_CB=1 // kol_clip_get() ++ SSF_WRITE_CB=2 // kol_clip_set() ++ SSF_DEL_SLOT=3 ; delete the last slot in the clipboard // kol_clip_pop() ++ SSF_UNLOCK_BUFFER=4 ; emergency buffer unlock // kol_clip_unlock() +SF_SPEAKER_PLAY=55 +SF_PCI_BIOS=57 +SF_IPC=60 ; Inter Process Communication ++ SSF_SET_AREA=1 ; set area for IPC receiving // ipc_set_area() ++ SSF_SEND_MESSAGE=2 // ipc_send_message() +SF_GET_GRAPHICAL_PARAMS=61 ++ SSF_SCREEN_SIZE=1 // GetScreenSize() + SSF_BITS_PER_PIXEL=2 + SSF_BYTES_PER_LINE=3 +SF_PCI=62 + SSF_GET_VERSION=0 ; get version of PCI-interface + SSF_GET_LAST_BUS=1 ; get number of the last PCI-bus + SSF_GET_ADRR_MODE=2 ; get addressing mode of the PCI configuration space + SSF_READ_BYTE=4 + SSF_READ_WORD=5 + SSF_READ_DWORD=6 + SSF_WRITE_BYTE=8 + SSF_WRITE_WORD=9 + SSF_WRITE_DWORD=10 +SF_BOARD=63 ++ SSF_DEBUG_WRITE=1 // debug_board_write_byte() + SSF_DEBUG_READ=2 +SF_MEMORY_RESIZE=64 ; resize total application memory +SF_PUT_IMAGE_EXT=65 ; draw image with palette to the window +SF_KEYBOARD=66 + SSF_SET_INPUT_MODE=1 + SSF_GET_INPUT_MODE=2 + SSF_GET_CONTROL_KEYS=3; get status of control keys + SSF_SET_SYS_HOTKEY=4 + SSF_DEL_SYS_HOTKEY=5 + SSF_LOCK_INPUT=6 ; block normal input + SSF_UNLOCK_INPUT=7 ; restore normal input +SF_CHANGE_WINDOW=67 ; change position/sizes of the window +SF_SYS_MISC=68 + SSF_GET_TASK_SWITCH_COUNT=0 ++ SSF_SWITCH_TASK=1 //yield() + SSF_PERFORMANCE=2 + SSSF_ALLOW_RDPMC=0 + SSSF_CACHE_STATUS=1 + SSSF_CACHE_ON=2 + SSSF_CACHE_OFF=3 + SSF_READ_MSR=3 + SSF_WRITE_MSR=4 + SSF_HEAP_INIT=11 ++ SSF_MEM_ALLOC=12 // user_alloc() ++ SSF_MEM_FREE=13 // user_free() + SSF_WAIT_SIGNAL=14 ; wait for signal from another program/driver ++ SSF_LOAD_DRIVER=16 // get_service() ++ SSF_CONTROL_DRIVER=17 // call_service() + SSF_LOAD_DLL=19 ++ SSF_MEM_REALLOC=20 //user_realloc() + SSF_LOAD_DRIVER_PE=21 ++ SSF_MEM_OPEN=22 ; open named memory area // shm_open() ++ SSF_MEM_CLOSE=23 // shm_close() + SSF_SET_EXCEPTION_HANDLER=24 + SSF_SET_EXCEPTION_STATE=25 ++ SSF_MEM_FREE_EXT=26 // user_unmap() ++ SSF_LOAD_FILE=27 +SF_DEBUG=69 + SSF_SET_MESSAGE_AREA=0 + SSF_GET_REGISTERS=1 + SSF_SET_REGISTERS=2 + SSF_DETACH=3 + SSF_SUSPEND=4 + SSF_RESUME=5 + SSF_READ_MEMORY=6 + SSF_WRITE_MEMORY=7 + SSF_TERMINATE=8 + SSF_DEFINE_BREAKPOINT=9 +SF_FILE=70 + SSF_READ_FILE=0 + SSF_READ_FOLDER=1 + SSF_CREATE_FILE=2 + SSF_WRITE_FILE=3 + SSF_SET_END=4 + SSF_GET_INFO=5 + SSF_SET_INFO=6 ++ SSF_START_APP=7 + SSF_DELETE=8 + SSF_CREATE_FOLDER=9 +SF_SET_CAPTION=71 +SF_SEND_MESSAGE=72 ++SF_BLITTER=73 //Blit() +SF_NETWORK_GET=74 + SSF_DEVICE_COUNT=255 ; get number of active network devices + SSF_DEVICE_TYPE=0 + SSF_DEVICE_NAME=1 + SSF_RESET_DEVICE=2 + SSF_STOP_DEVICE=3 + SSF_DEVICE_POINER=4 + SSF_TX_PACKET_COUNT=6 + SSF_RX_PACKET_COUNT=7 + SSF_TX_BYTE_COUNT=8 + SSF_RX_BYTE_COUNT=9 + SSF_LINK_STATUS=10 +SF_NETWORK_SOCKET=75 + SSF_OPEN=0 + SSF_CLOSE=1 + SSF_BIND=2 + SSF_LISTEN=3 + SSF_CONNECT=4 + SSF_ACCEPT=5 + SSF_SEND=6 + SSF_RECEIVE=7 + SSF_SET_OPTIONS=8 + SSF_GET_OPTIONS=9 + SSF_GET_PAIR=10 +SF_NETWORK_PROTOCOL=76 + SSF_ETHERNET_READ_MAC=0 + SSF_IP4_PACKETS_SENT=10000h + SSF_IP4_PACKETS_RECEIVED=10001h + SSF_IP4_READ_IP=10002h + SSF_IP4_WRITE_IP=10003h + SSF_IP4_READ_DNS=10004h + SSF_IP4_WRITE_DNS=10005h + SSF_IP4_READ_SUBNET=10006h + SSF_IP4_WRITE_SUBNET=10007h + SSF_IP4_READ_GATEWAY=10008h + SSF_IP4_WRITE_GATEWAY=10009h + SSF_ICMP_PACKETS_SENT=20000h + SSF_ICMP_PACKETS_RECEIVED=20001h + SSF_ICMP_ECHO_REPLY=20003h + SSF_UDP_PACKETS_SENT=30000h + SSF_UDP_PACKETS_RECEIVED=30001h + SSF_TCP_PACKETS_SENT=40000h + SSF_TCP_PACKETS_RECEIVED=40001h + SSF_ARP_PACKETS_SENT=50000h + SSF_ARP_PACKETS_RECEIVED=50001h + SSF_ARP_GET_ENTRY_COUNT=50002h + SSF_ARP_READ_ENTRY=50003h + SSF_ARP_ADD_STATIC_ENTRY=50004h + SSF_ARP_DEL_ENTRY=50005h + SSF_ARP_SEND_ANNOUNCE=50006h + SSF_ARP_CONFLICTS_COUNT=50007h +SF_FUTEX=77 + SSF_CREATE=0 + SSF_DESTROY=1 + SSF_WAIT=2 + SSF_WAKE=3 + +; File system errors: +FSERR_SUCCESS=0 +FSERR_UNSUPPORTED=2 +FSERR_UNKNOWN=3 +FSERR_FILE_NOT_FOUND=5 +FSERR_END_OF_FILE=6 +FSERR_INVALID_BUFFER=7 +FSERR_DISK_FULL=8 +FSERR_FAIL=9 +FSERR_ACCESS_DENIED=10 +FSERR_DEVICE_FAIL=11 +FSERR_OUT_OF_MEMORY=12 diff --git a/programs/develop/ktcc/trunk/libc/Makefile b/programs/develop/ktcc/trunk/libc/Makefile new file mode 100644 index 0000000000..80e3a941ec --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/Makefile @@ -0,0 +1,33 @@ +INCLUDE = include +LIBSFORBUILD = math +LIBNAME = libck.a +CC = ../bin/kos32-tcc +CFLAGS = -I$(INCLUDE) -m32 -nostdinc -nostdlib -DGNUC +DIRS := stdio memory kolibrisys string stdlib math dlfcn libgen fs net + +############################################################## +#files := $(foreach dir,$(DIRS),$(dir)/$(wildcard $(dir)/*)) +asmfiles := $(foreach dir,$(DIRS),$(patsubst %.asm, %.o, $(wildcard $(dir)/*.asm))) +cfiles := $(foreach dir,$(DIRS),$(patsubst %.c, %.o, $(wildcard $(dir)/*.c))) + +.PHONY: clean all + +ifdef windir +doClean = del /F /Q $(subst /,\,$(cfiles)) $(subst /,\,$(asmfiles)) +else +doClean = rm $(cfiles) $(asmfiles) +endif + +all: $(cfiles) $(asmfiles) + ar -rcs $(LIBNAME) $^ + +$(cfiles): $(INCLUDE)/*.h + +$(asmfiles): + fasm $*.asm $*.o + +clean: + $(doClean) + +install: + cp $(LIBNAME) ../bin/lib diff --git a/programs/develop/ktcc/trunk/libc/build.bat b/programs/develop/ktcc/trunk/libc/build.bat new file mode 100644 index 0000000000..5ff4a305ec --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/build.bat @@ -0,0 +1,71 @@ +@echo off +echo #################################################### +echo # Melibc builder # +echo # usage: build [clean] # +echo #################################################### +rem #### CONFIG SECTION #### +set LIBNAME=libck.a +set INCLUDE=include +set CC=kos32-tcc +set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" -Wall +set AR=kos32-ar +set ASM=fasm +set dirs=stdio memory kolibrisys string stdlib math dlfcn libgen fs net +rem #### END OF CONFIG SECTION #### + +set objs= +set target=%1 +if not "%1"=="clean" set target=all + +set INCLUDE="%cd%" +call :Target_%target% + +if ERRORLEVEL 0 goto Exit_OK + +echo Probably at runing has been created error +echo For help send a report... +pause +goto :eof + +:Compile_C +echo compile .c %1 + %CC% %CFLAGS% %1 -o "%~dpn1.o" + if not %errorlevel%==0 goto Error_Failed + set objs=%objs% "%~dpn1.o" +goto :eof + +:Compile_Asm +echo compile .asm %1 + %ASM% %1 "%~dpn1.o" + if not %errorlevel%==0 goto Error_Failed + set objs=%objs% "%~dpn1.o" +goto :eof + +:Target_clean + echo cleaning ... + for %%a in (%dirs%) do del /Q "%%a\*.o" +goto :Exit_OK + +:Target_all + echo building all ... + for %%a in (%dirs%) do ( + for %%f in ("%%a\*.asm") do call :Compile_Asm "%%f" + for %%f in ("%%a\*.c") do call :Compile_C "%%f" + ) + echo calling AR + %AR% -ru %LIBNAME% %objs% + if not %errorlevel%==0 goto Error_Failed +goto Exit_OK + +:Error_Failed +echo error: execution failed +pause +exit 1 + +:Exit_OK +echo #################################################### +echo # All operations has been done... # +echo # For cleaning run this script with param " clean" # +echo #################################################### +pause +exit 0 diff --git a/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c b/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c new file mode 100644 index 0000000000..c45969e0bf --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c @@ -0,0 +1,91 @@ +#include + +#include +#include +#include + +typedef struct { + char *name; + void *ptr; +} KosExp; + +typedef struct { + void **importNames; + char * libraryName; +} KosImp; + +static int stdcall dll_Load(KosImp *importTableEntry); + +static const char *__error; + +static int stdcall dll_Load(KosImp *importTableEntry) { + for (; importTableEntry->importNames; importTableEntry++) { + char libPath[256] = "/sys/lib/"; + KosExp *exports = NULL; + void **libImports = importTableEntry->importNames; + + strcat(libPath, importTableEntry->libraryName); + if (!(exports = dlopen(libPath, 0))) { return 1; } + for (; *libImports; libImports++) { + if (!(*libImports = dlsym(exports, *libImports))) { return 1; } + } + } + return 0; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlopen.html +// Current implementation fully ignores "mode" parameter +void *dlopen(const char *name, int mode) { + KosExp *exports = NULL; + + // load library using syscall + asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(name)); + if (!exports) { + char libPath[256] = "/sys/lib/"; + + strcat(libPath, name); + asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(libPath)); + if (!exports) { + __error = "Library not found in \"/sys/lib/\" nor current folder"; + return NULL; + } + } + // call anything starting with "lib_" + for (KosExp *export = exports; export->name; export++) { + if (!memcmp(export->name, "lib_", 4)) { + asm volatile ( + "call *%4" :: + "a"(sysmalloc), + "b"(sysfree), + "c"(sysrealloc), + "d"(dll_Load), + "r"(export->ptr)); + } + } + return exports; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlsym.html +void *dlsym(void *handle, const char *name) { + KosExp *exp = handle; + + for (; exp->name; exp++) { + if (!strcmp(exp->name, name)) { + return exp->ptr; + } + } + __error = "Symbol not found"; + return NULL; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlclose.html +int dlclose(void *handle) { + return 0; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlerror.html +char *dlerror(void) { + char *ret = __error ? strdup(__error) : NULL; + __error = NULL; + return ret; +} diff --git a/programs/develop/ktcc/trunk/libc/fs/dir.c b/programs/develop/ktcc/trunk/libc/fs/dir.c new file mode 100644 index 0000000000..fcf5a877fc --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/fs/dir.c @@ -0,0 +1,108 @@ +/* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 */ + +#include +#include +#include + +#pragma pack(push,1) +typedef struct { + unsigned p00; + unsigned long long p04; + unsigned p12; + unsigned p16; + char p20; + char *p21; +} kol_struct70; +#pragma pack(pop) + +int kol_file_70(kol_struct70 *k) +{ + asm volatile ("int $0x40"::"a"(70), "b"(k)); +} + +bool dir_operations(unsigned char fun_num, char *path) +{ + kol_struct70 inf; + inf.p00 = fun_num; + inf.p04 = 0; + inf.p12 = 0; + inf.p16 = 0; + inf.p20 = 0; + inf.p21 = path; + if(!kol_file_70(&inf)){ + return true; + } + else { + return false; + } +} + +int lsdir(const char* dir, short_file_info **list) +{ + int num_of_file=0; + kol_struct70 inf; + + inf.p00 = 1; + inf.p04 = 0; + inf.p12 = 2; + inf.p16 = (unsigned) malloc(32+inf.p12*560); + inf.p20 = 0; + inf.p21 = (char*)dir; + + if(kol_file_70(&inf)) + { + free((void*)inf.p16); + return FS_ERROR; + } + + num_of_file = *(unsigned*)(inf.p16+8); + inf.p12 = num_of_file; + free((void*)inf.p16); + inf.p16 = (unsigned) malloc(32+inf.p12*560); + *list = (short_file_info*)malloc(num_of_file*sizeof(short_file_info)); + + if(kol_file_70(&inf)) + { + free((void*)inf.p16); + return FS_ERROR; + } + + for(int i=0; i +/*ToDo + * voxel function + */ + +extern int kolibri_buf2d_init(void); + +typedef struct { + unsigned int *buf_pointer; + uint16_t left; + uint16_t top; + unsigned int width; + unsigned int height; + unsigned int bgcolor; + uint8_t color_bit; +} __attribute__ ((__packed__))buf2d_struct; + +enum BUF2D_ALGORITM_FILTR { + SIERRA_LITE, + FLOYD_STEINBERG, + BURKERS, + HEAVYIRON_MOD, + ATKINSON +}; + +enum BUF2D_OPT_CROP { + BUF2D_OPT_CROP_TOP = 1, + BUF2D_OPT_CROP_LEFT = 2, + BUF2D_OPT_CROP_BOTTOM = 4, + BUF2D_OPT_CROP_RIGHT = 8 +}; + +extern void (*buf2d_create_asm __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_curve_bezier_asm __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); + +buf2d_struct* buf2d_create(uint16_t tlx, uint16_t tly, unsigned int sizex, unsigned int sizey, unsigned int font_bgcolor, uint8_t color_bit) +{ + buf2d_struct *new_buf2d_struct = (buf2d_struct *)malloc(sizeof(buf2d_struct)); + new_buf2d_struct -> left = tlx; + new_buf2d_struct -> top = tly; + new_buf2d_struct -> width = sizex; + new_buf2d_struct -> height = sizey; + new_buf2d_struct -> bgcolor = font_bgcolor; + new_buf2d_struct -> color_bit = color_bit; + buf2d_create_asm(new_buf2d_struct); + return new_buf2d_struct; +} + +void buf2d_curve_bezier(buf2d_struct *buf, unsigned int p0_x, unsigned int p0_y, unsigned int p1_x, unsigned int p1_y, unsigned int p2_x, unsigned int p2_y, unsigned int color) +{ + buf2d_curve_bezier_asm(buf, (p0_x<<16)+p0_y, (p1_x<<16)+p1_y, (p2_x<<16)+p2_y, color); +} + +extern void (*buf2d_draw __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_clear __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_delete __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_rotate __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_resize __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_line __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_line_sm __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_rect_by_size __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_filled_rect_by_size __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_circle __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_img_hdiv2 __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_img_wdiv2 __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_conv_24_to_8 __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_conv_24_to_32 __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +extern void (*buf2d_bit_blt_transp __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); +extern void (*buf2d_bit_blt_alpha __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, buf2d_struct *); +extern void (*buf2d_convert_text_matrix __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_draw_text __attribute__((__stdcall__)))(buf2d_struct *, buf2d_struct *, const char *, unsigned int, unsigned int); +extern void (*buf2d_crop_color __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int); +extern void (*buf2d_offset_h __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_flood_fill __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int, unsigned int); +extern void (*buf2d_set_pixel __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int, unsigned int); +extern unsigned int (*buf2d_get_pixel __attribute__((__stdcall__)))(buf2d_struct *, unsigned int, unsigned int); +extern void (*buf2d_flip_h __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_flip_v __attribute__((__stdcall__)))(buf2d_struct *); +extern void (*buf2d_filter_dither __attribute__((__stdcall__)))(buf2d_struct *, unsigned int); +#endif /* KOLIBRI_BUF2D_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/dialog.h b/programs/develop/ktcc/trunk/libc/include/clayer/dialog.h new file mode 100644 index 0000000000..3cb7759e0c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/dialog.h @@ -0,0 +1,126 @@ +#ifndef KOLIBRI_DIALOG_H +#define KOLIBRI_DIALOG_H + +#include +#define NOT_SUCCESS 0 +#define SUCCESS 1 + +char sz_com_area_name[] = "FFFFFFFF_open_dialog"; +char sz_dir_default_path[] = "/rd/1"; +char sz_start_path[] = "/rd/1/File managers/opendial"; + +char cd_com_area_name[] = "FFFFFFFF_color_dialog"; +char cd_start_path[] = "/rd/1/colrdial"; + +enum open_dialog_mode { + OPEN, + SAVE, + SELECT +}; + +typedef struct { + unsigned int size; + unsigned char end; +}od_filter __attribute__ ((__packed__)); + +typedef struct { + unsigned int mode; + char* procinfo; + char* com_area_name; + unsigned int com_area; + char* opendir_path; + char* dir_default_path; + char* start_path; + void (*draw_window)(); + unsigned int status; + char* openfile_path; + char* filename_area; + od_filter* filter_area; + unsigned short x_size; + unsigned short x_start; + unsigned short y_size; + unsigned short y_start; +}open_dialog __attribute__ ((__packed__)); + + +typedef struct{ + unsigned int type; + char* procinfo; + char* com_area_name; + unsigned int com_area; + char* start_path; + void (*draw_window)(void); + unsigned int status; + unsigned short x_size; + unsigned short x_start; + unsigned short y_size; + unsigned short y_start; + unsigned int color_type; + unsigned int color; +}color_dialog __attribute__ ((__packed__)); + +void fake_on_redraw(void) {} + +open_dialog* kolibri_new_open_dialog(unsigned int mode, unsigned short tlx, unsigned short tly, unsigned short x_size, unsigned short y_size) +{ + open_dialog *new_opendialog = (open_dialog *)malloc(sizeof(open_dialog)); + od_filter *new_od_filter = (od_filter *)malloc(sizeof(od_filter)); + char *plugin_path = (char *)calloc(4096, sizeof(char)); + char *openfile_path = (char *)calloc(4096, sizeof(char)); + char *proc_info = (char *)calloc(1024, sizeof(char)); + char *filename_area = (char *)calloc(256, sizeof(char)); + + new_od_filter -> size = 0; + new_od_filter -> end = 0; + + new_opendialog -> mode = mode; + new_opendialog -> procinfo = proc_info; + new_opendialog -> com_area_name = sz_com_area_name; + new_opendialog -> com_area = 0; + new_opendialog -> opendir_path = plugin_path; + new_opendialog -> dir_default_path = sz_dir_default_path; + new_opendialog -> start_path = sz_start_path; + new_opendialog -> draw_window = &fake_on_redraw; + new_opendialog -> status = 0; + new_opendialog -> openfile_path = openfile_path; + new_opendialog -> filename_area = filename_area; + new_opendialog -> filter_area = new_od_filter; + new_opendialog -> x_size = x_size; + new_opendialog -> x_start = tlx; + new_opendialog -> y_size = y_size; + new_opendialog -> y_start = tly; + return new_opendialog; +} + +void cd_fake_on_redraw(void) {} + +color_dialog* kolibri_new_color_dialog(unsigned int type, unsigned short tlx, unsigned short tly, unsigned short x_size, unsigned short y_size) +{ + color_dialog *new_colordialog = (color_dialog *)malloc(sizeof(color_dialog)); + char *proc_info = (char *)calloc(1024, sizeof(char)); + + new_colordialog -> type = type; + new_colordialog -> procinfo = proc_info; + new_colordialog -> com_area_name = cd_com_area_name; + new_colordialog -> com_area = 0; + new_colordialog -> start_path = cd_start_path; + new_colordialog -> draw_window = &cd_fake_on_redraw; + new_colordialog -> status = 0; + new_colordialog -> x_size = x_size; + new_colordialog -> x_start = tlx; + new_colordialog -> y_size = y_size; + new_colordialog -> y_start = tly; + new_colordialog -> color_type = 0; + new_colordialog -> color = 0; + return new_colordialog; +} + +extern void kolibri_dialog_init(); + +extern void (*OpenDialog_init __attribute__((__stdcall__)))(open_dialog *); +extern void (*OpenDialog_start __attribute__((__stdcall__)))(open_dialog *); + +extern void (*ColorDialog_init __attribute__((__stdcall__)))(color_dialog *); +extern void (*ColorDialog_start __attribute__((__stdcall__)))(color_dialog *); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/gb.h b/programs/develop/ktcc/trunk/libc/include/clayer/gb.h new file mode 100644 index 0000000000..7e0ae75bdb --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/gb.h @@ -0,0 +1,19 @@ + +#pragma pack(push, 1) +typedef struct +{ +int w; +int h; +char *bmp; +char *alpha; +} GB_BMP; +#pragma pack(pop) + +void gb_pixel_set(GB_BMP *b, int x, int y, unsigned c); +int gb_pixel_get(GB_BMP *b, int x, int y, unsigned *c); +void gb_line(GB_BMP *b, int x1, int y1, int x2, int y2, unsigned c); +void gb_rect(GB_BMP *b, int x, int y, int w, int h, unsigned c); +void gb_bar(GB_BMP *b, int x, int y, int w, int h, unsigned c); +void gb_circle(GB_BMP *b, int x, int y, int r, unsigned c); +void gb_image_set(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h); +void gb_image_set_t(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h, unsigned c); diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/http.h b/programs/develop/ktcc/trunk/libc/include/clayer/http.h new file mode 100644 index 0000000000..ac296576e3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/http.h @@ -0,0 +1,153 @@ +/* + This is adapded thunk for http.obj sys library + .h is equal to svn:\\programs\develop\libraries\http\http_en.txt + + Adapted for TCC's dynamic API by Magomed Kostoev, 2020 +*/ + +#ifndef _HTTP_H_ +#define _HTTP_H_ + +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) + +// Bitflags for http_msg.flags +// status + +#define HTTP_FLAG_HTTP11 1 << 0 +#define HTTP_FLAG_GOT_HEADER 1 << 1 +#define HTTP_FLAG_GOT_ALL_DATA 1 << 2 +#define HTTP_FLAG_CONTENT_LENGTH 1 << 3 +#define HTTP_FLAG_CHUNKED 1 << 4 +#define HTTP_FLAG_CONNECTED 1 << 5 + +// user options +#define HTTP_FLAG_KEEPALIVE 1 << 8 +#define HTTP_FLAG_STREAM 1 << 9 +#define HTTP_FLAG_REUSE_BUFFER 1 << 10 +#define HTTP_FLAG_BLOCK 1 << 11 + +// error +#define HTTP_FLAG_INVALID_HEADER 1 << 16 +#define HTTP_FLAG_NO_RAM 1 << 17 +#define HTTP_FLAG_SOCKET_ERROR 1 << 18 +#define HTTP_FLAG_TIMEOUT_ERROR 1 << 19 +#define HTTP_FLAG_TRANSFER_FAILED 1 << 20 + +/* +User flags: + +For the flag codes themselves, see http.inc file. + + FLAG_KEEPALIVE will keep the connection open after first GET/POST/.. so you can send a second request on the same TCP session. +In this case, the session must be closed manually when done by using the exported disconnect() function. + + FLAG_STREAM will force receive() to put the received content in a series of fixed size buffers, instead of everything in one big buffer. +This can be used for example to receive an internet radio stream, +but also to download larger files for which it does not make sense to put them completely in RAM first. + + FLAG_REUSE_BUFFER is to be used in combination with FLAG_STREAM and will make receive() function re-use the same buffer. +This, for example, can be used when downloading a file straight to disk. + + FLAG_BLOCK will make receive() function blocking. This is only to be used when receiving one file from a thread that has no other work. +If however, you want to receive multiple files, or do other things in the program mainloop, you should call the receive function periodically. +You may use system function 10 or 23 to wait for network event before calling one or more receive() functions. +*/ + +#pragma pack(push,1) +typedef struct http_msg_s { + unsigned socket; // socket on which the actual transfer happens + unsigned flags; // flags, reflects status of the transfer using bitflags + unsigned write_ptr; // internal use only (where to write new data in buffer) + unsigned buffer_length; // internal use only (number of available bytes in buffer) + unsigned chunk_ptr; // internal use only (where the next chunk begins) + unsigned timestamp; // internal use only (when last data was received) + unsigned status; // HTTP status + unsigned header_length; // length of HTTP header + void * content_ptr; // ptr to content + unsigned content_length; // total length of HTTP content + unsigned content_received; // number of currently received content bytes + char * http_header; +} http_msg; +#pragma pack(pop) + +/* + url = pointer to ASCIIZ URL + identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one. + flags = bit flags (see end of this document). + add_header = pointer to ASCIIZ additional header parameters, or null for none. + Every additional parameter must end with CR LF bytes, including the last line. + Initiates a HTTP connection, using 'GET' method. + Returns NULL on error, identifier otherwise. +*/ +extern http_msg * stdcall (*http_get)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); + +/* + url = pointer to ASCIIZ URL + identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one. + flags = bit flags (see end of this document). + add_header = pointer to ASCIIZ additional header parameters, or null for none. + Every additional parameter must end with CR LF bytes, including the last line. + Initiate a HTTP connection, using 'HEAD' method. + Returns NULL on error, identifier otherwise. +*/ +extern http_msg * stdcall (*http_head)(const char *url, http_msg *identifier, unsigned flags, const char *add_header); + +/* + url = pointer to ASCIIZ URL + identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one. + flags = bit flags (see end of this document). + add_header = pointer to ASCIIZ additional header parameters, or null for none. + Every additional parameter must end with CR LF bytes, including the last line. + content-type = pointer to ASCIIZ string containing content type. + content-length = length of the content (in bytes). + Initiate a HTTP connection, using 'POST' method. + The content itself must be send to the socket (which you can find in the structure), + using system function 75, 6. + Returns 0 on error, identifier otherwise +*/ +extern http_msg * stdcall (*http_post)(const char *url, http_msg *identifier, unsigned flags, const char *add_header, + const char *content_type, unsigned content_length); + +/* + identifier = identifier which one of the previous functions returned + This procedure will handle all incoming data for a connection and place it in the buffer. + As long as the procedure expects more data, -1 is returned and the procedure must be called again. + When transfer is done, the procedure will return 0. + The receive procedure is non-blocking by default, but can be made to block by setting FLAG_BLOCK. + + The HTTP header is placed together with some flags and other attributes in the http_msg structure. + This structure is defined in http.inc (and not copied here because it might still change.) + The identifier used by the functions is actually a pointer to this structure. + In the dword named .flags, the library will set various bit-flags indicating the status of the process. + (When a transfer is done, one should check these bit-flags to find out if the transfer was error-free.) + The HTTP header is placed at the end of this structure. The content is placed in another buffer. + The dword .status contains the status code received from the server (e.g. 200 for OK). + In header_length you'll find the length of the header as soon as it has been received. + In content_ptr you'll find a pointer to the actual content. + In content_length you'll find the length of the content. + In content_received, you'll find the number of content bytes already received. +*/ +extern int stdcall (*http_receive)(http_msg *identifier); + +/* + identifier = identifier which one of the previous functions returned + dataptr = pointer to the data you want to send + datalength = length of the data to send (in bytes) + This procedure can be used to send data to the server (POST) + Returns number of bytes sent, -1 on error +*/ +extern int stdcall (*http_send)(http_msg *identifier, void *dataptr, unsigned datalength); + +/* + Sometimes the http_receive function receives incomplete data. If you have the same problem then a macro can help you: +*/ + +extern int stdcall (*http_free)(http_msg *identifier); +/* + Free unused data +*/ + +#define http_long_receive(x) while(http_receive(x)){}; + +#endif // _HTTP_H_ diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/inputbox.h b/programs/develop/ktcc/trunk/libc/include/clayer/inputbox.h new file mode 100644 index 0000000000..1b2a6c2041 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/inputbox.h @@ -0,0 +1,17 @@ +/* + This is wrapper for Inputbox.obj sys library + https://board.kolibrios.org/viewtopic.php?f=24&t=3767&sid=fd2ca95b24eec430db0c61d977f5d8ba#p71585 + + Adapted for TCC's dynamic API by Magomed Kostoev, 2020 +*/ + +#ifndef __KOS__INPUTBOX__H________ +#define __KOS__INPUTBOX__H________ + +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) + +extern unsigned stdcall (*InputBox)(void* Buffer, char* Caption, char* Prompt, char* Default, + unsigned long Flags, unsigned long BufferSize, void* RedrawProc); + +#endif // __KOS__INPUTBOX__H________ diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/libimg.h b/programs/develop/ktcc/trunk/libc/include/clayer/libimg.h new file mode 100644 index 0000000000..3b41978be2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/libimg.h @@ -0,0 +1,126 @@ +/* Written by turbocat2001 (Logaev Maxim) */ + +#ifndef KOLIBRI_LIBIMG_H +#define KOLIBRI_LIBIMG_H + +#include +#include + +extern int kolibri_libimg_init(void); + +#define _stdcall __attribute__((__stdcall__)) + +//list of format id's +#define LIBIMG_FORMAT_BMP 1 +#define LIBIMG_FORMAT_ICO 2 +#define LIBIMG_FORMAT_CUR 3 +#define LIBIMG_FORMAT_GIF 4 +#define LIBIMG_FORMAT_PNG 5 +#define LIBIMG_FORMAT_JPEG 6 +#define LIBIMG_FORMAT_TGA 7 +#define LIBIMG_FORMAT_PCX 8 +#define LIBIMG_FORMAT_XCF 9 +#define LIBIMG_FORMAT_TIFF 10 +#define LIBIMG_FORMAT_PNM 11 +#define LIBIMG_FORMAT_WBMP 12 +#define LIBIMG_FORMAT_XBM 13 +#define LIBIMG_FORMAT_Z80 14 + +#pragma pack(push, 1) +typedef struct{ + uint32_t Checksum; // ((Width ROL 16) OR Height) XOR Data[0] ; ignored so far + uint32_t Width; + uint32_t Height; + uint32_t Next; + uint32_t Previous; + uint32_t Type; // one of Image.bppN + uint32_t* Data; + uint32_t Palette; // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i + uint32_t Extended; + uint32_t Flags; // bitfield + uint32_t Delay; // used iff Image.IsAnimated is set in Flags +} Image; +#pragma pack(pop) + +#define IMAGE_BPP8i 1 // indexed +#define IMAGE_BPP24 2 +#define IMAGE_BPP32 3 +#define IMAGE_BPP15 4 +#define IMAGE_BPP16 5 +#define IMAGE_BPP1 6 +#define IMAGE_BPP8g 7 // grayscale +#define IMAGE_BPP2i 8 +#define IMAGE_BPP4i 9 +#define IMAGE_BPP8a 10 + +// scale type +#define LIBIMG_SCALE_NONE 0 +#define LIBIMG_SCALE_INTEGER 1 +#define LIBIMG_SCALE_TILE 2 +#define LIBIMG_SCALE_STRETCH 3 +#define LIBIMG_SCALE_FIT_BOTH LIBIMG_SCALE_STRETCH +#define LIBIMG_SCALE_FIT_MIN 4 +#define LIBIMG_SCALE_FIT_RECT LIBIMG_SCALE_FIT_MIN +#define LIBIMG_SCALE_FIT_WIDTH 5 +#define LIBIMG_SCALE_FIT_HEIGHT 6 +#define LIBIMG_SCALE_FIT_MAX 7 + +// interpolation algorithm +#define LIBIMG_INTER_NONE 0 // use it with LIBIMG_SCALE_INTEGER, LIBIMG_SCALE_TILE, etc +#define LIBIMG_INTER_BILINEAR 1 +#define LIBIMG_INTER_BICUBIC 2 +#define LIBIMG_INTER_LANCZOS 3 +#define LIBIMG_INTER_DEFAULT LIBIMG_INTER_BILINEAR + +//error codes +#define LIBIMG_ERROR_OUT_OF_MEMORY 1 +#define LIBIMG_ERROR_FORMAT 2 +#define LIBIMG_ERROR_CONDITIONS 3 +#define LIBIMG_ERROR_BIT_DEPTH 4 +#define LIBIMG_ERROR_ENCODER 5 +#define LIBIMG_ERROR_SRC_TYPE 6 +#define LIBIMG_ERROR_SCALE 7 +#define LIBIMG_ERROR_INTER 8 +#define LIBIMG_ERROR_NOT_INPLEMENTED 9 +#define LIBIMG_ERROR_INVALID_INPUT 10 + +//encode flags (byte 0x02 of _common option) +#define LIBIMG_ENCODE_STRICT_SPECIFIC 0x01 +#define LIBIMG_ENCODE_STRICT_BIT_DEPTH 0x02 +#define LIBIMG_ENCODE_DELETE_ALPHA 0x08 +#define LIBIMG_ENCODE_FLUSH_ALPHA 0x10 + +#define FLIP_VERTICAL 0x01 +#define FLIP_HORIZONTAL 0x02 + +#define ROTATE_90_CW 0x01 +#define ROTATE_180 0x02 +#define ROTATE_270_CW 0x03 +#define ROTATE_90_CCW ROTATE_270_CW +#define ROTATE_270_CCW ROTATE_90_CW + +extern Image* (*img_decode _stdcall)(void* file_data, uint32_t size, uint32_t b_color); +extern Image* (*img_encode _stdcall)(Image* img, uint32_t length, uint32_t option); +extern Image* (*img_create _stdcall)(uint32_t width, uint32_t height, uint32_t type); +extern void (*img_to_rgb2 _stdcall)(Image* img, void *rgb_data); +extern Image* (*img_to_rgb _stdcall)(Image* img); +extern bool (*img_flip _stdcall)(Image* img, uint32_t flip); +extern bool (*img_flip_layer _stdcall)(Image *img, uint32_t flip); +extern bool (*img_rotate _stdcall)(Image *img, uint32_t rotate); +extern bool (*img_rotate_layer _stdcall)(Image* data, uint32_t rotate); +extern void (*img_draw _stdcall)(Image *img, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t xoff, uint32_t yoff); +extern int32_t (*img_count _stdcall)(Image *img); +extern bool (*img_destroy _stdcall)(Image *img); +extern bool (*img_destroy_layer _stdcall)(Image* img); +extern Image* (*img_blend _stdcall)(Image* dst, Image* src, uint32_t out_x, uint32_t out_y, uint32_t in_x, uint32_t in_y, uint32_t width, uint32_t height); +extern Image* (*img_convert _stdcall)(Image *src, Image *dst, uint32_t dst_type, uint32_t, uint32_t); +extern Image* (*img_resize_data _stdcall)(Image *src, uint32_t width, uint32_t height); +extern Image* (*img_scale _stdcall)(Image* src, uint32_t crop_x, uint32_t crop_y, uint32_t crop_width, uint32_t crop_height, Image* dst, uint32_t scale_type, uint32_t inter, uint32_t new_width, uint32_t new_height); + +void img_fill_color(Image* img, uint32_t width, uint32_t height, uint32_t color){ + for (uint32_t i = 0; i < width*height; i++) { + img->Data[i] = color; + } +} + +#endif /* KOLIBRI_LIBIMG_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/msgbox.h b/programs/develop/ktcc/trunk/libc/include/clayer/msgbox.h new file mode 100644 index 0000000000..b0efe61f69 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/msgbox.h @@ -0,0 +1,55 @@ +#ifndef KOLIBRI_MSGBOX_H +#define KOLIBRI_MSGBOX_H +#include +#include +#include +#include + + +typedef struct { + uint8_t retval; // 0 - win closed, 1 to n - button num, also default button on start + uint8_t reserv; + char texts[2048]; // must be enough ;-) + char msgbox_stack[1024]; + uint32_t top_stack; +}__attribute__((packed)) msgbox; + +typedef void (*msgbox_callback)(void); + +extern void (*msgbox_create __attribute__((__stdcall__)))(msgbox *, void *thread); // clears callbacks, ! if fix lib, we can return eax as of Fn51 +extern void (*msgbox_setfunctions __attribute__((__stdcall__)))(msgbox_callback*); // must be called immediately after create, zero-ended array +extern void (*msgbox_reinit __attribute__((__stdcall__)))(msgbox *) ; // recalc sizes when structure changes, called auto when MsgBoxCreate + +static inline msgbox* kolibri_new_msgbox(char* title, char* text, int def_but, ...) +/// text can be multilined by code 13 = "\r" +/// def_but - highlighted and used on Enter (if zero - default is [X]), user may use Tabs or Arrows +/// last params are buttons text, max 8. last must set as NULL +{ + va_list vl=0; + va_start(vl, def_but); + msgbox* box = calloc(sizeof(msgbox), 1); + box->retval = (uint8_t)def_but; + char *pc = box->texts; + strcpy(pc, title); + pc += strlen(title) + 1; + strcpy(pc, text); + pc += strlen(text) + 1; + char *but_text = va_arg(vl, char*); + while (but_text) + { + strcpy(pc, but_text); + pc += strlen(but_text) + 1; + but_text = va_arg(vl, char*); + } + + va_end(vl); + return box; +} + +static inline void kolibri_start_msgbox(msgbox* box, msgbox_callback cb[]) +{ + (*msgbox_create)(box, &box->top_stack); + if (cb) (*msgbox_setfunctions)(cb); +} + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/clayer/rasterworks.h b/programs/develop/ktcc/trunk/libc/include/clayer/rasterworks.h new file mode 100644 index 0000000000..4c0aea79d2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/clayer/rasterworks.h @@ -0,0 +1,11 @@ +#ifndef KOLIBRI_RASTERWORKS_H +#define KOLIBRI_RASTERWORKS_H + +//extern int kolibri_rasterworks_init(void); + +extern void (*drawText __attribute__((__stdcall__)))(void *canvas, int x, int y, const char *string, int charQuantity, int fontColor, int params); +extern int (*countUTF8Z __attribute__((__stdcall__)))(const char *string, int byteQuantity); +extern int (*charsFit __attribute__((__stdcall__)))(int areaWidth, int charHeight); +extern int (*strWidth __attribute__((__stdcall__)))(int charQuantity, int charHeight); + +#endif /* KOLIBRI_RASTERWORKS_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/conio.h b/programs/develop/ktcc/trunk/libc/include/conio.h new file mode 100644 index 0000000000..5c231bd7af --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/conio.h @@ -0,0 +1,203 @@ +/* + +This is adapded thunk for console.obj sys library +.h is equal to svn:\programs\develop\libraries\console\console_en.txt + +Adapted for tcc by Siemargl, 2016 + +*/ +#ifndef __conio_h +#define __conio_h + +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) + +/* +console.obj exports the following functions +*/ +typedef unsigned int dword; /* 32-bit unsigned integer */ +typedef unsigned short word; /* 16-bit unsigned integer */ + +extern void stdcall (*con_init)(dword wnd_width, dword wnd_height, + dword scr_width, dword scr_height, const char* title); +/* Console initialization. Must be called only once. +wnd_width, wnd_height - width and height (in units of characters) of the visible +region; +scr_width, scr_height - width and height (in units of characters) of console; +Any of these four parameters can be set to -1 (=0xFFFFFFFF) +to use the library's default values; +title - console window's caption. */ + +extern void stdcall (*con_exit)(int bCloseWindow); +/* You should call this funstion at the end of the program. +If bCloseWindow is zero, the string "[Finished]" will be added to the caption of +the window and the console window will remain on the screen until the user +closes it. */ + +extern void stdcall (*con_set_title)(const char* title); +/* Set new window caption. */ + +extern void stdcall (*con_write_asciiz)(const char* str); +/* Display ASCIIZ-string to the console at the current position, shifting +the current position. */ + +extern void stdcall (*con_write_string)(const char* str, dword length); +/* Similar to con_write_asciiz, but length of the string must be given as a +separate parameter */ + +extern int cdecl (*con_printf)(const char* format, ...); +/* Standard "printf" function from ANSI C. */ + +extern dword stdcall (*con_get_flags)(void); +/* Get output flags. */ + +extern dword stdcall (*con_set_flags)(dword new_flags); +/* Set output flags. This function returns previous values. */ + +/* Flags (bitmask): */ +/* text color */ +#define CON_COLOR_BLUE 0x01 +#define CON_COLOR_GREEN 0x02 +#define CON_COLOR_RED 0x04 +#define CON_COLOR_BRIGHT 0x08 +/* background color */ +#define CON_BGR_BLUE 0x10 +#define CON_BGR_GREEN 0x20 +#define CON_BGR_RED 0x40 +#define CON_BGR_BRIGHT 0x80 +/* output controls */ +#define CON_IGNORE_SPECIALS 0x100 +/* if this flag is cleared, function interprets special characters: +10 ('\n') - next line +13 ('\r') - carriage return +8 ('\b') - backspace +9 ('\t') - tab +27 ('\033' = '\x1B') - the beginning of Esc-sequences; +otherwise, these characters will be displayed like ordinary characters. */ +/* Supported Esc-sequences: + Esc[;;m - choice of character attributes: + You can specify one, two or three codes in any order; + 0 = normal mode (white on black) + 1 = bright selection + 5 = bright background + 7 = inverse mode (black on white) + 30 = black characters + 31 = red characters + 32 = green characters + 33 = brown characters + 34 = blue characters + 35 = purple characters + 36 = turqoise characters + 37 = white characters + 40 = black background + 41 = red background + 42 = green background + 43 = brown background + 44 = blue background + 45 = purple background + 46 = turqoise background + 47 = white background + The following sequences appeared in version 5 of library: + Esc[2J - clear screen, move cursor to upper left corner + Esc[;H = Esc[;f - + move cursor to , + Esc[A - move cursor to lines up + Esc[B - move cursor to lines down + Esc[C - move cursor to positions right + Esc[D - move cursor to positions left +*/ +/* signal "console closed"; appeared in version 6; + ignored by con_set_flags */ +#define CON_WINDOW_CLOSED 0x200 +/* The default value for flags = 7. (grey text on black background) */ + +extern int stdcall (*con_get_font_height)(void); +/* Get the height of the font. */ + +extern int stdcall (*con_get_cursor_height)(void); +/* Get the height of the cursor. */ + +extern int stdcall (*con_set_cursor_height)(int new_height); +/* Set the height of the cursor. This function returns previous value. +An attempt to set the value out of the correct interval (from 0 to +font_height-1) is ignored. +Cursor with zero height isn't displayed. +Default value: - 15% from font height. */ + +extern int stdcall (*con_getch)(void); +/* Get one character from the keyboard. + +For normal characters function returns ASCII-code. For extended +characters (eg, Fx, and arrows), first function call returns 0 +and second call returns the extended code (similar to the DOS-function +input). Starting from version 7, after closing the console window, +this function returns 0. */ + +extern word stdcall (*con_getch2)(void); +/* Reads a character from the keyboard. Low byte contains the ASCII-code +(0 for extended characters), high byte - advanced code (like in BIOS +input functions). Starting from version 7, after closing the console +window, this function returns 0. */ + +extern int stdcall (*con_kbhit)(void); +/* Returns 1 if a key was pressed, 0 otherwise. To read pressed keys use +con_getch and con_getch2. Starting from version 6, after closing +the console window, this function returns 1. */ + +extern char* stdcall (*con_gets)(char* str, int n); +/* Reads a string from the keyboard. Reading is interrupted when got +"new line" character, or after reading the (n-1) characters (depending on +what comes first). In the first case the newline is also recorded in the +str. The acquired line is complemented by a null character. +Starting from version 6, the function returns a pointer to the entered +line if reading was successful, and NULL if the console window was closed. */ + +typedef int (stdcall * con_gets2_callback)(int keycode, char** pstr, int* pn, + int* ppos); + +extern char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n); +/* Con_gets completely analogous, except that when the user +press unrecognized key, it calls the specified callback-procedure +(which may, for example, handle up / down for history and tab to enter +autocompletion). You should pass to the procedure: key code and three pointers +- to the string, to the maximum length and to the current position. +function may change the contents of string and may change the string +itself (for example, to reallocate memory for increase the limit), +maximum length, and position of the line - pointers are passed for it. +Return value: 0 = line wasn't changed 1 = line changed, you should +remove old string and display new, 2 = line changed, it is necessary +to display it; 3 = immediately exit the function. +Starting from version 6, the function returns a pointer to the entered +line with the successful reading, and NULL if the console window was closed. */ + +extern void stdcall (*con_cls)(); +/* Clear screen and set cursor at upper left corner. */ + + +extern void stdcall (*con_get_cursor_pos)(int* px, int* py); +/* Wrote current (x) coordinate of cursor to *px, and (y) to *py. */ + +extern void stdcall (*con_set_cursor_pos)(int x, int y); +/* Set the cursor position to the specified coordinates. If any of the +parameters beyond the relevant range (from 0 to 1 scr_width- +for x, from 0 to 1 for scr_height-y, scr_width scr_height and were asked if +call con_init), then the corresponding coordinate of the cursor does not change. +*/ + +extern int __console_initdll_status; +/* == 1 if dll loaded */ + +extern dword *con_dll_ver; + +extern int con_init_console_dll(void); +/* load library and link function symbols. returns 1 if error +called automatic in printf, otherwise, see __console_initdll_status +*/ + +extern int con_init_console_dll_param(dword wnd_width, dword wnd_height, + dword scr_width, dword scr_height, const char* title); +/* work as con_init_console_dll, but call con_init with params +*/ + + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/aes.h b/programs/develop/ktcc/trunk/libc/include/cryptal/aes.h new file mode 100644 index 0000000000..25721c8cd3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/aes.h @@ -0,0 +1,123 @@ +/********************************************************************* +* Filename: aes.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding AES implementation. +*********************************************************************/ + +#ifndef AES_H +#define AES_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +/*********************** FUNCTION DECLARATIONS **********************/ +/////////////////// +// AES +/////////////////// +// Key setup must be done before any AES en/de-cryption functions can be used. +void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits + WORD w[], // Output key schedule to be used later + int keysize); // Bit length of the key, 128, 192, or 256 + +void aes_encrypt(const BYTE in[], // 16 bytes of plaintext + BYTE out[], // 16 bytes of ciphertext + const WORD key[], // From the key setup + int keysize); // Bit length of the key, 128, 192, or 256 + +void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext + BYTE out[], // 16 bytes of plaintext + const WORD key[], // From the key setup + int keysize); // Bit length of the key, 128, 192, or 256 + +/////////////////// +// AES - CBC +/////////////////// +int aes_encrypt_cbc(const BYTE in[], // Plaintext + size_t in_len, // Must be a multiple of AES_BLOCK_SIZE + BYTE out[], // Ciphertext, same length as plaintext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long + +// Only output the CBC-MAC of the input. +int aes_encrypt_cbc_mac(const BYTE in[], // plaintext + size_t in_len, // Must be a multiple of AES_BLOCK_SIZE + BYTE out[], // Output MAC + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long + +/////////////////// +// AES - CTR +/////////////////// +void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE + int counter_size); // Bytes of the IV used for counting (low end) + +void aes_encrypt_ctr(const BYTE in[], // Plaintext + size_t in_len, // Any byte length + BYTE out[], // Ciphertext, same length as plaintext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long + +void aes_decrypt_ctr(const BYTE in[], // Ciphertext + size_t in_len, // Any byte length + BYTE out[], // Plaintext, same length as ciphertext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long + +/////////////////// +// AES - CCM +/////////////////// +// Returns True if the input parameters do not violate any constraint. +int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext. + WORD plaintext_len, // IN - Plaintext length. + const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption. + unsigned short associated_data_len, // IN - Associated Data length in bytes. + const BYTE nonce[], // IN - The Nonce to be used for encryption. + unsigned short nonce_len, // IN - Nonce length in bytes. + BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC. + WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len. + WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16. + const BYTE key[], // IN - The AES key for encryption. + int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256. + +// Returns True if the input parameters do not violate any constraint. +// Use mac_auth to ensure decryption/validation was preformed correctly. +// If authentication does not succeed, the plaintext is zeroed out. To overwride +// this, call with mac_auth = NULL. The proper proceedure is to decrypt with +// authentication enabled (mac_auth != NULL) and make a second call to that +// ignores authentication explicitly if the first call failes. +int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC. + WORD ciphertext_len, // IN - Ciphertext length in bytes. + const BYTE assoc[], // IN - The Associated Data, required for authentication. + unsigned short assoc_len, // IN - Associated Data length in bytes. + const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption. + unsigned short nonce_len, // IN - Nonce length in bytes. + BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len. + WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len . + WORD mac_len, // IN - The length of the MAC that was calculated. + int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication. + const BYTE key[], // IN - The AES key for decryption. + int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256. + +/////////////////// +// Test functions +/////////////////// +int aes_test(); +int aes_ecb_test(); +int aes_cbc_test(); +int aes_ctr_test(); +int aes_ccm_test(); + +#endif // AES_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/arcfour.h b/programs/develop/ktcc/trunk/libc/include/cryptal/arcfour.h new file mode 100644 index 0000000000..f9f1e87df6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/arcfour.h @@ -0,0 +1,30 @@ +/********************************************************************* +* Filename: arcfour.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding ARCFOUR implementation. +*********************************************************************/ + +#ifndef ARCFOUR_H +#define ARCFOUR_H + +/*************************** HEADER FILES ***************************/ +#include + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte + +/*********************** FUNCTION DECLARATIONS **********************/ +// Input: state - the state used to generate the keystream +// key - Key to use to initialize the state +// len - length of key in bytes (valid lenth is 1 to 256) +void arcfour_key_setup(BYTE state[], const BYTE key[], int len); + +// Pseudo-Random Generator Algorithm +// Input: state - the state used to generate the keystream +// out - Must be allocated to be of at least "len" length +// len - number of bytes to generate +void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len); + +#endif // ARCFOUR_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/base64.h b/programs/develop/ktcc/trunk/libc/include/cryptal/base64.h new file mode 100644 index 0000000000..e35c6c7d9e --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/base64.h @@ -0,0 +1,27 @@ +/********************************************************************* +* Filename: base64.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding Base64 implementation. +*********************************************************************/ + +#ifndef BASE64_H +#define BASE64_H + +/*************************** HEADER FILES ***************************/ +#include + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte + +/*********************** FUNCTION DECLARATIONS **********************/ +// Returns the size of the output. If called with out = NULL, will just return +// the size of what the output would have been (without a terminating NULL). +size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag); + +// Returns the size of the output. If called with out = NULL, will just return +// the size of what the output would have been (without a terminating NULL). +size_t base64_decode(const BYTE in[], BYTE out[], size_t len); + +#endif // BASE64_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/blowfish.h b/programs/develop/ktcc/trunk/libc/include/cryptal/blowfish.h new file mode 100644 index 0000000000..d8e9d4a6a2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/blowfish.h @@ -0,0 +1,32 @@ +/********************************************************************* +* Filename: blowfish.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding Blowfish implementation. +*********************************************************************/ + +#ifndef BLOWFISH_H +#define BLOWFISH_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define BLOWFISH_BLOCK_SIZE 8 // Blowfish operates on 8 bytes at a time + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + WORD p[18]; + WORD s[4][256]; +} BLOWFISH_KEY; + +/*********************** FUNCTION DECLARATIONS **********************/ +void blowfish_key_setup(const BYTE user_key[], BLOWFISH_KEY *keystruct, size_t len); +void blowfish_encrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct); +void blowfish_decrypt(const BYTE in[], BYTE out[], const BLOWFISH_KEY *keystruct); + +#endif // BLOWFISH_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/des.h b/programs/develop/ktcc/trunk/libc/include/cryptal/des.h new file mode 100644 index 0000000000..1503772a2b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/des.h @@ -0,0 +1,37 @@ +/********************************************************************* +* Filename: des.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding DES implementation. + Note that encryption and decryption are defined by how + the key setup is performed, the actual en/de-cryption is + performed by the same function. +*********************************************************************/ + +#ifndef DES_H +#define DESH + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define DES_BLOCK_SIZE 8 // DES operates on 8 bytes at a time + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef enum { + DES_ENCRYPT, + DES_DECRYPT +} DES_MODE; + +/*********************** FUNCTION DECLARATIONS **********************/ +void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode); +void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6]); + +void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode); +void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6]); + +#endif // DES_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/md2.h b/programs/develop/ktcc/trunk/libc/include/cryptal/md2.h new file mode 100644 index 0000000000..97706af1e2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/md2.h @@ -0,0 +1,33 @@ +/********************************************************************* +* Filename: md2.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding MD2 implementation. +*********************************************************************/ + +#ifndef MD2_H +#define MD2_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define MD2_BLOCK_SIZE 16 + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte + +typedef struct { + BYTE data[16]; + BYTE state[48]; + BYTE checksum[16]; + int len; +} MD2_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void md2_init(MD2_CTX *ctx); +void md2_update(MD2_CTX *ctx, const BYTE data[], size_t len); +void md2_final(MD2_CTX *ctx, BYTE hash[]); // size of hash must be MD2_BLOCK_SIZE + +#endif // MD2_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/md5.h b/programs/develop/ktcc/trunk/libc/include/cryptal/md5.h new file mode 100644 index 0000000000..1370387cee --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/md5.h @@ -0,0 +1,34 @@ +/********************************************************************* +* Filename: md5.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding MD5 implementation. +*********************************************************************/ + +#ifndef MD5_H +#define MD5_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[4]; +} MD5_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void md5_init(MD5_CTX *ctx); +void md5_update(MD5_CTX *ctx, const BYTE data[], size_t len); +void md5_final(MD5_CTX *ctx, BYTE hash[]); + +#endif // MD5_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/rot-13.h b/programs/develop/ktcc/trunk/libc/include/cryptal/rot-13.h new file mode 100644 index 0000000000..4c581c39a0 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/rot-13.h @@ -0,0 +1,20 @@ +/********************************************************************* +* Filename: rot-13.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding ROT-13 implementation. +*********************************************************************/ + +#ifndef ROT13_H +#define ROT13_H + +/*************************** HEADER FILES ***************************/ +#include + +/*********************** FUNCTION DECLARATIONS **********************/ +// Performs IN PLACE rotation of the input. Assumes input is NULL terminated. +// Preserves each charcter's case. Ignores non alphabetic characters. +void rot13(char str[]); + +#endif // ROT13_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/sha1.h b/programs/develop/ktcc/trunk/libc/include/cryptal/sha1.h new file mode 100644 index 0000000000..f32bb7c04d --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/sha1.h @@ -0,0 +1,35 @@ +/********************************************************************* +* Filename: sha1.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding SHA1 implementation. +*********************************************************************/ + +#ifndef SHA1_H +#define SHA1_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[5]; + WORD k[4]; +} SHA1_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void sha1_init(SHA1_CTX *ctx); +void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len); +void sha1_final(SHA1_CTX *ctx, BYTE hash[]); + +#endif // SHA1_H diff --git a/programs/develop/ktcc/trunk/libc/include/cryptal/sha256.h b/programs/develop/ktcc/trunk/libc/include/cryptal/sha256.h new file mode 100644 index 0000000000..7123a30dd4 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/cryptal/sha256.h @@ -0,0 +1,34 @@ +/********************************************************************* +* Filename: sha256.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding SHA1 implementation. +*********************************************************************/ + +#ifndef SHA256_H +#define SHA256_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[8]; +} SHA256_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void sha256_init(SHA256_CTX *ctx); +void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); +void sha256_final(SHA256_CTX *ctx, BYTE hash[]); + +#endif // SHA256_H diff --git a/programs/develop/ktcc/trunk/libc/include/ctype.h b/programs/develop/ktcc/trunk/libc/include/ctype.h new file mode 100644 index 0000000000..c6fd00cd1a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/ctype.h @@ -0,0 +1,41 @@ +#ifndef _CTYPE_H +#define _CTYPE_H +/* +** All character classification functions except isascii(). +** Integer argument (c) must be in ASCII range (0-127) for +** dependable answers. +*/ + +#define __ALNUM 1 +#define __ALPHA 2 +#define __CNTRL 4 +#define __DIGIT 8 +#define __GRAPH 16 +#define __LOWER 32 +#define __PRINT 64 +#define __PUNCT 128 +#define __BLANK 256 +#define __UPPER 512 +#define __XDIGIT 1024 + +extern unsigned short __is[129]; + +#define isalnum(c)(__is[c+1] & __ALNUM ) /* 'a'-'z', 'A'-'Z', '0'-'9' */ +#define isalpha(c)(__is[c+1] & __ALPHA ) /* 'a'-'z', 'A'-'Z' */ +#define iscntrl(c)(__is[c+1] & __CNTRL ) /* 0-31, 127 */ +#define isdigit(c)(__is[c+1] & __DIGIT ) /* '0'-'9' */ +#define isgraph(c)(__is[c+1] & __GRAPH ) /* '!'-'~' */ +#define islower(c)(__is[c+1] & __LOWER ) /* 'a'-'z' */ +#define isprint(c)(__is[c+1] & __PRINT ) /* ' '-'~' */ +#define ispunct(c)(__is[c+1] & __PUNCT ) /* !alnum && !cntrl && !space */ +#define isspace(c)(__is[c+1] & __BLANK ) /* HT, LF, VT, FF, CR, ' ' */ +#define isupper(c)(__is[c+1] & __UPPER ) /* 'A'-'Z' */ +#define isxdigit(c)(__is[c+1] & __XDIGIT) /* '0'-'9', 'a'-'f', 'A'-'F' */ + +#define isascii(c) (!((c)&(~0x7f))) +#define toascii(c) ((c)&0x7f) + +extern unsigned char tolower(unsigned char c); +extern unsigned char toupper(unsigned char c); + +#endif \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/include/dir.h b/programs/develop/ktcc/trunk/libc/include/dir.h new file mode 100644 index 0000000000..d4f542cf24 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/dir.h @@ -0,0 +1,35 @@ +#ifndef _DIR_H +#define _DIR_H + +#include + +#define rmfile(obj) rmdir(obj) +#define PATH_MAX 4096 +#define T_FOLDER 16 +#define T_FILE 0 +#define FS_ERROR -1 + +typedef struct { + unsigned type; + char *name; +} short_file_info; + +//Writes information about files in the "dir" folder to an struct array"list". Returns the number of files. +int lsdir(const char* dir, short_file_info **list); + +// Get the path to the working directory(if buf is NULL, then memory will be allocated automatically) +char *getcwd(char *buf, unsigned size); + +// Set path to working directory +void setcwd(const char* cwd); + +// Delete empty folder (returns "true" if successful) +bool rmdir(const char* dir); + +// Delete a file (returns "true" if successful) +bool rmfile(const char* name); + +// Create a foldery (returns "true" if successful) +bool mkdir(const char* dir); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/dlfcn.h b/programs/develop/ktcc/trunk/libc/include/dlfcn.h new file mode 100644 index 0000000000..019d10ee7a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/dlfcn.h @@ -0,0 +1,14 @@ +#ifndef _DLFCN_H +#define _DLFCN_H + +#define RTLD_LAZY 0x00001 +#define RTLD_NOW 0x00002 +#define RTLD_GLOBAL 0x00100 +#define RTLD_LOCAL 0 + +int dlclose(void *handle); +char *dlerror(void); +void *dlopen(const char *name, int mode); +void *dlsym(void *restrict handle, const char *restrict name); + +#endif \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/include/errno.h b/programs/develop/ktcc/trunk/libc/include/errno.h new file mode 100644 index 0000000000..5890ae5305 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/errno.h @@ -0,0 +1,42 @@ +#ifndef _ERRNO_H +#define _ERRNO_H + +extern int errno; +/* errors codes from KOS, but minus */ +# define E_SUCCESS (0) +# define E_UNSUPPORTED (-2) +# define E_UNKNOWNFS (-3) +# define E_NOTFOUND (-5) +# define E_EOF (-6) +# define E_INVALIDPTR (-7) +# define E_DISKFULL (-8) +# define E_FSYSERROR (-9) +# define E_ACCESS (-10) +# define E_HARDWARE (-11) +# define E_NOMEM (-12) +/* conversion errors */ +# define ERANGE (-20) +# define EINVAL (-21) +/* program run and pipe errors */ +# define E_NOMEM2 (-30) +# define E_FILEFMT (-31) +# define E_TOOMANY (-32) +# define E_PARAM (-33) +/* socket error codes*/ +#define ENOBUFS 1 +#define EINPROGRESS 2 +#define EOPNOTSUPP 4 +#define EWOULDBLOCK 6 +#define ENOTCONN 9 +#define EALREADY 10 +#define EINVALUE 11 +#define EMSGSIZE 12 +#define ENOMEM 18 +#define EADDRINUSE 20 +#define ECONNREFUSED 61 +#define ECONNRESET 52 +#define EISCONN 56 +#define ETIMEDOUT 60 +#define ECONNABORTED 53 + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/float.h b/programs/develop/ktcc/trunk/libc/include/float.h new file mode 100644 index 0000000000..a2b4ed26a0 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/float.h @@ -0,0 +1,65 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#define FLT_RADIX 2 + +/* IEEE float */ +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +/* IEEE double */ +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +/* horrible intel long double */ +#if defined __i386__ || defined __x86_64__ + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 + +#else + +/* same as IEEE double */ +#define LDBL_MANT_DIG 53 +#define LDBL_DIG 15 +#define LDBL_EPSILON 2.2204460492503131e-16 +#define LDBL_MIN_EXP (-1021) +#define LDBL_MIN 2.2250738585072014e-308 +#define LDBL_MIN_10_EXP (-307) +#define LDBL_MAX_EXP 1024 +#define LDBL_MAX 1.7976931348623157e+308 +#define LDBL_MAX_10_EXP 308 + +#endif + +#ifndef NAN +# define NAN (__nan__) +#endif + +#ifndef INFINITY +# define INFINITY (__inf__) +#endif + +#endif /* _FLOAT_H_ */ diff --git a/programs/develop/ktcc/trunk/libc/include/getopt.h b/programs/develop/ktcc/trunk/libc/include/getopt.h new file mode 100644 index 0000000000..d41eaa0568 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/getopt.h @@ -0,0 +1,9 @@ +#ifndef GETOPT_H +#define GETOPT_H + +extern int optind, opterr; +extern char *optarg; + +int getopt(int argc, char *argv[], char *optstring); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/kolibrisys.h b/programs/develop/ktcc/trunk/libc/include/kolibrisys.h new file mode 100644 index 0000000000..b4a4fd79f8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/kolibrisys.h @@ -0,0 +1,197 @@ +#ifndef kolibrisys_h +#define kolibrisys_h +/* +#ifdef GNUC +#define stdcall __stdcall +#define cdecl __cdecl +#else +#define stdcall ((__stdcall)) +#define cdecl ((__cdecl)) +#endif +*/ +//#ifdef GNUC +//#define stdcall __stdcall +//#else +#define cdecl __attribute__ ((cdecl)) +#define stdcall __attribute__ ((stdcall)) +//#endif +typedef void* func_ptr; +typedef unsigned int dword; +typedef unsigned char byte; +typedef unsigned short word; + +typedef unsigned int fpos_t; +typedef unsigned int size_t; +#pragma pack(push,1) +struct process_table_entry{ + int cpu_usage; //+0 + int window_pos_info; //+4 + short int reserved1; //+8 + char name[12]; //+10 + int memstart; //+22 + int memused; //+26 + int pid; //+30 + int winx_start; //+34 + int winy_start; //+38 + int winx_size; //+42 + int winy_size; //+46 + short int slot_info; //+50 + short int reserved2; //+52 + int clientx; //+54 + int clienty; //+58 + int clientwidth; //+62 + int clientheight; //+66 + unsigned char window_state;//+70 + char reserved3[1024-71]; //+71 +}; +#pragma pack(pop) + +//----------------------------------------------------------------------------------- +//------------------------KolibriOS system acces to files---------------------------- +//----------------------------------------------------------------------------------- +extern dword stdcall _ksys_get_filesize(char *filename); +extern dword stdcall _ksys_readfile(char *filename,dword pos,dword blocksize,void *data, int *preadbytes); +extern dword stdcall _ksys_rewritefile(char *filename,dword blocksize,void *data); +extern dword stdcall _ksys_appendtofile(char *filename,dword pos,dword blocksize,void *data); +//----------------------------------------------------------------------------------- + +//----------------------Run program--------------------------------------------------- +extern void stdcall _ksys_run_program(char* filename,char* parameters); +//------------------------------------------------------------------------------------ + +//--------------------Debug output--------------------------------------------------- +extern void stdcall _ksys_debug_out(int c); +extern void stdcall debug_out_str(char* str); +//----------------------------------------------------------------------------------- + +//--------------------------Mouse state---------------------------------------------- +extern int stdcall _ksys_GetMouseXY(void); +extern int stdcall _ksys_GetMouseButtonsState(void); +//----------------------------------------------------------------------------------- + +//--------------------------get skin height------------------------------------------ +extern int stdcall _ksys_get_skin_height(void); +//----------------------------------------------------------------------------------- + +//----------------------------background--------------------------------------------- +extern void stdcall _ksys_set_background_size(int xsize,int ysize); +extern void stdcall _ksys_write_background_mem(int pos,int color); +extern void stdcall _ksys_draw_background(void); +extern void stdcall _ksys_set_background_draw_type(int type); +extern void stdcall _ksys_background_blockmove(void* src,int bgr_pos, int count); +//----------------------------------------------------------------------------------- + +//----------------------------functionf for draw window,lines.bar,etc.--------------- +extern void stdcall _ksys_draw_window(int xcoord,int ycoord, int xsize, + int ysize,int workcolor,int type, + int captioncolor,int windowtype,int bordercolor); +extern void stdcall _ksys_window_redraw(int status); +extern int stdcall _ksys_putpixel(int x,int y,int color); +extern void stdcall _ksys_draw_bar(int x, int y, int xsize, int ysize, int color); +extern void stdcall _ksys_line(int x1,int y1,int x2,int y2,int color); +extern void stdcall _ksys_putimage(int x, int y, int xsize, int ysize, void* image); +//----------------------------------------------------------------------------------- + +//--------------------------write text(system fonts 6x9)----------------------------- +extern void stdcall _ksys_write_text(int x,int y,int color,char* text,int len); +//----------------------------------------------------------------------------------- + +//------------------ get screen size and bytes per pixel--------------------------- +extern int stdcall _ksys_get_screen_size(int* x,int* y); +extern void stdcall _ksys_dga_get_resolution(int* xres, int* yres, int* bpp, int* bpscan); +//----------------------------------------------------------------------------------- + +//-------------------------------craete thread--------------------------------------- +extern void* stdcall _ksys_start_thread(void (* func_ptr)(void),int stack_size,int* pid); +//----------------------------------------------------------------------------------- + +//------------------system button(Old function. Better use libGUI functions.)-------- +extern void stdcall _ksys_make_button(int x, int y, int xsize, int ysize, int id, int color); +extern int stdcall _ksys_get_button_id(void); //get state of system button +//------------------------------------------------------------------------------------ + +//----------------------system clock(in 1/100 sec.) and date-------------------------- +extern int stdcall _ksys_get_system_clock(void); +extern int stdcall _ksys_get_date(void); +//------------------------------------------------------------------------------------ + +//-------------------------system delay(in 1/100 sec.)------------------------------- +extern void stdcall _ksys_delay(int m); +//----------------------------------------------------------------------------------- + +//------------------------system events---------------------------------------------- +extern int stdcall _ksys_wait_for_event_infinite(void); +extern int stdcall _ksys_check_for_event(void); +extern int stdcall _ksys_wait_for_event(int time); +extern void stdcall _ksys_set_wanted_events(int ev); +//----------------------------------------------------------------------------------- + +//----------------------------system exit program------------------------------------ +extern void stdcall _ksys_exit(void); +//----------------------------------------------------------------------------------- + +//-----------------------------system IPC send message------------------------------- +extern void stdcall _ksys_send_message(int pid, void* msg, int size); +//----------------------------------------------------------------------------------- + +//---------------------------system work with IRQ from user mode--------------------- +extern void stdcall _ksys_define_receive_area(void* area, int size); +extern int stdcall _ksys_get_irq_owner(int irq); +extern int stdcall _ksys_get_data_read_by_irq(int irq, int* size, void* data); +extern int stdcall _ksys_send_data_to_device(int port, unsigned char val); +extern int stdcall _ksys_receive_data_from_device(int port,unsigned char* data); +extern void stdcall _ksys_program_irq(void* intrtable, int irq); +extern void stdcall _ksys_reserve_irq(int irq); +extern void stdcall _ksys_free_irq(int irq); +//---------------------------------------------------------------------------------- + +//----------------------------system reserve diapason of ports---------------------- +extern int stdcall _ksys_reserve_port_area(int start,int end); +extern int stdcall _ksys_free_port_area(int start,int end); +//---------------------------------------------------------------------------------- + +//-------------functions get key and set keyboard mode------------------------------ +extern int stdcall _ksys_get_key(void); +extern void stdcall _ksys_set_keyboard_mode(int mode); +//---------------------------------------------------------------------------------- + +//--------------simple work with MPU401 sound device--------------------------------- +extern void stdcall _ksys_midi_reset(void); +extern void stdcall _ksys_midi_send(int data); +//----------------------------------------------------------------------------------- + +//--------------------------acces to PCI BUS from user mode--------------------------- +extern int stdcall _ksys_get_pci_version(void); +extern int stdcall _ksys_get_last_pci_bus(void); +extern int stdcall _ksys_get_pci_access_mechanism(void); +extern int stdcall _ksys_pci_read_config_byte(int bus,int dev,int fn,int reg); +extern int stdcall _ksys_pci_read_config_word(int bus,int dev,int fn,int reg); +extern int stdcall _ksys_pci_read_config_dword(int bus,int dev,int fn,int reg); +extern int stdcall _ksys_pci_write_config_byte(int bus,int dev,int fn,int reg,int value); +extern int stdcall _ksys_pci_write_config_word(int bus,int dev,int fn,int reg,int value); +extern int stdcall _ksys_pci_write_config_dword(int bus,int dev,int fn,int reg,int value); +//-------------------------------------------------------------------------------------- + +//------------------------Working with processes-------------------------------------- +extern int stdcall _ksys_get_process_table(struct process_table_entry *proctab,int pid); //if pid=-1 than get info about him. +extern int stdcall _ksys_kill_process(int pid); // if it returns -1 then an error. +//--------------------------------------------------------------------------------- + +//-----------------Old functions for work with sound(Sound Blaster only).--------- +extern void stdcall _ksys_sound_load_block(void* blockptr); +extern void stdcall _ksys_sound_play_block(void); +extern void stdcall _ksys_sound_set_channels(int channels); +extern void stdcall _ksys_sound_set_data_size(int size); +extern void stdcall _ksys_sound_set_frequency(int frequency); +//-------------------------------------------------------------------------------- + +//------------------------------system speaker(integrated speaker)---------------- +extern void stdcall _ksys_sound_speaker_play(void* data); +//-------------------------------------------------------------------------------- + +//------------------function for work with Dinamic Link Librarys(DLL)-------------- +extern dword* stdcall _ksys_cofflib_load(char* name); +extern func_ptr stdcall _ksys_cofflib_getproc(void* exp,char* sz_name); +//--------------------------------------------------------------------------------- + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/kos32sys1.h b/programs/develop/ktcc/trunk/libc/include/kos32sys1.h new file mode 100644 index 0000000000..48e6f9d3dd --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/kos32sys1.h @@ -0,0 +1,815 @@ +#ifndef __KOS_32_SYS_H__ +#define __KOS_32_SYS_H__ + +#include +#include +typedef unsigned int uint32_t; +typedef int int32_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned long long uint64_t; + +#ifdef __cplusplus +extern "C" { +#endif + +#define TYPE_3_BORDER_WIDTH 5 +#define WIN_STATE_MINIMIZED 0x02 +#define WIN_STATE_ROLLED 0x04 +#define POS_SCREEN 0 +#define POS_WINDOW 1 + +#define IPC_NOBUFFER 1 +#define IPC_LOCKED 2 +#define IPC_OVERFLOW 3 +#define IPC_NOPID 4 + +#define SHM_OPEN 0x00 +#define SHM_OPEN_ALWAYS 0x04 +#define SHM_CREATE 0x08 +#define SHM_READ 0x00 +#define SHM_WRITE 0x01 + +// for clipboard funtions +#define CLIP_UTF 0 +#define CLIP_CP866 1 +#define CLIP_CP1251 2 +#define CLIP_TEXT 0 +#define CLIP_IMAGE 1 +#define CLIP_RAW 2 + +//Read/Write data as type (int char, etc.) at address "addr" with offset "offset". eg DATA(int, buff, 8); +#define DATA(type, addr, offset) *((type*)((uint8_t*)addr+offset)) +#define X_W(X, W) ((X<<16)+W) +#define Y_H X_W + +typedef struct { + uint8_t blue; + uint8_t green; + uint8_t red; +}RGB; + +typedef unsigned int color_t; + +typedef union __attribute__((packed)) pos_t +{ + uint32_t val; + struct + { + short x; + short y; + }; +} pos_t; + + +typedef union __attribute__((packed)) oskey_t +{ + uint32_t val; + struct + { + uint8_t state; + uint8_t code; + uint16_t ctrl_key; + }; +} oskey_t; + +typedef struct +{ + unsigned handle; + unsigned io_code; + void *input; + int inp_size; + void *output; + int out_size; +}ioctl_t; + +typedef union +{ + struct + { + void *data; + size_t size; + } x; + unsigned long long raw; +}ufile_t; + +struct kolibri_system_colors { + color_t frame_area; + color_t grab_bar; + color_t grab_bar_button; + color_t grab_button_text; + color_t grab_text; + color_t work_area; + color_t work_button; + color_t work_button_text; + color_t work_text; + color_t work_graph; +}; + + +struct blit_call +{ + int dstx; + int dsty; + int w; + int h; + + int srcx; + int srcy; + int srcw; + int srch; + + void *bitmap; + int stride; +}; + +struct ipc_message +{ + uint32_t pid; // PID of sending thread + uint32_t datalen; // data bytes + char data[0]; // data begin +}; + +struct ipc_buffer +{ + uint32_t lock; // nonzero is locked + uint32_t used; // used bytes in buffer + struct ipc_message data[0]; // data begin +}; + +static inline void begin_draw(void) +{ + __asm__ __volatile__( + "int $0x40" ::"a"(12),"b"(1)); +}; + +static inline +void end_draw(void) +{ + __asm__ __volatile__( + "int $0x40" ::"a"(12),"b"(2)); +}; + +static inline +void sys_create_window(int x, int y, int w, int h, const char *name, + color_t workcolor, uint32_t style) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(0), + "b"((x << 16) | ((w-1) & 0xFFFF)), + "c"((y << 16) | ((h-1) & 0xFFFF)), + "d"((style << 24) | (workcolor & 0xFFFFFF)), + "D"(name), + "S"(0) : "memory"); +}; + +static inline +void sys_change_window(int new_x, int new_y, int new_w, int new_h) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(67), "b"(new_x), "c"(new_y), "d"(new_w),"S"(new_h) + ); +} + + +static inline +void define_button(uint32_t x_w, uint32_t y_h, uint32_t id, uint32_t color) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(8), + "b"(x_w), + "c"(y_h), + "d"(id), + "S"(color)); +}; + +static inline +void draw_line(int xs, int ys, int xe, int ye, color_t color) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(38), "d"(color), + "b"((xs << 16) | xe), + "c"((ys << 16) | ye)); +} + +static inline +void draw_bar(int x, int y, int w, int h, color_t color) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(13), "d"(color), + "b"((x << 16) | w), + "c"((y << 16) | h)); +} + +static inline +void draw_bitmap(void *bitmap, int x, int y, int w, int h) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(7), "b"(bitmap), + "c"((w << 16) | h), + "d"((x << 16) | y)); +} + +static inline +void draw_text_sys(const char *text, int x, int y, int len, color_t color) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(4),"d"(text), + "b"((x << 16) | y), + "S"(len),"c"(color) + :"memory"); +} +static inline +void draw_text_sys_bg(const char *text, int x, int y, int len, color_t color, color_t bg) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(4),"d"(text), + "b"((x << 16) | y), + "S"(len),"c"(color), "D"(bg) + :"memory"); +} + + +static inline +uint32_t get_skin_height(void) +{ + uint32_t height; + + __asm__ __volatile__( + "int $0x40 \n\t" + :"=a"(height) + :"a"(48),"b"(4)); + return height; +}; + +static inline +pos_t get_mouse_pos(int origin) +{ + pos_t val; + + __asm__ __volatile__( + "int $0x40 \n\t" + "rol $16, %%eax" + :"=a"(val) + :"a"(37),"b"(origin)); + return val; +} + +static inline +uint32_t get_mouse_buttons(void) +{ + uint32_t val; + + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(37),"b"(2)); + return val; +}; + +static inline +uint32_t get_mouse_wheels(void) +{ + uint32_t val; + + __asm__ __volatile__( + "int $0x40 \n\t" + :"=a"(val) + :"a"(37),"b"(7)); + return val; +}; + +static inline uint32_t load_cursor(void *path, uint32_t flags) +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(37), "b"(4), "c"(path), "d"(flags)); + return val; +} + +static inline uint32_t set_cursor(uint32_t cursor) +{ + uint32_t old; + __asm__ __volatile__( + "int $0x40" + :"=a"(old) + :"a"(37), "b"(5), "c"(cursor)); + return old; +}; + +static inline int destroy_cursor(uint32_t cursor) +{ + int ret; + __asm__ __volatile__( + "int $0x40" + :"=a"(ret) + :"a"(37), "b"(6), "c"(cursor) + :"memory"); + return ret; +}; + + +static inline +uint32_t wait_for_event(uint32_t time) +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(23), "b"(time)); + return val; +}; + +static inline uint32_t check_os_event() +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(11)); + return val; +}; + +static inline uint32_t get_os_event() +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(10)); + return val; +}; + +static inline +uint32_t get_tick_count(void) +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(26),"b"(9)); + return val; +}; + +static inline +uint64_t get_ns_count(void) +{ + uint64_t val; + __asm__ __volatile__( + "int $0x40" + :"=A"(val) + :"a"(26), "b"(10)); + return val; +}; + +static inline oskey_t get_key(void) +{ + oskey_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(2)); + return val; +} + +static inline +uint32_t get_os_button() +{ + uint32_t val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(17)); + return val>>8; +}; + +static inline uint32_t get_service(char *name) +{ + uint32_t retval = 0; + __asm__ __volatile__( + "int $0x40" + :"=a"(retval) + :"a"(68),"b"(16),"c"(name) + :"memory"); + + return retval; +}; + +static inline int call_service(ioctl_t *io) +{ + int retval; + + __asm__ __volatile__( + "int $0x40" + :"=a"(retval) + :"a"(68),"b"(17),"c"(io) + :"memory","cc"); + + return retval; +}; + + +static inline void yield(void) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(68), "b"(1)); +}; + +static inline void delay(uint32_t time) +{ + __asm__ __volatile__( + "int $0x40" + ::"a"(5), "b"(time) + :"memory"); +}; + +static inline +void *user_alloc(size_t size) +{ + void *val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(68),"b"(12),"c"(size)); + return val; +} + +static inline +int user_free(void *mem) +{ + int val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(68),"b"(13),"c"(mem)); + return val; +} + +static inline +void* user_realloc(void *mem, size_t size) +{ + void *val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(68),"b"(20),"c"(size),"d"(mem) + :"memory"); + + return val; +}; + +static inline +int *user_unmap(void *base, size_t offset, size_t size) +{ + int *val; + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(68),"b"(26),"c"(base),"d"(offset),"S"(size)); + return val; +}; + +static inline ufile_t load_file(const char *path) +{ + ufile_t uf; + + __asm__ __volatile__ ( + "int $0x40" + :"=A"(uf.raw) + :"a" (68), "b"(27),"c"(path)); + + return uf; +}; + +static inline int GetScreenSize() +{ + int retval; + + __asm__ __volatile__( + "int $0x40" + :"=a"(retval) + :"a"(61), "b"(1)); + return retval; +} + + +static inline void get_proc_info(char *info) +{ + __asm__ __volatile__( + "int $0x40" + : + :"a"(9), "b"(info), "c"(-1) + :"memory"); +}; + +static inline void Blit(void *bitmap, int dst_x, int dst_y, + int src_x, int src_y, int w, int h, + int src_w, int src_h, int stride) +{ + volatile struct blit_call bc; + + bc.dstx = dst_x; + bc.dsty = dst_y; + bc.w = w; + bc.h = h; + bc.srcx = src_x; + bc.srcy = src_y; + bc.srcw = src_w; + bc.srch = src_h; + bc.stride = stride; + bc.bitmap = bitmap; + + __asm__ __volatile__( + "int $0x40" + ::"a"(73),"b"(0),"c"(&bc.dstx)); +}; + + +// newlib exclusive +#ifndef __TINYC__ +int create_thread(int (*proc)(void *param), void *param, int stack_size); + +void* load_library(const char *name); + +void* get_proc_address(void *handle, const char *proc_name); + +void enumerate_libraries(int (*callback)(void *handle, const char* name, + uint32_t base, uint32_t size, void *user_data), + void *user_data); +#endif + +// May be next section need to be added in newlibc + +enum KOLIBRI_GUI_EVENTS { + KOLIBRI_EVENT_NONE = 0, /* Event queue is empty */ + KOLIBRI_EVENT_REDRAW = 1, /* Window and window elements should be redrawn */ + KOLIBRI_EVENT_KEY = 2, /* A key on the keyboard was pressed */ + KOLIBRI_EVENT_BUTTON = 3, /* A button was clicked with the mouse */ + KOLIBRI_EVENT_DESKTOP = 5, /* Desktop redraw finished */ + KOLIBRI_EVENT_MOUSE = 6, /* Mouse activity (movement, button press) was detected */ + KOLIBRI_EVENT_IPC = 7, /* Interprocess communication notify */ + KOLIBRI_EVENT_NETWORK = 8, /* Network event */ + KOLIBRI_EVENT_DEBUG = 9, /* Debug subsystem event */ + KOLIBRI_EVENT_IRQBEGIN = 16 /* 16..31 IRQ0..IRQ15 interrupt =IRQBEGIN+IRQn */ +}; + + +// copied from /programs/system/shell/system/kolibri.c +// fn's returned -1 as syserror, 1 as error, 0 as OK +static inline +int kol_clip_num() +{ + register uint32_t val; + asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(0)); + return val; +} + +static inline +char* kol_clip_get(int n) +// returned buffer must be freed by user_free() +{ + register char* val; + asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(1), "c"(n)); + return val; +} + +static inline +int kol_clip_set(int n, char buffer[]) +{ + register uint32_t val; + asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(2), "c"(n), "d"(buffer)); + return val; +} + +static inline +int kol_clip_pop() +{ + register uint32_t val; + asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(3)); + return val; +} + +static inline +int kol_clip_unlock() +{ + register uint32_t val; + asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(4)); + return val; +} + +static inline void get_system_colors(struct kolibri_system_colors *color_table) +{ + __asm__ volatile ("int $0x40" + : + :"a"(48),"b"(3),"c"(color_table),"d"(40) + ); + + /* color_table should point to the system color table */ +} + +static inline void debug_board_write_byte(const char ch){ + __asm__ __volatile__( + "int $0x40" + : + :"a"(63), "b"(1), "c"(ch)); +} + + +static inline void draw_number_sys(int32_t number, int x, int y, int len, color_t color){ + register uint32_t fmt; + fmt = len << 16 | 0x80000000; // no leading zeros + width +// fmt = len << 16 | 0x00000000; // leading zeros + width + __asm__ __volatile__( + "int $0x40" + : + :"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color)); +} + +static inline void draw_number_sys_bg(int32_t number, int x, int y, int len, color_t color, color_t bg){ + register uint32_t fmt; + fmt = len << 16 | 0x80000000; // no leading zeros + width +// fmt = len << 16 | 0x00000000; // leading zeros + width + __asm__ __volatile__( + "int $0x40" + : + :"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color), "D"(bg)); +} + +static inline +uint32_t get_mouse_eventstate(void) +{ + uint32_t val; + + __asm__ __volatile__( + "int $0x40" + :"=a"(val) + :"a"(37),"b"(3)); + return val; +}; + +static inline +uint32_t set_event_mask(uint32_t mask) +{ + register uint32_t val; + asm volatile ("int $0x40":"=a"(val):"a"(40), "b"(mask)); + return val; +} + +typedef void (*thread_proc)(void*); + +static inline +int start_thread(thread_proc proc, char* stack_top) +{ + register int val; + asm volatile ("int $0x40":"=a"(val):"a"(51), "b"(1), "c"(proc), "d"(stack_top)); + return val; +} + +static inline +void kos_exit() +{ + asm volatile ("int $0x40"::"a"(-1)); +} + +static inline void focus_window(int slot){ + asm volatile ("int $0x40"::"a"(18), "b"(3), "c"(slot)); +} + +static inline int get_thread_slot(int tid){ + register int val; + asm volatile ("int $0x40":"=a"(val):"a"(18), "b"(21), "c"(tid)); + return val; +} + +static inline void set_current_folder(char* dir){ + asm volatile ("int $0x40"::"a"(30), "b"(1), "c"(dir)); +} + +static inline int get_current_folder(char* buf, int bufsize){ + register int val; + asm volatile ("int $0x40":"=a"(val):"a"(30), "b"(2), "c"(buf), "d"(bufsize)); + return val; +} + +static inline +void ipc_set_area(void* buf, int bufsize){ + asm volatile ("int $0x40"::"a"(60), "b"(1), "c"(buf), "d"(bufsize)); +} + +static inline +int ipc_send_message(int pid_reciever, void *data, int datalen) { + register int val; + asm volatile ("int $0x40":"=a"(val):"a"(60), "b"(2), "c"(pid_reciever), "d"(data), "S"(datalen)); + return val; +} + +static inline +void* shm_open(char *shm_name, int msize, int flags, int *retsz){ + register int val, cod; + asm volatile ("int $0x40":"=a"(val),"=d"(cod):"a"(68), "b"(22), "c"(shm_name), "d"(msize), "S"(flags)); + + if(retsz) *retsz = cod; // errcode if NULL or memsize when open + return (void*)val; +} + +static inline +void shm_close(char *shm_name){ + asm volatile ("int $0x40"::"a"(68), "b"(23), "c"(shm_name)); +} + +static inline +int start_app(char *app_name, char *args){ + struct file_op_t + { + uint32_t fn; + uint32_t flags; + char* args; + uint32_t res1, res2; + char zero; + char* app_name __attribute__((packed)); + } file_op; + memset(&file_op, 0, sizeof(file_op)); + file_op.fn = 7; + file_op.args = args; + file_op.app_name = app_name; + + register int val; + asm volatile ("int $0x40":"=a"(val):"a"(70), "b"(&file_op)); + + return val; +} + +// TinyC don't support aliasing of static inline funcs, but support #define :) +#ifndef __TINYC__ +static inline void BeginDraw(void) __attribute__ ((alias ("begin_draw"))); +static inline void EndDraw(void) __attribute__ ((alias ("end_draw"))); +static inline void DrawWindow(int x, int y, int w, int h, const char *name, + color_t workcolor, uint32_t style) + __attribute__ ((alias ("sys_create_window"))); +static inline void DefineButton(void) __attribute__ ((alias ("define_button"))); +static inline void DrawLine(int xs, int ys, int xe, int ye, color_t color) + __attribute__ ((alias ("draw_line"))); +static inline void DrawBar(int x, int y, int w, int h, color_t color) + __attribute__ ((alias ("draw_bar"))); +static inline void DrawBitmap(void *bitmap, int x, int y, int w, int h) + __attribute__ ((alias ("draw_bitmap"))); +static inline uint32_t GetSkinHeight(void) __attribute__ ((alias ("get_skin_height"))); +static inline pos_t GetMousePos(int origin) __attribute__ ((alias ("get_mouse_pos"))); +static inline uint32_t GetMouseButtons(void) __attribute__ ((alias ("get_mouse_buttons"))); +static inline uint32_t GetMouseWheels(void) __attribute__ ((alias ("get_mouse_wheels"))); +static inline uint32_t LoadCursor(void *path, uint32_t flags) __attribute__ ((alias ("load_cursor"))); +static inline uint32_t SetCursor(uint32_t cursor) __attribute__ ((alias ("set_cursor"))); +static inline int DestroyCursor(uint32_t cursor) __attribute__ ((alias ("destroy_cursor"))); +static inline uint32_t GetOsEvent(void) __attribute__ ((alias ("get_os_event"))); +static inline void *UserAlloc(size_t size) __attribute__ ((alias ("user_alloc"))); +static inline int UserFree(void *mem) __attribute__ ((alias ("user_free"))); +static inline void* UserRealloc(void *mem, size_t size) __attribute__ ((alias ("user_realloc"))); +static inline int *UserUnmap(void *base, size_t offset, size_t size) __attribute__ ((alias ("user_unmap"))); +static inline ufile_t LoadFile(const char *path) __attribute__ ((alias ("load_file"))); +static inline void GetProcInfo(char *info) __attribute__ ((alias ("get_proc_info"))); +#else + #define BeginDraw begin_draw + #define EndDraw end_draw + #define DrawWindow sys_create_window + #define DefineButton define_button + #define DrawLine draw_line + #define DrawBar draw_bar + #define DrawBitmap draw_bitmap + #define GetSkinHeight get_skin_height + #define GetMousePos get_mouse_pos + #define GetMouseButtons get_mouse_buttons + #define GetMouseWheels get_mouse_wheels + #define LoadCursor load_cursor + #define SetCursor set_cursor + #define DestroyCursor destroy_cursor + #define GetOsEvent get_os_event + #define UserAlloc user_alloc + #define UserFree user_free + #define UserRealloc user_realloc + #define UserUnmap user_unmap + #define LoadFile load_file + #define GetProcInfo get_proc_info +#endif + +#ifdef __cplusplus +} +#endif + + +#endif + + + + + diff --git a/programs/develop/ktcc/trunk/libc/include/libgen.h b/programs/develop/ktcc/trunk/libc/include/libgen.h new file mode 100644 index 0000000000..af17a911f5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/libgen.h @@ -0,0 +1,7 @@ +#ifndef __KTCC_LIBGEN_H__ +#define __KTCC_LIBGEN_H__ + +char *basename(char *path); +char *dirname(char *path); + +#endif // __KTCC_LIBGEN_H__ diff --git a/programs/develop/ktcc/trunk/libc/include/math.h b/programs/develop/ktcc/trunk/libc/include/math.h new file mode 100644 index 0000000000..9a0f0ae92f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/math.h @@ -0,0 +1,55 @@ +/* Copyright (C) 1999 DJ Delorie, see http://www.delorie.com/copyright.html for details */ +/* Copyright (C) 1998 DJ Delorie, see http://www.delorie.com/copyright.html for details */ +/* Copyright (C) 1995 DJ Delorie, see http://www.delorie.com/copyright.html for details */ + +#ifndef _MATH_H +#define _MATH_H + +extern double acos(double _x); +extern double asin(double _x); +extern double atan(double _x); +extern double atan2(double _y, double _x); +extern float ceilf(float); +extern double ceil(double _x); +extern double cos(double _x); +extern double cosh(double _x); +extern double exp(double _x); +extern double exp2(double _x); +extern double fabs(double _x); +extern float fabsf(float); +extern double floor(double _x); +extern float floorf(float); +extern double fmod(double _x, double _y); +extern double frexp(double _x, int *_pexp); +extern double ldexp(double _x, int _exp); +extern double remainder(double, double); +extern double log(double _y); +extern double log10(double _x); +extern double modf(double _x, double *_pint); +extern double pow(double _x, double _y); +extern double sin(double _x); +extern double sinh(double _x); +extern double sqrt(double _x); +extern float sqrtf(float); +extern double tan(double _x); +extern double tanh(double _x); +extern double round(double x); +extern long double roundl (long double x); + +#define M_E 2.7182818284590452354 +#define M_LOG2E 1.4426950408889634074 +#define M_LOG10E 0.43429448190325182765 +#define M_LN2 0.69314718055994530942 +#define M_LN10 2.30258509299404568402 +#define M_PI 3.14159265358979323846 +#define M_PI_2 1.57079632679489661923 +#define M_PI_4 0.78539816339744830962 +#define M_1_PI 0.31830988618379067154 +#define M_2_PI 0.63661977236758134308 +#define M_2_SQRTPI 1.12837916709551257390 +#define M_SQRT2 1.41421356237309504880 +#define M_SQRT1_2 0.70710678118654752440 +#define PI M_PI +#define PI2 M_PI_2 + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/memory.h b/programs/develop/ktcc/trunk/libc/include/memory.h new file mode 100644 index 0000000000..b2b3d5ad1b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/memory.h @@ -0,0 +1,8 @@ +#ifndef _MEMORY_H +#define _MEMORY_H + +#ifndef _STRING_H + #include +#endif +#endif + diff --git a/programs/develop/ktcc/trunk/libc/include/net/network.h b/programs/develop/ktcc/trunk/libc/include/net/network.h new file mode 100644 index 0000000000..784c22edc8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/net/network.h @@ -0,0 +1,54 @@ +#ifndef __NETWORK_H +#define __NETWORK_H + +#include + +#define EAI_ADDRFAMILY 1 +#define EAI_AGAIN 2 +#define EAI_BADFLAGS 3 +#define EAI_FAIL 4 +#define EAI_FAMILY 5 +#define EAI_MEMORY 6 +#define EAI_NONAME 8 +#define EAI_SERVICE 9 +#define EAI_SOCKTYPE 10 +#define EAI_BADHINTS 12 +#define EAI_PROTOCOL 13 +#define EAI_OVERFLOW 14 + +// Flags for addrinfo +#define AI_PASSIVE 1 +#define AI_CANONNAME 2 +#define AI_NUMERICHOST 4 +#define AI_NUMERICSERV 8 +#define AI_ADDRCONFIG 0x400 + +#pragma pack(push, 1) +struct ARP_entry{ +unsigned int IP; +unsigned char MAC[6]; +unsigned short status; +unsigned short TTL; +}; +#pragma pack(pop) + +#pragma pack(push, 1) +struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + int ai_addrlen; + char *ai_canonname; + struct sockaddr *ai_addr; + struct addrinfo *ai_next; +}; +#pragma pack(pop) + +extern int networklib_init (); +extern int (*inet_addr __attribute__ ((stdcall)))(const char* hostname); +extern char* (*inet_ntoa __attribute__ ((stdcall)))(int ip_addr); +extern int (*getaddrinfo __attribute__ ((stdcall)))(const char* hostname, const char* servname, const struct addrinfo* hints, struct addrinfo** res); +extern void (*freeaddrinfo __attribute__ ((stdcall)))(struct addrinfo* ai); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/net/socket.h b/programs/develop/ktcc/trunk/libc/include/net/socket.h new file mode 100644 index 0000000000..8867fdaa9b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/net/socket.h @@ -0,0 +1,89 @@ +#ifndef __SOCKET_H +#define __SOCKET_H + +#include +#include + +// Socket Types +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 + +// IP protocols +#define IPPROTO_IP 0 +#define IPPROTO_ICMP 1 +#define IPPROTO_TCP 6 +#define IPPROTO_UDP 17 +#define IPPROTO_RAW 255 + +// IP options +#define IP_TTL 2 + +// Address families +#define AF_UNSPEC 0 +#define AF_LOCAL 1 +#define AF_INET 2 +#define AF_INET4 2 // IPv4 +#define AF_INET6 10 // IPv6 + +#define PF_UNSPEC AF_UNSPEC +#define PF_LOCAL AF_LOCAL +#define PF_INET4 AF_INET4 +#define PF_INET6 AF_INET6 + +// internal definition +#define AI_SUPPORTED 0x40F + +// for system function 76 +#define API_ETH (0<<16) +#define API_IPv4 (1<<16) +#define API_ICMP (2<<16) +#define API_UDP (3<<16) +#define API_TCP (4<<16) +#define API_ARP (5<<16) +#define API_PPPOE (6<<16) + +// Socket flags for user calls +#define MSG_NOFLAG 0 +#define MSG_PEEK 0x02 +#define MSG_DONTWAIT 0x40 + +// Socket levels +#define SOL_SOCKET 0xffff + +//Socket options +#define SO_BINDTODEVICE (1<<9) +#define SO_NONBLOCK (1<<31) +#define PORT(X) (X<<8) + +#pragma pack(push,1) +struct sockaddr{ + unsigned short sin_family; + unsigned short sin_port; + unsigned int sin_addr; + unsigned long long sin_zero; +}; +#pragma pack(pop) + +#pragma pack(push,1) +typedef struct{ + unsigned int level; + unsigned int optionname; + unsigned int optlenght; + unsigned char options; +}optstruct; +#pragma pack(pop) + +int socket(int domain, int type, int protocol); +int close(int socket); +int bind(int socket, const struct sockaddr *addres, int addres_len); +int listen(int socket, int backlog); +int connect(int socket, const struct sockaddr* address, int socket_len); +int accept(int socket, const struct sockaddr* address, int address_len); +int send(int socket, const void *message, size_t msg_len, int flag); +int recv(int socket, void *buffer, size_t buff_len, int flag); +int setsockopt(int socket,const optstruct* opt); +int getsockopt(int socket, optstruct* opt); +int socketpair(int *sock1, int *sock2); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/stdarg.h b/programs/develop/ktcc/trunk/libc/include/stdarg.h new file mode 100644 index 0000000000..06d592b93a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdarg.h @@ -0,0 +1,75 @@ +#ifndef _STDARG_H +#define _STDARG_H + +#ifdef __x86_64__ +#ifndef _WIN64 + +//This should be in sync with the declaration on our lib/libtcc1.c +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +typedef __va_list_struct va_list[1]; + +void __va_start(__va_list_struct *ap, void *fp); +void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); + +#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0)) +#define va_arg(ap, type) \ + (*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type)))) +#define va_copy(dest, src) (*(dest) = *(src)) +#define va_end(ap) + +#else /* _WIN64 */ +typedef char *va_list; +#define va_start(ap,last) __builtin_va_start(ap,last) +#define va_arg(ap,type) (ap += 8, sizeof(type)<=8 ? *(type*)ap : **(type**)ap) +#define va_copy(dest, src) ((dest) = (src)) +#define va_end(ap) +#endif + +#elif __arm__ +typedef char *va_list; +#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) +#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) + +#elif defined(__aarch64__) +typedef struct { + void *__stack; + void *__gr_top; + void *__vr_top; + int __gr_offs; + int __vr_offs; +} va_list; +#define va_start(ap, last) __va_start(ap, last) +#define va_arg(ap, type) __va_arg(ap, type) +#define va_end(ap) +#define va_copy(dest, src) ((dest) = (src)) + +#else /* __i386__ */ +typedef char *va_list; +/* only correct for i386 */ +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) +#endif + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif /* _STDARG_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/stdbool.h b/programs/develop/ktcc/trunk/libc/include/stdbool.h new file mode 100644 index 0000000000..d2ee446e70 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdbool.h @@ -0,0 +1,11 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +/* ISOC99 boolean */ + +#define bool _Bool +#define true 1 +#define false 0 +#define __bool_true_false_are_defined 1 + +#endif /* _STDBOOL_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/stddef.h b/programs/develop/ktcc/trunk/libc/include/stddef.h new file mode 100644 index 0000000000..791ba318cc --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stddef.h @@ -0,0 +1,46 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +typedef __SIZE_TYPE__ size_t; +typedef __PTRDIFF_TYPE__ ssize_t; +typedef __WCHAR_TYPE__ wchar_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; +typedef __PTRDIFF_TYPE__ intptr_t; +typedef __SIZE_TYPE__ uintptr_t; + +#ifndef __int8_t_defined +#define __int8_t_defined +typedef signed char int8_t; +typedef signed short int int16_t; +typedef signed int int32_t; +typedef signed long long int int64_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long int uint64_t; +#endif + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#define offsetof(type, field) ((size_t)&((type *)0)->field) + +void *alloca(size_t size); + +#endif + +/* Older glibc require a wint_t from (when requested + by __need_wint_t, as otherwise stddef.h isn't allowed to + define this type). Note that this must be outside the normal + _STDDEF_H guard, so that it works even when we've included the file + already (without requiring wint_t). Some other libs define _WINT_T + if they've already provided that type, so we can use that as guard. + TCC defines __WINT_TYPE__ for us. */ +#if defined (__need_wint_t) +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif +#undef __need_wint_t +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/stdint.h b/programs/develop/ktcc/trunk/libc/include/stdint.h new file mode 100644 index 0000000000..d8dcc0f82d --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdint.h @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/include/stdio.h b/programs/develop/ktcc/trunk/libc/include/stdio.h new file mode 100644 index 0000000000..5fd83e151d --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdio.h @@ -0,0 +1,105 @@ +#ifndef stdio_h +#define stdio_h + +#include "kolibrisys.h" +#include +#include +/* use stdarg.h +typedef char *va_list; +#define _roundsize(n) ( (sizeof(n) + 3) & ~3 ) +#define va_start(ap,v) (ap = (va_list)&v+_roundsize(v)) +#define va_arg(ap,t) ( *(t *)((ap += _roundsize(t)) - _roundsize(t)) ) +#define va_end(ap) (ap = (va_list)0) +*/ +#ifndef NULL +# define NULL ((void*)0) +#endif + +typedef unsigned int fpos_t; // 32bit is not enough! 4Gb limit +typedef unsigned int size_t; + +int format_print(char *dest, size_t maxlen,const char *fmt0, va_list argp); + +typedef struct { + char* buffer; + dword buffersize; + dword filesize; // too small + int filepos; // too small, may be -1 + char* filename; + int mode; + int ungetc_buf; + dword buffer_start; // 1st byte position + dword buffer_end; // points after last buffered data +} FILE; + +#define stderr ((FILE*)3) /* works only for fprintf!!! */ + +#define FILE_OPEN_READ 0 +#define FILE_OPEN_WRITE 1 +#define FILE_OPEN_APPEND 2 +#define FILE_OPEN_TEXT 4 +#define FILE_OPEN_PLUS 8 +#define EOF (-1) +#define BUFSIZ (4096) +#define FILENAME_MAX (0x400) + +extern FILE* fopen(const char* filename, const char *mode); +extern int fclose(FILE* file); +extern int feof(FILE* file); +extern int fflush(FILE* file); +extern int fgetc(FILE* file); +extern int fgetpos(FILE* file,fpos_t* pos); +extern int fsetpos(FILE* file,const fpos_t* pos); +extern int fputc(int c,FILE* file); +extern int fread(void* buffer,int size,int count,FILE* file); +extern int fwrite(void *buffer,int size,int count,FILE* file); +extern long ftell(FILE* file); +#define SEEK_CUR 0 +#define SEEK_END 1 +#define SEEK_SET 2 +extern int fseek(FILE* file,long offset,int origin); +extern void rewind(FILE* file); +extern int cdecl fprintf(FILE* file, const char* format,...); +extern int fscanf(FILE* file,const char* format,...); +extern int ungetc(int c,FILE* file); + +extern int cdecl printf(const char *format,...); + +extern int vsnprintf(char *dest, size_t size,const char *format,va_list ap); +extern int cdecl snprintf(char *dest, size_t size, const char *format,...); +extern int cdecl sprintf(char *dest,const char *format,...); + +#define getc(a) fgetc(a) +#define putc(a, b) fputc(a, b) +char * fgets (char * str, int num, FILE * stream); +int putchar (int ch); +int getchar (void); +int puts (const char * str); +char * gets (char * str); + +typedef int (*virtual_getc)(void *sp, const void *obj); +typedef void (*virtual_ungetc)(void *sp, int c, const void *obj); +int format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vgetc, virtual_ungetc vungetc); +int vscanf ( const char * format, va_list arg ); +int scanf ( const char * format, ...); +int vsscanf ( const char * s, const char * format, va_list arg ); +int sscanf ( const char * s, const char * format, ...); +int vfscanf ( FILE * stream, const char * format, va_list arg ); +int fputs ( const char * str, FILE * file ); +void clearerr ( FILE * stream ); +int ferror ( FILE * stream ); +void perror ( const char * str ); +int vprintf ( const char * format, va_list arg ); +int vsprintf (char * s, const char * format, va_list arg ); +int vfprintf ( FILE * stream, const char * format, va_list arg ); + +//debug +void debug_printf(const char *format,...); + +int tiny_sprintf (char * s, const char * format, ... ); +int tiny_snprintf (char * s, size_t n, const char * format, ... ); +int tiny_vsnprintf (char * s, size_t n, const char * format, va_list args ); +// support %c, %s, %d, %x, %u, %% for 32-bit values only. no width specs, left align +// always zero-ended +#endif + diff --git a/programs/develop/ktcc/trunk/libc/include/stdlib.h b/programs/develop/ktcc/trunk/libc/include/stdlib.h new file mode 100644 index 0000000000..5dc231baf2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdlib.h @@ -0,0 +1,77 @@ +#ifndef stdlib_h +#define stdlib_h +#include "kolibrisys.h" + +#define RAND_MAX 65535 +#ifndef NULL +# define NULL ((void*)0) +#endif + +#define abs(i) (((i)<0)?(-(i)):(i)) +#define labs(li) abs(li) + +#define min(a, b) ((a)<(b) ? (a) : (b)) +#define max(a, b) ((a)>(b) ? (a) : (b)) + + +extern int atoib(char *s,int b); +extern int atoi(char *s); +extern char *itoab(unsigned int n,char* s,int b); +extern char *__itoa(int n,char* s); + +// function using KOS syscalls +extern void* stdcall sysmalloc(dword size); +extern void stdcall sysfree(void *pointer); +extern void* stdcall sysrealloc(void* pointer,dword size); +extern void* syscalloc (size_t num, size_t size); + +// suballocator functions +extern void* wtmalloc(size_t size); +extern void wtfree(void *pointer); +extern void* wtrealloc(void* pointer, size_t size); +extern void* wtcalloc (size_t num, size_t size); +extern int wtmalloc_freelist_check(); +extern int wtmalloc_poiner_check(void *ptr); +extern void wtmalloc_freelist_print(); + +#ifdef USESYSALLOC +#define malloc(x) sysmalloc(x) +#define free(x) sysfree(x) +#define realloc(x,y) sysrealloc(x,y) +#define calloc(x,y) syscalloc(x,y) +#else +#define malloc(x) wtmalloc(x) +#define free(x) wtfree(x) +#define realloc(x,y) wtrealloc(x,y) +#define calloc(x,y) wtcalloc(x,y) +#endif + + +extern int rand (void); +extern void srand (unsigned int seed); + +double strtod (const char* str, char** endptr); +long double strtold (const char* str, char** endptr); +float strtof (const char* str, char** endptr); +long int strtol (const char* str, char** endptr, int base); +#define strtoul(s, ep, b) ((unsigned long int)strtol(s, ep, b)) + + +void exit (int status); /* close console if was initialized, also stay window [finished] when status is error < 0 */ +#define abort() exit(-1) + +typedef struct { + int quot; + int rem; +} div_t; + +typedef div_t ldiv_t; + +div_t div (int numer, int denom); +#define ldiv(a, b) div(a, b) +#define atol(a) atoi(a) +#define atof(a) strtod(a, NULL) + + + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/string.h b/programs/develop/ktcc/trunk/libc/include/string.h new file mode 100644 index 0000000000..bd2ad06211 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/string.h @@ -0,0 +1,36 @@ +#ifndef string_h +#define string_h +typedef unsigned int size_t; + + +extern void* memchr(const void*,int,size_t); +extern void* memrchr(const void*,int,size_t); +extern int memcmp(const void*,const void*,size_t); +extern void* memcpy(void*,const void*,size_t); +extern void* memmove(void*,const void*,size_t); +extern void* memset(void*,int,size_t); +extern char* strcat(char*,const char*); +extern char* strchr(const char*,int); +extern int strcmp(const char*,const char*); +extern int strcoll(const char*,const char*); +extern char* strcpy(char*,const char*); +extern size_t strcspn(const char*,const char*); +extern int strlen(const char*); +extern char* strncat(char*,const char*,size_t); +extern int strncmp(const char*,const char*,size_t); +extern char* strncpy(char*,const char*,size_t); +extern char* strpbrk(const char*,const char*); +extern char* strrchr(const char*,int); +extern size_t strspn(const char*,const char*); +extern char* strstr(const char*,const char*); +extern char* strtok(char*,const char*); +extern int strxfrm(char*,const char*,int); +extern char* strdup(const char*); +extern char* strrev(char *p); +char * strerror ( int errnum ); + +#ifndef NULL +# define NULL ((void*)0) +#endif + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/time.h b/programs/develop/ktcc/trunk/libc/include/time.h new file mode 100644 index 0000000000..8904899371 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/time.h @@ -0,0 +1,29 @@ +#ifndef _TIME_H +#define _TIME_H + + +typedef unsigned long int clock_t; +typedef unsigned long int time_t; +#define clock() get_tick_count() +#define CLOCKS_PER_SEC 100 + +struct tm { + int tm_sec; /* seconds after the minute 0-61*/ + int tm_min; /* minutes after the hour 0-59 */ + int tm_hour; /* hours since midnight 0-23 */ + int tm_mday; /* day of the month 1-31 */ + int tm_mon; /* months since January 0-11 */ + int tm_year; /* years since 1900 */ + int tm_wday; /* days since Sunday 0-6 */ + int tm_yday; /* days since January 1 0-365 */ + int tm_isdst; /* Daylight Saving Time flag */ +}; + +time_t mktime (struct tm * timeptr); +time_t time (time_t* timer); +struct tm * localtime (const time_t * timer); /* non-standard! ignore parameter and return just time now, not generate tm_isdst, tm_yday, tm_wday == -1 */ +double difftime (time_t end, time_t beginning); + +extern struct tm __buffertime; + +#endif \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/include/tinygl/gl.h b/programs/develop/ktcc/trunk/libc/include/tinygl/gl.h new file mode 100644 index 0000000000..9773624af8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/tinygl/gl.h @@ -0,0 +1,838 @@ +/* + * The following constants come from Mesa + */ +#ifndef GL_H +#define GL_H + +#define GL_VERSION_1_1 1 + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + /* Boolean values */ + GL_FALSE = 0, + GL_TRUE = 1, + + /* Data types */ + GL_BYTE = 0x1400, + GL_UNSIGNED_BYTE = 0x1401, + GL_SHORT = 0x1402, + GL_UNSIGNED_SHORT = 0x1403, + GL_INT = 0x1404, + GL_UNSIGNED_INT = 0x1405, + GL_FLOAT = 0x1406, + GL_DOUBLE = 0x140A, + GL_2_BYTES = 0x1407, + GL_3_BYTES = 0x1408, + GL_4_BYTES = 0x1409, + + /* Primitives */ + GL_LINES = 0x0001, + GL_POINTS = 0x0000, + GL_LINE_STRIP = 0x0003, + GL_LINE_LOOP = 0x0002, + GL_TRIANGLES = 0x0004, + GL_TRIANGLE_STRIP = 0x0005, + GL_TRIANGLE_FAN = 0x0006, + GL_QUADS = 0x0007, + GL_QUAD_STRIP = 0x0008, + GL_POLYGON = 0x0009, + GL_EDGE_FLAG = 0x0B43, + + /* Vertex Arrays */ + GL_VERTEX_ARRAY = 0x8074, + GL_NORMAL_ARRAY = 0x8075, + GL_COLOR_ARRAY = 0x8076, + GL_INDEX_ARRAY = 0x8077, + GL_TEXTURE_COORD_ARRAY = 0x8078, + GL_EDGE_FLAG_ARRAY = 0x8079, + GL_VERTEX_ARRAY_SIZE = 0x807A, + GL_VERTEX_ARRAY_TYPE = 0x807B, + GL_VERTEX_ARRAY_STRIDE = 0x807C, + GL_VERTEX_ARRAY_COUNT = 0x807D, + GL_NORMAL_ARRAY_TYPE = 0x807E, + GL_NORMAL_ARRAY_STRIDE = 0x807F, + GL_NORMAL_ARRAY_COUNT = 0x8080, + GL_COLOR_ARRAY_SIZE = 0x8081, + GL_COLOR_ARRAY_TYPE = 0x8082, + GL_COLOR_ARRAY_STRIDE = 0x8083, + GL_COLOR_ARRAY_COUNT = 0x8084, + GL_INDEX_ARRAY_TYPE = 0x8085, + GL_INDEX_ARRAY_STRIDE = 0x8086, + GL_INDEX_ARRAY_COUNT = 0x8087, + GL_TEXTURE_COORD_ARRAY_SIZE = 0x8088, + GL_TEXTURE_COORD_ARRAY_TYPE = 0x8089, + GL_TEXTURE_COORD_ARRAY_STRIDE = 0x808A, + GL_TEXTURE_COORD_ARRAY_COUNT = 0x808B, + GL_EDGE_FLAG_ARRAY_STRIDE = 0x808C, + GL_EDGE_FLAG_ARRAY_COUNT = 0x808D, + GL_VERTEX_ARRAY_POINTER = 0x808E, + GL_NORMAL_ARRAY_POINTER = 0x808F, + GL_COLOR_ARRAY_POINTER = 0x8090, + GL_INDEX_ARRAY_POINTER = 0x8091, + GL_TEXTURE_COORD_ARRAY_POINTER = 0x8092, + GL_EDGE_FLAG_ARRAY_POINTER = 0x8093, + GL_V2F = 0x2A20, + GL_V3F = 0x2A21, + GL_C4UB_V2F = 0x2A22, + GL_C4UB_V3F = 0x2A23, + GL_C3F_V3F = 0x2A24, + GL_N3F_V3F = 0x2A25, + GL_C4F_N3F_V3F = 0x2A26, + GL_T2F_V3F = 0x2A27, + GL_T4F_V4F = 0x2A28, + GL_T2F_C4UB_V3F = 0x2A29, + GL_T2F_C3F_V3F = 0x2A2A, + GL_T2F_N3F_V3F = 0x2A2B, + GL_T2F_C4F_N3F_V3F = 0x2A2C, + GL_T4F_C4F_N3F_V4F = 0x2A2D, + + /* Matrix Mode */ + GL_MATRIX_MODE = 0x0BA0, + GL_MODELVIEW = 0x1700, + GL_PROJECTION = 0x1701, + GL_TEXTURE = 0x1702, + + /* Points */ + GL_POINT_SMOOTH = 0x0B10, + GL_POINT_SIZE = 0x0B11, + GL_POINT_SIZE_GRANULARITY = 0x0B13, + GL_POINT_SIZE_RANGE = 0x0B12, + + /* Lines */ + GL_LINE_SMOOTH = 0x0B20, + GL_LINE_STIPPLE = 0x0B24, + GL_LINE_STIPPLE_PATTERN = 0x0B25, + GL_LINE_STIPPLE_REPEAT = 0x0B26, + GL_LINE_WIDTH = 0x0B21, + GL_LINE_WIDTH_GRANULARITY = 0x0B23, + GL_LINE_WIDTH_RANGE = 0x0B22, + + /* Polygons */ + GL_POINT = 0x1B00, + GL_LINE = 0x1B01, + GL_FILL = 0x1B02, + GL_CCW = 0x0901, + GL_CW = 0x0900, + GL_FRONT = 0x0404, + GL_BACK = 0x0405, + GL_CULL_FACE = 0x0B44, + GL_CULL_FACE_MODE = 0x0B45, + GL_POLYGON_SMOOTH = 0x0B41, + GL_POLYGON_STIPPLE = 0x0B42, + GL_FRONT_FACE = 0x0B46, + GL_POLYGON_MODE = 0x0B40, + GL_POLYGON_OFFSET_FACTOR = 0x3038, + GL_POLYGON_OFFSET_UNITS = 0x2A00, + GL_POLYGON_OFFSET_POINT = 0x2A01, + GL_POLYGON_OFFSET_LINE = 0x2A02, + GL_POLYGON_OFFSET_FILL = 0x8037, + + /* Display Lists */ + GL_COMPILE = 0x1300, + GL_COMPILE_AND_EXECUTE = 0x1301, + GL_LIST_BASE = 0x0B32, + GL_LIST_INDEX = 0x0B33, + GL_LIST_MODE = 0x0B30, + + /* Depth buffer */ + GL_NEVER = 0x0200, + GL_LESS = 0x0201, + GL_GEQUAL = 0x0206, + GL_LEQUAL = 0x0203, + GL_GREATER = 0x0204, + GL_NOTEQUAL = 0x0205, + GL_EQUAL = 0x0202, + GL_ALWAYS = 0x0207, + GL_DEPTH_TEST = 0x0B71, + GL_DEPTH_BITS = 0x0D56, + GL_DEPTH_CLEAR_VALUE = 0x0B73, + GL_DEPTH_FUNC = 0x0B74, + GL_DEPTH_RANGE = 0x0B70, + GL_DEPTH_WRITEMASK = 0x0B72, + GL_DEPTH_COMPONENT = 0x1902, + + /* Lighting */ + GL_LIGHTING = 0x0B50, + GL_LIGHT0 = 0x4000, + GL_LIGHT1 = 0x4001, + GL_LIGHT2 = 0x4002, + GL_LIGHT3 = 0x4003, + GL_LIGHT4 = 0x4004, + GL_LIGHT5 = 0x4005, + GL_LIGHT6 = 0x4006, + GL_LIGHT7 = 0x4007, + GL_SPOT_EXPONENT = 0x1205, + GL_SPOT_CUTOFF = 0x1206, + GL_CONSTANT_ATTENUATION = 0x1207, + GL_LINEAR_ATTENUATION = 0x1208, + GL_QUADRATIC_ATTENUATION = 0x1209, + GL_AMBIENT = 0x1200, + GL_DIFFUSE = 0x1201, + GL_SPECULAR = 0x1202, + GL_SHININESS = 0x1601, + GL_EMISSION = 0x1600, + GL_POSITION = 0x1203, + GL_SPOT_DIRECTION = 0x1204, + GL_AMBIENT_AND_DIFFUSE = 0x1602, + GL_COLOR_INDEXES = 0x1603, + GL_LIGHT_MODEL_TWO_SIDE = 0x0B52, + GL_LIGHT_MODEL_LOCAL_VIEWER = 0x0B51, + GL_LIGHT_MODEL_AMBIENT = 0x0B53, + GL_FRONT_AND_BACK = 0x0408, + GL_SHADE_MODEL = 0x0B54, + GL_FLAT = 0x1D00, + GL_SMOOTH = 0x1D01, + GL_COLOR_MATERIAL = 0x0B57, + GL_COLOR_MATERIAL_FACE = 0x0B55, + GL_COLOR_MATERIAL_PARAMETER = 0x0B56, + GL_NORMALIZE = 0x0BA1, + + /* User clipping planes */ + GL_CLIP_PLANE0 = 0x3000, + GL_CLIP_PLANE1 = 0x3001, + GL_CLIP_PLANE2 = 0x3002, + GL_CLIP_PLANE3 = 0x3003, + GL_CLIP_PLANE4 = 0x3004, + GL_CLIP_PLANE5 = 0x3005, + + /* Accumulation buffer */ + GL_ACCUM_RED_BITS = 0x0D58, + GL_ACCUM_GREEN_BITS = 0x0D59, + GL_ACCUM_BLUE_BITS = 0x0D5A, + GL_ACCUM_ALPHA_BITS = 0x0D5B, + GL_ACCUM_CLEAR_VALUE = 0x0B80, + GL_ACCUM = 0x0100, + GL_ADD = 0x0104, + GL_LOAD = 0x0101, + GL_MULT = 0x0103, + GL_RETURN = 0x0102, + + /* Alpha testing */ + GL_ALPHA_TEST = 0x0BC0, + GL_ALPHA_TEST_REF = 0x0BC2, + GL_ALPHA_TEST_FUNC = 0x0BC1, + + /* Blending */ + GL_BLEND = 0x0BE2, + GL_BLEND_SRC = 0x0BE1, + GL_BLEND_DST = 0x0BE0, + GL_ZERO = 0, + GL_ONE = 1, + GL_SRC_COLOR = 0x0300, + GL_ONE_MINUS_SRC_COLOR = 0x0301, + GL_DST_COLOR = 0x0306, + GL_ONE_MINUS_DST_COLOR = 0x0307, + GL_SRC_ALPHA = 0x0302, + GL_ONE_MINUS_SRC_ALPHA = 0x0303, + GL_DST_ALPHA = 0x0304, + GL_ONE_MINUS_DST_ALPHA = 0x0305, + GL_SRC_ALPHA_SATURATE = 0x0308, + GL_CONSTANT_COLOR = 0x8001, + GL_ONE_MINUS_CONSTANT_COLOR = 0x8002, + GL_CONSTANT_ALPHA = 0x8003, + GL_ONE_MINUS_CONSTANT_ALPHA = 0x8004, + + /* Render Mode */ + GL_FEEDBACK = 0x1C01, + GL_RENDER = 0x1C00, + GL_SELECT = 0x1C02, + + /* Feedback */ + GL_2D = 0x0600, + GL_3D = 0x0601, + GL_3D_COLOR = 0x0602, + GL_3D_COLOR_TEXTURE = 0x0603, + GL_4D_COLOR_TEXTURE = 0x0604, + GL_POINT_TOKEN = 0x0701, + GL_LINE_TOKEN = 0x0702, + GL_LINE_RESET_TOKEN = 0x0707, + GL_POLYGON_TOKEN = 0x0703, + GL_BITMAP_TOKEN = 0x0704, + GL_DRAW_PIXEL_TOKEN = 0x0705, + GL_COPY_PIXEL_TOKEN = 0x0706, + GL_PASS_THROUGH_TOKEN = 0x0700, + + /* Fog */ + GL_FOG = 0x0B60, + GL_FOG_MODE = 0x0B65, + GL_FOG_DENSITY = 0x0B62, + GL_FOG_COLOR = 0x0B66, + GL_FOG_INDEX = 0x0B61, + GL_FOG_START = 0x0B63, + GL_FOG_END = 0x0B64, + GL_LINEAR = 0x2601, + GL_EXP = 0x0800, + GL_EXP2 = 0x0801, + + /* Logic Ops */ + GL_LOGIC_OP = 0x0BF1, + GL_LOGIC_OP_MODE = 0x0BF0, + GL_CLEAR = 0x1500, + GL_SET = 0x150F, + GL_COPY = 0x1503, + GL_COPY_INVERTED = 0x150C, + GL_NOOP = 0x1505, + GL_INVERT = 0x150A, + GL_AND = 0x1501, + GL_NAND = 0x150E, + GL_OR = 0x1507, + GL_NOR = 0x1508, + GL_XOR = 0x1506, + GL_EQUIV = 0x1509, + GL_AND_REVERSE = 0x1502, + GL_AND_INVERTED = 0x1504, + GL_OR_REVERSE = 0x150B, + GL_OR_INVERTED = 0x150D, + + /* Stencil */ + GL_STENCIL_TEST = 0x0B90, + GL_STENCIL_WRITEMASK = 0x0B98, + GL_STENCIL_BITS = 0x0D57, + GL_STENCIL_FUNC = 0x0B92, + GL_STENCIL_VALUE_MASK = 0x0B93, + GL_STENCIL_REF = 0x0B97, + GL_STENCIL_FAIL = 0x0B94, + GL_STENCIL_PASS_DEPTH_PASS = 0x0B96, + GL_STENCIL_PASS_DEPTH_FAIL = 0x0B95, + GL_STENCIL_CLEAR_VALUE = 0x0B91, + GL_STENCIL_INDEX = 0x1901, + GL_KEEP = 0x1E00, + GL_REPLACE = 0x1E01, + GL_INCR = 0x1E02, + GL_DECR = 0x1E03, + + /* Buffers, Pixel Drawing/Reading */ + GL_NONE = 0, + GL_LEFT = 0x0406, + GL_RIGHT = 0x0407, + /*GL_FRONT = 0x0404, */ + /*GL_BACK = 0x0405, */ + /*GL_FRONT_AND_BACK = 0x0408, */ + GL_FRONT_LEFT = 0x0400, + GL_FRONT_RIGHT = 0x0401, + GL_BACK_LEFT = 0x0402, + GL_BACK_RIGHT = 0x0403, + GL_AUX0 = 0x0409, + GL_AUX1 = 0x040A, + GL_AUX2 = 0x040B, + GL_AUX3 = 0x040C, + GL_COLOR_INDEX = 0x1900, + GL_RED = 0x1903, + GL_GREEN = 0x1904, + GL_BLUE = 0x1905, + GL_ALPHA = 0x1906, + GL_LUMINANCE = 0x1909, + GL_LUMINANCE_ALPHA = 0x190A, + GL_ALPHA_BITS = 0x0D55, + GL_RED_BITS = 0x0D52, + GL_GREEN_BITS = 0x0D53, + GL_BLUE_BITS = 0x0D54, + GL_INDEX_BITS = 0x0D51, + GL_SUBPIXEL_BITS = 0x0D50, + GL_AUX_BUFFERS = 0x0C00, + GL_READ_BUFFER = 0x0C02, + GL_DRAW_BUFFER = 0x0C01, + GL_DOUBLEBUFFER = 0x0C32, + GL_STEREO = 0x0C33, + GL_BITMAP = 0x1A00, + GL_COLOR = 0x1800, + GL_DEPTH = 0x1801, + GL_STENCIL = 0x1802, + GL_DITHER = 0x0BD0, + GL_RGB = 0x1907, + GL_RGBA = 0x1908, + + /* Implementation limits */ + GL_MAX_LIST_NESTING = 0x0B31, + GL_MAX_ATTRIB_STACK_DEPTH = 0x0D35, + GL_MAX_MODELVIEW_STACK_DEPTH = 0x0D36, + GL_MAX_NAME_STACK_DEPTH = 0x0D37, + GL_MAX_PROJECTION_STACK_DEPTH = 0x0D38, + GL_MAX_TEXTURE_STACK_DEPTH = 0x0D39, + GL_MAX_EVAL_ORDER = 0x0D30, + GL_MAX_LIGHTS = 0x0D31, + GL_MAX_CLIP_PLANES = 0x0D32, + GL_MAX_TEXTURE_SIZE = 0x0D33, + GL_MAX_PIXEL_MAP_TABLE = 0x0D34, + GL_MAX_VIEWPORT_DIMS = 0x0D3A, + GL_MAX_CLIENT_ATTRIB_STACK_DEPTH= 0x0D3B, + + /* Gets */ + GL_ATTRIB_STACK_DEPTH = 0x0BB0, + GL_COLOR_CLEAR_VALUE = 0x0C22, + GL_COLOR_WRITEMASK = 0x0C23, + GL_CURRENT_INDEX = 0x0B01, + GL_CURRENT_COLOR = 0x0B00, + GL_CURRENT_NORMAL = 0x0B02, + GL_CURRENT_RASTER_COLOR = 0x0B04, + GL_CURRENT_RASTER_DISTANCE = 0x0B09, + GL_CURRENT_RASTER_INDEX = 0x0B05, + GL_CURRENT_RASTER_POSITION = 0x0B07, + GL_CURRENT_RASTER_TEXTURE_COORDS = 0x0B06, + GL_CURRENT_RASTER_POSITION_VALID = 0x0B08, + GL_CURRENT_TEXTURE_COORDS = 0x0B03, + GL_INDEX_CLEAR_VALUE = 0x0C20, + GL_INDEX_MODE = 0x0C30, + GL_INDEX_WRITEMASK = 0x0C21, + GL_MODELVIEW_MATRIX = 0x0BA6, + GL_MODELVIEW_STACK_DEPTH = 0x0BA3, + GL_NAME_STACK_DEPTH = 0x0D70, + GL_PROJECTION_MATRIX = 0x0BA7, + GL_PROJECTION_STACK_DEPTH = 0x0BA4, + GL_RENDER_MODE = 0x0C40, + GL_RGBA_MODE = 0x0C31, + GL_TEXTURE_MATRIX = 0x0BA8, + GL_TEXTURE_STACK_DEPTH = 0x0BA5, + GL_VIEWPORT = 0x0BA2, + + + /* Evaluators */ + GL_AUTO_NORMAL = 0x0D80, + GL_MAP1_COLOR_4 = 0x0D90, + GL_MAP1_GRID_DOMAIN = 0x0DD0, + GL_MAP1_GRID_SEGMENTS = 0x0DD1, + GL_MAP1_INDEX = 0x0D91, + GL_MAP1_NORMAL = 0x0D92, + GL_MAP1_TEXTURE_COORD_1 = 0x0D93, + GL_MAP1_TEXTURE_COORD_2 = 0x0D94, + GL_MAP1_TEXTURE_COORD_3 = 0x0D95, + GL_MAP1_TEXTURE_COORD_4 = 0x0D96, + GL_MAP1_VERTEX_3 = 0x0D97, + GL_MAP1_VERTEX_4 = 0x0D98, + GL_MAP2_COLOR_4 = 0x0DB0, + GL_MAP2_GRID_DOMAIN = 0x0DD2, + GL_MAP2_GRID_SEGMENTS = 0x0DD3, + GL_MAP2_INDEX = 0x0DB1, + GL_MAP2_NORMAL = 0x0DB2, + GL_MAP2_TEXTURE_COORD_1 = 0x0DB3, + GL_MAP2_TEXTURE_COORD_2 = 0x0DB4, + GL_MAP2_TEXTURE_COORD_3 = 0x0DB5, + GL_MAP2_TEXTURE_COORD_4 = 0x0DB6, + GL_MAP2_VERTEX_3 = 0x0DB7, + GL_MAP2_VERTEX_4 = 0x0DB8, + GL_COEFF = 0x0A00, + GL_DOMAIN = 0x0A02, + GL_ORDER = 0x0A01, + + /* Hints */ + GL_FOG_HINT = 0x0C54, + GL_LINE_SMOOTH_HINT = 0x0C52, + GL_PERSPECTIVE_CORRECTION_HINT = 0x0C50, + GL_POINT_SMOOTH_HINT = 0x0C51, + GL_POLYGON_SMOOTH_HINT = 0x0C53, + GL_DONT_CARE = 0x1100, + GL_FASTEST = 0x1101, + GL_NICEST = 0x1102, + + /* Scissor box */ + GL_SCISSOR_TEST = 0x0C11, + GL_SCISSOR_BOX = 0x0C10, + + /* Pixel Mode / Transfer */ + GL_MAP_COLOR = 0x0D10, + GL_MAP_STENCIL = 0x0D11, + GL_INDEX_SHIFT = 0x0D12, + GL_INDEX_OFFSET = 0x0D13, + GL_RED_SCALE = 0x0D14, + GL_RED_BIAS = 0x0D15, + GL_GREEN_SCALE = 0x0D18, + GL_GREEN_BIAS = 0x0D19, + GL_BLUE_SCALE = 0x0D1A, + GL_BLUE_BIAS = 0x0D1B, + GL_ALPHA_SCALE = 0x0D1C, + GL_ALPHA_BIAS = 0x0D1D, + GL_DEPTH_SCALE = 0x0D1E, + GL_DEPTH_BIAS = 0x0D1F, + GL_PIXEL_MAP_S_TO_S_SIZE = 0x0CB1, + GL_PIXEL_MAP_I_TO_I_SIZE = 0x0CB0, + GL_PIXEL_MAP_I_TO_R_SIZE = 0x0CB2, + GL_PIXEL_MAP_I_TO_G_SIZE = 0x0CB3, + GL_PIXEL_MAP_I_TO_B_SIZE = 0x0CB4, + GL_PIXEL_MAP_I_TO_A_SIZE = 0x0CB5, + GL_PIXEL_MAP_R_TO_R_SIZE = 0x0CB6, + GL_PIXEL_MAP_G_TO_G_SIZE = 0x0CB7, + GL_PIXEL_MAP_B_TO_B_SIZE = 0x0CB8, + GL_PIXEL_MAP_A_TO_A_SIZE = 0x0CB9, + GL_PIXEL_MAP_S_TO_S = 0x0C71, + GL_PIXEL_MAP_I_TO_I = 0x0C70, + GL_PIXEL_MAP_I_TO_R = 0x0C72, + GL_PIXEL_MAP_I_TO_G = 0x0C73, + GL_PIXEL_MAP_I_TO_B = 0x0C74, + GL_PIXEL_MAP_I_TO_A = 0x0C75, + GL_PIXEL_MAP_R_TO_R = 0x0C76, + GL_PIXEL_MAP_G_TO_G = 0x0C77, + GL_PIXEL_MAP_B_TO_B = 0x0C78, + GL_PIXEL_MAP_A_TO_A = 0x0C79, + GL_PACK_ALIGNMENT = 0x0D05, + GL_PACK_LSB_FIRST = 0x0D01, + GL_PACK_ROW_LENGTH = 0x0D02, + GL_PACK_SKIP_PIXELS = 0x0D04, + GL_PACK_SKIP_ROWS = 0x0D03, + GL_PACK_SWAP_BYTES = 0x0D00, + GL_UNPACK_ALIGNMENT = 0x0CF5, + GL_UNPACK_LSB_FIRST = 0x0CF1, + GL_UNPACK_ROW_LENGTH = 0x0CF2, + GL_UNPACK_SKIP_PIXELS = 0x0CF4, + GL_UNPACK_SKIP_ROWS = 0x0CF3, + GL_UNPACK_SWAP_BYTES = 0x0CF0, + GL_ZOOM_X = 0x0D16, + GL_ZOOM_Y = 0x0D17, + + /* Texture mapping */ + GL_TEXTURE_ENV = 0x2300, + GL_TEXTURE_ENV_MODE = 0x2200, + GL_TEXTURE_1D = 0x0DE0, + GL_TEXTURE_2D = 0x0DE1, + GL_TEXTURE_WRAP_S = 0x2802, + GL_TEXTURE_WRAP_T = 0x2803, + GL_TEXTURE_MAG_FILTER = 0x2800, + GL_TEXTURE_MIN_FILTER = 0x2801, + GL_TEXTURE_ENV_COLOR = 0x2201, + GL_TEXTURE_GEN_S = 0x0C60, + GL_TEXTURE_GEN_T = 0x0C61, + GL_TEXTURE_GEN_MODE = 0x2500, + GL_TEXTURE_BORDER_COLOR = 0x1004, + GL_TEXTURE_WIDTH = 0x1000, + GL_TEXTURE_HEIGHT = 0x1001, + GL_TEXTURE_BORDER = 0x1005, + GL_TEXTURE_COMPONENTS = 0x1003, + GL_NEAREST_MIPMAP_NEAREST = 0x2700, + GL_NEAREST_MIPMAP_LINEAR = 0x2702, + GL_LINEAR_MIPMAP_NEAREST = 0x2701, + GL_LINEAR_MIPMAP_LINEAR = 0x2703, + GL_OBJECT_LINEAR = 0x2401, + GL_OBJECT_PLANE = 0x2501, + GL_EYE_LINEAR = 0x2400, + GL_EYE_PLANE = 0x2502, + GL_SPHERE_MAP = 0x2402, + GL_DECAL = 0x2101, + GL_MODULATE = 0x2100, + GL_NEAREST = 0x2600, + GL_REPEAT = 0x2901, + GL_CLAMP = 0x2900, + GL_S = 0x2000, + GL_T = 0x2001, + GL_R = 0x2002, + GL_Q = 0x2003, + GL_TEXTURE_GEN_R = 0x0C62, + GL_TEXTURE_GEN_Q = 0x0C63, + + GL_PROXY_TEXTURE_1D = 0x8063, + GL_PROXY_TEXTURE_2D = 0x8064, + GL_TEXTURE_PRIORITY = 0x8066, + GL_TEXTURE_RESIDENT = 0x8067, + GL_TEXTURE_1D_BINDING = 0x8068, + GL_TEXTURE_2D_BINDING = 0x8069, + + /* Internal texture formats */ + GL_ALPHA4 = 0x803B, + GL_ALPHA8 = 0x803C, + GL_ALPHA12 = 0x803D, + GL_ALPHA16 = 0x803E, + GL_LUMINANCE4 = 0x803F, + GL_LUMINANCE8 = 0x8040, + GL_LUMINANCE12 = 0x8041, + GL_LUMINANCE16 = 0x8042, + GL_LUMINANCE4_ALPHA4 = 0x8043, + GL_LUMINANCE6_ALPHA2 = 0x8044, + GL_LUMINANCE8_ALPHA8 = 0x8045, + GL_LUMINANCE12_ALPHA4 = 0x8046, + GL_LUMINANCE12_ALPHA12 = 0x8047, + GL_LUMINANCE16_ALPHA16 = 0x8048, + GL_INTENSITY = 0x8049, + GL_INTENSITY4 = 0x804A, + GL_INTENSITY8 = 0x804B, + GL_INTENSITY12 = 0x804C, + GL_INTENSITY16 = 0x804D, + GL_R3_G3_B2 = 0x2A10, + GL_RGB4 = 0x804F, + GL_RGB5 = 0x8050, + GL_RGB8 = 0x8051, + GL_RGB10 = 0x8052, + GL_RGB12 = 0x8053, + GL_RGB16 = 0x8054, + GL_RGBA2 = 0x8055, + GL_RGBA4 = 0x8056, + GL_RGB5_A1 = 0x8057, + GL_RGBA8 = 0x8058, + GL_RGB10_A2 = 0x8059, + GL_RGBA12 = 0x805A, + GL_RGBA16 = 0x805B, + + /* Utility */ + GL_VENDOR = 0x1F00, + GL_RENDERER = 0x1F01, + GL_VERSION = 0x1F02, + GL_EXTENSIONS = 0x1F03, + + /* Errors */ + GL_INVALID_VALUE = 0x0501, + GL_INVALID_ENUM = 0x0500, + GL_INVALID_OPERATION = 0x0502, + GL_STACK_OVERFLOW = 0x0503, + GL_STACK_UNDERFLOW = 0x0504, + GL_OUT_OF_MEMORY = 0x0505, + + /* + * 1.0 Extensions + */ + /* GL_EXT_blend_minmax and GL_EXT_blend_color */ + GL_CONSTANT_COLOR_EXT = 0x8001, + GL_ONE_MINUS_CONSTANT_COLOR_EXT = 0x8002, + GL_CONSTANT_ALPHA_EXT = 0x8003, + GL_ONE_MINUS_CONSTANT_ALPHA_EXT = 0x8004, + GL_BLEND_EQUATION_EXT = 0x8009, + GL_MIN_EXT = 0x8007, + GL_MAX_EXT = 0x8008, + GL_FUNC_ADD_EXT = 0x8006, + GL_FUNC_SUBTRACT_EXT = 0x800A, + GL_FUNC_REVERSE_SUBTRACT_EXT = 0x800B, + GL_BLEND_COLOR_EXT = 0x8005, + + /* GL_EXT_polygon_offset */ + GL_POLYGON_OFFSET_EXT = 0x8037, + GL_POLYGON_OFFSET_FACTOR_EXT = 0x8038, + GL_POLYGON_OFFSET_BIAS_EXT = 0x8039, + + /* GL_EXT_vertex_array */ + GL_VERTEX_ARRAY_EXT = 0x8074, + GL_NORMAL_ARRAY_EXT = 0x8075, + GL_COLOR_ARRAY_EXT = 0x8076, + GL_INDEX_ARRAY_EXT = 0x8077, + GL_TEXTURE_COORD_ARRAY_EXT = 0x8078, + GL_EDGE_FLAG_ARRAY_EXT = 0x8079, + GL_VERTEX_ARRAY_SIZE_EXT = 0x807A, + GL_VERTEX_ARRAY_TYPE_EXT = 0x807B, + GL_VERTEX_ARRAY_STRIDE_EXT = 0x807C, + GL_VERTEX_ARRAY_COUNT_EXT = 0x807D, + GL_NORMAL_ARRAY_TYPE_EXT = 0x807E, + GL_NORMAL_ARRAY_STRIDE_EXT = 0x807F, + GL_NORMAL_ARRAY_COUNT_EXT = 0x8080, + GL_COLOR_ARRAY_SIZE_EXT = 0x8081, + GL_COLOR_ARRAY_TYPE_EXT = 0x8082, + GL_COLOR_ARRAY_STRIDE_EXT = 0x8083, + GL_COLOR_ARRAY_COUNT_EXT = 0x8084, + GL_INDEX_ARRAY_TYPE_EXT = 0x8085, + GL_INDEX_ARRAY_STRIDE_EXT = 0x8086, + GL_INDEX_ARRAY_COUNT_EXT = 0x8087, + GL_TEXTURE_COORD_ARRAY_SIZE_EXT = 0x8088, + GL_TEXTURE_COORD_ARRAY_TYPE_EXT = 0x8089, + GL_TEXTURE_COORD_ARRAY_STRIDE_EXT= 0x808A, + GL_TEXTURE_COORD_ARRAY_COUNT_EXT= 0x808B, + GL_EDGE_FLAG_ARRAY_STRIDE_EXT = 0x808C, + GL_EDGE_FLAG_ARRAY_COUNT_EXT = 0x808D, + GL_VERTEX_ARRAY_POINTER_EXT = 0x808E, + GL_NORMAL_ARRAY_POINTER_EXT = 0x808F, + GL_COLOR_ARRAY_POINTER_EXT = 0x8090, + GL_INDEX_ARRAY_POINTER_EXT = 0x8091, + GL_TEXTURE_COORD_ARRAY_POINTER_EXT= 0x8092, + GL_EDGE_FLAG_ARRAY_POINTER_EXT = 0x8093 + +}; + +enum { + GL_CURRENT_BIT = 0x00000001, + GL_POINT_BIT = 0x00000002, + GL_LINE_BIT = 0x00000004, + GL_POLYGON_BIT = 0x00000008, + GL_POLYGON_STIPPLE_BIT = 0x00000010, + GL_PIXEL_MODE_BIT = 0x00000020, + GL_LIGHTING_BIT = 0x00000040, + GL_FOG_BIT = 0x00000080, + GL_DEPTH_BUFFER_BIT = 0x00000100, + GL_ACCUM_BUFFER_BIT = 0x00000200, + GL_STENCIL_BUFFER_BIT = 0x00000400, + GL_VIEWPORT_BIT = 0x00000800, + GL_TRANSFORM_BIT = 0x00001000, + GL_ENABLE_BIT = 0x00002000, + GL_COLOR_BUFFER_BIT = 0x00004000, + GL_HINT_BIT = 0x00008000, + GL_EVAL_BIT = 0x00010000, + GL_LIST_BIT = 0x00020000, + GL_TEXTURE_BIT = 0x00040000, + GL_SCISSOR_BIT = 0x00080000, + GL_ALL_ATTRIB_BITS = 0x000fffff +}; + +/* some types */ + +typedef int GLenum; +typedef void GLvoid; +typedef unsigned char GLboolean; +typedef signed char GLbyte; /* 1-byte signed */ +typedef short GLshort; /* 2-byte signed */ +typedef int GLint; /* 4-byte signed */ +typedef unsigned char GLubyte; /* 1-byte unsigned */ +typedef unsigned short GLushort; /* 2-byte unsigned */ +typedef unsigned int GLuint; /* 4-byte unsigned */ +typedef float GLfloat; /* single precision float */ +typedef double GLdouble; /* double precision float */ +typedef int GLsizei; + +/* functions */ + +void glEnable(int code); +void glDisable(int code); + +void glShadeModel(int mode); +void glCullFace(int mode); +void glPolygonMode(int face,int mode); + +void glBegin(int type); +void glEnd(void); + +#define PROTO_GL1(name) \ +void gl ## name ## 1f(float); \ +void gl ## name ## 1d(double); \ +void gl ## name ## 1fv(float *); \ +void gl ## name ## 1dv(double *); + +#define PROTO_GL2(name) \ +void gl ## name ## 2f(float ,float); \ +void gl ## name ## 2d(double ,double); \ +void gl ## name ## 2fv(float *); \ +void gl ## name ## 2dv(double *); + +#define PROTO_GL3(name) \ +void gl ## name ## 3f(float ,float ,float); \ +void gl ## name ## 3d(double ,double ,double); \ +void gl ## name ## 3fv(float *); \ +void gl ## name ## 3dv(double *); + +#define PROTO_GL4(name) \ +void gl ## name ## 4f(float ,float ,float, float ); \ +void gl ## name ## 4d(double ,double ,double, double ); \ +void gl ## name ## 4fv(float *); \ +void gl ## name ## 4dv(double *); + +PROTO_GL2(Vertex) +PROTO_GL3(Vertex) +PROTO_GL4(Vertex) + +PROTO_GL3(Color) +PROTO_GL4(Color) + +PROTO_GL3(Normal) + +PROTO_GL1(TexCoord) +PROTO_GL2(TexCoord) +PROTO_GL3(TexCoord) +PROTO_GL4(TexCoord) + +void glEdgeFlag(int flag); + +/* matrix */ +void glMatrixMode(int mode); +void glLoadMatrixf(const float *m); +void glLoadIdentity(void); +void glMultMatrixf(const float *m); +void glPushMatrix(void); +void glPopMatrix(void); +void glRotatef(float angle,float x,float y,float z); +void glTranslatef(float x,float y,float z); +void glScalef(float x,float y,float z); + +void glViewport(int x,int y,int width,int height); +void glFrustum(double left,double right,double bottom,double top, + double near_,double far_); + +/* lists */ +unsigned int glGenLists(int range); +int glIsList(unsigned int list); +void glNewList(unsigned int list,int mode); +void glEndList(void); +void glCallList(unsigned int list); + +/* clear */ +void glClear(int mask); +void glClearColor(float r,float g,float b,float a); +void glClearDepth(double depth); + +/* selection */ +int glRenderMode(int mode); +void glSelectBuffer(int size,unsigned int *buf); + +void glInitNames(void); +void glPushName(unsigned int name); +void glPopName(void); +void glLoadName(unsigned int name); + +/* textures */ +void glGenTextures(int n, unsigned int *textures); +void glDeleteTextures(int n, const unsigned int *textures); +void glBindTexture(int target,int texture); +void glTexImage2D( int target, int level, int components, + int width, int height, int border, + int format, int type, void *pixels); +void glTexEnvi(int target,int pname,int param); +void glTexParameteri(int target,int pname,int param); +void glPixelStorei(int pname,int param); + +/* lighting */ + +void glMaterialfv(int mode,int type,float *v); +void glMaterialf(int mode,int type,float v); +void glColorMaterial(int mode,int type); + +void glLightfv(int light,int type,float *v); +void glLightf(int light,int type,float v); +void glLightModeli(int pname,int param); +void glLightModelfv(int pname,float *param); + +/* misc */ + +void glFlush(void); +void glHint(int target,int mode); +void glGetIntegerv(int pname,int *params); +void glGetFloatv(int pname, float *v); +void glFrontFace(int mode); + +/* opengl 1.2 arrays */ +void glEnableClientState(GLenum array); +void glDisableClientState(GLenum array); +void glArrayElement(GLint i); +void glVertexPointer(GLint size, GLenum type, GLsizei stride, + const GLvoid *pointer); +void glColorPointer(GLint size, GLenum type, GLsizei stride, + const GLvoid *pointer); +void glNormalPointer(GLenum type, GLsizei stride, + const GLvoid *pointer); +void glTexCoordPointer(GLint size, GLenum type, GLsizei stride, + const GLvoid *pointer); + +/* opengl 1.2 polygon offset */ +void glPolygonOffset(GLfloat factor, GLfloat units); + +/* not implemented, just added to compile */ + /* +inline void glPointSize(float) {} +inline void glLineWidth(float) {} +inline void glDeleteLists(int, int) {} +inline void glDepthFunc(int) {} +inline void glBlendFunc(int, int) {} +inline void glTexEnvf(int, int, int) {} +inline void glOrtho(float,float,float,float,float,float){} +inline void glVertex2i(int,int) {} +inline void glDepthMask(int) {} +inline void glFogi(int, int) {} +inline void glFogfv(int, const float*) {} +inline void glFogf(int, float) {} +inline void glRasterPos2f(float, float) {} +inline void glPolygonStipple(void*) {} +inline void glTexParameterf(int, int, int) {}; + */ +/* non compatible functions */ + +void glDebug(int mode); + +void glInit(void *zbuffer); +void glClose(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/tinygl/glu.h b/programs/develop/ktcc/trunk/libc/include/tinygl/glu.h new file mode 100644 index 0000000000..81e847869c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/tinygl/glu.h @@ -0,0 +1,44 @@ + +#ifndef GLU_H +#define GLU_H + +#ifdef __cplusplus +extern "C" { +#endif + +void gluPerspective( GLdouble fovy, GLdouble aspect, + GLdouble zNear, GLdouble zFar ); + +void +gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, + GLdouble centerx, GLdouble centery, GLdouble centerz, + GLdouble upx, GLdouble upy, GLdouble upz); + + +void drawTorus(float rc, int numc, float rt, int numt); + + + +typedef struct { + int draw_style; +} GLUquadricObj; + +#define GLU_LINE 0 + +GLUquadricObj* gluNewQuadric(void); +void gluQuadricDrawStyle(GLUquadricObj *obj, int style); + +void gluSphere(GLUquadricObj *qobj, + float radius,int slices,int stacks); +void gluCylinder( GLUquadricObj *qobj, + GLdouble baseRadius, GLdouble topRadius, GLdouble height, + GLint slices, GLint stacks ); +void gluDisk( GLUquadricObj *qobj, + GLdouble innerRadius, GLdouble outerRadius, + GLint slices, GLint loops ); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/tinygl/kosgl.h b/programs/develop/ktcc/trunk/libc/include/tinygl/kosgl.h new file mode 100644 index 0000000000..ce192ec704 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/tinygl/kosgl.h @@ -0,0 +1,28 @@ +#ifndef KOSGL_H +#define KOSGL_H + +#include "gl.h" +#include "glu.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void *KOSGLContext; + +extern KOSGLContext kosglCreateContext( KOSGLContext shareList, int flags ); + +extern void kosglDestroyContext( KOSGLContext ctx1 ); + +extern int kosglMakeCurrent( int win_x0, int win_y0, + int win_x, int win_y, + KOSGLContext ctx); + +extern void kosglSwapBuffers(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/varargs.h b/programs/develop/ktcc/trunk/libc/include/varargs.h new file mode 100644 index 0000000000..d614366edb --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/varargs.h @@ -0,0 +1,12 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _VARARGS_H +#define _VARARGS_H + +#error "TinyCC no longer implements ." +#error "Revise your code to use ." + +#endif diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/_ksys_files_acces.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/_ksys_files_acces.asm new file mode 100644 index 0000000000..2c7c51a72e --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/_ksys_files_acces.asm @@ -0,0 +1,124 @@ +format ELF + +section '.text' executable + +include '../proc32.inc' +public _ksys_get_filesize +public _ksys_readfile +public _ksys_rewritefile +public _ksys_appendtofile + +align 4 +proc _ksys_get_filesize stdcall, filename:dword + + xor eax,eax + mov ebx,[filename] + mov [fileinfo.subproc],dword 5 + mov [fileinfo.offset_l],eax + mov [fileinfo.offset_h],eax + mov [fileinfo.size],eax + mov [fileinfo.data],dword buffer_for_info + mov [fileinfo.letter],al + mov [fileinfo.filename],ebx + + mov eax,70 + mov ebx,fileinfo + int 0x40 + + test eax,eax + jnz error_for_file_size + + mov eax,[buffer_for_info+32] ;file size + ret + + error_for_file_size: + neg eax + + ret +endp + + +align 4 +proc _ksys_readfile stdcall,filename:dword,position:dword,sizeblock:dword,buffer:dword, preadbytes:dword + + xor eax,eax + mov ebx,[position] + mov ecx,[sizeblock] + mov edx,[buffer] + mov esi,[filename] + mov [fileinfo.subproc],eax + mov [fileinfo.offset_l],ebx + mov [fileinfo.offset_h],eax + mov [fileinfo.size],ecx + mov [fileinfo.data],edx + mov [fileinfo.letter],al + mov [fileinfo.filename],esi + + mov eax,70 + mov ebx,fileinfo + int 0x40 + + mov esi, [preadbytes] + mov [esi], ebx + + ret +endp + +align 4 +proc _ksys_rewritefile stdcall,filename:dword,sizeblock:dword,data_write:dword + + xor eax,eax + mov ebx,[sizeblock] + mov ecx,[data_write] + mov edx,[filename] + mov [fileinfo.subproc],dword 2 + mov [fileinfo.offset_l],eax + mov [fileinfo.offset_h],eax + mov [fileinfo.size],ebx + mov [fileinfo.data],ecx + mov [fileinfo.letter],al + mov [fileinfo.filename],edx + + mov eax,70 + mov ebx,fileinfo + int 0x40 + + ret +endp + +align 4 +proc _ksys_appendtofile stdcall,filename:dword,pos:dword,sizeblock:dword,data_append:dword + + xor eax,eax + mov ebx,[pos] + mov ecx,[sizeblock] + mov edx,[data_append] + mov esi,[filename] + mov [fileinfo.subproc],dword 3 + mov [fileinfo.offset_l],ebx + mov [fileinfo.offset_h],eax + mov [fileinfo.size],ecx + mov [fileinfo.data],edx + mov [fileinfo.letter],al + mov [fileinfo.filename],esi + + mov eax,70 + mov ebx,fileinfo + int 0x40 + + ret +endp + +struc FILEIO +{ + .subproc rd 1 + .offset_l rd 1 + .offset_h rd 1 + .size rd 1 + .data rd 1 + .letter rb 1 + .filename rd 1 +} + +fileinfo FILEIO +buffer_for_info rd 11 diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/backgr.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/backgr.asm new file mode 100644 index 0000000000..c65ff0c581 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/backgr.asm @@ -0,0 +1,54 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_set_background_size,8 +;arg1 - xsize +;arg2 - ysize + push ebx + mov ecx,[esp+8] + mov edx,[esp+12] + mov eax,15 + mov ebx,1 + int 0x40 + pop ebx + ret 8 +public_stdcall _ksys_write_background_mem,8 +;arg1 - pos +;arg2 - color + push ebx + mov eax,15 + mov ebx,2 + mov ecx,[esp+8] + mov edx,[esp+12] + int 0x40 + pop ebx + ret 8 +public_stdcall _ksys_draw_background,0 + mov edx,ebx + mov eax,15 + mov ebx,3 + int 0x40 + mov ebx,edx + ret +public_stdcall _ksys_set_background_draw_type,4 +;arg1 - type + mov edx,ebx + mov eax,15 + mov ebx,4 + mov ecx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 +public_stdcall _ksys_background_blockmove,12 +;arg1 - source +;arg2 - position in dest +;arg3 - size + push ebx esi + mov eax,15 + mov ebx,5 + mov ecx,[esp+12] + mov edx,[esp+16] + mov esi,[esp+20] + int 0x40 + pop esi ebx + ret 12 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/button.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/button.asm new file mode 100644 index 0000000000..e6baf0c8d3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/button.asm @@ -0,0 +1,35 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_make_button,24 +;arg1 - x +;arg2 - y +;arg3 - xsize +;arg4 - ysize +;arg5 - id +;arg6 - color + push ebx esi + mov ebx,[esp+12] + shl ebx,16 + mov bx,[esp+20] + mov ecx,[esp+16] + shl ecx,16 + mov cx,[esp+24] + mov edx,[esp+28] + mov esi,[esp+32] + mov eax,8 + int 0x40 + pop esi ebx + ret 24 + +public_stdcall _ksys_get_button_id,0 + mov eax,17 + int 0x40 + test al,al + jnz .no_button + shr eax,8 + ret +.no_button: + xor eax,eax + dec eax + ret \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/clock.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/clock.asm new file mode 100644 index 0000000000..726852a650 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/clock.asm @@ -0,0 +1,15 @@ +format ELF + +include "../proc32.inc" + +section '.text' executable +public _ksys_get_system_clock + +align 4 +proc _ksys_get_system_clock stdcall + + mov eax,3 + int 0x40 + ret + +endp \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/cofflib.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/cofflib.asm new file mode 100644 index 0000000000..a4114fd1b0 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/cofflib.asm @@ -0,0 +1,76 @@ +format ELF +include '../proc32.inc' +section '.text' executable + +public _ksys_cofflib_load +public _ksys_cofflib_getproc + +proc _ksys_cofflib_load stdcall, name:dword + + mov eax, 68 + mov ebx, 19 + mov ecx, [name] + int 0x40 + ret +endp + +proc _ksys_cofflib_getproc stdcall, export:dword,name:dword + + mov ebx,[export] + + next_name_check: + + mov ecx,[ebx] + test ecx,ecx + jz end_export + + ;cmp export string with name + mov esi,[name] + xor edi,edi + next_simbol_check: + + xor eax,eax + mov al,[ecx] +; siemargl moved to post-check +; test al,al +; jz exit_check_simbol + + xor edx,edx + mov dl,[esi] + cmp al,dl + je simbols_equvalent + add edi,1 + jmp exit_check_simbol + simbols_equvalent: + test al,al + jz exit_check_simbol + + ;pushad + + ;mov cl,al + ;mov ebx,1 + ;mov eax,63 + ;int 0x40 + + ;popad + + add ecx,1 + add esi,1 + jmp next_simbol_check + exit_check_simbol: + + test edi,edi + jnz function_not_finded + mov eax,[ebx+4] + jmp end_export + function_not_finded: + + add ebx,8 + + jmp next_name_check + + end_export: + + ret +endp + diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/date.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/date.asm new file mode 100644 index 0000000000..96e0b5d1b7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/date.asm @@ -0,0 +1,7 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_get_date,0 + mov eax,29 + int 0x40 + ret \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/debug_board.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/debug_board.asm new file mode 100644 index 0000000000..9f27e6bd1b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/debug_board.asm @@ -0,0 +1,56 @@ +format ELF + +include "../proc32.inc" + +section '.text' executable + +public _ksys_debug_out +public debug_out_str + +align 4 +proc _ksys_debug_out stdcall, c:dword + + pushad + + xor ecx,ecx + mov cl,byte[c] + mov ebx,1 + mov eax,63 + int 0x40 + + popad + + ret + +endp + +align 4 +proc debug_out_str stdcall, s:dword + + pushad + + mov eax,[s] ;eax=pointer to string + next_simbol_print: + + xor ebx,ebx + mov bl,[eax] + test bl,bl + jz exit_print_str + + cmp bl,10 + jne no_new_line + mov ecx,13 + stdcall _ksys_debug_out, ecx + no_new_line: + + stdcall _ksys_debug_out, ebx + add eax,1 + + jmp next_simbol_print + + exit_print_str: + + popad + + ret +endp diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/delay.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/delay.asm new file mode 100644 index 0000000000..b94fa14f04 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/delay.asm @@ -0,0 +1,11 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_delay,4 +;arg1 - time + mov edx,ebx + mov eax,5 + mov ebx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/dga.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/dga.asm new file mode 100644 index 0000000000..87ae6ca273 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/dga.asm @@ -0,0 +1,34 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_dga_get_resolution,16 +;arg1 - *xres +;arg2 - *yres +;arg3 - *bpp +;arg4 - *bpscan + mov edx,ebx + + mov eax,61 + mov ebx,1 + int 0x40 + mov ebx,[esp+8] + mov [ebx],ax + mov word [ebx+2],0 + shr eax,16 + mov ebx,[esp+4] + mov [ebx],eax + + mov eax,61 + mov ebx,2 + int 0x40 + mov ebx,[esp+12] + mov [ebx],eax + + mov eax,61 + mov ebx,3 + int 0x40 + mov ebx,[esp+16] + mov [ebx],eax + + mov ebx,edx + ret 16 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/draw_bar.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_bar.asm new file mode 100644 index 0000000000..9c9f73484b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_bar.asm @@ -0,0 +1,21 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_draw_bar,20 +;arg1 - x +;arg2 - y +;arg3 - xsize +;arg4 - ysize +;arg5 - color + push ebx + mov eax,13 + mov ebx,[esp+8] + shl ebx,16 + mov bx,[esp+16] + mov ecx,[esp+12] + shl ecx,16 + mov cx,[esp+20] + mov edx,[esp+24] + int 0x40 + pop ebx + ret 20 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/draw_image.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_image.asm new file mode 100644 index 0000000000..5c3af8a078 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_image.asm @@ -0,0 +1,21 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_putimage,20 +;arg1 - x +;arg2 - y +;arg3 - xsize +;arg4 - ysize +;arg5 - image + push ebx + mov ebx,[esp+24] + mov ecx,[esp+16] + shl ecx,16 + mov ecx,[esp+20] + mov ebx,[esp+8] + shl ebx,16 + mov ebx,[esp+12] + mov eax,7 + int 0x40 + pop ebx + ret 20 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/draw_window.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_window.asm new file mode 100644 index 0000000000..ffbff79ba3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/draw_window.asm @@ -0,0 +1,34 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_draw_window,36 +;arg1 - xcoord +;arg2 - ycoord +;arg3 - xsize +;arg4 - ysize +;arg5 - workcolor +;arg6 - type +;arg7 - captioncolor +;arg8 - windowtype +;arg9 - bordercolor + push ebp + mov ebp,esp + push ebx esi edi + mov ebx,[ebp+8] + shl ebx,16 + mov bx,[ebp+16] + mov ecx,[ebp+12] + shl ecx,16 + mov cx,[ebp+20] + mov edx,[ebp+28] + shl edx,24 + add edx,[ebp+24] + mov esi,[ebp+36] + shl esi,24 + add esi,[ebp+32] + mov edi,[ebp+40] + xor eax,eax + int 0x40 + pop edi esi ebx + pop ebp + ret 36 diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/event.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/event.asm new file mode 100644 index 0000000000..817f6d8c23 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/event.asm @@ -0,0 +1,40 @@ +format ELF + +section '.text' executable + +public _ksys_wait_for_event_infinite +public _ksys_check_for_event +public _ksys_wait_for_event +public _ksys_set_wanted_events + +_ksys_wait_for_event_infinite: + + mov eax,10 + int 0x40 + + ret + +_ksys_check_for_event: + + mov eax,11 + int 0x40 + + ret + +_ksys_wait_for_event: + +;arg1 - time + mov eax,23 + mov ebx,[esp+4] + int 0x40 + + ret 4 + +_ksys_set_wanted_events: + +;arg1 - flags + mov eax,40 + mov ebx,[esp+4] + int 0x40 + + ret 4 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/exit.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/exit.asm new file mode 100644 index 0000000000..0dc6def546 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/exit.asm @@ -0,0 +1,8 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_exit,0 + xor eax,eax + dec eax + int 0x40 +; ret \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/ipc.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/ipc.asm new file mode 100644 index 0000000000..309c2217b6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/ipc.asm @@ -0,0 +1,28 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_send_message,12 +;arg1 - pid +;arg2 - msg +;arg3 - size + push ebx esi + mov eax,60 + mov ebx,2 + mov ecx,[esp+12] + mov edx,[esp+16] + mov esi,[esp+20] + int 0x40 + pop esi ebx + ret 12 + +public_stdcall _ksys_define_receive_area,8 +;arg1 - area +;arg2 - size + push ebx + mov eax,60 + mov ebx,1 + mov ecx,[esp+8] + mov edx,[esp+12] + int 0x40 + pop ebx + ret 8 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/irq.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/irq.asm new file mode 100644 index 0000000000..79fc761d8b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/irq.asm @@ -0,0 +1,119 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_get_irq_owner,4 +;arg1 - irq + mov edx,ebx + mov eax,41 + mov ebx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 + +public_stdcall _ksys_get_data_read_by_irq,12 +;arg1 - irq +;arg2 - *size +;arg3 - data + mov edx,ebx + mov eax,42 + mov ebx,[esp+4] + int 0x40 + cmp ecx,2 + jz .not_an_owner + push ecx + mov ecx,[esp+16] + test ecx,ecx + jz .ignore_data + mov [ecx],bl +.ignore_data: + mov ecx,[esp+12] + mov [ecx],eax + pop eax + mov ebx,edx + ret 12 +.not_an_owner: + mov eax,2 + mov ebx,edx + ret + +public_stdcall _ksys_send_data_to_device,8 +;arg1 - port +;arg2 - data + mov edx,ebx + mov eax,63 + mov ebx,[esp+8] + mov ecx,[esp+4] + int 0x40 + mov ebx,edx + ret 8 + +public_stdcall _ksys_receive_data_from_device,8 +;arg1 - port +;arg2 - data + mov edx,ebx + mov eax,43 + mov ecx,[esp+4] + add ecx,0x80000000 + int 0x40 + mov ecx,[esp+8] + mov [ecx],bl + mov ebx,edx + ret 8 + +public_stdcall _ksys_program_irq,8 +;arg1 - intrtable +;arg2 - irq + mov edx,ebx + mov eax,44 + mov ebx,[esp+4] + mov ecx,[esp+8] + int 0x40 + mov ebx,edx + ret 8 + +public_stdcall _ksys_reserve_irq,4 +;arg1 - irq + mov edx,ebx + mov eax,45 + xor ebx,ebx + mov ecx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 + +public_stdcall _ksys_free_irq,4 +;arg1 - irq + mov edx,ebx + mov eax,45 + xor ebx,ebx + inc ebx + mov ecx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 + +public_stdcall _ksys_reserve_port_area,8 +;arg1 - start +;arg2 - end + push ebx + mov eax,46 + xor ebx,ebx + mov ecx,[esp+8] + mov edx,[esp+12] + int 0x40 + pop ebx + ret 8 + +public_stdcall _ksys_free_port_area,8 +;arg1 - start +;arg2 - end + push ebx + mov eax,46 + xor ebx,ebx + inc ebx + mov ecx,[esp+8] + mov edx,[esp+12] + int 0x40 + pop ebx + ret 8 + \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/keyboard.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/keyboard.asm new file mode 100644 index 0000000000..8d9f13c6c3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/keyboard.asm @@ -0,0 +1,29 @@ +format ELF + +include "../proc32.inc" + +section '.text' executable + +public _ksys_get_key +public _ksys_set_keyboard_mode + +align 4 +proc _ksys_get_key stdcall + + mov eax,2 + int 0x40 + ret + +endp + +align 4 +proc _ksys_set_keyboard_mode stdcall, mode:dword + + mov edx,ebx + mov eax,66 + xor ebx,ebx + inc ebx + mov ecx,[mode] + mov ebx,edx + ret +endp \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/line.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/line.asm new file mode 100644 index 0000000000..e776ad3698 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/line.asm @@ -0,0 +1,21 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_line,20 +;arg1 - x1 +;arg2 - y1 +;arg3 - x2 +;arg4 - y2 +;arg5 - color + push ebx + mov ebx,[esp+8] + shl ebx,16 + mov bx,[esp+16] + mov ecx,[esp+12] + shl ecx,16 + mov cx,[esp+20] + mov edx,[esp+24] + mov eax,38 + int 0x40 + pop ebx + ret 20 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/midi.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/midi.asm new file mode 100644 index 0000000000..80655e4cc8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/midi.asm @@ -0,0 +1,21 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_midi_reset,0 + mov edx,ebx + mov eax,20 + xor ebx,ebx + inc ebx + int 0x40 + mov ebx,edx + ret + +public_stdcall _ksys_midi_send,4 +;arg1 - data + mov edx,ebx + mov eax,20 + mov ebx,2 + xor ecx,ecx + mov cl,[esp+4] + mov ebx,edx + ret 4 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/mouse.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/mouse.asm new file mode 100644 index 0000000000..f609b8c63c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/mouse.asm @@ -0,0 +1,25 @@ + +format ELF + +section '.text' executable + +public _ksys_GetMouseXY +public _ksys_GetMouseButtonsState + +align 4 +_ksys_GetMouseXY: + + mov eax,37 + mov ebx,1 + int 0x40 + + ret + +align 4 +_ksys_GetMouseButtonsState: + + mov eax,37 + mov ebx,2 + int 0x40 + + ret diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/pci.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/pci.asm new file mode 100644 index 0000000000..6af5d0db59 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/pci.asm @@ -0,0 +1,138 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_get_pci_version,0 + mov edx,ebx + mov eax,62 + xor ebx,ebx + int 0x40 + movzx eax,ax + mov ebx,edx + ret + +public_stdcall _ksys_get_last_pci_bus,0 + mov edx,ebx + mov eax,62 + xor ebx,ebx + inc ebx + int 0x40 + movzx eax,al + mov ebx,edx + ret + +public_stdcall _ksys_get_pci_access_mechanism,0 + mov edx,ebx + mov eax,62 + mov ebx,2 + int 0x40 + movzx eax,al + mov ebx,edx + ret + +public_stdcall _ksys_pci_read_config_byte,16 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg + mov edx,ebx + mov eax,62 + mov bl,4 + mov bh,[esp+4] + mov ch,[esp+8] + shl ch,3 + add ch,[esp+12] + mov cl,[esp+16] + int 0x40 + mov ebx,edx + ret 16 + +public_stdcall _ksys_pci_read_config_word,16 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg + mov edx,ebx + mov eax,62 + mov bl,5 + mov bh,[esp+4] + mov ch,[esp+8] + shl ch,3 + add ch,[esp+12] + mov cl,[esp+16] + int 0x40 + mov ebx,edx + ret 16 + +public_stdcall _ksys_pci_read_config_dword,16 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg + mov edx,ebx + mov eax,62 + mov bl,6 + mov bh,[esp+4] + mov ch,[esp+8] + shl ch,3 + add ch,[esp+12] + mov cl,[esp+16] + int 0x40 + mov ebx,edx + ret 16 + +public_stdcall _ksys_pci_write_config_byte,20 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg +;arg5 - value + push ebx + mov eax,62 + mov bl,8 + mov bh,[esp+8] + mov ch,[esp+12] + shl ch,3 + mov ch,[esp+16] + mov cl,[esp+20] + movzx edx,byte [esp+24] + int 0x40 + pop ebx + ret 20 + +public_stdcall _ksys_pci_write_config_word,20 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg +;arg5 - value + push ebx + mov eax,62 + mov bl,9 + mov bh,[esp+8] + mov ch,[esp+12] + shl ch,3 + mov ch,[esp+16] + mov cl,[esp+20] + movzx edx,word [esp+24] + int 0x40 + pop ebx + ret 20 + +public_stdcall _ksys_pci_write_config_dword,20 +;arg1 - bus +;arg2 - dev +;arg3 - fn +;arg4 - reg +;arg5 - value + push ebx + mov eax,62 + mov bl,10 + mov bh,[esp+8] + mov ch,[esp+12] + shl ch,3 + mov ch,[esp+16] + mov cl,[esp+20] + mov edx,[esp+24] + int 0x40 + pop ebx + ret 20 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/pixel.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/pixel.asm new file mode 100644 index 0000000000..939bf32e00 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/pixel.asm @@ -0,0 +1,16 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_putpixel,12 +;arg1 - x +;arg2 - y +;arg3 - color + push ebx + xor eax,eax + mov ebx,[esp+8] + inc eax + mov ecx,[esp+12] + mov edx,[esp+16] + int 0x40 + pop ebx + ret 12 diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/process.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/process.asm new file mode 100644 index 0000000000..797a616ecb --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/process.asm @@ -0,0 +1,24 @@ +format ELF +;include "public_stdcall.inc" + +public _ksys_get_process_table +public _ksys_kill_process + +section '.text' executable + +_ksys_get_process_table: +;arg1 - pointer to information +;arg2 - pid + mov eax,9 + mov ebx,[esp+4] + mov ecx,[esp+8] + int 0x40 + ret 8 + +_ksys_kill_process: +;arg - pid + mov eax, 18 + mov ebx, 18 + mov ecx,[esp+4] + int 0x40 + ret 4 diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/screen.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/screen.asm new file mode 100644 index 0000000000..cec12f4e34 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/screen.asm @@ -0,0 +1,15 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_get_screen_size,8 +;arg1 - x +;arg2 - y + mov eax,14 + int 0x40 + mov ecx,[esp+8] + mov [ecx],ax + mov word [ecx+2],0 + shr eax,16 + mov ecx,[esp+4] + mov [ecx],eax + ret 8 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/skin.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/skin.asm new file mode 100644 index 0000000000..fc31bff852 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/skin.asm @@ -0,0 +1,13 @@ +format ELF + +section '.text' executable + +public _ksys_get_skin_height + +_ksys_get_skin_height: + + mov eax,48 + mov ebx,4 + int 0x40 + + ret \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/sound.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/sound.asm new file mode 100644 index 0000000000..2b6d4d7fc8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/sound.asm @@ -0,0 +1,65 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_sound_load_block,4 +;arg1 - blockptr + mov edx,ebx + mov eax,55 + xor ebx,ebx + mov ecx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 + +public_stdcall _ksys_sound_play_block,0 + mov edx,ebx + mov eax,55 + xor ebx,ebx + inc ebx + int 0x40 + mov ebx,edx + ret + +public_stdcall _ksys_sound_set_channels,4 +;arg1 - channels + push ebx + mov eax,55 + mov ebx,2 + xor ecx,ecx + mov edx,[esp+8] + int 0x40 + pop ebx + ret 4 + +public_stdcall _ksys_sound_set_data_size,4 +;arg1 - data size + push ebx + mov eax,55 + mov ebx,2 + xor ecx,ecx + inc ecx + mov edx,[esp+8] + int 0x40 + pop ebx + ret 4 + +public_stdcall _ksys_sound_set_frequency,4 +;arg1 - frequency + push ebx + mov eax,55 + mov ebx,2 + mov ecx,2 + mov edx,[esp+8] + int 0x40 + pop ebx + ret 4 + +public_stdcall _ksys_sound_speaker_play,4 +;arg1 - data + mov edx,ebx + mov eax,55 + mov ebx,55 + mov esi,[esp+4] + int 0x40 + mov ebx,edx + ret 4 diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/thread.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/thread.asm new file mode 100644 index 0000000000..ca744cd5ae --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/thread.asm @@ -0,0 +1,33 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +extrn sysmalloc +public_stdcall _ksys_start_thread,12 +;arg1 - proc +;arg2 - stacksize +;arg3 - pid + push dword [esp+8] + call sysmalloc + test eax,eax + jz .no_mem + push ebx + mov edx,eax + add edx,[esp+12] + mov [edx-4],dword 0 + mov ecx,[esp+8] + mov ebx,1 + mov eax,51 + int 0x40 + mov ebx,[esp+16] + test ebx,ebx + jz .no_val + mov [ebx],eax +.no_val: + mov eax,edx + sub eax,[esp+12] + pop ebx + ret 12 +.no_mem: + mov ecx,[esp+12] + mov [ecx],eax + ret 12 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/window_redraw.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/window_redraw.asm new file mode 100644 index 0000000000..226ed18585 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/window_redraw.asm @@ -0,0 +1,11 @@ +format ELF +include "public_stdcall.inc" +section '.text' executable +public_stdcall _ksys_window_redraw,4 +;arg1 - status + mov edx,ebx + mov eax,12 + mov ebx,[esp+4] + int 0x40 + mov ebx,edx + ret 4 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/kolibrisys/write_text.asm b/programs/develop/ktcc/trunk/libc/kolibrisys/write_text.asm new file mode 100644 index 0000000000..db97c6c4ab --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/kolibrisys/write_text.asm @@ -0,0 +1,21 @@ +format ELF +section '.text' executable +public _ksys_write_text + +_ksys_write_text: +;arg1 - x +;arg2 - y +;arg3 - color +;arg4 - text +;arg5 - len + + mov eax,4 + mov ebx,[esp+4] + shl ebx,16 + mov bx,[esp+8] + mov ecx,[esp+12] + mov edx,[esp+16] + mov esi,[esp+20] + int 0x40 + + ret 20 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/libgen/basename.c b/programs/develop/ktcc/trunk/libc/libgen/basename.c new file mode 100644 index 0000000000..03f9813f33 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/libgen/basename.c @@ -0,0 +1,23 @@ +/* Return the name-within-directory of a file name. + Copyright (C) 1996-2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + The GNU C Library 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 + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +char * +basename (const char *filename) +{ + char *p = strrchr (filename, '/'); + return p ? p + 1 : (char *) filename; +} diff --git a/programs/develop/ktcc/trunk/libc/libgen/dirname.c b/programs/develop/ktcc/trunk/libc/libgen/dirname.c new file mode 100644 index 0000000000..b889f7d0c6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/libgen/dirname.c @@ -0,0 +1,67 @@ +/* dirname - return directory part of PATH. + Copyright (C) 1996-2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1996. + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + The GNU C Library 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 + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +char * +dirname (char *path) +{ + static const char dot[] = "."; + char *last_slash; + /* Find last '/'. */ + last_slash = path != NULL ? strrchr (path, '/') : NULL; + if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') + { + /* Determine whether all remaining characters are slashes. */ + char *runp; + for (runp = last_slash; runp != path; --runp) + if (runp[-1] != '/') + break; + /* The '/' is the last character, we have to look further. */ + if (runp != path) + last_slash = memrchr (path, '/', runp - path); + } + if (last_slash != NULL) + { + /* Determine whether all remaining characters are slashes. */ + char *runp; + for (runp = last_slash; runp != path; --runp) + if (runp[-1] != '/') + break; + /* Terminate the path. */ + if (runp == path) + { + /* The last slash is the first character in the string. We have to + return "/". As a special case we have to return "//" if there + are exactly two slashes at the beginning of the string. See + XBD 4.10 Path Name Resolution for more information. */ + if (last_slash == path + 1) + ++last_slash; + else + last_slash = path + 1; + } + else + last_slash = runp; + last_slash[0] = '\0'; + } + else + /* This assignment is ill-designed but the XPG specs require to + return a string containing "." in any case no directory part is + found and so a static and constant string is required. */ + path = (char *) dot; + return path; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/math/acos.asm b/programs/develop/ktcc/trunk/libc/math/acos.asm new file mode 100644 index 0000000000..959e9e7a48 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/acos.asm @@ -0,0 +1,23 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public acos_ as "acos" + +acos_: + +; acos(x) = atan(sqrt((1-x*x)/(x*x))) + + fld qword[esp+4] + fld st0 ;Duplicate X on tos. + fmul st0, st1 ;Compute X**2. + fld st0 ;Duplicate X**2 on tos. + fld1 ;Compute 1-X**2. + fsub st0, st1 + fdiv st0, st1 ;Compute (1-x**2)/X**2. + fsqrt ;Compute sqrt((1-X**2)/X**2). + fld1 ;To compute full arctangent. + fpatan ;Compute atan of the above. + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/asin.c b/programs/develop/ktcc/trunk/libc/math/asin.c new file mode 100644 index 0000000000..fd7af3b0c7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/asin.c @@ -0,0 +1,6 @@ +#include + +double asin(double x) +{ + return atan(sqrt(x * x / (1.0 - x * x))); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/math/atan.asm b/programs/develop/ktcc/trunk/libc/math/atan.asm new file mode 100644 index 0000000000..68f6fddc1f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/atan.asm @@ -0,0 +1,15 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public atan_ as "atan" + +atan_: + + fld qword[esp+4] + fld1 + fpatan + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/atan2.asm b/programs/develop/ktcc/trunk/libc/math/atan2.asm new file mode 100644 index 0000000000..e0587bc3e5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/atan2.asm @@ -0,0 +1,15 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public atan2_ as "atan2" + +atan2_: + + fld qword[esp+8] + fld qword[esp+4] + fpatan + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/ceil.asm b/programs/develop/ktcc/trunk/libc/math/ceil.asm new file mode 100644 index 0000000000..c03f6056fd --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/ceil.asm @@ -0,0 +1,30 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public ceil + +ceil: + + push ebp + mov ebp,esp + sub esp,8 + + fstcw [ebp-12] + mov dx,[ebp-12] + or dx,0x0800 + and dx,0xfbff + mov word[ebp-16],dx + fldcw [ebp-16] + + fld qword[ebp+8] + frndint + + fldcw [ebp-12] + + leave + + ret + + diff --git a/programs/develop/ktcc/trunk/libc/math/ceilf.asm b/programs/develop/ktcc/trunk/libc/math/ceilf.asm new file mode 100644 index 0000000000..e5180d68e8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/ceilf.asm @@ -0,0 +1,30 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public ceilf + +ceilf: + + push ebp + mov ebp,esp + sub esp,8 + + fstcw [ebp-12] + mov dx,[ebp-12] + or dx,0x0800 + and dx,0xfbff + mov word[ebp-16],dx + fldcw [ebp-16] + + fld dword[ebp+8] + frndint + + fldcw [ebp-12] + + leave + + ret + + diff --git a/programs/develop/ktcc/trunk/libc/math/cos.asm b/programs/develop/ktcc/trunk/libc/math/cos.asm new file mode 100644 index 0000000000..75ac75e182 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/cos.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public cos_ as "cos" + +cos_: + + fld qword[esp+4] + fcos + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/cosh.c b/programs/develop/ktcc/trunk/libc/math/cosh.c new file mode 100644 index 0000000000..56f0f0aee7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/cosh.c @@ -0,0 +1,6 @@ +#include + +double cosh (double x) +{ + return (exp(x) + exp(-x)) / 2; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/math/exp.asm b/programs/develop/ktcc/trunk/libc/math/exp.asm new file mode 100644 index 0000000000..d3b27b41fa --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/exp.asm @@ -0,0 +1,60 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public exp_ as "exp" +public exp2_ as "exp2" + +SaveCW dw ? +MaskedCW dw ? + +; 2**x = 2**int(x) * 2**frac(x). +; We can easily compute 2**int(x) with fscale and +; 2**frac(x) using f2xm1. +exp2_int: + fstcw [SaveCW] + +; Modify the control word to truncate when rounding. + + fstcw [MaskedCW] + or byte ptr MaskedCW + 1, 1100b + fldcw [MaskedCW] + + fld st0 ;Duplicate tos. + fld st0 + frndint ;Compute integer portion. + + fxch ;Swap whole and int values. + fsub st0, st1 ;Compute fractional part. + + f2xm1 ;Compute 2**frac(x)-1. + fld1 + faddp st1, st0 ;Compute 2**frac(x). + + fxch ;Get integer portion. + fld1 ;Compute 1*2**int(x). + fscale + fstp st1 ;Remove st(1) (which is 1). + + fmulp st1, st0 ;Compute 2**int(x) * 2**frac(x). + fstp st1 ;Remove st1 + + fldcw [SaveCW] ;Restore rounding mode. + ret + +exp_: +; exp(x) = 2**(x * lg(e)) + + fld qword[esp+4] + fldl2e ;Put lg(e) onto the stack. + fmulp st1, st0 ;Compute x*lg(e). + call exp2_int;Compute 2**(x * lg(e)) + ret + +exp2_: + fld qword[esp+4] + call exp2_int;Compute 2 ** x + ret + + diff --git a/programs/develop/ktcc/trunk/libc/math/fabs.asm b/programs/develop/ktcc/trunk/libc/math/fabs.asm new file mode 100644 index 0000000000..1e2cd067c4 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/fabs.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public fabs_ as "fabs" + +fabs_: + + fld qword[esp+4] + fabs + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/fabsf.asm b/programs/develop/ktcc/trunk/libc/math/fabsf.asm new file mode 100644 index 0000000000..fba999a01b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/fabsf.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public fabsf + +fabsf: + + fld dword[esp+4] + fabs + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/floor.asm b/programs/develop/ktcc/trunk/libc/math/floor.asm new file mode 100644 index 0000000000..8df60993ed --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/floor.asm @@ -0,0 +1,29 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public floor + +floor: + + push ebp + mov ebp,esp + sub esp,8 + + fstcw [ebp-12] + mov dx,word[ebp-12] + or dx,0x0400 + and dx,0xf7ff + mov word[ebp-16],dx + fldcw [ebp-16] + + fld qword[ebp+8] + frndint + + fldcw [ebp-12] + + leave + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/floorf.asm b/programs/develop/ktcc/trunk/libc/math/floorf.asm new file mode 100644 index 0000000000..bc262752c6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/floorf.asm @@ -0,0 +1,29 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public floorf + +floorf: + + push ebp + mov ebp,esp + sub esp,8 + + fstcw [ebp-12] + mov dx,word[ebp-12] + or dx,0x0400 + and dx,0xf7ff + mov word[ebp-16],dx + fldcw [ebp-16] + + fld dword[ebp+8] + frndint + + fldcw [ebp-12] + + leave + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/fmod.c b/programs/develop/ktcc/trunk/libc/math/fmod.c new file mode 100644 index 0000000000..4e68f19bc2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/fmod.c @@ -0,0 +1,86 @@ +#include + +double remainder(double numer, double denom) +{ + double res; + asm("fldl %2;" + "fldl %1;" + "fprem1;" + "fstpl %0;" + "fstp %%st;" + : "=m"(res) + : "m"(numer), "m"(denom) + ); + return res; +} +//remainder of 5.3 / 2 is -0.700000 +//remainder of 18.5 / 4.2 is 1.700000 + + + +double fmod(double numer, double denom) +{ + double res; + asm("fldl %2;" + "fldl %1;" + "fprem;" + "fstpl %0;" + "fstp %%st;" + : "=m"(res) + : "m"(numer), "m"(denom) + ); + return res; +} +// fmod of 5.3 / 2 is 1.300000 +// fmod of 18.5 / 4.2 is 1.700000 + + +double modf(double x, double *intpart) +{ + double res, intp; + asm("fldl %2;" + "fldl %2;" + "frndint;" + "fstl %1;" + "fxch;" + "fsubp %%st, %%st(1);" + "fstpl %0" + : "=m"(res), "=m"(intp) + : "m"(x) + ); + *intpart = intp; + return res; +} + +double ldexp (double x, int expon) +// = x * 2^expot +{ + double res; + asm("fildl %2;" + "fldl %1;" + "fscale;" + "fstpl %0;" + "fstp %%st;" + : "=m"(res) + : "m"(x), "m"(expon) + ); + + return res; +} + +double frexp (double x, int* expon) +{ + double res; + asm("fldl %2;" + "fxtract;" + "fstpl %0;" + "fistpl %1;" + "fstp %%st;" + : "=m"(res), "=m"(*expon) + : "m"(x) + ); + +// *expon = (int)ex; + return res; +} +// 8.000000 = 0.500000 * 2^ 4 diff --git a/programs/develop/ktcc/trunk/libc/math/libtcc1.c b/programs/develop/ktcc/trunk/libc/math/libtcc1.c new file mode 100644 index 0000000000..2d36a205a1 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/libtcc1.c @@ -0,0 +1,754 @@ +/* TCC runtime library. + Parts of this code are (c) 2002 Fabrice Bellard + + Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc. + +This file 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, or (at your option) any +later version. + +In addition to the permissions in the GNU General Public License, the +Free Software Foundation gives you unlimited permission to link the +compiled version of this file into combinations with other programs, +and to distribute those combinations without any restriction coming +from the use of this file. (The General Public License restrictions +do apply in other respects; for example, they cover modification of +the file, and distribution when not linked into a combine +executable.) + +This file 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; see the file COPYING. If not, write to +the Free Software Foundation, 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ + +//#include +#define TCC_TARGET_I386 + +#define W_TYPE_SIZE 32 +#define BITS_PER_UNIT 8 + +typedef int Wtype; +typedef unsigned int UWtype; +typedef unsigned int USItype; +typedef long long DWtype; +typedef unsigned long long UDWtype; + +struct DWstruct { + Wtype low, high; +}; + +typedef union +{ + struct DWstruct s; + DWtype ll; +} DWunion; + +typedef long double XFtype; +#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT) +#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE) + +/* the following deal with IEEE single-precision numbers */ +#define EXCESS 126 +#define SIGNBIT 0x80000000 +#define HIDDEN (1 << 23) +#define SIGN(fp) ((fp) & SIGNBIT) +#define EXP(fp) (((fp) >> 23) & 0xFF) +#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN) +#define PACK(s,e,m) ((s) | ((e) << 23) | (m)) + +/* the following deal with IEEE double-precision numbers */ +#define EXCESSD 1022 +#define HIDDEND (1 << 20) +#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF) +#define SIGND(fp) ((fp.l.upper) & SIGNBIT) +#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \ + (fp.l.lower >> 22)) +#define HIDDEND_LL ((long long)1 << 52) +#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL) +#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m)) + +/* the following deal with x86 long double-precision numbers */ +#define EXCESSLD 16382 +#define EXPLD(fp) (fp.l.upper & 0x7fff) +#define SIGNLD(fp) ((fp.l.upper) & 0x8000) + +/* only for x86 */ +union ldouble_long { + long double ld; + struct { + unsigned long long lower; + unsigned short upper; + } l; +}; + +union double_long { + double d; +#if 1 + struct { + unsigned int lower; + int upper; + } l; +#else + struct { + int upper; + unsigned int lower; + } l; +#endif + long long ll; +}; + +union float_long { + float f; + unsigned int l; +}; + +/* XXX: we don't support several builtin supports for now */ +#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM) + +/* XXX: use gcc/tcc intrinsic ? */ +#if defined(TCC_TARGET_I386) +#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ + __asm__ ("subl %5,%1\n\tsbbl %3,%0" \ + : "=r" ((USItype) (sh)), \ + "=&r" ((USItype) (sl)) \ + : "0" ((USItype) (ah)), \ + "g" ((USItype) (bh)), \ + "1" ((USItype) (al)), \ + "g" ((USItype) (bl))) +#define umul_ppmm(w1, w0, u, v) \ + __asm__ ("mull %3" \ + : "=a" ((USItype) (w0)), \ + "=d" ((USItype) (w1)) \ + : "%0" ((USItype) (u)), \ + "rm" ((USItype) (v))) +#define udiv_qrnnd(q, r, n1, n0, dv) \ + __asm__ ("divl %4" \ + : "=a" ((USItype) (q)), \ + "=d" ((USItype) (r)) \ + : "0" ((USItype) (n0)), \ + "1" ((USItype) (n1)), \ + "rm" ((USItype) (dv))) +#define count_leading_zeros(count, x) \ + do { \ + USItype __cbtmp; \ + __asm__ ("bsrl %1,%0" \ + : "=r" (__cbtmp) : "rm" ((USItype) (x))); \ + (count) = __cbtmp ^ 31; \ + } while (0) +#else +#error unsupported CPU type +#endif + +/* most of this code is taken from libgcc2.c from gcc */ + +static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp) +{ + DWunion ww; + DWunion nn, dd; + DWunion rr; + UWtype d0, d1, n0, n1, n2; + UWtype q0, q1; + UWtype b, bm; + + nn.ll = n; + dd.ll = d; + + d0 = dd.s.low; + d1 = dd.s.high; + n0 = nn.s.low; + n1 = nn.s.high; + +#if !defined(UDIV_NEEDS_NORMALIZATION) + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + udiv_qrnnd (q1, n1, 0, n1, d0); + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0. */ + } + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = 0; + *rp = rr.ll; + } + } + +#else /* UDIV_NEEDS_NORMALIZATION */ + + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + count_leading_zeros (bm, d0); + + if (bm != 0) + { + /* Normalize, i.e. make the most significant bit of the + denominator set. */ + + d0 = d0 << bm; + n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm)); + n0 = n0 << bm; + } + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0 >> bm. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + count_leading_zeros (bm, d0); + + if (bm == 0) + { + /* From (n1 >= d0) /\ (the most significant bit of d0 is set), + conclude (the most significant bit of n1 is set) /\ (the + leading quotient digit q1 = 1). + + This special case is necessary, not an optimization. + (Shifts counts of W_TYPE_SIZE are undefined.) */ + + n1 -= d0; + q1 = 1; + } + else + { + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q1, n1, n2, n1, d0); + } + + /* n1 != d0... */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0 >> bm. */ + } + + if (rp != 0) + { + rr.s.low = n0 >> bm; + rr.s.high = 0; + *rp = rr.ll; + } + } +#endif /* UDIV_NEEDS_NORMALIZATION */ + + else + { + if (d1 > n1) + { + /* 00 = nn / DD */ + + q0 = 0; + q1 = 0; + + /* Remainder in n1n0. */ + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + /* 0q = NN / dd */ + + count_leading_zeros (bm, d1); + if (bm == 0) + { + /* From (n1 >= d1) /\ (the most significant bit of d1 is set), + conclude (the most significant bit of n1 is set) /\ (the + quotient digit q0 = 0 or 1). + + This special case is necessary, not an optimization. */ + + /* The condition on the next line takes advantage of that + n1 >= d1 (true due to program flow). */ + if (n1 > d1 || n0 >= d0) + { + q0 = 1; + sub_ddmmss (n1, n0, n1, n0, d1, d0); + } + else + q0 = 0; + + q1 = 0; + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + UWtype m1, m0; + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d1 = (d1 << bm) | (d0 >> b); + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q0, n1, n2, n1, d1); + umul_ppmm (m1, m0, q0, d0); + + if (m1 > n1 || (m1 == n1 && m0 > n0)) + { + q0--; + sub_ddmmss (m1, m0, m1, m0, d1, d0); + } + + q1 = 0; + + /* Remainder in (n1n0 - m1m0) >> bm. */ + if (rp != 0) + { + sub_ddmmss (n1, n0, n1, n0, m1, m0); + rr.s.low = (n1 << b) | (n0 >> bm); + rr.s.high = n1 >> bm; + *rp = rr.ll; + } + } + } + } + + ww.s.low = q0; + ww.s.high = q1; + return ww.ll; +} + +#define __negdi2(a) (-(a)) + +long long __divdi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) { + c = ~c; + vv.ll = __negdi2 (vv.ll); + } + w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0); + if (c) + w = __negdi2 (w); + return w; +} + +long long __moddi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) + vv.ll = __negdi2 (vv.ll); + + __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w); + if (c) + w = __negdi2 (w); + return w; +} + +unsigned long long __udivdi3(unsigned long long u, unsigned long long v) +{ + return __udivmoddi4 (u, v, (UDWtype *) 0); +} + +unsigned long long __umoddi3(unsigned long long u, unsigned long long v) +{ + UDWtype w; + + __udivmoddi4 (u, v, &w); + return w; +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashrdi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = u.s.high >> (b - 32); + u.s.high = u.s.high >> 31; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +unsigned long long __lshrdi3(unsigned long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = (unsigned)u.s.high >> (b - 32); + u.s.high = 0; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = (unsigned)u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashldi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.high = (unsigned)u.s.low << (b - 32); + u.s.low = 0; + } else if (b != 0) { + u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b)); + u.s.low = (unsigned)u.s.low << b; + } + return u.ll; +#else + return a << b; +#endif +} + +#ifndef COMMIT_4ad186c5ef61_IS_FIXED +long long __tcc_cvt_ftol(long double x) +{ + unsigned c0, c1; + long long ret; + __asm__ __volatile__ ("fnstcw %0" : "=m" (c0)); + c1 = c0 | 0x0C00; + __asm__ __volatile__ ("fldcw %0" : : "m" (c1)); + __asm__ __volatile__ ("fistpll %0" : "=m" (ret)); + __asm__ __volatile__ ("fldcw %0" : : "m" (c0)); + return ret; +} +#endif + +#endif /* !__x86_64__ */ + +/* XXX: fix tcc's code generator to do this instead */ +float __floatundisf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (float)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (float)r; + } +} + +double __floatundidf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (double)r; + } +} + +long double __floatundixf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (long double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (long double)r; + } +} + +unsigned long long __fixunssfdi (float a1) +{ + register union float_long fl1; + register int exp; + register unsigned long l; + + fl1.f = a1; + + if (fl1.l == 0) + return (0); + + exp = EXP (fl1.l) - EXCESS - 24; + + l = MANT(fl1.l); + if (exp >= 41) + return (unsigned long long)-1; + else if (exp >= 0) + return (unsigned long long)l << exp; + else if (exp >= -23) + return l >> -exp; + else + return 0; +} + +unsigned long long __fixunsdfdi (double a1) +{ + register union double_long dl1; + register int exp; + register unsigned long long l; + + dl1.d = a1; + + if (dl1.ll == 0) + return (0); + + exp = EXPD (dl1) - EXCESSD - 53; + + l = MANTD_LL(dl1); + + if (exp >= 12) + return (unsigned long long)-1; + else if (exp >= 0) + return l << exp; + else if (exp >= -52) + return l >> -exp; + else + return 0; +} + +unsigned long long __fixunsxfdi (long double a1) +{ + register union ldouble_long dl1; + register int exp; + register unsigned long long l; + + dl1.ld = a1; + + if (dl1.l.lower == 0 && dl1.l.upper == 0) + return (0); + + exp = EXPLD (dl1) - EXCESSLD - 64; + + l = dl1.l.lower; + + if (exp > 0) + return (unsigned long long)-1; + else if (exp >= -63) + return l >> -exp; + else + return 0; +} + +long long __fixsfdi (float a1) +{ + long long ret; int s; + ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +long long __fixdfdi (double a1) +{ + long long ret; int s; + ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +long long __fixxfdi (long double a1) +{ + long long ret; int s; + ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +#if defined(TCC_TARGET_X86_64) && !defined(_WIN64) + +#ifndef __TINYC__ +#include +#include +#include +#else +/* Avoid including stdlib.h because it is not easily available when + cross compiling */ +#include /* size_t definition is needed for a x86_64-tcc to parse memset() */ +extern void *malloc(unsigned long long); +extern void *memset(void *s, int c, size_t n); +extern void free(void*); +extern void abort(void); +#endif + +enum __va_arg_type { + __va_gen_reg, __va_float_reg, __va_stack +}; + +//This should be in sync with the declaration on our include/stdarg.h +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +#undef __va_start +#undef __va_arg +#undef __va_copy +#undef __va_end + +void __va_start(__va_list_struct *ap, void *fp) +{ + memset(ap, 0, sizeof(__va_list_struct)); + *ap = *(__va_list_struct *)((char *)fp - 16); + ap->overflow_arg_area = (char *)fp + ap->overflow_offset; + ap->reg_save_area = (char *)fp - 176 - 16; +} + +void *__va_arg(__va_list_struct *ap, + enum __va_arg_type arg_type, + int size, int align) +{ + size = (size + 7) & ~7; + align = (align + 7) & ~7; + switch (arg_type) { + case __va_gen_reg: + if (ap->gp_offset + size <= 48) { + ap->gp_offset += size; + return ap->reg_save_area + ap->gp_offset - size; + } + goto use_overflow_area; + + case __va_float_reg: + if (ap->fp_offset < 128 + 48) { + ap->fp_offset += 16; + return ap->reg_save_area + ap->fp_offset - 16; + } + size = 8; + goto use_overflow_area; + + case __va_stack: + use_overflow_area: + ap->overflow_arg_area += size; + ap->overflow_arg_area = (char*)((intptr_t)(ap->overflow_arg_area + align - 1) & -(intptr_t)align); + return ap->overflow_arg_area - size; + + default: +#ifndef __TINYC__ + fprintf(stderr, "unknown ABI type for __va_arg\n"); +#endif + abort(); + } +} + +#endif /* __x86_64__ */ + +/* Flushing for tccrun */ +#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_I386) + +void __clear_cache(void *beginning, void *end) +{ +} + +#elif defined(TCC_TARGET_ARM) + +#define _GNU_SOURCE +#include +#include +#include + +void __clear_cache(void *beginning, void *end) +{ +/* __ARM_NR_cacheflush is kernel private and should not be used in user space. + * However, there is no ARM asm parser in tcc so we use it for now */ +#if 1 + syscall(__ARM_NR_cacheflush, beginning, end, 0); +#else + __asm__ ("push {r7}\n\t" + "mov r7, #0xf0002\n\t" + "mov r2, #0\n\t" + "swi 0\n\t" + "pop {r7}\n\t" + "ret"); +#endif +} + +#else +#warning __clear_cache not defined for this architecture, avoid using tcc -run +#endif diff --git a/programs/develop/ktcc/trunk/libc/math/log.asm b/programs/develop/ktcc/trunk/libc/math/log.asm new file mode 100644 index 0000000000..964c711268 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/log.asm @@ -0,0 +1,18 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public log_ as "log" + +log_: + +; ln(x) = lg(x)/lg(e). + + fld qword[esp+4] + fld1 + fxch + fyl2x ;Compute 1*lg(x). + fldl2e ;Load lg(e). + fdivp st1, st0 ;Compute lg(x)/lg(e). + ret diff --git a/programs/develop/ktcc/trunk/libc/math/log10.asm b/programs/develop/ktcc/trunk/libc/math/log10.asm new file mode 100644 index 0000000000..8c5c4daa51 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/log10.asm @@ -0,0 +1,18 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public log10_ as "log10" + +log10_: + +; ln(x) = lg(x)/lg(e). + + fld qword[esp+4] + fld1 + fxch + fyl2x ;Compute 1*lg(x). + fldl2t ;Load lg(10). + fdivp st1, st0 ;Compute lg(x)/lg(10). + ret diff --git a/programs/develop/ktcc/trunk/libc/math/pow.c b/programs/develop/ktcc/trunk/libc/math/pow.c new file mode 100644 index 0000000000..c2485f3e2c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/pow.c @@ -0,0 +1,6 @@ +#include + +double pow(double x, double y) +{ + return exp(y * log(x)); +} diff --git a/programs/develop/ktcc/trunk/libc/math/round.c b/programs/develop/ktcc/trunk/libc/math/round.c new file mode 100644 index 0000000000..73b297f6ee --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/round.c @@ -0,0 +1,9 @@ +#include + +double round (double x) +{ + if (x > 0) + return floor(x + 0.5); + else + return ceil(x - 0.5); +} diff --git a/programs/develop/ktcc/trunk/libc/math/roundl.c b/programs/develop/ktcc/trunk/libc/math/roundl.c new file mode 100644 index 0000000000..8ebf5637a4 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/roundl.c @@ -0,0 +1,9 @@ +#include + +long double roundl (long double x) +{ + if (x > 0) + return floor(x + 0.5); + else + return ceil(x - 0.5); +} diff --git a/programs/develop/ktcc/trunk/libc/math/sin.asm b/programs/develop/ktcc/trunk/libc/math/sin.asm new file mode 100644 index 0000000000..97515945cd --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/sin.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public sin_ as "sin" + +sin_: + + fld qword[esp+4] + fsin + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/sinh.c b/programs/develop/ktcc/trunk/libc/math/sinh.c new file mode 100644 index 0000000000..1969267dd2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/sinh.c @@ -0,0 +1,6 @@ +#include + +double sinh (double x) +{ + return (exp(x) - exp(-x)) / 2; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/math/sqrt.asm b/programs/develop/ktcc/trunk/libc/math/sqrt.asm new file mode 100644 index 0000000000..4016991d7f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/sqrt.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public sqrt + +sqrt: + + fld qword[esp+4] + fsqrt + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/sqrtf.asm b/programs/develop/ktcc/trunk/libc/math/sqrtf.asm new file mode 100644 index 0000000000..9306ad4ec1 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/sqrtf.asm @@ -0,0 +1,14 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public sqrtf + +sqrtf: + + fld dword[esp+4] + fsqrt + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/tan.asm b/programs/develop/ktcc/trunk/libc/math/tan.asm new file mode 100644 index 0000000000..2dae8a0312 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/tan.asm @@ -0,0 +1,15 @@ + +format ELF +include '../proc32.inc' +section '.text' executable + +public tan_ as "tan" + +tan_: + + fld qword[esp+4] + fptan + fxch + + ret + diff --git a/programs/develop/ktcc/trunk/libc/math/tanh.c b/programs/develop/ktcc/trunk/libc/math/tanh.c new file mode 100644 index 0000000000..3c3a0190aa --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/math/tanh.c @@ -0,0 +1,8 @@ +#include + +double tanh (double x) +{ + double ex = exp(x), exm = exp(-x); + + return (ex - exm) / (ex + exm); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/memory/memalloc.asm b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm new file mode 100644 index 0000000000..2c45afb329 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm @@ -0,0 +1,46 @@ +format ELF + +;include "../proc32.inc" +section '.text' executable +public sysmalloc +public sysfree +public sysrealloc + +align 4 +sysmalloc: + push ebx + push ecx + mov eax,68 + mov ebx,12 + mov ecx,[esp+12] ;size + int 0x40 + pop ecx + pop ebx + ret 4 + +align 4 +sysfree: + push ebx + push ecx + mov eax,68 + mov ebx,13 + mov ecx,[esp+12] + int 0x40 + pop ecx + pop ebx + ret 4 + +align 4 +sysrealloc: + push ebx + push ecx + push edx + mov eax,68 + mov ebx,20 + mov ecx,[esp+20] ; size + mov edx,[esp+16] ; pointer + int 0x40 + pop edx + pop ecx + pop ebx + ret 8 diff --git a/programs/develop/ktcc/trunk/libc/memory/watermark.c b/programs/develop/ktcc/trunk/libc/memory/watermark.c new file mode 100644 index 0000000000..d91b195553 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/memory/watermark.c @@ -0,0 +1,410 @@ +/* + * Easy and fast memory allocator from + * https://wiki.osdev.org/Memory_Allocation + * Coded by Siemargl, 2018 + * + * No Garbage Collector + */ + +#include +#include +#include +#include +#include + +#define UINT_MAX (4294967295U) + +#ifndef NDEBUG +#include +# ifdef __TINYC__ +# include +# define TRACE1(s, a) { char buf[400]; sprintf(buf, s, a); debug_out_str(buf); } +# define TRACE2(s, a, b) { char buf[400]; sprintf(buf, s, a, b); debug_out_str(buf); } +# else +# define TRACE1(s, a) printf(s, a) +# define TRACE2(s, a, b) printf(s, a, b) +# endif +#else +# define TRACE1(s, a) (void)0 +# define TRACE2(s, a, b) (void)0 +#endif + + + + +// get address, fromwhere function was called +#define CALLEDFROM(param1) (*(int*)((char*)¶m1-4)-5) + +const uint32_t c_used = 0x44455355; //'USED' +const uint32_t c_free = 0x45455246; //'FREE' + +struct hdrfree { + uint32_t mark; // 'FREE' + size_t size; // including header + struct hdrfree *prev; + struct hdrfree *next; +}; + +struct hdrused { + uint32_t mark; // 'USED' + size_t size; +}; + + +static char *__freebase = NULL; // begin of free area +static char *__freetop = NULL; // after last byte of free area +static struct hdrfree *__firstfree = NULL; // ptr to first node in dual-link list + +static struct { + uint32_t malloc_calls; + uint32_t malloc_max; + uint32_t malloc_sum; + + uint32_t sysalloc_calls; + uint32_t sysalloc_max; + uint32_t sysalloc_sum; + + uint32_t crtfreeblocks; // number of free blocks, checking corruptions + uint32_t freeblocks_sum; +} wtalloc_stat; + + +void *wtmalloc(size_t sz) +{ + struct hdrfree *fndnode, *newnode; + sz = (sizeof(struct hdrused) + sz + 15) & ~15; // align 16bytes +//TRACE1("_call alloc(%d)\n", sz); + + //statistics + wtalloc_stat.malloc_calls++; + if (sz > wtalloc_stat.malloc_max) wtalloc_stat.malloc_max = sz; + wtalloc_stat.malloc_sum += sz; + + // try to find free block enough size + fndnode = __firstfree; + while(fndnode) + { +#ifndef NDEBUG + if (fndnode->mark != c_free) + { + TRACE2("heap free block list corrupt %x EIP@%x\n", fndnode, CALLEDFROM(sz)); + assert(0); + } +#endif + if (fndnode->size >= sz) break; + fndnode = fndnode->next; + } + + if (fndnode) // found free block + { + if (fndnode->size - sz > 15) // split smaller size, move free node + { +//TRACE2("alloc(%d) split (%x)\n", sz, fndnode); + wtalloc_stat.freeblocks_sum -= sz; + newnode = (struct hdrfree*)((char*)fndnode + sz); + newnode->mark = c_free; + newnode->size = fndnode->size - sz; + newnode->next = fndnode->next; + newnode->prev = fndnode->prev; + + if (fndnode->next) + fndnode->next->prev = newnode; + + //перед может быть РЅРµ РЅРѕРґР°, Р° 1Р№ указатель + if (fndnode->prev) + newnode->prev->next = newnode; + else + __firstfree = newnode; + } else // nothing to split, just exclude + { +//TRACE1("alloc(%d) remove freenode\n", sz); + + wtalloc_stat.crtfreeblocks--; + wtalloc_stat.freeblocks_sum -= fndnode->size; + if (fndnode->next) + fndnode->next->prev = fndnode->prev; + //перед может быть РЅРµ РЅРѕРґР°, Р° 1Р№ указатель + if (fndnode->prev) + fndnode->prev->next = fndnode->next; + else + __firstfree = fndnode->next; + } + + fndnode->mark = c_used; + fndnode->size = sz; + return (char*)fndnode + sizeof(struct hdrused); + } + + char *ptr; + // free block not found, try to add @end + if (__freetop - __freebase < sz) // not enough memory - call system + { + if (sz > UINT_MAX - 16) return NULL; // check 32-bit heap overflow +// size_t new_heap_size = (__freetop - __freebase + sz + 4095) & ~4095; + size_t new_heap_size = (sz + sz / 5 + 4095) & ~4095; // 20% reserved + + //statistics + wtalloc_stat.sysalloc_calls++; + if (new_heap_size > wtalloc_stat.malloc_max) wtalloc_stat.sysalloc_max = new_heap_size; + wtalloc_stat.sysalloc_sum += new_heap_size; + + + //С…РІРѕСЃС‚ сунуть РІ свободные, Р° фритоп Рё базу перености РЅР° новый РєСѓСЃРѕРє + ptr = sysmalloc(new_heap_size); // rounded 4k +//TRACE2("call systemalloc(%d) returned %x\n", new_heap_size, ptr); + if (!ptr) + { + TRACE2("sysmalloc failed trying to allocate %u bytes EIP@%x\n", sz, CALLEDFROM(sz)); + return NULL; + } + // add new free block in front of list + if (__freetop - __freebase > 15) + { + newnode = (struct hdrfree*)__freebase; + newnode->mark = c_free; + newnode->size = __freetop - __freebase; + newnode->next = __firstfree; + newnode->prev = NULL; + if (__firstfree) + __firstfree->prev = newnode; + __firstfree = newnode; + wtalloc_stat.crtfreeblocks++; + wtalloc_stat.freeblocks_sum += newnode->size; +//TRACE2("alloc(%d) add tail %d to freenode", sz, newnode->size); +//TRACE1(".tail [%x]\n", newnode); + } + // we don't save allocated block from system, so cant free them ltr + + __freebase = ptr; + __freetop = __freebase + new_heap_size; + } + + ptr = __freebase + sizeof(struct hdrused); + ((struct hdrused*)__freebase)->mark = c_used; + ((struct hdrused*)__freebase)->size = sz; + __freebase += sz; +//TRACE1("__freebase [%x]\n", __freebase); + + +// check list availability +/* +int maxfree = 0; +for (fndnode = __firstfree; fndnode; fndnode = fndnode->next) +{ + if (fndnode->size > maxfree) maxfree = fndnode->size; +} + +TRACE2("alloc(%d) from freebase, maxfree = %d,", sz, maxfree); +TRACE1(" freelist len = %u \n", wtalloc_stat.crtfreeblocks); +*/ + return ptr; +} + +void wtfree(void *ptr) +{ + if (!ptr) return; + +//TRACE1("free() to freenode, sized %d\n", ((struct hdrused*)((char*)ptr - 8))->size); + +#ifndef NDEBUG + if (((struct hdrused*)((char*)ptr - 8))->mark != c_used) + { + TRACE2("try free unallocated block ptr = %x bytes EIP@%x\n", ptr, CALLEDFROM(ptr)); + assert(0); + } +#endif + struct hdrfree *newnode = (struct hdrfree*)((char*)ptr - 8); + newnode->mark = c_free; + //size stays + newnode->next = NULL; + newnode->prev = NULL; + + + // experimental - try to merge, if adjanced from bottom is also freeblock + int reorganized = 0; + struct hdrfree *higher; + { + struct hdrfree *p1; + higher = NULL; + for (p1 = __firstfree; p1; p1 = p1->next) + { + higher = (struct hdrfree *)((char*)p1 + p1->size); + if (higher == newnode) break; + } + if (p1) // yes, it is + { + wtalloc_stat.freeblocks_sum += newnode->size; + p1->size += newnode->size; + // p1->prev, p1->next already OK + newnode->mark = 0; // for safety + newnode = p1; // continue optimization +//TRACE2("free block merged w/bottom sized %u bytes, list len %u\n", p1->size, wtalloc_stat.crtfreeblocks); + reorganized = 1; + } + } + + +/* removed, as very seldom succeeds */ + // experimental - try to merge, if adjanced from top is also freeblock + higher = (struct hdrfree *)((char*)newnode + newnode->size); +// dont work - we try to read after our memory +// if ((char*)higher < (char*)__freetop && // saves from reading out of our memory +// higher->mark == c_free) // only suspisious, must be in list + { + struct hdrfree *p1; + for (p1 = __firstfree; p1 && p1 != higher; p1 = p1->next); + if (p1) // yes, it is + { + if (newnode->next || newnode->prev) // optimized 1st stage, must remove from list and readd later + { + wtalloc_stat.crtfreeblocks--; + wtalloc_stat.freeblocks_sum -= newnode->size; + if (newnode->next) + newnode->next->prev = newnode->prev; + if (newnode->prev) + newnode->prev->next = newnode->next; + else + __firstfree = newnode->next; + } + wtalloc_stat.freeblocks_sum += newnode->size; + newnode->size += higher->size; + newnode->prev = higher->prev; + newnode->next = higher->next; + higher->mark = 0; // for safety + if (higher->next) + higher->next->prev = newnode; + if (higher->prev) + higher->prev->next = newnode; + else + __firstfree = newnode; +//TRACE1("free block merged w/top\n", 0); + reorganized = 1; + } + } + + if (reorganized) return; // experimental reorganized do all work + +//TRACE1("free block added\n", 0); + wtalloc_stat.crtfreeblocks++; + wtalloc_stat.freeblocks_sum += newnode->size; + + newnode->next = __firstfree; + newnode->prev = NULL; + if (__firstfree) + __firstfree->prev = newnode; + __firstfree = newnode; +} + + +void *wtrealloc(void *ptr, size_t sz) +{ + if (!ptr) return wtmalloc(sz); + + struct hdrused* oldptr = (struct hdrused*)((char*)ptr - 8); + +#ifndef NDEBUG + if (oldptr->mark != c_used) + { + TRACE2("try realloc unallocated block ptr = %x EIP@%x\n", ptr, CALLEDFROM(ptr)); + assert(0); + } +#endif + + if (oldptr->size - 8 >= sz) return ptr; // enough room in this block, ex from freelist + + /* experimental growth last block */ + int growth = (oldptr->size + sz + 15) & ~15; + if ((char*)oldptr + oldptr->size == __freebase && + __freetop - __freebase + oldptr->size >= growth ) // we at top, can grow up + { + wtalloc_stat.malloc_sum += growth - oldptr->size; + __freebase += growth - oldptr->size; + oldptr->size = growth; + return ptr; + } + + void *newptr = wtmalloc(sz); + if (newptr) + { + memcpy(newptr, (char*)oldptr +8, oldptr->size -8); // why forgeting -8 dont fail test?!? + wtfree((char*)oldptr +8); + return newptr; + } + + return NULL; +} + +void* wtcalloc( size_t num, size_t size ) +{ + void *newptr = wtmalloc(num * size); + if (newptr) + memset(newptr, 0, num * size); + return newptr; +} + + + + +int wtmalloc_freelist_check() +//контроль целостности СЃРїРёСЃРєР° фри OK == 1 +{ + int cnt = 0; + struct hdrfree *ptr = __firstfree; + + if(ptr && ptr->prev) + { + TRACE1("allocated memory freelist 1st block fail, ptr = %x\n", ptr); + return 0; + } + + for(;ptr; ptr = ptr->next) + { +//TRACE1("(%x)", ptr); + + cnt++; + if (ptr->mark != c_free) + { + TRACE1("allocated memory freelist check fail, ptr = %x\n", ptr); + return 0; + } + } + if (cnt != wtalloc_stat.crtfreeblocks) + { + TRACE2("allocated memory freelist check fail, length must be = %u but is %u\n", wtalloc_stat.crtfreeblocks, cnt); + return 0; + } + return 1; +} + +void wtmalloc_freelist_print() +{ + struct hdrfree *ptr = __firstfree; + for(;ptr; ptr = ptr->next) + { + TRACE2("(%x[%u])", ptr, ptr->size); + } + TRACE1("\n", 0); + +} + +int wtmalloc_poiner_check(void *ptr) +//контроль указателя - mark OK == 1 +{ + if (((struct hdrused*)((char*)ptr - 8))->mark != c_used) + { + TRACE2("pointer watermark check fail ptr = %x bytes EIP@%x\n", ptr, CALLEDFROM(ptr)); + return 0; + } + return 1; +} + +void wtdump_alloc_stats() +{ + TRACE1("----Watermark allocator stats:----\n", 0); + TRACE2("allocated %u calls, max of %u bytes\n", wtalloc_stat.malloc_calls, wtalloc_stat.malloc_max); + TRACE2("total %u bytes, average call %u bytes\n", wtalloc_stat.malloc_sum, wtalloc_stat.malloc_sum / wtalloc_stat.malloc_calls); + TRACE1("SYSTEM:\n", 0); + TRACE2("allocated %u calls, max of %u bytes\n", wtalloc_stat.sysalloc_calls, wtalloc_stat.sysalloc_max); + TRACE2("total %u bytes, average call %u bytes\n", wtalloc_stat.sysalloc_sum, wtalloc_stat.sysalloc_sum / wtalloc_stat.sysalloc_calls); + TRACE2("free list %u bytes, length %u chunks\n", wtalloc_stat.freeblocks_sum, wtalloc_stat.crtfreeblocks); +} diff --git a/programs/develop/ktcc/trunk/libc/net/socket.c b/programs/develop/ktcc/trunk/libc/net/socket.c new file mode 100644 index 0000000000..15c2159fb5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/net/socket.c @@ -0,0 +1,103 @@ +/* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 */ + +#include + +int socket(int domain, int type, int protocol) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(0), "c"(domain), "d"(type), "S"(protocol) + ); +} + +int close(int socket) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(1), "c"(socket) + ); +} +int bind(int socket, const struct sockaddr *addres, int addres_len) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(2), "c"(socket), "d"(addres), "S"(addres_len) + ); +} + +int listen(int socket, int backlog) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(3), "c"(socket), "d"(backlog) + ); +} + +int connect(int socket,const struct sockaddr* address, int socket_len) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(4), "c"(socket), "d"(address), "S"(socket_len) + ); +} + +int accept(int socket, const struct sockaddr *address, int address_len) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(5), "c"(socket), "d"(address), "S"(address_len) + ); +} + +int send(int socket, const void *message, size_t msg_len, int flag) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(6), "c"(socket), "d"(message), "S"(msg_len), "D"(flag) + ); +} + +int recv(int socket, void *buffer, size_t buff_len, int flag) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(7), "c"(socket), "d"(buffer), "S"(buff_len), "D"(flag) + ); +} + +int setsockopt(int socket,const optstruct* opt) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(8), "c"(socket),"d"(opt) + ); +} + +int getsockopt(int socket, optstruct* opt) +{ + asm volatile( + "int $0x40" + :"=b"(errno) + :"a"(75), "b"(9), "c"(socket),"d"(opt) + ); +} + +int socketpair(int *sock1, int *sock2) +{ + asm volatile( + "int $0x40" + :"=b"(*sock2), "=a"(*sock1) + :"a"(75), "b"(10) + ); + errno = *sock2; + return *sock1; +} diff --git a/programs/develop/ktcc/trunk/libc/proc32.inc b/programs/develop/ktcc/trunk/libc/proc32.inc new file mode 100644 index 0000000000..762826cbbb --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/proc32.inc @@ -0,0 +1,301 @@ + +; Macroinstructions for defining and calling procedures + +macro stdcall proc,[arg] ; directly call STDCALL procedure + { common + if ~ arg eq + reverse + pushd arg + common + end if + call proc } + +macro invoke proc,[arg] ; indirectly call STDCALL procedure + { common + if ~ arg eq + reverse + pushd arg + common + end if + call [proc] } + +macro ccall proc,[arg] ; directly call CDECL procedure + { common + size@ccall = 0 + if ~ arg eq + reverse + pushd arg + size@ccall = size@ccall+4 + common + end if + call proc + if size@ccall + add esp,size@ccall + end if } + +macro cinvoke proc,[arg] ; indirectly call CDECL procedure + { common + size@ccall = 0 + if ~ arg eq + reverse + pushd arg + size@ccall = size@ccall+4 + common + end if + call [proc] + if size@ccall + add esp,size@ccall + end if } + +macro proc [args] ; define procedure + { common + match name params, args> + \{ define@proc name, \{ prologue name,flag,parmbytes,localbytes,reglist \} + virtual at parmbase@proc + match =,args, params \{ defargs@proc args \} + match =args@proc args, args@proc params \{ defargs@proc args \} + parmbytes = $-(parmbase@proc) + end virtual + name # % = parmbytes/4 + all@vars equ + current = 0 + macro locals + \{ virtual at localbase@proc+current + macro label def \\{ match . type,def> \\\{ deflocal@proc .,label, \\\{ epilogue name,flag,parmbytes,localbytes,reglist \\\} \\} \} + macro finish@proc + \{ localbytes = current + match close:reglist, close@proc: \\{ close name,flag,parmbytes,localbytes,reglist \\} + end if \} } + +macro defargs@proc [arg] + { common + if ~ arg eq + forward + local ..arg,current@arg + match argname:type, arg + \{ current@arg equ argname + label ..arg type + argname equ ..arg + if qqword eq type + dd ?,?,?,?,?,?,?,? + else if dqword eq type + dd ?,?,?,? + else if tbyte eq type + dd ?,?,? + else if qword eq type | pword eq type + dd ?,? + else + dd ? + end if \} + match =current@arg,current@arg + \{ current@arg equ arg + arg equ ..arg + ..arg dd ? \} + common + args@proc equ current@arg + forward + restore current@arg + common + end if } + +macro deflocal@proc name,def,[val] { name def val } + +macro deflocal@proc name,def,[val] + { common + match vars, all@vars \{ all@vars equ all@vars, \} + all@vars equ all@vars name + forward + local ..var,..tmp + ..var def val + match =?, val \{ ..tmp equ \} + match any =?, val \{ ..tmp equ \} + match any (=?), val \{ ..tmp equ \} + match =label, def \{ ..tmp equ \} + match tmp : value, ..tmp : val + \{ tmp: end virtual + initlocal@proc ..var,def value + virtual at tmp\} + common + match first rest, ..var, \{ name equ first \} } + +struc label type { label . type } + +macro initlocal@proc name,def + { virtual at name + def + size@initlocal = $ - name + end virtual + position@initlocal = 0 + while size@initlocal > position@initlocal + virtual at name + def + if size@initlocal - position@initlocal < 2 + current@initlocal = 1 + load byte@initlocal byte from name+position@initlocal + else if size@initlocal - position@initlocal < 4 + current@initlocal = 2 + load word@initlocal word from name+position@initlocal + else + current@initlocal = 4 + load dword@initlocal dword from name+position@initlocal + end if + end virtual + if current@initlocal = 1 + mov byte [name+position@initlocal],byte@initlocal + else if current@initlocal = 2 + mov word [name+position@initlocal],word@initlocal + else + mov dword [name+position@initlocal],dword@initlocal + end if + position@initlocal = position@initlocal + current@initlocal + end while } + +macro endp + { purge ret,locals,endl + finish@proc + purge finish@proc + restore regs@proc + match all,args@proc \{ restore all \} + restore args@proc + match all,all@vars \{ restore all \} } + +macro local [var] + { common + locals + forward done@local equ + match varname[count]:vartype, var + \{ match =BYTE, vartype \\{ varname rb count + restore done@local \\} + match =WORD, vartype \\{ varname rw count + restore done@local \\} + match =DWORD, vartype \\{ varname rd count + restore done@local \\} + match =PWORD, vartype \\{ varname rp count + restore done@local \\} + match =QWORD, vartype \\{ varname rq count + restore done@local \\} + match =TBYTE, vartype \\{ varname rt count + restore done@local \\} + match =DQWORD, vartype \\{ label varname dqword + rq count*2 + restore done@local \\} + match =QQWORD, vartype \\{ label varname qqword + rq count*4 + restore done@local \\} + match =XWORD, vartype \\{ label varname xword + rq count*2 + restore done@local \\} + match =YWORD, vartype \\{ label varname yword + rq count*4 + restore done@local \\} + match , done@local \\{ virtual + varname vartype + end virtual + rb count*sizeof.\#vartype + restore done@local \\} \} + match :varname:vartype, done@local:var + \{ match =BYTE, vartype \\{ varname db ? + restore done@local \\} + match =WORD, vartype \\{ varname dw ? + restore done@local \\} + match =DWORD, vartype \\{ varname dd ? + restore done@local \\} + match =PWORD, vartype \\{ varname dp ? + restore done@local \\} + match =QWORD, vartype \\{ varname dq ? + restore done@local \\} + match =TBYTE, vartype \\{ varname dt ? + restore done@local \\} + match =DQWORD, vartype \\{ label varname dqword + dq ?,? + restore done@local \\} + match =QQWORD, vartype \\{ label varname qqword + dq ?,?,?,? + restore done@local \\} + match =XWORD, vartype \\{ label varname xword + dq ?,? + restore done@local \\} + match =YWORD, vartype \\{ label varname yword + dq ?,?,?,? + restore done@local \\} + match , done@local \\{ varname vartype + restore done@local \\} \} + match ,done@local + \{ var + restore done@local \} + common + endl } diff --git a/programs/develop/ktcc/trunk/libc/public_stdcall.inc b/programs/develop/ktcc/trunk/libc/public_stdcall.inc new file mode 100644 index 0000000000..83bd601c14 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/public_stdcall.inc @@ -0,0 +1,7 @@ +macro public_stdcall name,size +{ + public name + public name#@#size + name: + name#@#size: +} diff --git a/programs/develop/ktcc/trunk/libc/start/start.asm b/programs/develop/ktcc/trunk/libc/start/start.asm new file mode 100644 index 0000000000..33cfee0375 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/start/start.asm @@ -0,0 +1,220 @@ +format ELF +section '.text' executable +public start +public start as '_start' +;extrn mf_init +extrn main +;include 'debug2.inc' +include '..\..\..\..\..\proc32.inc' +include '..\..\..\..\..\macros.inc' +include '..\..\..\..\..\dll.inc' +__DEBUG__=0 + +;start_: +virtual at 0 + db 'MENUET01' ; 1. Magic number (8 bytes) + dd 0x01 ; 2. Version of executable file + dd start ; 3. Start address +imgsz dd 0x0 ; 4. Size of image + dd 0x100000 ; 5. Size of needed memory + dd 0x100000 ; 6. Pointer to stack +hparams dd 0x0 ; 7. Pointer to program arguments +hpath dd 0x0 ; 8. Pointer to program path +end virtual + +start: +;DEBUGF 'Start programm\n' + ;init heap of memory + mov eax,68 + mov ebx,11 + int 0x40 + + mov [argc], 0 + mov eax, [hparams] + test eax, eax + jz .without_path + mov eax, path + cmp word ptr eax, 32fh ; '/#3' UTF8 + jne .without_path + mov word ptr eax, 12fh ; '/#1' fix to CP866 +.without_path: + mov esi, eax + call push_param +; retrieving parameters + mov esi, params + xor edx, edx ; dl - идёт параметр(1) или разделители(0) + ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) + mov ecx, 1 ; cl = 1 + ; ch = 0 просто ноль +.parse: + lodsb + test al, al + jz .run + test dl, dl + jnz .findendparam + ;{если был разделитель + cmp al, ' ' + jz .parse ;загружен пробел, грузим следующий символ + mov dl, cl ;начинается параметр + cmp al, '"' + jz @f ;загружены кавычки + mov dh, ch ;параметр без кавычек + dec esi + call push_param + inc esi + jmp .parse + + @@: + mov dh, cl ;параметр в кавычеках + call push_param ;если не пробел значит начинается какой то параметр + jmp .parse ;если был разделитель} + +.findendparam: + test dh, dh + jz @f ; без кавычек + cmp al, '"' + jz .clear + jmp .parse + @@: + cmp al, ' ' + jnz .parse + +.clear: + lea ebx, [esi - 1] + mov [ebx], ch + mov dl, ch + jmp .parse + +.run: + call load_imports + push argv + push [argc] + call main +.exit: + xor eax,eax + dec eax + int 0x40 + dd -1 +.crash: + jmp .exit +;============================ +push_param: +;============================ +;parameters +; esi - pointer +;description +; procedure increase argc +; and add pointer to array argv +; procedure changes ebx + mov ebx, [argc] + cmp ebx, max_parameters + jae .dont_add + mov [argv+4*ebx], esi + inc [argc] +.dont_add: + ret +;============================== + +;============================== +load_imports: +;============================== +;parameters +; none +;description +; imports must be located at end of image (but before BSS sections) +; the address of end of imports (next byte after imports) is located in imgsz +; look at each import from that address up to illegal import +; legal import is such that: +; first pointer points to procedure name +; and is smaller than imgsz +; second pointer points lo library name, starting with 0x55, 0xAA +; and is smaller than imgsz +; each library should be initialized as appropriate, once +; so as library is initialized, its name will be replaced 0x00 + mov ebx, [imgsz] ; byte after imports +.handle_next_import: + sub ebx, 4 ; ebx = pointer to pointer to library name + mov esi, dword[ebx] ; esi = pointer to library name + push ebx + push esi + call load_library ; eax = pointer to library exports + pop esi + pop ebx + test eax, eax + jz .done + sub ebx, 4 ; ebx = pointer to pointer to symbol name + push ebx + stdcall dll.GetProcAddress, eax, dword[ebx] + pop ebx + test eax, eax + jz .fail + mov dword[ebx], eax + jmp .handle_next_import +.done: + ret +.fail: + ret +;============================== + +;============================== +load_library: +;============================== +;parameters +; ebx: library name address +;description +; each library should be initialized as appropriate, once +; so as library is initialized, its name will be replaced 0x00 +; and 4 next bytes will be set to address of library + ; first two bytes of library name must be 0x55, 0xAA (is like a magic) + cld ; move esi further, not back + cmp esi, [imgsz] + ja .fail + lodsb ; al = first byte of library name + cmp al, 0x55 + jne .fail + lodsb ; al = second byte of library name + cmp al, 0xAA + jne .fail + lodsb ; al = third byte of library name (0x00 if the library is already loaded) + test al, al + jnz .load + lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes + ; now eax contains address of library + ret +.load: + dec esi ; we checked on 0 before, let's go back + mov eax, 68 + mov ebx, 19 + mov ecx, esi + int 0x40 ; eax = address of exports + mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name + mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library + ; call lib_init + stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init + test eax, eax + jz .ret + stdcall dll.Init, eax +.ret: + mov eax, [esi + 1] ; put address of library into eax + ret +.fail: + mov eax, 0 + ret +;============================== + +lib_init_str db 'lib_init', 0 + +public argc as '__argc' +public params as '__argv' +public path as '__path' + +section '.bss' +buf_len = 0x400 +max_parameters=0x20 +argc rd 1 +argv rd max_parameters +path rb buf_len +params rb buf_len + +;section '.data' +;include_debug_strings ; ALWAYS present in data section diff --git a/programs/develop/ktcc/trunk/libc/start/start.old.asm b/programs/develop/ktcc/trunk/libc/start/start.old.asm new file mode 100644 index 0000000000..920aa984db --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/start/start.old.asm @@ -0,0 +1,143 @@ +format ELF +section '.text' executable +public start +public start as '_start' +;extrn mf_init +extrn main +;include 'debug2.inc' +__DEBUG__=0 + +;start_: +virtual at 0 + db 'MENUET01' ; 1. Magic number (8 bytes) + dd 0x01 ; 2. Version of executable file + dd start ; 3. Start address + dd 0x0 ; 4. Size of image + dd 0x100000 ; 5. Size of needed memory + dd 0x100000 ; 6. Pointer to stack +hparams dd 0x0 ; 7. Pointer to program arguments +hpath dd 0x0 ; 8. Pointer to program path +end virtual + +start: +;DEBUGF 'Start programm\n' + ;init heap of memory + mov eax,68 + mov ebx,11 + int 0x40 + +;DEBUGF ' path "%s"\n params "%s"\n', .path, .params +; check for overflow +;; that not work +; mov al, [path+buf_len-1] +; or al, [params+buf_len-1] +; jnz .crash +; check if path written by OS + mov [argc], 0 + mov eax, [hparams] + test eax, eax + jz .without_path + mov eax, path + cmp word ptr eax, 32fh ; '/#3' UTF8 + jne .without_path + mov word ptr eax, 12fh ; '/#1' fix to CP866 +.without_path: + mov esi, eax + call push_param +; retrieving parameters + mov esi, params + xor edx, edx ; dl - идёт параметр(1) или разделители(0) + ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) + mov ecx, 1 ; cl = 1 + ; ch = 0 просто ноль +.parse: + lodsb + test al, al + jz .run + test dl, dl + jnz .findendparam + ;{если был разделитель + cmp al, ' ' + jz .parse ;загружен пробел, грузим следующий символ + mov dl, cl ;начинается параметр + cmp al, '"' + jz @f ;загружены кавычки + mov dh, ch ;параметр без кавычек + dec esi + call push_param + inc esi + jmp .parse + + @@: + mov dh, cl ;параметр в кавычеках + call push_param ;если не пробел значит начинается какой то параметр + jmp .parse ;если был разделитель} + +.findendparam: + test dh, dh + jz @f ; без кавычек + cmp al, '"' + jz .clear + jmp .parse + @@: + cmp al, ' ' + jnz .parse + +.clear: + lea ebx, [esi - 1] + mov [ebx], ch + mov dl, ch + jmp .parse + +.run: +;DEBUGF 'call main(%x, %x) with params:\n', [argc], argv +if __DEBUG__ = 1 + mov ecx, [argc] + @@: + lea esi, [ecx * 4 + argv-4] + DEBUGF '0x%x) "%s"\n', cx, [esi] + loop @b +end if + push argv + push [argc] + call main +.exit: +;DEBUGF 'Exit from prog\n'; + xor eax,eax + dec eax + int 0x40 + dd -1 +.crash: +;DEBUGF 'E:buffer overflowed\n' + jmp .exit +;============================ +push_param: +;============================ +;parameters +; esi - pointer +;description +; procedure increase argc +; and add pointer to array argv +; procedure changes ebx + mov ebx, [argc] + cmp ebx, max_parameters + jae .dont_add + mov [argv+4*ebx], esi + inc [argc] +.dont_add: + ret +;============================== +public argc as '__argc' +public params as '__argv' +public path as '__path' + +section '.bss' +buf_len = 0x400 +max_parameters=0x20 +argc rd 1 +argv rd max_parameters +path rb buf_len +params rb buf_len + +;section '.data' +;include_debug_strings ; ALWAYS present in data section diff --git a/programs/develop/ktcc/trunk/libc/stdio/clearerr.c b/programs/develop/ktcc/trunk/libc/stdio/clearerr.c new file mode 100644 index 0000000000..7f0e8a0106 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/clearerr.c @@ -0,0 +1,22 @@ +#include +#include + +void clearerr ( FILE * stream ) +{ + errno = 0; +} + +int ferror ( FILE * stream ) +{ + return errno; +} + +void perror ( const char * str ) +{ + char *msg = strerror(errno); + + if (str) + fprintf(stderr, "%s:%s\n", str, msg); + else + fprintf(stderr, "%s\n", msg); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/conio.c b/programs/develop/ktcc/trunk/libc/stdio/conio.c new file mode 100644 index 0000000000..a91ca92668 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/conio.c @@ -0,0 +1,102 @@ +#include +#include + +char* con_caption = "Console app"; +extern int __argc; +extern char** __argv; +extern char* __path; +dword *con_dll_ver; +int __console_initdll_status; + +char* con_dllname="/sys/lib/console.obj"; + +struct import{ + char *name; + void *data; +}; + +void stdcall (*con_init)(dword wnd_width, dword wnd_height, + dword scr_width, dword scr_height, const char* title); +void stdcall (*con_exit)(int bCloseWindow); +void stdcall (*con_set_title)(const char* title); +void stdcall (*con_write_asciiz)(const char* str); +void stdcall (*con_write_string)(const char* str, dword length); +int cdecl (*con_printf)(const char* format, ...); +dword stdcall (*con_get_flags)(void); +dword stdcall (*con_set_flags)(dword new_flags); +int stdcall (*con_get_font_height)(void); +int stdcall (*con_get_cursor_height)(void); +int stdcall (*con_set_cursor_height)(int new_height); +int stdcall (*con_getch)(void); +word stdcall (*con_getch2)(void); +int stdcall (*con_kbhit)(void); +char* stdcall (*con_gets)(char* str, int n); +char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n); +void stdcall (*con_cls)(); +void stdcall (*con_get_cursor_pos)(int* px, int* py); +void stdcall (*con_set_cursor_pos)(int x, int y); + + +// don't change order in this! linked by index +char* con_imports[] = { + "START", "version", "con_init", "con_write_asciiz", "con_write_string", + "con_printf", "con_exit", "con_get_flags", "con_set_flags", "con_kbhit", + "con_getch", "con_getch2", "con_gets", "con_gets2", "con_get_font_height", + "con_get_cursor_height", "con_set_cursor_height", "con_cls", + "con_get_cursor_pos", "con_set_cursor_pos", "con_set_title", + (char*)0 +}; + +void con_lib_link(struct import *exp, char** imports){ + + con_dll_ver = _ksys_cofflib_getproc(exp, imports[1]); + con_init = _ksys_cofflib_getproc(exp, imports[2]); + con_write_asciiz = _ksys_cofflib_getproc(exp, imports[3]); + con_write_string = _ksys_cofflib_getproc(exp, imports[4]); + con_printf = _ksys_cofflib_getproc(exp, imports[5]); + con_exit = _ksys_cofflib_getproc(exp, imports[6]); + con_get_flags = _ksys_cofflib_getproc(exp, imports[7]); + con_set_flags = _ksys_cofflib_getproc(exp, imports[8]); + con_kbhit = _ksys_cofflib_getproc(exp, imports[9]); + con_getch = _ksys_cofflib_getproc(exp, imports[10]); + con_getch2 = _ksys_cofflib_getproc(exp, imports[11]); + con_gets = _ksys_cofflib_getproc(exp, imports[12]); + con_gets2 = _ksys_cofflib_getproc(exp, imports[13]); + con_get_font_height = _ksys_cofflib_getproc(exp, imports[14]); + con_get_cursor_height=_ksys_cofflib_getproc(exp, imports[15]); + con_set_cursor_height=_ksys_cofflib_getproc(exp, imports[16]); + con_cls = _ksys_cofflib_getproc(exp, imports[17]); + con_get_cursor_pos = _ksys_cofflib_getproc(exp, imports[18]); + con_set_cursor_pos = _ksys_cofflib_getproc(exp, imports[19]); + con_set_title = _ksys_cofflib_getproc(exp, imports[20]); +} + + +int con_init_console_dll(void) +{ + return con_init_console_dll_param(-1, -1, -1, -1, con_caption); +} + + +int con_init_console_dll_param(dword wnd_width, dword wnd_height, + dword scr_width, dword scr_height, const char* title) +/* work as con_init_console_dll, but call con_init with params +*/ +{ + struct import * hDll; + + if (__console_initdll_status == 1) return 0; + + if((hDll = (struct import *)_ksys_cofflib_load(con_dllname)) == 0){ + debug_out_str("can't load lib\n"); + return 1; + } + con_lib_link(hDll, con_imports); + + con_init(wnd_width, wnd_height, scr_width, scr_height, title); + + __console_initdll_status = 1; + + return 0; +} + diff --git a/programs/develop/ktcc/trunk/libc/stdio/debug.c b/programs/develop/ktcc/trunk/libc/stdio/debug.c new file mode 100644 index 0000000000..be8d62514d --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/debug.c @@ -0,0 +1,13 @@ +#include +#include +#include + +void debug_printf(const char *format,...) +{ + va_list ap; + char log_board[300]; + va_start (ap, format); + vsnprintf(log_board, sizeof log_board, format, ap); + va_end(ap); + debug_out_str(log_board); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fclose.c b/programs/develop/ktcc/trunk/libc/stdio/fclose.c new file mode 100644 index 0000000000..2475ba0329 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fclose.c @@ -0,0 +1,20 @@ +#include +#include +#include + +int fclose(FILE* file) +{ + if(!file) + { + errno = E_INVALIDPTR; + return EOF; + } + + if(file->buffer) + free(file->buffer); + if(file->filename) + free(file->filename); + free(file); + + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/feof.c b/programs/develop/ktcc/trunk/libc/stdio/feof.c new file mode 100644 index 0000000000..f74639ddc3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/feof.c @@ -0,0 +1,11 @@ +#include +int feof(FILE* file) +{ + if(!file) + { + errno = E_INVALIDPTR; + return EOF; + } + + return file->filepos>=file->filesize; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fflush.c b/programs/develop/ktcc/trunk/libc/stdio/fflush.c new file mode 100644 index 0000000000..24f8d7d2fa --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fflush.c @@ -0,0 +1,9 @@ +#include +int fflush(FILE* file) +// file can be zero, as flush all +{ + if (file && (file->mode & 3)==FILE_OPEN_READ) + return 0; + + return(0); // always good, as no write buffering +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fgetc.c b/programs/develop/ktcc/trunk/libc/stdio/fgetc.c new file mode 100644 index 0000000000..01299755db --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fgetc.c @@ -0,0 +1,11 @@ +#include +int fgetc(FILE* file) +{ + int c = 0, rc; + + rc = fread(&c, 1, 1, file); + + if (rc < 1) return EOF; + + return c; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fgetpos.c b/programs/develop/ktcc/trunk/libc/stdio/fgetpos.c new file mode 100644 index 0000000000..d71d203230 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fgetpos.c @@ -0,0 +1,13 @@ +#include +int fgetpos(FILE* file,fpos_t* pos) +{ + if(!file || !pos) + { + errno = E_INVALIDPTR; + return EOF; + } + + *pos=file->filepos; + + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fgets.c b/programs/develop/ktcc/trunk/libc/stdio/fgets.c new file mode 100644 index 0000000000..1fca699929 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fgets.c @@ -0,0 +1,34 @@ +#include + +char * fgets ( char * str, int num, FILE * file ) +// need to ignore \r\n in text mode +{ + int rd = 0; + char c; + + if(!file || !str) + { + errno = E_INVALIDPTR; + return NULL; + } + + + while (rd < num - 1) + { + c = fgetc(file); + if (EOF == c) break; + if ('\n' == c) + { + str[rd++] = c; + break; + } + else + str[rd++] = c; + } + if (0 == rd) return NULL; + else + { + str[rd] = '\0'; + return str; + } +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fopen.c b/programs/develop/ktcc/trunk/libc/stdio/fopen.c new file mode 100644 index 0000000000..ef78d97a28 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fopen.c @@ -0,0 +1,142 @@ +#include +#include +#include + +int errno = 0; + +/* +// removed by Seiemargl 26-oct-2018 +// use get_current_folder() from kos32sys.h instead + + +extern char __argv; +extern char __path; + +// convert relative to program path ./file.txt to absolute +const char* getfullpath(const char *path){ + + + int relpath_pos, localpath_size; + char *programpath; + char *newpath; + char *prgname; + + if (path[0] == '/') // + { + return(strdup(path)); // dup need as free in fclose() + } + + relpath_pos = 0; + if (path[0] == '.' && path[1] == '/') + { + //detected relative path, begins with ./ + relpath_pos=2; + } + + programpath=&__path; + + //if we here than path is a relative or local + prgname = strrchr(programpath, '/'); + if (!prgname) return strdup(path); + + localpath_size = prgname - programpath + 1; + + newpath = malloc(FILENAME_MAX); + if(!newpath) + { + errno = E_NOMEM; + return NULL; + } + //copy local path to the new path + strncpy(newpath, programpath, localpath_size); + newpath[localpath_size] = 0; + + //copy filename to the new path + strcpy(newpath + localpath_size, path + relpath_pos); + + return(newpath); +} +*/ + + +FILE* fopen(const char* filename, const char *mode) +{ + FILE* res; + int imode, sz = -1; + char *fullname; + + imode=0; + if (*mode=='r') + { + imode=FILE_OPEN_READ; + mode++; + }else if (*mode=='w') + { + imode=FILE_OPEN_WRITE; + mode++; + }else if (*mode=='a') + { + imode=FILE_OPEN_APPEND; + mode++; + }else + return 0; + if (*mode=='+') + { + imode|=FILE_OPEN_PLUS; + mode++; + } + if (*mode=='t') + { + imode|=FILE_OPEN_TEXT; + mode++; + }else if (*mode=='b') + mode++; + if (*mode=='+') + { + imode|=FILE_OPEN_PLUS; + mode++; + } + if (*mode!=0) + return NULL; + +// fullname = (char*)getfullpath(filename); + fullname = strdup(filename); + if ((imode & 3) == FILE_OPEN_READ && fullname) /* check existense */ + { + sz = _ksys_get_filesize(fullname); + if (sz < 0) + { + free(fullname); + errno = sz; + return NULL; + } + } + + res = malloc(sizeof(FILE)); + if (res) + { + res->buffer=malloc(BUFSIZ); + res->buffersize=BUFSIZ; + res->filesize=0; + res->filepos=0; + res->mode=imode; + res->filename=fullname; + res->ungetc_buf = EOF; + res->buffer_start = -1; + res->buffer_end = -1; + } + if(!res || !res->buffer || !res->filename) + { + errno = E_NOMEM; + return NULL; + } + + if ((imode & 3) == FILE_OPEN_READ || (imode & 3) == FILE_OPEN_APPEND) + { + if (sz > 0) /*already got*/ + res->filesize = sz; + else + res->filesize=_ksys_get_filesize(res->filename); + } + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/format_print.c b/programs/develop/ktcc/trunk/libc/stdio/format_print.c new file mode 100644 index 0000000000..5cf6d379c5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/format_print.c @@ -0,0 +1,639 @@ +/* + function for format output to the string + +Siemargl update formats as http://www.cplusplus.com/reference/cstdio/printf/, no wchar though +http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap05.html is used too +%g explain https://support.microsoft.com/en-us/kb/43392 + +todo: +-fix precision in %g +-%u printed as signed, %x, %o also is promoted to long long +// FAIL 0x0FFFF7A7E as %x - signed long promotes sign, need %llx or %Lx and type conversion +-%a +-%n nothing printed +-%17.18f digits maximum format +-use %C as w_char L'x' (non standard extension) +-radix point always '.', no LOCALEs +*/ + + +#include +#include +#include +#include +#include + +enum flags_t +{ + flag_unsigned = 0x02, + flag_register = 0x04, + flag_plus = 0x08, + flag_left_just = 0x10, + flag_lead_zeros = 0x20, + flag_space_plus = 0x40, + flag_hash_sign = 0x80, + flag_point = 0x100 +}; +int formatted_double_to_string(long double number, int format1, int format2, char *s, int flags); +int formatted_double_to_string_scientific(long double number, int format1, int format2, char *s, int flags); +int formatted_long_to_string(long long number, int format1, int prec, char *s, int flags); +int formatted_hex_to_string(unsigned long long number, int fmt1, int prec, char *s, int flags); +int formatted_octa_to_string(unsigned long long number, int fmt1, int prec, char *s, int flags); + + +int formatted_double_special(long double number, char *s) +// return 0 if no special values: NAN, INF. -0.0 ignored +// http://steve.hollasch.net/cgindex/coding/ieeefloat.html +{ + struct IEEExp { + unsigned manl:32; + unsigned manh:32; + unsigned exp:15; + unsigned sign:1; + } *ip = (struct IEEExp *)&number; + + if (ip->exp != 0x7fff) return 0; + + if (ip->manh == 0x80000000 && ip->manl == 0) // Inf + { + if(ip->sign) + strcpy(s, "-INF"); + else + strcpy(s, "+INF"); + } else + if (ip->manh & ~0x7fffffff) + strcpy(s, "QNaN"); + else + strcpy(s, "SNaN"); + + return 4; +} + +int copy_and_align(char *dest, int width, char *src, int src_len, char sign, int flags) +// alingn number in buffer, put sign and fills additional places +// flags used only flag_left_just and flag_lead_zeros +// sign can be zero, 0, x, X, space, +, - +{ + int rc = 0, sign_len; + char fill; + + fill = (flags & flag_lead_zeros)&&((flags & flag_left_just)==0) ? '0' : ' '; + if(sign == 'x' || sign == 'X') + { + sign_len = 2; + } else + if (sign) + sign_len = 1; + else + sign_len = 0; + + if ((flags & flag_left_just) || (src_len + sign_len >= width)) // left justify or no room + { + if (sign) + { + if(sign == 'x' || sign == 'X') + { + dest[0] = '0'; + dest[1] = sign; + memcpy(dest + 2, src, src_len); + rc = src_len + 2; + } else + { // single sign + dest[0] = sign; + memcpy(dest + 1, src, src_len); + rc = src_len + 1; + } + } else + { + memcpy(dest, src, src_len); + rc = src_len; + } + if (rc < width) + { + memset(dest + rc, fill, width - rc); + rc = width; + } + } else // right justify and fill + { + rc = width; + memcpy(dest + width - src_len, src, src_len); + memset(dest, fill, width - src_len); + if (flags & flag_lead_zeros) + { + if(sign == 'x' || sign == 'X') + { + dest[0] = '0'; + dest[1] = sign; + } else + if (sign) dest[0] = sign; + } else + { + if(sign == 'x' || sign == 'X') + { + dest[width - src_len - 2] = '0'; + dest[width - src_len - 1] = sign; + } else + if (sign) dest[width - src_len - 1] = sign; + } + } + return rc; +} + +int formatted_double_to_string_scientific(long double number, int format1, int format2, char *s, int flags) +{ + long double norm_digit; + long mul = 0; + char sign = 0; + char buf[50]; + int len; + + if((flags & flag_point) == 0) format2 = 6; // default prec if no point spec + + len = formatted_double_special(number, buf); + if (len == 0) + { + if (number < 0) { sign = '-'; norm_digit = -number; } + else + { + norm_digit = number; + if (flags & flag_plus) sign = '+'; else + if (flags & flag_space_plus) sign = ' '; + } + // normalize + while (norm_digit < 1.0 && norm_digit > 0) { norm_digit *= 10; mul--; } + while (norm_digit >= 10.0) { norm_digit /= 10; mul++; } + + len = formatted_double_to_string(norm_digit, 0, format2, buf, flags & ~(flag_plus | flag_space_plus)); + + if (flags & flag_register) + buf[len++] = 'E'; + else + buf[len++] = 'e'; + + len += formatted_long_to_string(mul, 0, 3, buf + len, flag_plus | flag_lead_zeros); + } + else + flags &= ~flag_lead_zeros; // no need for INF, NAN + + len = copy_and_align(s, format1, buf, len, sign, flags); + + return len; +} + +int formatted_double_to_string(long double number, int format1, int format2, char *s, int flags) +{ + long double nafter, beforpointdigit; + long long intdigit, mul; + int div; + int i; + char sign = 0; + int fmt1; + int fmt2; + char buf[100], *pbuf = buf; + char buf_low[50], *pbuf_lo = buf_low; + + if((flags & flag_point) == 0) format2 = 6; // default prec if no point spec + + i = formatted_double_special(number, buf); + if (i == 0) + { + if (number < 0) {sign = '-'; number = -number; } + else + { + if (flags & flag_plus) sign = '+'; else + if (flags & flag_space_plus) sign = ' '; + } + + fmt1 = 1; + fmt2 = format2; + if (fmt2 > 18) fmt2 = 18; //maximum size of long long type + + beforpointdigit = floor(number + 0.00000000000001); + nafter = number - beforpointdigit; + + //print part of number before point + mul = 1; + for(i = 0; i < sizeof buf - 1; i++) + { + mul *= 10; + if ((beforpointdigit/mul) < 1.0) { fmt1 = i + 1; break; } + } + if (i == sizeof buf - 1 || fmt1 > 17) + { + strcpy(s, "[too big number for %f, %a]"); + return strlen(s); + } + + mul /= 10; + while(mul > 1) + { + div = beforpointdigit / mul; + *pbuf++ = (char)div + '0'; + beforpointdigit = beforpointdigit - div * mul; + mul /= 10; + } + *pbuf++=(char)beforpointdigit + '0'; + + //print part of number after point + mul = 1; + for(i = 0; i < fmt2; i++) + { + nafter = nafter*10; + mul *= 10; + } + + intdigit = roundl(nafter); + + mul /= 10; + for(i = 0; i < fmt2 - 1; i++) + { + div = intdigit / mul; + *pbuf_lo++=(char)div + '0'; + intdigit = intdigit - div * mul; + mul /= 10; + if (mul == 1) break; + } + *pbuf_lo++ = (char)intdigit + '0'; + + // form full number + if (roundl(nafter) != 0 || fmt2 != 0) + { + *pbuf++ = '.'; + memcpy(pbuf, buf_low, pbuf_lo - buf_low); pbuf += pbuf_lo - buf_low; + } else if (flags & flag_hash_sign) + *pbuf++ = '.'; + } + else + { + flags &= ~flag_lead_zeros; // no need for INF, NAN + pbuf += i; + } + + return copy_and_align(s, format1, buf, pbuf - buf, sign, flags); +} + +int formatted_long_to_string(long long number, int format1, int prec, char *s, int flags) +{ + int i; + int fmt; + char sign = 0; + long long digit; + long long mul; + int div; + char buf[100], *pbuf = buf; + + if (number == -9223372036854775807LL - 1) // overflow all our math, cant minus this + { + strcpy(s, "-9223372036854775808"); + return strlen(s); + } + + if (flags & flag_point) flags &= ~flag_lead_zeros; // conflicting flags + + if (number < 0) {sign = '-'; number = -number; } + else + { + if (flags & flag_plus) sign = '+'; else + if (flags & flag_space_plus) sign = ' '; + } + + digit = number; + + mul = (digit < 0) ? -1 : 1; + + for(i = 0; i < sizeof buf - 2; i++) + { + if (digit / mul < 10) { fmt = i + 1; break; } + mul *= 10; + } + + // add leading zeros by prec + for(i = 0; i < prec - fmt; i++) *pbuf++ = '0'; + + for(i = 0; i < fmt - 1; i++) + { + div = digit / mul; + *pbuf++ = (char)div + '0'; + digit = digit - div * mul; + mul /= 10; + if (mul == 1 || mul == -1) break; + } + *pbuf++ = (char)digit + '0'; + + return copy_and_align(s, format1, buf, pbuf - buf, sign, flags); +} + +int formatted_hex_to_string(unsigned long long number, int fmt1, int prec, char *s, int flags) +{ + unsigned long long digit, mul; + int i, div, fmt; + char xdigs_lower[16]="0123456789abcdef"; + char xdigs_upper[16]="0123456789ABCDEF"; + char buf[50], *pbuf = buf, sign; + + if (number == -9223372036854775807LL - 1) // overflow all our math, cant minus this + { + strcpy(buf, "FFFFFFFFFFFFFFFF"); + pbuf += strlen(buf); + } + else + { + if (flags & flag_point) flags &= ~flag_lead_zeros; // conflicting flags + + digit = number; + + mul = (digit < 0) ? -1 : 1; + + for(i = 0; i < sizeof buf - 2; i++) + { + if (digit / mul < 16) { fmt = i + 1; break; } + mul <<= 4; + } + + // add leading zeros by prec + for(i = 0; i < prec - fmt; i++) *pbuf++ = '0'; + + for(i = 0; i < fmt - 1; i++) + { + div = digit / mul; + *pbuf++ = (flags & flag_register) ? xdigs_upper[div] : xdigs_lower[div]; + digit = digit - div * mul; + mul >>= 4; + if (mul == 1 || mul == -1) break; + } + *pbuf++ = (flags & flag_register) ? xdigs_upper[digit] : xdigs_lower[digit]; + } + + sign = 0; + if(flags & flag_hash_sign) + sign = (flags & flag_register) ? 'X' : 'x'; + + return copy_and_align(s, fmt1, buf, pbuf - buf, sign, flags); +} + +int formatted_octa_to_string(unsigned long long number, int fmt1, int prec, char *s, int flags) +{ + unsigned long long digit, mul; + int i, div, fmt; + char xdigs_lower[16]="01234567"; + char buf[50], *pbuf = buf; + + if (number == -9223372036854775807LL - 1) // overflow all our math, cant minus this + { + strcpy(buf, "1777777777777777777777"); + pbuf += strlen(buf); + } + else + { + if (flags & flag_point) flags &= ~flag_lead_zeros; // conflicting flags + + digit = number; + + mul = (digit < 0) ? -1 : 1; + + for(i = 0; i < sizeof buf - 2; i++) + { + if (digit / mul < 8) { fmt = i + 1; break; } + mul <<= 3; + } + + // add leading zeros by prec + for(i = 0; i < prec - fmt; i++) *pbuf++ = '0'; + + for(i = 0; i < fmt - 1; i++) + { + div = digit / mul; + *pbuf++ = xdigs_lower[div & 0x7]; + digit = digit - div * mul; + mul >>= 3; + if (mul == 1 || mul == -1) break; + } + *pbuf++ = xdigs_lower[digit]; + } + + return copy_and_align(s, fmt1, buf, pbuf - buf, (flags & flag_hash_sign) ? '0' : 0, flags); +} + +//int vsnprintf (char * s, size_t n, const char * format, va_list arg ); +int format_print(char *dest, size_t maxlen, const char *fmt0, va_list argp) +{ + int i; + int length; + int fmt1, fmt2; // width, precision + size_t pos, posc; + long long intdigit; + long double doubledigit; + const char *fmt, *fmtc; // first point to %, fmtc points to specifier + char *s; // pointer to current dest char + char *str; + char buf[200]; // buffer for current argument value print representation + int format_flag; + int flag_long; // 2 = long double or long long int or wchar + int *point_to_n = NULL; + int flags; // parsed flags + + fmt = fmt0; + s = dest; + pos = 0; + while(pos < maxlen) + { + if (*fmt != '%') // usual char + { + if ('\0' == (*s++ = *fmt++)) break; + pos++; + continue; + } + + if (*(fmt + 1) == '%') // %% + { + *s++ = '%'; pos++; + fmt += 2; + continue; + } + //checking to containg format in the string + fmtc = fmt; + posc = pos; + + flags = 0; + format_flag = 0; + flag_long = 0; // 2 = long double or long long int or wchar + + while(*fmtc != '\0' && !format_flag) // searching end of format + { + fmtc++; posc++; + switch( *fmtc ) + { + case 'a': + format_flag = 1; + flags |= flag_unsigned; + break; + case 'A': + format_flag = 1; + flags |= flag_unsigned | flag_register; + break; + case 'c': case 'd': case 'i': case 'e': case 'f': case 'g': case 's': case 'n': + format_flag = 1; + break; + case 'E': case 'F': case 'G': + format_flag = 1; + flags |= flag_register; + break; + case 'l': + flag_long = flag_long ? 2 : 1; // ll.eq.L + break; + case 'L': + flag_long = 2; + break; + case 'o': case 'u': case 'x': case 'p': + format_flag = 1; + flags |= flag_unsigned; + break; + case 'X': case 'P': + format_flag = 1; + flags |= flag_unsigned | flag_register; + break; + case '+': + flags |= flag_plus; + break; + case '-': + flags |= flag_left_just; + break; + case ' ': // space + flags |= flag_space_plus; + break; + case '#': + flags |= flag_hash_sign; + break; + case '*': case '.': // just skip + break; + default: + if(isdigit(*fmtc)) break; + strncpy(dest, "print format error - in % invalid char found", maxlen); + return -1; // non format char found - user error + } + } + + if (format_flag == 0) + { + strncpy(dest, "print format error - % without format specifier", maxlen); + return -1; // format char not found - user error + } + + fmt1 = 0; + fmt2 = 0; + if (posc - pos > 1) // try to read width, precision + { + fmt++; + for(i = pos + 1; i < posc; i++) + { + switch(*fmt) + { + case '0': + if(fmt1 == 0 && (flags & flag_point) == 0) flags |= flag_lead_zeros; + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + if ((flags & flag_point) == 0) + fmt1 = fmt1 * 10 + (*fmt -'0'); + else + fmt2 = fmt2 * 10 + (*fmt -'0'); + break; + case '*': + if (flag_point == 0) + fmt1 = va_arg(argp, int); + else + fmt2 = va_arg(argp, int); + break; + case '.': + flags |= flag_point; + break; + case 'l': case 'L': case '+': // valid chars - skip + case '-': case ' ': case '#': + break; + default: // must be error + strncpy(dest, "print format error - %width.precision", maxlen); + return -1; // format char not found - user error + } + fmt++; + } + } + + // do real work - format arguments values + length = 0; + switch(*fmtc) + { + case 'n': + point_to_n = va_arg(argp, int*); + break; + case 'c': + if (pos + 1 <= maxlen) + { + buf[0] = (char)va_arg(argp, int); + length = 1; + } + break; + case 's': // special case - without buf + str = va_arg(argp, char*); + length = strlen(str); + if ((flags & flag_point) && (length > fmt2)) length = fmt2; // cut by precision + if (pos + length > maxlen) length = maxlen - pos; + memcpy(s, str ,length); + s += length; + pos += length; + break; + case 'd': case 'i': case 'u': case 'U': + if (flag_long == 0) intdigit = va_arg(argp, int); else + if (flag_long == 1) intdigit = va_arg(argp, long); else + if (flag_long == 2) intdigit = va_arg(argp, long long); + length = formatted_long_to_string(intdigit, fmt1, fmt2, buf, flags); + break; + case 'o': + if (flag_long == 0) intdigit = va_arg(argp, int); else + if (flag_long == 1) intdigit = va_arg(argp, long); else + if (flag_long == 2) intdigit = va_arg(argp, long long); + length = formatted_octa_to_string(intdigit, fmt1, fmt2, buf, flags); + break; + case 'p': case 'P': case 'x': case 'X': + if (flag_long == 0) intdigit = va_arg(argp, int); else + if (flag_long == 1) intdigit = va_arg(argp, long); else + if (flag_long == 2) intdigit = va_arg(argp, long long); + length=formatted_hex_to_string(intdigit, fmt1, fmt2, buf, flags); + break; + case 'a': case 'A': case 'f': case 'F': + if (flag_long <= 1) doubledigit = va_arg(argp, double); else + if (flag_long == 2) doubledigit = va_arg(argp, long double); + length = formatted_double_to_string(doubledigit, fmt1, fmt2, buf, flags); + break; + case 'e': case 'E': + if (flag_long <= 1) doubledigit = va_arg(argp, double); else + if (flag_long == 2) doubledigit = va_arg(argp, long double); + length = formatted_double_to_string_scientific(doubledigit, fmt1, fmt2, buf, flags); + break; + case 'g': case 'G': + //prec special case, this is just workaround + if (flag_long <= 1) doubledigit = va_arg(argp, double); else + if (flag_long == 2) doubledigit = va_arg(argp, long double); + if (flags & flag_point) + length = formatted_double_to_string(doubledigit, fmt1, fmt2, buf, flags); + else + length = formatted_double_to_string(doubledigit, fmt1, 1, buf, flags | flag_point); + i = formatted_double_to_string_scientific(doubledigit, fmt1, fmt2, buf + sizeof buf / 2, flags); + if(length > i) + { + memcpy(buf, buf + sizeof buf / 2, i); + length = i; + } + break; + } + if (*fmtc != 's' && length > 0) // skip multiple string copying + { + if (pos + length > maxlen) length = maxlen - pos; + memcpy(s, buf, length); + s += length; + pos += length; + } + fmt = fmtc + 1; + } + + if (point_to_n) *point_to_n = pos; + return(pos); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/format_scan.c b/programs/develop/ktcc/trunk/libc/stdio/format_scan.c new file mode 100644 index 0000000000..18de59b0d6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/format_scan.c @@ -0,0 +1,417 @@ +/* + function for format read from any source + +Siemargl formats as http://www.cplusplus.com/reference/cstdio/scanf/, no wchar though +http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap05.html is used too + +todo: +[characters], [^characters] +-%n nothing scanned, filled only if good result +-%d, i, u, o, x, p read similar - detect base by prefix 0 or 0x +-%a +-can overflow unsigned as signed +-radix point always '.', no LOCALEs +*/ + + +#include +#include +#include +#include +typedef int (*virtual_getc)(void *sp, const void *obj); +typedef void (*virtual_ungetc)(void *sp, int c, const void *obj); + +enum flags_t +{ + flag_unsigned = 0x02, + flag_register = 0x04, + flag_plus = 0x08, + flag_left_just = 0x10, + flag_lead_zeros = 0x20, + flag_space_plus = 0x40, + flag_hash_sign = 0x80, + flag_point = 0x100 +}; + +int try_parse_real(long double *real, int ch, const void *src, void *save, virtual_getc vgetc, virtual_ungetc vungetc) +// returns 1 if OK, -1 == EOF, -2 parse broken +{ + int sign = 1, have_digits = 0; + long long div; + + if (ch == '+') + { + ch = vgetc(save, src); + if (ch == EOF) return EOF; + } else + if (ch == '-') + { + sign = -1; + ch = vgetc(save, src); + if (ch == EOF) return EOF; + }; + *real = 0.0; + for (;;) // mantissa before point + { + // test ch is valid + if (isdigit(ch)) + { + *real = *real * 10 + ch - '0'; + have_digits++; + ch = vgetc(save, src); + if (ch == EOF || isspace(ch)) break; // ok, just finish num + } else + if (ch == '.' || ch == 'E' || ch == 'e') + { + break; // ok + } + else + { + vungetc(save, ch, src); + break; + } + } + if (ch != '.' && ch != 'E' && ch != 'e') // ok, just integer part + { + *real *= sign; + if (have_digits) + return 1; + else + return -2; + } + + if(ch == '.') + { + ch = vgetc(save, src); + div = 10; // use as divisor + for (;;) // mantissa after point + { + // test ch is valid + if (isdigit(ch)) + { + *real += (double)(ch - '0') / div; + div *= 10; + have_digits++; + ch = vgetc(save, src); + if (ch == EOF || isspace(ch)) break; // ok, just finish num + } else + if (ch == 'E' || ch == 'e') + { + break; // ok + } + else + { + vungetc(save, ch, src); + break; + } + } + if (ch != 'E' && ch != 'e') // ok, real as XX.YY + { + *real *= sign; + if (have_digits) + return 1; + else + return -2; + } + } + + ch = vgetc(save, src); + *real *= sign; + // exponent + sign = 1; + if (ch == '+') + { + ch = vgetc(save, src); + if (ch == EOF) return EOF; + } else + if (ch == '-') + { + sign = -1; + ch = vgetc(save, src); + if (ch == EOF) return EOF; + }; + div = 0; + for (;;) + { + // test ch is valid + if (isdigit(ch)) + { + div = div * 10 + ch - '0'; + ch = vgetc(save, src); + if (ch == EOF || isspace(ch)) break; // ok, just finish num + } + else + { + vungetc(save, ch, src); + break; + } + } + div *= sign; + *real *= pow(10, div); + + return 1; +} + +int try_parse_int(long long *digit, int ch, const void *src, void *save, virtual_getc vgetc, virtual_ungetc vungetc) +{ + int sign = 1, base = 10, have_digits = 0; + + if (ch == '+') + { + ch = vgetc(save, src); + if (ch == EOF) return EOF; + } else + if (ch == '-') + { + sign = -1; + ch = vgetc(save, src); + if (ch == EOF) return EOF; + }; + + if (ch == '0') // octal or hex, read next + { + base = 8; + ch = vgetc(save, src); + if (ch == EOF || isspace(ch)) + have_digits++; + else + if (ch == 'x' || ch == 'X') + { + base = 16; + ch = vgetc(save, src); + if (ch == EOF) return EOF; + } + } + *digit = 0; + for (;;) + { + // test ch is valid + if ((isdigit(ch) && base == 10) || + (isdigit(ch) && base == 8 && ch < '8') || + (isxdigit(ch) && base == 16)) + { + if (base == 16) + { + if (ch <= '9') ch-= '0'; + else + if (ch <= 'F') ch = 10 + ch - 'A'; + else + ch = 10 + ch - 'a'; + } + else + ch -= '0'; + *digit = *digit * base + ch; + have_digits++; + ch = vgetc(save, src); + if (ch == EOF || isspace(ch)) break; // ok, just finish num + } + else if (ch == EOF || isspace(ch)) + break; + else + { + vungetc(save, ch, src); + break; + } + } + *digit *= sign; + if (have_digits) + return 1; + else + return -2; +} + + + +int format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vgetc, virtual_ungetc vungetc) +{ + int i; + int length; + int fmt1, fmt2; // width, precision + size_t pos, posc; + const char *fmtc; // first point to %, fmtc points to specifier + int ch; + int format_flag; + int flag_long; // 2 = long double or long long int or wchar + int *point_to_n = NULL, nread = 0; + int flags; // parsed flags + int save = 0; + char *arg_str; + int *arg_int; + long *arg_long; + long long *arg_longlong; + float *arg_float; + double *arg_double; + long double *arg_longdouble; + long long digit; + long double real; + + + pos = 0; + while(*fmt) + { + while (*fmt && isspace(*fmt)) fmt++; // skip paces in format str + + if (*fmt != '%') // usual char + { + ch = vgetc(&save, src); + if (ch != *fmt++) // char not match format + { + vungetc(&save, ch, src); + break; + } + pos++; + continue; + } + + if (*(fmt + 1) == '%') // %% + { + ch = vgetc(&save, src); + if (ch != '%') // char not match format + { + vungetc(&save, ch, src); + break; + } + pos++; + fmt += 2; + continue; + } + //checking to containg format in the string + fmtc = fmt; + posc = pos; + + flags = 0; + format_flag = 0; + flag_long = 0; // 2 = long double or long long int or wchar + + while(*fmtc != '\0' && !format_flag) // searching end of format + { + fmtc++; posc++; + switch( *fmtc ) + { + case 'a': + format_flag = 1; + flags |= flag_unsigned; + break; + case 'c': case 'd': case 'i': case 'e': case 'f': case 'g': case 's': case 'n': + format_flag = 1; + break; + case 'l': + flag_long = flag_long ? 2 : 1; // ll.eq.L + break; + case 'L': + flag_long = 2; + break; + case 'o': case 'u': case 'x': case 'p': + format_flag = 1; + flags |= flag_unsigned; + break; + case '*': case '.': // just skip + break; + default: + if(isdigit(*fmtc)) break; + goto exit_me; // non format char found - user error + } + } + + if (format_flag == 0) + { + goto exit_me; // format char not found - user error + } + + fmt1 = 0; + fmt2 = 0; + if (posc - pos > 1) // try to read width, precision + { + fmt++; + for(i = pos + 1; i < posc; i++) + { + switch(*fmt) + { + case '0': + if(fmt1 == 0 && (flags & flag_point) == 0) flags |= flag_lead_zeros; + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + if ((flags & flag_point) == 0) + fmt1 = fmt1 * 10 + (*fmt -'0'); + else + fmt2 = fmt2 * 10 + (*fmt -'0'); + break; + case '*': // ignoring + break; + case '.': + flags |= flag_point; + break; + case 'l': case 'L': // valid chars - skip + break; + default: // must be error + goto exit_me; // format char not found - user error + } + fmt++; + } + } + + // do real work - format arguments values + // skip input spaces + do { + ch = vgetc(&save, src); + if (ch == EOF) goto exit_me; + } while (isspace(ch)); + + switch(*fmtc) + { + case 'n': + point_to_n = va_arg(argp, int*); + vungetc(&save, ch, src); + break; + case 'c': // read width chars, ever spaces + arg_str = va_arg(argp, char*); + if (fmt1 == 0) length = 1; + else length = fmt1; + for (i = 0; i < length;) + { + *arg_str++ = ch; i++; + ch = vgetc(&save, src); + if (ch == EOF) break; + } + if (i < length) goto exit_me; // not enough chars + break; + case 's': + arg_str = va_arg(argp, char*); + if (fmt1 == 0) length = 4095; // max string scan 4096 + else length = fmt1; + for (i = 0; i < length; i++) + { + *arg_str++ = ch; + ch = vgetc(&save, src); + if (ch == EOF || isspace(ch)) break; // ok, just finish string + } + *arg_str++ = '\0'; + break; + case 'd': case 'i': case 'u': + case 'o': case 'p': case 'x': + i = try_parse_int(&digit, ch, src, &save, vgetc, vungetc); + if (i < 0) goto exit_me; + + if (flag_long == 0) { arg_int = va_arg(argp, int*); *arg_int = (int)digit; } else + if (flag_long == 1) { arg_long = va_arg(argp, long*); *arg_long = (long)digit; } else + if (flag_long == 2) { arg_longlong = va_arg(argp, long long*); *arg_longlong = digit; } + break; + case 'a': case 'A': case 'f': case 'F': + case 'e': case 'E': + case 'g': case 'G': + i = try_parse_real(&real, ch, src, &save, vgetc, vungetc); + if (i < 0) goto exit_me; + + if (flag_long == 0) { arg_float = va_arg(argp, float*); *arg_float = (float)real; } else + if (flag_long == 1) { arg_double = va_arg(argp, double*); *arg_double = (double)real; } else + if (flag_long == 2) { arg_longdouble = va_arg(argp, long double*); *arg_longdouble = real; } + break; + } + + fmt = fmtc + 1; + nread++; + } +exit_me: + if (point_to_n) *point_to_n = nread; + return nread; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fprintf.c b/programs/develop/ktcc/trunk/libc/stdio/fprintf.c new file mode 100644 index 0000000000..4828555a32 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fprintf.c @@ -0,0 +1,44 @@ +#include +#include + + + +int fprintf(FILE* file, const char* format, ...) +{ + va_list arg; + va_start (arg, format); + + return vfprintf(file, format, arg); + +} + +int vfprintf ( FILE * file, const char * format, va_list arg ) +{ + char *buf; + int printed, rc = 0; + + if(!file || !format) + { + errno = E_INVALIDPTR; + return errno; + } + + buf=malloc(4096*2); //8kb max + if(!buf) + { + errno = E_NOMEM; + return errno; + } + + printed=format_print(buf,8191, format,arg); + if (file == stderr) + debug_out_str(buf); + else + rc = fwrite(buf,printed,1,file); + free(buf); + + if (rc < 0) + return rc; + else + return(printed); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fputc.c b/programs/develop/ktcc/trunk/libc/stdio/fputc.c new file mode 100644 index 0000000000..aba6212280 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fputc.c @@ -0,0 +1,10 @@ +#include +int fputc(int c,FILE* file) +{ + dword res; + + res = fwrite(&c, 1, 1, file); + if (res < 1) return EOF; + + return c; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fputs.c b/programs/develop/ktcc/trunk/libc/stdio/fputs.c new file mode 100644 index 0000000000..b360313a31 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fputs.c @@ -0,0 +1,27 @@ +#include + +int fputs ( const char * str, FILE * file ) +{ + int rc; + + if(!file || !str) + { + errno = E_INVALIDPTR; + return EOF; + } + + if ((file->mode & 3)==FILE_OPEN_READ) + { + errno = E_ACCESS; + return EOF; + } + + while(*str) + { + rc = fputc(*str, file); + if (rc < 0) return rc; + str++; + } + + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fread.c b/programs/develop/ktcc/trunk/libc/stdio/fread.c new file mode 100644 index 0000000000..5628ed539d --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fread.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +int fread(void *buffer,int size,int count,FILE* file) +{ + dword res, readbytes; + dword fullsize, read4cache, toread, readcount; + + if(!file || !buffer) + { + errno = E_INVALIDPTR; + return 0; + } + + if ((file->mode &3)!=FILE_OPEN_READ && (file->mode & FILE_OPEN_PLUS)==0) + { + errno = E_ACCESS; + return 0; + } + + fullsize=count*size; + if (fullsize + file->filepos >= file->filesize) + { + fullsize=file->filesize - file->filepos; + if (fullsize <= 0) return 0; + } + + /***** file buffering strategy, just read forward ***** + if we read small part - read full buffer, but if buffer have this data - dont read again nothing (or partial read forward 4k pages) + any writes drops buffers as ungetc_buf */ + read4cache = 0; + readcount = 0; + if (file->filepos >= file->buffer_start && file->filepos < file->buffer_end) + { + read4cache = min(file->buffer_end - file->filepos, fullsize); + memcpy(buffer, file->buffer + file->filepos - file->buffer_start, read4cache); + file->filepos += read4cache; + if (file->ungetc_buf != EOF) // subst ungetc byte + { + *((char*)buffer) = (char)file->ungetc_buf; + file->ungetc_buf = EOF; + } + buffer += read4cache; // ! advance + fullsize -= read4cache; + readcount = read4cache / size; + } + + toread = max(fullsize, file->buffersize); + if (toread + file->filepos >= file->filesize) + { + toread = file->filesize - file->filepos; + } + + if (fullsize <= 0 || toread <= 0) + res = 0; // already read or file end + else + { + file->buffer_start = file->filepos; + if (toread <= fullsize) // read to bigger buffer + { + res = _ksys_readfile(file->filename, file->filepos, toread, buffer, &readbytes); + read4cache = min(readbytes, file->buffersize); + memcpy(file->buffer, buffer, read4cache); + file->filepos += readbytes; + } else + { + res = _ksys_readfile(file->filename, file->filepos, toread, file->buffer, &readbytes); + read4cache = readbytes; + memcpy(buffer, file->buffer, min(fullsize, read4cache)); + file->filepos += min(fullsize, read4cache); + } + file->buffer_end = file->buffer_start + read4cache; + if (readbytes >= fullsize) + readcount += fullsize / size; + else + readcount += readbytes / size; + } + + if (file->ungetc_buf != EOF) // subst ungetc byte + { + *((char*)buffer) = (char)file->ungetc_buf; + file->ungetc_buf = EOF; + } + + if (res != 0) + { + file->ungetc_buf = EOF; + errno = -res; + } + + return readcount; // really full readed plus cached items +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fscanf.c b/programs/develop/ktcc/trunk/libc/stdio/fscanf.c new file mode 100644 index 0000000000..9ae0ff86f8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fscanf.c @@ -0,0 +1,42 @@ +#include +#include +#include + +int virtual_getc_file(void *sp, const void *obj) +// get next chat from file obj, save point is ptr to string char ptr +{ + FILE *f = (FILE *)obj; + int ch = fgetc(f); + +//printf("getc '%c'[%d];", ch, ch); + + return ch; +} + +void virtual_ungetc_file(void *sp, int c, const void *obj) +// if can, one step back savepoint in s +{ + FILE *f = (FILE *)obj; + + if (f) ungetc(c, f); +} + + +int vfscanf ( FILE * stream, const char * format, va_list arg ) +{ + return format_scan(stream, format, arg, &virtual_getc_file, &virtual_ungetc_file); +}; + +int fscanf ( FILE * stream, const char * format, ...) +{ + va_list arg; + int n; + va_start(arg, format); + + n = vfscanf(stream, format, arg); + + va_end(arg); + return n; +} + + diff --git a/programs/develop/ktcc/trunk/libc/stdio/fseek.c b/programs/develop/ktcc/trunk/libc/stdio/fseek.c new file mode 100644 index 0000000000..813815dfee --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fseek.c @@ -0,0 +1,19 @@ +#include +int fseek(FILE* file,long offset,int origin) +{ + fpos_t pos; + if(!file) + { + errno = E_INVALIDPTR; + return errno; + } + + if (origin==SEEK_CUR) + offset+=file->filepos; + else if (origin==SEEK_END) + offset+=file->filesize; + else if (origin!=SEEK_SET) + return EOF; + pos = offset; + return fsetpos(file, &pos); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fsetpos.c b/programs/develop/ktcc/trunk/libc/stdio/fsetpos.c new file mode 100644 index 0000000000..fcedd73430 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fsetpos.c @@ -0,0 +1,18 @@ +#include +int fsetpos(FILE* file,const fpos_t * pos) +{ + if(!file || !pos) + { + errno = E_INVALIDPTR; + return errno; + } + + if (*pos>=0) + { + file->filepos=*pos; + file->ungetc_buf = EOF; + return 0; + } + else + return EOF; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/ftell.c b/programs/develop/ktcc/trunk/libc/stdio/ftell.c new file mode 100644 index 0000000000..053ce76e2c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/ftell.c @@ -0,0 +1,11 @@ +#include +long ftell(FILE* file) +{ + if(!file) + { + errno = E_INVALIDPTR; + return -1L; + } + + return file->filepos; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/fwrite.c b/programs/develop/ktcc/trunk/libc/stdio/fwrite.c new file mode 100644 index 0000000000..5acd588d08 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/fwrite.c @@ -0,0 +1,83 @@ +#include +#include + +// dont support return partial writing when fail +// only 32-bit filesize +int fwrite(void *buffer,int size,int count,FILE* file) +{ + dword res; + dword fullsize; + + if(!file || !buffer) + { + errno = E_INVALIDPTR; + return EOF; + } + + + if ((file->mode & 3)==FILE_OPEN_READ) + { + errno = E_ACCESS; + return 0; + } + + if ((file->mode &3)==FILE_OPEN_APPEND) + file->filepos=file->filesize; + + fullsize=count*size; + + if ((file->filesize)<(file->filepos+fullsize)) file->filesize=file->filepos+fullsize; + + /* + if (file->mode==FILE_OPEN_APPEND) + { + file->filepos==file->filesize; + res=_ksys_appendtofile(file->filename,file->filepos,fullsize,buffer); + if (res==0) + { + file->filepos+=fullsize; + fullsize=fullsize/size; + return(fullsize); + } + else return(0); + + } + */ + file->ungetc_buf = EOF; + if (file->filepos >= file->buffer_start && file->filepos < file->buffer_end) // drop buffer, if change his data + { + file->buffer_start = -1; + file->buffer_end = -1; + } + + if ((file->mode &3)==FILE_OPEN_WRITE || (file->mode&3)==FILE_OPEN_APPEND) // always true, as read checked previous + { + if (file->filepos==0) + { //file mot created yet + res=_ksys_rewritefile(file->filename,fullsize,buffer); + if (res==0) + { + file->filepos+=fullsize; + return(count); + } else + { + errno = -res; + return(0); + } + } + else + { + res=_ksys_appendtofile(file->filename,file->filepos,fullsize,buffer); + if (res==0) + { + file->filepos+=fullsize; + return(count); + } else + { + errno = -res; + return(0); + } + } + } + else return(0); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/getchar.c b/programs/develop/ktcc/trunk/libc/stdio/getchar.c new file mode 100644 index 0000000000..73d901f5bd --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/getchar.c @@ -0,0 +1,6 @@ +#include + +int getchar ( void ) +{ + return con_getch(); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/gets.c b/programs/develop/ktcc/trunk/libc/stdio/gets.c new file mode 100644 index 0000000000..9b2a12c9bf --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/gets.c @@ -0,0 +1,8 @@ +#include + +char * gets ( char * str ) +{ + con_init_console_dll(); + + return con_gets(str, 80); // small, to reduce overflow risk +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdio/printf.c b/programs/develop/ktcc/trunk/libc/stdio/printf.c new file mode 100644 index 0000000000..e9b9851707 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/printf.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +int printf(const char *format, ...) +{ + va_list arg; + va_start(arg, format); + + return vprintf(format, arg); +} + + +int vprintf ( const char * format, va_list arg ) +{ + int i = 0; + int printed_simbols = 0; + char *s; + + i=con_init_console_dll(); + + if (i == 0) + { + s = malloc(4096); + printed_simbols = format_print(s, 4096, format, arg); + con_write_string(s, printed_simbols); + free(s); + } + + return(printed_simbols); +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/putchar.c b/programs/develop/ktcc/trunk/libc/stdio/putchar.c new file mode 100644 index 0000000000..d28f59e988 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/putchar.c @@ -0,0 +1,14 @@ +#include + +int putchar ( int ch ) +{ + char s[2]; + + con_init_console_dll(); + + s[0] = (char)ch; + s[1] = '\0'; + + con_write_asciiz(s); + return ch; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdio/puts.c b/programs/develop/ktcc/trunk/libc/stdio/puts.c new file mode 100644 index 0000000000..9644db80eb --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/puts.c @@ -0,0 +1,11 @@ +#include + +int puts ( const char * str ) +{ + con_init_console_dll(); + + con_write_asciiz(str); + con_write_asciiz("\n"); + + return 1; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdio/rewind.c b/programs/develop/ktcc/trunk/libc/stdio/rewind.c new file mode 100644 index 0000000000..2df1a1e286 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/rewind.c @@ -0,0 +1,12 @@ +#include +void rewind(FILE* file) +{ + if(!file) + { + errno = E_INVALIDPTR; + return; + } + + file->ungetc_buf = EOF; + file->filepos=0; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/scanf.c b/programs/develop/ktcc/trunk/libc/stdio/scanf.c new file mode 100644 index 0000000000..01093ea0ff --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/scanf.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +char *__scanf_buffer = 0; + +int virtual_getc_con(void *sp, const void *obj) +// get next chat from string obj, save point is ptr to string char ptr +{ + int ch; + const char**spc= (const char**)sp; + if (!spc) return EOF; // error + + if (!*spc) *spc = __scanf_buffer; // first call, init savepoint + + while (!**spc) // need to read more + { + if(!gets(__scanf_buffer)) return EOF; + *spc = __scanf_buffer; + strcat(__scanf_buffer,"\n"); // imitate delimiter + } + if (**spc == 26 || **spc == 3) // ^C ^Z end of scan, clear buffer + { + *spc = __scanf_buffer; + *__scanf_buffer = 0; + return EOF; // ^C ^Z + } + + ch = **spc; (*spc)++ ; + +//printf("getc '%c'[%d];", ch, ch); + return ch; +} + +void virtual_ungetc_con(void *sp, int c, const void *obj) +// if can, one step back savepoint in s +{ + const char**spc= (const char**)sp; + + if (spc && *spc > __scanf_buffer) (*spc)--; +//printf("Ungetc '%c'[%d];", c, c); +} + + +int vscanf ( const char * format, va_list arg ) +{ + return format_scan(NULL, format, arg, &virtual_getc_con, &virtual_ungetc_con); +}; + +int scanf ( const char * format, ...) +{ + va_list arg; + int n; + va_start(arg, format); + + if(__scanf_buffer == NULL) __scanf_buffer = malloc(4096); + if(__scanf_buffer == NULL) return -3; + + *__scanf_buffer = 0; + n = vscanf(format, arg); + + va_end(arg); + return n; +} + + diff --git a/programs/develop/ktcc/trunk/libc/stdio/snprintf.c b/programs/develop/ktcc/trunk/libc/stdio/snprintf.c new file mode 100644 index 0000000000..0db3e146a7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/snprintf.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int format_print(char *dest, size_t maxlen, const char *fmt,va_list argp); + + +int snprintf(char *dest, size_t size,const char *format,...) +{ + va_list arg; + va_start (arg, format); + return format_print(dest,size, format, arg); +} + + diff --git a/programs/develop/ktcc/trunk/libc/stdio/sprintf.c b/programs/develop/ktcc/trunk/libc/stdio/sprintf.c new file mode 100644 index 0000000000..30ebadf9a2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/sprintf.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int format_print(char *dest, size_t maxlen, const char *fmt,va_list argp); + + +int sprintf(char *dest,const char *format,...) +{ + va_list arg; + va_start (arg, format); + return format_print(dest,4096, format, arg); +} + + diff --git a/programs/develop/ktcc/trunk/libc/stdio/sprintf_tiny.c b/programs/develop/ktcc/trunk/libc/stdio/sprintf_tiny.c new file mode 100644 index 0000000000..4c0810b5b4 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/sprintf_tiny.c @@ -0,0 +1,97 @@ +/* + function for format output to the string. much lighter than standard sprintf + because of lesser formats supported +*/ + + +#include +//#include +#include +#include + +char* __itoa(int n,char* s); +char* itoab(unsigned int n, char* s, int b); + +int tiny_vsnprintf (char * s, size_t n, const char * format, va_list args ) +// support %c, %s, %d, %x, %u, %% for 32-bit values only. no width specs, left align +// always zero-ended +{ + char *fmt, *dest, buf[32]; + fmt = (char*)format; + dest = s; dest[n - 1] = '\0'; + int arg, len; + while (*fmt && (dest - s < n - 1)) + { + if (*fmt != '%') + { + *dest++ = *fmt++; + continue; + } + if (fmt[1] == '%') // %% + { + *dest++ = '%'; + fmt += 2; + continue; + } + arg = va_arg(args, int); + len = n - 1 - (dest - s); + switch (*++fmt) + { + case 'c': + *dest++ = (char)arg; + break; + case 's': + strncpy(dest, (char*)arg, len); + dest = strchr(dest, 0); + break; + case 'd': + __itoa(arg, buf); + strncpy(dest, buf, len); + dest = strchr(dest, 0); + break; + case 'x': + itoab((unsigned)arg, buf, 16); + strncpy(dest, buf, len); + dest = strchr(dest, 0); + break; + case 'u': + itoab((unsigned)arg, buf, 10); + strncpy(dest, buf, len); + dest = strchr(dest, 0); + break; + default: + *dest++ = *fmt; + } + fmt++; + } + *dest = '\0'; + return dest - s; +} + + +int tiny_snprintf (char * s, size_t n, const char * format, ... ) +{ + va_list arg; + int rc; + va_start(arg, format); + + rc = tiny_vsnprintf(s, n, format, arg); + + va_end(arg); + return rc; +} + +int tiny_sprintf (char * s, const char * format, ... ) +{ + va_list arg; + int rc; + va_start(arg, format); + + rc = tiny_vsnprintf(s, 4096, format, arg); + + va_end(arg); + return rc; +} + + + diff --git a/programs/develop/ktcc/trunk/libc/stdio/sscanf.c b/programs/develop/ktcc/trunk/libc/stdio/sscanf.c new file mode 100644 index 0000000000..2595506703 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/sscanf.c @@ -0,0 +1,46 @@ +#include +#include + +int virtual_getc_str(void *sp, const void *obj) +// get next chat from string obj, save point is ptr to string char ptr +{ + int ch; + const char *s = (const char *)obj; + const char**spc= (const char**)sp; + if (!s || !spc) return EOF; // error + + if (!*spc) *spc = s; // first call, init savepoint + + if (!**spc) return EOF; // EOS + + ch = **spc; (*spc)++ ; + + return ch; +} + +void virtual_ungetc_str(void *sp, int c, const void *obj) +// if can, one step back savepoint in s +{ + const char *s = (const char *)obj; + const char**spc= (const char**)sp; + + if (s && spc && *spc > s) (*spc)--; +} + +int vsscanf ( const char * s, const char * format, va_list arg ) +{ + return format_scan(s, format, arg, &virtual_getc_str, &virtual_ungetc_str); +}; + +int sscanf ( const char * s, const char * format, ...) +{ + va_list arg; + int n; + va_start(arg, format); + + n = vsscanf(s, format, arg); + + va_end(arg); + return n; +} + diff --git a/programs/develop/ktcc/trunk/libc/stdio/ungetc.c b/programs/develop/ktcc/trunk/libc/stdio/ungetc.c new file mode 100644 index 0000000000..2d7c295800 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/ungetc.c @@ -0,0 +1,32 @@ +#include +// non standard realization - support for virtually change ONLY ONE char + + + +int ungetc(int c,FILE* file) +{ + dword res; + + if(!file) + { + errno = E_INVALIDPTR; + return EOF; + } + + if ((file->mode & 3) != FILE_OPEN_READ && (file->mode & FILE_OPEN_PLUS) == 0) + { + errno = E_ACCESS; + return EOF; + } + + if (file->filepos > file->filesize || file->filepos == 0 || c == EOF || file->ungetc_buf != EOF) + { + errno = E_EOF; + return EOF; + } + + file->ungetc_buf = c; + file->filepos--; + + return c; +} diff --git a/programs/develop/ktcc/trunk/libc/stdio/vsnprintf.c b/programs/develop/ktcc/trunk/libc/stdio/vsnprintf.c new file mode 100644 index 0000000000..82941198ca --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdio/vsnprintf.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int vsnprintf(char *dest, size_t size, const char *format, va_list ap) +{ + return format_print(dest,size, format, ap); +} + + +int vsprintf (char * dest, const char * format, va_list ap ) +{ + return format_print(dest, 4096, format, ap); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/assert.c b/programs/develop/ktcc/trunk/libc/stdlib/assert.c new file mode 100644 index 0000000000..b5d6ca901c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/assert.c @@ -0,0 +1,7 @@ +#include +#include + +void __assert_fail(const char *expr, const char *file, int line, const char *func){ + fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); + abort(); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/atoi.c b/programs/develop/ktcc/trunk/libc/stdlib/atoi.c new file mode 100644 index 0000000000..70ef2d6323 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/atoi.c @@ -0,0 +1,21 @@ +#include "stdio.h" +#include "stdlib.h" +#include "ctype.h" + + +/* +** atoi(s) - convert s to integer. +*/ +int atoi(char *s) +{ + int sign, n; + while(isspace(*s)) ++s; + sign = 1; + switch(*s) { + case '-': sign = -1; + case '+': ++s; + } + n = 0; + while(isdigit(*s)) n = 10 * n + *s++ - '0'; + return (sign * n); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/atoib.cpp b/programs/develop/ktcc/trunk/libc/stdlib/atoib.cpp new file mode 100644 index 0000000000..70c102bf95 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/atoib.cpp @@ -0,0 +1,22 @@ +#include "stdio.h" +#include "stdlib.h" +#include "ctype.h" + +/* +** atoib(s,b) - Convert s to "unsigned" integer in base b. +** NOTE: This is a non-standard function. +*/ +int atoib(char *s,int b) +{ + int n, digit; + n = 0; + while(isspace(*s)) ++s; + while((digit = (127 & *s++)) >= '0') { + if(digit >= 'a') digit -= 87; + else if(digit >= 'A') digit -= 55; + else digit -= '0'; + if(digit >= b) break; + n = b * n + digit; + } + return (n); + } diff --git a/programs/develop/ktcc/trunk/libc/stdlib/div.c b/programs/develop/ktcc/trunk/libc/stdlib/div.c new file mode 100644 index 0000000000..159e77e16f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/div.c @@ -0,0 +1,10 @@ +#include + +div_t div (int numer, int denom) +{ + div_t res; + res.quot = numer / denom; + res.rem = numer % denom; + + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/exit.c b/programs/develop/ktcc/trunk/libc/stdlib/exit.c new file mode 100644 index 0000000000..15ab8864a6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/exit.c @@ -0,0 +1,11 @@ +#include +#include +#include + +void exit (int status) +/* close console if was initialized, also stay window [finished] when status is error < 0 */ +{ + if (__console_initdll_status) + con_exit(status > 0); + _ksys_exit(); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdlib/getopt.c b/programs/develop/ktcc/trunk/libc/stdlib/getopt.c new file mode 100644 index 0000000000..6286d05032 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/getopt.c @@ -0,0 +1,63 @@ +#include +#include +#include + +char *optarg = NULL; // global argument pointer +int optind = 0; // global argv index + +int getopt(int argc, char *argv[], char *optstring) { + static char *next = NULL; + char c; + char *cp = NULL; + + if (optind == 0) + next = NULL; + + optarg = NULL; + + if (next == NULL || *next == '\0') { + if (optind == 0) + optind++; + + if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') { + optarg = NULL; + if (optind < argc) + optarg = argv[optind]; + return EOF; + } + + if (strncmp(argv[optind], "--", 2) == 0) { + optind++; + optarg = NULL; + if (optind < argc) + optarg = argv[optind]; + return EOF; + } + + next = argv[optind]; + next++; // skip past - + optind++; + } + + c = *next++; + cp = strchr(optstring, c); + + if (cp == NULL || c == ':') + return '?'; + + cp++; + if (*cp == ':') { + if (*next != '\0') { + optarg = next; + next = NULL; + } + else if (optind < argc) { + optarg = argv[optind]; + optind++; + } + else { + return '?'; + } + } + return c; +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/itoa.c b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c new file mode 100644 index 0000000000..8839cb565c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +/* +** itoa(n,s) - Convert n to characters in s +*/ +char* __itoa(int n,char* s) +{ + int sign; + char *ptr; + ptr = s; + + if(n == (int)0x80000000) + return strcpy(s, "-2147483648"); // overflowed -n + + if ((sign = n) < 0) n = -n; + do { + *ptr++ = n % 10 + '0'; + } while ((n = n / 10) > 0); + if (sign < 0) *ptr++ = '-'; + *ptr = '\0'; + return strrev(s); +} + diff --git a/programs/develop/ktcc/trunk/libc/stdlib/itoab.c b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c new file mode 100644 index 0000000000..d7fb98c210 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +/* +** itoab(n,s,b) - Convert "unsigned" n to characters in s using base b. +** NOTE: This is a non-standard function. +*/ +char* itoab(unsigned int n, char* s, int b) +{ + char *ptr; + int lowbit; + ptr = s; + b >>= 1; + do { + lowbit = n & 1; + n = (n >> 1) & 0x7FFFFFFF; + *ptr = ((n % b) << 1) + lowbit; + if(*ptr < 10) *ptr += '0'; else *ptr += 55; + ++ptr; + } while(n /= b); + *ptr = 0; + return strrev(s); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/random.c b/programs/develop/ktcc/trunk/libc/stdlib/random.c new file mode 100644 index 0000000000..c832bad477 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/random.c @@ -0,0 +1,15 @@ + +#include "stdlib.h" + +unsigned int seed_o=1; + +void srand (unsigned int seed) +{ + seed_o=seed; +} + +int rand (void) +{ + seed_o=(seed_o*25173+13849) & (65535); + return(seed_o); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/strtod.c b/programs/develop/ktcc/trunk/libc/stdlib/strtod.c new file mode 100644 index 0000000000..29b2bfaf3b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/strtod.c @@ -0,0 +1,70 @@ +#include +#include + +double strtod (const char* str, char** endptr) +{ + double res = 0.0; + int pwr = 0, pwr1, esign = 1, sign = 1; + + while (isspace(*str)) str++; + + if (*str == '-') { sign = -1; str++; } + else + if (*str == '+') str++; + + + while (isdigit(*str)) + { + res = 10 * res + (*str - '0'); + str++; + } + + if (*str =='.') + { + str++; + double div = 10.0; + while (isdigit(*str)) + { + res += (*str - '0') / div; + str++; + div *= 10; + } + } + + if (*str =='e' || *str =='E') + { + str++; + if (*str == '-') { esign = -1; str++; } + else + if (*str == '+') str++; + + while (isdigit(*str)) + { + pwr = 10.0 * pwr + (*str - '0'); + str++; + } + + // fck, i've no pow() yet + // res = res * pow(10, pwr); + for (pwr1 = pwr; pwr1 !=0; pwr1--) + if (esign == 1) + res *= 10; + else + res /= 10; + + } + if (endptr) + *endptr = (char*)str; + + return res * sign; +} + +long double strtold (const char* str, char** endptr) +{ + return (long double)strtod(str, endptr); +} + +float strtof (const char* str, char** endptr) +{ + return (float)strtod(str, endptr); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/strtol.c b/programs/develop/ktcc/trunk/libc/stdlib/strtol.c new file mode 100644 index 0000000000..a9141520e3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/strtol.c @@ -0,0 +1,78 @@ +#include +#include +#include + +#define LONG_MIN (-2147483647L-1) +#define LONG_MAX (2147483647L) +#define ULONG_MAX (4294967295UL) + + +int getdigit(char ch, int base) +{ + if (isdigit(ch)) ch-= '0'; + else + if (isalpha(ch) && ch <= 'Z') ch = 10 + ch - 'A'; + else + if (isalpha(ch)) ch = 10 + ch - 'a'; + else + return -1; + + if (ch / base != 0) return -1; + + return ch; +} + + +long int strtol (const char* str, char** endptr, int base) +{ + long int res = 0; + int sign = 1; + + if (base > 36) + { + errno = EINVAL; + goto bye; + } + + while (isspace(*str)) str++; + + if (*str == '-') { sign = -1; str++; } + else + if (*str == '+') str++; + + if (base == 0 || base == 16) + { + if (*str == '0' && (str[1] == 'x' || str[1] == 'X')) + { + base = 16; + str += 2; + } + } + + if (base == 0 && *str == '0') base = 8; + + if (base == 0) base = 10; + + + int digit; + while ((digit = getdigit(*str, base)) >= 0) + { + res = base * res + digit; + str++; + if (res < 0) + { + errno = ERANGE; + if (sign > 0) + res = LONG_MAX; + else + res = LONG_MIN; + } + } + +bye: + if (endptr) + *endptr = (char*)str; + + return res * sign; +} + diff --git a/programs/develop/ktcc/trunk/libc/stdlib/time.c b/programs/develop/ktcc/trunk/libc/stdlib/time.c new file mode 100644 index 0000000000..87e77db061 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/time.c @@ -0,0 +1,86 @@ +#include +#include + +struct tm __buffertime; + + + +struct tm * localtime (const time_t * timer) +/* non-standard! ignore parameter and return just time now */ +{ + int kos_date, kos_time; + kos_date = _ksys_get_date(); + kos_time = _ksys_get_system_clock(); + + int bcd_day = (kos_date >> 16); + int bcd_mon = ((kos_date & 0xFF00) >> 8); + int bcd_year = (kos_date & 0xFF); + __buffertime.tm_mday = ((bcd_day & 0xF0)>>4)*10 + (bcd_day & 0x0F); + __buffertime.tm_mon = ((bcd_mon & 0xF0)>>4)*10 + (bcd_mon & 0x0F) - 1; + __buffertime.tm_year = ((bcd_year & 0xF0)>>4)*10 + (bcd_year & 0x0F) + 100; + + __buffertime.tm_wday = __buffertime.tm_yday = __buffertime.tm_isdst = -1; /* temporary */ + + int bcd_sec = (kos_time >> 16); + int bcd_min = ((kos_time & 0xFF00) >> 8); + int bcd_hour = (kos_time & 0xFF); + + __buffertime.tm_sec = ((bcd_sec & 0xF0)>>4)*10 + (bcd_sec & 0x0F); + __buffertime.tm_min = ((bcd_min & 0xF0)>>4)*10 + (bcd_min & 0x0F); + __buffertime.tm_hour = ((bcd_hour & 0xF0)>>4)*10 + (bcd_hour & 0x0F); + + return &__buffertime; +} + +time_t time (time_t* timer) +{ + time_t t = mktime(localtime(0)); + + if (timer) *timer = t; + + return t; +} + +time_t mktime (struct tm * timeptr) +{ + /*int y, m, d; + time_t t; + y = timeptr->tm_year + 1900; + m = timeptr->tm_mon + 1; + d = timeptr->tm_mday; // to -1 or not to -1? + + if (m < 3) { m += 12; y -= 1; } + + t = y * 365 + y / 4 + y /400 - y / 100; // years - > days + t += 30 * m + 3 * (m + 1) / 5 + d; // add month days + + t -= 719561; // 01 jan 1970 + t *= 86400; + + t += 3600 * timeptr->tm_hour + 60 * timeptr->tm_min + timeptr->tm_sec; + + return t;*/ + + int utcdiff = -3; + const int mon_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + unsigned long int tyears, tdays, leaps, utc_hrs; + int i; + + tyears = timeptr->tm_year - 70 ;// tm->tm_year is from 1900. + leaps = (tyears + 2) / 4; // no of next two lines until year 2100. + i = (timeptr->tm_year - 100) / 100; + leaps -= ( (i/4)*3 + i%4 ); + tdays = 0; + for (i=0; i < timeptr->tm_mon; i++) tdays += mon_days[i]; + + tdays += timeptr->tm_mday-1; // days of month passed. + tdays = tdays + (tyears * 365) + leaps; + + utc_hrs = timeptr->tm_hour + utcdiff; // for your time zone. + return (tdays * 86400) + (utc_hrs * 3600) + (timeptr->tm_min * 60) + timeptr->tm_sec; +} + +double difftime (time_t end, time_t beginning) +{ + return end - beginning; +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/tolower.c b/programs/develop/ktcc/trunk/libc/stdlib/tolower.c new file mode 100644 index 0000000000..75ad383f6e --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/tolower.c @@ -0,0 +1,9 @@ +#include +/* +** return lower-case of c if upper-case, else c +*/ +unsigned char tolower(unsigned char c) +{ + if(c<='Z' && c>='A') return (c+32); + return (c); +} diff --git a/programs/develop/ktcc/trunk/libc/stdlib/toupper.c b/programs/develop/ktcc/trunk/libc/stdlib/toupper.c new file mode 100644 index 0000000000..44f6c9afd7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/toupper.c @@ -0,0 +1,9 @@ +#include +/* +** return upper-case of c if it is lower-case, else c +*/ +unsigned char toupper(unsigned char c) +{ + if(c<='z' && c>='a') return (c-32); + return (c); +} diff --git a/programs/develop/ktcc/trunk/libc/string/calloc.c b/programs/develop/ktcc/trunk/libc/string/calloc.c new file mode 100644 index 0000000000..b524473683 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/calloc.c @@ -0,0 +1,13 @@ +#include +#include + +void* syscalloc (size_t num, size_t size) +{ + size_t bytes = num * size; + void *p = sysmalloc(bytes); + + if(p) + memset(p, 0, bytes); + + return p; +} diff --git a/programs/develop/ktcc/trunk/libc/string/is.c b/programs/develop/ktcc/trunk/libc/string/is.c new file mode 100644 index 0000000000..8abb9f0dca --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/is.c @@ -0,0 +1,20 @@ +#include +unsigned short __is[129] = { + 0, /* EOF */ + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x004, 0x104, 0x104, 0x104, 0x104, 0x104, 0x004, 0x004, + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x140, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, + 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, + 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, + 0x459, 0x459, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, + 0x0D0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253, + 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, + 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, + 0x253, 0x253, 0x253, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, + 0x0D0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073, + 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, + 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, + 0x073, 0x073, 0x073, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x004 + }; diff --git a/programs/develop/ktcc/trunk/libc/string/memchr.c b/programs/develop/ktcc/trunk/libc/string/memchr.c new file mode 100644 index 0000000000..f143e0e54a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/memchr.c @@ -0,0 +1,12 @@ +#include + +void* memchr(const void* buf,int c,size_t count) +{ + int i; + for (i=0;i + +typedef unsigned char uc; +int memcmp(const void* buf1,const void* buf2,size_t count) +{ + int i; + for (i=0;i*(uc*)buf2) + return 1; + (uc*)buf1++; + (uc*)buf2++; + } + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/string/memmove.asm b/programs/develop/ktcc/trunk/libc/string/memmove.asm new file mode 100644 index 0000000000..32a1989392 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/memmove.asm @@ -0,0 +1,53 @@ +format ELF + +section '.text' executable +include '../proc32.inc' + +public memcpy +public memmove + +proc memcpy c, to:dword,from:dword,count:dword + push esi + push edi + mov ecx,[count] + test ecx,ecx + jz no_copy_block + mov esi,[from] + mov edi,[to] + cld + rep movsb +no_copy_block: + + pop edi + pop esi + mov eax, [to] + ret +endp + +proc memmove c, to:dword,from:dword,count:dword + + push esi + push edi + mov ecx,[count] + test ecx,ecx + jz no_copy_block_ + mov esi,[from] + mov edi,[to] + cmp esi, edi + je no_copy_block_ + jg copy_ + add esi, ecx + add edi, ecx + dec esi + dec edi + std +copy_: + rep movsb + cld +no_copy_block_: + + pop edi + pop esi + mov eax,[to] + ret +endp diff --git a/programs/develop/ktcc/trunk/libc/string/memrchr.c b/programs/develop/ktcc/trunk/libc/string/memrchr.c new file mode 100644 index 0000000000..742693a1e5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/memrchr.c @@ -0,0 +1,12 @@ +/* memrchr.c from musl + */ + +#include + +void *memrchr(const void *m, int c, size_t n) +{ + const unsigned char *s = m; + c = (unsigned char)c; + while (n--) if (s[n]==c) return (void *)(s+n); + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/string/memset.asm b/programs/develop/ktcc/trunk/libc/string/memset.asm new file mode 100644 index 0000000000..3d73257e3b --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/memset.asm @@ -0,0 +1,15 @@ +format ELF +section '.text' executable +public memset +memset: + push edi + mov edi,[esp+8] + mov eax,[esp+12] + mov ecx,[esp+16] + jecxz .no_set + cld + rep stosb +.no_set: + mov eax, [esp+8] + pop edi + ret diff --git a/programs/develop/ktcc/trunk/libc/string/strcat.c b/programs/develop/ktcc/trunk/libc/string/strcat.c new file mode 100644 index 0000000000..eb2e181fe2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strcat.c @@ -0,0 +1,10 @@ +#include + +char* strcat(char* strDest, const char* strSource) +{ + char* res; + res=strDest; + while (*strDest) strDest++; + while ((*strDest++ = *strSource++)) ; + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strchr.c b/programs/develop/ktcc/trunk/libc/string/strchr.c new file mode 100644 index 0000000000..6895b62daf --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strchr.c @@ -0,0 +1,11 @@ +#include + +char* strchr(const char* string, int c) +{ + do { + if (*string == (char)c) + return (char*)string; + } while (*string++); + + return NULL; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strcmp.c b/programs/develop/ktcc/trunk/libc/string/strcmp.c new file mode 100644 index 0000000000..8fc46702de --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strcmp.c @@ -0,0 +1,16 @@ +#include + +int strcmp(const char* string1, const char* string2) +{ + while (1) + { + if (*string1<*string2) + return -1; + if (*string1>*string2) + return 1; + if (*string1=='\0') + return 0; + string1++; + string2++; + } +} diff --git a/programs/develop/ktcc/trunk/libc/string/strcoll.c b/programs/develop/ktcc/trunk/libc/string/strcoll.c new file mode 100644 index 0000000000..6e96af7833 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strcoll.c @@ -0,0 +1,6 @@ +#include + +int strcoll(const char* string1,const char* string2) +{ + return strcmp(string1,string2); +} diff --git a/programs/develop/ktcc/trunk/libc/string/strcpy.c b/programs/develop/ktcc/trunk/libc/string/strcpy.c new file mode 100644 index 0000000000..61aab01503 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strcpy.c @@ -0,0 +1,9 @@ +#include + +char* strcpy(char* strDest,const char* strSource) +{ + char* res; + res=strDest; + while((*strDest++ = *strSource++)) ; + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strcspn.c b/programs/develop/ktcc/trunk/libc/string/strcspn.c new file mode 100644 index 0000000000..31173d698e --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strcspn.c @@ -0,0 +1,20 @@ +#include + +size_t strcspn(const char* string, const char* strCharSet) +{ + const char* temp; + int i; + i=0; + while(*string) + { + temp=strCharSet; + while (*temp!='\0') + { + if (*string==*temp) + return i; + temp++; + } + i++;string++; + } + return i; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strdup.c b/programs/develop/ktcc/trunk/libc/string/strdup.c new file mode 100644 index 0000000000..8c831d814f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strdup.c @@ -0,0 +1,13 @@ +#include +#include + +char* strdup(const char* str) +{ + char* res; + int len; + len=strlen(str)+1; + res=malloc(len); + if(res) + memcpy(res,str,len); + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strerror.c b/programs/develop/ktcc/trunk/libc/string/strerror.c new file mode 100644 index 0000000000..58b8988fe3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strerror.c @@ -0,0 +1,105 @@ +#include +#include + +char* strerror(int err) +{ + char *msg; + switch(err) + { + case E_SUCCESS: + msg = "Success"; + break; + case -1: + msg = "End of file"; + break; + case E_UNSUPPORTED: + msg = "Function is not supported for the given file system"; + break; + case E_UNKNOWNFS: + msg = "Unknown file system"; + break; + case E_NOTFOUND: + msg = "File not found"; + break; + case E_EOF: + msg = "End of file, EOF"; + break; + case E_INVALIDPTR: + msg = "Pointer lies outside of application memory"; + break; + case E_DISKFULL: + msg = "Disk is full"; + break; + case E_FSYSERROR: + msg = "Dile system error"; + break; + case E_ACCESS: + msg = "Access denied"; + break; + case E_HARDWARE: + msg = "Device error"; + break; + case E_NOMEM: + msg = "File system requires more memory"; + break; + case E_NOMEM2: + msg = "Not enough memory"; + break; + case E_FILEFMT: + msg = "File is not executable"; + break; + case E_TOOMANY: + msg = "Too many processes"; + break; + /* Socket errors */ + case ENOBUFS: + msg = "Broken buffer"; + break; + case EINPROGRESS: + msg = "Operation now in progress"; + break; + case EOPNOTSUPP: + msg = "Operation not supported on transport endpoint"; + break; + case EWOULDBLOCK: + msg = "Operation would block"; + break; + case ENOTCONN: + msg = "Transport endpoint is not connected"; + break; + case EALREADY: + msg = "Operation already in progress"; + break; + case EINVALUE: + msg = "Invalid argument"; + break; + case EMSGSIZE: + msg = "Message too long"; + break; + case ENOMEM: + msg = "Out of memory"; + break; + case EADDRINUSE: + msg = "Address already in use"; + break; + case ECONNREFUSED: + msg = "Connection refused"; + break; + case ECONNRESET: + msg = "Connection reset by peer"; + break; + case EISCONN: + msg = "Transport endpoint is already connected"; + break; + case ETIMEDOUT: + msg = "Connection timed out"; + break; + case ECONNABORTED: + msg = "Software caused connection abort"; + break; + default: + msg = "Unknown error"; + break; + } + return msg; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strlen.c b/programs/develop/ktcc/trunk/libc/string/strlen.c new file mode 100644 index 0000000000..08914d3e5f --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strlen.c @@ -0,0 +1,7 @@ +int strlen(const char* string) +{ + int i; + i=0; + while (*string++) i++; + return i; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strnbrk.c b/programs/develop/ktcc/trunk/libc/string/strnbrk.c new file mode 100644 index 0000000000..86d1b290b8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strnbrk.c @@ -0,0 +1,18 @@ +#include + +char* strpbrk(const char* string, const char* strCharSet) +{ + const char* temp; + while (*string!='\0') + { + temp=strCharSet; + while (*temp!='\0') + { + if (*string==*temp) + return (char*)string; + temp++; + } + string++; + } + return (char*)0; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strncat.c b/programs/develop/ktcc/trunk/libc/string/strncat.c new file mode 100644 index 0000000000..47426c4dcf --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strncat.c @@ -0,0 +1,15 @@ +#include + +char* strncat(char* strDest,const char* strSource,size_t count) +{ + char* res; + res=strDest; + while (*strDest) strDest++; + while(count-- > 0) + { + if((*strDest++ = *strSource++)) continue; + return(res); + } + *strDest = 0; + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strncmp.c b/programs/develop/ktcc/trunk/libc/string/strncmp.c new file mode 100644 index 0000000000..03a40ffd68 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strncmp.c @@ -0,0 +1,14 @@ +#include + +int strncmp(const char* string1, const char* string2, size_t count) +{ + while(count>0 && (*string1==*string2)) + { + if ('\0' == *string1) return 0; + ++string1; + ++string2; + --count; + } + if(count) return (*string1 - *string2); + return 0; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strncpy.c b/programs/develop/ktcc/trunk/libc/string/strncpy.c new file mode 100644 index 0000000000..b6f658c5f7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strncpy.c @@ -0,0 +1,16 @@ +#include + +char* strncpy(char* strDest,const char* strSource,size_t count) +{ + char* res; + res=strDest; + while (count>0) + { + *strDest=*strSource; + if (*strSource!='\0') + strSource++; + strDest++; + count--; + } + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strrchr.c b/programs/develop/ktcc/trunk/libc/string/strrchr.c new file mode 100644 index 0000000000..9594db9fa4 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strrchr.c @@ -0,0 +1,16 @@ +#include + +char* strrchr(const char* s,int c) +{ + char* res; + res=(char*)0; + while (1) + { + if (*s==(char)c) + res=(char*)s; + if (*s=='\0') + break; + s++; + } + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strrev.c b/programs/develop/ktcc/trunk/libc/string/strrev.c new file mode 100644 index 0000000000..015ad0700c --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strrev.c @@ -0,0 +1,14 @@ +#include + +char* strrev(char *p) +{ + char *q = p, *res = p, z; + while(q && *q) ++q; /* find eos */ + for(--q; p < q; ++p, --q) + { + z = *p; + *p = *q; + *q = z; + } + return res; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strspn.c b/programs/develop/ktcc/trunk/libc/string/strspn.c new file mode 100644 index 0000000000..22a7fce6a1 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strspn.c @@ -0,0 +1,23 @@ +#include + +size_t strspn(const char* string,const char* strCharSet) +{ + int i; + const char* temp; + i=0; + while (*string!='\0') + { + temp=strCharSet; + while (*temp!='\0') + { + if (*temp==*string) + break; + temp++; + } + if (*temp=='\0') + break; + string++; + i++; + } + return i; +} diff --git a/programs/develop/ktcc/trunk/libc/string/strstr.c b/programs/develop/ktcc/trunk/libc/string/strstr.c new file mode 100644 index 0000000000..72fc2bde51 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strstr.c @@ -0,0 +1,14 @@ +#include + +char* strstr(const char* s, const char* find) +{ + int len; + len=strlen(find); + while (1) + { + if (strncmp(s,find,len)==0) return (char*)s; + if (*s=='\0') + return (char*) 0; + s++; + } +} diff --git a/programs/develop/ktcc/trunk/libc/string/strtok.c b/programs/develop/ktcc/trunk/libc/string/strtok.c new file mode 100644 index 0000000000..d9ae819aef --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strtok.c @@ -0,0 +1,25 @@ +#include + +char* strtok(char* s,const char* delim) +// non reentrant +{ + static char* savep; + char* res; + + if(s) + savep = NULL; + else + s = savep; + + if (*s == '\0') + return NULL; + s += strspn(s, delim); + if (*s == '\0') + return NULL; + res = s; + s += strcspn(s, delim); + savep = s + 1; + *s = '\0'; + return res; +} + diff --git a/programs/develop/ktcc/trunk/libc/string/strxfrm.c b/programs/develop/ktcc/trunk/libc/string/strxfrm.c new file mode 100644 index 0000000000..a7b7fe03f3 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strxfrm.c @@ -0,0 +1,4 @@ +int strxfrm(char* strDest, const char* strSource, int count) +{ + return 0; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/build.bat b/programs/develop/ktcc/trunk/libctest/build.bat new file mode 100644 index 0000000000..db078a7773 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/build.bat @@ -0,0 +1,68 @@ +@echo off +echo #################################################### +echo # test libc builder # +echo # usage: build [clean] # +echo #################################################### +rem #### CONFIG SECTION #### +set LIBNAME=libck.a +set INCLUDE=include +set CC=D:\VSProjects\msys-kos32-4.8.2\ktcc\trunk\libc\kos32-tcc.exe +set CFLAGS=-I"%cd%\%INCLUDE%" -Wall +set AR=kos32-ar +set ASM=fasm +set dirs=. +rem #### END OF CONFIG SECTION #### + +set objs= +set target=%1 +if not "%1"=="clean" set target=all + +set INCLUDE="%cd%" +call :Target_%target% + +if ERRORLEVEL 0 goto Exit_OK + +echo Probably at runing has been created error +echo For help send a report... +pause +goto :eof + +:Compile_C + %CC% %CFLAGS% %1 -o "%~dpn1.kex" -lck + if not %errorlevel%==0 goto Error_Failed + set objs=%objs% "%~dpn1.o" +goto :eof + +:Compile_Asm + %ASM% %1 "%~dpn1.o" + if not %errorlevel%==0 goto Error_Failed + set objs=%objs% "%~dpn1.o" +goto :eof + +:Target_clean + echo cleaning ... + for %%a in (%dirs%) do del /Q "%%a\*.o" + for %%a in (%dirs%) do del /Q "%%a\*.kex" +goto :Exit_OK + +:Target_all + echo building all ... + for %%a in (%dirs%) do ( + for %%f in ("%%a\*.asm") do call :Compile_Asm "%%f" + for %%f in ("%%a\*.c") do call :Compile_C "%%f" + ) +:: %AR% -ru %LIBNAME% %objs% +:: if not %errorlevel%==0 goto Error_Failed +goto Exit_OK + +:Error_Failed +echo error: execution failed +pause +exit 1 + +:Exit_OK +echo #################################################### +echo # All operations has been done... # +echo #################################################### +pause +exit 0 \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/float2.c b/programs/develop/ktcc/trunk/libctest/float2.c new file mode 100644 index 0000000000..11e42ab0f7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/float2.c @@ -0,0 +1,42 @@ +#include +#include + +main() +{ + int i; + for (i = 0; i < 10; i++) + { + printf("------------------------------------------------------\n"); + printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) ); + printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) ); +//remainder of 5.3 / 2 is -0.700000 +//remainder of 18.5 / 4.2 is 1.700000 + + printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) ); + printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) ); +// fmod of 5.3 / 2 is 1.300000 +// fmod of 18.5 / 4.2 is 1.700000 + + double param, fractpart, intpart, result; + int n; + + param = 3.14159265; + fractpart = modf (param , &intpart); + printf ("%f = %f + %f \n", param, intpart, fractpart); +//3.141593 = 3.000000 + 0.141593 + + param = 0.95; + n = 4; + result = ldexp (param , n); + printf ("%f * 2^%d = %f\n", param, n, result); +//0.950000 * 2^4 = 15.200000 + + param = 8.0; + result = frexp (param , &n); + printf ("%f = %f * 2^%d\n", param, result, n); +//8.000000 = 0.500000 * 2^4 + param = 50; + result = frexp (param , &n); + printf ("%f = %f * 2^%d\n", param, result, n); + } +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/fscanf.c b/programs/develop/ktcc/trunk/libctest/fscanf.c new file mode 100644 index 0000000000..4646a481a6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/fscanf.c @@ -0,0 +1,125 @@ +#include +#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + errno=0, ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +static FILE *writetemp(const char *data) +{ + FILE *f = fopen("_tmpfile.tmp", "w+"); + if (!f) return 0; + if (!fwrite(data, strlen(data), 1, f)) { + fclose(f); + return 0; + } + rewind(f); + return f; +} + +int main(void) +{ + int i, x, y; + double u; + char a[100], b[100]; + FILE *f; + + + TEST(i, !!(f=writetemp("hello, world")), 1, "failed to make temp file"); + if (f) { + TEST(i, fscanf(f, "%s %[own]", a, b), 2, "got %d fields, expected %d"); + TEST_S(a, "hello,", "wrong result for %s"); + TEST_S(b, "wo", "wrong result for %[own]"); + TEST(i, fgetc(f), 'r', "'%c' != '%c') (%s)"); + fclose(f); + } + + TEST(i, !!(f=writetemp("ld 0x12 0x34")), 1, "failed to make temp file"); + if (f) { + TEST(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "got %d fields, expected %d"); + TEST(i, x, 0x12, "%d != %d"); + TEST(i, fgetc(f), '3', "'%c' != '%c'"); + fclose(f); + } + + TEST(i, !!(f=writetemp(" 42")), 1, "failed to make temp file"); + if (f) { + x=y=-1; + TEST(i, fscanf(f, " %n%*d%n", &x, &y), 0, "%d != %d"); + TEST(i, x, 6, "%d != %d"); + TEST(i, y, 8, "%d != %d"); + TEST(i, ftell(f), 8, "%d != %d"); + TEST(i, !!feof(f), 1, "%d != %d"); + fclose(f); + } + + TEST(i, !!(f=writetemp("[abc123]....x")), 1, "failed to make temp file"); + if (f) { + x=y=-1; + TEST(i, fscanf(f, "%10[^]]%n%10[].]%n", a, &x, b, &y), 2, "%d != %d"); + TEST_S(a, "[abc123", "wrong result for %[^]]"); + TEST_S(b, "]....", "wrong result for %[].]"); + TEST(i, x, 7, "%d != %d"); + TEST(i, y, 12, "%d != %d"); + TEST(i, ftell(f), 12, "%d != %d"); + TEST(i, feof(f), 0, "%d != %d"); + TEST(i, fgetc(f), 'x', "%d != %d"); + fclose(f); + } + + TEST(i, !!(f=writetemp("0x1.0p12")), 1, "failed to make temp file"); + if (f) { + x=y=-1; + u=-1; + TEST(i, fscanf(f, "%lf%n %d", &u, &x, &y), 1, "%d != %d"); + TEST(u, u, 0.0, "%g != %g"); + TEST(i, x, 1, "%d != %d"); + TEST(i, y, -1, "%d != %d"); + TEST(i, ftell(f), 1, "%d != %d"); + TEST(i, feof(f), 0, "%d != %d"); + TEST(i, fgetc(f), 'x', "%d != %d"); + rewind(f); + TEST(i, fgetc(f), '0', "%d != %d"); + TEST(i, fgetc(f), 'x', "%d != %d"); + TEST(i, fscanf(f, "%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d"); + TEST(u, u, 1.0, "%g != %g"); + TEST(i, x, 3, "%d != %d"); + TEST(i, a[0], 'p', "%d != %d"); + TEST(i, y, 12, "%d != %d"); + TEST(i, ftell(f), 8, "%d != %d"); + TEST(i, !!feof(f), 1, "%d != %d"); + fclose(f); + } + + TEST(i, !!(f=writetemp("1.0 012")), 1, "failed to make temp file"); + if (f) { + x=y=-1; + u=-1; + TEST(i, fscanf(f, "%lf%n %i", &u, &x, &y), 2, "%d != %d"); + TEST(u, u, 1.0, "%g != %g"); + TEST(i, x, 3, "%d != %d"); + TEST(i, y, 10, "%d != %d"); + TEST(i, ftell(f), 13, "%d != %d"); + TEST(i, !!feof(f), 1, "%d != %d"); + fclose(f); + } + + TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file"); + if (f) { + x=y=-1; + TEST(i, fscanf(f, "%x%n", &x, &y), 0, "%d != %d"); + TEST(i, x, -1, "%d != %d"); + TEST(i, y, -1, "%d != %d"); + TEST(i, ftell(f), 2, "%d != %d"); + TEST(i, feof(f), 0, "%d != %d"); + fclose(f); + } + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/memmove.c b/programs/develop/ktcc/trunk/libctest/memmove.c new file mode 100644 index 0000000000..789332d395 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/memmove.c @@ -0,0 +1,186 @@ +/* A minor test-program for memmove. + Copyright (C) 2005 Axis Communications. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Neither the name of Axis Communications nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS + COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ + +/* Test moves of 0..MAX bytes; overlapping-src-higher, + overlapping-src-lower and non-overlapping. The overlap varies with + 1..N where N is the size moved. This means an order of MAX**2 + iterations. The size of an octet may seem appropriate for MAX and + makes an upper limit for simple testing. For the CRIS simulator, + making this 256 added 90s to the test-run (2GHz P4) while 64 (4s) was + enough to spot the bugs that had crept in, hence the number chosen. */ +#define MAX 64 + +#include +#include +#include + +#define TOO_MANY_ERRORS 11 +int errors = 0; + +#define DEBUGP \ + if (errors == TOO_MANY_ERRORS) \ + printf ("Further errors omitted\n"); \ + else if (errors < TOO_MANY_ERRORS) \ + printf + +/* A safe target-independent memmove. */ + +void +mymemmove (unsigned char *dest, unsigned char *src, size_t n) +{ + size_t i; + + if ((src <= dest && src + n <= dest) + || src >= dest) + while (n-- > 0) + *dest++ = *src++; + else + { + dest += n; + src += n; + while (n-- > 0) + *--dest = *--src; + } +} + +/* It's either the noinline attribute or forcing the test framework to + pass -fno-builtin-memmove. */ +void +xmemmove (unsigned char *dest, unsigned char *src, size_t n) + __attribute__ ((__noinline__)); + +void +xmemmove (unsigned char *dest, unsigned char *src, size_t n) +{ + void *retp; + retp = memmove (dest, src, n); + + if (retp != dest) + { + errors++; + DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n", + retp, dest); + } +} + + +/* Fill the array with something we can associate with a position, but + not exactly the same as the position index. */ + +void +fill (unsigned char dest[MAX*3]) +{ + size_t i; + for (i = 0; i < MAX*3; i++) + dest[i] = (10 + i) % MAX; +} + +int +main (void) +{ + size_t i; + int errors = 0; + + /* Leave some room before and after the area tested, so we can detect + overwrites of up to N bytes, N being the amount tested. If you + want to test using valgrind, make these malloced instead. */ + unsigned char from_test[MAX*3]; + unsigned char to_test[MAX*3]; + unsigned char from_known[MAX*3]; + unsigned char to_known[MAX*3]; + + /* Non-overlap. */ + for (i = 0; i < MAX; i++) + { + /* Do the memmove first before setting the known array, so we know + it didn't change any of the known array. */ + fill (from_test); + fill (to_test); + xmemmove (to_test + MAX, 1 + from_test + MAX, i); + + fill (from_known); + fill (to_known); + mymemmove (to_known + MAX, 1 + from_known + MAX, i); + + if (memcmp (to_known, to_test, sizeof (to_known)) != 0) + { + errors++; + DEBUGP ("memmove failed non-overlap test for %d bytes\n", i); + } + } + + /* Overlap-from-before. */ + for (i = 0; i < MAX; i++) + { + size_t j; + for (j = 0; j < i; j++) + { + fill (to_test); + xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i); + + fill (to_known); + mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i); + + if (memcmp (to_known, to_test, sizeof (to_known)) != 0) + { + errors++; + DEBUGP ("memmove failed for %d bytes," + " with src %d bytes before dest\n", + i, j); + } + } + } + + /* Overlap-from-after. */ + for (i = 0; i < MAX; i++) + { + size_t j; + for (j = 0; j < i; j++) + { + fill (to_test); + xmemmove (to_test + MAX, to_test + MAX + j, i); + + fill (to_known); + mymemmove (to_known + MAX, to_known + MAX + j, i); + + if (memcmp (to_known, to_test, sizeof (to_known)) != 0) + { + errors++; + DEBUGP ("memmove failed when moving %d bytes," + " with src %d bytes after dest\n", + i, j); + } + } + } + +printf("%s finished\n", __FILE__); + + if (errors != 0) + abort (); + exit (0); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/myfile.txt b/programs/develop/ktcc/trunk/libctest/myfile.txt new file mode 100644 index 0000000000..980fb7b2a5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/myfile.txt @@ -0,0 +1,5 @@ +#test line 1; +-test line 2; +@test line 3; +#testline4. +# \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/nulprintf.c b/programs/develop/ktcc/trunk/libctest/nulprintf.c new file mode 100644 index 0000000000..d2ba76e507 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/nulprintf.c @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2014 by ARM Ltd. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include +#include + +const char m[8] = {'M','M','M','M','M','M','M','M'}; + +int main() +{ + printf ("%.*s\n", 8, m); // must print MMMMMMMM + +printf("%s finished\n", __FILE__); + + exit (0); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/print.inc b/programs/develop/ktcc/trunk/libctest/print.inc new file mode 100644 index 0000000000..f07a6ed1ff --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/print.inc @@ -0,0 +1,28 @@ +#include +#include +//#include +//#include "test.h" + +volatile int t_status = 0; + +int t_printf(const char *s, ...) +{ + va_list ap; + char buf[512]; + int n; + + t_status = 1; + va_start(ap, s); + n = vsnprintf(buf, sizeof buf, s, ap); + va_end(ap); + if (n < 0) + n = 0; + else if (n >= sizeof buf) { + n = sizeof buf; + buf[n - 1] = '0'; + buf[n - 2] = '.'; + buf[n - 3] = '.'; + buf[n - 4] = '.'; + } + return printf("%s\n", buf); +} diff --git a/programs/develop/ktcc/trunk/libctest/qsort.c b/programs/develop/ktcc/trunk/libctest/qsort.c new file mode 100644 index 0000000000..c150c36a9a --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/qsort.c @@ -0,0 +1,173 @@ +//#include +#include +#include +#include +#include "test.h" + +static int scmp(const void *a, const void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} + +static int icmp(const void *a, const void *b) +{ + return *(int*)a - *(int*)b; +} + +static int ccmp(const void *a, const void *b) +{ + return *(char*)a - *(char*)b; +} + +static int cmp64(const void *a, const void *b) +{ + const uint64_t *ua = a, *ub = b; + return *ua < *ub ? -1 : *ua != *ub; +} + +/* 26 items -- even */ +static const char *s[] = { + "Bob", "Alice", "John", "Ceres", + "Helga", "Drepper", "Emeralda", "Zoran", + "Momo", "Frank", "Pema", "Xavier", + "Yeva", "Gedun", "Irina", "Nono", + "Wiener", "Vincent", "Tsering", "Karnica", + "Lulu", "Quincy", "Osama", "Riley", + "Ursula", "Sam" +}; +static const char *s_sorted[] = { + "Alice", "Bob", "Ceres", "Drepper", + "Emeralda", "Frank", "Gedun", "Helga", + "Irina", "John", "Karnica", "Lulu", + "Momo", "Nono", "Osama", "Pema", + "Quincy", "Riley", "Sam", "Tsering", + "Ursula", "Vincent", "Wiener", "Xavier", + "Yeva", "Zoran" +}; + +/* 23 items -- odd, prime */ +static int n[] = { + 879045, 394, 99405644, 33434, 232323, 4334, 5454, + 343, 45545, 454, 324, 22, 34344, 233, 45345, 343, + 848405, 3434, 3434344, 3535, 93994, 2230404, 4334 +}; +static int n_sorted[] = { + 22, 233, 324, 343, 343, 394, 454, 3434, + 3535, 4334, 4334, 5454, 33434, 34344, 45345, 45545, + 93994, 232323, 848405, 879045, 2230404, 3434344, 99405644 +}; + +static void str_test(const char **a, const char **a_sorted, int len) +{ + int i; + qsort(a, len, sizeof *a, scmp); + for (i=0; i + +// TODO: use large period prng +static uint64_t seed = -1; +static uint32_t rand32(void) +{ + seed = 6364136223846793005ULL*seed + 1; + return seed >> 32; +} +static uint64_t rand64(void) +{ + uint64_t u = rand32(); + return u<<32 | rand32(); +} +static double frand() +{ + return rand64() * 0x1p-64; +} +static float frandf() +{ + return rand32() * 0x1p-32f; +} +static long double frandl() +{ + return rand64() * 0x1p-64L +#if LDBL_MANT_DIG > 64 ++ rand64() * 0x1p-128L +#endif +; +} + +void t_randseed(uint64_t s) +{ + seed = s; +} + +/* uniform random in [0,n), n > 0 must hold */ +uint64_t t_randn(uint64_t n) +{ + uint64_t r, m; + + /* m is the largest multiple of n */ + m = -1; + m -= m%n; + while ((r = rand64()) >= m); + return r%n; +} + +/* uniform on [a,b], a <= b must hold */ +uint64_t t_randint(uint64_t a, uint64_t b) +{ + uint64_t n = b - a + 1; + if (n) + return a + t_randn(n); + return rand64(); +} + +/* shuffle the elements of p and q until the elements in p are well shuffled */ +static void shuffle2(uint64_t *p, uint64_t *q, size_t np, size_t nq) +{ + size_t r; + uint64_t t; + + while (np) { + r = t_randn(nq+np--); + t = p[np]; + if (r < nq) { + p[np] = q[r]; + q[r] = t; + } else { + p[np] = p[r-nq]; + p[r-nq] = t; + } + } +} + +/* shuffle the elements of p */ +void t_shuffle(uint64_t *p, size_t n) +{ + shuffle2(p,0,n,0); +} + +void t_randrange(uint64_t *p, size_t n) +{ + size_t i; + for (i = 0; i < n; i++) + p[i] = i; + t_shuffle(p, n); +} + +/* hash table insert, 0 means empty, v > 0 must hold, len is power-of-2 */ +static int insert(uint64_t *tab, size_t len, uint64_t v) +{ + size_t i = v & (len-1); + size_t j = 1; + + while (tab[i]) { + if (tab[i] == v) + return -1; + i += j++; + i &= len-1; + } + tab[i] = v; + return 0; +} + +/* choose k unique numbers from [0,n), k <= n */ +int t_choose(uint64_t n, size_t k, uint64_t *p) +{ + uint64_t *tab; + size_t i, j, len; + + if (n < k) + return -1; + + if (n < 16) { + /* no alloc */ + while (k) + if (t_randn(n--) < k) + p[--k] = n; + return 0; + } + + if (k < 8) { + /* no alloc, n > 15 > 2*k */ + for (i = 0; i < k;) { + p[i] = t_randn(n); + for (j = 0; p[j] != p[i]; j++); + if (j == i) + i++; + } + return 0; + } + + // TODO: if k < n/k use k*log(k) solution without alloc + + if (n < 5*k && (n-k)*sizeof *tab < (size_t)-1) { + /* allocation is n-k < 4*k */ + tab = malloc((n-k) * sizeof *tab); + if (!tab) + return -1; + for (i = 0; i < k; i++) + p[i] = i; + for (; i < n; i++) + tab[i-k] = i; + if (k < n-k) + shuffle2(p, tab, k, n-k); + else + shuffle2(tab, p, n-k, k); + free(tab); + return 0; + } + + /* allocation is 2*k <= len < 4*k */ + for (len = 16; len < 2*k; len *= 2); + tab = calloc(len, sizeof *tab); + if (!tab) + return -1; + for (i = 0; i < k; i++) + while (insert(tab, len, t_randn(n)+1)); + for (i = 0; i < len; i++) + if (tab[i]) + *p++ = tab[i]-1; + free(tab); + return 0; +} + diff --git a/programs/develop/ktcc/trunk/libctest/readme.txt b/programs/develop/ktcc/trunk/libctest/readme.txt new file mode 100644 index 0000000000..f2591ad630 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/readme.txt @@ -0,0 +1,34 @@ +most test adapted from "musl-libctest-master" project +some taken from newlib + +Status or libc tests + +---NOT TESTED--- +no library fns realized +qsort +strtol +time + +---HANG--- +sscanf +>TEST_F(0x1234p56) + + +---STACK IS SMALL--- +strtod_long +tstring + + +--other-- +fscanf +-?scanf ignores width specs, '*' and [chars], cant read %a float +-%n counts as parameter + +snprintf +-some format misturbances + +ungetc +-ungetc fails if filepos == 0 - no tricks + +all file ops limited to 2Gb + diff --git a/programs/develop/ktcc/trunk/libctest/snprintf.c b/programs/develop/ktcc/trunk/libctest/snprintf.c new file mode 100644 index 0000000000..49ec73d38f --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/snprintf.c @@ -0,0 +1,188 @@ +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 +#endif +#include +#include +//#include +//#include +#include +#include "test.h" + +#define DISABLE_SLOW_TESTS + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +static const struct { + const char *fmt; + int i; + const char *expect; +} int_tests[] = { + /* width, precision, alignment */ + { "%04d", 12, "0012" }, + { "%.3d", 12, "012" }, + { "%3d", 12, " 12" }, + { "%-3d", 12, "12 " }, + { "%+3d", 12, "+12" }, + { "%+-5d", 12, "+12 " }, + { "%+- 5d", 12, "+12 " }, + { "%- 5d", 12, " 12 " }, + { "% d", 12, " 12" }, + { "%0-5d", 12, "12 " }, + { "%-05d", 12, "12 " }, + + /* ...explicit precision of 0 shall be no characters. */ + { "%.0d", 0, "" }, + { "%.0o", 0, "" }, + { "%#.0d", 0, "" }, + { "%#.0o", 0, "" }, + { "%#.0x", 0, "" }, + + /* ...but it still has to honor width and flags. */ + { "%2.0u", 0, " " }, + { "%02.0u", 0, " " }, + { "%2.0d", 0, " " }, + { "%02.0d", 0, " " }, + { "% .0d", 0, " " }, + { "%+.0d", 0, "+" }, + + /* hex: test alt form and case */ + { "%x", 63, "3f" }, + { "%#x", 63, "0x3f" }, + { "%X", 63, "3F" }, + + /* octal: test alt form */ + { "%o", 15, "17" }, + { "%#o", 15, "017" }, + + { NULL, 0.0, NULL } +}; + +static const struct { + const char *fmt; + double f; + const char *expect; +} fp_tests[] = { + /* basic form, handling of exponent/precision for 0 */ + { "%a", 0.0, "0x0p+0" }, + { "%e", 0.0, "0.000000e+00" }, + { "%f", 0.0, "0.000000" }, + { "%g", 0.0, "0" }, + { "%#g", 0.0, "0.00000" }, + { "%la", 0.0, "0x0p+0" }, + { "%le", 0.0, "0.000000e+00" }, + { "%lf", 0.0, "0.000000" }, + { "%lg", 0.0, "0" }, + { "%#lg", 0.0, "0.00000" }, + + /* rounding */ + { "%f", 1.1, "1.100000" }, + { "%f", 1.2, "1.200000" }, + { "%f", 1.3, "1.300000" }, + { "%f", 1.4, "1.400000" }, + { "%f", 1.5, "1.500000" }, + { "%.4f", 1.06125, "1.0613" }, /* input is not representible exactly as double */ + { "%.4f", 1.03125, "1.0312" }, /* 0x1.08p0 */ + { "%.2f", 1.375, "1.38" }, + { "%.1f", 1.375, "1.4" }, + { "%.1lf", 1.375, "1.4" }, + { "%.15f", 1.1, "1.100000000000000" }, + { "%.16f", 1.1, "1.1000000000000001" }, + { "%.17f", 1.1, "1.10000000000000009" }, + { "%.2e", 1500001.0, "1.50e+06" }, + { "%.2e", 1505000.0, "1.50e+06" }, + { "%.2e", 1505000.00000095367431640625, "1.51e+06" }, + { "%.2e", 1505001.0, "1.51e+06" }, + { "%.2e", 1506000.0, "1.51e+06" }, + + /* correctness in DBL_DIG places */ + { "%.15g", 1.23456789012345, "1.23456789012345" }, + + /* correct choice of notation for %g */ + { "%g", 0.0001, "0.0001" }, + { "%g", 0.00001, "1e-05" }, + { "%g", 123456, "123456" }, + { "%g", 1234567, "1.23457e+06" }, + { "%.7g", 1234567, "1234567" }, + { "%.7g", 12345678, "1.234568e+07" }, + { "%.8g", 0.1, "0.1" }, + { "%.9g", 0.1, "0.1" }, + { "%.10g", 0.1, "0.1" }, + { "%.11g", 0.1, "0.1" }, + + /* pi in double precision, printed to a few extra places */ + { "%.15f", M_PI, "3.141592653589793" }, + { "%.18f", M_PI, "3.141592653589793116" }, + + /* exact conversion of large integers */ + { "%.0f", 340282366920938463463374607431768211456.0, + "340282366920938463463374607431768211456" }, + + { NULL, 0.0, NULL } +}; + +int main(void) +{ + int i, j, k; + char b[2000]; + + TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d"); + TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d"); + TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d"); + + strcpy(b, "xxxxxxxx"); + TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d"); + TEST_S(b, "123", "incorrect output"); + TEST(i, b[5], 'x', "buffer overrun"); + + /* Perform ascii arithmetic to test printing tiny doubles */ + TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d"); + b[1] = '0'; + for (i=0; i<1021; i++) { + for (k=0, j=1023; j>0; j--) { + if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0; + else b[j]+=b[j]-'0'-10+k, k=1; + } + } + TEST(i, b[1], '1', "'%c' != '%c'"); + for (j=2; b[j]=='0'; j++); + TEST(i, j, 1024, "%d != %d"); + + +#ifndef DISABLE_SLOW_TESTS + errno = 0; + TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d"); + TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d"); + TEST(i, errno, EOVERFLOW, "after overflow: %d != %d"); +#endif + for (j=0; int_tests[j].fmt; j++) { + i = snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i); + if (i != strlen(int_tests[j].expect)) { + t_error("snprintf(b, sizeof b, \"%s\", %d) returned %d wanted %d\n", + int_tests[j].fmt, int_tests[j].i, i, strlen(int_tests[j].expect)); + } + if (strcmp(b, int_tests[j].expect) != 0) + t_error("bad integer conversion fmt[%s]: got \"%s\", want \"%s\"\n", int_tests[j].fmt, b, int_tests[j].expect); + } + + for (j=0; fp_tests[j].fmt; j++) { + i = snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f); + if (i != strlen(fp_tests[j].expect)) { + t_error("snprintf(b, sizeof b, \"%s\", %f) returned %d wanted %d\n", + fp_tests[j].fmt, fp_tests[j].f, i, strlen(fp_tests[j].expect)); + } + if (strcmp(b, fp_tests[j].expect) != 0) + t_error("bad floating-point conversion: got \"%s\", want \"%s\"\n", b, fp_tests[j].expect); + } + + TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d"); + + printf("%s finished\n", __FILE__); + + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/sscanf.c b/programs/develop/ktcc/trunk/libctest/sscanf.c new file mode 100644 index 0000000000..c748bd753e --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/sscanf.c @@ -0,0 +1,89 @@ +#include +#include +#include +//#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +#define TEST_F(x) ( \ + TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \ + TEST(t, d, (double)x, "%g != %g") ) + +int main(void) +{ + int i; + char a[100], b[100]; + int x, y, z, u, v; + double d, t; +/* + TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hello,", ""); + TEST_S(b, "world", ""); + + TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hell", ""); + TEST_S(b, "o,", ""); + + TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d"); + TEST_S(a, "hell", ""); + TEST_S(b, "o,", ""); + + a[8] = 'X'; + a[9] = 0; + TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d"); + TEST_S(a, "hello, wX", ""); +*/ + TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d"); + TEST(i, x, 56, "%d != %d"); + TEST(i, y, 789, "%d != %d"); + TEST_S(a, "56", ""); + + TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d"); + TEST(i, x, 9, "%d != %d"); + TEST(i, y, 256, "%d != %d"); + TEST(i, z, 9, "%d != %d"); + TEST(i, u, 256, "%d != %d"); + TEST(i, v, 256, "%d != %d"); + + TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d"); + TEST(i, x, 20, "%d != %d"); + + TEST(i, sscanf("xyz", "%d %d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); + + TEST(i, sscanf("", "%d %d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); + + TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d"); + TEST(i, x, 12, "%d != %d"); + TEST(i, y, 345, "%d != %d"); + TEST(i, z, 6, "%d != %d"); + + TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d"); + TEST(i, x, 0x12, "%d != %d"); + + TEST_F(123); + TEST_F(123.0); + TEST_F(123.0e+0); + TEST_F(123.0e+4); + TEST_F(1.234e1234); + TEST_F(1.234e-1234); + TEST_F(1.234e56789); + TEST_F(1.234e-56789); + TEST_F(-0.5); + TEST_F(0.1); + TEST_F(0.2); + TEST_F(0.1e-10); +// TEST_F(0x1234p56); hangs on + + TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)"); + TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)"); + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string.c b/programs/develop/ktcc/trunk/libctest/string.c new file mode 100644 index 0000000000..22b469a7a2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string.c @@ -0,0 +1,147 @@ +#define _BSD_SOURCE +#include +#include +#include +#include "test.h" + +#ifndef min +#define min(a,b) ((a 0 && nsrc >= size) + dst[size - 1] = '\0'; + + return nsrc; +} + +size_t strlcat(char *dst, const char *src, size_t size) +{ + int ndest = strlen(dst); + int nsrc = strlen(src); + if (size > ndest + 1) + { + strncat(dst, src, size - ndest - 1); + if (size > 0 && nsrc + ndest >= size) + dst[size - 1] = '\0'; + } + + return nsrc + ndest; +} + + + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +int main(void) +{ + char b[32]; + char *s; + int i; + + b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0; + TEST(s, strcpy(b, b+16), b, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p"); + TEST_S(s, "abc", "strcpy gave incorrect string"); + + TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p"); + TEST_S(s, "bc", "strcpy gave incorrect string"); + TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p"); + TEST_S(s, "c", "strcpy gave incorrect string"); + TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p"); + TEST_S(s, "", "strcpy gave incorrect string"); + + TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p"); + TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p"); + TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest"); + TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)"); + + b[3] = 'x'; b[4] = 0; + strncpy(b, "abc", 3); + TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu"); + TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu"); + + TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n"); + TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte"); + + strcpy(b, "abc"); + TEST(s, strncat(b, "123456", 3), b, "%p != %p"); + TEST(i, b[6], 0, "strncat failed to null-terminate (%d)"); + TEST_S(s, "abc123", "strncat gave incorrect string"); + + strcpy(b, "aaababccdd0001122223"); + TEST(s, strchr(b, 'b'), b+3, "%p != %p"); + TEST(s, strrchr(b, 'b'), b+5, "%p != %p"); + TEST(i, strspn(b, "abcd"), 10, "%d != %d"); + TEST(i, strcspn(b, "0123"), 10, "%d != %d"); + TEST(s, strpbrk(b, "0123"), b+10, "%d != %d"); + + strcpy(b, "abc 123; xyz; foo"); + TEST(s, strtok(b, " "), b, "%p != %p"); + TEST_S(s, "abc", "strtok result"); + + TEST(s, strtok(NULL, ";"), b+4, "%p != %p"); + TEST_S(s, " 123", "strtok result"); + + TEST(s, strtok(NULL, " ;"), b+11, "%p != %p"); + TEST_S(s, "xyz", "strtok result"); + + TEST(s, strtok(NULL, " ;"), b+16, "%p != %p"); + TEST_S(s, "foo", "strtok result"); + + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d"); + TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)"); + TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)"); + + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d"); + TEST(i, b[0], 'a', "strlcpy did not copy character %d"); + TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)"); + + memset(b, 'x', sizeof b); + TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d"); + TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)"); + + TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d"); + TEST_S(b, "abc123", "strlcat result"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 6), 6, "length %d != %d"); + TEST_S(b, "abc12", "strlcat result"); + TEST(i, b[6], 'x', "strlcat wrote past string %d != %d"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 4), 6, "length %d != %d"); + TEST_S(b, "abc", "strlcat result"); + + memcpy(b, "abc\0\0\0x\0", 8); + TEST(i, strlcat(b, "123", 3), 6, "length %d != %d"); + TEST_S(b, "abc", "strlcat result"); + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string_memcpy.c b/programs/develop/ktcc/trunk/libctest/string_memcpy.c new file mode 100644 index 0000000000..6500f7a1a7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string_memcpy.c @@ -0,0 +1,56 @@ +#include +#include +//#include +#include "test.h" + +static char buf[512]; + +static void *(*volatile pmemcpy)(void *restrict, const void *restrict2, size_t); + +static void *aligned(void *p) { + return (void*)(((uintptr_t)p + 63) & -64U); +} + +#define N 80 +static void test_align(int dalign, int salign, int len) +{ + char *src = aligned(buf); + char *dst = aligned(buf + 128); + char *want = aligned(buf + 256); + char *p; + int i; + + if (salign + len > N || dalign + len > N) + abort(); + for (i = 0; i < N; i++) { + src[i] = '#'; + dst[i] = want[i] = ' '; + } + for (i = 0; i < len; i++) + src[salign+i] = want[dalign+i] = '0'+i; + p = pmemcpy(dst+dalign, src+salign, len); + if (p != dst+dalign) + t_error("memcpy(%p,...) returned %p\n", dst+dalign, p); + for (i = 0; i < N; i++) + if (dst[i] != want[i]) { + t_error("memcpy(align %d, align %d, %d) failed\n", dalign, salign, len); + t_printf("got : %.*s\n", dalign+len+1, dst); + t_printf("want: %.*s\n", dalign+len+1, want); + break; + } +} + +int main(void) +{ + int i,j,k; + + pmemcpy = memcpy; + + for (i = 0; i < 16; i++) + for (j = 0; j < 16; j++) + for (k = 0; k < 64; k++) + test_align(i,j,k); + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string_memset.c b/programs/develop/ktcc/trunk/libctest/string_memset.c new file mode 100644 index 0000000000..e2c93f1399 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string_memset.c @@ -0,0 +1,71 @@ +#include +#include +//#include +#include "test.h" + +static char buf[512]; + +static void *(*volatile pmemset)(void *, int, size_t); + +static void *aligned(void *p) +{ + return (void*)(((uintptr_t)p + 63) & -64U); +} + +#define N 80 +static void test_align(int align, int len) +{ + char *s = aligned(buf); + char *want = aligned(buf + 256); + char *p; + int i; + + if (align + len > N) + abort(); + for (i = 0; i < N; i++) + s[i] = want[i] = ' '; + for (i = 0; i < len; i++) + want[align+i] = '#'; + p = pmemset(s+align, '#', len); + if (p != s+align) + t_error("memset(%p,...) returned %p\n", s+align, p); + for (i = 0; i < N; i++) + if (s[i] != want[i]) { + t_error("memset(align %d, '#', %d) failed\n", align, len); + t_printf("got : %.*s\n", align+len+1, s); + t_printf("want: %.*s\n", align+len+1, want); + break; + } +} + +static void test_value(int c) +{ + int i; + + pmemset(buf, c, 10); + for (i = 0; i < 10; i++) + if ((unsigned char)buf[i] != (unsigned char)c) { + t_error("memset(%d) failed: got %d\n", c, buf[i]); + break; + } +} + +int main(void) +{ + int i,j,k; + + pmemset = memset; + + for (i = 0; i < 16; i++) + for (j = 0; j < 64; j++) + test_align(i,j); + + test_value('c'); + test_value(0); + test_value(-1); + test_value(-5); + test_value(0xab); + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string_strchr.c b/programs/develop/ktcc/trunk/libctest/string_strchr.c new file mode 100644 index 0000000000..b687a162fe --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string_strchr.c @@ -0,0 +1,60 @@ +#include +#include +#include "test.h" + +#define N(s, c) { \ + char *p = s; \ + char *q = strchr(p, c); \ + if (q) \ + t_error("strchr(%s,%s) returned str+%d, wanted 0\n", #s, #c, q-p); \ +} + +#define T(s, c, n) { \ + char *p = s; \ + char *q = strchr(p, c); \ + if (q == 0) \ + t_error("strchr(%s,%s) returned 0, wanted str+%d\n", #s, #c, n); \ + else if (q - p != n) \ + t_error("strchr(%s,%s) returned str+%d, wanted str+%d\n", #s, #c, q-p, n); \ +} + +int main(void) +{ + int i; + char a[128]; + char s[256]; + + for (i = 0; i < 128; i++) + a[i] = (i+1) & 127; + for (i = 0; i < 256; i++) + *((unsigned char*)s+i) = i+1; + + N("", 'a') + N("a", 'b') + N("abc abc", 'x') + N(a, 128) + N(a, 255) + + T("", 0, 0) + T("a", 'a', 0) + T("a", 'a'+256, 0) + T("a", 0, 1) + T("ab", 'b', 1) + T("aab", 'b', 2) + T("aaab", 'b', 3) + T("aaaab", 'b', 4) + T("aaaaab", 'b', 5) + T("aaaaaab", 'b', 6) + T("abc abc", 'c', 2) + T(s, 1, 0) + T(s, 2, 1) + T(s, 10, 9) + T(s, 11, 10) + T(s, 127, 126) + T(s, 128, 127) + T(s, 255, 254) + T(s, 0, 255) + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string_strcspn.c b/programs/develop/ktcc/trunk/libctest/string_strcspn.c new file mode 100644 index 0000000000..ea207a4962 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string_strcspn.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "test.h" + +#define T(s, c, n) { \ + char *p = s; \ + char *q = c; \ + size_t r = strcspn(p, q); \ + if (r != n) \ + t_error("strcspn(%s,%s) returned %lu, wanted %lu\n", #s, #c, (unsigned long)r, (unsigned long)(n)); \ +} + +int main(void) +{ + int i; + char a[128]; + char s[256]; + + for (i = 0; i < 128; i++) + a[i] = (i+1) & 127; + for (i = 0; i < 256; i++) + *((unsigned char*)s+i) = i+1; + + T("", "", 0) + T("a", "", 1) + T("", "a", 0) + T("abc", "cde", 2) + T("abc", "ccc", 2) + T("abc", a, 0) + T("\xff\x80 abc", a, 2) + T(s, "\xff", 254) + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/string_strstr.c b/programs/develop/ktcc/trunk/libctest/string_strstr.c new file mode 100644 index 0000000000..a497f77871 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/string_strstr.c @@ -0,0 +1,57 @@ +#include +#include +#include "test.h" + +#define N(s, sub) { \ + char *p = s; \ + char *q = strstr(p, sub); \ + if (q) \ + t_error("strstr(%s,%s) returned str+%d, wanted 0\n", #s, #sub, q-p); \ +} + +#define T(s, sub, n) { \ + char *p = s; \ + char *q = strstr(p, sub); \ + if (q == 0) \ + t_error("strstr(%s,%s) returned 0, wanted str+%d\n", #s, #sub, n); \ + else if (q - p != n) \ + t_error("strstr(%s,%s) returned str+%d, wanted str+%d\n", #s, #sub, q-p, n); \ +} + +int main(void) +{ + N("", "a") + N("a", "aa") + N("a", "b") + N("aa", "ab") + N("aa", "aaa") + N("abba", "aba") + N("abc abc", "abcd") + N("0-1-2-3-4-5-6-7-8-9", "-3-4-56-7-8-") + N("0-1-2-3-4-5-6-7-8-9", "-3-4-5+6-7-8-") + N("_ _ _\xff_ _ _", "_\x7f_") + N("_ _ _\x7f_ _ _", "_\xff_") + + T("", "", 0) + T("abcd", "", 0) + T("abcd", "a", 0) + T("abcd", "b", 1) + T("abcd", "c", 2) + T("abcd", "d", 3) + T("abcd", "ab", 0) + T("abcd", "bc", 1) + T("abcd", "cd", 2) + T("ababa", "baba", 1) + T("ababab", "babab", 1) + T("abababa", "bababa", 1) + T("abababab", "bababab", 1) + T("ababababa", "babababa", 1) + T("nanabanabanana", "aba", 3) + T("nanabanabanana", "ban", 4) + T("nanabanabanana", "anab", 1) + T("nanabanabanana", "banana", 8) + T("_ _\xff_ _", "_\xff_", 2) + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/strtod.c b/programs/develop/ktcc/trunk/libctest/strtod.c new file mode 100644 index 0000000000..43a30724ee --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtod.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + double f; +} t[] = { + {"0", 0.0}, + {"00.00", 0.0}, + {"-.00000", -0.0}, + {"1e+1000000", INFINITY}, + {"1e-1000000", 0}, + // 2^-1074 * 0.5 - eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, + // 2^-1074 * 0.5 + eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, + // 2^-1074 * 1.5 - eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, + // 2^-1074 * 1.5 + eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, + // 2^-1022 + 2^-1075 - eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, + // 2^-1022 + 2^-1075 + eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, + // 2^1024 - 2^970 - eps + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, + // 2^1024 - 2^970 + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497792", INFINITY}, + // some random numbers + {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 + {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 + {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 + {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 + {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 + {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 + {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 + {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 + {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 + {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 + {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 + {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 + {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 + {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 + {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 + {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 + {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 + {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 +}; + +int main(void) +{ + int i; + double x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtod(t[i].s, &p); + if (x != t[i].f) + t_error("strtod(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); + } + + printf("%s finished\n", __FILE__); + return t_status; +} + diff --git a/programs/develop/ktcc/trunk/libctest/strtod_long.c b/programs/develop/ktcc/trunk/libctest/strtod_long.c new file mode 100644 index 0000000000..c13abe6f2a --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtod_long.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include "test.h" + +int main(void) +{ + double x, want = .1111111111111111111111; + char buf[40000]; + + memset(buf, '1', sizeof buf); + buf[0] = '.'; + buf[sizeof buf - 1] = 0; + + if ((x=strtod(buf, 0)) != want) + t_error("strtod(.11[...]1) got %.18f want %.18f\n", x, want); + + printf("%s finished\n", __FILE__); + return t_status; +} + diff --git a/programs/develop/ktcc/trunk/libctest/strtod_simple.c b/programs/develop/ktcc/trunk/libctest/strtod_simple.c new file mode 100644 index 0000000000..851a5218bd --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtod_simple.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include "test.h" + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) + */ + +#define TEST(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x, r-x), 0) ) + +int main(void) +{ + int i; + double d, d2; + char buf[1000]; + + for (i=0; i<100; i++) { + d = sin(i); + snprintf(buf, sizeof buf, "%.300f", d); + TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)"); + } + + TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a"); + TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a"); + + printf("%s finished\n", __FILE__); + return t_status; +} + diff --git a/programs/develop/ktcc/trunk/libctest/strtof.c b/programs/develop/ktcc/trunk/libctest/strtof.c new file mode 100644 index 0000000000..4b01477b85 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtof.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + float f; +} t[] = { + // 2^-149 * 0.5 - eps + {".7006492321624085354618647916449580656401309709382578858785341419448955413429303e-45", 0}, + // 2^-149 * 0.5 + eps + {".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149}, + // 2^-149 * 0.5 - eps + {".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149}, + // 2^-149 * 0.5 + eps + {".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148}, + // 2^-126 + 2^-150 - eps + {".1175494420887210724209590083408724842314472120785184615334540294131831453944281e-37", 0x1p-126}, + // 2^-126 + 2^-150 + eps + {".1175494420887210724209590083408724842314472120785184615334540294131831453944282e-37", 0x1.000002p-126}, + // 2^128 - 2^103 - eps + {"340282356779733661637539395458142568447.9999999999999999999", 0x1.fffffep127}, + // 2^128 - 2^103 + {"340282356779733661637539395458142568448", INFINITY}, +}; + +int main(void) +{ + int i; + float x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtof(t[i].s, &p); + if (x != t[i].f) + t_error("strtof(\"%s\") want %a got %a\n", t[i].s, t[i].f, x); + } + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/strtol.c b/programs/develop/ktcc/trunk/libctest/strtol.c new file mode 100644 index 0000000000..f4d8089863 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtol.c @@ -0,0 +1,80 @@ +#include +#include +#include +//#include +#include "test.h" + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ + errno = 0, msg = #f, ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x), 0) ) + +#define TEST2(r, f, x, m) ( \ + ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", msg, r, x), 0) ) + +int main(void) +{ + int i; + long l; + unsigned long ul; + char *msg=""; + char *s, *c; + + TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld"); + TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld"); + TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu"); + + if (sizeof(long) == 4) { + TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu"); + TEST2(i, c-s, 10, "wrong final position %d != %d"); + TEST2(i, errno, ERANGE, "missing errno %d != %d"); + TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 2, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu"); + TEST2(i, c-s, 11, "wrong final position %d != %d"); + TEST2(i, errno, 0, "spurious errno %d != %d"); + } else { + TEST(i, 0, 1, "64bit tests not implemented"); + } + + TEST(l, strtol("z", 0, 36), 35, "%ld != %ld"); + TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld"); + TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld"); + + TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld"); + TEST2(i, c-s, 1, "wrong final position %ld != %ld"); + + TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld"); + TEST2(i, c-s, 6, "wrong final position %ld != %ld"); + + c = NULL; + TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld"); + TEST2(i, c-s, 0, "wrong final position %d != %d"); + TEST2(i, errno, EINVAL, "%d != %d"); + + TEST(l, strtol(s=" 15437", &c, 8), 015437, "%ld != %ld"); + TEST2(i, c-s, 7, "wrong final position %d != %d"); + + TEST(l, strtol(s=" 1", &c, 0), 1, "%ld != %ld"); + TEST2(i, c-s, 3, "wrong final position %d != %d"); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/strtold.c b/programs/develop/ktcc/trunk/libctest/strtold.c new file mode 100644 index 0000000000..7e50d5eae9 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/strtold.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include "test.h" + +#define length(x) (sizeof(x) / sizeof *(x)) + +static struct { + char *s; + long double f; +} t[] = { + {"0", 0.0}, + {"12.345", 12.345L}, + {"1.2345e1", 12.345L}, + {"1e+1000000", INFINITY}, + {"1e-1000000", 0}, +#if LDBL_MANT_DIG == 53 + // 2^-1074 * 0.5 - eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351043e-323", 0}, + // 2^-1074 * 0.5 + eps + {".2470328229206232720882843964341106861825299013071623822127928412503377536351044e-323", 0x1p-1074}, + // 2^-1074 * 1.5 - eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053131e-323", 0x1p-1074}, + // 2^-1074 * 1.5 + eps + {".7410984687618698162648531893023320585475897039214871466383785237510132609053132e-323", 0x1p-1073}, + // 2^-1022 + 2^-1075 - eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606759e-307", 0x1p-1022}, + // 2^-1022 + 2^-1075 + eps + {".2225073858507201630123055637955676152503612414573018013083228724049586647606760e-307", 0x1.0000000000001p-1022}, + // 2^1024 - 2^970 - eps + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497791.999999999999999999999999999999", 0x1.fffffffffffffp1023}, + // 2^1024 - 2^970 + {"17976931348623158079372897140530341507993413271003782693617377898044" + "49682927647509466490179775872070963302864166928879109465555478519404" + "02630657488671505820681908902000708383676273854845817711531764475730" + "27006985557136695962284291481986083493647529271907416844436551070434" + "2711559699508093042880177904174497792", INFINITY}, + // some random numbers + {".5961860348131807091861002266453941950428e00", 0.59618603481318067}, // 0x1.313f4bc3b584cp-1 + {"1.815013169218038729887460898733526957442e-1", 0.18150131692180388}, // 0x1.73b6f662e1712p-3 + {"42.07082357534453600681618685682257590772e-2", 0.42070823575344535}, // 0x1.aece23c6e028dp-2 + {"665.4686306516261456328973225579833470816e-3", 0.66546863065162609}, // 0x1.54b84dea53453p-1 + {"6101.852922970868621786690495485449831753e-4", 0.61018529229708685}, // 0x1.386a34e5d516bp-1 + {"76966.95208236968077849464348875471158549e-5", 0.76966952082369677}, // 0x1.8a121f9954dfap-1 + {"250506.5322228682496132604807222923702304e-6", 0.25050653222286823}, // 0x1.0084c8cd538c2p-2 + {"2740037.230228005325852424697698331177377e-7", 0.27400372302280052}, // 0x1.18946e9575ef4p-2 + {"20723093.50049742645941529268715428324490e-8", 0.20723093500497428}, // 0x1.a868b14486e4dp-3 + {"0.7900280238081604956226011047460238748912e1", 7.9002802380816046}, // 0x1.f99e3100f2eaep+2 + {"0.9822860653737296848190558448760465863597e2", 98.228606537372968}, // 0x1.88ea17d506accp+6 + {"0.7468949723190370809405570560160405324869e3", 746.89497231903704}, // 0x1.75728e73f48b7p+9 + {"0.1630268320282728475980459844271031751665e4", 1630.2683202827284}, // 0x1.97912c28d5cbp+10 + {"0.4637168629719170695109918769645492022088e5", 46371.686297191707}, // 0x1.6a475f6258737p+15 + {"0.6537805944497711554209461686415872067523e6", 653780.59444977110}, // 0x1.3f3a9305bb86cp+19 + {"0.2346324356502437045212230713960457676531e6", 234632.43565024371}, // 0x1.ca4437c3631eap+17 + {"0.9709481716420048341897258980454298205278e8", 97094817.164200485}, // 0x1.7263284a8242cp+26 + {"0.4996908522051874110779982354932499499602e9", 499690852.20518744}, // 0x1.dc8ad6434872ap+28 +#elif LDBL_MANT_DIG == 64 + // 2^-16445 * 0.5 - eps + {".1822599765941237301264202966809709908199525407846781671860490243514185844316698e-4950", 0}, + // 2^-16445 * 0.5 + eps + {".1822599765941237301264202966809709908199525407846781671860490243514185844316699e-4950", 0x1p-16445L}, + // 2^-16445 * 1.5 - eps + {".5467799297823711903792608900429129724598576223540345015581470730542557532950096e-4950", 0x1p-16445L}, + // 2^-16445 * 1.5 + eps + {".5467799297823711903792608900429129724598576223540345015581470730542557532950097e-4950", 0x1p-16444L}, + // 2^-16382 + 2^-16446 - eps + {".3362103143112093506444937793915876332724499641527442230928779770593420866576777e-4931", 0x1p-16382L}, + // 2^-16382 + 2^-16446 + eps + {".3362103143112093506444937793915876332724499641527442230928779770593420866576778e-4931", 0x1.0000000000000002p-16382L}, + // 2^16384 - 2^16319 - eps + {"118973149535723176505351158982948.86679662540046955672e4900", 0x1.fffffffffffffffep16383L}, + // 2^16384 - 2^16319 + eps + {"118973149535723176505351158982948.86679662540046955673e4900", INFINITY}, +#endif +}; + +int main(void) +{ + int i; + long double x; + char *p; + + for (i = 0; i < length(t); i++) { + x = strtold(t[i].s, &p); + if (x != t[i].f) + t_error("strtold(\"%s\") want %La got %La\n", t[i].s, t[i].f, x); + } + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/test.h b/programs/develop/ktcc/trunk/libctest/test.h new file mode 100644 index 0000000000..0168fc262f --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/test.h @@ -0,0 +1,37 @@ +#include +#include +#ifdef __GNUC__ +#include +#include +#include +#include +#endif + + +/* TODO: not thread-safe nor fork-safe */ +extern volatile int t_status; + +#define T_LOC2(l) __FILE__ ":" #l +#define T_LOC1(l) T_LOC2(l) +#define t_error(...) t_printf(T_LOC1(__LINE__) ": " __VA_ARGS__) + +int t_printf(const char *s, ...); + +int t_vmfill(void **, size_t *, int); +int t_memfill(void); + +void t_fdfill(void); + +void t_randseed(uint64_t s); +uint64_t t_randn(uint64_t n); +uint64_t t_randint(uint64_t a, uint64_t b); +void t_shuffle(uint64_t *p, size_t n); +void t_randrange(uint64_t *p, size_t n); +int t_choose(uint64_t n, size_t k, uint64_t *p); + +char *t_pathrel(char *buf, size_t n, char *argv0, char *p); + +int t_setrlim(int r, long lim); + +#include "print.inc" +#include "rand.inc" diff --git a/programs/develop/ktcc/trunk/libctest/tgmath.c b/programs/develop/ktcc/trunk/libctest/tgmath.c new file mode 100644 index 0000000000..09265be916 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/tgmath.c @@ -0,0 +1,41 @@ +//#include +#include +#include +#include "test.h" + +int main(void) +{ + long i; +/* + i = lrint(123456789.1f) & 0x7fffffff; + if (i != 123456792) + t_error("lrint(123456789.1f)&0x7fffffff want 123456792 got %ld\n", i); + i = lrint(123456789.1) & 0x7fffffff; + if (i != 123456789) + t_error("lrint(123456789.1)&0x7fffffff want 123456789 got %ld\n", i); +*/ + if (sqrt(2.0f) != 1.41421353816986083984375) + t_error("sqrt(2.0f) want 1.41421353816986083984375 got %f\n", sqrt(2.0f)); + if (sqrt(2.0) != 1.414213562373095145474621858738828450441360) + t_error("sqrt(2.0) want 1.414213562373095145474621858738828450441360 got %d\n", sqrt(2.0)); + if (sqrt(2) != 1.414213562373095145474621858738828450441360) + t_error("sqrt(2) want 0x1.6a09e667f3bcdp+0 got %a\n", sqrt(2.0)); + + if (sizeof pow(sqrt(8),0.5f) != sizeof(double)) + t_error("sizeof pow(sqrt(8),0.5f) want %d got %d\n", (int)sizeof(double), (int)sizeof pow(sqrt(8),0.5f)); + if (sizeof pow(2.0,0.5) != sizeof(double)) + t_error("sizeof pow(2.0,0.5) want %d got %d\n", (int)sizeof(double), (int)sizeof pow(2.0,0.5)); + if (sizeof pow(2.0f,0.5f) != sizeof(float)) + t_error("sizeof pow(2.0f,0.5f) want %d got %d\n", (int)sizeof(float), (int)sizeof pow(2.0f,0.5f)); +// if (sizeof pow(2.0,0.5+0*I) != sizeof(double complex)) +// t_error("sizeof pow(2.0,0.5+0*I) want %d got %d\n", (int)sizeof(double complex), (int)sizeof pow(2.0,0.5+0*I)); + + if (pow(2.0,0.5) != 1.414213562373095145474621858738828450441360) + t_error("pow(2.0,0.5) want 0x1.6a09e667f3bcdp+0 got %a\n", pow(2.0,0.5)); + if (pow(2,0.5) != 1.414213562373095145474621858738828450441360) + t_error("pow(2,0.5) want 0x1.6a09e667f3bcdp+0 got %a\n", pow(2,0.5)); + if (pow(2,0.5f) != 1.414213562373095145474621858738828450441360) + t_error("pow(2,0.5f) want 0x1.6a09e667f3bcdp+0 got %a\n", pow(2,0.5f)); + + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/time.c b/programs/develop/ktcc/trunk/libctest/time.c new file mode 100644 index 0000000000..0b22dfc12c --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/time.c @@ -0,0 +1,110 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include "test.h" + +/* We use this instead of memcmp because some broken C libraries + * add additional nonstandard fields to struct tm... */ + +int tm_cmp(struct tm tm1, struct tm tm2) +{ + return tm1.tm_sec != tm2.tm_sec || + tm1.tm_min != tm2.tm_min || + tm1.tm_hour != tm2.tm_hour || + tm1.tm_mday != tm2.tm_mday || + tm1.tm_mon != tm2.tm_mon || + tm1.tm_year != tm2.tm_year || + tm1.tm_wday != tm2.tm_wday || + tm1.tm_yday != tm2.tm_yday || + tm1.tm_isdst!= tm2.tm_isdst; +} + +char *tm_str(struct tm tm) +{ + static int i; + static char b[4][64]; + i = (i+1)%4; + snprintf(b[i], sizeof b[i], + "s=%02d m=%02d h=%02d mday=%02d mon=%02d year=%04d wday=%d yday=%d isdst=%d", + tm.tm_sec, tm.tm_min, tm.tm_hour, + tm.tm_mday, tm.tm_mon, tm.tm_year, + tm.tm_wday, tm.tm_yday, tm.tm_isdst); + return b[i]; +} + +#define TM(ss,mm,hh,md,mo,yr,wd,yd,dst) (struct tm){ \ + .tm_sec = ss, .tm_min = mm, .tm_hour = hh, \ + .tm_mday = md, .tm_mon = mo, .tm_year = yr, \ + .tm_wday = wd, .tm_yday = yd, .tm_isdst = dst } + +#define TM_EPOCH TM(0,0,0,1,0,70,4,0,0) +#define TM_Y2038_1S TM(7,14,3,19,0,138,2,18,0) +#define TM_Y2038 TM(8,14,3,19,0,138,2,18,0) + +static void sec2tm(time_t t, char *m) +{ + struct tm *tm; + time_t r; + + errno = 0; + tm = gmtime(&t); + if (errno != 0) + t_error("%s: gmtime((time_t)%lld) should not set errno, got %s\n", + m, (long long)t, strerror(errno)); + errno = 0; + r = mktime(tm); + if (errno != 0) + t_error("%s: mktime(%s) should not set errno, got %s\n", + m, tm_str(*tm), strerror(errno)); + if (t != r) + t_error("%s: mktime(gmtime(%lld)) roundtrip failed: got %lld (gmtime is %s)\n", + m, (long long)t, (long long)r, tm_str(*tm)); +} + +static void tm2sec(struct tm *tm, int big, char *m) +{ + struct tm *r; + time_t t; + int overflow = big && (time_t)LLONG_MAX!=LLONG_MAX; + + errno = 0; + t = mktime(tm); + if (overflow && t != -1) + t_error("%s: mktime(%s) expected -1, got (time_t)%ld\n", + m, tm_str(*tm), (long)t); + if (overflow && errno != 10000) //EOVERFLOW + t_error("%s: mktime(%s) expected EOVERFLOW (%s), got (%s)\n", + m, tm_str(*tm), strerror(10000), strerror(errno)); + if (!overflow && t == -1) + t_error("%s: mktime(%s) expected success, got (time_t)-1\n", + m, tm_str(*tm)); + if (!overflow && errno) + t_error("%s: mktime(%s) expected no error, got (%s)\n", + m, tm_str(*tm), strerror(errno)); + r = gmtime(&t); + if (!overflow && tm_cmp(*r, *tm)) + t_error("%s: gmtime(mktime(%s)) roundtrip failed: got %s\n", + m, tm_str(*tm), tm_str(*r)); +} + +int main(void) +{ + time_t t; + + putenv("TZ=GMT"); + tzset(); + tm2sec(&TM_EPOCH, 0, "gmtime(0)"); + tm2sec(&TM_Y2038_1S, 0, "2038-1s"); + tm2sec(&TM_Y2038, 1, "2038"); + + sec2tm(0, "EPOCH"); + for (t = 1; t < 1000; t++) + sec2tm(t*100003, "EPOCH+eps"); + + /* FIXME: set a TZ var and check DST boundary conditions */ + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/traceadr.c b/programs/develop/ktcc/trunk/libctest/traceadr.c new file mode 100644 index 0000000000..478973808e --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/traceadr.c @@ -0,0 +1,19 @@ +#include + +# define TRACE1(s, a) printf(s, a) + +void caller(void* ptr) +{ + + ptr = 0xaaaaaaaa; + TRACE1("caller is called from EIP@%x\n", *(int*)((char*)&ptr-4)-5); + +} + + +int main() +{ + + caller(0xffffffff); +} + diff --git a/programs/develop/ktcc/trunk/libctest/tstring.c b/programs/develop/ktcc/trunk/libctest/tstring.c new file mode 100644 index 0000000000..0b0c1e04cd --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/tstring.c @@ -0,0 +1,357 @@ +/* + * Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include +#include +#include + +#ifndef MAX_1 +#ifdef __SPU__ +#define MAX_1 11000 +#else +#define MAX_1 33000 +#endif +#endif + +#define MAX_2 (2 * MAX_1 + MAX_1 / 10) + +void eprintf (int line, char *result, char *expected, int size) +{ + if (size != 0) + printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n", + line, size, result, expected, size); + else + printf ("Failure at line %d, result is <%s>, should be <%s>\n", + line, result, expected); +} + +void mycopy (char *target, char *source, int size) +{ + int i; + + for (i = 0; i < size; ++i) + { + target[i] = source[i]; + } +} + +void myset (char *target, char ch, int size) +{ + int i; + + for (i = 0; i < size; ++i) + { + target[i] = ch; + } +} + +int main() +{ + char target[MAX_1] = "A"; + char first_char; + char second_char; + char array[] = "abcdefghijklmnopqrstuvwxz"; + char array2[] = "0123456789!@#$%^&*("; + char buffer2[MAX_1]; + char buffer3[MAX_1]; + char buffer4[MAX_1]; + char buffer5[MAX_2]; + char buffer6[MAX_2]; + char buffer7[MAX_2]; + char expected[MAX_1]; + char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7; + int i, j, k, x, z, align_test_iterations; + + int test_failed = 0; + + tmp1 = target; + tmp2 = buffer2; + tmp3 = buffer3; + tmp4 = buffer4; + tmp5 = buffer5; + tmp6 = buffer6; + tmp7 = buffer7; + + tmp2[0] = 'Z'; + tmp2[1] = '\0'; + + if (memset (target, 'X', 0) != target || + memcpy (target, "Y", 0) != target || + memmove (target, "K", 0) != target || + strncpy (tmp2, "4", 0) != tmp2 || + strncat (tmp2, "123", 0) != tmp2 || + strcat (target, "") != target) + { + eprintf (__LINE__, target, "A", 0); + test_failed = 1; + } + + if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL + || memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) || + tmp2[0] != 'Z' || tmp2[1] != '\0') + { + eprintf (__LINE__, target, "A", 0); + test_failed = 1; + } + + tmp2[2] = 'A'; + if (strcpy (target, "") != target || + strncpy (tmp2, "", 4) != tmp2 || + strcat (target, "") != target) + { + eprintf (__LINE__, target, "", 0); + test_failed = 1; + } + + if (target[0] != '\0' || strncmp (target, "", 1) || + memcmp (tmp2, "\0\0\0\0", 4)) + { + eprintf (__LINE__, target, "", 0); + test_failed = 1; + } + + tmp2[2] = 'A'; + if (strncat (tmp2, "1", 3) != tmp2 || + memcmp (tmp2, "1\0A", 3)) + { + eprintf (__LINE__, tmp2, "1\0A", 3); + test_failed = 1; + } + + if (strcpy (tmp3, target) != tmp3 || + strcat (tmp3, "X") != tmp3 || + strncpy (tmp2, "X", 2) != tmp2 || + memset (target, tmp2[0], 1) != target) + { + eprintf (__LINE__, target, "X", 0); + test_failed = 1; + } + + if (strcmp (target, "X") || strlen (target) != 1 || + memchr (target, 'X', 2) != target || + strchr (target, 'X') != target || + memchr (target, 'Y', 2) != NULL || + strchr (target, 'Y') != NULL || + strcmp (tmp3, target) || + strncmp (tmp3, target, 2) || + memcmp (target, "K", 0) || + strncmp (target, tmp3, 3)) + { + eprintf (__LINE__, target, "X", 0); + test_failed = 1; + } + + if (strcpy (tmp3, "Y") != tmp3 || + strcat (tmp3, "Y") != tmp3 || + memset (target, 'Y', 2) != target) + { + eprintf (__LINE__, target, "Y", 0); + test_failed = 1; + } + + target[2] = '\0'; + if (memcmp (target, "YY", 2) || strcmp (target, "YY") || + strlen (target) != 2 || memchr (target, 'Y', 2) != target || + strcmp (tmp3, target) || + strncmp (target, tmp3, 3) || + strncmp (target, tmp3, 4) || + strncmp (target, tmp3, 2) || + strchr (target, 'Y') != target) + { + eprintf (__LINE__, target, "YY", 2); + test_failed = 1; + } + + strcpy (target, "WW"); + if (memcmp (target, "WW", 2) || strcmp (target, "WW") || + strlen (target) != 2 || memchr (target, 'W', 2) != target || + strchr (target, 'W') != target) + { + eprintf (__LINE__, target, "WW", 2); + test_failed = 1; + } + + if (strncpy (target, "XX", 16) != target || + memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)) + { + eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16); + test_failed = 1; + } + + if (strcpy (tmp3, "ZZ") != tmp3 || + strcat (tmp3, "Z") != tmp3 || + memcpy (tmp4, "Z", 2) != tmp4 || + strcat (tmp4, "ZZ") != tmp4 || + memset (target, 'Z', 3) != target) + { + eprintf (__LINE__, target, "ZZZ", 3); + test_failed = 1; + } + + target[3] = '\0'; + tmp5[0] = '\0'; + strncat (tmp5, "123", 2); + if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") || + strcmp (tmp3, target) || strcmp (tmp4, target) || + strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 || + strncmp ("ZZY", target, 4) >= 0 || + memcmp (tmp5, "12", 3) || + strlen (target) != 3) + { + eprintf (__LINE__, target, "ZZZ", 3); + test_failed = 1; + } + + target[2] = 'K'; + if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 || + memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 || + memchr (target, 'K', 3) != target + 2 || + strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 || + strchr (target, 'K') != target + 2) + { + eprintf (__LINE__, target, "ZZK", 3); + test_failed = 1; + } + + strcpy (target, "AAA"); + if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") || + strncmp (target, "AAA", 3) || + strlen (target) != 3) + { + eprintf (__LINE__, target, "AAA", 3); + test_failed = 1; + } + + j = 5; + while (j < MAX_1) + { + for (i = j-1; i <= j+1; ++i) + { + /* don't bother checking unaligned data in the larger + sizes since it will waste time without performing additional testing */ + if (i <= 16 * sizeof(long)) + { + align_test_iterations = 2*sizeof(long); + if (i <= 2 * sizeof(long) + 1) + z = 2; + else + z = 2 * sizeof(long); + } + else + { + align_test_iterations = 1; + } + + for (x = 0; x < align_test_iterations; ++x) + { + tmp1 = target + x; + tmp2 = buffer2 + x; + tmp3 = buffer3 + x; + tmp4 = buffer4 + x; + tmp5 = buffer5 + x; + tmp6 = buffer6 + x; + + first_char = array[i % (sizeof(array) - 1)]; + second_char = array2[i % (sizeof(array2) - 1)]; + memset (tmp1, first_char, i); + mycopy (tmp2, tmp1, i); + myset (tmp2 + z, second_char, i - z - 1); + if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z) + { + printf ("error at line %d\n", __LINE__); + test_failed = 1; + } + + tmp1[i] = '\0'; + tmp2[i] = '\0'; + if (strcpy (expected, tmp2) != expected) + { + printf ("error at line %d\n", __LINE__); + test_failed = 1; + } + tmp2[i-z] = first_char + 1; + if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 || + memset (tmp3, first_char, i) != tmp3) + { + printf ("error at line %d\n", __LINE__); + test_failed = 1; + } + + myset (tmp4, first_char, i); + tmp5[0] = '\0'; + if (strncpy (tmp5, tmp1, i+1) != tmp5 || + strcat (tmp5, tmp1) != tmp5) + { + printf ("error at line %d\n", __LINE__); + test_failed = 1; + } + mycopy (tmp6, tmp1, i); + mycopy (tmp6 + i, tmp1, i + 1); + + tmp7[2*i+z] = second_char; + strcpy (tmp7, tmp1); + + strchr (tmp1, second_char); + + if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) || + strncmp (tmp1, expected, i) || + strncmp (tmp1, expected, i+1) || + strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 || + strncmp (tmp1, tmp2, i+1) >= 0 || + strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 || + strchr (tmp1, first_char) != tmp1 || + memchr (tmp1, second_char, i) != tmp1 + z || + strchr (tmp1, second_char) != tmp1 + z || + strcmp (tmp5, tmp6) || + strncat (tmp7, tmp1, i+2) != tmp7 || + strcmp (tmp7, tmp6) || + tmp7[2*i+z] != second_char) + { + eprintf (__LINE__, tmp1, expected, 0); + printf ("x is %d\n",x); + printf ("i is %d\n", i); + printf ("tmp1 is <%p>\n", tmp1); + printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5); + printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6); + test_failed = 1; + } + + for (k = 1; k <= align_test_iterations && k <= i; ++k) + { + if (memcmp (tmp3, tmp4, i - k + 1) != 0 || + strncmp (tmp3, tmp4, i - k + 1) != 0) + { + printf ("Failure at line %d, comparing %.*s with %.*s\n", + __LINE__, i, tmp3, i, tmp4); + test_failed = 1; + } + tmp4[i-k] = first_char + 1; + if (memcmp (tmp3, tmp4, i) >= 0 || + strncmp (tmp3, tmp4, i) >= 0 || + memcmp (tmp4, tmp3, i) <= 0 || + strncmp (tmp4, tmp3, i) <= 0) + { + printf ("Failure at line %d, comparing %.*s with %.*s\n", + __LINE__, i, tmp3, i, tmp4); + test_failed = 1; + } + tmp4[i-k] = first_char; + } + } + } + j = ((2 * j) >> 2) << 2; + } + + printf("\n %s finished\n", __FILE__); + if (test_failed) + { + printf("\n %s FAILED\n", __FILE__); + abort(); + } + else + exit(0); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/udiv.c b/programs/develop/ktcc/trunk/libctest/udiv.c new file mode 100644 index 0000000000..3522c6340f --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/udiv.c @@ -0,0 +1,170 @@ +#include "test.h" +//#include + +/* +static uint64_t randstate = 0x123456789abcdef0ull; +static uint64_t rnd(void) { + randstate = 6364136223846793005ull*randstate + 1; + return randstate; +} +void test_maketest() +{ + int i; + uint64_t x,y; + for (i = 0; i < 128; i++) { + x = rnd(); + y = rnd()>>(i/2); + if (!y) + continue; + printf("0x%llxull, 0x%llxull, 0x%llxull, 0x%llxull,\n", x, y, x/y, x%y); + } +} +*/ + +static struct { + uint64_t x, y, div, mod; +} t[] = { +0x8ddb1a43e77c4031ull, 0x5950e8c33d34979eull, 0x1ull, 0x348a3180aa47a893ull, +0x723f4114006c08c7ull, 0x817de530db2b43fcull, 0x0ull, 0x723f4114006c08c7ull, +0x47811fa5f00f74dull, 0x3d98e7d3fcd5d5c5ull, 0x0ull, 0x47811fa5f00f74dull, +0x51ffcc7cdc989d43ull, 0x36be8bd6746b70e4ull, 0x1ull, 0x1b4140a6682d2c5full, +0x57bf9128512fe829ull, 0x197b3858155d498dull, 0x3ull, 0xb4de82011180b82ull, +0x89fc1c5968fa817full, 0xdcea797734c7115ull, 0x9ull, 0xdb838065b4a87c2ull, +0x4ed5264cf7092ec5ull, 0xde40d1e15ef3e74ull, 0x5ull, 0x960e4b6895cf681ull, +0xffd86b253d97317bull, 0x13f9ff2d24b6d6f4ull, 0xcull, 0x1020750785051e0bull, +0x8771fa2da656a721ull, 0x9210fe654c59bfcull, 0xeull, 0x7a31b9503881f59ull, +0xb5961d12bcd3e937ull, 0xbdb5a33662f547aull, 0xfull, 0x3bbd40fc00df611ull, +0x93c79eecdac7ed3dull, 0x6f267c57ea2b7b5ull, 0x15ull, 0x1e51bb9776edb64ull, +0x6b93ffce49f1a4b3ull, 0x3583d1f9702ee03ull, 0x20ull, 0x8c5bdb6993e453ull, +0x138aefcc98ce5d19ull, 0x117002fa7600b11ull, 0x11ull, 0x103eca27b6da0f8ull, +0xb3da641cef491fefull, 0x357615f638334b8ull, 0x35ull, 0x2c33b5d551f35d7ull, +0x71c4b06e463912b5ull, 0x1c286ad9e8f5229ull, 0x40ull, 0x1230506a2648875ull, +0x97d4cf7df046d6ebull, 0x1e9412f5c77b2b8ull, 0x4full, 0xd9b1e06756b023ull, +0x1428f04bd490ea11ull, 0x9d97f29a897c93ull, 0x20ull, 0x75f1f8836157b1ull, +0x35256c76832705a7ull, 0xa962f1a447dcd7ull, 0x50ull, 0x3680f32cb20277ull, +0x2969e82bd9347f2dull, 0x723d68574d4156ull, 0x5cull, 0x5bd6ac79710445ull, +0x9061a12aae71a823ull, 0x4186d8a1a66175ull, 0x234ull, 0x48be68be2f25full, +0x695b8d33ef342e09ull, 0x3ed1fe1a998fe3ull, 0x1adull, 0x15a6615bde0ea2ull, +0x46b4dd1e06367a5full, 0xa04e70622e4e8ull, 0x70eull, 0x64750bc0b9dafull, +0xd68b05ba7eee12a5ull, 0x72ab3fb682444ull, 0x1defull, 0x3c437fc988329ull, +0x1e59cc2ac508f85bull, 0xeb15ae6d4d7f9ull, 0x210ull, 0xc00aeae0b86cbull, +0x296f8d2c76a0901ull, 0xf65628b31b01ull, 0x2b0ull, 0xf14566117651ull, +0x7036f5ad7cbc5e17ull, 0xa09d3bfcf72cfull, 0xb2dull, 0x72236db564ab4ull, +0x915d6883c575ad1dull, 0x3a38d68d3a38eull, 0x27f2ull, 0x241de6f7a6ee1ull, +0x845ba74f5adfa793ull, 0x2f6950e58d00bull, 0x2caaull, 0x249dc90239c45ull, +0xb910d16c54805af9ull, 0x1fc2ca5c99a7aull, 0x5d3aull, 0x1771487b50955ull, +0x27a2e280bcf990cfull, 0x389aa0c0b0cc0ull, 0xb34ull, 0x9d71d12eb9cfull, +0x1e032f04a5372e95ull, 0x63c2a1d58710ull, 0x4d04ull, 0x154ce4414255ull, +0x3a1a5659908495cbull, 0x279dcd85418aull, 0x17775ull, 0x132c6f9c7bb9ull, +0xd769a376e5e103f1ull, 0xadacb670e0c7ull, 0x13d85ull, 0x8ad256e5d18eull, +0x269f4f4baaaf287ull, 0x1aed2ad9daf0ull, 0x16f3ull, 0x426550f80b7ull, +0x6700daeeb87a770dull, 0xeca7ab1aa93ull, 0x6f6c5ull, 0x70d9466f1eeull, +0xd0201f3783c2a303ull, 0x3a0c01aa3e6aull, 0x395e1ull, 0x18b33b9015d9ull, +0xca3f2e00d291e3e9ull, 0xbe0e048cd94ull, 0x1106c2ull, 0x37f7fc0a1c1ull, +0xec4d240dc289633full, 0x4f8aadb7483ull, 0x2f8855ull, 0x46e0db91bc0ull, +0xd7967b29e2e36685ull, 0xe61d902db27ull, 0xefd69ull, 0x36811fff886ull, +0xe3ecd4374320af3bull, 0x4edd0edd0a0ull, 0x2e3defull, 0x4ad0da4c9dbull, +0x7a08fe1d98b4dae1ull, 0x6bced9c0c15ull, 0x121c89ull, 0x40c856617a4ull, +0x34435992a5c9c2f7ull, 0x4f4a94c109full, 0xa8bc9ull, 0x94c5d46120ull, +0x6fd0027468f1dcfdull, 0x597186b0153ull, 0x140060ull, 0x16f26555dddull, +0x4fe37c1db1619a73ull, 0x47a0c30bd15ull, 0x11d861ull, 0x5964fb3d7eull, +0x77aa77f86d07c8d9ull, 0x3a39cf03d65ull, 0x20e21cull, 0x37f7fede7cdull, +0xc072e76ad59cf1afull, 0x3a786701dull, 0x34a98c59ull, 0x22b6b1b9aull, +0xfb8e8f1f7781ba75ull, 0xe8ca427d3eull, 0x114a34dull, 0xa344eb94cfull, +0x160e34cf590444abull, 0xe2388f12feull, 0x18f574ull, 0xc303329393ull, +0x2509ddea3a648dd1ull, 0xec762d81bcull, 0x281955ull, 0xc0463d1e65ull, +0xc9ba10cd6eafcf67ull, 0x96a51d06f7ull, 0x156ce72ull, 0x133e2df369ull, +0x1dd4fe261b4adeedull, 0x2736e25406ull, 0xc2bfefull, 0x1354c1f353ull, +0x480258f92fc38de3ull, 0x2599b52bb0ull, 0x1ea450cull, 0x2879f11a3ull, +0x5a3257b1114109c9ull, 0x2978f9f1aaull, 0x22cc30aull, 0x1317311b25ull, +0xf4eeda8f34ab3c1full, 0x1aa70450d9ull, 0x9309d64ull, 0x1187b6925bull, +0x3c2c319ca8612a65ull, 0x73fc01eceull, 0x84d0088ull, 0x3165accf5ull, +0x4f6034e74a16561bull, 0x1f29d53707ull, 0x28c0daaull, 0xd88e07075ull, +0x206665a7072f1cc1ull, 0xda87e7ceaull, 0x25f48c1ull, 0xd3ddb2057ull, +0x100c559d7db417d7ull, 0xb907ebbc2ull, 0x1634188ull, 0xa2eae16c7ull, +0x64c5f83691b47cddull, 0x5aced6ebbull, 0x11c17fb7ull, 0x344109030ull, +0x32a812777eaf7d53ull, 0x1cb63fe4full, 0x1c3a9675ull, 0xb113f938ull, +0x67478d96865ca6b9ull, 0x142fa03aull, 0x51dcb463dull, 0x11359ce7ull, +0x71024e740deb428full, 0x142d3885ull, 0x599d9edd5ull, 0x13b1ae6ull, +0x52c78160b090b655ull, 0xd02101c6ull, 0x65d1b205ull, 0x1c0a0177ull, +0x16917d5f9fde38bull, 0xfb1566c7ull, 0x17029e0ull, 0x1bbe166bull, +0xa6ee688a0d1387b1ull, 0x22c4d384ull, 0x4cd19afcfull, 0x77143f5ull, +0x74babc1be2ed9c47ull, 0x22eda9a6ull, 0x3578b1967ull, 0x189b247dull, +0x7c5cbf2dfc1db6cdull, 0x5f09c060ull, 0x14efd44d4ull, 0x5210e74dull, +0x7c046071c1ac68c3ull, 0x3696c8e6ull, 0x24596d86bull, 0x26060a1ull, +0x84728ab55d399fa9ull, 0x267d7771ull, 0x370ea7405ull, 0x255d1674ull, +0x99f57100ef5404ffull, 0x10c0df86ull, 0x9308fef0dull, 0x9009131ull, +0x3f4c0514b0df5e45ull, 0xf2c3810ull, 0x42bf84d39ull, 0x3aa12b5ull, +0xd01edb572821ecfbull, 0x2a443aeull, 0x4ec8b88639ull, 0x111c73dull, +0xeecb08561bd0cea1ull, 0xbeca231ull, 0x140692508bull, 0x9b36e06ull, +0x8c856facc6335cb7ull, 0x398eab4ull, 0x271008c7a5ull, 0x922ab3ull, +0x23fb9839e8358cbdull, 0x24deb54ull, 0xf9d714151ull, 0xb9c329ull, +0x2005d5de30015033ull, 0x47c06dbull, 0x7240bccbaull, 0x104d115ull, +0x67d59c29e076f499ull, 0x179f009ull, 0x465554ac22ull, 0x10b0767ull, +0x32d2dd34369c836full, 0x13d3fbfull, 0x2902f2fb54ull, 0x7553c3ull, +0x3960c3c99fdc2235ull, 0x1b808baull, 0x21618743cdull, 0x11e7743ull, +0x343bad5adfa9726bull, 0xeef444ull, 0x37f58c51a6ull, 0x3d8a53ull, +0x7a4aadd7b4e5f191ull, 0x129c9ull, 0x6921bb5a2a53ull, 0x6b66ull, +0x9eb7dae5d71c5927ull, 0x31d7f5ull, 0x32f2ff2c6d5ull, 0x22c4eull, +0x1b285999316afeadull, 0x115477ull, 0x1912cf6611eull, 0x801bbull, +0x917aee3d84b533a3ull, 0x71d26full, 0x1473408589aull, 0x6e74ddull, +0x18e6a86b0473a589ull, 0x50a12ull, 0x4f0fabc67d4ull, 0x210a1ull, +0xf22c9887813bbddfull, 0x5b17aull, 0x2a897505c07bull, 0x1f841ull, +0xef7a551239d60225ull, 0x7fb5aull, 0x1e00b98e188bull, 0x41847ull, +0xffd2ad0e77b73dbull, 0x146f14ull, 0xc8500600a3ull, 0xba1full, +0x76743abdfb91f081ull, 0xd5888ull, 0x8e0303c479cull, 0x245a1ull, +0xc2eeb030bcff9197ull, 0x7a4e8ull, 0x198034e02c37ull, 0x343bfull, +0x63cc9c23f0ed0c9dull, 0x6c1e5ull, 0xec4d5841041ull, 0x38178ull, +0x7ad70f846e8f1313ull, 0x7fdf5ull, 0xf5ecec69bc9ull, 0x756b6ull, +0x60de3d71574eb279ull, 0x6ea3ull, 0xe02421997a61ull, 0x18b6ull, +0xd27054901c68b44full, 0x2dd0full, 0x497d639c8f46ull, 0xe135ull, +0xbcf297b8f0dbfe15ull, 0xcf17ull, 0xe992af0ca1abull, 0x32b8ull, +0x96c3ae70323ef14bull, 0xbbdcull, 0xcd7329b68d81ull, 0x1b6full, +0xdc1a13cfa4d3cb71ull, 0xdb16ull, 0x1012fe5ed296full, 0x46e7ull, +0xa1d40a2986f40607ull, 0x8067ull, 0x142a473fdb7beull, 0x1895ull, +0x227f92ef6daab68dull, 0x15ecull, 0x192dda5d5ed25ull, 0xf71ull, +0xc0a4a7810015ee83ull, 0x6064ull, 0x1ffa220762fc8ull, 0x4463ull, +0xd38b6eb9f0e71b69ull, 0x1909ull, 0x8732ce2cc77f4ull, 0xfd5ull, +0x2e588bdb751a66bfull, 0x229cull, 0x156d025c70d97ull, 0x10bbull, +0xd90f7e11dcbd1605ull, 0x760ull, 0x1d6e934381ba2eull, 0x2c5ull, +0x60ab67a4e5aeabbull, 0x1bf7ull, 0x374f26f3e3edull, 0x210ull, +0x224f627be76a8261ull, 0x4f4ull, 0x6ed4d3882b567ull, 0x35ull, +0x300d1ab91bd0b677ull, 0xe9cull, 0x34a002fb76e63ull, 0x823ull, +0x2a63d80e0c52fc7dull, 0x32ull, 0xd90970ebc4383full, 0x2full, +0xb0e94bbc1f90c5f3ull, 0x3b3ull, 0x2fd2ef70381c52ull, 0x29dull, +0x2b5dc22562dbe059ull, 0x30aull, 0xe45055015fff5ull, 0x1c7ull, +0x4a7fd1078807d52full, 0x18dull, 0x300a32f60677d4ull, 0x16bull, +0x41a01ee8ab0849f5ull, 0x13cull, 0x352a3971f57e9dull, 0x29ull, +0x95a7287ad5f6602bull, 0x1d0ull, 0x529130d1034a23ull, 0xbbull, +0x723bacc76bd51551ull, 0x16ull, 0x53142091089af83ull, 0xfull, +0x81c49febaa2ca2e7ull, 0xcdull, 0xa20d44956f5bf4ull, 0x83ull, +0x11593d6b3f54de6dull, 0x63ull, 0x2cdc6b1a7f9078ull, 0x5ull, +0x756c82d6f7069963ull, 0x5cull, 0x146bea3ba565525ull, 0x17ull, +0xda882ab2a88c0149ull, 0x1bull, 0x8180194d6d5c728ull, 0x11ull, +0xbb03671751a7ff9full, 0x20ull, 0x5d81b38ba8d3ffcull, 0x1full, +0x6884fa0a8f0c99e5ull, 0x12ull, 0x5ce7fab40d6088cull, 0xdull, +0x5052a2953c528441ull, 0x7ull, 0xb7984f0bf79809bull, 0x4ull, +0x58dd1583185ecb57ull, 0x9ull, 0x9dfad0e90ee1697ull, 0x8ull, +0xaa6870c376df5c5dull, 0x3ull, 0x38cd7aebd24a741full, 0x0ull, +0x4b21d01617167e39ull, 0x2ull, 0x2590e80b0b8b3f1cull, 0x1ull, +}; + +int main(void) +{ + uint64_t x, y, div, mod; + int i; + + for (i = 0; i < sizeof t/sizeof *t; i++) { + x = t[i].x; + y = t[i].y; + div = x / y; + mod = x % y; + if (div != t[i].div) + t_error("udiv %llu/%llu want %llu got %llu\n", x, y, t[i].div, div); + if (mod != t[i].mod) + t_error("umod %llu%%%llu want %llu got %llu\n", x, y, t[i].mod, mod); + } + + printf("%s finished\n", __FILE__); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/ungetc.c b/programs/develop/ktcc/trunk/libctest/ungetc.c new file mode 100644 index 0000000000..d658c199c7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/ungetc.c @@ -0,0 +1,114 @@ +#include +#include +#include +//#include +//#include +//#include +#include "test.h" + +#define TEST(r, f, x, m) ( \ + errno = 0, ((r) = (f)) == (x) || \ + (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) + +#define TEST_S(s, x, m) ( \ + !strcmp((s),(x)) || \ + (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) + +#include +#include +#include +#include + +#define fgetc fgetc_dbg +#define ungetc ungetc_dbg + + +int fgetc_dbg(FILE* file) +{ + int c = 0, rc; + + rc = fread(&c, 1, 1, file); + + if (rc < 1) return EOF; + + return c; +} + +int ungetc_dbg(int c,FILE* file) +{ + dword res; + + if(!file) + { + errno = E_INVALIDPTR; + return EOF; + } + + if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) + { + errno = E_ACCESS; + return EOF; + } + + if (file->filepos>file->filesize || file->filepos==0 || c == EOF || file->ungetc_buf != EOF) + { + errno = E_EOF; + return EOF; + } + + file->ungetc_buf = c; + file->filepos--; + + return c; +} + +void mark(int n) +{ + n++; +} + + +int main(void) +{ + int i; + char a[100]; + FILE *f; + + TEST(i, !(f = fopen("_tmpfile.tmp","w+")), 0, "failed to create temp file %d!=%d (%s)"); + + if (!f) return t_status; + + TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)"); + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)"); + + TEST(i, feof(f), 0, "%d != %d"); + TEST(i, fgetc(f), 'h', "'%c' != '%c'"); + TEST(i, ftell(f), 1, "%d != %d"); + TEST(i, ungetc('x', f), 'x', "%d != %d"); + TEST(i, ftell(f), 0, "%d != %d"); + TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d"); + TEST(i, ftell(f), 0, "%d != %d"); +mark(0x11); +printf("debug file ungetbuf=%d\n", f->ungetc_buf); + TEST(i, fgetc(f), 'x', "'%c' != '%c'"); + TEST(i, ftell(f), 1, "%d != %d"); + + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); + TEST(i, ungetc('x', f), 'x', "%d != %d"); +mark(0x22); + TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d"); + a[14] = 0; + TEST_S(a, "xhello, world\n", "mismatch reading ungot character"); + + TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); + TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d"); + TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d"); + TEST(i, fgetc(f), 'x', "'%c' != '%c'"); +mark(0x33); + TEST(i, ftell(f), 1, "%d != %d"); + TEST(i, fgetc(f), 'h', "'%c' != '%c'"); + + printf("%s finished\n", __FILE__); + fclose(f); + return t_status; +} diff --git a/programs/develop/ktcc/trunk/libctest/ungetc2.c b/programs/develop/ktcc/trunk/libctest/ungetc2.c new file mode 100644 index 0000000000..8f4b44df81 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/ungetc2.c @@ -0,0 +1,39 @@ +/* ungetc example */ +#include + +void trace_file(FILE* f, char* cmt); + +int main () +{ + FILE * pFile; + int c; + char buffer [256]; + + pFile = fopen ("myfile.txt","rt"); + if (pFile==NULL) perror ("Error opening file"); + else while (!feof (pFile)) { + trace_file(pFile, "1"); + + c=getc (pFile); + + trace_file(pFile, "before ungetc"); + + if (c == EOF) break; + if (c == '#') ungetc ('@',pFile); + else ungetc (c,pFile); + + trace_file(pFile, "after"); + + if (fgets (buffer,255,pFile) != NULL) + puts (buffer); + else break; + } + return 0; +} + +void trace_file(FILE* f, char* cmt) +{ + printf("%s[%s]\n", cmt, f->buffer); + printf("mode=%0X, filesize=%d, filepos=%d\n", f->mode, f->filesize, f->filepos); + printf("ungetc=%d, buffer_start=%d, buffer_end=%d\n", f->ungetc_buf, f->buffer_start, f->buffer_end); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libctest/wtalloc.c b/programs/develop/ktcc/trunk/libctest/wtalloc.c new file mode 100644 index 0000000000..9f45e97de7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libctest/wtalloc.c @@ -0,0 +1,131 @@ +#include +#include +#include +#include + +// suballocator functions +extern void* wtmalloc(size_t size); +extern void wtfree(void *pointer); +extern void* wtrealloc(void* pointer, size_t size); +extern void* wtcalloc (size_t num, size_t size); +extern int wtmalloc_freelist_check(); +extern int wtmalloc_poiner_check(void *ptr); +extern void wtdump_alloc_stats(); + +#ifdef __GNUC__ +void* sysmalloc(size_t sz) +{ + return malloc(sz); +} +#endif + + + +#define NUMPTR 10000 + +char *pointers[NUMPTR]; +char values[NUMPTR]; +int sizes[NUMPTR]; + +int checkvalues() +{ + for (int i = 0; i < NUMPTR; i++) + { + if (!pointers[i]) continue; + assert(wtmalloc_poiner_check(pointers[i])); + + for (int j = 0; j < sizes[i]; j++) + assert(pointers[i][j] == values[i]); + } + return 1; +} + + +int main() +{ + char *ptr; + int i, sz; + + puts("Test started"); + + // test start settings + assert(wtmalloc_freelist_check()); + // test just single alloc/dealloc + ptr = wtmalloc(1000); + assert(wtmalloc_poiner_check(ptr)); + wtfree(ptr); + assert(wtmalloc_freelist_check()); + + puts("test allocation started"); + // test allocation + for (i = 0; i < NUMPTR; i++) + { + sz = rand() % 4200; + pointers[i] = wtmalloc(sz); + sizes[i] = sz; + values[i] = sz % 256; + memset(pointers[i], values[i], sz); + + assert(wtmalloc_freelist_check()); + } + assert(checkvalues()); + + puts("test random deallocation started"); + // random deallocation + for (i = 0; i < NUMPTR; i++) + { + sz = rand() % 2; + if (sz) + { + wtfree(pointers[i]); + pointers[i] = NULL; + } + } + assert(wtmalloc_freelist_check()); + assert(checkvalues()); + + puts("test allocation in free list gaps started"); + // test allocation in free list gaps + for (i = 0; i < NUMPTR; i++) + { + if (pointers[i]) continue; + + sz = rand() % 4200; + pointers[i] = wtmalloc(sz); + sizes[i] = sz; + values[i] = sz % 256; + memset(pointers[i], values[i], sz); + } + assert(wtmalloc_freelist_check()); + assert(checkvalues()); + + puts("test realloc started"); + // test realloc + for (i = 0; i < NUMPTR; i++) + { + sz = rand() % 4200; + pointers[i] = wtrealloc(pointers[i], sz); + + sizes[i] = sz; + memset(pointers[i], values[i], sz); + } + assert(wtmalloc_freelist_check()); + assert(checkvalues()); + + + puts("test full deallocation started"); + // full deallocation + for (i = 0; i < NUMPTR; i++) + { + wtfree(pointers[i]); + pointers[i] = NULL; + } + assert(wtmalloc_freelist_check()); + + wtdump_alloc_stats(); + + printf("\ntests all OK\n"); + + return 0; + +} diff --git a/programs/develop/ktcc/trunk/samples/Makefile b/programs/develop/ktcc/trunk/samples/Makefile new file mode 100755 index 0000000000..a97b8c7443 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/Makefile @@ -0,0 +1,28 @@ +CC=../bin/kos32-tcc +CFLAGS= -I ../libc/include + +all: + $(CC) $(CFLAGS) asm_ex.c -lck -o asm_ex.kex + $(CC) $(CFLAGS) consoleio.c -lck -o consoleio.kex + $(CC) $(CFLAGS) files.c -lck -o files.kex + $(CC) $(CFLAGS) winbasics.c -lck -o winbasics.kex + $(CC) $(CFLAGS) dynamic.c -lck -lhttp -linputbox -o dynamic.kex + $(CC) $(CFLAGS) load_coff.c -o load_coff.kex -lck + $(CC) $(CFLAGS) graphics.c -lck -lgb -o graphics.kex + $(CC) $(CFLAGS) dir_example.c -lck -o dir_example.kex + $(CC) $(CFLAGS) getopt_ex.c -lck -o getopt_ex.kex + + $(CC) $(CFLAGS) clayer/msgbox.c -lck -lmsgbox -o clayer/msgbox.kex + $(CC) $(CFLAGS) clayer/rasterworks.c -lck -lrasterworks -o clayer/rasterworks.kex + $(CC) $(CFLAGS) clayer/boxlib.c -lck -lbox -o clayer/boxlib.kex + $(CC) $(CFLAGS) clayer/libimg.c -lck -limg -o clayer/libimg.kex + $(CC) $(CFLAGS) clayer/dialog.c -lck -ldialog -o clayer/dialog.kex + + $(CC) $(CFLAGS) net/tcpsrv_demo.c -lck -o net/tcpsrv_demo.kex + $(CC) $(CFLAGS) net/nslookup.c -lck -lnetwork -o net/nslookup.kex + $(CC) $(CFLAGS) net/http_tcp_demo.c -lck -lnetwork -o net/http_tcp_demo.kex + + $(CC) $(CFLAGS) tinygl/fps.c tinygl/gears.c -o tinygl/gears.kex -ltinygl -lck + +clean: + rm *.kex clayer/*.kex net/*.kex tinygl/*.kex diff --git a/programs/develop/ktcc/trunk/samples/asm_ex.c b/programs/develop/ktcc/trunk/samples/asm_ex.c new file mode 100644 index 0000000000..2a0bc40313 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/asm_ex.c @@ -0,0 +1,68 @@ +/* examples for interoperability with assembler + +1. Calling assembler code from .c : see in libc\math any .asm file +2. Using inline assembler: see \include\kos32sys1.h and libc\math\fmod.c +- https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Extended-Asm.html +- not all constraints from gcc are supported, no "f" or "t" for example +- not supported clobberring st registers, must manual add "fstp %%st" at end or similar +- need full suffixes for opcodes, fstpl but not fstp + +3. Calling c functions from .asm: see \libc\start\start.asm:99 +Remember: +- small ints always passed as int32, floating point params as 64-bit +- returned structs passed on stack with additional hidden 1st param +- c functions can use EAX, ECX, EDX without warnings +- .c default is cdecl calling convention https://en.wikipedia.org/wiki/X86_calling_conventions +- dont use fastcall calling convention, tinycc realized it non-conformant way +- tinycc supports only ELF object files + +tcc can be used as a linker +*/ + + + + +#include +#include + + +main() +{ + int i; + for (i = 0; i < 20; i++) + { + printf("------------------------------------------------------\n"); + printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) ); + printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) ); +//remainder of 5.3 / 2 is -0.700000 +//remainder of 18.5 / 4.2 is 1.700000 + + printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) ); + printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) ); +// fmod of 5.3 / 2 is 1.300000 +// fmod of 18.5 / 4.2 is 1.700000 + + double param, fractpart, intpart, result; + int n; + + param = 3.14159265; + fractpart = modf (param , &intpart); + printf ("%f = %f + %f \n", param, intpart, fractpart); +//3.141593 = 3.000000 + 0.141593 + + param = 0.95; + n = 4; + result = ldexp (param , n); + printf ("%f * 2^%d = %f\n", param, n, result); +//0.950000 * 2^4 = 15.200000 + + param = 8.0; + result = frexp (param , &n); + printf ("%f = %f * 2^%d\n", param, result, n); +//8.000000 = 0.500000 * 2^4 + param = 50; + result = frexp (param , &n); + printf ("%f = %f * 2^%d\n", param, result, n); + + } +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/samples/build_all.sh b/programs/develop/ktcc/trunk/samples/build_all.sh new file mode 100644 index 0000000000..baf5c3c644 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/build_all.sh @@ -0,0 +1,23 @@ +#SHS +/sys/@notify 'Build in progress...\nYou will find binaries in /tmp0/1/tccbin' -I +mkdir /tmp0/1/tccbin +../tcc asm_ex.c -lck -o /tmp0/1/tccbin/asm_ex +../tcc consoleio.c -lck -o /tmp0/1/tccbin/consoleio +../tcc files.c -lck -o /tmp0/1/tccbin/files +../tcc winbasics.c -lck -o /tmp0/1/tccbin/winbasics +../tcc dynamic.c -lck -lhttp -linputbox -o /tmp0/1/tccbin/dynamic +../tcc load_coff.c -o /tmp0/1/tccbin/load_coff -lck +../tcc clayer/msgbox.c -lck -lmsgbox -o /tmp0/1/tccbin/msgbox +../tcc graphics.c -lck -lgb -o /tmp0/1/tccbin/graphics +../tcc clayer/rasterworks.c -lck -lrasterworks -o /tmp0/1/tccbin/rasterworks +../tcc clayer/boxlib.c -lck -lbox -o /tmp0/1/tccbin/boxlib_ex +../tcc clayer/libimg.c -lck -limg -o /tmp0/1/tccbin/libimg_ex +cp clayer/logo.png /tmp0/1/tccbin/logo.png +../tcc clayer/dialog.c -lck -ldialog -o /tmp0/1/tccbin/dialog_ex +../tcc dir_example.c -lck -o /tmp0/1/tccbin/dir_example +../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tccbin/tcpsrv_demo +../tcc net/nslookup.c -lck -lnetwork -o /tmp0/1/tccbin/nslookup +../tcc net/http_tcp_demo.c -lck -lnetwork -o /tmp0/1/tccbin/http_tcp_demo +../tcc getopt_ex.c -lck -o /tmp0/1/tccbin/getopt_ex +../tcc tinygl/fps.c tinygl/gears.c -o /tmp0/1/tccbin/gears -ltinygl -lck +exit diff --git a/programs/develop/ktcc/trunk/samples/clayer/boxlib.c b/programs/develop/ktcc/trunk/samples/clayer/boxlib.c new file mode 100644 index 0000000000..39f01d023d --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/clayer/boxlib.c @@ -0,0 +1,110 @@ +// BOXLIB EXAMPLE (scrollbar, progressbar, editbox and checkbox) +// Writed by maxcodehack and superturbocat2001 + +#include +#include +#include +#include +#include + +#define WIN_W 640 +#define WIN_H 563 + +#define ED_BUFF_LEN 50 +#define TEXT_SIZE 0x10000000 +#define SCROLL_BUTTON_SIZE 15 +#define SCROLL_MAX_LEN 215 +#define BLACK 0x000000 +#define WHITE 0xFFFFFF +#define BLUE 0x0000FF + +uint32_t wheels; +char* title = "Boxlib example"; +char ed_buff[ED_BUFF_LEN]; + + +scrollbar scroll = {15, WIN_W - 26, WIN_H - 29, 0, 0, 2, 215, SCROLL_BUTTON_SIZE, 0,0x707070,0xD2CED0,0x555555}; +progressbar pg = {0, 10, 10, 270, 35, 1, 0, 200, 0xB4B4B4, 0x2728FF, 0xA9A9A9}; +edit_box ed={WIN_W-140,10,60,0xFFFFFF,0x6a9480,0,0x6a9480, BLACK | TEXT_SIZE, ED_BUFF_LEN, ed_buff,NULL,ed_focus}; +check_box output_off={X_W(10, 15), Y_H(120,15), 10, WHITE, BLUE, BLACK | TEXT_SIZE, "Disable duplicate output",0}; + +void draw_window(){ + BeginDraw(); + DrawWindow(215,100,WIN_W,WIN_H,title, 0x858585, 0x34); + edit_box_draw(&ed); + check_box_draw2(&output_off); + if(!output_off.flags) + { + draw_text_sys(ed_buff, 10, 90, strlen(ed_buff), BLACK | TEXT_SIZE); + } + scrollbar_v_draw(&scroll); + progressbar_draw(&pg); + EndDraw(); +} + +//// EVENTMASK +#define EVM_REDRAW 1 +#define EVM_KEY 2 +#define EVM_BUTTON 4 +#define EVM_EXIT 8 +#define EVM_BACKGROUND 16 +#define EVM_MOUSE 32 +#define EVM_IPC 64 +#define EVM_STACK 128 +#define EVM_DEBUG 256 +#define EVM_STACK2 512 +#define EVM_MOUSE_FILTER 0x80000000 +#define EVM_CURSOR_FILTER 0x40000000 +//// EVENTMASK + + +int main() +{ + kolibri_boxlib_init(); + init_checkbox2(&output_off); + set_event_mask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE+EVM_MOUSE_FILTER); + while(1) + { + switch(GetOsEvent()) + { + case KOLIBRI_EVENT_BUTTON: + if (get_os_button() == 1) exit(0); + break; + + case KOLIBRI_EVENT_KEY: + edit_box_key(&ed, get_key().val); + draw_window(); + break; + + case KOLIBRI_EVENT_REDRAW: + draw_window(); + break; + case KOLIBRI_EVENT_MOUSE: + edit_box_mouse(&ed); + scrollbar_v_mouse(&scroll); + pg.value = scroll.position; + progressbar_draw(&pg); + check_box_mouse2(&output_off); + unsigned int scroll_strong = 10; + wheels = GetMouseWheels(); + if(wheels & 0xFFFF) + { + if((short)wheels > 0){ + scroll.position += scroll_strong; + if(scroll.position>scroll.max_area-scroll.cur_area) + { + scroll.position=scroll.max_area-scroll.cur_area; + } + } + else if((short)wheels < 0 && scroll.position > 0){ + scroll.position -= scroll_strong; + if((int)scroll.position<0){ + scroll.position=0; + } + } + scrollbar_v_draw(&scroll); + } + break; + } + } +} diff --git a/programs/develop/ktcc/trunk/samples/clayer/dialog.c b/programs/develop/ktcc/trunk/samples/clayer/dialog.c new file mode 100644 index 0000000000..5ee125fecd --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/clayer/dialog.c @@ -0,0 +1,32 @@ +#include +#include +#include + +int main() +{ + kolibri_dialog_init(); // dialog init + open_dialog *dlg_open = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320); // create opendialog struct + OpenDialog_init(dlg_open); // Initializing an open dialog box. + OpenDialog_start(dlg_open); // Show open dialog box + + color_dialog *color_select = kolibri_new_color_dialog(SELECT, 10, 10,420,320); // create colordialog struct + ColorDialog_init(color_select); // Initializing an color dialog box. + ColorDialog_start(color_select); // Show color dialog + + if(dlg_open->status == SUCCESS){ + printf("File selected '%s'\n",dlg_open->openfile_path); + }else{ + puts("No file selected!"); + } + + if(color_select->status == SUCCESS){ + printf("Color selected: #%06X\n",color_select->color); + RGB color_rgb = (RGB)color_select->color; + printf("Red:%d Green:%d Blue:%d", color_rgb.red, color_rgb.green, color_rgb.blue); + }else{ + puts("No color selected!"); + } + + free(dlg_open); + free(color_select); +} diff --git a/programs/develop/ktcc/trunk/samples/clayer/libimg.c b/programs/develop/ktcc/trunk/samples/clayer/libimg.c new file mode 100755 index 0000000000..299ed763be --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/clayer/libimg.c @@ -0,0 +1,97 @@ +/* Written by turbocat2001 (Logaev Maxim) */ + +#include +#include +#include +#include +#include + +#define NEW_IMG_H 128 +#define NEW_IMG_W 128 + +#define IMG_H 256 +#define IMG_W 256 + +Image *image_blend; // Create image struct + +struct kolibri_system_colors sys_color_table; // Create system colors table + +char* load_img(char* fname, int32_t* read_sz){ // Image file upload function + FILE *f = fopen(fname, "rb"); + if (!f) { + printf("Can't open file: %s\n", fname); + exit(0); + } + if (fseek(f, 0, SEEK_END)) { + printf("Can't SEEK_END file: %s\n", fname); + exit(0); + } + int filesize = ftell(f); + rewind(f); + char* fdata = malloc(filesize); + if(!fdata) { + printf("No memory for file %s\n", fname); + exit(0); + } + *read_sz = fread(fdata, 1, filesize, f); + if (ferror(f)) { + printf("Error reading file %s\n", fname); + exit(0); + } + fclose(f); + return fdata; +} + +void DrawGUI(){ + BeginDraw(); + DrawWindow(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34); + img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0); // Draw blended image to window + EndDraw(); +} + +int main(){ + if (kolibri_libimg_init() == -1){ + printf("Error loading lib_img.obj\n"); + exit(0); + } + + get_system_colors(&sys_color_table); // Get system colors theme + set_event_mask(0xC0000027); + + uint32_t img_size; + void *file_data = load_img("logo.png", &img_size); // Get RAW data and size + + Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data + + if (image->Type != IMAGE_BPP32) { + image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32 + if (!image) { + printf("РЎonvert error!: \n"); + exit(0); + } + } + + image_blend = img_create(IMG_W+NEW_IMG_W, IMG_H, IMAGE_BPP32); // Create an empty layer + img_fill_color(image_blend, IMG_W+NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color + img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H); // Blending images to display the alpha channel. + /* Reduce image size from 256x256 to 128x128 */ + image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H); + img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H); + img_destroy(image); // Destroy image structure + free(file_data); // Free allocated file_data buffer + + /* Main event loop */ + while (1) { + switch(get_os_event()){ + case KOLIBRI_EVENT_REDRAW: + DrawGUI(); + break; + case KOLIBRI_EVENT_BUTTON: + if (get_os_button() == 1){ + return 0; + } + break; + } + } + exit(0); +} diff --git a/programs/develop/ktcc/trunk/samples/clayer/logo.png b/programs/develop/ktcc/trunk/samples/clayer/logo.png new file mode 100644 index 0000000000..5a1285f747 Binary files /dev/null and b/programs/develop/ktcc/trunk/samples/clayer/logo.png differ diff --git a/programs/develop/ktcc/trunk/samples/clayer/msgbox.c b/programs/develop/ktcc/trunk/samples/clayer/msgbox.c new file mode 100644 index 0000000000..7ccd9369eb --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/clayer/msgbox.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + msgbox *msg1 = NULL; + msg1 = kolibri_new_msgbox("Title", "Text in window", 0, "Ok"); + kolibri_start_msgbox(msg1, NULL); +} diff --git a/programs/develop/ktcc/trunk/samples/clayer/rasterworks.c b/programs/develop/ktcc/trunk/samples/clayer/rasterworks.c new file mode 100644 index 0000000000..145cf47fcd --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/clayer/rasterworks.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +// Sizes +int x_size = 768, y_size = 256; + +// Out example string +char* string = "Пример/Example"; + +int main() +{ + // Count length + int ln_str = countUTF8Z(string, -1); + + // Create image buffer + void *buffi = malloc(x_size * y_size * 3 * sizeof(char) + 8); + + // Set sizes + *((int*)buffi) = x_size; + *((int*)buffi+1) = y_size; + + // Fill color + memset((char*)buffi + 8, 0xFF, x_size * y_size * 3); + + // Draw text on buffer + drawText(buffi, 5, 0, string, ln_str, 0xFF000000, 0x30C18); + drawText(buffi, 5, 32, string, ln_str, 0xFF000000, 0x1030C18); + drawText(buffi, 5, 64, string, ln_str, 0xFF000000, 0x2030C18); + drawText(buffi, 5, 96, string, ln_str, 0xFF000000, 0x4030C18); + drawText(buffi, 5, 128, string, ln_str, 0xFF000000, 0x8030C18); + drawText(buffi, 5, 160, string, ln_str, 0xFF000000, 0x0F031428); + + while (1) + { + switch (get_os_event()) + { + case 1: + BeginDraw(); + DrawWindow(50, 50, 800, 300, "Rasterworks Example", 0x999999, 0x34); + DrawBitmap(buffi + 8, 10, 10, 768, 256); + EndDraw(); + break; + case 2: + get_key(); + break; + case 3: + if (get_os_button() == 1) exit(0); + break; + }; + } + return 0; +} diff --git a/programs/develop/ktcc/trunk/samples/consoleio.c b/programs/develop/ktcc/trunk/samples/consoleio.c new file mode 100644 index 0000000000..f2d72c9c80 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/consoleio.c @@ -0,0 +1,23 @@ +// demonstration conio use, color text +// more info in conio.h + +#include + +int main() +{ + int i; + if (con_init_console_dll()) return 1; // init fail + +// con_write_asciiz("\033[0;31;42m test \n"); // red on green bk + + for(i = 30; i < 48; i++) + { + con_printf("\033[%dmColor 0x%02X: ", i, i); + con_write_asciiz("Text sample."); + + con_printf(" printf %s test %d\n", "small", i); + + } + + con_exit(0); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/samples/dir_example.c b/programs/develop/ktcc/trunk/samples/dir_example.c new file mode 100644 index 0000000000..3a8260c45f --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/dir_example.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +int main() +{ + char *path=getcwd(NULL, PATH_MAX); + printf("Current directory: %s\n", path); + if(true==mkdir("test")){ + puts("Test folder created!"); + } + else{ + puts("Error creating folder!"); + } + short_file_info *info; + int num = lsdir(path, &info); + if(num==FS_ERROR) + { + puts("File system error."); + return -1; + } + printf("Objects in the folder: %d\n", num); + for(int j=0; j +#include +#include + +#define OK 200 + +int main() { + if (con_init_console_dll()) return 1; // init fail + con_write_asciiz("Wait, I'll ask you... when I'll done to fetch one site...\n"); + con_set_title("Dynamicaly linked app"); + + http_msg *h = http_get("http://kolibrios.org/en/", 0, HTTP_FLAG_BLOCK, ""); + http_long_receive(h); + + if (h->status == OK) { + con_write_string(h->content_ptr, h->content_length); + } else { + con_write_asciiz("Oops! Can't access to the page.\n"); + } + char buffer[256]; + InputBox(buffer, "Hay!", "How do you do?", "Hmm?", 0, 256, 0); + con_printf("Your answer is \"%s\"\n", buffer); + con_write_string("It's surprising, isn't it?", 26); + con_exit(0); + return 0; +} diff --git a/programs/develop/ktcc/trunk/samples/files.c b/programs/develop/ktcc/trunk/samples/files.c new file mode 100644 index 0000000000..f6c8f1033e --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/files.c @@ -0,0 +1,43 @@ +#include +#include + +int main(int argc, char **argv) +{ + + int i; + char c; + FILE *f; + FILE *fin; + FILE *fout; + + //write to file + f=fopen("testfile.txt","w"); + + for(i=0;i<50;i++) + { + fputc('1',f); + } + fclose(f); + + //append to file + f=fopen("testfile.txt","a"); + + for(i=0;i<50;i++) + { + fputc('2',f); + } + fclose(f); + + //copy from testfile.txt to copyfile.txt + + fin=fopen("testfile.txt","r"); + fout=fopen("copyfile.txt","w"); + + while((c=fgetc(fin))!=EOF) + { + fputc(c,fout); + } + fclose(fin); + fclose(fout); + +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/samples/getopt_ex.c b/programs/develop/ktcc/trunk/samples/getopt_ex.c new file mode 100644 index 0000000000..98b9cc1db8 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/getopt_ex.c @@ -0,0 +1,38 @@ +#include +#include +#include + +void main(int argc, char *argv[]) { + int c; + if(argc<2) + { + puts("Usage: getopt_ex [options]\n"); + puts("-a Show 'Option a'"); + puts("-B Show 'Option B'"); + puts("-n [num] Show 'num'"); + } + while ((c = getopt(argc, argv, "aBn:")) != EOF) { + switch (c) { + case 'a': + puts("Option 'a'"); + break; + + case 'B': + puts("Option 'B'"); + break; + + case 'n': + printf("Option n: value=%d\n", atoi(optarg)); + break; + + case '?': + printf("ERROR: illegal option %s\n", argv[optind-1]); + exit(0); + + default: + printf("WARNING: no handler for option %c\n", c); + exit(0); + } + } + exit(0); + } diff --git a/programs/develop/ktcc/trunk/samples/graphics.c b/programs/develop/ktcc/trunk/samples/graphics.c new file mode 100644 index 0000000000..6e52d8600c --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/graphics.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +/// =========================================================== + +int main() +{ + GB_BMP b; + unsigned event; + + b.w = 300; + b.h = 200; + b.bmp = malloc (300*200*3); + + gb_bar (&b, 4, 8, 4, 12, 0xff0000); // red + gb_bar (&b, 10, 8, 4, 12, 0x00ff00); // green + gb_bar (&b, 16, 8, 4, 12, 0x0000ff); // blue + + gb_line(&b, 4, 30, 50, 30, 0xffffff); // white line + gb_line(&b, 55, 4, 120, 60, 0xf0f033); // another line + + gb_rect(&b, 65, 24, 100, 60, 0x2065ff); // rectangle + + gb_circle(&b, 55, 95, 40, 0x20ff20); // circle + + for (;;) + { + event = get_os_event(); + switch (event) + { + case 1: + begin_draw(); + sys_create_window(50, 50, 310, 230, "testlibgb" ,0x34f0f0f0, 0x14); + draw_bitmap(b.bmp, 5, 25, 300, 200); + end_draw(); + break; + case 2: + get_key(); + break; + + case 3: + if (1==get_os_button()) + { + exit(0); + } + break; + }; + } + exit(0); +} +/// =========================================================== diff --git a/programs/develop/ktcc/trunk/samples/load_coff.c b/programs/develop/ktcc/trunk/samples/load_coff.c new file mode 100644 index 0000000000..d4c73fbe3b --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/load_coff.c @@ -0,0 +1,21 @@ +#include + +/*Using the "coff" library in ktcc using "inputbox.obj" as an example*/ + +unsigned (*InputBox)(void* Buffer, char* Caption, char* Prompt, char* Default, unsigned long Flags, unsigned long BufferSize, void* RedrawProc); + +void *InputBoxLib; + +void load_coff() +{ + InputBoxLib = dlopen("/sys/lib/inputbox.obj", RTLD_GLOBAL); + InputBox = dlsym(InputBoxLib,"InputBox"); +} + +int main() +{ + load_coff(); + char buffer[256]; + InputBox(buffer, "Hay!", "How do you do?", "Hmm?", 10, 256, 0); + dlclose(InputBoxLib); +} diff --git a/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c b/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c new file mode 100644 index 0000000000..b0924ef9c8 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +int main() { + con_init_console_dll(); + networklib_init(); + con_set_title("http request demo using raw sockets"); + + char *host = "kolibrios.org"; + int port = 80; + printf("Connecting to %s on port %d\n", host, port); + + struct addrinfo *addr_info; + char port_str[16]; sprintf(port_str, "%d", port); + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter + hints.ai_socktype = SOCK_STREAM; // TCP stream sockets + if (getaddrinfo(host, port_str, 0, &addr_info) != 0) { + printf("Host %s not found!\n", host); + freeaddrinfo(addr_info); + exit(-1); + } + printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr)); + //printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8); + + char request[256]; + sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host); + printf("request = %s\n", request); + + int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP); + + puts("Connecting...\n"); + if (connect(sock, addr_info->ai_addr, addr_info->ai_addrlen) != 0) { + printf("Connection failed, errno = %d\n", errno); + exit(errno); + } + puts("Connected successfully\n"); + + puts("Sending request...\n"); + if (send(sock, request, strlen(request), MSG_NOFLAG) == -1) { + printf("Sending failed, errno = %d\n", errno); + exit(errno); + } + puts("Request sended successfully, waiting for response...\n"); + + char buf[512 + 1]; + if (recv(sock, buf, 512, MSG_NOFLAG) == -1) { + printf("Receive failed, errno = %d\n", errno); + exit(errno); + } + + printf("Response = %s\n", buf); + + freeaddrinfo(addr_info); + + close(sock); + puts("\n goodbye)\n"); + con_exit(0); + return 0; +} diff --git a/programs/develop/ktcc/trunk/samples/net/nslookup.c b/programs/develop/ktcc/trunk/samples/net/nslookup.c new file mode 100644 index 0000000000..010c2b9e6e --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/net/nslookup.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +struct addrinfo *res; +char host[256]; + +void main() +{ + con_init_console_dll(); + networklib_init(); + con_set_title("nslookup demo"); + printf("Host name to resolve: "); + con_gets(host, 256); + host[strlen(host)-1] = '\0'; + if(getaddrinfo(host ,0, 0, &res)!=0) + { + puts("Host not found!"); + freeaddrinfo(res); + con_exit(0); + } + else + { + printf("%s",inet_ntoa(res->ai_addr->sin_addr)); + freeaddrinfo(res); + con_exit(0); + } +} diff --git a/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c b/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c new file mode 100644 index 0000000000..5e8b64cf01 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/net/tcpsrv_demo.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +const char msg1[]="Hello!"; +char msg2='\0'; + +int main() +{ + struct sockaddr addr={AF_INET4, PORT(23) , 0, 0}; + + int sk1=socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP); + printf("Open socket: %d. Status: %s\n",sk1, strerror(errno)); + + bind(sk1, &addr,sizeof(addr)); + printf("Socket binding. Status: %s\n", strerror(errno)); + + listen(sk1, 1); + printf("Listening to a socket. Status: %s\n", strerror(errno)); + printf("You can connect to 'tcp server' via 'telnet' on localhost:23 !"); + + int sk2 = accept(sk1, &addr, sizeof(addr)); + printf("Accept done. Status: %s\n", strerror(errno)); + + send(sk2, msg1, strlen(msg1),MSG_NOFLAG); + printf("Send message: '%s'. Status: %s\n",msg1, strerror(errno)); + puts("Received data:"); + while(msg2!='!') + { + recv(sk2, &msg2, 1, MSG_NOFLAG); + printf("%c",msg2); + } + close(sk1); + close(sk2); + puts("\nGood bye!"); + exit(0); +} diff --git a/programs/develop/ktcc/trunk/samples/tinygl/fps.c b/programs/develop/ktcc/trunk/samples/tinygl/fps.c new file mode 100644 index 0000000000..dff4a9870c --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/tinygl/fps.c @@ -0,0 +1,33 @@ +#include +int time1=0; +int time2=0; +int fps1=0; +int timerend=0; + +int Fps() +{ + int tr; + time1=get_tick_count(); + if (timerend==0) + { + time2=time1; + timerend=time1; + } + + tr = time1 - timerend; + + if ((time1 - time2) < 100) + { + fps1++; + } + else + { + draw_bar(330,8, 50,10,0xFFFFFF); + draw_number_sys(fps1,330,8,10,0x0); + fps1=0; + time2=time1; + } + timerend=time1; +return tr; +} + diff --git a/programs/develop/ktcc/trunk/samples/tinygl/gears.c b/programs/develop/ktcc/trunk/samples/tinygl/gears.c new file mode 100644 index 0000000000..bc82a2da81 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/tinygl/gears.c @@ -0,0 +1,313 @@ +#include +#include +#include +#include +#include +#include + +int Fps(); + +struct { + int x,y; + int dx,dy; +} win; + +#define KEY_ESC 1 +#define KEY_F 33 + +char *title1 = "TinyGL in KolibriOS"; +char *title2 = "F full screen"; +char *title3 = "ESC - exit"; +char *fps = "FPS:"; + +unsigned char FullScreen = 0; +unsigned char skin = 3; + +struct process_table_entry *pri; +KOSGLContext cgl; + +static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0; +static GLint gear1, gear2, gear3; +static GLfloat angle = 0.0; + +static GLuint limit; +static GLuint count = 1; + +/* + * Draw a gear wheel. You'll probably want to call this function when + * building a display list since we do a lot of trig here. + * + * Input: inner_radius - radius of hole at center + * outer_radius - radius at center of teeth + * width - width of gear + * teeth - number of teeth + * tooth_depth - depth of tooth + */ +static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width, + GLint teeth, GLfloat tooth_depth ) +{ + GLint i; + GLfloat r0, r1, r2; + GLfloat angle, da; + GLfloat u, v, len; + + r0 = inner_radius; + r1 = outer_radius - tooth_depth/2.0; + r2 = outer_radius + tooth_depth/2.0; + + da = 2.0*M_PI / teeth / 4.0; + + glShadeModel( GL_FLAT ); + + glNormal3f( 0.0, 0.0, 1.0 ); + + /* draw front face */ + glBegin( GL_QUAD_STRIP ); + for (i=0;i<=teeth;i++) { + angle = i * 2.0*M_PI / teeth; + glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 ); + glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 ); + glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 ); + glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 ); + } + glEnd(); + + /* draw front sides of teeth */ + glBegin( GL_QUADS ); + da = 2.0*M_PI / teeth / 4.0; + for (i=0;iwinx_size, pri->winy_size-20); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0, (GLfloat)pri->winx_size/pri->winy_size, 1.0, 60.0); + glTranslatef( 0.0, 0.0, 20.0 ); + glMatrixMode(GL_MODELVIEW); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); +} + +void disabletgl() +{ + kosglDestroyContext(cgl); + free(pri); +} + +void Title() +{ + _ksys_write_text(300,8,0x0,fps,strlen(fps)); + _ksys_write_text(8,8,0x0,title1,strlen(title1)); + _ksys_write_text(180,8,0x0,title2,strlen(title2)); + _ksys_write_text(600,8,0x0,title3,strlen(title3)); +} + + +void draw_window() +{ + begin_draw(); + sys_create_window(win.x, win.y, win.dx, win.dy,"Gears", 0xFFFFFF, 0x34); + Title(); + end_draw(); +} + + +void main(void) +{ + pri=malloc(sizeof(struct process_table_entry)); + win.x = 100; + win.y = 100; + win.dx = 400; + win.dy = 400; + draw_window(); + _ksys_set_keyboard_mode(1); + cgl = kosglCreateContext( 0, 0); + kosglMakeCurrent( 0, 20, win.dx, win.dy-20, cgl); + init(); + reshape(); + do{ + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glPushMatrix(); + glRotatef( view_rotx, 1.0, 0.0, 0.0 ); + glRotatef( view_roty, 0.0, 1.0, 0.0 ); + glRotatef( view_rotz, 0.0, 0.0, 1.0 ); + + glPushMatrix(); + glTranslatef( -2.0, -2.0, 0.0 ); + glRotatef( angle, 0.0, 0.0, 1.0 ); + glCallList(gear1); + glPopMatrix(); + + glPushMatrix(); + glTranslatef( 4.1, -2.0, 0.0 ); + glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 ); + glCallList(gear2); + glPopMatrix(); + + glPushMatrix(); + glTranslatef( -2.1, 4.2, 0.0 ); + glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 ); + glCallList(gear3); + glPopMatrix(); + glPopMatrix(); + kosglSwapBuffers(); + + angle += 0.01 + 0.3* Fps(); + switch(_ksys_check_for_event()){ + case KOLIBRI_EVENT_REDRAW: draw_window(); + reshape(); + break; + + case KOLIBRI_EVENT_KEY: + switch(get_key().ctrl_key){ + case KEY_F: + if(!FullScreen){ + skin=0; + int screen_size_x; + int screen_size_y; + _ksys_get_screen_size(&screen_size_x, &screen_size_y); + sys_change_window(0,0,screen_size_x,screen_size_y); + draw_window(); + reshape(); + FullScreen = 1; + } + else{ + skin=3; + draw_window(); + sys_change_window(win.x,win.y, win.dx, win.dy); + reshape(); + FullScreen = 0; + }; + break; + case KEY_ESC: disabletgl(); + return;} + break; + + case 3: disabletgl(); + return; + } + } while(1); +} diff --git a/programs/develop/ktcc/trunk/samples/winbasics.c b/programs/develop/ktcc/trunk/samples/winbasics.c new file mode 100644 index 0000000000..636d6a7a58 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/winbasics.c @@ -0,0 +1,148 @@ +/* + newlib-style window example +*/ +#include +#include +#include +#include +#include "kos32sys1.h" + +struct kolibri_system_colors sys_color_table; +void __attribute__ ((noinline)) debug_board_printf(const char *format,...); +void __attribute__ ((noinline)) debug_board_write_str(const char* str); + +char statusbar[255]; +char proc_info[1024]; +char text_line[255]; + +enum BUTTONS +{ + BTN_QUIT = 1, + BTN_POP = 10, + BTN_UNLOCK = 11 +}; + +#define FONT_W 8 +#define FONT_H 14 +#define LINES 10 + +void draw_window() +{ + int win_hight, win_width, i, pos_y = get_skin_height() + 36; // 60 == 24+36 + + // start redraw + begin_draw(); + // define&draw window + sys_create_window(10, 40, 600, 400, "My window", /*sys_color_table.work_area*/0xFFFFFF, 0x13); + + get_proc_info(proc_info); + win_width = *(int*)(proc_info + 0x3E); // client, 2A windows + win_hight = *(int*)(proc_info + 0x42); // client, 2E windows + + define_button((10 << 16) + 80, (30 << 16) + 20, BTN_POP, sys_color_table.work_button); + draw_text_sys("BUTTON1", 15, 34, 0, 0x90000000 | sys_color_table.work_button_text); //0x80000000 asciiz + + define_button((100 << 16) + 100, (30 << 16) + 20, BTN_UNLOCK, sys_color_table.work_button); + draw_text_sys("BUTTTON2", 110, 34, 0, 0x90000000 | sys_color_table.work_button_text); + + // display statusbar + draw_bar(6, win_hight - 17, win_width - 11, 12, 0x80000000 | sys_color_table.work_area); //0x80000000 gradient + draw_text_sys(statusbar, 10, win_hight - 15, 0, 0x80000000 | sys_color_table.work_text); + + // display strings + for (i = LINES; i > 0; i--) + { + tiny_snprintf (text_line, sizeof text_line, "Line[%d]<>", i); + + text_line[(win_width - 10 - 5) / FONT_W + 1] = '\0'; // clip text size, seems to big lines crashing OS, and form len by window size +// draw_number_sys(nbytes, 5, pos_y, 6, 0x10000000); 8x12 font + draw_text_sys(text_line, 5, pos_y, 0, 0x90000000 /*| sys_color_table.work_text*/); + pos_y += FONT_H; + + if(pos_y + 29 > win_hight) break; // 12 font + 12 statusbar + 5 border + } + + // end redraw + end_draw(); +} + +int main() +{ + int gui_event; + uint32_t pressed_button = 0, mouse_button; + pos_t mouse_pos; + strcpy(statusbar, "Program running...Double click on TEXT for details"); + + get_system_colors(&sys_color_table); + set_event_mask(0xC0000027); // mouse events only when focused window and mouse inside + + do /* Start of main activity loop */ + { +// gui_event = wait_for_event(10); // 100 = 1 sec, case you have background work + gui_event = get_os_event(); + switch(gui_event) + { + case KOLIBRI_EVENT_NONE: + // background work + break; + case KOLIBRI_EVENT_REDRAW: + draw_window(); + break; + case KOLIBRI_EVENT_KEY: + // scroll + break; + case KOLIBRI_EVENT_BUTTON: + pressed_button = get_os_button(); + switch (pressed_button) + { + case BTN_POP: + strcpy(statusbar, "POP pressed...."); + draw_window(); + break; + case BTN_UNLOCK: + strcpy(statusbar, "UNLOCK pressed...."); + draw_window(); + break; + case BTN_QUIT: + return 0; + break; + } + break; + case KOLIBRI_EVENT_MOUSE: + mouse_pos = get_mouse_pos(POS_WINDOW); // window relative + mouse_button = get_mouse_eventstate(); + debug_board_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button); + if (mouse_button & (1<<24)) // double click + { + int n = (mouse_pos.y - 60) / FONT_H; + if (n < 0 || n >= LINES) break; + debug_board_printf("click on str(%d), clip slot(%d)\n", n, LINES - n - 1); + tiny_sprintf(statusbar, "click on str(%d), clip slot(%d)\n", n, LINES - n - 1); + draw_window(); + } + // ignore + break; + } + } while(1) ; /* End of main activity loop */ + + return 0; +} + + + +void __attribute__ ((noinline)) debug_board_write_str(const char* str){ + while(*str) + debug_board_write_byte(*str++); +} + +void __attribute__ ((noinline)) debug_board_printf(const char *format,...) +{ + va_list ap; + char log_board[300]; + + va_start (ap, format); + tiny_vsnprintf(log_board, sizeof log_board, format, ap); + va_end(ap); + debug_board_write_str(log_board); + +} diff --git a/programs/develop/ktcc/trunk/source/Makefile.linux32 b/programs/develop/ktcc/trunk/source/Makefile.linux32 index 6f60b80b72..f397c08cda 100644 --- a/programs/develop/ktcc/trunk/source/Makefile.linux32 +++ b/programs/develop/ktcc/trunk/source/Makefile.linux32 @@ -2,7 +2,7 @@ CC=gcc NAME=kos32-tcc SRC=libtcc.c tcc.c -CFLAGS= -DTCC_TARGET_MEOS_LINUX +CFLAGS= -DTCC_TARGET_MEOS_LINUX LFLAGS= -m32 all: diff --git a/programs/develop/ktcc/trunk/source/libtcc.c b/programs/develop/ktcc/trunk/source/libtcc.c index e903ba0469..a3310d2397 100644 --- a/programs/develop/ktcc/trunk/source/libtcc.c +++ b/programs/develop/ktcc/trunk/source/libtcc.c @@ -113,12 +113,17 @@ static void tcc_set_lib_path_w32(TCCState *s) { char path[1024], *p; GetModuleFileNameA(tcc_module, path, sizeof path); +#ifdef TCC_TARGET_KX + kx_fix_root_directory(path, sizeof path); + normalize_slashes(strlwr(path)); +#else p = tcc_basename(normalize_slashes(strlwr(path))); if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5)) p -= 5; else if (p > path) p--; *p = 0; +#endif tcc_set_lib_path(s, path); } @@ -150,13 +155,17 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) #if defined TCC_TARGET_MEOS /* on Kolibri host, we suppose the lib and includes are at the location of 'tcc' /lib, /include */ static void tcc_set_lib_path_kos(TCCState *s) -{ +{ char** argv0 = (char**)0x20; // path in kolibri header - char path[1024], *p; + char path[1024], *p; strncpy(path, *argv0, sizeof path); +#ifdef TCC_TARGET_KX + kx_fix_root_directory(path, sizeof path); +#else p = tcc_basename(path); if (p > path) p--; *p = 0; +#endif tcc_set_lib_path(s, path); } @@ -165,7 +174,12 @@ static void tcc_set_lib_path_linux(TCCState *s) { char buff[4096+1]; readlink("/proc/self/exe", buff, 4096); +#ifdef TCC_TARGET_KX + kx_fix_root_directory(buff, sizeof buff); + const char *path = buff; +#else const char *path = dirname(buff); +#endif tcc_set_lib_path(s, path); } @@ -1526,7 +1540,7 @@ ST_FUNC int tcc_add_crt(TCCState *s, const char *filename) { if (-1 == tcc_add_library_internal(s, "%s/%s", filename, 0, s->crt_paths, s->nb_crt_paths)) - tcc_error("file '%s' not found", filename); + tcc_error_noabort("file '%s' not found", filename); return 0; } @@ -1731,7 +1745,8 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) #elif defined(TCC_TARGET_MEOS) if (s->output_type != TCC_OUTPUT_OBJ && !s->nostdlib) { - tcc_add_library_err(s, "c"); + tcc_add_crt(s,"crt0.o"); + //tcc_add_library(s,"lc.obj"); // adding libck.a dont work, because need to be added last } #else /* add libc crt1/crti objects */ @@ -2264,7 +2279,6 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) case TCC_OPTION_B: /* set tcc utilities path (mainly for tcc development) */ tcc_set_lib_path(s, optarg); - tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX); break; case TCC_OPTION_l: args_parser_add_file(s, r, TCC_FILETYPE_BINARY); diff --git a/programs/develop/ktcc/trunk/source/tcc.h b/programs/develop/ktcc/trunk/source/tcc.h index a56da580ed..16ef960787 100644 --- a/programs/develop/ktcc/trunk/source/tcc.h +++ b/programs/develop/ktcc/trunk/source/tcc.h @@ -47,7 +47,7 @@ #ifndef TCC_TARGET_MEOS #define TCC_TARGET_MEOS #endif -//void kx_fix_root_directory(char *buf, size_t size); +void kx_fix_root_directory(char *buf, size_t size); #endif #ifndef _WIN32 diff --git a/programs/develop/ktcc/trunk/source/tcckx.c b/programs/develop/ktcc/trunk/source/tcckx.c index d0cd9017f5..556a896169 100644 --- a/programs/develop/ktcc/trunk/source/tcckx.c +++ b/programs/develop/ktcc/trunk/source/tcckx.c @@ -269,8 +269,11 @@ typedef struct { tcc_free(str_arr); tcc_free(len_arr); tcc_free(sym_arr); + } + + void kx_init(me_info* me) { ElfW(Sym) *sym; int sym_index = 1, sym_end = symtab_section->data_offset / sizeof(ElfW(Sym)); @@ -323,7 +326,6 @@ typedef struct { This function correct tcc_root if tcc_root/kx is a run directory, otherwise do trim filename */ -#if 0 void kx_fix_root_directory(char *buf, size_t size) { int defult = 1; @@ -364,6 +366,5 @@ typedef struct { } tcc_free(tcc_conf); - kx_debug_output("tcc root = %s\n", buf); - } -#endif + //kx_debug_output("tcc root = %s\n", buf); + } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/source/tccmeos.c b/programs/develop/ktcc/trunk/source/tccmeos.c index 039a92ad01..8cd6e4ba78 100644 --- a/programs/develop/ktcc/trunk/source/tccmeos.c +++ b/programs/develop/ktcc/trunk/source/tccmeos.c @@ -302,14 +302,8 @@ int tcc_output_me(TCCState* s1,const char *filename) #ifdef TCC_TARGET_KX kx_init(&me); #endif - if (s1->output_type != TCC_OUTPUT_OBJ && !s1->nostdlib){ - tcc_add_crt(s1, "crt0.o"); - tcc_add_library_err(s1, "tcc1"); - } - relocate_common_syms(); assign_addresses(&me); - #ifndef TCC_TARGET_KX if(undef_sym_flag){ tcc_error("Linker error!");