+#include <std.h>
#include <liba.h>
#include <sys/poll.h>
-#define _XOPEN_SOURCE 700
+#include <std.h>
#include <liba.h>
#include <sys/wait.h>
+#include <std.h>
#include <liba.h>
#include <sys/types.h>
char* Usage = "listen DIALSTR CMD [ARG...]";
char** Command = NULL;
-void OnNewClient(int cfd)
+void OnNewClient(Int cfd)
{
int pid = fork();
if (pid < 0)
+#include <std.h>
#include <liba.h>
#include <libui.h>
+#include <std.h>
#include <liba.h>
#include <sys/wait.h>
#include <sys/types.h>
-/* Standard Macros and Types */
-#include <stddef.h>
-#define _XOPEN_SOURCE 700
-
-#include <stdint.h>
-#include <stdbool.h>
-#include <errno.h>
-#include <limits.h>
#include <assert.h>
-
-/* Useful Standard Functions */
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <stdarg.h>
-#include <string.h>
+#include <errno.h>
-/* we're only targeting unixes */
-#include <unistd.h>
+typedef unsigned char Byte;
+typedef long Int;
+typedef unsigned long Uint;
+typedef double Real;
+typedef Int Rune;
+typedef _Bool Bool;
+
+#ifndef true
+#define true ((Bool)1)
+#endif
+
+#ifndef false
+#define false ((Bool)0)
+#endif
+
+#ifndef NULL
+#define NULL ((void*)0)
+#endif
+
+#ifndef EOF
+#define EOF ((Int)-1)
+#endif
+
+void exit(int status);
/* Miscellaneous Macros
******************************************************************************/
#ifndef container_of
#define container_of(obj, type, member) \
- (type*)((uintptr_t)obj - offsetof(type, member))
+ (type*)((uIntptr_t)obj - offsetof(type, member))
#endif
/*
Garbage Collector Interface
*/
-void* GC_Allocate(size_t sz);
+void* GC_Allocate(Int sz);
void GC_AddRef(void* p);
void GC_DelRef(void* p);
char* l;
char* d;
char s;
- unsigned int a : 1;
+ Uint a : 1;
} Option_T;
-int Options_Parse(int argc, char** argv);
+Int Options_Parse(Int argc, char** argv);
void Options_PrintHelp(void);
-/*
- Standard Library Helpers
-*/
-void fatal(const char* fmt, ...);
-void warn(const char* fmt, ...);
-void esignal(int sig, void (*func)(int));
-int eraise(int sig);
-void* ecalloc(size_t num, size_t size);
-void* emalloc(size_t size);
-void* erealloc(void* ptr, size_t size);
-char* smprintf(const char* fmt, ...);
-FILE* efopen(const char* filename, const char* mode);
-char* efreadline(FILE* input);
-char* estrdup(const char *s);
-int forkexec(char** cmd);
-char* strmcat(char* first, ...);
-
/*
Networking
*/
-int Net_Announce(char* dialstr);
-int Net_Listen(int fd, int backlog);
-int Net_Accept(int fd);
-int Net_Dial(char* dialstr);
-void Net_Serve(char* dialstr, void (*on_client)(int cfd));
+Int Net_Announce(char* dialstr);
+Int Net_Listen(Int fd, Int backlog);
+Int Net_Accept(Int fd);
+Int Net_Dial(char* dialstr);
+void Net_Serve(char* dialstr, void (*on_client)(Int cfd));
/*
UTF8 Encoding and Decoding
#define RUNE_MAX ((Rune)0x10FFFF)
#define RUNE_EOF ((Rune)EOF)
-typedef uint32_t Rune;
-
-size_t UTF8_Encode(char str[UTF_MAX], Rune rune);
-bool UTF8_Decode(Rune* rune, size_t* length, int byte);
+Int UTF8_Encode(Byte str[UTF_MAX], Rune rune);
+Bool UTF8_Decode(Rune* rune, Int* length, Int byte);
/*
Basic Runtime Facilities
#define main usermain
extern int usermain(int argc, char** argv);
-extern void set_option(int sname, char* lname, char* arg);
+extern void set_option(Int sname, char* lname, char* arg);
extern char* ARGV0;
extern char* Usage;
--- /dev/null
+#define _XOPEN_SOURCE 700
+
+/* Standard Macros and Types */
+#include <stddef.h>
+#include <assert.h>
+
+/* Useful Standard Functions */
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+/* we're only targeting unixes */
+#include <unistd.h>
+
+void fatal(const char* fmt, ...);
+void warn(const char* fmt, ...);
+void esignal(int sig, void (*func)(int));
+int eraise(int sig);
+void* ecalloc(int num, int size);
+void* emalloc(int size);
+void* erealloc(void* ptr, int size);
+char* smprintf(const char* fmt, ...);
+FILE* efopen(const char* filename, const char* mode);
+char* efreadline(FILE* input);
+char* estrdup(const char *s);
+int forkexec(char** cmd);
+char* strmcat(char* first, ...);
+#include <std.h>
#include <liba.h>
#include <sys/types.h>
#include <sys/socket.h>
} conn_t;
struct socket_t {
- int fd;
+ Int fd;
union {
struct sockaddr_in in;
struct sockaddr_un un;
return addr;
}
-int MakeSocket(char* dialstr, struct socket_t* sock)
+Int MakeSocket(char* dialstr, struct socket_t* sock)
{
size_t i;
/* make a local copy of dial string */
sock->addr.in.sin_port = htons(strtol(conn.service, NULL, 0));
sock->addr.in.sin_addr = ResolveAddress(conn.address);
sock->fd = socket(AF_INET, SOCK_STREAM, 0);
- setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
+ setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(Int){1}, sizeof(Int));
}
else if (!strcmp("udp", conn.network))
{
sock->addr.in.sin_port = htons(strtol(conn.service, NULL, 0));
sock->addr.in.sin_addr = ResolveAddress(conn.address);
sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
- setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
+ setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(Int){1}, sizeof(Int));
}
else if (!strcmp("unix", conn.network))
{
return sock->fd;
}
-int Net_Announce(char* dialstr)
+Int Net_Announce(char* dialstr)
{
struct socket_t sock = { .fd = -1 };
if (MakeSocket(dialstr, &sock))
{
- int rv = -1;
+ Int rv = -1;
switch (sock.addr.in.sin_family)
{
case AF_INET:
return sock.fd;
}
-int Net_Listen(int fd, int backlog)
+Int Net_Listen(Int fd, Int backlog)
{
return listen(fd, backlog);
}
-int Net_Accept(int fd)
+Int Net_Accept(Int fd)
{
return accept(fd, 0, 0);
}
-int Net_Dial(char* dialstr)
+Int Net_Dial(char* dialstr)
{
- int rv = -1;
+ Int rv = -1;
struct socket_t sock = { .fd = -1 };
if (MakeSocket(dialstr, &sock))
{
return (rv == 0 ? sock.fd : rv);
}
-void Net_Serve(char* dialstr, void (*on_client)(int cfd))
+void Net_Serve(char* dialstr, void (*on_client)(Int cfd))
{
- int sfd = Net_Announce(dialstr);
+ Int sfd = Net_Announce(dialstr);
if (sfd >= 0)
{
while (!Net_Listen(sfd, 5))
{
- int cfd;
+ Int cfd;
if ((cfd = Net_Accept(sfd)) >= 0)
{
on_client(cfd);
+#include <std.h>
#include <liba.h>
-static void HandleOption(int s, char* l, char* arg)
+static void HandleOption(Int s, char* l, char* arg)
{
if (s == 'h')
{
{
Option_T* opt = NULL;
- for (int i = 0; Options[i].s || Options[i].l; i++)
+ for (Int i = 0; Options[i].s || Options[i].l; i++)
{
- int match = (
+ Int match = (
(flag[1] && !strcmp(Options[i].l, flag))
|| (!flag[1] && flag[0] == Options[i].s)
);
return opt;
}
-static inline void ParseLongOption(int* currp, char** argv)
+static inline void ParseLongOption(Int* currp, char** argv)
{
/* get the option and the arg if there is one */
char* flag = argv[*currp] + 2;
*currp += 1;
}
-static inline void ParseShortOption(int* currp, char** argv)
+static inline void ParseShortOption(Int* currp, char** argv)
{
char* argstr = argv[*currp]+1;
char flag[] = "\0\0";
*currp += 1;
}
-int Options_Parse(int argc, char** argv)
+Int Options_Parse(Int argc, char** argv)
{
/* Record the program name */
ARGV0 = argv[0];
- int new_argc = 0;
- int curr_arg = 1;
+ Int new_argc = 0;
+ Int curr_arg = 1;
/* now parse the arguments */
for (; curr_arg < argc;)
{
void Options_PrintHelp(void)
{
/* calculate padding */
- size_t padding = 0;
- for (int i = 0; Options[i].s || Options[i].l; i++)
+ Int padding = 0;
+ for (Int i = 0; Options[i].s || Options[i].l; i++)
{
- size_t pad = 4;
+ Int pad = 4;
if (Options[i].s)
{
pad += 2;
printf("usage: %s\n\n", Usage);
}
- /* print option help messages */
- for (int i = 0; Options[i].s || Options[i].l; i++)
+ /* prInt option help messages */
+ for (Int i = 0; Options[i].s || Options[i].l; i++)
{
int remain = padding;
if (Options[i].s)
#include <liba.h>
-static const uint8_t UTF8_SeqBits[] = { 0x00u, 0x80u, 0xC0u, 0xE0u, 0xF0u, 0xF8u, 0xFCu, 0xFEu };
-static const uint8_t UTF8_SeqMask[] = { 0x00u, 0xFFu, 0x1Fu, 0x0Fu, 0x07u, 0x03u, 0x01u, 0x00u };
-static const uint8_t UTF8_SeqLens[] = { 0x01u, 0x00u, 0x02u, 0x03u, 0x04u, 0x05u, 0x06u, 0x00u };
+static const Byte UTF8_SeqBits[] = { 0x00u, 0x80u, 0xC0u, 0xE0u, 0xF0u, 0xF8u, 0xFCu, 0xFEu };
+static const Byte UTF8_SeqMask[] = { 0x00u, 0xFFu, 0x1Fu, 0x0Fu, 0x07u, 0x03u, 0x01u, 0x00u };
+static const Byte UTF8_SeqLens[] = { 0x01u, 0x00u, 0x02u, 0x03u, 0x04u, 0x05u, 0x06u, 0x00u };
-static bool runevalid(Rune val) {
+static Bool runevalid(Rune val) {
return (val <= RUNE_MAX)
&& ((val & 0xFFFEu) != 0xFFFEu)
&& ((val < 0xD800u) || (val > 0xDFFFu))
&& ((val < 0xFDD0u) || (val > 0xFDEFu));
}
-static size_t runelen(Rune rune) {
+static Int runelen(Rune rune) {
if(!runevalid(rune))
return 0;
else if(rune <= 0x7F)
return 4;
}
-static uint8_t utfseq(uint8_t byte) {
- for (int i = 1; i < 8; i++)
+static Byte utfseq(Byte byte) {
+ for (Int i = 1; i < 8; i++)
if ((byte & UTF8_SeqBits[i]) == UTF8_SeqBits[i-1])
return UTF8_SeqLens[i-1];
return 0;
}
-size_t UTF8_Encode(char str[UTF_MAX], Rune rune)
+Int UTF8_Encode(Byte str[UTF_MAX], Rune rune)
{
- size_t len = runelen(rune);
+ Int len = runelen(rune);
str[0] = (len == 1 ? 0x00 : UTF8_SeqBits[len])
| (UTF8_SeqMask[len] & (rune >> (6 * (len-1))));
- for (size_t i = 1; i < len; i++)
+ for (Int i = 1; i < len; i++)
{
str[i] = 0x80u | (0x3Fu & (rune >> (6 * (len-i-1))));
}
return len;
}
-bool UTF8_Decode(Rune* rune, size_t* length, int byte)
+Bool UTF8_Decode(Rune* rune, Int* length, Int byte)
{
/* Handle the start of a new rune */
if (*length == 0)
#include <liba.h>
Option_T Options[] = {
- { .s = 'h', .l = "help", .a = 0, .d = "print this help message" },
+ { .s = 'h', .l = "help", .a = 0, .d = "prInt this help message" },
{0}
};
\ No newline at end of file
#include <liba.h>
-void set_option(int sname, char* lname, char* arg)
+void set_option(Int sname, char* lname, char* arg)
{
(void)sname;
(void)lname;
/* the real main lives here */
#undef main
+#include <std.h>
+
/* TODO:
* Process all increments *then* all decrements
*/
#define THOLD 0.65
#define DELETED ((void*)(intptr_t)-1)
#define LOG_SIZE 1023u
-#define NUM_PRIMES (sizeof(Primes)/sizeof(unsigned int))
+#define NUM_PRIMES (sizeof(Primes)/sizeof(unsigned Int))
enum {
INCREMENT = 0,
static void obj_addref(object_t* obj);
static void obj_delref(object_t* obj);
static void gc_collect(void);
-static void log_add(object_t* obj, int op);
+static void log_add(object_t* obj, Int op);
typedef struct {
- size_t size;
- size_t nslots;
- size_t thold;
+ Int size;
+ Int nslots;
+ Int thold;
void** slots;
} hash_t;
-static void hash_init(hash_t* hash, int nslots);
+static void hash_init(hash_t* hash, Int nslots);
static void hash_grow(hash_t* hash);
static void hash_add(hash_t* hash, void* entry);
-static int hash_del(hash_t* hash, void* entry);
+static Int hash_del(hash_t* hash, void* entry);
static intptr_t* Stack_Bot = 0;
static buffer_t Log = {0};
static hash_t ZCT;
-static unsigned int Primes[] = {
+static Int Primes[] = {
1543, 3079, 6151, 12289, 24593,
49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
/* Public Routines
***************************************/
-void* GC_Allocate(size_t sz)
+void* GC_Allocate(Int sz)
{
sz = (sz / sizeof(intptr_t)) + ((sz % sizeof(intptr_t)) ? 1 : 0);
object_t* obj = ecalloc(1, sizeof(object_t) + sz);
gc_scan_globals(&oldzct);
/* free the rest as they are dead for sure */
- size_t nslots = Primes[oldzct.nslots];
+ Int nslots = Primes[oldzct.nslots];
void** slots = oldzct.slots;
- for (size_t i = 0; i < nslots; i++)
+ for (Int i = 0; i < nslots; i++)
{
if (slots[i] != NULL && slots[i] != DELETED)
{
free(slots);
}
-static void log_add(object_t* obj, int op)
+static void log_add(object_t* obj, Int op)
{
Log.log[Log.index++] = (((uintptr_t)obj) | op);
if (Log.index >= ((sizeof(Log)/sizeof(uintptr_t))-1))
return key;
}
-static void hash_init(hash_t* hash, int nslots)
+static void hash_init(hash_t* hash, Int nslots)
{
memset(hash, 0, sizeof(hash_t));
hash->nslots = nslots;
- hash->thold = (size_t)(Primes[hash->nslots] * THOLD);
+ hash->thold = (Int)(Primes[hash->nslots] * THOLD);
hash->slots = calloc(Primes[hash->nslots], sizeof(void*));
}
static void hash_grow(hash_t* hash)
{
- size_t nslots = Primes[hash->nslots];
+ Int nslots = Primes[hash->nslots];
void** slots = hash->slots;
hash->size = 0;
hash->nslots++;
- hash->thold = (size_t)(Primes[hash->nslots] * THOLD);
+ hash->thold = (Int)(Primes[hash->nslots] * THOLD);
hash->slots = calloc(Primes[hash->nslots], sizeof(void*));
- for (size_t i = 0; i < nslots; i++)
+ for (Int i = 0; i < nslots; i++)
{
if (slots[i] != NULL)
{
/* now hash and add the new item */
uint64_t hcode = hash64((uint64_t)entry);
- size_t index = (hcode % Primes[hash->nslots]);
+ Int index = (hcode % Primes[hash->nslots]);
while (1)
{
if (hash->slots[index] == entry)
}
}
-static int hash_del(hash_t* hash, void* entry)
+static Int hash_del(hash_t* hash, void* entry)
{
uint64_t hcode = hash64((uint64_t)entry);
- size_t index = (hcode % Primes[hash->nslots]);
+ Int index = (hcode % Primes[hash->nslots]);
while (1)
{
#include <liba.h>
+#include <std.h>
-void* ecalloc(size_t num, size_t size)
+void* ecalloc(int num, int size)
{
void* ret;
if (NULL == (ret = calloc(num,size)))
#include <liba.h>
+#include <std.h>
FILE* efopen(const char* filename, const char* mode)
{
#include <liba.h>
+#include <std.h>
char* efreadline(FILE* input)
{
- size_t size = 32;
- size_t index = 0;
+ Int size = 32;
+ Int index = 0;
char* str = (char*)emalloc(size);
memset(str, 0, size);
if (feof(input))
#include <liba.h>
+#include <std.h>
-void* emalloc(size_t size)
+void* emalloc(int size)
{
void* ret;
if (NULL == (ret = malloc(size)))
#include <liba.h>
+#include <std.h>
int eraise(int sig)
{
#include <liba.h>
+#include <std.h>
-void* erealloc(void* ptr, size_t size)
+void* erealloc(void* ptr, int size)
{
void* ret;
if (NULL == (ret = realloc(ptr,size)))
#include <liba.h>
+#include <std.h>
void esignal(int sig, void (*func)(int))
{
#include <liba.h>
+#include <std.h>
char* estrdup(const char *s)
{
#include <liba.h>
+#include <std.h>
void fatal(const char* fmt, ...)
{
fprintf(stderr, " %s", strerror(errno));
}
fprintf(stderr, "\n");
- exit(EXIT_FAILURE);
+ exit(1);
}
#include <liba.h>
+#include <std.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <liba.h>
+#include <std.h>
-char* smprintf(const char* fmt, ...)
+char* smprIntf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- int strsz = vsnprintf(NULL, 0, fmt, args);
+ Int strsz = vsnprintf(NULL, 0, fmt, args);
va_end(args);
char* str = emalloc(strsz+1);
va_start(args, fmt);
#include <liba.h>
+#include <std.h>
char* strmcat(char* first, ...)
{
va_list args;
/* calculate the length of the final string */
- size_t len = strlen(first);
+ Int len = strlen(first);
va_start(args, first);
for (char* s = NULL; (s = va_arg(args, char*));)
{
len += strlen(s);
}
va_end(args);
- /* allocate the final string and copy the args into it */
+ /* allocate the final string and copy the args Into it */
char *str = malloc(len+1), *curr = str;
while (first && *first)
{
#include <liba.h>
+#include <std.h>
void warn(const char* fmt, ...)
{
+#include <std.h>
#include <liba.h>
#include <libui.h>
#include <impl/libui.h>
UIFont font_load(char* patt)
{
- static bool inited = false;
+ static Bool inited = false;
if (!inited && !FcInit())
{
fatal("failed to initialize fontconfig");
+#include <std.h>
#include <liba.h>
#include <libui.h>
#include <impl/libui.h>