Reorganized header files
This commit is contained in:
parent
657330ddd6
commit
2aa5f544a7
2
Makefile
2
Makefile
@ -16,7 +16,7 @@ all: $(NAME)
|
|||||||
clean:
|
clean:
|
||||||
rm -f $(NAME) $(OBJFILES)
|
rm -f $(NAME) $(OBJFILES)
|
||||||
|
|
||||||
.c.o:
|
%.o: %.c %.h types.h utils.h
|
||||||
$(CC) -c $(CFLAGS) $(CWARN) $(G) $(O) -o $@ $<
|
$(CC) -c $(CFLAGS) $(CWARN) $(G) $(O) -o $@ $<
|
||||||
|
|
||||||
$(NAME): $(OBJFILES) bb.c
|
$(NAME): $(OBJFILES) bb.c
|
||||||
|
12
bb.c
12
bb.c
@ -6,6 +6,7 @@
|
|||||||
* This file contains the main source code of `bb`.
|
* This file contains the main source code of `bb`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -20,14 +21,19 @@
|
|||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "bb.h"
|
|
||||||
#include "draw.h"
|
#include "draw.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#ifndef BB_NAME
|
#ifndef BB_NAME
|
||||||
#define BB_NAME "bb"
|
#define BB_NAME "bb"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define BB_VERSION "0.27.0"
|
||||||
|
#define MAX_BINDINGS 1024
|
||||||
|
#define SCROLLOFF MIN(5, (winsize.ws_row-4)/2)
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
void bb_browse(bb_t *bb, const char *initial_path);
|
void bb_browse(bb_t *bb, const char *initial_path);
|
||||||
static void check_cmdfile(bb_t *bb);
|
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;
|
const char *n1 = e1->name, *n2 = e2->name;
|
||||||
while (*n1 && *n2) {
|
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') {
|
if ('0' <= c1 && c1 <= '9' && '0' <= c2 && c2 <= '9') {
|
||||||
long i1 = strtol(n1, (char**)&n1, 10);
|
long i1 = strtol(n1, (char**)&n1, 10);
|
||||||
long i2 = strtol(n2, (char**)&n2, 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;
|
++n1; ++n2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
COMPARE(LOWERCASE(*n2), LOWERCASE(*n1));
|
COMPARE(tolower(*n2), tolower(*n1));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case COL_PERM: COMPARE((e1->info.st_mode & 0x3FF), (e2->info.st_mode & 0x3FF)); break;
|
case COL_PERM: COMPARE((e1->info.st_mode & 0x3FF), (e2->info.st_mode & 0x3FF)); break;
|
||||||
|
5
draw.c
5
draw.c
@ -11,10 +11,9 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "draw.h"
|
#include "draw.h"
|
||||||
#include "entry.h"
|
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include "types.h"
|
||||||
#define E_ISDIR(e) (S_ISDIR(S_ISLNK((e)->info.st_mode) ? (e)->linkedmode : (e)->info.st_mode))
|
#include "utils.h"
|
||||||
|
|
||||||
column_t column_info[255] = {
|
column_t column_info[255] = {
|
||||||
['*'] = {.name = "*", .render = col_selected},
|
['*'] = {.name = "*", .render = col_selected},
|
||||||
|
13
draw.h
13
draw.h
@ -7,8 +7,17 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "bb.h"
|
#include "types.h"
|
||||||
#include "entry.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 TIME_FMT " %T %D "
|
||||||
#define SELECTED_INDICATOR " \033[31;7m \033[0m"
|
#define SELECTED_INDICATOR " \033[31;7m \033[0m"
|
||||||
|
38
entry.h
38
entry.h
@ -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
|
|
82
types.h
Normal file
82
types.h
Normal file
@ -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
|
@ -1,49 +1,22 @@
|
|||||||
/*
|
/*
|
||||||
* Bitty Browser (bb)
|
* utils.h
|
||||||
* Copyright 2019 Bruce Hill
|
* Copyright 2020 Bruce Hill
|
||||||
* Released under the MIT license
|
* 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 <stdio.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 "
|
|
||||||
|
|
||||||
#define MAX(a,b) ((a) < (b) ? (b) : (a))
|
#define MAX(a,b) ((a) < (b) ? (b) : (a))
|
||||||
#define MIN(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)
|
#define ONSCREEN (winsize.ws_row - 3)
|
||||||
|
|
||||||
|
// Platform-dependent time strucutre accessors:
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
#define mtime(s) (s).st_mtimespec
|
#define mtime(s) (s).st_mtimespec
|
||||||
#define atime(s) (s).st_atimespec
|
#define atime(s) (s).st_atimespec
|
||||||
@ -54,6 +27,7 @@
|
|||||||
#define ctime(s) (s).st_ctim
|
#define ctime(s) (s).st_ctim
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Error reporting macros:
|
||||||
#define err(...) do { \
|
#define err(...) do { \
|
||||||
cleanup(); \
|
cleanup(); \
|
||||||
fprintf(stderr, __VA_ARGS__); \
|
fprintf(stderr, __VA_ARGS__); \
|
||||||
@ -72,6 +46,14 @@
|
|||||||
bb->dirty = 1; \
|
bb->dirty = 1; \
|
||||||
} while (0)
|
} 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 { \
|
#define LL_PREPEND(head, node, name) do { \
|
||||||
((node)->name).atme = &(head); \
|
((node)->name).atme = &(head); \
|
||||||
((node)->name).next = head; \
|
((node)->name).next = head; \
|
||||||
@ -88,38 +70,6 @@
|
|||||||
((node)->name).next = NULL; \
|
((node)->name).next = NULL; \
|
||||||
} while (0)
|
} 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
|
#endif
|
||||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|
Loading…
Reference in New Issue
Block a user