-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.h
105 lines (93 loc) · 3.12 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef _JAUNCH_COMMON_H
#define _JAUNCH_COMMON_H
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUCCESS 0
#define ERROR_DLOPEN 1
#define ERROR_DLSYM 2
#define ERROR_CREATE_JAVA_VM 3
#define ERROR_FIND_CLASS 4
#define ERROR_GET_STATIC_METHOD_ID 5
#define ERROR_PIPE 6
#define ERROR_FORK 7
#define ERROR_EXECLP 8
#define ERROR_MALLOC 9
#define ERROR_REALLOC 10
#define ERROR_WAITPID 11
#define ERROR_STRDUP 12
#define ERROR_COMMAND_PATH 13
#define ERROR_OUTPUT 14
#define ERROR_ARGC_OUT_OF_BOUNDS 15
#define ERROR_UNKNOWN_DIRECTIVE 16
// ===========================================================
// PLATFORM-SPECIFIC FUNCTION DECLARATIONS
// For implementations, see linux.h, macos.h, posix.h, win32.h
// ===========================================================
// Implementations in posix.h, win32.h
void *lib_open(const char *path);
void *lib_sym(void *library, const char *symbol);
void lib_close(void *library);
char *lib_error();
int run_command(const char *command,
size_t numInput, const char *input[],
size_t *numOutput, char ***output);
// Implementations in linux.h, win32.h, macos.h
void setup(const int argc, const char *argv[]);
void teardown();
void init_threads();
void show_alert(const char *title, const char *message);
typedef int (*LaunchFunc)(const size_t, const char **);
int launch(const LaunchFunc launch_func,
const size_t argc, const char **argv);
// =================
// UTILITY FUNCTIONS
// =================
int debug_mode = 0;
void print_at_level(int verbosity, const char *fmt, ...) {
if (debug_mode < verbosity) return;
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
fflush(stderr);
}
#define error(fmt, ...) print_at_level(0, fmt, ##__VA_ARGS__)
#define debug(fmt, ...) print_at_level(1, fmt, ##__VA_ARGS__)
#define debug_verbose(fmt, ...) print_at_level(2, fmt, ##__VA_ARGS__)
#define CHECK_ARGS(prefix, name, argc, min, max, argv) \
do { \
debug_verbose("[%s] %s_argc = %zu", (prefix), (name), (argc)); \
if ((argc) < (min) || (argc) > (max)) { \
error("Error: %s_argc value %d is out of bounds [%d, %d]\n", \
name, (argc), (min), (max)); \
return ERROR_ARGC_OUT_OF_BOUNDS; \
} \
for (size_t a = 0; a < (argc); a++) { \
debug_verbose("[%s] %s_argv[%zu] = %s", (prefix), (name), a, argv[a]); \
} \
} while(0)
/* Splits an output buffer into lines. */
int split_lines(char *buffer, char *delim, char ***output, size_t *numOutput) {
size_t lineCount = 0;
char *token = strtok(buffer, delim);
while (token != NULL) {
*output = realloc(*output, (lineCount + 1) * sizeof(char *));
if (*output == NULL) {
error("Memory reallocation failed");
return ERROR_REALLOC;
}
(*output)[lineCount] = strdup(token);
if ((*output)[lineCount] == NULL) {
error("String duplication failed");
return ERROR_STRDUP;
}
lineCount++;
token = strtok(NULL, delim);
}
*numOutput = lineCount;
return SUCCESS;
}
#endif