From: John Hodge Date: Tue, 20 Jul 2010 15:19:33 +0000 (+0800) Subject: Cleaning up files X-Git-Tag: rel0.06~94 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=7bc1f2fbeb70981e23b427e72d3f3d195c104884;p=tpg%2Facess2.git Cleaning up files --- diff --git a/Usermode/Libraries/.gitignore b/Usermode/Libraries/.gitignore new file mode 100644 index 00000000..c8e61682 --- /dev/null +++ b/Usermode/Libraries/.gitignore @@ -0,0 +1 @@ +acess.ld diff --git a/Usermode/Libraries/liburi.so_src/Makefile b/Usermode/Libraries/liburi.so_src/Makefile new file mode 100644 index 00000000..047a7047 --- /dev/null +++ b/Usermode/Libraries/liburi.so_src/Makefile @@ -0,0 +1,13 @@ +# Acess 2 +# + +include ../Makefile.cfg + +CPPFLAGS += +CFLAGS += -Wall +LDFLAGS += -lc -soname liburi.so + +OBJ = main.o +BIN = ../liburi.so + +include ../Makefile.tpl diff --git a/Usermode/Libraries/liburi.so_src/main.c b/Usermode/Libraries/liburi.so_src/main.c new file mode 100644 index 00000000..2d785f3e --- /dev/null +++ b/Usermode/Libraries/liburi.so_src/main.c @@ -0,0 +1,324 @@ +/* + * Acess2 - URI Parser and opener + * By John Hodge (thePowersGang) + */ +#include +#include +#include +#include +#include +#include + +// === STRUCTURES === +struct sURIFile +{ + int Handle; + int Mode; + tURIHandler *Handler; + int CurBlockOffset; + char Buffer[]; +}; + +// === PROTOTYPES === + int SoMain(void); +tURI *URI_Parse(const char *String); +tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler); +tURIFile *URI_Open(int Mode, tURI *URI); +size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer); +size_t URI_Write(tURIFile *File, size_t Bytes, void *Buffer); +void URI_Close(tURIFile *File); +// --- file:/// handler + int URI_file_Open(char *Host, int Port, char *Path, int Mode); +size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer); +size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer); +void URI_file_Close(int Handle); + +// === CONSTANTS === +// Builtin URI protocol handlers +tURIHandler caBuiltinHandlers[] = { + {"file", 0, URI_file_Open, URI_file_Close, URI_file_Read, URI_file_Write} +}; +#define NUM_BUILTIN_HANDLERS (sizeof(caBuiltinHandlers)/sizeof(caBuiltinHandlers[0])) + +// === CODE === +int SoMain(void) +{ + return 0; +} + +tURI *URI_Parse(const char *String) +{ + const char *tmp = String; + tURI *ret; + int protolen; + + if(!String) return NULL; + + protolen = 0; + while( isalpha(*tmp) ) tmp++, protolen++; + + // true URI + if(tmp[0] == ':' && tmp[1] == '/' && tmp[2] == '/') + { + int hostlen, portlen, pathlen; + tmp += 3; // Eat '://' + ret = malloc(sizeof(tURI) + strlen(String) - 2); + + ret->Proto = (char*)ret + sizeof(tURI); + memcpy(ret->Proto, String, protolen); + ret->Proto[protolen] = '\0'; + + ret->Host = ret->Proto + protolen + 1; + hostlen = 0; + + // IPv6 + if( *tmp == '[' ) + { + tmp ++; + while( *tmp != ']' ) { + ret->Host[hostlen] = *tmp; + tmp ++; + hostlen ++; + } + tmp ++; + ret->Host[hostlen] = '\0'; + } + // IPv4/DNS + else + { + while( *tmp != '/' && *tmp != ':' ) + { + ret->Host[hostlen] = *tmp; + tmp ++; + hostlen ++; + } + ret->Host[hostlen] = '\0'; + } + + // Port + if( *tmp == ':' ) { + ret->PortStr = ret->Host + hostlen + 1; + tmp ++; + portlen = 0; + while(isalpha(*tmp) || isdigit(*tmp)) + { + ret->PortStr[portlen] = *tmp; + portlen ++; + tmp ++; + } + ret->PortStr[portlen] = '\0'; + + ret->PortNum = atoi(ret->PortStr); + if(!ret->PortNum && !(ret->PortStr[0] == '0' && portlen == 1) ) + { + // error! + ret->Path = NULL; + return ret; + } + } + else { + ret->PortStr = NULL; + ret->PortNum = -1; + } + + if(*tmp == '\0') + { + ret->Path = NULL; + return ret; + } + + // TODO: What to do on a parse error + if(*tmp != '/') { + ret->Path = NULL; + return ret; + } + + if(ret->PortStr) + ret->Path = ret->PortStr + portlen + 1; + else + ret->Path = ret->Host + hostlen + 1; + + tmp ++; + pathlen = 0; + while(*tmp) + { + ret->Path[pathlen] = *tmp; + tmp ++; + pathlen ++; + } + + return ret; + } + else + { +// char *cwd; +// int retlen; + + // Path + // TODO: What to do? + // Probably return file:/// + // but should I get the CWD and use append that? + ret = malloc( sizeof(tURI) + strlen(String) + 1 ); + ret->Path = (char*)ret + sizeof(tURI); + strcpy(ret->Path, String); + ret->Proto = "file"; + ret->Host = NULL; + ret->PortNum = 0; + ret->PortStr = NULL; + return ret; + } +} + +tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler) +{ + tURIFile *ret; + + ret = malloc(sizeof(tURIFile)+Handler->BlockSize); + if(!ret) return NULL; + + ret->Handle = Handle; + ret->Mode = Mode; + ret->Handler = Handler; + ret->CurBlockOffset = 0; + + return ret; +} + +tURIFile *URI_Open(int Mode, tURI *URI) +{ + tURIHandler *handler; + tURIFile *ret; + int handle; + int i; + + if(!URI) + return NULL; + + for( i = 0; i < NUM_BUILTIN_HANDLERS; i ++ ) + { + if(strcmp(URI->Proto, caBuiltinHandlers[i].Name) == 0) + break; + } + + if( i == NUM_BUILTIN_HANDLERS ) + { + // TODO: Dynamics + printf("URI_Open: Warning - Unknown URI handler\n"); + return NULL; + } + else + handler = &caBuiltinHandlers[i]; + + printf("URI_Open: handler->Open = %p\n", handler->Open); + + handle = handler->Open(URI->Host, URI->PortNum, URI->Path, Mode); + printf("URI_Open: handle = %i\n", handle); + if(handle == -1) return NULL; + + printf("URI_MakeHandle(Mode=%i, handle=%i, handler=%p)\n", + Mode, handle, handler); + ret = URI_MakeHandle(Mode, handle, handler); + if(!ret) { + handler->Close( handle ); + return NULL; + } + return ret; +} + +/** + * \brief Read from a URI file + */ +size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer) +{ + size_t rem = Bytes; + void *buf = Buffer; + size_t tmp; + + printf("URI_Read(File=%p, Bytes=%u, Buffer=%p)\n", + File, Bytes, Buffer); + + if(!File || !Buffer) return -1; + if(Bytes == 0) return 0; + + if( !(File->Mode & URI_MODE_READ) ) return -1; + + // Read from cache if avaliable + if(File->Handler->BlockSize && File->CurBlockOffset) + { + int avail; + + avail = File->Handler->BlockSize - File->CurBlockOffset; + + if(avail >= Bytes) { + memcpy(Buffer, File->Buffer, Bytes); + File->CurBlockOffset += Bytes; + File->CurBlockOffset %= File->Handler->BlockSize; + return Bytes; + } + + rem -= avail; + memcpy(Buffer, File->Buffer, avail); + File->CurBlockOffset = 0; + buf += avail; + } + + + if( File->Handler->BlockSize ) + { + // Read whole blocks + while( rem >= File->Handler->BlockSize ) + { + tmp = File->Handler->Read( File->Handle, File->Handler->BlockSize, buf ); + if(tmp < File->Handler->BlockSize) + return Bytes - rem - tmp; + buf += File->Handler->BlockSize; + } + + // Read the trailing part + if(rem) + { + File->Handler->Read( File->Handle, File->Handler->BlockSize, File->Buffer ); + memcpy( buf, File->Buffer, rem ); + File->CurBlockOffset += rem; + } + return Bytes; + } + + return File->Handler->Read( File->Handle, Bytes, Buffer ); +} + +/** + * \brief Write to a URI file + */ + + +// ==== +// Builtin Handlers +// ==== +int URI_file_Open(char *Host, int Port, char *Path, int Mode) +{ + int smode = 0; + if(Mode & URI_MODE_READ) smode |= OPENFLAG_READ; + if(Mode & URI_MODE_WRITE) smode |= OPENFLAG_WRITE; + + printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode); + { + int ret; + ret = open(Path, smode); + return ret; + } +} +size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer) +{ + printf("URI_file_Read: (Handle=%i, Bytes=%i, Buffer=%p)\n", + Handle, Bytes, Buffer); + return read(Handle, Bytes, Buffer); +} +size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer) +{ + return write(Handle, Bytes, Buffer); +} +void URI_file_Close(int Handle) +{ + close(Handle); +} diff --git a/Usermode/include/ctype.h b/Usermode/include/ctype.h new file mode 100644 index 00000000..038dc783 --- /dev/null +++ b/Usermode/include/ctype.h @@ -0,0 +1,16 @@ +/* + */ +#ifndef _CTYPE_H_ +#define _CTYPE_H_ + +static inline int isalpha(char ch) { + if('A'<=ch&&ch<='Z') return 1; + if('a'<=ch&&ch<='z') return 1; + return 0; +} +static inline int isdigit(char ch) { + if('0'<=ch&&ch<='9') return 1; + return 0; +} + +#endif diff --git a/Usermode/include/string.h b/Usermode/include/string.h new file mode 100644 index 00000000..1e721974 --- /dev/null +++ b/Usermode/include/string.h @@ -0,0 +1,29 @@ +/* + * AcessOS LibC + * string.h + */ +#ifndef __STRING_H +#define __STRING_H + +#include + +// Strings +extern int strlen(const char *string); +extern int strcmp(const char *str1, const char *str2); +extern int strncmp(const char *str1, const char *str2, size_t len); +extern char *strcpy(char *dst, const char *src); +extern char *strncpy(char *dst, const char *src, size_t num); +extern char *strcat(char *dst, const char *src); +extern char *strdup(const char *src); +extern char *strchr(char *str, int character); +extern char *strrchr(char *str, int character); +extern char *strstr(char *str1, const char *str2); + +// Memory +extern void *memset(void *dest, int val, size_t count); +extern void *memcpy(void *dest, const void *src, size_t count); +extern void *memmove(void *dest, const void *src, size_t count); +extern int memcmp(const void *mem1, const void *mem2, size_t count); +extern void *memchr(void *ptr, int value, size_t num); + +#endif diff --git a/Usermode/include/uri.h b/Usermode/include/uri.h new file mode 100644 index 00000000..92956b05 --- /dev/null +++ b/Usermode/include/uri.h @@ -0,0 +1,45 @@ +/* + * Acess2 - URI Parser and opener + * By John Hodge (thePowersGang) + */ +#ifndef _LIB_URI_H_ +#define _LIB_URI_H_ + +typedef struct sURI tURI; +typedef struct sURIFile tURIFile; +typedef struct sURIHandler tURIHandler; + +enum eURIModes +{ + URI_MODE_READ = 0x01, + URI_MODE_WRITE = 0x02 +}; + +struct sURI +{ + char *Proto; + char *Host; + char *PortStr; + int PortNum; + char *Path; +}; + +struct sURIHandler +{ + char *Name; + int BlockSize; + + int (*Open)(char *Host, int Port, char *Path, int Flags); + void (*Close)(int Handle); + size_t (*Read)(int Handle, size_t Bytes, void *Buffer); + size_t (*Write)(int Handle, size_t Bytes, void *Buffer); +}; + +// === FUNCTIONS === +extern tURI *URI_Parse(const char *String); +extern tURIFile *URI_Open(int Mode, tURI *URI); +extern size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer); +extern size_t URI_Write(tURIFile *File, size_t Bytes, void *Buffer); +extern void URI_Close(tURIFile *File); + +#endif