Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
7.6.0 : improve static and const usage, improve performance
Browse files Browse the repository at this point in the history
Signed-off-by: Ari Archer <ari.web.xyz@gmail.com>
  • Loading branch information
Ari Archer committed May 21, 2023
1 parent b6766fc commit f652daf
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 58 deletions.
2 changes: 1 addition & 1 deletion baz
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[ "$BAZ_DEBUG" ] && set -x

export BAZ_VERSION='7.5.7'
export BAZ_VERSION='7.6.0'
export BAZ_DIR="$HOME/.local/share/baz"
export BAZ_CONFDIR="$HOME/.config/baz"
export BAZ_CONF="$BAZ_CONFDIR/config.env"
Expand Down
5 changes: 5 additions & 0 deletions doc/PLUGIN_FOLDER_STRUCTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,8 @@ also you an append `-pre` to run before the hook and
- Hook (run)
- Source (source)
- Post (source)
## disabled
disabled plugins will have the `disabled` 0 byte file in the root of them,
that means they can still be loaded using `baz_load_plugin_low` but wont be loaded automatically
26 changes: 16 additions & 10 deletions loader/file.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#ifndef _FILE_H
#define _FILE_H
#ifndef FILE_NO_STDDEF
#include "config.h"

#include <stddef.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

typedef struct {
size_t content_size;
char *content;
} File;

static void free_file(File *);
static void alloc_file(const char *, File *);
static char *read_line(const char *);
static void free_file(const File *const);
static void alloc_file(const char *const, File *const);
static char *read_line(const char *const);

#ifdef FILE_IMPL
static void free_file(File *f) { free(f->content); }
static void free_file(const File *const f) { free(f->content); }

static void alloc_file(const char *path, File *f) {
static void alloc_file(const char *const path, File *const f) {
static int fd;
static size_t content_size;

Expand All @@ -42,10 +45,13 @@ static void alloc_file(const char *path, File *f) {
close(fd);
}

static char *read_line(const char *path) {
static char *read_line(const char *const path) {
static int fd;
static ssize_t sz = 0, rb;
char *buf = NULL;
static ssize_t sz, rb;
static char *buf;

sz = 0;
buf = 0;

if ((fd = open(path, O_RDONLY)) == -1)
return NULL;
Expand Down
61 changes: 28 additions & 33 deletions loader/loader.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "config.h"

#define FILE_NO_STDDEF
#define PATH_NO_DIRENT
#define SHELL_NO_FILE

#define FILE_IMPL
#define PATH_IMPL
#define SHELL_IMPL
Expand Down Expand Up @@ -33,13 +29,18 @@ static unsigned char debug_load;

static File f = {0}, *pf = &f;

static char path[PATH_MAX + 1];
static size_t base;

static DIR *dp;
static struct dirent *ep;

typedef struct {
void (*stage)(char *, const size_t, DIR *, struct dirent *);
void (*stage)(void);
const char *path;
} stage_t;

static void
load_envs(char *path, const size_t base, DIR *dp, struct dirent *ep) {
LOAD_FN(envs) {
log("loading environment variables");

nputs("export");
Expand All @@ -57,8 +58,7 @@ load_envs(char *path, const size_t base, DIR *dp, struct dirent *ep) {
pnl();
}

static void
load_cmds(char *path, const size_t base, DIR *dp, struct dirent *ep) {
LOAD_FN(cmds) {
log("adding commands");

printf("PATH=\"%s:$PATH\"\n", path);
Expand All @@ -70,8 +70,7 @@ load_cmds(char *path, const size_t base, DIR *dp, struct dirent *ep) {
}
}

static void
load_functions(char *path, const size_t base, DIR *dp, struct dirent *ep) {
LOAD_FN(functions) {
log("loading functions");

while ((ep = readdir_visible(dp))) {
Expand All @@ -84,8 +83,7 @@ load_functions(char *path, const size_t base, DIR *dp, struct dirent *ep) {
}
}

static void
load_aliases(char *path, const size_t base, DIR *dp, struct dirent *ep) {
LOAD_FN(aliases) {
log("loading aliases");

nputs("alias");
Expand All @@ -103,8 +101,7 @@ load_aliases(char *path, const size_t base, DIR *dp, struct dirent *ep) {
pnl();
}

static void
load_runners(char *path, const size_t base, DIR *dp, struct dirent *ep) {
LOAD_FN(runners) {
log("running runners");

while ((ep = readdir_visible(dp))) {
Expand All @@ -116,9 +113,8 @@ load_runners(char *path, const size_t base, DIR *dp, struct dirent *ep) {
}
}

static void
load_completions(char *path, const size_t base, DIR *dp, struct dirent *ep) {
char *line;
LOAD_FN(completions) {
static char *line;

log("loading completions");

Expand All @@ -135,9 +131,9 @@ load_completions(char *path, const size_t base, DIR *dp, struct dirent *ep) {
}
}

static void load_keybinds(char *path, const size_t base) {
struct dirent *ep;
DIR *dp;
LOAD_FN(keybinds) {
static struct dirent *epk;
static DIR *dpk;

log("binding keybinds");

Expand All @@ -151,29 +147,26 @@ static void load_keybinds(char *path, const size_t base) {
path[base] = '\0';
pathcat(DP(KEYS_CTX_DIR));

if ((!path_exists(path)) || ((dp = opendir(path)) == NULL))
if ((!path_exists(path)) || ((dpk = opendir(path)) == NULL))
return;

log("loading contexed keybinds");

while ((ep = readdir_visible(dp))) {
pathcat(ep->d_name);
printf("bind -m %s -f %s\n", ep->d_name, path);
while ((epk = readdir_visible(dpk))) {
pathcat(epk->d_name);
printf("bind -m %s -f %s\n", epk->d_name, path);
path[base] = '\0';
}

closedir(dp);
closedir(dpk);
}

int main(int argc, const char **argv) {
int main(int argc, const char *const *argv) {
static size_t path_base;

static unsigned char stage_num;
static stage_t stage;

static char path[PATH_MAX + 1];
static DIR *dp;
static struct dirent *ep;

static const stage_t stages[] = {
{load_envs, DP(ENVS_DIR)}, {load_cmds, DP(CMDS_DIR)},
{load_functions, DP(FUNCS_DIR)}, {load_aliases, DP(ALIASES_DIR)},
Expand Down Expand Up @@ -204,7 +197,8 @@ int main(int argc, const char **argv) {
continue;
}

stage.stage(path, strlen(path), dp, ep);
base = strlen(path);
stage.stage();

path[path_base] = '\0';
closedir(dp);
Expand All @@ -215,9 +209,10 @@ int main(int argc, const char **argv) {
* the keybinds dir to be open */

pathcat(DP(KEYS_DIR));
base = strlen(path);

if (path_exists(path))
load_keybinds(path, strlen(path));
load_keybinds();
}

puts("export PATH"); /* finish off commands, export path */
Expand Down
1 change: 1 addition & 0 deletions loader/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
#else
#define pathcat(what) strncat(path, what, PATH_MAX)
#endif /* BSD */
#define LOAD_FN(what) static void load_##what(void)
#endif /* _MACROS_H */
13 changes: 6 additions & 7 deletions loader/path.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#ifndef _PATH_H
#define _PATH_H
#ifndef PATH_NO_DIRENT
#include <dirent.h>
#endif
#include <unistd.h>

static unsigned char path_exists(const char *);
static struct dirent *readdir_visible(DIR *);
static unsigned char path_exists(const char *const);
static struct dirent *readdir_visible(DIR *const);

#ifdef PATH_IMPL
static unsigned char path_exists(const char *path) {
static unsigned char path_exists(const char *const path) {
return access(path, F_OK) == 0;
}

static struct dirent *readdir_visible(DIR *dp) {
struct dirent *ep;
static struct dirent *readdir_visible(DIR *const dp) {
static struct dirent *ep;

while ((ep = readdir(dp)) && *ep->d_name == '.')
;
Expand Down
20 changes: 13 additions & 7 deletions loader/shell.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#ifndef _SHELL_H
#define _SHELL_H
#ifndef SHELL_NO_FILE
#include "file.h"
#endif

static void escape_quotes(File *);
static void escape_quotes(File *const);

#ifdef SHELL_IMPL
static void escape_quotes(File *file) {
size_t quotes = 0;
char *new_content, *old_ptr, *new_ptr;
static void escape_quotes(File *const file) {
static size_t quotes;
static char *new_content, *old_ptr, *new_ptr;

quotes = 0;

old_ptr = file->content;
new_content = file->content;

while (*new_content)
quotes += (*new_content++ == '"');

if (!quotes) {
file->content[file->content_size -
(file->content[file->content_size - 1] == '\n')] = '\0';
return;
}

new_content = malloc((file->content_size += quotes));
new_ptr = new_content;

Expand All @@ -29,7 +35,7 @@ static void escape_quotes(File *file) {

*(new_ptr - (*(new_ptr - 1) == '\n')) = '\0';

free(file->content);
free_file(file);
file->content = new_content;
}
#endif /* SHELL_IMPL */
Expand Down

0 comments on commit f652daf

Please sign in to comment.