Skip to content

Commit

Permalink
Added autoexec
Browse files Browse the repository at this point in the history
  • Loading branch information
alttabber committed Nov 20, 2024
1 parent 6b9f0cb commit 7bdf20f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

// Default configuration parameters
#define CFG_ORDER_DEF "alpha"
Expand All @@ -32,6 +33,19 @@ struct image_list {
/** Global image list instance. */
static struct image_list ctx;

struct autoexec {
const char* format;
const char* command;
};

struct autoexecs_array {
struct autoexec* entries;
size_t capacity;
size_t size;
};

struct autoexecs_array autoexecs = {NULL, 0, 0};

/** Order names. */
static const char* order_names[] = {
[order_none] = "none",
Expand Down Expand Up @@ -82,6 +96,21 @@ static void add_file(const char* file)
file += 2;
}

// check for autoexec
for (size_t i = 0; i < autoexecs.size; i++) {
const char* ext = strrchr(file, '.');
if ( ext != NULL && strcmp(ext+1, autoexecs.entries[i].format) == 0){
char* execFile = (char*) malloc(sizeof(char)*(LDRSRC_EXEC_LEN+strlen(autoexecs.entries[i].command)+strlen(file)+3));
char* execTail = strchr((const char*) autoexecs.entries[i].command, (int) '%');
char* execHead = strncpy((char*)malloc(sizeof(char)*(strlen(autoexecs.entries[i].command)-strlen(execTail)+1)),
autoexecs.entries[i].command, strlen(autoexecs.entries[i].command)-strlen(execTail));
sprintf(execFile, "%s%s%s%s", LDRSRC_EXEC, execHead, file, execTail+1);
add_entry(execFile);
free(execHead);
return;
}
}

add_entry(file);
}

Expand Down Expand Up @@ -250,6 +279,39 @@ static void load_config(struct config* cfg)
CFG_RECURSIVE_DEF);
ctx.all_files =
config_get_bool(cfg, IMGLIST_SECTION, IMGLIST_ALL, CFG_ALL_DEF);

// autoexec

const char* autoexec_formats = config_get_string(cfg, IMGLIST_AUTOEXEC, AUTOEXEC_FORMATS, NULL);
if ( autoexec_formats == NULL ) return;
char* autoexec_iterator = malloc(sizeof(char)*(strlen(autoexec_formats)+1));
strcpy(autoexec_iterator, autoexec_formats);
char* autoexec_format_entry = strtok(autoexec_iterator, ",");
autoexecs.entries = (struct autoexec*) malloc(sizeof(struct autoexec));
autoexecs.capacity = 1;
while ( autoexec_format_entry != NULL ) {

struct autoexec entry;
entry.format = autoexec_format_entry;

entry.command = config_get_string(cfg, IMGLIST_AUTOEXEC, entry.format, NULL);

if ( entry.command == NULL ) fprintf(stderr ,"WARNING: No corresponding command for %s autoexec format", entry.format);

else {
if (autoexecs.size >= autoexecs.capacity){
struct autoexec* entries_new = (struct autoexec*) malloc(sizeof(struct autoexec)*autoexecs.capacity*2);
memcpy(entries_new, autoexecs.entries, sizeof(struct autoexec)*autoexecs.size);
free(autoexecs.entries);
autoexecs.entries = entries_new;
autoexecs.capacity *= 2;
}

autoexecs.entries[autoexecs.size] = entry;
autoexecs.size++;
}
autoexec_format_entry = strtok(NULL, ",");
}
}

size_t image_list_init(struct config* cfg, const char** sources, size_t num)
Expand Down
2 changes: 2 additions & 0 deletions src/imagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

// Configuration parameters
#define IMGLIST_SECTION "list"
#define IMGLIST_AUTOEXEC "list.autoexec"
#define IMGLIST_ORDER "order"
#define IMGLIST_LOOP "loop"
#define IMGLIST_RECURSIVE "recursive"
#define IMGLIST_ALL "all"
#define AUTOEXEC_FORMATS "formats"

// Invalid index of the entry
#define IMGLIST_INVALID SIZE_MAX
Expand Down

0 comments on commit 7bdf20f

Please sign in to comment.