aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-01-28 22:13:51 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-01-28 22:13:51 -0800
commit2aa5f544a7a03c1ad59c70ed149911aa3ece0898 (patch)
tree0273c184d85863da1b17bebd286a8b7002ec4ffb
parent657330ddd6d7a67e16c198b4e9ad2aac239d4799 (diff)
Reorganized header files
-rw-r--r--Makefile2
-rw-r--r--bb.c12
-rw-r--r--draw.c5
-rw-r--r--draw.h13
-rw-r--r--entry.h38
-rw-r--r--types.h82
-rw-r--r--utils.h (renamed from bb.h)84
7 files changed, 122 insertions, 114 deletions
diff --git a/Makefile b/Makefile
index ff07c87..8370baf 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@ all: $(NAME)
clean:
rm -f $(NAME) $(OBJFILES)
-.c.o:
+%.o: %.c %.h types.h utils.h
$(CC) -c $(CFLAGS) $(CWARN) $(G) $(O) -o $@ $<
$(NAME): $(OBJFILES) bb.c
diff --git a/bb.c b/bb.c
index 76bf659..960fbe7 100644
--- a/bb.c
+++ b/bb.c
@@ -6,6 +6,7 @@
* This file contains the main source code of `bb`.
*/
+#include <ctype.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
@@ -20,14 +21,19 @@
#include <termios.h>
#include <unistd.h>
-#include "bb.h"
#include "draw.h"
#include "terminal.h"
+#include "types.h"
+#include "utils.h"
#ifndef BB_NAME
#define BB_NAME "bb"
#endif
+#define BB_VERSION "0.27.0"
+#define MAX_BINDINGS 1024
+#define SCROLLOFF MIN(5, (winsize.ws_row-4)/2)
+
// Functions
void bb_browse(bb_t *bb, const char *initial_path);
static void check_cmdfile(bb_t *bb);
@@ -208,7 +214,7 @@ static int compare_files(const void *v1, const void *v2, void *v)
*/
const char *n1 = e1->name, *n2 = e2->name;
while (*n1 && *n2) {
- char c1 = LOWERCASE(*n1), c2 = LOWERCASE(*n2);
+ char c1 = tolower(*n1), c2 = tolower(*n2);
if ('0' <= c1 && c1 <= '9' && '0' <= c2 && c2 <= '9') {
long i1 = strtol(n1, (char**)&n1, 10);
long i2 = strtol(n2, (char**)&n2, 10);
@@ -224,7 +230,7 @@ static int compare_files(const void *v1, const void *v2, void *v)
++n1; ++n2;
}
}
- COMPARE(LOWERCASE(*n2), LOWERCASE(*n1));
+ COMPARE(tolower(*n2), tolower(*n1));
break;
}
case COL_PERM: COMPARE((e1->info.st_mode & 0x3FF), (e2->info.st_mode & 0x3FF)); break;
diff --git a/draw.c b/draw.c
index 7a762c2..e3d99bd 100644
--- a/draw.c
+++ b/draw.c
@@ -11,10 +11,9 @@
#include <time.h>
#include "draw.h"
-#include "entry.h"
#include "terminal.h"
-
-#define E_ISDIR(e) (S_ISDIR(S_ISLNK((e)->info.st_mode) ? (e)->linkedmode : (e)->info.st_mode))
+#include "types.h"
+#include "utils.h"
column_t column_info[255] = {
['*'] = {.name = "*", .render = col_selected},
diff --git a/draw.h b/draw.h
index 8ed1de0..b72dee0 100644
--- a/draw.h
+++ b/draw.h
@@ -7,8 +7,17 @@
#include <stdio.h>
-#include "bb.h"
-#include "entry.h"
+#include "types.h"
+
+// Colors (using ANSI escape sequences):
+#define TITLE_COLOR "\033[37;1m"
+#define NORMAL_COLOR "\033[37m"
+#define CURSOR_COLOR "\033[43;30;1m"
+#define LINK_COLOR "\033[35m"
+#define DIR_COLOR "\033[34m"
+#define EXECUTABLE_COLOR "\033[31m"
+#define SCROLLBAR_FG "\033[48;5;247m "
+#define SCROLLBAR_BG "\033[48;5;239m "
#define TIME_FMT " %T %D "
#define SELECTED_INDICATOR " \033[31;7m \033[0m"
diff --git a/entry.h b/entry.h
deleted file mode 100644
index 0574341..0000000
--- a/entry.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * entry.h - Define types for file entries.
- */
-#ifndef FILE_ENTRY__H
-#define FILE_ENTRY__H
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#define IS_SELECTED(p) (((p)->selected.atme) != NULL)
-#define IS_VIEWED(p) ((p)->index >= 0)
-#define IS_LOADED(p) ((p)->hash.atme != NULL)
-
-/* entry_t uses intrusive linked lists. This means entries can only belong to
- * one list at a time, in this case the list of selected entries. 'atme' is an
- * indirect pointer to either the 'next' field of the previous list member, or
- * the variable that points to the first list member. In other words,
- * item->next->atme == &item->next and firstitem->atme == &firstitem.
- */
-typedef struct entry_s {
- struct {
- struct entry_s *next, **atme;
- } selected, hash;
- char *name, *linkname;
- struct stat info;
- mode_t linkedmode;
- int no_esc : 1;
- int link_no_esc : 1;
- int shufflepos;
- int index;
- char fullname[1];
- // ------- fullname must be last! --------------
- // When entries are allocated, extra space on the end is reserved to fill
- // in fullname.
-} entry_t;
-
-#endif
diff --git a/types.h b/types.h
new file mode 100644
index 0000000..1c38ea5
--- /dev/null
+++ b/types.h
@@ -0,0 +1,82 @@
+/*
+ * types.h
+ * Copyright 2019 Bruce Hill
+ * Released under the MIT license
+ *
+ * This file contains definitions of different types.
+ */
+#ifndef FILE_TYPES__H
+#define FILE_TYPES__H
+
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "terminal.h"
+
+#define MAX_COLS 12
+#define MAX_SORT (2*MAX_COLS)
+#define HASH_SIZE 1024
+#define HASH_MASK (HASH_SIZE - 1)
+
+/* Datastructure for file/directory entries.
+ * entry_t uses intrusive linked lists. This means entries can only belong to
+ * one list at a time, in this case the list of selected entries. 'atme' is an
+ * indirect pointer to either the 'next' field of the previous list member, or
+ * the variable that points to the first list member. In other words,
+ * item->next->atme == &item->next and firstitem->atme == &firstitem.
+ */
+typedef struct entry_s {
+ struct {
+ struct entry_s *next, **atme;
+ } selected, hash;
+ char *name, *linkname;
+ struct stat info;
+ mode_t linkedmode;
+ int no_esc : 1;
+ int link_no_esc : 1;
+ int shufflepos;
+ int index;
+ char fullname[1];
+ // ------- fullname must be last! --------------
+ // When entries are allocated, extra space on the end is reserved to fill
+ // in fullname.
+} entry_t;
+
+// For keeping track of child processes:
+typedef struct proc_s {
+ pid_t pid;
+ struct {
+ struct proc_s *next, **atme;
+ } running;
+} proc_t;
+
+// Structure for bb program state:
+typedef struct bb_s {
+ entry_t *hash[HASH_SIZE];
+ entry_t **files;
+ entry_t *selected;
+ char path[PATH_MAX];
+ char prev_path[PATH_MAX];
+ int nfiles, nselected;
+ int scroll, cursor;
+
+ char *globpats;
+ char sort[MAX_SORT+1];
+ char columns[MAX_COLS+1];
+ unsigned int interleave_dirs : 1;
+ unsigned int should_quit : 1;
+ unsigned int dirty : 1;
+ proc_t *running_procs;
+} bb_t;
+
+// Key bindings:
+typedef struct {
+ int key;
+ char *script;
+ char *description;
+} binding_t;
+
+#endif
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
diff --git a/bb.h b/utils.h
index 2967273..82018b3 100644
--- a/bb.h
+++ b/utils.h
@@ -1,49 +1,22 @@
/*
- * Bitty Browser (bb)
- * Copyright 2019 Bruce Hill
+ * utils.h
+ * Copyright 2020 Bruce Hill
* Released under the MIT license
*
- * This file contains definitions and customization for `bb`.
+ * This file contains some definitions of some utility macros.
*/
-#ifndef FILE_BB__H
-#define FILE_BB__H
-#include <limits.h>
+#ifndef FILE_UTILS__H
+#define FILE_UTILS__H
-#include "terminal.h"
-#include "entry.h"
-
-// Macros:
-#define BB_VERSION "0.27.0"
-
-#ifndef PATH_MAX
-#define PATH_MAX 4096
-#endif
-
-#define MAX_COLS 12
-#define MAX_SORT (2*MAX_COLS)
-#define HASH_SIZE 1024
-#define HASH_MASK (HASH_SIZE - 1)
-#define MAX_BINDINGS 1024
-
-// Configurable options:
-#define SCROLLOFF MIN(5, (winsize.ws_row-4)/2)
-// Colors (using ANSI escape sequences):
-#define TITLE_COLOR "\033[37;1m"
-#define NORMAL_COLOR "\033[37m"
-#define CURSOR_COLOR "\033[43;30;1m"
-#define LINK_COLOR "\033[35m"
-#define DIR_COLOR "\033[34m"
-#define EXECUTABLE_COLOR "\033[31m"
-#define SCROLLBAR_FG "\033[48;5;247m "
-#define SCROLLBAR_BG "\033[48;5;239m "
+#include <stdio.h>
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
-#define LOWERCASE(c) ('A' <= (c) && (c) <= 'Z' ? ((c) + 'a' - 'A') : (c))
-#define E_ISDIR(e) (S_ISDIR(S_ISLNK((e)->info.st_mode) ? (e)->linkedmode : (e)->info.st_mode))
+
#define ONSCREEN (winsize.ws_row - 3)
+// Platform-dependent time strucutre accessors:
#ifdef __APPLE__
#define mtime(s) (s).st_mtimespec
#define atime(s) (s).st_atimespec
@@ -54,6 +27,7 @@
#define ctime(s) (s).st_ctim
#endif
+// Error reporting macros:
#define err(...) do { \
cleanup(); \
fprintf(stderr, __VA_ARGS__); \
@@ -72,6 +46,14 @@
bb->dirty = 1; \
} while (0)
+// Entry macros
+#define IS_SELECTED(e) (((e)->selected.atme) != NULL)
+#define IS_VIEWED(e) ((e)->index >= 0)
+#define IS_LOADED(e) ((e)->hash.atme != NULL)
+
+#define E_ISDIR(e) (S_ISDIR(S_ISLNK((e)->info.st_mode) ? (e)->linkedmode : (e)->info.st_mode))
+
+// Linked list macros
#define LL_PREPEND(head, node, name) do { \
((node)->name).atme = &(head); \
((node)->name).next = head; \
@@ -88,38 +70,6 @@
((node)->name).next = NULL; \
} while (0)
-// Types:
-typedef struct {
- int key;
- char *script;
- char *description;
-} binding_t;
-
-// For keeping track of child processes
-typedef struct proc_s {
- pid_t pid;
- struct {
- struct proc_s *next, **atme;
- } running;
-} proc_t;
-
-typedef struct bb_s {
- entry_t *hash[HASH_SIZE];
- entry_t **files;
- entry_t *selected;
- char path[PATH_MAX];
- char prev_path[PATH_MAX];
- int nfiles, nselected;
- int scroll, cursor;
-
- char *globpats;
- char sort[MAX_SORT+1];
- char columns[MAX_COLS+1];
- unsigned int interleave_dirs : 1;
- unsigned int should_quit : 1;
- unsigned int dirty : 1;
- proc_t *running_procs;
-} bb_t;
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1