aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-06-06 13:26:49 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-06-06 13:26:49 -0700
commitb3ea2b804fbd35babac86c83c718a5491542db7c (patch)
tree9d00af565805538213d06bf50cfbd5b499632495
parentc320322dffd8f4648cc24ab731ac64dda6eec77d (diff)
done tinkering with st
-rw-r--r--include/libn.h3
-rw-r--r--sys/cmd/term/term.c152
-rw-r--r--sys/cmd/term/term.h249
-rw-r--r--sys/cmd/term/x.c2
4 files changed, 210 insertions, 196 deletions
diff --git a/include/libn.h b/include/libn.h
index d47e7c5..5829cb1 100644
--- a/include/libn.h
+++ b/include/libn.h
@@ -9,12 +9,15 @@
#include <math.h>
#include <ctype.h>
#include <stdio.h>
+#include <wchar.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
+typedef wchar_t wchar;
+
// ----------------------------------------------------------------------------
// dynamic array
diff --git a/sys/cmd/term/term.c b/sys/cmd/term/term.c
index 81dd937..aecdcb4 100644
--- a/sys/cmd/term/term.c
+++ b/sys/cmd/term/term.c
@@ -26,126 +26,11 @@
#define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
#define ISDELIM(u) (u && wcschr(worddelimiters, u))
-/* terminal mode */
-enum {
- Twrap = iota(0),
- Tinsert = iota(1),
- Taltscreen = iota(2),
- Tcrlf = iota(3),
- Techo = iota(4),
- Tprint = iota(5),
- Tutf8 = iota(6),
- Tsixel = iota(7),
-};
-
-/* cursor state */
-enum {
- CursorSave,
- CursorLoad
-};
-
-/* cursor mode */
-enum {
- CursorDefault = 0,
- CursorWrap = 1,
- CursorOrigin = 2
-};
-
-/* character set */
-enum {
- CSgfx0,
- CSgfx1,
- CSuk,
- CSusa,
- CSmulti,
- CSger,
- CSfin,
-};
-
-/* escape sequences */
-enum {
- Xstart = 1,
- Xcsi = 2,
- Xstr = 4, /* OSC, PM, APC */
- Xaltcs = 8,
- Xstrend = 16, /* a final string was encountered */
- Xtest = 32, /* Enter in test mode */
- Xutf8 = 64,
- Xdcs =128,
-};
-
-typedef struct {
- Letter attr; /* current char attributes */
- int x;
- int y;
- char state;
-} TCursor;
-
-typedef struct {
- int mode;
- int type;
- int snap;
- /*
- * Selection variables:
- * nb – normalized coordinates of the beginning of the selection
- * ne – normalized coordinates of the end of the selection
- * ob – original coordinates of the beginning of the selection
- * oe – original coordinates of the end of the selection
- */
- struct {
- int x, y;
- } nb, ne, ob, oe;
-
- int alt;
-} Selection;
-
-/* Internal representation of the screen */
-typedef struct {
- int row; /* nb row */
- int col; /* nb col */
- Line *line; /* screen */
- Line *alt; /* alternate screen */
- int *dirty; /* dirtyness of lines */
- TCursor c; /* cursor */
- int ocx; /* old cursor col */
- int ocy; /* old cursor row */
- int top; /* top scroll limit */
- int bot; /* bottom scroll limit */
- int mode; /* terminal mode flags */
- int esc; /* escape state flags */
- char trantbl[4]; /* charset table translation */
- int charset; /* current charset */
- int icharset; /* selected charset for sequence */
- int *tabs;
- rune lastc; /* last printed char outside of sequence, 0 if control */
-} Term;
-
-/* CSI Escape sequence structs */
-/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
-typedef struct {
- char buf[ESC_BUF_SIZ]; /* raw string */
- size_t len; /* raw string length */
- char priv;
- int arg[ESC_ARG_SIZ];
- int narg; /* nb of args */
- char mode[2];
-} CSIEscape;
-
-/* STR Escape sequence structs */
-/* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
-typedef struct {
- char type; /* ESC type ... */
- char *buf; /* allocated raw string */
- size_t siz; /* allocation size */
- size_t len; /* raw string length */
- char *args[STR_ARG_SIZ];
- int narg; /* nb of args */
-} STREscape;
-
-static void execsh(char *, char **);
-static void stty(char **);
-static void sigchld(int);
-static void ttywriteraw(char *, size_t);
+/* forward declare functions */
+static void execsh(char *, char **);
+static void stty(char **);
+static void sigchld(int);
+static void ttywriteraw(char *, size_t);
static void csidump(void);
static void csihandle(void);
@@ -201,11 +86,10 @@ static char *base64dec(char *);
static char base64dec_getc(char **);
static uintptr xwrite(int, char *, size_t);
-
-extern int wcwidth(wchar_t wc);
+extern int wcwidth(wchar_t wc);
/* Globals */
-static Term term;
+static Terminal term;
static Selection sel;
static CSIEscape csiescseq;
static STREscape strescseq;
@@ -912,7 +796,7 @@ tfulldirt(void)
void
tcursor(int mode)
{
- static TCursor c[2];
+ static Dot c[2];
int alt = IS_SET(Taltscreen);
if (mode == CursorSave) {
@@ -928,7 +812,7 @@ treset(void)
{
uint i;
- term.c = (TCursor){{
+ term.c = (Dot){{
.mode = Gnil,
.fg = defaultfg,
.bg = defaultbg
@@ -954,7 +838,7 @@ treset(void)
void
tnew(int col, int row)
{
- term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
+ term = (Terminal){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
tresize(col, row);
treset();
}
@@ -962,7 +846,7 @@ tnew(int col, int row)
void
tswapscreen(void)
{
- Line *tmp = term.line;
+ Letter **tmp = term.line;
term.line = term.alt;
term.alt = tmp;
@@ -974,7 +858,7 @@ void
tscrolldown(int orig, int n)
{
int i;
- Line temp;
+ Letter *temp;
LIMIT(n, 0, term.bot-orig+1);
@@ -994,7 +878,7 @@ void
tscrollup(int orig, int n)
{
int i;
- Line temp;
+ Letter *temp;
LIMIT(n, 0, term.bot-orig+1);
@@ -2395,7 +2279,7 @@ tresize(int col, int row)
int minrow = MIN(row, term.row);
int mincol = MIN(col, term.col);
int *bp;
- TCursor c;
+ Dot c;
if (col < 1 || row < 1) {
fprintf(stderr,
@@ -2414,8 +2298,8 @@ tresize(int col, int row)
}
/* ensure that both src and dst are not NULL */
if (i > 0) {
- memmove(term.line, term.line + i, row * sizeof(Line));
- memmove(term.alt, term.alt + i, row * sizeof(Line));
+ memmove(term.line, term.line + i, row * sizeof(Letter*));
+ memmove(term.alt, term.alt + i, row * sizeof(Letter*));
}
for (i += row; i < term.row; i++) {
free(term.line[i]);
@@ -2423,8 +2307,8 @@ tresize(int col, int row)
}
/* resize to new height */
- term.line = xrealloc(term.line, row * sizeof(Line));
- term.alt = xrealloc(term.alt, row * sizeof(Line));
+ term.line = xrealloc(term.line, row * sizeof(Letter*));
+ term.alt = xrealloc(term.alt, row * sizeof(Letter*));
term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
diff --git a/sys/cmd/term/term.h b/sys/cmd/term/term.h
index 62c407f..a155bbe 100644
--- a/sys/cmd/term/term.h
+++ b/sys/cmd/term/term.h
@@ -18,16 +18,19 @@
#define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
#define DEFAULT(a, b) (a) = (a) ? (a) : (b)
#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
-#define GLYPHCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
- (a).bg != (b).bg)
-#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
- (t1.tv_nsec-t2.tv_nsec)/1E6)
+#define GLYPHCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
+#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6)
#define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
-
#define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
#define IS_TRUECOL(x) (1 << 24 & (x))
-#define iota(x) 1 << (x)
+#define iota(x) 1 << (x)
+
+/* arbitrary sizes */
+#define ESC_BUF_SIZ (128*UTFmax)
+#define ESC_ARG_SIZ 16
+#define STR_BUF_SIZ ESC_BUF_SIZ
+#define STR_ARG_SIZ ESC_ARG_SIZ
// -----------------------------------------------------------------------
// constants
@@ -64,32 +67,88 @@ enum {
SnapLine = 2
};
-enum win_mode {
- Wvisible = 1 << 0,
- Wfocused = 1 << 1,
- Wappkeypad = 1 << 2,
- Wmousebtn = 1 << 3,
- Wmousemotion = 1 << 4,
- Wreverse = 1 << 5,
- Wkbdblock = 1 << 6,
- Whide = 1 << 7,
- Wappcursor = 1 << 8,
- Wmousesgr = 1 << 9,
- W8bit = 1 << 10,
- Wblink = 1 << 11,
- Wbflink = 1 << 12,
- Wfocus = 1 << 13,
- Wmousex10 = 1 << 14,
- Wmousemany = 1 << 15,
- Wbrcktpaste = 1 << 16,
- Wnumlock = 1 << 17,
+/* cursor state */
+enum {
+ CursorSave,
+ CursorLoad
+};
+
+/* cursor mode */
+enum {
+ CursorDefault = 0,
+ CursorWrap = 1,
+ CursorOrigin = 2
+};
+
+/* character set */
+enum {
+ CSgfx0,
+ CSgfx1,
+ CSuk,
+ CSusa,
+ CSmulti,
+ CSger,
+ CSfin,
+};
+
+/* escape sequences */
+enum {
+ Xstart = 1,
+ Xcsi = 2,
+ Xstr = 4, /* OSC, PM, APC */
+ Xaltcs = 8,
+ Xstrend = 16, /* a final string was encountered */
+ Xtest = 32, /* Enter in test mode */
+ Xutf8 = 64,
+ Xdcs =128,
+};
+
+/* terminal mode */
+enum {
+ Twrap = iota(0),
+ Tinsert = iota(1),
+ Taltscreen = iota(2),
+ Tcrlf = iota(3),
+ Techo = iota(4),
+ Tprint = iota(5),
+ Tutf8 = iota(6),
+ Tsixel = iota(7),
+};
+
+/* window mode */
+enum {
+ Wvisible = iota(0),
+ Wfocused = iota(1),
+ Wappkeypad = iota(2),
+ Wmousebtn = iota(3),
+ Wmousemotion = iota(4),
+ Wreverse = iota(5),
+ Wkbdblock = iota(6),
+ Whide = iota(7),
+ Wappcursor = iota(8),
+ Wmousesgr = iota(9),
+ W8bit = iota(10),
+ Wblink = iota(11),
+ Wbflink = iota(12),
+ Wfocus = iota(13),
+ Wmousex10 = iota(14),
+ Wmousemany = iota(15),
+ Wbrcktpaste = iota(16),
+ Wnumlock = iota(17),
Wmouse = Wmousebtn|Wmousemotion|Wmousex10|Wmousemany,
};
// -----------------------------------------------------------------------
// types
-typedef struct Letter Letter;
+
+/* term.c */
+typedef struct Letter Letter;
+typedef struct Dot Dot;
+typedef struct Selection Selection;
+typedef struct Terminal Terminal;
+
+typedef union Arg Arg;
struct Letter {
rune u; /* character code */
@@ -98,20 +157,88 @@ struct Letter {
uint32_t bg; /* background */
};
-typedef Letter *Line;
-
-typedef union {
- int i;
- uint ui;
+union Arg {
+ int i;
+ uint ui;
float f;
- void *v;
- char *s;
-} Arg;
+ void *v;
+ char *s;
+};
+
+struct Dot {
+ Letter attr; /* current char attributes */
+ int x;
+ int y;
+ char state;
+};
+
+struct Selection {
+ int mode;
+ int type;
+ int snap;
+ /*
+ * Selection variables:
+ * nb – normalized coordinates of the beginning of the selection
+ * ne – normalized coordinates of the end of the selection
+ * ob – original coordinates of the beginning of the selection
+ * oe – original coordinates of the end of the selection
+ */
+ struct {
+ int x, y;
+ } nb, ne, ob, oe;
+
+ int alt;
+};
+
+/* Internal representation of the screen */
+struct Terminal {
+ int row; /* nb row */
+ int col; /* nb col */
+ Letter **line; /* screen */
+ Letter **alt; /* alternate screen */
+ int *dirty; /* dirtyness of lines */
+ Dot c; /* cursor */
+ int ocx; /* old cursor col */
+ int ocy; /* old cursor row */
+ int top; /* top scroll limit */
+ int bot; /* bottom scroll limit */
+ int mode; /* terminal mode flags */
+ int esc; /* escape state flags */
+ char trantbl[4];/* charset table translation */
+ int charset; /* current charset */
+ int icharset; /* selected charset for sequence */
+ int *tabs;
+ rune lastc; /* last printed char outside of sequence, 0 if control */
+};
+
+/* CSI Escape sequence structs */
+/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
+typedef struct {
+ char buf[ESC_BUF_SIZ]; /* raw string */
+ ulong len; /* raw string length */
+ char priv;
+ int arg[ESC_ARG_SIZ];
+ int narg; /* nb of args */
+ char mode[2];
+} CSIEscape;
+
+/* STR Escape sequence structs */
+/* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
+typedef struct {
+ char type; /* ESC type ... */
+ char *buf; /* allocated raw string */
+ size_t siz; /* allocation size */
+ size_t len; /* raw string length */
+ char *args[STR_ARG_SIZ];
+ int narg; /* nb of args */
+} STREscape;
+
+
void xbell(void);
void xclipcopy(void);
void xdrawcursor(int, int, Letter, int, int, Letter);
-void xdrawline(Line, int, int, int);
+void xdrawline(Letter*, int, int, int);
void xfinishdraw(void);
void xloadcols(void);
int xsetcolorname(int, char *);
@@ -127,20 +254,20 @@ void die( char *, ...);
void redraw(void);
void draw(void);
-void printscreen(Arg *);
-void printsel(Arg *);
-void sendbreak(Arg *);
-void toggleprinter(Arg *);
-
-int tattrset(int);
-void tnew(int, int);
-void tresize(int, int);
-void tsetdirtattr(int);
-void ttyhangup(void);
-int ttynew(char *, char *, char *, char **);
-size_t ttyread(void);
-void ttyresize(int, int);
-void ttywrite( char *, size_t, int);
+void printscreen(Arg *);
+void printsel(Arg *);
+void sendbreak(Arg *);
+void toggleprinter(Arg *);
+
+int tattrset(int);
+void tnew(int, int);
+void tresize(int, int);
+void tsetdirtattr(int);
+void ttyhangup(void);
+int ttynew(char *, char *, char *, char **);
+ulong ttyread(void);
+void ttyresize(int, int);
+void ttywrite( char *, size_t, int);
void resettitle(void);
@@ -148,7 +275,7 @@ void selclear(void);
void selinit(void);
void selstart(int, int, int);
void selextend(int, int, int, int);
-int selected(int, int);
+int selected(int, int);
char *getsel(void);
void *xmalloc(size_t);
@@ -156,14 +283,14 @@ void *xrealloc(void *, size_t);
char *xstrdup(char *);
/* config.h globals */
-extern char *utmp;
-extern char *scroll;
-extern char *stty_args;
-extern char *vtiden;
-extern wchar_t *worddelimiters;
-extern int allowaltscreen;
-extern int allowwindowops;
-extern char *termname;
-extern unsigned int tabspaces;
-extern unsigned int defaultfg;
-extern unsigned int defaultbg;
+extern char *utmp;
+extern char *scroll;
+extern char *stty_args;
+extern char *vtiden;
+extern wchar *worddelimiters;
+extern int allowaltscreen;
+extern int allowwindowops;
+extern char *termname;
+extern uint tabspaces;
+extern uint defaultfg;
+extern uint defaultbg;
diff --git a/sys/cmd/term/x.c b/sys/cmd/term/x.c
index 129d639..74f3752 100644
--- a/sys/cmd/term/x.c
+++ b/sys/cmd/term/x.c
@@ -1590,7 +1590,7 @@ xstartdraw(void)
}
void
-xdrawline(Line line, int x1, int y1, int x2)
+xdrawline(Letter *line, int x1, int y1, int x2)
{
int i, x, ox, numspecs;
Letter base, new;