#include <stddef.h>
#include <stdint.h>
#include <syscall.h>
#include <system.h>

#define do_syscall1(id, a1, a2, a3, a4, a5, a6, ...)                                               \
  syscall (id, (uintptr_t)a1, (uintptr_t)a2, (uintptr_t)a3, (uintptr_t)a4, (uintptr_t)a5,          \
           (uintptr_t)a6)

#define do_syscall(...) do_syscall1 (__VA_ARGS__, 0, 0, 0, 0, 0, 0)

int quit (void) { return do_syscall (SYS_QUIT, 0); }

int test (char c) { return do_syscall (SYS_TEST, c); }

int sched (void) { return do_syscall (SYS_SCHED, 0); }

void* map (uintptr_t vaddr, size_t pages, uint32_t flags) {
  return (void*)do_syscall (SYS_MAP, vaddr, pages, flags);
}

int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }

int clone (uintptr_t vstack_top, void (*entry) (void), void* argument_ptr) {
  return do_syscall (SYS_CLONE, vstack_top, entry, argument_ptr);
}

int mutex_create (void) { return do_syscall (SYS_MUTEX_CREATE, 0); }

int mutex_delete (int mutex_rid) { return do_syscall (SYS_MUTEX_DELETE, mutex_rid); }

int mutex_lock (int mutex_rid) { return do_syscall (SYS_MUTEX_LOCK, mutex_rid); }

int mutex_unlock (int mutex_rid) { return do_syscall (SYS_MUTEX_UNLOCK, mutex_rid); }

void* argument_ptr (void) { return (void*)do_syscall (SYS_ARGUMENT_PTR, 0); }

int device_do (const char* device_key, int cmd, void* a1, void* a2, void* a3, void* a4) {
  return (int)do_syscall (SYS_DEVICE_DO, device_key, cmd, a1, a2, a3, a4);
}

int exec (const char* volume, const char* path) { return (int)do_syscall (SYS_EXEC, volume, path); }

int volume_open (const char* volume) { return (int)do_syscall (SYS_VOLUME_OPEN, volume); }

int volume_close (void) { return (int)do_syscall (SYS_VOLUME_CLOSE, 0); }

int read_file (const char* path, size_t off, uint8_t* buffer, size_t size) {
  return (int)do_syscall (SYS_READ_FILE, path, off, buffer, size);
}

int describe (const char* path, struct desc* desc) {
  return (int)do_syscall (SYS_DESCRIBE, path, desc);
}

int mail_send (int pgid, void* mesg, size_t mesg_size) {
  return (int)do_syscall (SYS_MAIL_SEND, pgid, mesg, mesg_size);
}

int mail_receive (void* mesg, size_t mesg_size) {
  return (int)do_syscall (SYS_MAIL_RECEIVE, mesg, mesg_size);
}

int get_procgroup (int pid) { return (int)do_syscall (SYS_GET_PROCGROUP, pid); }

int get_exec_pid (void) { return (int)do_syscall (SYS_GET_EXEC_PID, 0); }

int read_dir_entry (const char* path, struct dir_entry* entry, size_t entry_num) {
  return (int)do_syscall (SYS_READ_DIR_ENTRY, path, entry, entry_num);
}

int create_file (const char* path) { return (int)do_syscall (SYS_CREATE_FILE, path); }

int write_file (const char* path, size_t off, uint8_t* buffer, size_t size, uint32_t flags) {
  return (int)do_syscall (SYS_WRITE_FILE, path, off, buffer, size, flags);
}

int wait_for_pid (int pid) { return (int)do_syscall (SYS_WAIT_FOR_PID, pid); }

int kill (int pid) { return (int)do_syscall (SYS_KILL, pid); }

int create_dir (const char* path) { return (int)do_syscall (SYS_CREATE_DIR, path); }

int remove (const char* path) { return (int)do_syscall (SYS_REMOVE, path); }

int create_volume (const char* key, int fs_type, const char* device_key) {
  return (int)do_syscall (SYS_CREATE_VOLUME, key, fs_type, device_key);
}
