#[ctype 'char', bytes '1'] AsciiChar # wchar (4 bytes on linux, 2 on binbows) #[ctype 'wchar_t', bytes '4'] WChar # different synonyms to check stuff CChar = AsciiChar inst FromChar AsciiChar from-charlike (ptr, num-bytes) if num-bytes /= 1 @panic('(Cnile.AsciiChar.from-charlike) character is not valid ascii') return @cast(ptr&) as AsciiChar inst Eq AsciiChar eq (l _, r _): @memeq(l, r) #[ctype 'void'] Void #[ctype 'FILE'] FILE CInt = I32 CUInt = U32 CULong = U64 ClockId = I32 fn clock-monotonic() -> ClockId: 1 #[ctype 'struct timespec'] Timespec #[cfield 'tv_sec'] tv-sec I64 #[cfield 'tv_nsec'] tv-nsec I64 # struct termios (x86_64 Linux, NCCS=32) Termios c-iflag U32 c-oflag U32 c-cflag U32 c-lflag U32 c-line U8 c-cc Array 32 U8 c-ispeed U32 c-ospeed U32 #[ctype 'struct termios'] CTermios Pollfd fd CInt events U16 revents U16 #[ctype 'struct pollfd'] CPollfd #[ctype 'void*'] CastPtr a this-field-is-only-for-sizing-cast-the-whole-thing Ptr Void # TODO: change Ptr to CPtr OR change to Maybe (Ptr a) fn castptr-ptr(cptr CastPtr a) -> Ptr a: @cast(cptr) fn ptr-castptr(ptr Ptr a) -> CastPtr a: @cast(ptr) # C nullable pointer yah. #[ctype 'void*'] CPtr a cptr Ptr a fn from-c-ptr (p CPtr a) -> Maybe (Ptr a) if @cast(p) as Size == 0 return None else return Just(@cast(p)) fn into-c-ptr (p Maybe (Ptr a)) -> CPtr a case p None return @cast(0 as Size) Just(ptr) return @cast(ptr) # NOTE: I'm thinking, since records are already C layout compatible, # my default would be to NOT set custom field names, cause it's annoying. # #[ctype 'struct winsize'] Winsize # #[cfield 'ws_row'] ws-row U16 # #[cfield 'ws_col'] ws-col U16 # #[cfield 'ws_xpixel'] ws-xpixel U16 # #[cfield 'ws_ypixel'] ws-ypixel U16 # Stat for x86_64 linux :) # #[ctype 'struct stat'] Stat dev U64 ino U64 nlink U64 #usize mode U32 uid U32 gid U32 pad0 U32 rdev U64 size I64 blksize I64 #isize blocks I64 atim Timespec mtim Timespec ctim Timespec unused1 I64 # [3]isize unused2 I64 unused3 I64 #[ctype 'char* const*'] ExecVpArgv #[ctype 'DIR'] DIR Dirent d-ino U64 d-off I64 d-reclen U16 d-type U8 d-name Array 256 AsciiChar SockAddr sa-family U16 sa-data Array 14 U8 AddrInfo ai-flags CInt ai-family CInt ai-socktype CInt ai-protocol CInt ai-addrlen Size ai-addr Ptr SockAddr ai-canonname Ptr AsciiChar ai-next CPtr () # TEMP: should be AddrInfo InAddr s-addr U32 SockAddrIn sin-family I16 sin-port U16 sin-addr InAddr sin-zero Array 8 U8 In6Addr s6-addr Array 16 U8 SockAddrIn6 sin6-family U16 sin6-port U16 sin6-flowinfo U32 sin6-addr In6Addr sin6-scope-id U32 SockLenT = U32 # Had to change from libc.so to libc.so.6 after switching from musl to glibc #[dylib 'libc.so.6'] external #[cstdinclude 'stdlib.h'] abort () -> Void #[cfunname 'printf', cstdinclude 'stdio.h'] printf0(format ConstStr) -> Void #[cfunname 'printf', cstdinclude 'stdio.h'] printf1(format ConstStr, x a) -> Void #[cfunname 'printf', cstdinclude 'stdio.h'] printf2(format ConstStr, x a, y b) -> Void #[cfunname 'printf', cstdinclude 'stdio.h'] printf3(format ConstStr, x a, y b, z c) -> Void #[cfunname 'fprintf', cstdinclude 'stdio.h'] fprintf1 (file Ptr FILE, format ConstStr, x a) -> CInt #[cfunname 'fprintf', cstdinclude 'stdio.h'] fprintf2 (file Ptr FILE, format ConstStr, x a, y b) -> CInt #[cfunname 'snprintf', cstdinclude 'stdio.h'] snprintf1 (p Ptr AsciiChar, n CInt, format ConstStr, x a) -> CInt #[cstdinclude 'stdlib.h'] srand (seed CUInt) -> Void #[cstdinclude 'stdlib.h'] rand () -> CInt # wchar stuff #[cstdinclude 'stdlib.h'] mbstowcs (wchs Ptr WChar, cstr Ptr CChar, count Size) -> Size #[cstdinclude 'wchar.h' '__USE_XOPEN'] wcwidth (wc WChar) -> CInt #[cstdinclude 'locale.h'] setlocale (category CInt, locale ConstStr) -> ConstStr #[cstdinclude 'string.h'] memset (dest Ptr Void, byte CInt, count Size) -> Ptr Void #[cstdinclude 'string.h'] memcpy (dest Ptr Void, src Ptr Void, count Size) -> Ptr Void #[cstdinclude 'stdlib.h'] exit (status CInt) -> Void malloc (bytes Size) -> Ptr Void free (x Ptr Void) -> Void remove (s ConstStr) -> CInt #[cstdinclude 'time.h'] time (p CPtr Void) -> CInt #[cstdinclude 'time.h', cfunname 'clock_gettime'] clock-gettime (clock-id ClockId, timespec Ptr Timespec) -> Void #[cstdinclude 'time.h'] nanosleep (req Ptr Timespec, rem Ptr Timespec) -> CInt #### string.h ##### #[cstdinclude 'string.h'] strlen (s ConstStr) -> CInt #[cstdinclude 'string.h'] strcmp (l ConstStr, r ConstStr) -> CInt ##### file ops ##### fopen (filename ConstStr, fileopts ConstStr) -> Ptr FILE # nullable! fclose (handle Ptr FILE) -> Void # in the near future maybe SEEK_END and stuff might be type-numbers instead of a hack datatypes. # SEEK_END = 1 # for example. # in case of `seek-end`, we might be able to modify constants which is bad. another thing might be functions, like `seek-end(): 1` fseek (file Ptr FILE, offset CInt, origin CInt) -> Void ftell (file Ptr FILE) -> CInt fread (buf Ptr a, size CInt, count CInt, stream Ptr FILE) -> CInt fflush (file Ptr FILE) -> CInt fwrite (buf Ptr Void, size Size, count Size, file Ptr FILE) -> CInt #[cstdinclude 'stdlib.h'] getenv (s ConstStr) -> ConstStr ##### sys/stat.h ##### #[cstdinclude 'sys/stat.h'] stat (path ConstStr, stat CastPtr Stat) -> Void #[cstdinclude 'sys/stat.h'] mkdir (path ConstStr, mode CInt) -> CInt #[cstdinclude 'sys/stat.h'] chmod (path ConstStr, mode U32) -> CInt #### unistd.h #### #[cstdinclude 'unistd.h'] chdir (path ConstStr) -> CInt #[cstdinclude 'unistd.h'] isatty (fileno CInt) -> CInt #[cstdinclude 'unistd.h'] access (path ConstStr, mode CInt) -> CInt #### ???? #[cstdinclude 'unistd.h'] fork () -> CInt # pid_t #[cstdinclude 'unistd.h'] execvp (name ConstStr, args ExecVpArgv) -> Void #[cstdinclude 'sys/wait.h'] waitpid (pid CInt, status Ptr CInt, options CInt) -> CInt #[cstdinclude 'sys/ioctl.h'] ioctl (fd CInt, request CULong, opt1 a) -> CInt #[cstdinclude 'termios.h'] tcgetattr (fd CInt, termios Ptr CTermios) -> CInt #[cstdinclude 'termios.h'] tcsetattr (fd CInt, optional-actions CInt, termios Ptr CTermios) -> CInt #[cstdinclude 'poll.h'] poll (fds Ptr CPollfd, nfds CInt, timeout CInt) -> CInt #[cstdinclude 'unistd.h'] read (fd CInt, buf Ptr Void, count Size) -> CInt #### dirent.h #### #[cstdinclude 'dirent.h'] opendir (path ConstStr) -> Ptr DIR # nullable! #[cstdinclude 'dirent.h'] readdir (dir Ptr DIR) -> CastPtr Dirent # nullable! #[cstdinclude 'dirent.h'] closedir (dir Ptr DIR) -> CInt # network #[cstdinclude 'arpa/inet.h', cfunname 'inet_pton'] inet-pton (af CInt, src ConstStr, dest Ptr Void) -> CInt #[cstdinclude 'arpa/inet.h', cfunname 'inet_ntop'] inet-ntop (af CInt, src Ptr Void, dest Ptr AsciiChar, size SockLenT) -> ConstStr # stdin #[cstdinclude 'stdio.h'] getchar () -> CInt #[dylib 'libm.so.6'] #[cstdinclude 'math.h'] #[coption '-lm'] external sqrt (f F64) -> F64 # seek fn seek-set(): 0 fn seek-cur(): 1 fn seek-end(): 2 # fd # later, we'll use type synonyms for this btw # STDOUT-FILENO = 1 fn stdout-fileno(): 1 fn stdin-fileno(): 0 # signals fn sigint(): 2 fn sigill(): 4 fn sigabrt(): 6 fn sigfpe(): 8 fn sigsegv(): 11 fn sigterm(): 15 fn sigwinch(): 28 # termios lflags fn echo(): 8 fn icanon(): 2 fn isig(): 1 fn iexten(): 32768 # termios iflags fn brkint(): 2 fn icrnl(): 256 fn inpck(): 16 fn istrip(): 32 fn ixon(): 1024 # termios oflags fn opost(): 1 # termios cflags fn cs8(): 48 # termios c_cc fn vmin(): 6 fn vtime(): 5 # tcattr actions fn tcsa-flush() -> CInt: 2 # ioctl requests fn tiocgwinsz() -> CULong: 21523 fn wifexited (status CInt): status == 0 fn errno-EEXIST(): @errno == 17 # sys/stat.h mode constants fn s-ifmt() -> U32: 61440 # 0o170000 - bitmask for file type bits fn s-ifdir() -> U32: 16384 # 0o040000 - directory file type # locale fn lc-ctype() -> CInt: 0 # network fn af-inet() -> CInt: 2 fn af-inet6() -> CInt: 10 fn inet-addr-strlen() -> SockLenT: 16 fn inet6-addr-strlen() -> SockLenT: 46 # TODO: nullable CPtr and a function CPtr a -> Maybe (Ptr a)