tonarchy

tonarchy

https://git.tonybtw.com/tonarchy.git git://git.tonybtw.com/tonarchy.git
3,145 bytes raw
1
#ifndef TONARCHY_H
2
#define TONARCHY_H
3
4
#define _POSIX_C_SOURCE 200809L
5
6
#include <stdbool.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <stdarg.h>
11
#include <termios.h>
12
#include <unistd.h>
13
#include <sys/ioctl.h>
14
#include <sys/stat.h>
15
#include <sys/types.h>
16
#include <ctype.h>
17
#include <time.h>
18
#include <pwd.h>
19
#include <grp.h>
20
21
#define CHROOT_PATH "/mnt"
22
#define MAX_CMD_SIZE 4096
23
24
#define ANSI_ESC           "\033["
25
#define ANSI_RESET         ANSI_ESC "0m"
26
#define ANSI_BOLD          ANSI_ESC "1m"
27
#define ANSI_WHITE         ANSI_ESC "37m"
28
#define ANSI_GREEN         ANSI_ESC "32m"
29
#define ANSI_YELLOW        ANSI_ESC "33m"
30
#define ANSI_GRAY          ANSI_ESC "90m"
31
#define ANSI_BLUE          ANSI_ESC "34m"
32
#define ANSI_BLUE_BOLD     ANSI_ESC "1;34m"
33
#define ANSI_CURSOR_POS    ANSI_ESC "%d;%dH"
34
35
typedef enum {
36
    LOG_LEVEL_DEBUG,
37
    LOG_LEVEL_INFO,
38
    LOG_LEVEL_WARN,
39
    LOG_LEVEL_ERROR
40
} Log_Level;
41
42
typedef struct {
43
    const char *repo_url;
44
    const char *name;
45
    const char *build_dir;
46
} Git_Repo;
47
48
typedef struct {
49
    const char *filename;
50
    const char *content;
51
    mode_t permissions;
52
} Dotfile;
53
54
typedef struct {
55
    const char *key;
56
    const char *value;
57
} Config_Entry;
58
59
typedef struct {
60
    const char *service_name;
61
    const char *drop_in_dir;
62
    const char *drop_in_file;
63
    Config_Entry *entries;
64
    size_t entry_count;
65
} Systemd_Override;
66
67
typedef struct {
68
    const char *label;
69
    const char *value;
70
    const char *default_display;
71
    int is_password;
72
} Tui_Field;
73
74
typedef enum {
75
    INPUT_TEXT,
76
    INPUT_PASSWORD,
77
    INPUT_FZF_KEYMAP,
78
    INPUT_FZF_TIMEZONE
79
} Input_Type;
80
81
typedef struct {
82
    char *dest;
83
    const char *default_val;
84
    Input_Type type;
85
    int cursor_offset;
86
    const char *error_msg;
87
} Form_Field;
88
89
void logger_init(const char *log_path);
90
void logger_close(void);
91
void log_msg(Log_Level level, const char *fmt, ...);
92
93
#define LOG_DEBUG(...) log_msg(LOG_LEVEL_DEBUG, __VA_ARGS__)
94
#define LOG_INFO(...)  log_msg(LOG_LEVEL_INFO, __VA_ARGS__)
95
#define LOG_WARN(...)  log_msg(LOG_LEVEL_WARN, __VA_ARGS__)
96
#define LOG_ERROR(...) log_msg(LOG_LEVEL_ERROR, __VA_ARGS__)
97
98
int write_file(const char *path, const char *content);
99
int write_file_fmt(const char *path, const char *fmt, ...);
100
int set_file_perms(const char *path, mode_t mode, const char *owner, const char *group);
101
int create_directory(const char *path, mode_t mode);
102
int chroot_exec(const char *cmd);
103
int chroot_exec_fmt(const char *fmt, ...);
104
int chroot_exec_as_user(const char *username, const char *cmd);
105
int chroot_exec_as_user_fmt(const char *username, const char *fmt, ...);
106
int git_clone_as_user(const char *username, const char *repo_url, const char *dest_path);
107
int make_clean_install(const char *build_dir);
108
int create_user_dotfile(const char *username, const Dotfile *dotfile);
109
int setup_systemd_override(const Systemd_Override *override);
110
111
void show_message(const char *message);
112
113
#define CHECK_OR_FAIL(expr, user_msg) \
114
    do { \
115
        if (!(expr)) { \
116
            LOG_ERROR("%s", #expr); \
117
            show_message(user_msg); \
118
            return 0; \
119
        } \
120
    } while(0)
121
122
#endif