tonarchy

tonarchy

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