forked from KolibriOS/kolibrios
Added new functions in ksys module
Added socket module(WIP) git-svn-id: svn://kolibrios.org@8578 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
0737264612
commit
fb583c04b3
@ -1,13 +1,13 @@
|
|||||||
# Read/Write file example
|
# Read/Write file example
|
||||||
# Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3
|
# 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.write("I love KolibriOS") # Write symbols to my.txt file
|
||||||
fw.close() # Close 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
|
str=fr.read() # Read symbols from file
|
||||||
print(str) # Print to console
|
print(str) # Print to console
|
||||||
fr.close() # Close file
|
fr.close() # Close file
|
||||||
|
@ -11,3 +11,10 @@ static tp_obj _add(TP){
|
|||||||
return tp_number(num1 | num2);
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ void bitwise_init(TP)
|
|||||||
tp_obj bit_mod = tp_dict(tp);
|
tp_obj bit_mod = tp_dict(tp);
|
||||||
|
|
||||||
EXPORT(bit_mod, "add" , _add);
|
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("__doc__"), tp_string("Bitwise operations for large numbers"));
|
||||||
tp_set(tp, bit_mod, tp_string("__name__"), tp_string("bitwise"));
|
tp_set(tp, bit_mod, tp_string("__name__"), tp_string("bitwise"));
|
||||||
|
20
programs/develop/tinypy/std_modules/file/init.c
Executable file
20
programs/develop/tinypy/std_modules/file/init.c
Executable file
@ -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);
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
#include "tinypy.h"
|
#include "tinypy.h"
|
||||||
#include "syscalls.c"
|
#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))
|
#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 @@ void ksys_init(TP)
|
|||||||
EXPORT(ksys_mod, "get_event" , _get_event);
|
EXPORT(ksys_mod, "get_event" , _get_event);
|
||||||
EXPORT(ksys_mod, "get_button" ,_get_button);
|
EXPORT(ksys_mod, "get_button" ,_get_button);
|
||||||
EXPORT(ksys_mod, "get_sys_colors",_get_sys_colors);
|
EXPORT(ksys_mod, "get_sys_colors",_get_sys_colors);
|
||||||
// filesystem
|
EXPORT(ksys_mod, "get_key" , _get_key);
|
||||||
EXPORT(ksys_mod, "open", kolibri_open);
|
|
||||||
|
|
||||||
tp_set(tp, ksys_mod, tp_string("__doc__"), tp_string("KolibriOS system specific functions."));
|
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"));
|
tp_set(tp, ksys_mod, tp_string("__name__"), tp_string("kolibri"));
|
||||||
|
@ -1,296 +0,0 @@
|
|||||||
#include <sys/socket.h>
|
|
||||||
// #include <menuet/net.h>
|
|
||||||
|
|
||||||
#include "tp.h"
|
|
||||||
|
|
||||||
extern tp_obj tp_dict(TP);
|
|
||||||
extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
|
|
||||||
extern tp_obj tp_fnc(TP,tp_obj v(TP));
|
|
||||||
extern tp_obj tp_get(TP,tp_obj self, tp_obj k);
|
|
||||||
tp_obj tp_has(TP,tp_obj self, tp_obj k);
|
|
||||||
// #define _cdecl __attribute__((cdecl))
|
|
||||||
extern int (* _cdecl con_printf)(const char* format,...);
|
|
||||||
|
|
||||||
#define PRECISION 0.000001
|
|
||||||
|
|
||||||
#define GET_SOCKET_DESCRIPTOR(_obj, _sock) do{ \
|
|
||||||
if (fabs(tp_has(tp, _obj, tp_string("socket")).number.val) < PRECISION)\
|
|
||||||
tp_raise(tp_None, "Socket not open", tp_None); \
|
|
||||||
_sock = (__u32)(tp_get(tp, _obj, tp_string("socket")).number.val + PRECISION);\
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/* Socket close method.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* s.close() # s must be a socket object created by socket.
|
|
||||||
*
|
|
||||||
* Raises exception if socket was not opened. Otherwise returns True.
|
|
||||||
*/
|
|
||||||
static tp_obj kolibri_close_socket(TP)
|
|
||||||
{
|
|
||||||
// tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
// __u32 socktype;
|
|
||||||
// __u32 s;
|
|
||||||
|
|
||||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
|
||||||
|
|
||||||
// socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
|
||||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
|
||||||
// if (socktype == SOCK_STREAM)
|
|
||||||
// __menuet__close_TCP_socket(s);
|
|
||||||
// else if (socktype == SOCK_DGRAM)
|
|
||||||
// __menuet__close_UDP_socket(s);
|
|
||||||
return tp_True;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Socket send method.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
|
|
||||||
* s.send(data)
|
|
||||||
* or:
|
|
||||||
* s.send(data, 20) # Send just 20 bytes
|
|
||||||
*/
|
|
||||||
static tp_obj kolibri_send(TP)
|
|
||||||
{
|
|
||||||
// tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
// tp_obj data_obj = TP_TYPE(TP_STRING);
|
|
||||||
// __u32 datalen = TP_DEFAULT(tp_False).number.val;
|
|
||||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
|
||||||
// __u32 s;
|
|
||||||
// int result;
|
|
||||||
|
|
||||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
|
||||||
|
|
||||||
// if (datalen < 0 || datalen > data_obj.string.len)
|
|
||||||
// datalen = data_obj.string.len;
|
|
||||||
// if (socktype == SOCK_STREAM)
|
|
||||||
// result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
|
|
||||||
// else if (socktype == SOCK_DGRAM)
|
|
||||||
// result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
|
|
||||||
// return tp_number(!(result != 0));
|
|
||||||
return tp_number(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define __u32 unsigned int
|
|
||||||
#define __u8 unsigned char
|
|
||||||
|
|
||||||
/* Socket recv method.
|
|
||||||
*
|
|
||||||
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
|
|
||||||
* s.recv(data)
|
|
||||||
* or:
|
|
||||||
* s.recv(data, 20) # Send just 20 bytes
|
|
||||||
*/
|
|
||||||
static tp_obj kolibri_recv(TP)
|
|
||||||
{
|
|
||||||
tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
__u32 datalen = TP_DEFAULT(tp_False).number.val;
|
|
||||||
__u32 s;
|
|
||||||
__u8 c;
|
|
||||||
__u8 *buf, *p;
|
|
||||||
__u32 buf_size;
|
|
||||||
__u32 bytes_read = 0;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
|
||||||
|
|
||||||
// if (datalen)
|
|
||||||
// buf_size = datalen;
|
|
||||||
// else
|
|
||||||
// buf_size = 2048;
|
|
||||||
// if (!(buf = malloc(datalen)))
|
|
||||||
// tp_raise(tp_None, "Cannot allocate buffer for received data", tp_None);
|
|
||||||
// p = buf;
|
|
||||||
// while (__menuet__read_socket(s, &c) && bytes_read < buf_size)
|
|
||||||
// {
|
|
||||||
// *p++ = c;
|
|
||||||
// bytes_read++;
|
|
||||||
// if (bytes_read >= buf_size && !datalen)
|
|
||||||
// {
|
|
||||||
// buf_size += 1024;
|
|
||||||
// buf = realloc(buf, buf_size);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
return tp_string_n(buf, bytes_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void inet_pton(TP, const char *buf, int len, __u32 *addr)
|
|
||||||
{
|
|
||||||
char *p = (char *)buf;
|
|
||||||
int i = 0;
|
|
||||||
__u32 val = 0;
|
|
||||||
*addr = 0;
|
|
||||||
while (*p && p < buf + len && i < 4)
|
|
||||||
{
|
|
||||||
if (*p == '.' || !*p)
|
|
||||||
{
|
|
||||||
if (val > 255)
|
|
||||||
tp_raise(tp_None, "ValueError: number > 255 in IP address", tp_None);
|
|
||||||
*addr += (val << ((i++) << 3));
|
|
||||||
val = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (*p < '0' || *p > '9')
|
|
||||||
tp_raise(tp_None, "ValueError: bad char in IP address, digit expected", tp_None);
|
|
||||||
val = val * 10 + *p - '0';
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
if (!*p)
|
|
||||||
{
|
|
||||||
if (i == 3)
|
|
||||||
*addr += (val << ((i++) << 3));
|
|
||||||
else
|
|
||||||
tp_raise(tp_None, "ValueError: bad IP address", tp_None);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Converter from string presentation to binary address. */
|
|
||||||
static tp_obj kolibri_inet_pton(TP)
|
|
||||||
{
|
|
||||||
tp_obj obj;
|
|
||||||
__u32 addr;
|
|
||||||
obj = TP_TYPE(TP_STRING);
|
|
||||||
// inet_pton(tp, (char *)obj.string.val, (int)obj.string.len, &addr);
|
|
||||||
return tp_number(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Socket bind method.
|
|
||||||
*
|
|
||||||
* In KolibriOS it just sets local address and port.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000
|
|
||||||
*/
|
|
||||||
tp_obj kolibri_bind(TP)
|
|
||||||
{
|
|
||||||
// tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
// tp_obj local_addr_obj = TP_OBJ();
|
|
||||||
// __u32 local_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
|
||||||
// __u32 local_addr;
|
|
||||||
|
|
||||||
// if (local_addr_obj.type == TP_NUMBER)
|
|
||||||
// local_addr = local_addr_obj.number.val;
|
|
||||||
// else if (local_addr_obj.type == TP_STRING)
|
|
||||||
// inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr);
|
|
||||||
|
|
||||||
// tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr));
|
|
||||||
// tp_set(tp, self, tp_string("local_port"), tp_number(local_port));
|
|
||||||
return tp_None;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Socket connect method.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
|
|
||||||
*/
|
|
||||||
tp_obj kolibri_connect(TP)
|
|
||||||
{
|
|
||||||
// tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
// tp_obj remote_addr_obj = TP_OBJ();
|
|
||||||
// __u32 remote_addr;
|
|
||||||
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
|
||||||
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
|
|
||||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
|
||||||
// int s = -1; /* Socket descriptor */
|
|
||||||
|
|
||||||
|
|
||||||
// if (remote_addr_obj.type == TP_NUMBER)
|
|
||||||
// remote_addr = remote_addr_obj.number.val;
|
|
||||||
// else if (remote_addr_obj.type == TP_STRING)
|
|
||||||
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
|
|
||||||
|
|
||||||
// if (socktype == SOCK_STREAM)
|
|
||||||
// s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
|
|
||||||
// else if (socktype == SOCK_DGRAM)
|
|
||||||
// s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
|
|
||||||
// if (s >= 0)
|
|
||||||
// {
|
|
||||||
// tp_set(tp, self, tp_string("socket"), tp_number(s));
|
|
||||||
// return tp_True;
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
return tp_False;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Socket listen method.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* s.listen('10.10.1.1', 5000)
|
|
||||||
*/
|
|
||||||
tp_obj kolibri_listen(TP)
|
|
||||||
{
|
|
||||||
// tp_obj self = TP_TYPE(TP_DICT);
|
|
||||||
// tp_obj remote_addr_obj = TP_OBJ();
|
|
||||||
// __u32 remote_addr;
|
|
||||||
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
|
||||||
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
|
|
||||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
|
||||||
// int s = -1; /* Socket descriptor */
|
|
||||||
|
|
||||||
// if (socktype != SOCK_STREAM)
|
|
||||||
// tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);
|
|
||||||
|
|
||||||
// if (remote_addr_obj.type == TP_NUMBER)
|
|
||||||
// remote_addr = remote_addr_obj.number.val;
|
|
||||||
// else if (remote_addr_obj.type == TP_STRING)
|
|
||||||
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
|
|
||||||
|
|
||||||
// if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
|
|
||||||
// {
|
|
||||||
// tp_set(tp, self, tp_string("socket"), tp_number(s));
|
|
||||||
// return tp_True;
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
return tp_False;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Exported function.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
*
|
|
||||||
* s = socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
*
|
|
||||||
* Returns socket object.
|
|
||||||
*/
|
|
||||||
tp_obj kolibri_socket(TP)
|
|
||||||
{
|
|
||||||
tp_obj s;
|
|
||||||
tp_obj sockfamily = TP_TYPE(TP_NUMBER);
|
|
||||||
tp_obj socktype = TP_TYPE(TP_NUMBER);
|
|
||||||
|
|
||||||
// if (fabs(sockfamily.number.val - AF_INET) > PRECISION ||
|
|
||||||
// (fabs(socktype.number.val - SOCK_STREAM) > PRECISION &&
|
|
||||||
// fabs(socktype.number.val - SOCK_DGRAM) > PRECISION))
|
|
||||||
// return tp_None;
|
|
||||||
s = tp_dict(tp);
|
|
||||||
// tp_set(tp, s, tp_string("family"), sockfamily);
|
|
||||||
// tp_set(tp, s, tp_string("type"), socktype);
|
|
||||||
// tp_set(tp, s, tp_string("bind"), tp_method(tp, s, kolibri_bind));
|
|
||||||
// tp_set(tp, s, tp_string("connect"), tp_method(tp, s, kolibri_connect));
|
|
||||||
// tp_set(tp, s, tp_string("send"), tp_method(tp, s, kolibri_send));
|
|
||||||
// tp_set(tp, s, tp_string("recv"), tp_method(tp, s, kolibri_recv));
|
|
||||||
// tp_set(tp, s, tp_string("close"), tp_method(tp, s, kolibri_close_socket));
|
|
||||||
// if (fabs(socktype.number.val - SOCK_STREAM) < PRECISION)
|
|
||||||
// tp_set(tp, s, tp_string("listen"), tp_method(tp, s, kolibri_listen));
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
tp_obj kolibri_socket_module(TP)
|
|
||||||
{
|
|
||||||
tp_obj socket_mod = tp_dict(tp);
|
|
||||||
|
|
||||||
// tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
|
|
||||||
// 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("AF_INET"), tp_number(0));
|
|
||||||
tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(0));
|
|
||||||
tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(0));
|
|
||||||
tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
|
|
||||||
tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
|
|
||||||
return socket_mod;
|
|
||||||
}
|
|
22
programs/develop/tinypy/std_modules/ksys/start_app.c
Normal file
22
programs/develop/tinypy/std_modules/ksys/start_app.c
Normal file
@ -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;
|
||||||
|
}
|
@ -74,6 +74,21 @@ static tp_obj _get_button(TP){
|
|||||||
return tp_number(get_os_button());
|
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){
|
static tp_obj _get_sys_colors(TP){
|
||||||
tp_obj color_obj = tp_dict(tp);
|
tp_obj color_obj = tp_dict(tp);
|
||||||
struct kolibri_system_colors colors;
|
struct kolibri_system_colors colors;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "tinypy.h"
|
#include <tinypy.h>
|
||||||
|
|
||||||
#ifndef M_E
|
#ifndef M_E
|
||||||
#define M_E 2.7182818284590452354
|
#define M_E 2.7182818284590452354
|
||||||
|
@ -4,12 +4,16 @@
|
|||||||
#include "ksys/init.c"
|
#include "ksys/init.c"
|
||||||
#include "pygame/init.c"
|
#include "pygame/init.c"
|
||||||
#include "bitwise/init.c"
|
#include "bitwise/init.c"
|
||||||
|
#include "file/init.c"
|
||||||
|
//#include "socket/socket.c"
|
||||||
|
|
||||||
void init_std_modules(TP){
|
void init_std_modules(TP){
|
||||||
math_init(tp);
|
math_init(tp);
|
||||||
random_init(tp);
|
random_init(tp);
|
||||||
re_init(tp);
|
re_init(tp);
|
||||||
ksys_init(tp);
|
ksys_init(tp);
|
||||||
|
file_init(tp);
|
||||||
|
//socket_init(tp);
|
||||||
pygame_init(tp);
|
pygame_init(tp);
|
||||||
bitwise_init(tp);
|
bitwise_init(tp);
|
||||||
}
|
}
|
||||||
|
34
programs/develop/tinypy/std_modules/socket/init.c
Executable file
34
programs/develop/tinypy/std_modules/socket/init.c
Executable file
@ -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);
|
||||||
|
}
|
111
programs/develop/tinypy/std_modules/socket/socket.c
Normal file
111
programs/develop/tinypy/std_modules/socket/socket.c
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -2,20 +2,22 @@ NAME=tinypy
|
|||||||
CC = kos32-gcc
|
CC = kos32-gcc
|
||||||
LD = kos32-ld
|
LD = kos32-ld
|
||||||
|
|
||||||
|
SDK_DIR:=$(abspath ../../../../contrib/sdk)
|
||||||
|
|
||||||
LDFLAGS = -static -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds \
|
LDFLAGS = -static -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds \
|
||||||
--image-base 0 -lgcc -lSDLn -lc.dll -lsound
|
--image-base 0 -lgcc -lSDLn -lc.dll -lsound
|
||||||
|
|
||||||
SDL_DIR = $(SDK_DIR)/sources/SDL-1.2.2_newlib
|
SDL_DIR = $(SDK_DIR)/sources/SDL-1.2.2_newlib
|
||||||
|
|
||||||
CFLAGS = -DCONIO -U_Win32 -U_WIN32 -U__MINGW32__ -mpreferred-stack-boundary=2 \
|
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
|
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.
|
LIBPATH:= -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib -L.
|
||||||
|
|
||||||
STD_MODULES= ../std_modules/modules.o
|
STD_MODULES= ../std_modules/modules.o
|
||||||
|
|
||||||
OBJECTS = tpmain.o $(STD_MODULES)
|
OBJECTS = $(STD_MODULES) tpmain.o
|
||||||
|
|
||||||
all:$(NAME)
|
all:$(NAME)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user