From f3b23174bfed7203d53040b6ba028b6049f556ad Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 9 Sep 2022 17:47:28 -0400 Subject: Initial commit. --- .gitignore | 3 + .gitmodules | 12 + build.sh | 33 +++ lib/libgit2 | 1 + lib/lua | 1 + lib/openssl | 1 + lib/zlib | 1 + lpm.c | 396 +++++++++++++++++++++++++++++++++ lpm.lua | 730 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 1178 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100755 build.sh create mode 160000 lib/libgit2 create mode 160000 lib/lua create mode 160000 lib/openssl create mode 160000 lib/zlib create mode 100644 lpm.c create mode 100644 lpm.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99f9e35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +lpm +*.o +lpm.lua.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3755286 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[submodule "lua"] + path = lib/lua + url = https://github.com/lua/lua.git +[submodule "lib/libgit2"] + path = lib/libgit2 + url = https://github.com/libgit2/libgit2.git +[submodule "lib/openssl"] + path = lib/openssl + url = https://github.com/openssl/openssl.git +[submodule "lib/zlib"] + path = lib/zlib + url = https://github.com/madler/zlib.git diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..497767d --- /dev/null +++ b/build.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +: ${CC=gcc} +: ${MAKE=make} +: ${BIN=lpm} +: ${JOBS=4} + +SRCS="*.c" +LDFLAGS="$LDFLAGS -lm -pthread -static-libgcc" + +[[ "$@" == "clean" ]] && rm -rf lib/libgit2/build lib/zlib/build lib/openssl/build $BIN && exit 0 + +# Build supporting libraries, libgit2, libz, libssl (with libcrypto), libpcre +if [[ "$@" != *"-libgit2"* ]]; then + [ ! -e "lib/libgit2/include" ] && echo "Make sure you've cloned submodules. (git submodule update --init --depth=1)" && exit -1 + [ ! -e "lib/libgit2/build" ] && cd lib/libgit2 && mkdir build && cmake .. -DBUILD_SHARED_LIBS=OFF -DUSE_SSH=OFF && $MAKE -j $JOBS && cd ../../ + LDFLAGS="$LDFLAGS -Llib/libgit2/build -l:libgit2.a" && CFLAGS="$CFLAGS -Ilib/libgit2/build/include/git2 -Ilib/libgit2/build/include -Ilib/libgit2/include" +fi +if [[ "$@" != *"-lz"* ]]; then + [ ! -e "lib/zlib/build" ] && cd lib/zlib && mkdir build && ../configure && $MAKE -j $JOBS && cd ../../ + LDFLAGS="$LDFLAGS -Llib/libz/build -l:libz.a" && CFLAGS="$CFLAGS -Ilib/libz" +fi +if [[ "$@" != *"-lssl"* ]]; then + [ ! -e "lib/openssl/build" ] && cd lib/openssl && mkdir build && ../Configure && $MAKE -j $JOBS && cd ../../ + LDFLAGS="$LDFLAGS -Llib/libz/build -l:libssl.a -l:libcrypto.a" && CFLAGS="$CFLAGS -Ilib/libz" +fi +[[ "$@" != *"-llua"* ]] && CFLAGS="$CFLAGS -Ilib/lua -DMAKE_LIB=1" && SRCS="$SRCS lib/lua/onelua.c" + +# Build the pre-packaged lua file into the executbale. +echo "const char* luafile = " > lpm.lua.c && cat lpm.lua | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/^/"/' | sed 's/$/\\n"/' >> lpm.lua.c && echo ";" >> lpm.lua.c + +[[ "$@" != *" -g "* || "$@" != *" -O"* ]] && CFLAGS="$CFLAGS -O3" && LDFLAGS="$LDFLAGS -s" +$CC $CFLAGS $SRCS $@ -o lpm $LDFLAGS -ldl -l:libpcre.a diff --git a/lib/libgit2 b/lib/libgit2 new file mode 160000 index 0000000..22f3825 --- /dev/null +++ b/lib/libgit2 @@ -0,0 +1 @@ +Subproject commit 22f382539d78f69aa91721ad62bbd7229750a043 diff --git a/lib/lua b/lib/lua new file mode 160000 index 0000000..997f11f --- /dev/null +++ b/lib/lua @@ -0,0 +1 @@ +Subproject commit 997f11f54322883c3181225f29d101a597f31730 diff --git a/lib/openssl b/lib/openssl new file mode 160000 index 0000000..fd24de9 --- /dev/null +++ b/lib/openssl @@ -0,0 +1 @@ +Subproject commit fd24de9f93049b05a54c48da5316f42882489230 diff --git a/lib/zlib b/lib/zlib new file mode 160000 index 0000000..21767c6 --- /dev/null +++ b/lib/zlib @@ -0,0 +1 @@ +Subproject commit 21767c654d31d2dccdde4330529775c6c5fd5389 diff --git a/lpm.c b/lpm.c new file mode 100644 index 0000000..2da8a13 --- /dev/null +++ b/lpm.c @@ -0,0 +1,396 @@ +/* lpm.c - Core of the lite package manager. + +LPM is a package manager for `lite-xl`, written in C (and packed-in lua). + +It has the following commands: + +lpm add +lpm rm +lpm update [] +lpm install +lpm uninstall +lpm list + +It stores files in a cache directory in the follwoing format: + +: + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef _WIN32 + #include + #include + #include +#endif + +/* 64bit fnv-1a hash */ +typedef unsigned long long hash_t; +static char hexDigits[] = "0123456789abcdef"; +#define FNV_64_PRIME ((hash_t)0x100000001b3ULL) +static int lpm_hash(lua_State* L) { + hash_t hval = 0; + size_t len; + const char* data = luaL_checklstring(L, 1, &len); + const unsigned char *bp = (unsigned char*)data; + const unsigned char *be = data + len; + while (bp < be) { + hval *= FNV_64_PRIME; + hval ^= (unsigned long long)*bp++; + } + char buffer[16]; + for (size_t i = 0; i < len; ++i) { + buffer[i*2+0] = hexDigits[data[i] >> 4]; + buffer[i*2+1] = hexDigits[data[i] & 0xF]; + } + lua_pushlstring(L, buffer, 16); + return 1; +} + +/** BEGIN STOLEN LITE CODE **/ +static int lpm_ls(lua_State *L) { + const char *path = luaL_checkstring(L, 1); + +#ifdef _WIN32 + lua_settop(L, 1); + if (strchr("\\/", path[strlen(path) - 2]) != NULL) + lua_pushstring(L, "*"); + else + lua_pushstring(L, "/*"); + + lua_concat(L, 2); + path = lua_tostring(L, -1); + + LPWSTR wpath = utfconv_utf8towc(path); + if (wpath == NULL) { + lua_pushnil(L); + lua_pushstring(L, UTFCONV_ERROR_INVALID_CONVERSION); + return 2; + } + + WIN32_FIND_DATAW fd; + HANDLE find_handle = FindFirstFileExW(wpath, FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0); + free(wpath); + if (find_handle == INVALID_HANDLE_VALUE) { + lua_pushnil(L); + push_win32_error(L, GetLastError()); + return 2; + } + + char mbpath[MAX_PATH * 4]; // utf-8 spans 4 bytes at most + int len, i = 1; + lua_newtable(L); + + do + { + if (wcscmp(fd.cFileName, L".") == 0) { continue; } + if (wcscmp(fd.cFileName, L"..") == 0) { continue; } + + len = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, mbpath, MAX_PATH * 4, NULL, NULL); + if (len == 0) { break; } + lua_pushlstring(L, mbpath, len - 1); // len includes \0 + lua_rawseti(L, -2, i++); + } while (FindNextFileW(find_handle, &fd)); + + if (GetLastError() != ERROR_NO_MORE_FILES) { + lua_pushnil(L); + push_win32_error(L, GetLastError()); + FindClose(find_handle); + return 2; + } + + FindClose(find_handle); + return 1; +#else + DIR *dir = opendir(path); + if (!dir) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_newtable(L); + int i = 1; + struct dirent *entry; + while ( (entry = readdir(dir)) ) { + if (strcmp(entry->d_name, "." ) == 0) { continue; } + if (strcmp(entry->d_name, "..") == 0) { continue; } + lua_pushstring(L, entry->d_name); + lua_rawseti(L, -2, i); + i++; + } + + closedir(dir); + return 1; +#endif +} + +static int lpm_rmdir(lua_State *L) { + const char *path = luaL_checkstring(L, 1); + +#ifdef _WIN32 + LPWSTR wpath = utfconv_utf8towc(path); + int deleted = RemoveDirectoryW(wpath); + free(wpath); + if (deleted > 0) { + lua_pushboolean(L, 1); + } else { + lua_pushboolean(L, 0); + push_win32_error(L, GetLastError()); + return 2; + } +#else + int deleted = remove(path); + if(deleted < 0) { + lua_pushboolean(L, 0); + lua_pushstring(L, strerror(errno)); + + return 2; + } else { + lua_pushboolean(L, 1); + } +#endif + + return 1; +} + +static int lpm_mkdir(lua_State *L) { + const char *path = luaL_checkstring(L, 1); + +#ifdef _WIN32 + LPWSTR wpath = utfconv_utf8towc(path); + if (wpath == NULL) { + lua_pushboolean(L, 0); + lua_pushstring(L, UTFCONV_ERROR_INVALID_CONVERSION); + return 2; + } + + int err = _wmkdir(wpath); + free(wpath); +#else + int err = mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); +#endif + if (err < 0) { + lua_pushboolean(L, 0); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_pushboolean(L, 1); + return 1; +} + +static int lpm_stat(lua_State *L) { + const char *path = luaL_checkstring(L, 1); + +#ifdef _WIN32 + struct _stat s; + LPWSTR wpath = utfconv_utf8towc(path); + if (wpath == NULL) { + lua_pushnil(L); + lua_pushstring(L, UTFCONV_ERROR_INVALID_CONVERSION); + return 2; + } + int err = _wstat(wpath, &s); + free(wpath); +#else + struct stat s; + int err = stat(path, &s); +#endif + if (err < 0) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_newtable(L); + lua_pushinteger(L, s.st_mtime); + lua_setfield(L, -2, "modified"); + + lua_pushinteger(L, s.st_size); + lua_setfield(L, -2, "size"); + + if (S_ISREG(s.st_mode)) { + lua_pushstring(L, "file"); + } else if (S_ISDIR(s.st_mode)) { + lua_pushstring(L, "dir"); + } else { + lua_pushnil(L); + } + lua_setfield(L, -2, "type"); + +#if __linux__ + if (S_ISDIR(s.st_mode)) { + if (lstat(path, &s) == 0) { + lua_pushboolean(L, S_ISLNK(s.st_mode)); + lua_setfield(L, -2, "symlink"); + } + } +#endif + return 1; +} +/** END STOLEN LITE CODE **/ + +static const char* git_error_last_string() { + const git_error* last_error = git_error_last(); + return last_error->message; +} + +static int git_get_id(git_oid* commit_id, git_repository* repository, const char* name) { + int length = strlen(name); + int is_hex = length == 40; + for (int i = 0; is_hex && i < length; ++i) + is_hex = isxdigit(name[i]); + if (!is_hex) + return git_reference_name_to_id(commit_id, repository, name); + return git_oid_fromstr(commit_id, name); +} + +static git_repository* luaL_checkgitrepo(lua_State* L, int index) { + const char* path = luaL_checkstring(L, index); + git_repository* repository; + if (git_repository_open(&repository, path)) + return (void*)(long long)luaL_error(L, "git open error: %s", git_error_last_string()); + return repository; +} + + +static git_commit* git_retrieve_commit(git_repository* repository, const char* commit_name) { + git_oid commit_id; + git_commit* commit; + if (git_get_id(&commit_id, repository, commit_name)) + return NULL; + if (git_commit_lookup(&commit, repository, &commit_id)) + return NULL; + return commit; +} + + +int lpm_reset(lua_State* L) { + git_repository* repository = luaL_checkgitrepo(L, 1); + const char* commit_name = luaL_checkstring(L, 2); + const char* type = luaL_checkstring(L, 3); + git_commit* commit = git_retrieve_commit(repository, commit_name); + if (!commit) { + git_repository_free(repository); + return luaL_error(L, "git retrieve commit error: %s", git_error_last_string()); + } + git_reset_t reset_type = GIT_RESET_SOFT; + if (strcmp(type, "mixed") == 0) + reset_type = GIT_RESET_MIXED; + else if (strcmp(type, "hard") == 0) + reset_type = GIT_RESET_HARD; + int result = git_reset(repository, (git_object*)commit, reset_type, NULL); + git_commit_free(commit); + git_repository_free(repository); + if (result) + return luaL_error(L, "git reset error: %s", git_error_last_string()); + return 0; +} + + +int lpm_init(lua_State* L) { + const char* path = luaL_checkstring(L, 1); + const char* url = luaL_checkstring(L, 2); + git_repository* repository; + if (git_repository_init(&repository, path, 0) != 0) + return luaL_error(L, "git init error: %s", git_error_last_string()); + git_remote* remote; + if (git_remote_create(&remote, repository, "origin", url)) { + git_repository_free(repository); + return luaL_error(L, "git remote add error: %s", git_error_last_string()); + } + git_remote_free(remote); + git_repository_free(repository); + return 0; +} + + +int lpm_fetch(lua_State* L) { + git_repository* repository = luaL_checkgitrepo(L, 1); + git_remote* remote; + if (git_remote_lookup(&remote, repository, "origin")) { + git_repository_free(repository); + return luaL_error(L, "git remote fetch error: %s", git_error_last_string()); + } + git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; + if (git_remote_fetch(remote, NULL, &fetch_opts, NULL)) { + git_remote_free(remote); + git_repository_free(repository); + return luaL_error(L, "git remote fetch error: %s", git_error_last_string()); + } + git_remote_free(remote); + git_repository_free(repository); + return 0; +} + + +int lpm_status(lua_State* L) { + const char* path = luaL_checkstring(L, 1); + git_repository* repository; + if (git_repository_open(&repository, path)) + return luaL_error(L, "git open error: %s", git_error_last_string()); + git_repository_free(repository); + lua_newtable(L); + lua_pushnil(L); + lua_setfield(L, -2, "commit"); + lua_pushnil(L); + lua_setfield(L, -2, "branch"); + return 1; +} + + +static const luaL_Reg system_lib[] = { + { "ls", lpm_ls }, // Returns an array of files. + { "stat", lpm_stat }, // Returns info about a single file. + { "mkdir", lpm_mkdir }, // Makes a directory. + { "rmdir", lpm_rmdir }, // Removes a directory. + { "hash", lpm_hash }, // Returns a hexhash. + { "init", lpm_init }, // Initializes a git repository with the specified remote. + { "fetch", lpm_fetch }, // Updates a git repository with the specified remote. + { "reset", lpm_reset }, // Updates a git repository to the specified commit/hash/branch. + { "status", lpm_status } // Returns the git repository in question's current branch, if any, and commit hash. +}; + +extern const char* luafile; +int main(int argc, char* argv[]) { + git_libgit2_init(); + lua_State* L = luaL_newstate(); + luaL_openlibs(L); + luaL_newlib(L, system_lib); lua_setglobal(L, "system"); + lua_newtable(L); + for (int i = 0; i < argc; ++i) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i+1); + } + lua_setglobal(L, "ARGV"); + #if _WIN32 + lua_pushliteral(L, "\\"); + #else + lua_pushliteral(L, "/"); + #endif + lua_setglobal(L, "PATHSEP"); + //if (luaL_loadstring(L, luafile)) { + if (luaL_loadfile(L, "lpm.lua")) { + fprintf(stderr, "internal error when starting the application: %s\n", lua_tostring(L, -1)); + return -1; + } + lua_pcall(L, 0, 1, 0); + int status = lua_tointeger(L, -1); + lua_close(L); + git_libgit2_shutdown(); + return status; +} diff --git a/lpm.lua b/lpm.lua new file mode 100644 index 0000000..d0e53e7 --- /dev/null +++ b/lpm.lua @@ -0,0 +1,730 @@ +local strict = {} +strict.defined = {} + +-- used to define a global variable +function global(t) + for k, v in pairs(t) do + strict.defined[k] = true + rawset(_G, k, v) + end +end + +function strict.__newindex(t, k, v) + error("cannot set undefined variable: " .. k, 2) +end + +function strict.__index(t, k) + if not strict.defined[k] then + error("cannot get undefined variable: " .. k, 2) + end +end + +setmetatable(_G, strict) + + +-- Begin JSON library. +-- +-- json.lua +-- +-- Copyright (c) 2020 rxi +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy of +-- this software and associated documentation files (the "Software"), to deal in +-- the Software without restriction, including without limitation the rights to +-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +-- of the Software, and to permit persons to whom the Software is furnished to do +-- so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. +-- + +local json = { _version = "0.1.2" } + +------------------------------------------------------------------------------- +-- Encode +------------------------------------------------------------------------------- + +local encode + +local escape_char_map = { + [ "\\" ] = "\\", + [ "\"" ] = "\"", + [ "\b" ] = "b", + [ "\f" ] = "f", + [ "\n" ] = "n", + [ "\r" ] = "r", + [ "\t" ] = "t", +} + +local escape_char_map_inv = { [ "/" ] = "/" } +for k, v in pairs(escape_char_map) do + escape_char_map_inv[v] = k +end + + +local function escape_char(c) + return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte())) +end + + +local function encode_nil(val) + return "null" +end + + +local function encode_table(val, stack) + local res = {} + stack = stack or {} + + -- Circular reference? + if stack[val] then error("circular reference") end + + stack[val] = true + + if rawget(val, 1) ~= nil or next(val) == nil then + -- Treat as array -- check keys are valid and it is not sparse + local n = 0 + for k in pairs(val) do + if type(k) ~= "number" then + error("invalid table: mixed or invalid key types") + end + n = n + 1 + end + if n ~= #val then + error("invalid table: sparse array") + end + -- Encode + for i, v in ipairs(val) do + table.insert(res, encode(v, stack)) + end + stack[val] = nil + return "[" .. table.concat(res, ",") .. "]" + + else + -- Treat as an object + for k, v in pairs(val) do + if type(k) ~= "string" then + error("invalid table: mixed or invalid key types") + end + table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) + end + stack[val] = nil + return "{" .. table.concat(res, ",") .. "}" + end +end + + +local function encode_string(val) + return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"' +end + + +local function encode_number(val) + -- Check for NaN, -inf and inf + if val ~= val or val <= -math.huge or val >= math.huge then + error("unexpected number value '" .. tostring(val) .. "'") + end + return string.format("%.14g", val) +end + + +local type_func_map = { + [ "nil" ] = encode_nil, + [ "table" ] = encode_table, + [ "string" ] = encode_string, + [ "number" ] = encode_number, + [ "boolean" ] = tostring, +} + + +encode = function(val, stack) + local t = type(val) + local f = type_func_map[t] + if f then + return f(val, stack) + end + error("unexpected type '" .. t .. "'") +end + + +function json.encode(val) + return ( encode(val) ) +end + + +------------------------------------------------------------------------------- +-- Decode +------------------------------------------------------------------------------- + +local parse + +local function create_set(...) + local res = {} + for i = 1, select("#", ...) do + res[ select(i, ...) ] = true + end + return res +end + +local space_chars = create_set(" ", "\t", "\r", "\n") +local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") +local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") +local literals = create_set("true", "false", "null") + +local literal_map = { + [ "true" ] = true, + [ "false" ] = false, + [ "null" ] = nil, +} + + +local function next_char(str, idx, set, negate) + for i = idx, #str do + if set[str:sub(i, i)] ~= negate then + return i + end + end + return #str + 1 +end + + +local function decode_error(str, idx, msg) + local line_count = 1 + local col_count = 1 + for i = 1, idx - 1 do + col_count = col_count + 1 + if str:sub(i, i) == "\n" then + line_count = line_count + 1 + col_count = 1 + end + end + error( string.format("%s at line %d col %d", msg, line_count, col_count) ) +end + + +local function codepoint_to_utf8(n) + -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa + local f = math.floor + if n <= 0x7f then + return string.char(n) + elseif n <= 0x7ff then + return string.char(f(n / 64) + 192, n % 64 + 128) + elseif n <= 0xffff then + return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) + elseif n <= 0x10ffff then + return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, + f(n % 4096 / 64) + 128, n % 64 + 128) + end + error( string.format("invalid unicode codepoint '%x'", n) ) +end + + +local function parse_unicode_escape(s) + local n1 = tonumber( s:sub(1, 4), 16 ) + local n2 = tonumber( s:sub(7, 10), 16 ) + -- Surrogate pair? + if n2 then + return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) + else + return codepoint_to_utf8(n1) + end +end + + +local function parse_string(str, i) + local res = "" + local j = i + 1 + local k = j + + while j <= #str do + local x = str:byte(j) + + if x < 32 then + decode_error(str, j, "control character in string") + + elseif x == 92 then -- `\`: Escape + res = res .. str:sub(k, j - 1) + j = j + 1 + local c = str:sub(j, j) + if c == "u" then + local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1) + or str:match("^%x%x%x%x", j + 1) + or decode_error(str, j - 1, "invalid unicode escape in string") + res = res .. parse_unicode_escape(hex) + j = j + #hex + else + if not escape_chars[c] then + decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string") + end + res = res .. escape_char_map_inv[c] + end + k = j + 1 + + elseif x == 34 then -- `"`: End of string + res = res .. str:sub(k, j - 1) + return res, j + 1 + end + + j = j + 1 + end + + decode_error(str, i, "expected closing quote for string") +end + + +local function parse_number(str, i) + local x = next_char(str, i, delim_chars) + local s = str:sub(i, x - 1) + local n = tonumber(s) + if not n then + decode_error(str, i, "invalid number '" .. s .. "'") + end + return n, x +end + + +local function parse_literal(str, i) + local x = next_char(str, i, delim_chars) + local word = str:sub(i, x - 1) + if not literals[word] then + decode_error(str, i, "invalid literal '" .. word .. "'") + end + return literal_map[word], x +end + + +local function parse_array(str, i) + local res = {} + local n = 1 + i = i + 1 + while 1 do + local x + i = next_char(str, i, space_chars, true) + -- Empty / end of array? + if str:sub(i, i) == "]" then + i = i + 1 + break + end + -- Read token + x, i = parse(str, i) + res[n] = x + n = n + 1 + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "]" then break end + if chr ~= "," then decode_error(str, i, "expected ']' or ','") end + end + return res, i +end + + +local function parse_object(str, i) + local res = {} + i = i + 1 + while 1 do + local key, val + i = next_char(str, i, space_chars, true) + -- Empty / end of object? + if str:sub(i, i) == "}" then + i = i + 1 + break + end + -- Read key + if str:sub(i, i) ~= '"' then + decode_error(str, i, "expected string for key") + end + key, i = parse(str, i) + -- Read ':' delimiter + i = next_char(str, i, space_chars, true) + if str:sub(i, i) ~= ":" then + decode_error(str, i, "expected ':' after key") + end + i = next_char(str, i + 1, space_chars, true) + -- Read value + val, i = parse(str, i) + -- Set + res[key] = val + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "}" then break end + if chr ~= "," then decode_error(str, i, "expected '}' or ','") end + end + return res, i +end + + +local char_func_map = { + [ '"' ] = parse_string, + [ "0" ] = parse_number, + [ "1" ] = parse_number, + [ "2" ] = parse_number, + [ "3" ] = parse_number, + [ "4" ] = parse_number, + [ "5" ] = parse_number, + [ "6" ] = parse_number, + [ "7" ] = parse_number, + [ "8" ] = parse_number, + [ "9" ] = parse_number, + [ "-" ] = parse_number, + [ "t" ] = parse_literal, + [ "f" ] = parse_literal, + [ "n" ] = parse_literal, + [ "[" ] = parse_array, + [ "{" ] = parse_object, +} + + +parse = function(str, idx) + local chr = str:sub(idx, idx) + local f = char_func_map[chr] + if f then + return f(str, idx) + end + decode_error(str, idx, "unexpected character '" .. chr .. "'") +end + + +function json.decode(str) + if type(str) ~= "string" then + error("expected argument of type string, got " .. type(str)) + end + local res, idx = parse(str, next_char(str, 1, space_chars, true)) + idx = next_char(str, idx, space_chars, true) + if idx <= #str then + decode_error(str, idx, "trailing garbage") + end + return res +end + +-- End JSON library. + +local common = {} + +function common.rmrf(root) + if not root or root == "" then return end + local info = system.stat(root) + if not info then return end + if info.type == "file" then return os.remove(root) end + for i,v in ipairs(system.ls(root)) do common.rmrf(root .. PATHSEP .. v) end + system.rmdir(root) +end + +function common.mkdirp(path) + local stat = system.stat(path) + if stat and stat.type == "dir" then return true end + if stat and stat.type == "file" then error("path " .. path .. " exists") end + local subdirs = {} + while path and path ~= "" do + system.mkdir(path) + local updir, basedir = path:match("(.*)[/\\](.+)$") + table.insert(subdirs, 1, basedir or path) + path = updir + end + for _, dirname in ipairs(subdirs) do + path = path and path .. PATHSEP .. dirname or dirname + system.mkdir(path) + end +end + + +function common.basename(path) + local s = path:reverse():find(PATHSEP) + if not s then return path end + return path:sub(#path - s + 1) +end + + +function common.copy(src, dst) + local src_stat, dst_stat = system.stat(src), system.stat(dst) + if not src_stat then error("can't find " .. src) end + if dst_stat and dst_stat.type == "dir" and src_stat.type == "file" then return common.copy(src, dst .. PATHSEP .. common.basename(src)) end + if dst_stat then error("path " .. dst .. " exists") end + local src_io, dst_io = io.open(src, "rb"), io.write(dst, "wb") + while true do + local chunk = src_io:read(16*1024) + if not chunk then break end + dst_io:write(chunk) + end +end + +local HOME, USERDIR, CACHEDIR, JSON, repositories + +local Repository = {} +function Repository.__index(self, idx) return rawget(self, idx) or Repository[idx] end +function Repository.new(hash) + if not hash.remote then error("requires a remote") end + local self = setmetatable({ + commit = hash.commit, + remote = hash.remote, + branch = hash.branch, + plugins = nil, + local_path = CACHEDIR .. PATHSEP .. system.hash(hash.remote), + last_retrieval = nil + }, Repository) + if system.stat(self.local_path) and not self.commit and not self.branch then + -- In the case where we don't have a branch, and don't have a commit, check for the presence of `master` and `main`. + if system.stat(self.local_path .. PATHSEP .. "master") then + self.branch = "master" + elseif system.stat(self.local_path .. PATHSEP .. PATHSEP .. "main") then + self.branch = "main" + end + end + self:parse_manifest() + return self +end + + +function Repository:parse_manifest() + if self.manifest then return self.manifest end + if system.stat(self.local_path) and system.stat(self.local_path .. PATHSEP .. (self.commit or self.branch)) then + self.manifest_path = self.local_path .. PATHSEP .. (self.commit or self.branch) .. PATHSEP .. "manifest.json" + if system.stat(self.manifest_path) then + self.manifest = json.parse(io.open(manifest_path, "rb"):read("*all")) + self.plugins = {} + for i, metadata in ipairs(self.manifest["plugins"]) do + table.insert(self.plugins, Plugin.new(self, metadata)) + end + end + end + return self.manifest +end + + +-- in the cases where we don't have a manifest, assume generalized structure, take plugins folder, trawl through it, build manifest that way +-- assuming each .lua file under +function Repository:generate_manifest() + +end + +function Repository:add() + -- If neither specified then pull onto `master`, and check the main branch name, and move if necessary. + if not self.branch and not self.commit then + local path = self.local_path .. PATHSEP .. "master" + common.mkdirp(path) + system.init(path, self.remote) + if not pcall(system.reset, "refs/heads/master") then + if pcall(system.reset, "refs/heads/main") then + os.rename(path, self.local_path .. PATHSEP .. "main") + else + error("Can't find master or main.") + end + end + else + local path = self.local_path .. PATHSEP .. (self.commit or self.branch) + common.mkdirp(path) + system.init(path, self.remote) + system.fetch(path) + system.reset(path, self.commit or ("refs/remotes/origin/" .. self.branch), "hard") + self.manifest = nil + self:parse_manifest() + end +end + + +function Repository:update() + if self.branch then + local path = self.local_path .. PATHSEP .. self.branch + system.fetch(path) + system.reset(path, "refs/remotes/origin/" .. self.branch, "hard") + self.manifest = nil + self:parse_manifest() + end +end + + +function Repository:remove() + common.rmrf(self.path) +end + + +local Plugin = { __index = function(self, idx) return rawget(self, idx) or Plugin[idx] end } +function Plugin.new(repository, metadata) + return setmetatable(common.merge({ + repository = repository, + tags = {}, + remote = nil, + path = nil, + version = "1.0", + local_path = repository.local_path .. PATHSEP .. "repo" .. PATHSEP .. (metadata.path or metadata.name), + install_path = USERDIR .. PATHSEP .. "plugins" .. PATHSEP .. (metadata.path or metadata.name), + }, metadata), Plugin) +end + +function Plugin:install() + common.copy(self.local_path, self.install_path) +end + + +function Plugin:uninstall() + common.rmrf(self.install_path) +end + + +local function get_repository(url) + if not url then error("requires a repository url") end + for i,v in ipairs(repositories) do + if v.url == url then return i, v end + end + return nil +end + + +local function match_version(version, pattern) + return not pattern or version == pattern +end + + +local function get_plugin(name, version) + local candidates = {} + for i,repo in ipairs(repositories) do + for j,plugin in ipairs(repo.plugins) do + if match_version(plugin.version, version) then + table.insert(candidates, plugin) + end + end + end + return table.unpack(table.sort(candidates, function (a,b) return a.version < b.version end)) +end + + +local function lpm_add(url) + local idx, repo = get_repository(url) + if repo then -- if we're alreayd a repo, put this at the head of the resolution list + table.remove(repositories, idx) + else + repo = Repository.new(url):add() + end + table.insert(repositories, 1, repo) + repo:update() +end + + +local function lpm_rm(url) + local idx, repo = get_repository(url) + if not repo then error("cannot find repository " .. url) end + table.remove(repositories, idx) + repo:remove() +end + + +local function lpm_update(url) + local repo = url and get_repository(url) + for i,v in ipairs(repositories) do if not repo or v == repo then v:update() end end +end + + +local function lpm_install(name, version) + local plugin = get_plugin(name, version) + if not plugin then error("can't find plugin " .. name) end + plugin:install() +end + + +local function lpm_uninstall(name) + local plugin = get_plugin(name, version) + if not plugin then error("can't find plugin " .. name) end + if not plugin.installed then error("plugin " .. name .. " not installed") end + plugin:uninstall() +end + + +local function lpm_list() + local result = { plugins = { } } + for i,repo in ipairs(repositories) do + if not repo.plugins then error("can't find plugins for repo " .. repo.remote .. ":" .. (repo.commit or repo.branch or "master")) end + for j,plugin in ipairs(repo.plugins) do + table.insert(result.plugins, plugin) + end + end + if JSON then + io.stdout:write(json.encode(result)) + else + + end +end + + +local function lpm_purge() + common.rmrf(CACHEDIR) +end + + +local status = 0 +xpcall(function() + JSON = os.getenv("LPM_JSON") + HOME = (os.getenv("USERPROFILE") or os.getenv("HOME")):gsub(PATHSEP .. "$", "") + USERDIR = os.getenv("LITE_USERDIR") or (os.getenv("XDG_CONFIG_HOME") and os.getenv("XDG_CONFIG_HOME") .. PATHSEP .. "lite-xl") + or (HOME and (HOME .. PATHSEP .. '.config' .. PATHSEP .. 'lite-xl')) + CACHEDIR = os.getenv("LPM_CACHE") or USERDIR .. PATHSEP .. "lpm" + repositories = {} + repositories = { Repository.new({ remote = "https://github.com/lite-xl/lite-xl-plugins.git", branch = "master" }) } + if not system.stat(CACHEDIR) or not system.stat(repositories[1].local_path) then + common.mkdirp(repositories[1].local_path) + repositories[1]:add() + else + for i, remote_hash in ipairs(system.ls(CACHEDIR)) do + local remote + for j, commit_or_branch in ipairs(system.ls(CACHEDIR .. PATHSEP .. remote_hash)) do + if system.stat(CACHEDIR .. PATHSEP .. remote_hash .. PATHSEP .. commit_or_branch .. PATHSEP .. ".git" .. PATHSEP .."config") then + for line in io.lines(CACHEDIR .. PATHSEP .. remote_hash .. PATHSEP .. commit_or_branch .. PATHSEP .. ".git" .. PATHSEP .."config") do + local s,e = line:find("url = ") + if s then remote = line:sub(s+1) break end + end + if remote then + if #commit_or_branch == 40 and not commit_or_branch:find("[^a-z0-9]") then + table.insert(repositories, Repository.new({ remote = remote, commit = commit_or_branch })) + else + table.insert(repositories, Repository.new({ remote = remote, branch = commit_or_branch })) + end + end + end + end + end + end + + if ARGV[2] == "add" then return lpm_add(ARGV[3]) end + if ARGV[2] == "rm" then return lpm_rm(ARGV[3]) end + if ARGV[2] == "update" then return lpm_update(ARGV[3]) end + if ARGV[2] == "install" then return lpm_install(ARGV[3]) end + if ARGV[2] == "uninstall" then return lpm_uninstall(ARGV[3]) end + if ARGV[2] == "purge" then return lpm_purge(ARGV[4]) end + if ARGV[2] == "list" then return lpm_list() end + io.stderr:write([[ +Usage: lpm COMMAND + +LPM is a package manager for `lite-xl`, written in C (and packed-in lua). + +It has the following commands: + +lpm add +lpm rm +lpm update [] +lpm install +lpm uninstall +lpm list +]]) +end, function(err) + if JSON then + io.stderr:write(json.encode({ error = err, traceback = debug.traceback() })) + else + io.stderr:write(err .. "\n") + io.stderr:write(debug.traceback() .. "\n") + end + status = -1 +end) + + +return status; -- cgit v1.2.3