ciabatta/include/tinyrt.h

79 lines
3.1 KiB
C
Raw Normal View History

2023-07-23 15:33:12 +00:00
#pragma once
// Common errors
2023-07-27 13:04:24 +00:00
#define _RT_STATUS_OK 0 // No errors
#define _RT_ERROR_NOT_IMPLEMENTED -1 // Function not implemented
#define _RT_ERROR_BAD_PARAM -2 // One of the function parameters was wrong
2023-07-28 10:49:56 +00:00
#define _RT_ERROR_GENERIC -3 // Just any random error
2023-07-23 15:33:12 +00:00
// File API errors
2023-07-27 13:04:24 +00:00
#define _RT_STATUS_FILE_ACCESS 1 // No access to the file
#define _RT_STATUS_FILE_NO_SPACE 2 // Storage device has no space for the file
#define _RT_STATUS_FILE_EXISTS 3 // File exists when shouldn't
#define _RT_STATUS_FILE_NOT_EXISTS 4 // File doesn't exist when should
#define _RT_STATUS_FILE_DIRECTORY 5 // The file was a directory when shouldn't've been
#define _RT_STATUS_FILE_NOT_DIRECTORY 6 // The file wasn't a directory when should've been
#define _RT_STATUS_FILE_NAME_TOO_LONG 7 // The filename was too long for the Filesystem
#define _RT_STATUS_FILE_LOOP 8 // Too many symlinks followed or a symlink encountered when expected none
#define _RT_STATUS_FILE_BUSY 9 // The device is busy if exclusive access is requested
#define _RT_STATUS_FILE_TOO_MANY_OPEN 10 // Too many open files in the process
#define _RT_STATUS_FILE_NO_MEMORY 11 // No kernel memory or user limit on memory allocation exceeded
#define _RT_STATUS_FILE_IO_ERROR 12 // I/O error
#define _RT_STATUS_FILE_BAD_FILE 13 // Bad file handle
#define _RT_STATUS_FILE_EOF 14 // Read operation reached the end-of-file
2023-07-23 15:33:12 +00:00
// File API flags
2023-07-27 13:04:24 +00:00
#define _RT_FILE_READ 0x01
#define _RT_FILE_WRITE 0x02
#define _RT_FILE_CREATE 0x04
#define _RT_FILE_EXCLUSIVE 0x08
#define _RT_FILE_TRUNCATE 0x10
2023-07-23 15:33:12 +00:00
2023-07-27 13:04:24 +00:00
typedef i32 _RT_Status;
2023-07-23 15:33:12 +00:00
// API implementation flags (managed and used by the layers on top of tinyrt)
static bool _rt_api_file;
static bool _rt_api_tmpfile;
// Initialization & termination of minirt
2023-07-27 13:04:24 +00:00
static _RT_Status _rt_init();
static _RT_Status _rt_deinit();
2023-07-23 15:33:12 +00:00
2023-07-25 05:24:19 +00:00
// Program API
2023-07-27 13:04:24 +00:00
#if _RT_API_PROGRAM == 1
_Noreturn static void _rt_program_exit(int code);
2023-07-25 05:24:19 +00:00
#endif
2023-07-28 09:55:41 +00:00
// Environment API
2023-07-27 13:04:24 +00:00
#if _RT_API_ENVIRONMENT == 1
static _RT_Status _rt_shell_exec(char const *cmd);
static _RT_Status _rt_env_get(char const *name);
2023-07-25 05:24:19 +00:00
#endif
2023-07-23 15:33:12 +00:00
// File API
2023-07-27 13:49:53 +00:00
struct _RT_File typedef _RT_File;
struct _RT_File {
union {
void *handle;
u64 fd;
2023-07-23 15:33:12 +00:00
};
2023-07-27 13:49:53 +00:00
i32 flags;
};
#if _RT_API_FILE == 1
static _RT_File _rt_file_stdin;
static _RT_File _rt_file_stdout;
static _RT_File _rt_file_stderr;
static _RT_Status _rt_file_std_handles_init();
static _RT_Status _rt_file_open(_RT_File *file, char const *name, int flags);
2023-07-27 13:04:24 +00:00
static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out_bytes_read);
static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written);
static _RT_Status _rt_file_close(_RT_File *file);
2023-07-23 15:33:12 +00:00
#endif
2023-07-28 10:49:56 +00:00
// Memory API
#if _RT_API_MEMORY == 1
static _RT_Status _rt_mem_alloc(void *optional_desired_addr, u64 size, void **out_addr);
static _RT_Status _rt_mem_free(void *ptr, u64 size);
2023-07-28 10:49:56 +00:00
#endif