mirror of
https://github.com/pine64/blisp.git
synced 2025-02-25 21:33:42 +00:00
Move some stuff to new util file
This commit is contained in:
parent
2740d305ba
commit
a072ddb3f3
@ -1,6 +1,6 @@
|
|||||||
add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3)
|
add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3)
|
||||||
|
|
||||||
add_executable(blisp src/main.c src/cmd/write.c)
|
add_executable(blisp src/main.c src/cmd/write.c src/util.c)
|
||||||
|
|
||||||
target_include_directories(blisp PRIVATE
|
target_include_directories(blisp PRIVATE
|
||||||
"${CMAKE_SOURCE_DIR}/include"
|
"${CMAKE_SOURCE_DIR}/include"
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
#include <assert.h>
|
|
||||||
#include <blisp.h>
|
#include <blisp.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -7,17 +6,10 @@
|
|||||||
#include "../cmd.h"
|
#include "../cmd.h"
|
||||||
#include "argtable3.h"
|
#include "argtable3.h"
|
||||||
#include "blisp_struct.h"
|
#include "blisp_struct.h"
|
||||||
|
#include "../util.h"
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
#include <unistd.h>
|
|
||||||
#elif defined(_MSC_VER)
|
|
||||||
#include <BaseTsd.h>
|
|
||||||
typedef SSIZE_T ssize_t;
|
|
||||||
#include <windows.h>
|
|
||||||
#define PATH_MAX MAX_PATH
|
|
||||||
#elif defined(__APPLE__)
|
|
||||||
#include <sys/syslimits.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define REG_EXTENDED 1
|
#define REG_EXTENDED 1
|
||||||
@ -30,45 +22,6 @@ static struct arg_lit* reset;
|
|||||||
static struct arg_end* end;
|
static struct arg_end* end;
|
||||||
static void* cmd_write_argtable[6];
|
static void* cmd_write_argtable[6];
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
// Ugh. This stuff is just so messy without C++17 or Qt...
|
|
||||||
// These are not thread safe, but it doesn't place the responsibility
|
|
||||||
// to free an allocated buffer on the caller.nn
|
|
||||||
|
|
||||||
static void get_executable_path(char* buffer_out, uint32_t max_size) {
|
|
||||||
assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux.
|
|
||||||
|
|
||||||
char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x
|
|
||||||
char real_path_name[PATH_MAX]; // /var/tmp/x
|
|
||||||
uint32_t raw_path_size = sizeof(raw_path_name);
|
|
||||||
|
|
||||||
if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) {
|
|
||||||
realpath(raw_path_name, real_path_name);
|
|
||||||
}
|
|
||||||
// *real_path_name is appropriately sized and null terminated.
|
|
||||||
strcpy(buffer_out, real_path_name);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ssize_t get_binary_folder(char* buffer, uint32_t buffer_size) {
|
|
||||||
#ifdef __linux__
|
|
||||||
if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
char* pos = strrchr(buffer, '/');
|
|
||||||
#elif defined(__APPLE__)
|
|
||||||
get_executable_path(buffer, buffer_size);
|
|
||||||
char* pos = strrchr(buffer, '/');
|
|
||||||
#else
|
|
||||||
if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
char* pos = strrchr(buffer, '\\');
|
|
||||||
#endif
|
|
||||||
pos[0] = '\0';
|
|
||||||
return pos - buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
||||||
memcpy(boot_header->magiccode, "BFNP", 4);
|
memcpy(boot_header->magiccode, "BFNP", 4);
|
||||||
|
|
||||||
@ -278,7 +231,7 @@ void blisp_flash_firmware() {
|
|||||||
|
|
||||||
char exe_path[PATH_MAX];
|
char exe_path[PATH_MAX];
|
||||||
char eflash_loader_path[PATH_MAX];
|
char eflash_loader_path[PATH_MAX];
|
||||||
if (get_binary_folder(exe_path, PATH_MAX) <= 0) {
|
if (util_get_binary_folder(exe_path, PATH_MAX) <= 0) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Failed to find executable path to search for the "
|
"Failed to find executable path to search for the "
|
||||||
"eflash loader\n");
|
"eflash loader\n");
|
||||||
|
56
tools/blisp/src/util.c
Normal file
56
tools/blisp/src/util.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
#include "util.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <unistd.h>
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#include <BaseTsd.h>
|
||||||
|
typedef SSIZE_T ssize_t;
|
||||||
|
#include <windows.h>
|
||||||
|
#define PATH_MAX MAX_PATH
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <sys/syslimits.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
// Ugh. This stuff is just so messy without C++17 or Qt...
|
||||||
|
// These are not thread safe, but it doesn't place the responsibility
|
||||||
|
// to free an allocated buffer on the caller.nn
|
||||||
|
|
||||||
|
static void util_get_executable_path(char* buffer_out, uint32_t max_size) {
|
||||||
|
assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux.
|
||||||
|
|
||||||
|
char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x
|
||||||
|
char real_path_name[PATH_MAX]; // /var/tmp/x
|
||||||
|
uint32_t raw_path_size = sizeof(raw_path_name);
|
||||||
|
|
||||||
|
if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) {
|
||||||
|
realpath(raw_path_name, real_path_name);
|
||||||
|
}
|
||||||
|
// *real_path_name is appropriately sized and null terminated.
|
||||||
|
strcpy(buffer_out, real_path_name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size) {
|
||||||
|
#ifdef __linux__
|
||||||
|
if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
char* pos = strrchr(buffer, '/');
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
util_get_executable_path(buffer, buffer_size);
|
||||||
|
char* pos = strrchr(buffer, '/');
|
||||||
|
#else
|
||||||
|
if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
char* pos = strrchr(buffer, '\\');
|
||||||
|
#endif
|
||||||
|
pos[0] = '\0';
|
||||||
|
return pos - buffer;
|
||||||
|
}
|
12
tools/blisp/src/util.h
Normal file
12
tools/blisp/src/util.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
#ifndef BLISP_UTIL_H
|
||||||
|
#define BLISP_UTIL_H
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user