/programs/develop/tinypy/examples/rw_file.py |
---|
1,13 → 1,13 |
# Read/Write file example |
# Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 |
import ksys # KolibriOS syscalls |
import file |
fw=ksys.open('my.txt','w') # Open file for writing |
fw=file.open('my.txt','w') # Open file for writing |
fw.write("I love KolibriOS") # Write symbols to my.txt file |
fw.close() # Close file |
fr=ksys.open('my.txt', 'r') # Open file for reading |
fr=file.open('my.txt', 'r') # Open file for reading |
str=fr.read() # Read symbols from file |
print(str) # Print to console |
fr.close() # Close file |
/programs/develop/tinypy/std_modules/bitwise/bitwise.c |
---|
11,3 → 11,10 |
return tp_number(num1 | num2); |
} |
static tp_obj _mul(TP){ |
unsigned num1 = (unsigned)GET_NUM_ARG(); |
unsigned num2 = (unsigned)GET_NUM_ARG(); |
return tp_number(num1 & num2); |
} |
/programs/develop/tinypy/std_modules/bitwise/init.c |
---|
8,6 → 8,7 |
tp_obj bit_mod = tp_dict(tp); |
EXPORT(bit_mod, "add" , _add); |
EXPORT(bit_mod, "mul" , _mul); |
tp_set(tp, bit_mod, tp_string("__doc__"), tp_string("Bitwise operations for large numbers")); |
tp_set(tp, bit_mod, tp_string("__name__"), tp_string("bitwise")); |
/programs/develop/tinypy/std_modules/file/fs.c |
---|
0,0 → 1,255 |
#include <string.h> |
#include "tinypy.h" |
#define call70(par, st) __asm__ __volatile__("int $0x40":"=a"(st):"a"(70), "b"(par)) |
#define call70_rw(par, st, cnt) __asm__ __volatile__ ("int $0x40":"=a"(st), "=b"(cnt):"a"(70), "b"(par)) |
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val |
typedef unsigned int uint32_t; |
typedef unsigned char uint8_t; |
#pragma pack(push, 1) |
typedef struct { |
uint32_t subfnc; |
uint32_t res1; |
uint32_t res2; |
uint32_t res3; |
uint8_t *data; |
uint8_t res4; |
char *fn; |
}info_params_t; |
#pragma pack(pop) |
#pragma pack(push,1) |
typedef struct { |
uint32_t subfnc; |
uint32_t pos; |
uint32_t res1; |
uint32_t cnt; |
char* data; |
uint8_t res2; |
char* fn; |
}rw_params_t; |
#pragma pack(pop) |
static tp_obj kolibri_close(TP) |
{ |
tp_obj self = TP_TYPE(TP_DICT); |
uint32_t size = tp_get(tp, self, tp_string("size")).number.val; |
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val; |
tp_set(tp, self, tp_string("closed"), tp_True); |
return tp_None; |
} |
static tp_obj kolibri_read(TP) |
{ |
uint32_t status, num; |
tp_obj self = TP_TYPE(TP_DICT); |
uint32_t pos = tp_get(tp, self, tp_string("pos")).number.val; |
uint32_t size = tp_get(tp, self, tp_string("size")).number.val; |
uint32_t cnt; |
char *buf = (char *)malloc(size - pos); |
rw_params_t params = {0, pos, 0, size - pos, buf, 0, |
(char *)tp_get(tp, self, tp_string("name")).string.val}; |
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val; |
if (*mode != 'r') |
tp_raise_f(tp_None, "IOError: file not open for reading", tp_None); |
if (!buf) |
return tp_None; |
call70_rw((¶ms), status, cnt); |
buf[cnt] = '\0'; |
return tp_string(buf); |
} |
#if 0 |
static tp_obj kolibri_readline(TP) |
{ |
return tp_string("Line"); |
} |
static tp_obj kolibri_readlines(TP) |
{ |
tp_obj result = tp_list(tp); |
int i; |
for(i=0; i < 5; i++) |
tp_add(result, tp_string("Line")); |
return result; |
} |
#endif |
/* Write object to file. |
* |
* tp TinyPy virtual machine structure. |
* |
* returns tp_True |
*/ |
static tp_obj kolibri_write(TP) |
{ |
tp_obj self = TP_TYPE(TP_DICT); |
tp_obj obj = TP_OBJ(); /* What to write. */ |
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val; |
uint32_t pos = (uint32_t)tp_get(tp, self, tp_string("pos")).number.val; |
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val; |
if (*mode != 'w' && *mode != 'a') |
{ |
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None); |
} |
else |
{ |
char *data = (char *)TP_CSTR(obj); |
rw_params_t params = {3, pos, 0, strlen(data), data, 0, |
(char *)tp_get(tp, self, tp_string("name")).string.val}; |
uint32_t status; |
uint32_t cnt; |
call70_rw((¶ms), status, cnt); |
if (status) |
{ |
tp_raise_f(tp_None, "IOError: writing failed with status %d", status); |
} |
pos += cnt; |
tp_set(tp, self, tp_string("pos"), tp_number(pos)); |
if (pos > size) |
{ |
/* If writing has come beyond the file, increase its size. */ |
tp_set(tp, self, tp_string("size"), tp_number(pos)); |
} |
return tp_True; |
} |
} |
/* Write line list into file. |
* |
* tp TinyPy virtual machine structure. |
* |
* returns tp_None. |
*/ |
static tp_obj kolibri_writelines(TP) |
{ |
tp_obj result = tp_None; |
tp_obj self = TP_TYPE(TP_DICT); |
tp_obj list = TP_TYPE(TP_LIST); /* What to write. */ |
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val; |
long pos = (long)tp_get(tp, self, tp_string("pos")).number.val; |
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val; |
if (*mode != 'w' && *mode != 'a') |
{ |
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None); |
} |
else |
{ |
int i; |
char *fn = (char *)tp_get(tp, self, tp_string("name")).string.val; |
rw_params_t params = {3, 0, 0, 0, NULL, 0, fn}; |
for (i = 0; i < list.list.val->len; i++) |
{ |
char *data = (char *)TP_CSTR(list.list.val->items[i]); |
uint32_t status; |
uint32_t cnt; |
params.data = data; |
params.cnt = strlen(data); |
params.pos = pos; |
call70_rw((¶ms), status, cnt); |
if (status) |
tp_raise_f(tp_None, "IOError: writing failed with status %d", status); |
pos += cnt; |
} |
tp_set(tp, self, tp_string("pos"), tp_number(pos)); |
if (pos > size) |
{ |
/* If writing has come beyond the file, increase its size. */ |
tp_set(tp, self, tp_string("size"), tp_number(pos)); |
} |
return tp_True; |
} |
} |
/* Get file size. |
* |
* fn ASCIIZ absolute file path. |
*/ |
long long int kolibri_filesize(const char *fn) |
{ |
uint8_t data[40]; |
uint32_t status; |
long long int result; |
info_params_t params = {5, 0, 0, 0, data, 0, (char *)fn}; |
call70((¶ms), status); |
/* File size is at offset 32. */ |
if (status == 0) |
result = *(long long*)(&data[32]); |
else |
result = -status; |
return result; |
} |
/* Open file. |
* |
* tp TinyPy virtual machine structure. |
* |
* returns file object. |
*/ |
tp_obj kolibri_open(TP) { |
tp_obj obj; |
char *fn = (char *)(TP_TYPE(TP_STRING).string.val); |
tp_obj mode_obj = TP_OBJ(); |
long long int size; |
long long int pos = 0; |
uint32_t status; |
info_params_t params = {2, 0, 0, 0, NULL, 0, fn}; |
if (mode_obj.type == TP_NONE) |
mode_obj = tp_string("r"); |
else if (mode_obj.type != TP_STRING) |
tp_raise_f(tp_None, "ValueError: bad file access mode %s", TP_CSTR(mode_obj)); |
switch(mode_obj.string.val[0]) |
{ |
case 'w': |
call70((¶ms), status); |
if (status) |
tp_raise_f(tp_None, "IOError: cannot open file for writing", tp_None); |
size = 0; |
break; |
case 'a': |
pos = size; |
break; |
case 'r': |
break; |
default: |
tp_raise_f(tp_None, "ValueError: mode string must begin with 'r', 'w', or 'a'", tp_None); |
} |
if ((size = kolibri_filesize(fn)) < 0) |
tp_raise_f(tp_None, "IOError: filesize returned %d", tp_number(size)); |
obj = tp_dict(tp); |
tp_set(tp, obj, tp_string("name"), tp_string(fn)); |
tp_set(tp, obj, tp_string("size"), tp_number(size)); |
tp_set(tp, obj, tp_string("pos"), tp_number(pos)); |
tp_set(tp, obj, tp_string("mode"), mode_obj); |
#if 0 |
tp_set(tp, obj, tp_string("__doc__"), |
tp_string("File object.\nAttributes:\n name: file name\n" |
" closed: boolean indicating whether file was closed\n" |
"Methods:\n read: read the entire file into string\n" |
" readlines: get list of lines\n" |
" close: close the file\n" |
)); |
#endif |
tp_set(tp, obj, tp_string("closed"), tp_False); |
tp_set(tp, obj, tp_string("close"), tp_method(tp, obj, kolibri_close)); |
tp_set(tp, obj, tp_string("read"), tp_method(tp, obj, kolibri_read)); |
tp_set(tp, obj, tp_string("write"), tp_method(tp, obj, kolibri_write)); |
tp_set(tp, obj, tp_string("writelines"), tp_method(tp, obj, kolibri_writelines)); |
return obj; |
} |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/programs/develop/tinypy/std_modules/file/init.c |
---|
0,0 → 1,20 |
#include "tinypy.h" |
#include "fs.c" |
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT)) |
extern tp_obj tp_dict(TP); |
extern tp_obj tp_fnc(TP,tp_obj v(TP)); |
void file_init(TP) |
{ |
tp_obj file_mod = tp_dict(tp); |
EXPORT(file_mod, "open" , kolibri_open); |
tp_set(tp, file_mod, tp_string("__doc__"), tp_string("File module (read / write)")); |
tp_set(tp, file_mod, tp_string("__name__"), tp_string("File")); |
tp_set(tp, file_mod, tp_string("__file__"), tp_string(__FILE__)); |
tp_set(tp, tp->modules, tp_string("file"), file_mod); |
} |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/programs/develop/tinypy/std_modules/ksys/net.c |
---|
File deleted |
/programs/develop/tinypy/std_modules/ksys/fs.c |
---|
File deleted |
Property changes: |
Deleted: svn:executable |
-* |
\ No newline at end of property |
/programs/develop/tinypy/std_modules/ksys/init.c |
---|
1,6 → 1,5 |
#include "tinypy.h" |
#include "syscalls.c" |
#include "fs.c" |
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT)) |
21,8 → 20,7 |
EXPORT(ksys_mod, "get_event" , _get_event); |
EXPORT(ksys_mod, "get_button" ,_get_button); |
EXPORT(ksys_mod, "get_sys_colors",_get_sys_colors); |
// filesystem |
EXPORT(ksys_mod, "open", kolibri_open); |
EXPORT(ksys_mod, "get_key" , _get_key); |
tp_set(tp, ksys_mod, tp_string("__doc__"), tp_string("KolibriOS system specific functions.")); |
tp_set(tp, ksys_mod, tp_string("__name__"), tp_string("kolibri")); |
/programs/develop/tinypy/std_modules/ksys/start_app.c |
---|
0,0 → 1,22 |
static inline |
int start_app(char *app_name, char *args){ |
#pragma pack(push, 1) |
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; |
#pragma pack(pop) |
register int val; |
asm volatile ("int $0x40":"=a"(val):"a"(70), "b"(&file_op)); |
return val; |
} |
/programs/develop/tinypy/std_modules/ksys/syscalls.c |
---|
74,6 → 74,21 |
return tp_number(get_os_button()); |
} |
static tp_obj _get_key(TP){ |
tp_obj key_obj = tp_dict(tp); |
oskey_t key_info = get_key(); |
tp_set(tp, key_obj, tp_string("code"), tp_number(key_info.code)); |
tp_set(tp, key_obj, tp_string("ctrl_key"), tp_number(key_info.ctrl_key)); |
tp_set(tp, key_obj, tp_string("state"), tp_number(key_info.state)); |
tp_set(tp, key_obj, tp_string("val"), tp_number(key_info.val)); |
return key_obj; |
} |
static tp_obj _start_app(TP){ |
const char *prog_name = GET_STR_ARG(); |
const char *args = GET_STR_ARG(); |
} |
static tp_obj _get_sys_colors(TP){ |
tp_obj color_obj = tp_dict(tp); |
struct kolibri_system_colors colors; |
/programs/develop/tinypy/std_modules/math/math.c |
---|
1,5 → 1,5 |
#include <math.h> |
#include "tinypy.h" |
#include <tinypy.h> |
#ifndef M_E |
#define M_E 2.7182818284590452354 |
/programs/develop/tinypy/std_modules/modules.c |
---|
4,6 → 4,8 |
#include "ksys/init.c" |
#include "pygame/init.c" |
#include "bitwise/init.c" |
#include "file/init.c" |
//#include "socket/socket.c" |
void init_std_modules(TP){ |
math_init(tp); |
10,6 → 12,8 |
random_init(tp); |
re_init(tp); |
ksys_init(tp); |
file_init(tp); |
//socket_init(tp); |
pygame_init(tp); |
bitwise_init(tp); |
} |
/programs/develop/tinypy/std_modules/socket/init.c |
---|
0,0 → 1,34 |
#include "tinypy.h" |
#include "net.c" |
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT)) |
extern tp_obj tp_dict(TP); |
extern tp_obj tp_fnc(TP,tp_obj v(TP)); |
void socket_init(TP) |
{ |
tp_obj socket_mod = tp_dict(tp); |
EXPORT(socket_mod, "socket" , _socket); |
tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET)); |
tp_set(tp, socket_mod, tp_string("AF_INET6"), tp_number(AF_INET6)); |
tp_set(tp, socket_mod, tp_string("AF_LOCAL"), tp_number(AF_LOCAL)); |
tp_set(tp, socket_mod, tp_string("AF_UNSPEC"), tp_number(AF_UNSPEC)); |
tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM)); |
tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM)); |
tp_set(tp, socket_mod, tp_string("SOCK_RAW"), tp_number(SOCK_RAW)); |
tp_set(tp, socket_mod, tp_string("IPPROTO_IP"), tp_number(IPPROTO_IP)); |
tp_set(tp, socket_mod, tp_string("IPPROTO_TCP"), tp_number(IPPROTO_TCP)); |
tp_set(tp, socket_mod, tp_string("IPPROTO_UDP"), tp_number(IPPROTO_UDP)); |
tp_set(tp, socket_mod, tp_string("IPPROTO_RAW"), tp_number(IPPROTO_RAW)); |
tp_set(tp, socket_mod, tp_string("IPPROTO_ICMP"), tp_number(IPPROTO_ICMP)); |
tp_set(tp, socket_mod, tp_string("__doc__"), tp_string("Working with network sockets")); |
tp_set(tp, socket_mod, tp_string("__name__"), tp_string("Sockets")); |
tp_set(tp, socket_mod, tp_string("__file__"), tp_string(__FILE__)); |
tp_set(tp, tp->modules, tp_string("socket"), socket_mod); |
} |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/programs/develop/tinypy/std_modules/socket/socket.c |
---|
0,0 → 1,111 |
#include "tinypy.h" |
#include <sys/socket.h> |
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val |
#define NET_ERROR(FUNC, MSG) if(FUNC==-1){ tp_raise_f(tp_None, "%s Error code: %d", MSG , err_code);} |
static tp_obj inet_pton(TP, const char *buf, int len, unsigned *addr) |
{ |
char *p = (char *)buf; |
int i = 0; |
unsigned val = 0; |
*addr = 0; |
while (*p && p < buf + len && i < 4){ |
if (*p == '.' || !*p){ |
if (val > 255) |
tp_raise(tp_None, tp_string("ValueError: number > 255 in IP address")); |
*addr += (val << ((i++) << 3)); |
val = 0; |
}else{ |
if (*p < '0' || *p > '9') |
tp_raise(tp_None, tp_string("ValueError: bad char in IP address, digit expected")); |
val = val * 10 + *p - '0'; |
} |
p++; |
} |
if (!*p){ |
if (i == 3){ |
*addr += (val << ((i++) << 3)); |
}else{ |
tp_raise_f(tp_None, "ValueError: bad IP address!", tp_None); |
} |
} |
} |
static tp_obj _close(TP){ |
tp_obj socket_info=TP_TYPE(TP_DICT); |
int id = (int)tp_get(tp, socket_info, tp_string("id")).number.val; |
NET_ERROR(close(id), "Unable to close socket!") |
else{ |
tp_set(tp, socket_info, tp_string("closed"), tp_True); |
} |
return tp_None; |
} |
static tp_obj _bind(TP) |
{ |
tp_obj self = TP_TYPE(TP_DICT); |
const char* local_addr = TP_TYPE(TP_STRING).string.val; |
unsigned local_port = PORT((unsigned)TP_NUM()); |
unsigned addr_n; |
inet_pton(tp, local_addr, strlen(local_addr), &addr_n); |
unsigned family = (unsigned)tp_get(tp, self, tp_string("domain")).number.val; |
int id = (int)tp_get(tp, self, tp_string("id")).number.val; |
struct sockaddr addr={addr_n, family, local_port,0}; |
NET_ERROR(bind(id, &addr, sizeof(addr)), "Bind error!"); |
return tp_None; |
} |
static tp_obj _listen(TP){ |
int backlog = (int)TP_NUM(); |
tp_obj socket_obj = TP_TYPE(TP_DICT); |
int id = (int)tp_get(tp,socket_obj, tp_string("id")).number.val; |
NET_ERROR(listen(id, backlog), "Listen error!"); |
return tp_None; |
} |
static tp_obj _send(TP){ |
const char * msg = GET_STR_ARG(); |
tp_obj socket_obj = TP_TYPE(TP_DICT); |
int id = (int)tp_get(tp,socket_obj, tp_string("id")).number.val; |
NET_ERROR(send(id, (const void*)msg, strlen(msg), MSG_NOFLAG),"Sending failed!"); |
return tp_None; |
} |
static tp_obj _connect(TP) |
{ |
tp_obj self = TP_TYPE(TP_DICT); |
const char* local_addr = TP_TYPE(TP_STRING).string.val; |
unsigned local_port = PORT((unsigned)TP_NUM()); |
unsigned addr_n; |
inet_pton(tp, local_addr, strlen(local_addr), &addr_n); |
unsigned family = (unsigned)tp_get(tp, self, tp_string("domain")).number.val; |
int id = (int)tp_get(tp, self, tp_string("id")).number.val; |
struct sockaddr addr={addr_n, family, local_port,0}; |
NET_ERROR(connect(id, &addr, sizeof(addr)), "Connection failed!"); |
return tp_None; |
} |
static tp_obj _socket(TP){ |
int domain = (int)TP_NUM(); |
int type = (int)TP_NUM(); |
int protocol=(int)TP_NUM(); |
int id = socket(domain, type, protocol); |
tp_obj socket_info=tp_dict(tp); |
NET_ERROR(id, "Unable to open socket!") |
else{ |
tp_set(tp, socket_info, tp_string("id"), tp_number(id)); |
tp_set(tp, socket_info, tp_string("domain"), tp_number(domain)); |
tp_set(tp, socket_info, tp_string("type"), tp_number(type)); |
tp_set(tp, socket_info, tp_string("protocol"), tp_number(protocol)); |
} |
tp_set(tp, socket_info, tp_string("connect"), tp_method(tp, socket_info, _connect)); |
tp_set(tp, socket_info, tp_string("bind"), tp_method(tp, socket_info, _bind)); |
tp_set(tp, socket_info, tp_string("close"), tp_method(tp, socket_info, _close)); |
tp_set(tp, socket_info, tp_string("send"), tp_method(tp, socket_info, _send)); |
return socket_info; |
} |
/programs/develop/tinypy/tinypy/Makefile |
---|
2,6 → 2,8 |
CC = kos32-gcc |
LD = kos32-ld |
SDK_DIR:=$(abspath ../../../../contrib/sdk) |
LDFLAGS = -static -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds \ |
--image-base 0 -lgcc -lSDLn -lc.dll -lsound |
8,7 → 10,7 |
SDL_DIR = $(SDK_DIR)/sources/SDL-1.2.2_newlib |
CFLAGS = -DCONIO -U_Win32 -U_WIN32 -U__MINGW32__ -mpreferred-stack-boundary=2 \ |
-mincoming-stack-boundary=2 -fno-builtin -fno-common -O3 |
-mincoming-stack-boundary=2 -fno-builtin -fno-common -O0 |
INCLUDES= -I. -I$(SDK_DIR)/sources/newlib/libc/include -I$(SDL_DIR)/include |
LIBPATH:= -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib -L. |
15,7 → 17,7 |
STD_MODULES= ../std_modules/modules.o |
OBJECTS = tpmain.o $(STD_MODULES) |
OBJECTS = $(STD_MODULES) tpmain.o |
all:$(NAME) |