MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

respect xdg paths

+206 -52
+8
include/utils.h
··· 4 4 5 5 #include <stdlib.h> 6 6 #include <stdint.h> 7 + #include <stdbool.h> 7 8 #include <string.h> 8 9 9 10 typedef struct { ··· 23 24 24 25 int hex_digit(char c); 25 26 int is_typescript_file(const char *filename); 27 + 28 + int ant_mkdir_p(const char *path); 29 + int ant_user_bin_path(char *out, size_t out_size); 30 + 31 + int ant_xdg_cache_path(char *out, size_t out_size, const char *suffix); 32 + int ant_xdg_data_path(char *out, size_t out_size, const char *suffix); 33 + int ant_xdg_state_path(char *out, size_t out_size, const char *suffix); 26 34 27 35 int strip_typescript_inplace( 28 36 char **buffer,
+1 -1
meson/ant.version
··· 1 - 0.10.1 1 + 0.10.2
+17 -8
src/cli/pkg.c
··· 325 325 326 326 static const char *get_global_dir(void) { 327 327 static char global_dir[4096] = {0}; 328 - if (global_dir[0] == '\0') { 329 - const char *home = getenv("HOME"); 330 - if (home) snprintf(global_dir, sizeof(global_dir), "%s/.ant/pkg/global", home); 331 - } 328 + if (global_dir[0] == '\0') ant_xdg_data_path(global_dir, sizeof(global_dir), "pkg/global"); 332 329 return global_dir; 330 + } 331 + 332 + static const char *get_global_bin_dir(void) { 333 + static char bin_dir[4096] = {0}; 334 + if (bin_dir[0] == '\0') ant_user_bin_path(bin_dir, sizeof(bin_dir)); 335 + return bin_dir; 336 + } 337 + 338 + static const char *get_cache_dir(void) { 339 + static char cache_dir[4096] = {0}; 340 + if (cache_dir[0] == '\0') ant_xdg_cache_path(cache_dir, sizeof(cache_dir), "pkg"); 341 + return cache_dir; 333 342 } 334 343 335 344 static int cmd_add_global(const char *const *package_specs, int count) { ··· 368 377 printf("\n%sinstalled globally%s %s%s%s\n", 369 378 C_GREEN, C_RESET, C_BOLD, package_specs[i], C_RESET); 370 379 } 371 - printf(" %s(binaries linked to ~/.ant/bin)%s\n", C_DIM, C_RESET); 380 + printf(" %s(binaries linked to %s)%s\n", C_DIM, get_global_bin_dir(), C_RESET); 372 381 printf("\n%s[%s", C_DIM, C_RESET); 373 382 print_elapsed(result.elapsed_ms); 374 383 printf("%s]%s done\n", C_DIM, C_RESET); ··· 1057 1066 if (help->count > 0) { 1058 1067 printf("Usage: ant install [packages...] [-g] [-D] [--verbose]\n\n"); 1059 1068 printf("Install from lockfile, or add packages if specified.\n"); 1060 - printf("\nOptions:\n -g, --global Install globally to ~/.ant/pkg/global\n"); 1069 + printf("\nOptions:\n -g, --global Install globally to %s\n", get_global_dir()); 1061 1070 printf(" -D, --save-dev Add as devDependency\n"); 1062 1071 } else if (nerrors > 0) { 1063 1072 arg_print_errors(stdout, end, "ant install"); ··· 1112 1121 if (help->count > 0) { 1113 1122 printf("Usage: ant add <package[@version]>... [options]\n\n"); 1114 1123 printf("Add packages to dependencies.\n"); 1115 - printf("\nOptions:\n -g, --global Install globally to ~/.ant/pkg/global\n"); 1124 + printf("\nOptions:\n -g, --global Install globally to %s\n", get_global_dir()); 1116 1125 printf(" -D, --save-dev Add as devDependency\n"); 1117 1126 } else if (nerrors > 0) { 1118 1127 arg_print_errors(stdout, end, "ant add"); ··· 1643 1652 } 1644 1653 1645 1654 char size_buf[64], db_buf[64]; 1646 - printf("%sCache location:%s ~/.ant/pkg\n", C_BOLD, C_RESET); 1655 + printf("%sCache location:%s %s\n", C_BOLD, C_RESET, get_cache_dir()); 1647 1656 printf("%sPackages:%s %u\n", C_BOLD, C_RESET, stats.package_count); 1648 1657 printf("%sSize:%s %s\n", C_BOLD, C_RESET, format_size(stats.total_size, size_buf, sizeof(size_buf))); 1649 1658 printf("%sDB size:%s %s\n", C_BOLD, C_RESET, format_size(stats.db_size, db_buf, sizeof(db_buf)));
+9 -28
src/esm/remote.c
··· 101 101 ctx->completed = 1; 102 102 } 103 103 104 - static const char *esm_get_home_dir(void) { 105 - #ifdef _WIN32 106 - const char *home = getenv("USERPROFILE"); 107 - if (home) return home; 108 - 109 - const char *drive = getenv("HOMEDRIVE"); 110 - const char *path = getenv("HOMEPATH"); 111 - if (drive && path) { 112 - static char win_home[MAX_PATH]; 113 - snprintf(win_home, sizeof(win_home), "%s%s", drive, path); 114 - return win_home; 115 - } 116 - #else 117 - const char *home = getenv("HOME"); 118 - if (home) return home; 119 - #endif 120 - return NULL; 121 - } 122 - 123 104 static void esm_url_fetch_body_cb(tlsuv_http_req_t *http_req, char *body, ssize_t len) { 124 105 esm_url_fetch_t *ctx = (esm_url_fetch_t *)http_req->data; 125 106 ··· 171 152 } 172 153 173 154 static char *esm_get_cache_path(const char *url) { 174 - const char *home = esm_get_home_dir(); 175 - if (!home) home = "."; 176 - 177 155 uint64_t hash = hash_key(url, strlen(url)); 156 + 157 + char suffix[64]; 158 + snprintf(suffix, sizeof(suffix), "remote/%016llx", (unsigned long long)hash); 178 159 179 - size_t len = strlen(home) + 48; 160 + size_t len = 4096; 180 161 char *cache_path = malloc(len); 181 162 if (!cache_path) return NULL; 182 163 183 - #ifdef _WIN32 184 - snprintf(cache_path, len, "%s\\.ant\\remote\\%016llx", home, (unsigned long long)hash); 185 - #else 186 - snprintf(cache_path, len, "%s/.ant/remote/%016llx", home, (unsigned long long)hash); 187 - #endif 164 + if (ant_xdg_cache_path(cache_path, len, suffix) != 0) { 165 + free(cache_path); 166 + return NULL; 167 + } 168 + 188 169 return cache_path; 189 170 } 190 171
+35 -4
src/pkg/root.zig
··· 25 25 return allocator.dupe(u8, home); 26 26 } 27 27 28 + fn getAbsoluteEnv(name: [:0]const u8) ?[]const u8 { 29 + if (builtin.os.tag == .windows) return null; 30 + const value = std.posix.getenv(name) orelse return null; 31 + if (value.len == 0 or !std.fs.path.isAbsolute(value)) return null; 32 + return value; 33 + } 34 + 28 35 pub const PkgError = enum(c_int) { 29 36 ok = 0, 30 37 out_of_memory = -1, ··· 147 154 pub fn init(allocator: std.mem.Allocator, options: PkgOptions) !*PkgContext { 148 155 const ctx = try allocator.create(PkgContext); 149 156 errdefer allocator.destroy(ctx); 150 - 151 - const cache_path = if (options.cache_dir) |dir| std.mem.span(dir) 152 - else try getDefaultCacheDir(allocator); 153 - 157 + 158 + const default_cache_path = if (options.cache_dir == null) 159 + try getDefaultCacheDir(allocator) else null; 160 + 161 + defer { if (default_cache_path) |path| allocator.free(path); } 162 + const cache_path = if (options.cache_dir) |dir| std.mem.span(dir) else default_cache_path.?; 163 + 154 164 ctx.* = .{ 155 165 .allocator = allocator, 156 166 .arena_state = std.heap.ArenaAllocator.init(allocator), ··· 238 248 } 239 249 240 250 fn getDefaultCacheDir(allocator: std.mem.Allocator) ![]const u8 { 251 + if (builtin.os.tag != .windows) { 252 + if (getAbsoluteEnv("XDG_CACHE_HOME")) |base| { 253 + return std.fmt.allocPrint(allocator, "{s}/ant/pkg", .{base}); 254 + } 255 + const home = try getHomeDir(allocator); 256 + defer allocator.free(home); 257 + return std.fmt.allocPrint(allocator, "{s}/.cache/ant/pkg", .{home}); 258 + } 259 + 241 260 const home = try getHomeDir(allocator); 242 261 defer allocator.free(home); 243 262 return std.fmt.allocPrint(allocator, "{s}/.ant/pkg", .{home}); ··· 2938 2957 } 2939 2958 2940 2959 fn getGlobalDir(allocator: std.mem.Allocator) ![]const u8 { 2960 + if (builtin.os.tag != .windows) { 2961 + if (getAbsoluteEnv("XDG_DATA_HOME")) |base| { 2962 + return std.fmt.allocPrint(allocator, "{s}/ant/pkg/global", .{base}); 2963 + } 2964 + const home = try getHomeDir(allocator); 2965 + defer allocator.free(home); 2966 + return std.fmt.allocPrint(allocator, "{s}/.local/share/ant/pkg/global", .{home}); 2967 + } 2968 + 2941 2969 const home = try getHomeDir(allocator); 2942 2970 defer allocator.free(home); 2943 2971 return std.fmt.allocPrint(allocator, "{s}/.ant/pkg/global", .{home}); ··· 2946 2974 fn getGlobalBinDir(allocator: std.mem.Allocator) ![]const u8 { 2947 2975 const home = try getHomeDir(allocator); 2948 2976 defer allocator.free(home); 2977 + if (builtin.os.tag != .windows) { 2978 + return std.fmt.allocPrint(allocator, "{s}/.local/bin", .{home}); 2979 + } 2949 2980 return std.fmt.allocPrint(allocator, "{s}/.ant/bin", .{home}); 2950 2981 } 2951 2982
+18 -11
src/readline.c
··· 4 4 #include <stdio.h> 5 5 #include <stdlib.h> 6 6 #include <string.h> 7 + #include <crprintf.h> 7 8 #include <sys/stat.h> 8 9 9 10 #ifdef _WIN32 ··· 20 21 #define mkdir_p(path) mkdir(path, 0755) 21 22 #endif 22 23 23 - #include <crprintf.h> 24 - #include "highlight.h" 25 - #include "readline.h" 26 24 #include "utf8.h" 25 + #include "utils.h" 26 + #include "readline.h" 27 + #include "highlight.h" 27 28 28 29 #define MAX_LINE_LENGTH 4096 29 30 ··· 71 72 72 73 void ant_history_add(ant_history_t *hist, const char *line) { 73 74 if (!hist || !hist->lines || hist->capacity <= 0 || !line || line[0] == '\0') return; 74 - if (hist->count > 0 && strcmp(hist->lines[hist->count - 1], line) == 0) return; 75 + 76 + if (hist->count > 0 && strcmp(hist->lines[hist->count - 1], line) == 0) { 77 + hist->current = hist->count; 78 + return; 79 + } 75 80 76 81 if (hist->count >= hist->capacity) { 77 82 free(hist->lines[0]); ··· 110 115 } 111 116 112 117 static char *get_history_path(void) { 113 - const char *home = getenv("HOME"); 114 - if (!home) home = getenv("USERPROFILE"); 115 - if (!home) return NULL; 118 + char dir[4096]; 119 + 120 + if (ant_xdg_state_path(dir, sizeof(dir), NULL) != 0) return NULL; 121 + if (ant_mkdir_p(dir) != 0) return NULL; 116 122 117 - size_t len = strlen(home) + 32; 123 + size_t len = strlen(dir) + sizeof("/repl_history"); 118 124 char *path = malloc(len); 119 - snprintf(path, len, "%s/.ant", home); 120 - mkdir_p(path); 121 - snprintf(path, len, "%s/.ant/repl_history", home); 125 + 126 + if (!path) return NULL; 127 + snprintf(path, len, "%s/repl_history", dir); 128 + 122 129 return path; 123 130 } 124 131
+118
src/utils.c
··· 2 2 #include "messages.h" 3 3 4 4 #include <oxc.h> 5 + #include <errno.h> 5 6 #include <stdio.h> 6 7 #include <stdbool.h> 7 8 #include <sys/stat.h> 8 9 #include <crprintf.h> 10 + 11 + #ifdef _WIN32 12 + #include <direct.h> 13 + #define ANT_MKDIR(path) _mkdir(path) 14 + #else 15 + #define ANT_MKDIR(path) mkdir(path, 0755) 16 + #endif 9 17 10 18 const char *const module_resolve_extensions[] = { 11 19 ".js", ".mjs", ".cjs", 12 20 ".ts", ".mts", ".cts", 13 21 ".json", ".node", NULL 14 22 }; 23 + 24 + static const char *ant_home_dir(void) { 25 + #ifdef _WIN32 26 + const char *home = getenv("USERPROFILE"); 27 + if (home && home[0]) return home; 28 + #else 29 + const char *home = getenv("HOME"); 30 + if (home && home[0]) return home; 31 + #endif 32 + return NULL; 33 + } 34 + 35 + static const char *ant_absolute_env(const char *name) { 36 + #ifdef _WIN32 37 + return NULL; 38 + #else 39 + const char *value = getenv(name); 40 + if (!value || value[0] != '/') return NULL; 41 + return value; 42 + #endif 43 + } 44 + 45 + static int ant_join_app_path( 46 + char *out, size_t out_size, 47 + const char *base, 48 + const char *app, 49 + const char *suffix 50 + ) { 51 + if (!out || out_size == 0 || !base || !base[0] || !app || !app[0]) return -1; 52 + while (suffix && (*suffix == '/' || *suffix == '\\')) suffix++; 53 + 54 + int written = 0; 55 + if (suffix && suffix[0]) written = snprintf(out, out_size, "%s/%s/%s", base, app, suffix); 56 + else written = snprintf(out, out_size, "%s/%s", base, app); 57 + 58 + return (written < 0 || (size_t)written >= out_size) ? -1 : 0; 59 + } 60 + 61 + static int ant_xdg_path( 62 + char *out, size_t out_size, 63 + const char *env_name, 64 + const char *home_suffix, 65 + const char *suffix 66 + ) { 67 + #ifdef _WIN32 68 + const char *home = ant_home_dir(); 69 + if (!home) return -1; 70 + return ant_join_app_path(out, out_size, home, ".ant", suffix); 71 + #else 72 + const char *base = ant_absolute_env(env_name); 73 + if (base) return ant_join_app_path(out, out_size, base, "ant", suffix); 74 + 75 + const char *home = ant_home_dir(); 76 + if (!home) return -1; 77 + char fallback[4096]; 78 + 79 + int written = snprintf(fallback, sizeof(fallback), "%s/%s", home, home_suffix); 80 + if (written < 0 || (size_t)written >= sizeof(fallback)) return -1; 81 + 82 + return ant_join_app_path(out, out_size, fallback, "ant", suffix); 83 + #endif 84 + } 85 + 86 + int ant_mkdir_p(const char *path) { 87 + if (!path || !path[0]) return -1; 88 + 89 + char tmp[4096]; 90 + size_t len = strlen(path); 91 + 92 + if (len >= sizeof(tmp)) return -1; 93 + memcpy(tmp, path, len + 1); 94 + 95 + while ( 96 + len > 1 && (tmp[len - 1] == '/' || 97 + tmp[len - 1] == '\\') 98 + ) tmp[--len] = '\0'; 99 + 100 + for (char *p = tmp + 1; *p; p++) { 101 + if (*p != '/' && *p != '\\') continue; 102 + char sep = *p; *p = '\0'; 103 + if (ANT_MKDIR(tmp) != 0 && errno != EEXIST) return -1; 104 + *p = sep; 105 + } 106 + 107 + if (ANT_MKDIR(tmp) != 0 && errno != EEXIST) return -1; 108 + return 0; 109 + } 110 + 111 + int ant_xdg_cache_path(char *out, size_t out_size, const char *suffix) { 112 + return ant_xdg_path(out, out_size, "XDG_CACHE_HOME", ".cache", suffix); 113 + } 114 + 115 + int ant_xdg_data_path(char *out, size_t out_size, const char *suffix) { 116 + return ant_xdg_path(out, out_size, "XDG_DATA_HOME", ".local/share", suffix); 117 + } 118 + 119 + int ant_xdg_state_path(char *out, size_t out_size, const char *suffix) { 120 + return ant_xdg_path(out, out_size, "XDG_STATE_HOME", ".local/state", suffix); 121 + } 122 + 123 + int ant_user_bin_path(char *out, size_t out_size) { 124 + const char *home = ant_home_dir(); 125 + if (!home || !out || out_size == 0) return -1; 126 + #ifdef _WIN32 127 + int written = snprintf(out, out_size, "%s/.ant/bin", home); 128 + #else 129 + int written = snprintf(out, out_size, "%s/.local/bin", home); 130 + #endif 131 + return (written < 0 || (size_t)written >= out_size) ? -1 : 0; 132 + } 15 133 16 134 int hex_digit(char c) { 17 135 static const int8_t lookup[256] = {