rename cli/ -> cursetree/

This commit is contained in:
Emile Clark-Boman 2025-09-13 10:39:58 +10:00
parent 2ad6e4e104
commit 878be07a71
12 changed files with 37 additions and 18 deletions

177
cursetree/_pty.c Normal file
View file

@ -0,0 +1,177 @@
/* WARNING: The mkpty(), forkmkpty(), & bindpty() functions are based on
* WARNING: the glibc openpty(), forkpty(), & login_tty() functions respectively.
* WARNING:
* WARNING: The GNU C Library's COPYING & COPYING.LIB licenses are available at
* WARNING: LICENSES/GLIBC-COPYING & LICENSES/GLIBC-COPYING.LIB in this repo.
* WARNING:
* WARNING: login_tty() maintains the original University of California license
* WARNING: available at LICENSES/UC-LICENSE in this repo.
*/
/* _XOPEN_SOURCE unlocks <stdlib.h> pty/ptmx/pts declarations. */
#define _XOPEN_SOURCE 600
/* _GNU_SOURCE unlocks the ptsname_r declaration*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdlib.h>
#include <asm/termbits.h> /* TIOC* constants */
#include <sys/ioctl.h>
#include "_pty.h"
/* Allocate PTY master and slave file descriptors.
* errno will have been set if newpty() fails.
*
* NOTE: This function is my alternative to GLibC's
* NOTE: openpty() function. It exists as a learning resource.
* REF: https://sourceware.org/git/glibc.git -> ./login/openpty.c
*/
int mkpty(int *fdmx, int *fds) {
int _fdmx = -1, _fds = -1;
char sname[TTYNAME_MAX];
// Configure PTY master (file descriptor)
_fdmx = posix_openpt(O_RDWR | O_NOCTTY);
if (_fdmx == -1)
return EXIT_FAILURE;
if (grantpt(_fdmx))
goto fail;
if (unlockpt(_fdmx))
goto fail;
#ifdef TIOCGPTPEER
/* Try to allocate slave fd solely based on PTMX fd first. */
_fds = ioctl(_fdmx, TIOCGPTPEER, O_RDWR | O_NOCTTY);
#endif
if (_fds == -1) {
/* Fallback to path-based slave fd allocation
* (if the kernel doesn't support TIOCGPTPEER, ie Linux <4.13) */
if(ptsname_r(_fdmx, sname, sizeof(sname)))
goto fail;
_fds = open(sname, O_RDWR | O_NOCTTY);
if (_fds == -1)
goto fail;
}
/* Propagate file descriptors via parameters */
*fdmx = _fdmx;
*fds = _fds;
return EXIT_SUCCESS;
fail:
if (_fdmx == -1) {
close(_fdmx);
if (_fds == -1)
close(_fds);
}
return EXIT_FAILURE;
}
/* Set fdty as the controlling terminal for the calling process.
* Returns 0 on success, and 1 on failure.
* NOTE: This function is my alternative to GLibC's
* NOTE: login_tty() function. It exists as a learning resource.
* REF: https://sourceware.org/git/glibc.git -> ./login/login_tty.c
*/
static inline int setctty(const int fdty) {
/* We assume any kernel compiling this defines TIOCSCTTY,
* otherwise this implementation won't exactly work... */
return (ioctl(fdty, TIOCSCTTY, 0) != -1);
}
/* Bind fdty (terminal fd) to stdin/stdout/stderr for the calling process.
* This functions blocks until the EBUSY (see `man dup2`) race condition lifts.
* NOTE: This function is my alternative to GLibC's
* NOTE: login_tty() function. It exists as a learning resource.
* WARNING: This function maintains the original University of California
* WARNING: LICENSE (1990-1993) as per glibc.git:/login/login_tty.c
* WARNING: available at LICENSES/UC-LICENSE in this repo.
* REF: https://sourceware.org/git/glibc.git -> ./login/login_tty.c
*/
void bindpty(const int fdty) {
/* Adjust stdin/stdout/stderr to refer to fd*/
BIND(fdty, STDIN_FILENO);
BIND(fdty, STDOUT_FILENO);
BIND(fdty, STDERR_FILENO);
if (fdty > 2)
close(fdty);
}
/* Allocate a PTY and fork, giving ptmx (master) to the parent
* and binding the child's stdin/stdout/stderr to pts (slave).
* Return value is indentical to fork(2).
* NOTE: This function is my alternative to GLibC's
* NOTE: forkpty() function. It exists as a learning resource.
* REF: https://sourceware.org/git/glibc.git -> ./login/forkpty.c
*/
pid_t forkmkpty(int *fdmx) {
int _fdmx, fds;
pid_t pid;
if (mkpty(&_fdmx, &fds))
return EXIT_FAILURE;
switch (pid = fork()) {
case -1:
close(_fdmx);
close(fds);
return -1;
case 0:
/* Child Process */
close(_fdmx);
setctty(fds);
bindpty(fds);
break;
default:
/* Parent Process */
close(fds);
// propagate ptmx (master) fd
*fdmx = _fdmx;
break;
}
/* Both Processes */
return pid;
}
/* Set pseudoterminal slave's window size.
* Returns 0 on success, and fails with -1 if the kernel doesn't
* implement this, or 1 for general errors (errno will be set).
* NOTE: Typically this is part of a glibc openpty() call.
*/
int setptsxy(const unsigned short rows, const unsigned short cols, const int fds) {
#ifndef TIOCSWINSZ
/* Fail if kernel doesn't support TIOCSWINSZ. */
return -1;
#else
struct winsize win = {
.ws_row = rows,
.ws_col = cols,
};
if (ioctl(fds, TIOCSWINSZ, &win) == -1)
return EXIT_FAILURE;
return EXIT_SUCCESS;
#endif /* TIOCSWINSZ */
}
/* Get pseudoterminal slave's window size.
* Returns 0 on success, and fails with -1 if the kernel doesn't
* implement this, or 1 for general errors (errno will be set).
*/
int getptsxy(unsigned short *rows, unsigned short *cols, const int fds) {
#ifndef TIOCGWINSZ
/* Fail if kernel doesn't support TIOCGWINSZ. */
return -1;
#else
struct winsize win = (struct winsize){ 0 };
if (ioctl(fds, TIOCGWINSZ, &win) == -1)
return EXIT_FAILURE;
*rows = win.ws_row;
*cols = win.ws_col;
return EXIT_SUCCESS;
#endif /* TIOCGWINSZ */
}

30
cursetree/_pty.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef DORNE_MKPTY_H
#define DORNE_MKPTY_H
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef PATH_MAX
#define TTYNAME_MAX PATH_MAX
#else
#define TTYNAME_MAX 512
#endif /* PATH_MAX */
#define BIND(fdsrc, fddst) \
while (dup2(fdsrc, fddst) == -1 && errno == EBUSY) \
;
/* Custom implementation of glibc::openpty() */
int mkpty(int *fdmx, int *fds);
/* Custom implementation of glibc::login_tty() */
void bindpty(const int fdty);
/* Custom implementation of glibc::forkpty() */
pid_t forkmkpty(int *fdmx);
int setptsxy(const unsigned short rows, const unsigned short cols,
const int fds);
int getptsxy(unsigned short *rows, unsigned short *cols, const int fds);
#endif /* DORNE_MKPTY_H */

63
cursetree/child.c Normal file
View file

@ -0,0 +1,63 @@
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include "child.h"
#include "epty.h"
/* SIGTERM grace period before a child is sent SIGKILL.
* Value is in microseconds (us), 1000us = 1ms */
#define CHILD_GRACE_US 100000
/* Check if a child is still alive by pid
* by kill(3p) sending a null signal (0).
*/
static int is_alive(struct d_child *child) {
return (kill(child->pid, 0) == 0);
}
/* Force a child to die, first by sending SIGTERM, otherwise SIGKILL.
* NOTE: killchild() ASSUMES the child is LIVING at the start.
* NOTE: Keep this in mind when threading!
*/
void killchild(struct d_child *child) {
int stat;
/* Request Death */
kill(child->pid, SIGTERM);
usleep(CHILD_GRACE_US);
/* Force Death */
if (is_alive(child))
kill(child->pid, SIGKILL);
/* Reap */
waitpid(child->pid, &stat, 0);
}
/* Fork to spawn a child process running bin/shfx in an "epty".
*/
int spawnchild(struct d_child *child) {
*child = (struct d_child){ 0 };
/* fork(2) and allocate an "error-piped pseudoterminal" (epty) */
switch (child->pid = forkepty(&child->fdmx, &child->fderr)) {
case -1:
perror("forkepty");
return EXIT_FAILURE;
case 0:
char *args[] = {"shfx", NULL};
execvp("shfx", args);
perror("execvp");
exit(1);
default:
usleep(1000);
char bufout[10];
bufout[9] = '\0';
read(child->fdmx, bufout, 9);
printf("child stdout: \"%s\"\n", bufout);
break;
}
return EXIT_SUCCESS;
}

14
cursetree/child.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef DORNE_CHILD_H
#define DORNE_CHILD_H
#include <unistd.h>
struct d_child {
pid_t pid;
int fdmx; /* PTY master fd (read/write) */
int fderr; /* child's stderr (readonly) */
} __attribute__((packed, aligned(4)));
int spawnchild(struct d_child *child);
#endif /* DORNE_CHILD_H */

84
cursetree/curse.c Normal file
View file

@ -0,0 +1,84 @@
#include <locale.h>
#include <unistd.h>
#include "curse.h"
#define BASE_DELAY 1000000
#define CRS_LOCALE "en_US.UTF-8"
/* Set ncurses terminal mode (buffering, character processing,
* Key->SIG handling, and other termios(3) functionality).
*/
int termmode(const enum crs_termmode mode) {
switch (mode) {
case TMODE_CBREAK:
return cbreak();
case TMODE_RAW:
return raw();
case TMODE_NOCBREAK:
return nocbreak();
case TMODE_NORAW:
return noraw();
default:
/* defaulting is not possible. */
return 1;
}
}
/* Apply a default IO configuration for an ncurses WINDOW.
*/
static inline void __conf_window(WINDOW *const win) {
nodelay(win, TRUE); // getch(3x) nonblocking IO
keypad(win, TRUE); // allow function keys
// intrflush(3x) - flush terminal input buffer on interrupt key
intrflush(win, FALSE);
}
/* Initialise ncurses for our usecase.
* WARNING: This function should only be called once.
*/
void init_ncurses(void) {
// ncurses expects a locale for consistent behaviour
setlocale(LC_ALL, CRS_LOCALE);
/* NCurses Init */
initscr();
/* WARNING: no you shouldn't delwin(stdscr) it breaks everything... */
__conf_window(stdscr);
start_color();
/* Screen Configuration */
termmode(TMODE_CBREAK);
noecho(); // manually echo from getch(3x)
curs_set(0); // hide cursor
}
/* Initialise (with default IO configuration) a new ncurses WINDOW.
*/
WINDOW *new_window(const int x, const int y, const int width,
const int height) {
WINDOW *win = newwin(height, width, y, x);
__conf_window(win);
return win;
}
/* Initialise (with default IO configuration) the root ncurses WINDOW.
* NOTE: This is typically done via initscr(3x) and called "stdscr".
*/
WINDOW *root_window(void) {
WINDOW *rootwin = new_window(0, 0, COLS, LINES);
__conf_window(rootwin);
return rootwin;
}
/* Resize and move (if resized successfully) an ncurses WINDOW.
* Returns ERR (1) on fail, and OK (0) on success.
* NOTE: Failure occurs if width or height <= 0,
* NOTE: or if the x,y coordinate is outside the screen bounds.
*/
int resizemv_window(const int x, const int y, const int width, const int height,
WINDOW *const win) {
return wresize(win, height, width) || mvwin(win, y, x);
}

29
cursetree/curse.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef DORNE_CURSE_H
#define DORNE_CURSE_H
/* libncurses with wide-character support. */
#include <ncursesw/ncurses.h>
enum crs_termmode {
/* tty cbreak mode */
TMODE_CBREAK,
/* tty raw mode */
TMODE_RAW,
/* tty cooked mode (ISIG & IXON not modified)*/
TMODE_NOCBREAK,
/* tty cooked mode (ISIG & IXON set) */
TMODE_NORAW,
};
#define termsize(width, height) (width=COLS); (height=LINES)
int termmode(const enum crs_termmode mode);
void init_ncurses(void);
WINDOW *new_window(const int x, const int y, const int width, const int height);
WINDOW *root_window(void);
int resizemv_window(const int x, const int y, const int width, const int height,
WINDOW *const win);
#endif /* DORNE_CURSE_H */

292
cursetree/cursetree.c Normal file
View file

@ -0,0 +1,292 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "curse.h"
#include "ncurses.h"
#include "cursetree.h"
/* Internal allocator method for crs_node structures.
*/
static inline struct crs_node *__alloc_node(const enum crs_nodetype type) {
struct crs_node *node = (struct crs_node *)malloc(sizeof(struct crs_node));
node->type = type;
return node;
}
/* Construct a new window node (crs_node of type NODE_WIN).
*/
static struct crs_node *init_window_node(WINDOW *const win) {
struct crs_node *node = __alloc_node(NODE_WINDOW);
node->win = win;
return node;
}
static struct crs_node *
auto_window_node(const struct crs_nodedims *const dims) {
WINDOW *win = newwin(dims->height, dims->width, dims->y, dims->x);
return init_window_node(win);
}
static struct crs_node *init_abstract_node(struct crs_node *const node0,
struct crs_node *const node1,
const enum crs_axis axis,
const float ratio,
struct crs_nodedims *const dims) {
struct crs_node *node = __alloc_node(NODE_ABSTRACT);
node->axis = axis;
node->ratio = ratio;
node->dims = dims;
node->child[0] = node0;
node->child[1] = node1;
return node;
}
static void destroy_node(struct crs_node *const node) {
if (node->type == NODE_WINDOW) {
/* Window Node */
delwin(node->win);
goto end;
}
/* Abstract Node */
assert(node->type == NODE_ABSTRACT);
destroy_node(node->child[0]);
destroy_node(node->child[1]);
free(node->dims);
end:
free(node);
}
static inline struct crs_nodedims *__alloc_dims(int x, int y, int width,
int height) {
struct crs_nodedims *dims;
dims = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
*dims = (struct crs_nodedims){
.x = x,
.y = y,
.width = width,
.height = height,
};
return dims;
}
static inline struct crs_nodedims *
__dup_dims(const struct crs_nodedims *const dims) {
struct crs_nodedims *dup;
dup = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
memcpy(dup, dims, sizeof(struct crs_nodedims));
return dup;
}
static inline struct crs_nodedims *termdims(void) {
struct crs_nodedims *dims = __alloc_dims(0, 0, 0, 0);
termsize(dims->width, dims->height);
return dims;
}
static inline void nodesize(const struct crs_node *const node, int *const width,
int *const height) {
if (node->type == NODE_WINDOW) {
/* Window Node */
getmaxyx(node->win, *height, *width);
} else {
/* Abstract Node */
assert(node->type == NODE_ABSTRACT);
*width = node->dims->width;
*height = node->dims->height;
}
}
static inline struct crs_nodedims *nodedims(const struct crs_node *const node) {
struct crs_nodedims *dims;
if (node->type == NODE_WINDOW) {
/* Window Node */
dims = (struct crs_nodedims *)malloc(sizeof(struct crs_nodedims));
getbegyx(node->win, dims->y, dims->x);
getmaxyx(node->win, dims->height, dims->width);
} else {
/* Abstract Node */
assert(node->type == NODE_ABSTRACT);
dims = __dup_dims(node->dims);
}
return dims;
}
/* Calculate the dimensions for nodes resulting from a bifurcation.
* Returns 0 on success, and 1 on failure if any width/height are 0 characters.
* WARNING: This function does not guarantee the x,y positions returned
* WARNING: are valid screen coordinates.
*/
static int bifurcate_dims(const struct crs_nodedims *const parent_dims,
const enum crs_axis axis, const float ratio,
struct crs_nodedims **const dims0,
struct crs_nodedims **const dims1) {
assert(0 < ratio && ratio < 1);
struct crs_nodedims *_dims0, *_dims1;
_dims0 = __dup_dims(parent_dims);
_dims1 = __dup_dims(parent_dims);
if (axis == AXIS_X) {
_dims0->width *= ratio;
_dims1->width -= _dims0->width;
_dims1->x += _dims0->width;
} else {
_dims0->height *= ratio;
_dims1->height -= _dims0->height;
_dims1->y += _dims0->height;
}
if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height)
return 1;
// propagate bifurcated dimensions
*dims0 = _dims0;
*dims1 = _dims1;
return 0;
}
/* NOTE: resize_node calls wnoutrefresh(3x), which expects
* NOTE: a call doupdate(3x) call afterwards to flush ncurses
* NOTE: virtual screen to the physical screen.
*/
static void resize_node(struct crs_node *const node,
struct crs_nodedims *const new_dims) {
if (node->type == NODE_WINDOW) {
/* Window Node */
resizemv_window(new_dims->x, new_dims->y, new_dims->width, new_dims->height,
node->win);
free(new_dims);
wnoutrefresh(node->win);
} else {
/* Abstract Node */
assert(node->type == NODE_ABSTRACT);
struct crs_nodedims *dims0, *dims1;
free(node->dims);
node->dims = new_dims;
bifurcate_dims(new_dims, node->axis, node->ratio, &dims0, &dims1);
resize_node(node->child[0], dims0);
resize_node(node->child[1], dims1);
}
}
/* Subdivide a window node's allocated region into two window nodes
* replacing the original node with an abstract node.
* Parameters:
* axis - controls which direction the subdivision occurs
* invert_axis - invert index of the original node in the new abstract node
*/
void bifurcate_window_node(struct crs_node **const node,
const enum crs_axis axis, const int invert_axis,
const float ratio) {
assert((*node)->type == NODE_WINDOW);
struct crs_nodedims *dims0, *dims1;
struct crs_node *node0, *node1;
struct crs_nodedims *original_dims = nodedims(*node);
if (bifurcate_dims(original_dims, axis, ratio, &dims0, &dims1)) {
/* TODO: handle this error properly */
free(original_dims);
exit(1);
return;
}
if (invert_axis) {
/* Inverted Bifurcation */
node0 = auto_window_node(dims0);
node1 = *node;
resize_node(node1, dims1);
} else {
/* Non-Inverted Bifurcation */
node0 = *node;
node1 = auto_window_node(dims1);
resize_node(node0, dims0);
}
*node = init_abstract_node(node0, node1, axis, ratio, original_dims);
}
/* Collapse an abstract node, killing one child node and resizing
* the other to take its place.
*/
static void collapse_abstract_node(struct crs_node **const node,
const int collapse_i) {
assert((*node)->type == NODE_ABSTRACT);
assert(0 <= collapse_i && collapse_i < NODE_CHILD_N);
// WARNING: only works for NODE_CHILD_N=2 (binary trees)
destroy_node((*node)->child[!collapse_i]);
struct crs_node *collapse_target = (*node)->child[collapse_i];
free(*node);
*node = collapse_target;
}
/*
*/
static struct crs_node *init_root_node(void) {
WINDOW *rootwin;
rootwin = root_window();
return init_window_node(rootwin);
}
int ct_init(struct crs_tree **const tree) {
*tree = (struct crs_tree *)malloc(sizeof(struct crs_tree));
/* Initialise NCurses Library & Root Node */
init_ncurses();
(*tree)->root = init_root_node();
return EXIT_SUCCESS;
}
void destroy_tree(struct crs_tree *const tree) {
destroy_node(tree->root);
endwin();
free(tree);
}
void resize_tree(struct crs_tree *const tree, struct crs_nodedims *const dims) {
resize_node(tree->root, dims);
}
void ct_update(struct crs_tree *const tree) {
struct crs_nodedims * term_dims;
int term_width, term_height;
const int key = wgetch(curscr);
/* ncurses binds a SIGWINCH handler if SIGWINCH has SIG_DFL disposition
* when initscr(3x) is called. This handler emits KEY_RESIZE (decimal 410) to stdin.
* REF: manpages -> resizeterm(3x) initscr(3x) wgetch(3x)
*/
switch (key) {
case -1:
wclear(tree->root->child[0]->win);
mvwprintw(tree->root->child[0]->win, 0, 0, " \r-1\n");
wrefresh(tree->root->child[0]->win);
return;
case KEY_RESIZE:
termsize(term_width, term_height);
term_dims = __alloc_dims(0, 0, term_width, term_height);
resize_tree(tree, term_dims);
// flush ncurses virtual screen -> physical screen
doupdate();
break;
default:
wclear(tree->root->child[0]->win);
mvwprintw(tree->root->child[0]->win, 0, 0, " \r%d\n", key);
wrefresh(tree->root->child[0]->win);
break;
}
}

63
cursetree/cursetree.h Normal file
View file

@ -0,0 +1,63 @@
#ifndef DORNE_TREE_H
#define DORNE_TREE_H
#ifndef __NCURSES_H
typedef struct _win_st WINDOW;
#endif /* __NCURSES_H */
#define NODE_CHILD_N 2
/* MACRO:
* Get widnow node start x,y coordinates, width, & height.
* void NODEDIMS(struct crs_node *node, struct crs_nodedims dims);
*/
#define GET_WNODEDIMS(dims, node) \
(getbegyx((node)->win, dims.y, dims.x)); \
getmaxyx((node)->win, dims.y, dims.x)
enum crs_nodetype {
NODE_WINDOW,
NODE_ABSTRACT,
};
/* Stores a node's starting x,y coordinates, width, & height.
* NOTE: Intended for interfunction communication.
*/
struct crs_nodedims {
int x, y, width, height;
};
enum crs_axis {
AXIS_X,
AXIS_Y,
};
struct crs_node {
enum crs_nodetype type;
// union value depends on crs_node.type
union {
WINDOW *win;
struct {
enum crs_axis axis;
float ratio;
struct crs_nodedims *dims;
struct crs_node *child[NODE_CHILD_N];
};
};
};
struct crs_tree {
struct crs_node *root;
};
/* === External Interface === */
int ct_init(struct crs_tree **const tree);
void destroy_tree(struct crs_tree *const tree);
void resize_tree(struct crs_tree *const tree, struct crs_nodedims *const dims);
void ct_update(struct crs_tree *const tree);
void bifurcate_window_node(struct crs_node **const node,
const enum crs_axis axis, const int invert_axis,
const float ratio);
#endif /* DORNE_TREE_H */

45
cursetree/epty.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdlib.h>
#include "_pty.h"
#define PIPE_READ 0
#define PIPE_WRITE 1
/* Allocate a PTY and fork, giving fdmx (master) to the parent
* and binding the child's stdin/stdout to fds (slave).
* Return value is indentical to fork(2).
* NOTE: This function is my alternative to GLibC's
* NOTE: forkpty() function. It exists as a learning resource.
* REF: https://sourceware.org/git/glibc.git -> ./login/forkpty.c
*/
pid_t forkepty(int *fdmx, int *fderr) {
// master/slave, and stderr pipe fds
int epipe[2];
pid_t pid;
if (pipe(epipe) == -1)
return EXIT_FAILURE;
switch (pid = forkmkpty(fdmx)) {
case -1:
/* forkmkpty() will close fdmx/fds for us */
close(epipe[PIPE_READ]);
close(epipe[PIPE_WRITE]);
return -1;
case 0:
/* Child Process */
/* forkmkpty() will close fdmx for us */
close(epipe[PIPE_READ]);
BIND(epipe[PIPE_WRITE], STDERR_FILENO);
break;
default:
/* Parent Process */
/* forkmkpty() will close fds for us */
close(epipe[PIPE_WRITE]);
*fderr = epipe[PIPE_READ];
break;
}
/* Both Processes */
return pid;
}

8
cursetree/epty.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef DORNE_EPTY_H
#define DORNE_EPTY_H
#include <sys/types.h>
pid_t forkepty(int *fdmx, int *fderr);
#endif /* DORNE_EPTY_H */