| 1 |
/* |
| 2 |
* Copyright © 2025 Pierre Le Marre <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include "config.h" |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <limits.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <spawn.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <assert.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/wait.h> |
| 19 |
#include <getopt.h> |
| 20 |
|
| 21 |
#include "utils.h" |
| 22 |
#include "darray.h" |
| 23 |
|
| 24 |
#define PIPE_WRITE 1 |
| 25 |
#define PIPE_READ 0 |
| 26 |
#define SETUP_FAILURE 99 |
| 27 |
|
| 28 |
int |
| 29 |
compile_with(const char* compiler_name, const char* const *compiler_argv, |
| 30 |
const char *keymap_in, size_t keymap_size_in, |
| 31 |
char **keymap_out, size_t *keymap_size_out); |
| 32 |
|
| 33 |
int |
| 34 |
compile_with(const char* compiler_name, const char* const *compiler_argv, |
| 35 |
const char *keymap_in, size_t keymap_size_in, |
| 36 |
char **keymap_out, size_t *keymap_size_out) |
| 37 |
{ |
| 38 |
int ret = EXIT_FAILURE; |
| 39 |
assert(keymap_in); |
| 40 |
|
| 41 |
/* Prepare parameters */ |
| 42 |
char *envp[] = { NULL }; |
| 43 |
|
| 44 |
/* Prepare input */ |
| 45 |
int stdin_pipe[2] = {0}; |
| 46 |
int stdout_pipe[2] = {0}; |
| 47 |
posix_spawn_file_actions_t action; |
| 48 |
if (pipe(stdin_pipe) == -1) { |
| 49 |
perror("stdin pipe"); |
| 50 |
ret = SETUP_FAILURE; |
| 51 |
goto pipe_error; |
| 52 |
} |
| 53 |
if (posix_spawn_file_actions_init(&action)) { |
| 54 |
perror("spawn_file_actions_init error"); |
| 55 |
goto posix_spawn_file_actions_init_error; |
| 56 |
} |
| 57 |
|
| 58 |
/* Make spawned process close unused write-end of pipe, else it will not |
| 59 |
* receive EOF when write-end of the pipe is closed below and it will result |
| 60 |
* in a deadlock. */ |
| 61 |
if (posix_spawn_file_actions_addclose(&action, stdin_pipe[PIPE_WRITE])) |
| 62 |
goto posix_spawn_file_actions_error; |
| 63 |
/* Make spawned process replace stdin with read end of the pipe */ |
| 64 |
if (posix_spawn_file_actions_adddup2(&action, stdin_pipe[PIPE_READ], STDIN_FILENO)) |
| 65 |
goto posix_spawn_file_actions_error; |
| 66 |
|
| 67 |
/* Prepare stdout */ |
| 68 |
if (pipe(stdout_pipe) == -1) { |
| 69 |
perror("stdout pipe"); |
| 70 |
ret = SETUP_FAILURE; |
| 71 |
goto pipe_error; |
| 72 |
} |
| 73 |
|
| 74 |
if (posix_spawn_file_actions_addclose(&action, stdout_pipe[PIPE_READ])) |
| 75 |
goto posix_spawn_file_actions_error; |
| 76 |
if (posix_spawn_file_actions_adddup2(&action, stdout_pipe[PIPE_WRITE], STDOUT_FILENO)) |
| 77 |
goto posix_spawn_file_actions_error; |
| 78 |
|
| 79 |
/* Launch compiler */ |
| 80 |
pid_t compiler_pid; |
| 81 |
ret = posix_spawnp(&compiler_pid, compiler_name, &action, NULL, |
| 82 |
(char* const*)compiler_argv, envp); |
| 83 |
if (ret != 0) { |
| 84 |
errno = ret; |
| 85 |
perror("posix_spawnp XKB compiler failed"); |
| 86 |
goto posix_spawn_file_actions_init_error; |
| 87 |
} |
| 88 |
/* Close unused read-end of pipe */ |
| 89 |
close(stdin_pipe[PIPE_READ]); |
| 90 |
close(stdout_pipe[PIPE_WRITE]); |
| 91 |
ssize_t ret2 = write(stdin_pipe[PIPE_WRITE], keymap_in, keymap_size_in); |
| 92 |
/* Close write-end of the pipe, to emit EOF */ |
| 93 |
close(stdin_pipe[PIPE_WRITE]); |
| 94 |
if (ret2 == -1) { |
| 95 |
perror("Cannot write keymap to stdin"); |
| 96 |
kill(compiler_pid, SIGTERM); |
| 97 |
} |
| 98 |
|
| 99 |
/* Capture stdout */ |
| 100 |
const size_t buffer_size = 1024; |
| 101 |
darray_char stdout = darray_new(); |
| 102 |
darray_resize0(stdout, buffer_size); |
| 103 |
size_t size = 0; |
| 104 |
ssize_t count; |
| 105 |
while ((count = read(stdout_pipe[PIPE_READ], darray_items(stdout) + size, |
| 106 |
buffer_size)) > 0) { |
| 107 |
size += (size_t) count; |
| 108 |
darray_resize0(stdout, size + buffer_size); |
| 109 |
} |
| 110 |
close(stdout_pipe[PIPE_READ]); |
| 111 |
|
| 112 |
/* Ensure we get a NULL-terminated string */ |
| 113 |
if (size > 0 && darray_item(stdout, size - 1) != '\0') |
| 114 |
darray_resize0(stdout, size + 1); |
| 115 |
|
| 116 |
darray_steal(stdout, keymap_out, NULL); |
| 117 |
*keymap_size_out = size; |
| 118 |
|
| 119 |
/* Wait for compiler to complete */ |
| 120 |
int status; |
| 121 |
ret = waitpid(compiler_pid, &status, 0); |
| 122 |
ret = (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 123 |
? SETUP_FAILURE |
| 124 |
: EXIT_SUCCESS; |
| 125 |
goto cleanup; |
| 126 |
|
| 127 |
posix_spawn_file_actions_error: |
| 128 |
perror("posix_spawn_file_actions_* error"); |
| 129 |
posix_spawn_file_actions_init_error: |
| 130 |
close(stdin_pipe[PIPE_WRITE]); |
| 131 |
close(stdin_pipe[PIPE_READ]); |
| 132 |
close(stdout_pipe[PIPE_READ]); |
| 133 |
close(stdout_pipe[PIPE_WRITE]); |
| 134 |
ret = SETUP_FAILURE; |
| 135 |
cleanup: |
| 136 |
posix_spawn_file_actions_destroy(&action); |
| 137 |
pipe_error: |
| 138 |
return ret; |
| 139 |
} |
| 140 |
|
| 141 |
int |
| 142 |
compile_with_xkbcomp(const char* display, const char *include_path, |
| 143 |
const char *keymap_in, size_t keymap_size_in, |
| 144 |
char **keymap_out, size_t *keymap_size_out); |
| 145 |
|
| 146 |
int |
| 147 |
compile_with_xkbcomp(const char* display, const char *include_path, |
| 148 |
const char *keymap_in, size_t keymap_size_in, |
| 149 |
char **keymap_out, size_t *keymap_size_out) |
| 150 |
{ |
| 151 |
const bool has_display = !isempty(display); |
| 152 |
const char* const out = (has_display) ? display : "-"; |
| 153 |
|
| 154 |
/* Prepare xkbcomp parameters */ |
| 155 |
char include_path_arg[PATH_MAX+2] = "-I"; |
| 156 |
const char *xkbcomp_argv[] = { |
| 157 |
"xkbcomp", "-I" /* reset include path*/, include_path_arg, |
| 158 |
"-opt", "g", "-w", "10", "-xkb", "-" /* stdin */, out, NULL |
| 159 |
}; |
| 160 |
if (include_path) { |
| 161 |
int ret = snprintf(include_path_arg, ARRAY_SIZE(include_path_arg), |
| 162 |
"-I%s", include_path); |
| 163 |
if (ret < 0 || ret >= (int)ARRAY_SIZE(include_path_arg)) { |
| 164 |
return SETUP_FAILURE; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return compile_with("xkbcomp", xkbcomp_argv, |
| 169 |
keymap_in, keymap_size_in, |
| 170 |
keymap_out, keymap_size_out); |
| 171 |
} |
| 172 |
|
| 173 |
int |
| 174 |
compile_with_kbvm(const char *include_path, |
| 175 |
const char *keymap_in, size_t keymap_size_in, |
| 176 |
char **keymap_out, size_t *keymap_size_out); |
| 177 |
|
| 178 |
int |
| 179 |
compile_with_kbvm(const char *include_path, |
| 180 |
const char *keymap_in, size_t keymap_size_in, |
| 181 |
char **keymap_out, size_t *keymap_size_out) |
| 182 |
{ |
| 183 |
/* Prepare xkbcomp parameters */ |
| 184 |
const char *kbvm_argv[] = { |
| 185 |
"kbvm", "compile-xkb", "-", NULL, NULL, NULL, NULL |
| 186 |
}; |
| 187 |
if (include_path) { |
| 188 |
kbvm_argv[3] = "--no-default-includes"; |
| 189 |
kbvm_argv[4] = "--append-include"; |
| 190 |
kbvm_argv[5] = include_path; |
| 191 |
} |
| 192 |
|
| 193 |
return compile_with("kbvm", kbvm_argv, |
| 194 |
keymap_in, keymap_size_in, |
| 195 |
keymap_out, keymap_size_out); |
| 196 |
} |