From 3e571bd7ccf55f4c4e1e204090fb9ca7609348be Mon Sep 17 00:00:00 2001 From: siemargl Date: Fri, 10 Jun 2016 11:10:52 +0000 Subject: [PATCH] small polish, samples git-svn-id: svn://kolibrios.org@6443 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../develop/ktcc/trunk/libc/include/stdio.h | 7 + .../develop/ktcc/trunk/libc/include/stdlib.h | 9 +- .../develop/ktcc/trunk/libc/start/start.asm | 13 +- .../develop/ktcc/trunk/libc/stdio/scanf.c | 2 +- .../ktcc/trunk/libc/stdio/sprintf_tiny.c | 97 ++++++++++++ .../develop/ktcc/trunk/libc/stdlib/itoa.c | 8 +- .../develop/ktcc/trunk/libc/stdlib/itoab.c | 4 +- .../develop/ktcc/trunk/libc/stdlib/strtol.c | 78 +++++++++ .../develop/ktcc/trunk/libc/string/memcmp.c | 2 + .../develop/ktcc/trunk/samples/consoleio.c | 23 +++ programs/develop/ktcc/trunk/samples/files.c | 43 +++++ .../ktcc/trunk/samples/simplewin_old.c | 61 ++++++++ .../develop/ktcc/trunk/samples/winbasics.c | 148 ++++++++++++++++++ .../ktcc/trunk/source/readme_kos32.txt | 13 +- programs/develop/ktcc/trunk/source/tccmeos.c | 3 +- 15 files changed, 487 insertions(+), 24 deletions(-) create mode 100644 programs/develop/ktcc/trunk/libc/stdio/sprintf_tiny.c create mode 100644 programs/develop/ktcc/trunk/libc/stdlib/strtol.c create mode 100644 programs/develop/ktcc/trunk/samples/consoleio.c create mode 100644 programs/develop/ktcc/trunk/samples/files.c create mode 100644 programs/develop/ktcc/trunk/samples/simplewin_old.c create mode 100644 programs/develop/ktcc/trunk/samples/winbasics.c diff --git a/programs/develop/ktcc/trunk/libc/include/stdio.h b/programs/develop/ktcc/trunk/libc/include/stdio.h index 0db71099c4..046de2a68b 100644 --- a/programs/develop/ktcc/trunk/libc/include/stdio.h +++ b/programs/develop/ktcc/trunk/libc/include/stdio.h @@ -90,6 +90,13 @@ 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 ); + +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 + extern int errno; /* errors codes from KOS, but minus */ #ifndef E_SUCCESS diff --git a/programs/develop/ktcc/trunk/libc/include/stdlib.h b/programs/develop/ktcc/trunk/libc/include/stdlib.h index 79719f1bfe..a5d4dc2324 100644 --- a/programs/develop/ktcc/trunk/libc/include/stdlib.h +++ b/programs/develop/ktcc/trunk/libc/include/stdlib.h @@ -12,8 +12,8 @@ extern int atoib(char *s,int b); extern int atoi(char *s); -extern char *itoab(int n,char* s,int b); -extern char *itoa(int n,char* s); +extern char *itoab(unsigned int n,char* s,int b); +extern char *__itoa(int n,char* s); extern void* stdcall malloc(dword size); extern void stdcall free(void *pointer); @@ -25,6 +25,8 @@ 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* calloc (size_t num, size_t size); @@ -41,5 +43,8 @@ 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/start/start.asm b/programs/develop/ktcc/trunk/libc/start/start.asm index 090dda6c73..200b7e58ac 100644 --- a/programs/develop/ktcc/trunk/libc/start/start.asm +++ b/programs/develop/ktcc/trunk/libc/start/start.asm @@ -28,9 +28,10 @@ start: ;DEBUGF ' path "%s"\n params "%s"\n', .path, .params ; check for overflow - mov al, [path+buf_len-1] - or al, [params+buf_len-1] - jnz .crash +;; that not work +; mov al, [path+buf_len-1] +; or al, [params+buf_len-1] +; jnz .crash ; check if path written by OS mov eax, [hparams] test eax, eax @@ -126,13 +127,13 @@ public argc as '__argc' public params as '__argv' public path as '__path' -section '.bss' +section '.bss' buf_len = 0x400 max_parameters=0x20 argc rd 1 argv rd max_parameters -path rb buf_len -params rb buf_len +path rb buf_len +params rb buf_len ;section '.data' ;include_debug_strings ; ALWAYS present in data section \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdio/scanf.c b/programs/develop/ktcc/trunk/libc/stdio/scanf.c index 1594283d69..01093ea0ff 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/scanf.c +++ b/programs/develop/ktcc/trunk/libc/stdio/scanf.c @@ -47,7 +47,7 @@ int vscanf ( const char * format, va_list arg ) return format_scan(NULL, format, arg, &virtual_getc_con, &virtual_ungetc_con); }; -int kos_scanf ( const char * format, ...) +int scanf ( const char * format, ...) { va_list arg; int n; 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/stdlib/itoa.c b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c index 529243fcf4..8839cb565c 100644 --- a/programs/develop/ktcc/trunk/libc/stdlib/itoa.c +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c @@ -4,13 +4,17 @@ #include /* -** itoa(n,s) - Convert n to characters in s +** itoa(n,s) - Convert n to characters in s */ -char* itoa(int n,char* 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'; diff --git a/programs/develop/ktcc/trunk/libc/stdlib/itoab.c b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c index 6e639058f4..d7fb98c210 100644 --- a/programs/develop/ktcc/trunk/libc/stdlib/itoab.c +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c @@ -7,7 +7,7 @@ ** itoab(n,s,b) - Convert "unsigned" n to characters in s using base b. ** NOTE: This is a non-standard function. */ -char* itoab(int n,char* s,int b) +char* itoab(unsigned int n, char* s, int b) { char *ptr; int lowbit; @@ -15,7 +15,7 @@ char* itoab(int n,char* s,int b) b >>= 1; do { lowbit = n & 1; - n = (n >> 1) & 32767; + n = (n >> 1) & 0x7FFFFFFF; *ptr = ((n % b) << 1) + lowbit; if(*ptr < 10) *ptr += '0'; else *ptr += 55; ++ptr; 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/string/memcmp.c b/programs/develop/ktcc/trunk/libc/string/memcmp.c index e61e9af824..3401299f6f 100644 --- a/programs/develop/ktcc/trunk/libc/string/memcmp.c +++ b/programs/develop/ktcc/trunk/libc/string/memcmp.c @@ -10,6 +10,8 @@ int memcmp(const void* buf1,const void* buf2,size_t count) return -1; if (*(uc*)buf1>*(uc*)buf2) return 1; + (uc*)buf1++; + (uc*)buf2++; } 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/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/simplewin_old.c b/programs/develop/ktcc/trunk/samples/simplewin_old.c new file mode 100644 index 0000000000..63bf266186 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/simplewin_old.c @@ -0,0 +1,61 @@ + +// simple sample by Ghost + +#include +#include +#include + +#define FONT0 0 +#define FONT1 0x10000000 + +#define BT_NORMAL 0 +#define BT_DEL 0x80000000 +#define BT_HIDE 0x40000000 +#define BT_NOFRAME 0x20000000 + +char header[]={" -= C demo programm. Compiled whith KTCC halyavin and andrew_programmer port =- "}; + +void rotate_str(char *str){ + char tmp; + int i; + tmp = str[0]; + for(i = 1; str[i]; i++)str[i - 1] = str[i]; + str[i - 1] = tmp; +} + +void draw_window(){ + static int offs = 0; + static int fcolor = 0; + static int col = 0; + + _ksys_window_redraw(1); + _ksys_draw_window(100, 100, 300, 120, 0xaabbcc, 2, 0x5080d0, 0, 0x5080d0); + _ksys_write_text(6 - offs, 8, fcolor | FONT0, header, strlen(header)); + _ksys_draw_bar(1, 6, 5, 13, 0x05080d0); + _ksys_draw_bar(274, 6, 26, 13, 0x05080d0); + _ksys_make_button(300 - 19, 5, 12, 12, 1 | BT_NORMAL, 0x6688dd); + _ksys_window_redraw(2); + + offs = (offs + 1) % 6; + if(!offs)rotate_str(header); + + fcolor += (col)?-0x80808:0x80808; + if(fcolor > 0xf80000 || fcolor == 0)col = !col; +} + +int main(int argc, char **argv){ + + while(!0){ + switch(_ksys_wait_for_event(10)){ + case 2:return 0; + + case 3: + if(_ksys_get_button_id() == 1)return 0; + break; + + default: + draw_window(); + break; + } + } +} diff --git a/programs/develop/ktcc/trunk/samples/winbasics.c b/programs/develop/ktcc/trunk/samples/winbasics.c new file mode 100644 index 0000000000..8b8b82f370 --- /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; + +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/readme_kos32.txt b/programs/develop/ktcc/trunk/source/readme_kos32.txt index 64b3635905..11831ab263 100644 --- a/programs/develop/ktcc/trunk/source/readme_kos32.txt +++ b/programs/develop/ktcc/trunk/source/readme_kos32.txt @@ -18,8 +18,6 @@ otherwise (error) underscoring all symbols, not only cdecl -silent (kos) -> writes to debugboard -impossible using with mingw-gcc compiled lib, incompatible library format: .o is PE-format from gcc but ELF from tcc, may be linux-gcc does it ok --no symbols (mapfile) for debug, see howtodebugtcc --no debug info for -g (kos32 linker imperfection) -__fastcall incompatible with other compilers. now stack freed by caller. must fix i386-gen.c@490,572 (fixed in other branch https://github.com/mirror/tinycc) @@ -30,8 +28,6 @@ otherwise (error) underscoring all symbols, not only cdecl -not working: default search path are ./include ./lib from executable --under KOS need to use -Bpath_to_ktcc --start.o not found using -B (kos) - put near your.c file --if static var sized more than 14096+ -> crash compiled .exe (kos) ----^ stack size set in menuet header at compile time tccmeos.c:177 about 4k -bench timing coarse (0s or 1s), no usec in newlib gettimeofday. OK Tests status: @@ -93,9 +89,6 @@ setvbuf stdlib.h: -atof -atol -strtol, strtoul atexit getenv system @@ -114,13 +107,12 @@ strxfrm Status or libc tests ---FAILED--- -tstring - need to fix +strtoul incorrect work with big unsigned > MAX_LONG ---NOT TESTED--- no library fns realized qsort -strtol time ---HANG--- @@ -129,9 +121,9 @@ sscanf ---STACK IS SMALL--- +use new -stack=1280000 option to pass test tstring strtodlong -use new -stack=1280000 option --other-- @@ -141,6 +133,7 @@ fscanf snprintf -some format misturbances +-may incorrect prints unsigned > 2147483647L ungetc -ungetc fails if filepos == 0 - no tricks diff --git a/programs/develop/ktcc/trunk/source/tccmeos.c b/programs/develop/ktcc/trunk/source/tccmeos.c index b02ba0dd44..e8d38c68ce 100644 --- a/programs/develop/ktcc/trunk/source/tccmeos.c +++ b/programs/develop/ktcc/trunk/source/tccmeos.c @@ -358,7 +358,8 @@ int tcc_output_dbgme(const char *filename, me_info* me) // return 1 on error { FILE *fdbg; - char fname[400], buf[200]; + char fname[400], + buf[80]; // no more fits in mtdbg string strcpy(fname, filename); strcat(fname, ".dbg");