Skip to content

lib_profan

pf4 edited this page Dec 27, 2024 · 22 revisions

profan Library

profan is a library with small practical additions to the standard C library and other internal functions. It can be used to write userland programs.

Related files

Index

serial_debug

Exactly like printf, but sends the output to the serial portA.

int serial_debug (
    char *frm,
    ...
);
  • frm: The format string.
  • ...: The arguments to the format string.
  • Returns: The number of characters printed.

profan_fn_name

Return the name of a function from a pointer to it. The pointer does not have to be the start of the function. If libname is not NULL, it will be set to the name of the library the function is in. The returned string and the library name should not be freed.

char *profan_fn_name (
    void *ptr,
    char **libname
);
  • ptr: A pointer to the function.
  • libname: A pointer to a char *
  • Returns: The name of the function.

profan_print_trace

Print a backtrace with the names of the functions and the libraries in stderr. The stack trace is limited to 5 lines.

void profan_print_trace (
    void
);

profan_input

Call profan_input_keyboard if the terminal is not a serial port, otherwise call profan_input_serial. Returns the input buffer and edits the size pointer to the size of the buffer. The buffer must be freed after usage. Returns NULL on failure.

char *profan_input (
    int *size
);
  • size: A pointer to the size of the buffer.
  • Returns: The input buffer (must be freed).

profan_nimpl

Print a message to stderr that the function is not implemented and abort the program (the abort can be disabled in config file).

void profan_nimpl (
    char *func
);
  • func: The name of the function.

profan_libc_version

Return the version of the libc as a string. The returned string should not be freed.

char *profan_libc_version (
    void
);
  • Returns: The version of the libc.

profan_join_path

Assemble a path from two parts, adding a '/' between them if necessary. The result is a newly allocated string that must be freed. If new is an absolute path, old is ignored.

char *profan_join_path (
    char *old,
    char *new
);
  • old: The first part of the path.
  • new: The second part of the path.
  • Returns: The assembled path (must be freed).

profan_sep_path

Separate a full path into a parent path and a content path. The parent is the path to the directory containing the content, and the content is the base name of the file or directory. This function does not check if the path is valid / exists. parent and cnt can be NULL if the value is not needed else they must be freed after usage.

void profan_sep_path (
    const char *fullpath,
    char **parent,
    char **cnt
);
  • fullpath: The full path to separate.
  • parent: A char * pointer to store the parent path.
  • cnt: A char * pointer to store the content path.

profan_resolve_path

Resolve a path (absolute or relative) to a sector id. The function will a SID_NULL if the path is invalid or not found.

uint32_t profan_resolve_path (
    const char *path
);
  • path: The path to resolve.
  • Returns: The sector id of the path.

profan_kalloc

Use the kernel allocator to allocate or free memory. The as_kernel flag is used to determine if the memory should be free when the process exits. Check the unix man page for information about aguments and return values.

void *profan_kmalloc (
    uint32_t size,
    int as_kernel
);

void *profan_kcalloc (
    uint32_t nmemb,
    uint32_t lsize,
    int as_kernel
);

void *profan_krealloc (
    void *mem,
    uint32_t new_size,
    int as_kernel
);

void profan_kfree (
    void *mem
);

userspace_reporter

Report a message to the user. This function is used by the kernel to report errors to the user.

int userspace_reporter (
    char *message
);
  • message: The message to report.
  • Returns: 0 on success, 1 on failure.

profan_kb_load_map

Load a keyboard map from the a keymap file. The file should start with #KEYMAP and then have a list characters corresponding to the scancodes.

int profan_kb_load_map (
    char *path
);
  • path: The path to the keymap file.
  • Returns: 0 on success, 1 on failure.

profan_kb_get_char

Get the character corresponding to a scancode and shift state from the current loaded keymap. If the scancode is not found, it will return 0.

char profan_kb_get_char (
    uint8_t scancode,
    uint8_t shift
);
  • scancode: The scancode of the key.
  • shift: The shift state of the keyboard.
  • Returns: The corresponding character.

profan_input_keyboard

Wait for a key to be pressed, resolve it to a character using the current keymap, display it on the given terminal and append it to the output buffer. Returns a pointer to the output buffer and edits the size pointer to the size of the buffer after '\n' is pressed. The buffer must be freed after usage. Returns NULL on failure.

char *profan_input_keyboard (
    int  *size,
    char *term_path
);
  • size: A pointer to the size of the buffer.
  • term_path: The path to the terminal.
  • Returns: The input buffer (must be freed).

profan_input_serial

Wait for a character to be received on the given serial port, display it on the given serial port and append it to the output buffer. Returns a pointer to the output buffer and edits the size pointer to the size of the buffer after '\n' is received. The buffer must be freed after usage. Returns NULL on failure.

char *profan_input_serial (
    int *size,
    int  serial_port
);
  • size: A pointer to the size of the buffer.
  • serial_port: The serial port to read from.
  • Returns: The input buffer (must be freed).

run_ifexist

Run a program if it exists. The program will be run in a new process. If pid_ptr is not NULL, it will be set to the pid of the new process. Returns -1 on failure and the exit code of the process on success (0 if the process was not finished).

typedef struct {
    char   *path;

    int     argc;
    char  **argv;
    char  **envp;

    uint8_t sleep_mode;
} runtime_args_t;

int run_ifexist_full (
    runtime_args_t args,
    int *pid_ptr
);
  • args: The arguments to run the program with.
    • path: The path to the program.
    • argc: The number of arguments.
    • argv: The list of arguments.
    • envp: The list of environment variables.
    • sleep_mode: The sleep mode.
  • pid_ptr: A pointer to the pid of the new process.
  • Returns: The exit code of the process on success, -1 on failure.

Note

argv must start with the name of the program but must not necessarily end with a NULL pointer but envp must end with a NULL pointer!

Sleep mode is a flag interpreted like this:

  • 0: The process will be run in the background.
  • 1: The parent process will wait for the child process to finish.
  • 2: The child process will not be started.
  • 3: The program will be run in the current process (like exec).

Supported magical numbers:

Number Name Description
0x7F 'E' 'L' 'F' ELF file The program is an ELF file.
'#' '!' '/' Shebang standard shebang
0x55 0x89 0xE5 x86 x86 machine code

Elf files will be run with deluge dynamical linker, Machine code should have a entry point at 0xB0000000.

#define run_ifexist(path, argc, argv) \
        run_ifexist_full((runtime_args_t){path, \
        argc, argv, get_environ_ptr(), 1}, NULL)

This macro can be used instead of the full function to run a program in the background and wait for it to finish.