ref: dcb88c71fb2132c1007a2ad0971ac475a54f02b5
parent: e6665c534f2efee4ae14b5b90395d2835bba9207
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Aug 14 07:23:31 EDT 2022
Make st compilable in 9front
--- a/config.h
+++ b/config.h
@@ -11,15 +11,9 @@
/*
* What program is execed by st depends of these precedence rules:
* 1: program passed with -e
- * 2: scroll and/or utmp
- * 3: SHELL environment variable
- * 4: value of shell in /etc/passwd
- * 5: value of shell in config.h
+ * 3: shell environment variable
+ * 4: /bin/rc
*/
-static char *shell = "/bin/sh";
-char *utmp = NULL;
-/* scroll program: to enable use a string like "scroll" */
-char *scroll = NULL;
char *stty_args = "stty raw pass8 nl -echo -iexten -cstopb 38400";
/* identification sequence returned in DA and DECID */
@@ -34,7 +28,7 @@
*
* More advanced example: L" `'\"()[]{}"
*/
-wchar_t *worddelimiters = L" ";
+Rune *worddelimiters = L" ";
/* selection timeouts (in milliseconds) */
static unsigned int doubleclicktimeout = 300;
@@ -151,25 +145,13 @@
static unsigned int rows = 24;
/*
- * Default colour and shape of the mouse cursor
- */
-static unsigned int mouseshape = XC_xterm;
-static unsigned int mousefg = 7;
-static unsigned int mousebg = 0;
-
-/*
* Color used to display font attributes when fontconfig selected a font which
* doesn't match the ones requested.
*/
static unsigned int defaultattr = 11;
-/*
- * Force mouse select/shortcuts while mask is active (when MODE_MOUSE is set).
- * Note that if you want to use ShiftMask with selmasks, set this to an other
- * modifier, set to 0 to not use it.
- */
-static uint forcemousemod = ShiftMask;
+#ifdef NO
/*
* Internal mouse shortcuts.
* Beware that overloading Button1 will disable the selection.
@@ -182,11 +164,13 @@
{ ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} },
{ XK_ANY_MOD, Button5, ttysend, {.s = "\005"} },
};
+#endif
/* Internal keyboard shortcuts. */
#define MODKEY Mod1Mask
#define TERMMOD (ControlMask|ShiftMask)
+#ifdef NO
static Shortcut shortcuts[] = {
/* mask keysym function argument */
{ XK_ANY_MOD, XK_Break, sendbreak, {.i = 0} },
@@ -202,6 +186,7 @@
{ ShiftMask, XK_Insert, selpaste, {.i = 0} },
{ TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
};
+#endif
/*
* Special keys (change & recompile st.info accordingly)
@@ -224,6 +209,7 @@
* position for a key.
*/
+#ifdef NO
/*
* If you want keys other than the X11 function keys (0xFD00 - 0xFFFF)
* to be mapped below, add them to this array.
@@ -452,17 +438,7 @@
{ XK_F34, XK_NO_MOD, "\033[21;5~", 0, 0},
{ XK_F35, XK_NO_MOD, "\033[23;5~", 0, 0},
};
-
-/*
- * Selection types' masks.
- * Use the same masks as usual.
- * Button1Mask is always unset, to make masks match between ButtonPress.
- * ButtonRelease and MotionNotify.
- * If no match is found, regular selection is used.
- */
-static uint selmasks[] = {
- [SEL_RECTANGULAR] = Mod1Mask,
-};
+#endif
/*
* Printable characters in ASCII, used to estimate the advance width
--- a/main.c
+++ b/main.c
@@ -1146,6 +1146,8 @@
chording = mc->buttons;
}
+#ifdef NO
+TODO
int
selected(int x, int y)
{
@@ -1164,6 +1166,7 @@
}
return s;
}
+#endif
void
readmenu(void)
--- a/mkfile
+++ b/mkfile
@@ -4,10 +4,12 @@
OFILES=\
main.$O\
+ st.$O\
vt.$O\
+ x.$O\
fs.$O\
-HFILES=cons.h
+HFILES=cons.h st.h win.h
BIN=/$objtype/bin
</sys/src/cmd/mkone
--- a/st.c
+++ b/st.c
@@ -1,33 +1,15 @@
/* See LICENSE for license details. */
+#include <u.h>
+#include <libc.h>
#include <ctype.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <pwd.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/ioctl.h>
-#include <sys/select.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <termios.h>
-#include <unistd.h>
-#include <wchar.h>
+#include <ape/limits.h>
+#include <ape/stdint.h>
+
#include "st.h"
#include "win.h"
+#include "config.h"
-#if defined(__linux)
- #include <pty.h>
-#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
- #include <util.h>
-#elif defined(__FreeBSD__) || defined(__DragonFly__)
- #include <libutil.h>
-#endif
-
/* Arbitrary sizes */
#define UTF_INVALID 0xFFFD
#define UTF_SIZ 4
@@ -41,8 +23,10 @@
#define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
#define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
#define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
-#define ISDELIM(u) (u && wcschr(worddelimiters, u))
+#define ISDELIM(u) (u && runestrchr(worddelimiters, u))
+#define BUFSIZ 512
+
enum term_mode {
MODE_WRAP = 1 << 0,
MODE_INSERT = 1 << 1,
@@ -134,7 +118,7 @@
/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
typedef struct {
char buf[ESC_BUF_SIZ]; /* raw string */
- size_t len; /* raw string length */
+ usize len; /* raw string length */
char priv;
int arg[ESC_ARG_SIZ];
int narg; /* nb of args */
@@ -146,8 +130,8 @@
typedef struct {
char type; /* ESC type ... */
char *buf; /* allocated raw string */
- size_t siz; /* allocation size */
- size_t len; /* raw string length */
+ usize siz; /* allocation size */
+ usize len; /* raw string length */
char *args[STR_ARG_SIZ];
int narg; /* nb of args */
} STREscape;
@@ -155,7 +139,7 @@
static void execsh(char *, char **);
static void stty(char **);
static void sigchld(int);
-static void ttywriteraw(const char *, size_t);
+static void ttywriteraw(const char *, usize);
static void csidump(void);
static void csihandle(void);
@@ -168,7 +152,7 @@
static void strparse(void);
static void strreset(void);
-static void tprinter(char *, size_t);
+static void tprinter(char *, usize);
static void tdumpsel(void);
static void tdumpline(int);
static void tdump(void);
@@ -198,7 +182,7 @@
static void tcontrolcode(uchar );
static void tdectest(char );
static void tdefutf8(char);
-static int32_t tdefcolor(const int *, int *, int);
+static long tdefcolor(const int *, int *, int);
static void tdeftran(char);
static void tstrsequence(uchar);
@@ -208,15 +192,14 @@
static void selscroll(int, int);
static void selsnap(int *, int *, int);
-static size_t utf8decode(const char *, Rune *, size_t);
-static Rune utf8decodebyte(char, size_t *);
-static char utf8encodebyte(Rune, size_t);
-static size_t utf8validate(Rune *, size_t);
+static usize utf8decode(const char *, Rune *, usize);
+static Rune utf8decodebyte(char, usize *);
+static char utf8encodebyte(Rune, usize);
+static usize utf8validate(Rune *, usize);
static char *base64dec(const char *);
static char base64dec_getc(const char **);
-static ssize_t xwrite(int, const char *, size_t);
/* Globals */
static Term term;
@@ -225,7 +208,7 @@
static STREscape strescseq;
static int iofd = 1;
static int cmdfd;
-static pid_t pid;
+static int pid;
static const uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
@@ -232,39 +215,24 @@
static const Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
-ssize_t
-xwrite(int fd, const char *s, size_t len)
-{
- size_t aux = len;
- ssize_t r;
+int errno; /* TODO: remove it!!! */
- while (len > 0) {
- r = write(fd, s, len);
- if (r < 0)
- return r;
- len -= r;
- s += r;
- }
-
- return aux;
-}
-
void *
-xmalloc(size_t len)
+xmalloc(usize len)
{
void *p;
if (!(p = malloc(len)))
- die("malloc: %s\n", strerror(errno));
+ sysfatal("malloc: %r\n");
return p;
}
void *
-xrealloc(void *p, size_t len)
+xrealloc(void *p, usize len)
{
- if ((p = realloc(p, len)) == NULL)
- die("realloc: %s\n", strerror(errno));
+ if ((p = realloc(p, len)) == nil)
+ sysfatal("realloc: %r\n");
return p;
}
@@ -274,16 +242,16 @@
{
char *p;
- if ((p = strdup(s)) == NULL)
- die("strdup: %s\n", strerror(errno));
+ if ((p = strdup(s)) == nil)
+ sysfatal("strdup: %r\n");
return p;
}
-size_t
-utf8decode(const char *c, Rune *u, size_t clen)
+static usize
+utf8decode(const char *c, Rune *u, usize clen)
{
- size_t i, j, len, type;
+ usize i, j, len, type;
Rune udecoded;
*u = UTF_INVALID;
@@ -305,8 +273,8 @@
return len;
}
-Rune
-utf8decodebyte(char c, size_t *i)
+static Rune
+utf8decodebyte(char c, usize *i)
{
for (*i = 0; *i < LEN(utfmask); ++(*i))
if (((uchar)c & utfmask[*i]) == utfbyte[*i])
@@ -315,10 +283,10 @@
return 0;
}
-size_t
+usize
utf8encode(Rune u, char *c)
{
- size_t len, i;
+ usize len, i;
len = utf8validate(&u, 0);
if (len > UTF_SIZ)
@@ -333,14 +301,14 @@
return len;
}
-char
-utf8encodebyte(Rune u, size_t i)
+static char
+utf8encodebyte(Rune u, usize i)
{
return utfbyte[i] | (u & ~utfmask[i]);
}
-size_t
-utf8validate(Rune *u, size_t i)
+static usize
+utf8validate(Rune *u, usize i)
{
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
@@ -350,7 +318,7 @@
return i;
}
-char
+static char
base64dec_getc(const char **src)
{
while (**src && !isprint((unsigned char)**src))
@@ -358,10 +326,10 @@
return **src ? *((*src)++) : '='; /* emulate padding if string ends */
}
-char *
+static char *
base64dec(const char *src)
{
- size_t in_len = strlen(src);
+ usize in_len = strlen(src);
char *result, *dst;
static const char base64_digits[256] = {
[43] = 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
@@ -404,7 +372,7 @@
sel.ob.x = -1;
}
-int
+static int
tlinelen(int y)
{
int i = term.col;
@@ -464,7 +432,7 @@
sel.mode = done ? SEL_IDLE : SEL_READY;
}
-void
+static void
selnormalize(void)
{
int i;
@@ -508,7 +476,7 @@
&& (y != sel.ne.y || x <= sel.ne.x);
}
-void
+static void
selsnap(int *x, int *y, int direction)
{
int newx, newy, xt, yt;
@@ -589,7 +557,7 @@
const Glyph *gp, *last;
if (sel.ob.x == -1)
- return NULL;
+ return nil;
bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
ptr = str = xmalloc(bufsize);
@@ -646,102 +614,56 @@
tsetdirt(sel.nb.y, sel.ne.y);
}
-void
-die(const char *errstr, ...)
-{
- va_list ap;
-
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
- va_end(ap);
- exit(1);
-}
-
-void
+static void
execsh(char *cmd, char **args)
{
char *sh, *prog, *arg;
- const struct passwd *pw;
+ char *targs[3];
- errno = 0;
- if ((pw = getpwuid(getuid())) == NULL) {
- if (errno)
- die("getpwuid: %s\n", strerror(errno));
- else
- die("who are you?\n");
- }
+ sh = cmd ? cmd : "/bin/rc";
- if ((sh = getenv("SHELL")) == NULL)
- sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
-
if (args) {
prog = args[0];
- arg = NULL;
- } else if (scroll) {
- prog = scroll;
- arg = utmp ? utmp : sh;
- } else if (utmp) {
- prog = utmp;
- arg = NULL;
+ arg = nil;
} else {
prog = sh;
- arg = NULL;
+ arg = nil;
}
- DEFAULT(args, ((char *[]) {prog, arg, NULL}));
- unsetenv("COLUMNS");
- unsetenv("LINES");
- unsetenv("TERMCAP");
- setenv("LOGNAME", pw->pw_name, 1);
- setenv("USER", pw->pw_name, 1);
- setenv("SHELL", sh, 1);
- setenv("HOME", pw->pw_dir, 1);
- setenv("TERM", termname, 1);
+ targs[0] = prog;
+ targs[1] = arg;
+ targs[2] = nil;
+ DEFAULT(args, targs);
- signal(SIGCHLD, SIG_DFL);
- signal(SIGHUP, SIG_DFL);
- signal(SIGINT, SIG_DFL);
- signal(SIGQUIT, SIG_DFL);
- signal(SIGTERM, SIG_DFL);
- signal(SIGALRM, SIG_DFL);
+ remove("/env/COLUMNS");
+ remove("/env/LINES");
+ remove("/env/TERMCAP");
+ putenv("LOGNAME", getenv("user"));
+ putenv("USER", getenv("user"));
+ putenv("SHELL", sh);
+ putenv("HOME", getenv("home"));
+ putenv("TERM", termname);
- execvp(prog, args);
- _exit(1);
+ exec(prog, args);
+ sysfatal("%r");
}
-void
-sigchld(int a)
-{
- int stat;
- pid_t p;
-
- if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
- die("waiting for pid %hd failed: %s\n", pid, strerror(errno));
-
- if (pid != p)
- return;
-
- if (WIFEXITED(stat) && WEXITSTATUS(stat))
- die("child exited with status %d\n", WEXITSTATUS(stat));
- else if (WIFSIGNALED(stat))
- die("child terminated due to signal %d\n", WTERMSIG(stat));
- _exit(0);
-}
-
-void
+static void
stty(char **args)
{
+ USED(args);
+/*
char cmd[_POSIX_ARG_MAX], **p, *q, *s;
- size_t n, siz;
+ usize n, siz;
if ((n = strlen(stty_args)) > sizeof(cmd)-1)
- die("incorrect stty parameters\n");
+ sysfatal("incorrect stty parameters\n");
memcpy(cmd, stty_args, n);
q = cmd + n;
siz = sizeof(cmd) - n;
for (p = args; p && (s = *p); ++p) {
if ((n = strlen(s)) > siz-1)
- die("stty parameter length too long\n");
+ sysfatal("stty parameter length too long\n");
*q++ = ' ';
memcpy(q, s, n);
q += n;
@@ -750,71 +672,22 @@
*q = '\0';
if (system(cmd) != 0)
perror("Couldn't call stty");
+*/
}
int
ttynew(const char *line, char *cmd, const char *out, char **args)
{
- int m, s;
+ USED(line);
+ USED(cmd);
+ USED(out);
+ USED(args);
- if (out) {
- term.mode |= MODE_PRINT;
- iofd = (!strcmp(out, "-")) ?
- 1 : open(out, O_WRONLY | O_CREAT, 0666);
- if (iofd < 0) {
- fprintf(stderr, "Error opening %s:%s\n",
- out, strerror(errno));
- }
- }
-
- if (line) {
- if ((cmdfd = open(line, O_RDWR)) < 0)
- die("open line '%s' failed: %s\n",
- line, strerror(errno));
- dup2(cmdfd, 0);
- stty(args);
- return cmdfd;
- }
-
- /* seems to work fine on linux, openbsd and freebsd */
- if (openpty(&m, &s, NULL, NULL, NULL) < 0)
- die("openpty failed: %s\n", strerror(errno));
-
- switch (pid = fork()) {
- case -1:
- die("fork failed: %s\n", strerror(errno));
- break;
- case 0:
- close(iofd);
- close(m);
- setsid(); /* create a new process group */
- dup2(s, 0);
- dup2(s, 1);
- dup2(s, 2);
- if (ioctl(s, TIOCSCTTY, NULL) < 0)
- die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
- if (s > 2)
- close(s);
-#ifdef __OpenBSD__
- if (pledge("stdio getpw proc exec", NULL) == -1)
- die("pledge\n");
-#endif
- execsh(cmd, args);
- break;
- default:
-#ifdef __OpenBSD__
- if (pledge("stdio rpath tty proc", NULL) == -1)
- die("pledge\n");
-#endif
- close(s);
- cmdfd = m;
- signal(SIGCHLD, sigchld);
- break;
- }
- return cmdfd;
+ /* TODO: implement this */
+ return 0;
}
-size_t
+usize
ttyread(void)
{
static char buf[BUFSIZ];
@@ -826,9 +699,9 @@
switch (ret) {
case 0:
- exit(0);
+ exits(nil);
case -1:
- die("couldn't read from shell: %s\n", strerror(errno));
+ sysfatal("couldn't read from shell: %r\n");
default:
buflen += ret;
written = twrite(buf, buflen, 0);
@@ -841,7 +714,7 @@
}
void
-ttywrite(const char *s, size_t n, int may_echo)
+ttywrite(const char *s, usize n, int may_echo)
{
const char *next;
@@ -868,12 +741,15 @@
}
}
-void
-ttywriteraw(const char *s, size_t n)
+static void
+ttywriteraw(const char *s, usize n)
{
+ USED(s);
+ USED(n);
+#ifdef NO
fd_set wfd, rfd;
- ssize_t r;
- size_t lim = 256;
+ susize r;
+ usize lim = 256;
/*
* Remember that we are using a pty, which might be a modem line.
@@ -888,10 +764,10 @@
FD_SET(cmdfd, &rfd);
/* Check if we can write. */
- if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
+ if (pselect(cmdfd+1, &rfd, &wfd, nil, nil, nil) < 0) {
if (errno == EINTR)
continue;
- die("select failed: %s\n", strerror(errno));
+ sysfatal("select failed: %r\n");
}
if (FD_ISSET(cmdfd, &wfd)) {
/*
@@ -922,27 +798,24 @@
return;
write_error:
- die("write error on tty: %s\n", strerror(errno));
+ sysfatal("write error on tty: %r\n");
+#endif
}
void
ttyresize(int tw, int th)
{
- struct winsize w;
-
- w.ws_row = term.row;
- w.ws_col = term.col;
- w.ws_xpixel = tw;
- w.ws_ypixel = th;
- if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
- fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
+ USED(tw);
+ USED(th);
+ abort();
}
void
ttyhangup()
{
+ /* TODO */
/* Send SIGHUP to shell */
- kill(pid, SIGHUP);
+ abort();
}
int
@@ -960,13 +833,13 @@
return 0;
}
-void
+static void
tsetdirt(int top, int bot)
{
int i;
- LIMIT(top, 0, term.row-1);
- LIMIT(bot, 0, term.row-1);
+ top = CLAMP(top, 0, term.row-1);
+ bot = CLAMP(bot, 0, term.row-1);
for (i = top; i <= bot; i++)
term.dirty[i] = 1;
@@ -987,13 +860,13 @@
}
}
-void
+static void
tfulldirt(void)
{
tsetdirt(0, term.row-1);
}
-void
+static void
tcursor(int mode)
{
static TCursor c[2];
@@ -1007,16 +880,18 @@
}
}
-void
+static void
treset(void)
{
uint i;
- term.c = (TCursor){{
- .mode = ATTR_NULL,
- .fg = defaultfg,
- .bg = defaultbg
- }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
+ memset(&term.c, 0, sizeof(term.c));
+ term.c.attr.mode = ATTR_NULL;
+ term.c.attr.fg = defaultfg;
+ term.c.attr.bg = defaultbg;
+ term.c.x = 0;
+ term.c.y = 0;
+ term.c.state = CURSOR_DEFAULT;
memset(term.tabs, 0, term.col * sizeof(*term.tabs));
for (i = tabspaces; i < term.col; i += tabspaces)
@@ -1038,12 +913,14 @@
void
tnew(int col, int row)
{
- term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
+ memset(&term, 0, sizeof(term));
+ term.c.attr.fg = defaultfg;
+ term.c.attr.bg = defaultbg;
tresize(col, row);
treset();
}
-void
+static void
tswapscreen(void)
{
Line *tmp = term.line;
@@ -1054,13 +931,13 @@
tfulldirt();
}
-void
+static void
tscrolldown(int orig, int n)
{
int i;
Line temp;
- LIMIT(n, 0, term.bot-orig+1);
+ n = CLAMP(n, 0, term.bot-orig+1);
tsetdirt(orig, term.bot-n);
tclearregion(0, term.bot-n+1, term.col-1, term.bot);
@@ -1074,13 +951,13 @@
selscroll(orig, n);
}
-void
+static void
tscrollup(int orig, int n)
{
int i;
Line temp;
- LIMIT(n, 0, term.bot-orig+1);
+ n = CLAMP(n, 0, term.bot-orig+1);
tclearregion(0, orig, term.col-1, orig+n-1);
tsetdirt(orig+n, term.bot);
@@ -1094,7 +971,7 @@
selscroll(orig, -n);
}
-void
+static void
selscroll(int orig, int n)
{
if (sel.ob.x == -1)
@@ -1114,7 +991,7 @@
}
}
-void
+static void
tnewline(int first_col)
{
int y = term.c.y;
@@ -1127,7 +1004,7 @@
tmoveto(first_col ? 0 : term.c.x, y);
}
-void
+static void
csiparse(void)
{
char *p = csiescseq.buf, *np;
@@ -1141,7 +1018,7 @@
csiescseq.buf[csiescseq.len] = '\0';
while (p < csiescseq.buf+csiescseq.len) {
- np = NULL;
+ np = nil;
v = strtol(p, &np, 10);
if (np == p)
v = 0;
@@ -1158,13 +1035,13 @@
}
/* for absolute user moves, when decom is set */
-void
+static void
tmoveato(int x, int y)
{
tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
}
-void
+static void
tmoveto(int x, int y)
{
int miny, maxy;
@@ -1177,11 +1054,11 @@
maxy = term.row - 1;
}
term.c.state &= ~CURSOR_WRAPNEXT;
- term.c.x = LIMIT(x, 0, term.col-1);
- term.c.y = LIMIT(y, miny, maxy);
+ term.c.x = CLAMP(x, 0, term.col-1);
+ term.c.y = CLAMP(y, miny, maxy);
}
-void
+static void
tsetchar(Rune u, const Glyph *attr, int x, int y)
{
static const char *vt100_0[62] = { /* 0x41 - 0x7e */
@@ -1217,7 +1094,7 @@
term.line[y][x].u = u;
}
-void
+static void
tclearregion(int x1, int y1, int x2, int y2)
{
int x, y, temp;
@@ -1228,10 +1105,10 @@
if (y1 > y2)
temp = y1, y1 = y2, y2 = temp;
- LIMIT(x1, 0, term.col-1);
- LIMIT(x2, 0, term.col-1);
- LIMIT(y1, 0, term.row-1);
- LIMIT(y2, 0, term.row-1);
+ x1 = CLAMP(x1, 0, term.col-1);
+ x2 = CLAMP(x2, 0, term.col-1);
+ y1 = CLAMP(y1, 0, term.row-1);
+ y2 = CLAMP(y2, 0, term.row-1);
for (y = y1; y <= y2; y++) {
term.dirty[y] = 1;
@@ -1247,13 +1124,13 @@
}
}
-void
+static void
tdeletechar(int n)
{
int dst, src, size;
Glyph *line;
- LIMIT(n, 0, term.col - term.c.x);
+ n = CLAMP(n, 0, term.col - term.c.x);
dst = term.c.x;
src = term.c.x + n;
@@ -1264,13 +1141,13 @@
tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
}
-void
+static void
tinsertblank(int n)
{
int dst, src, size;
Glyph *line;
- LIMIT(n, 0, term.col - term.c.x);
+ n = CLAMP(n, 0, term.col - term.c.x);
dst = term.c.x + n;
src = term.c.x;
@@ -1281,7 +1158,7 @@
tclearregion(src, term.c.y, dst - 1, term.c.y);
}
-void
+static void
tinsertblankline(int n)
{
if (BETWEEN(term.c.y, term.top, term.bot))
@@ -1288,7 +1165,7 @@
tscrolldown(term.c.y, n);
}
-void
+static void
tdeleteline(int n)
{
if (BETWEEN(term.c.y, term.top, term.bot))
@@ -1295,16 +1172,16 @@
tscrollup(term.c.y, n);
}
-int32_t
+static long
tdefcolor(const int *attr, int *npar, int l)
{
- int32_t idx = -1;
+ long idx = -1;
uint r, g, b;
switch (attr[*npar + 1]) {
case 2: /* direct color in RGB space */
if (*npar + 4 >= l) {
- fprintf(stderr,
+ fprint(2,
"erresc(38): Incorrect number of parameters (%d)\n",
*npar);
break;
@@ -1314,7 +1191,7 @@
b = attr[*npar + 4];
*npar += 4;
if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
- fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
+ fprint(2, "erresc: bad rgb color (%ud,%ud,%ud)\n",
r, g, b);
else
idx = TRUECOLOR(r, g, b);
@@ -1321,7 +1198,7 @@
break;
case 5: /* indexed color */
if (*npar + 2 >= l) {
- fprintf(stderr,
+ fprint(2,
"erresc(38): Incorrect number of parameters (%d)\n",
*npar);
break;
@@ -1328,7 +1205,7 @@
}
*npar += 2;
if (!BETWEEN(attr[*npar], 0, 255))
- fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
+ fprint(2, "erresc: bad fgcolor %d\n", attr[*npar]);
else
idx = attr[*npar];
break;
@@ -1337,7 +1214,7 @@
case 3: /* direct color in CMY space */
case 4: /* direct color in CMYK space */
default:
- fprintf(stderr,
+ fprint(2,
"erresc(38): gfx attr %d unknown\n", attr[*npar]);
break;
}
@@ -1345,11 +1222,11 @@
return idx;
}
-void
+static void
tsetattr(const int *attr, int l)
{
int i;
- int32_t idx;
+ long idx;
for (i = 0; i < l; i++) {
switch (attr[i]) {
@@ -1437,7 +1314,7 @@
} else if (BETWEEN(attr[i], 100, 107)) {
term.c.attr.bg = attr[i] - 100 + 8;
} else {
- fprintf(stderr,
+ fprint(2,
"erresc(default): gfx attr %d unknown\n",
attr[i]);
csidump();
@@ -1447,13 +1324,13 @@
}
}
-void
+static void
tsetscroll(int t, int b)
{
int temp;
- LIMIT(t, 0, term.row-1);
- LIMIT(b, 0, term.row-1);
+ t = CLAMP(t, 0, term.row-1);
+ b = CLAMP(b, 0, term.row-1);
if (t > b) {
temp = t;
t = b;
@@ -1463,7 +1340,7 @@
term.bot = b;
}
-void
+static void
tsetmode(int priv, int set, const int *args, int narg)
{
int alt; const int *lim;
@@ -1562,7 +1439,7 @@
codes. */
break;
default:
- fprintf(stderr,
+ fprint(2,
"erresc: unknown private set/reset mode %d\n",
*args);
break;
@@ -1584,7 +1461,7 @@
MODBIT(term.mode, set, MODE_CRLF);
break;
default:
- fprintf(stderr,
+ fprint(2,
"erresc: unknown set/reset mode %d\n",
*args);
break;
@@ -1593,7 +1470,7 @@
}
}
-void
+static void
csihandle(void)
{
char buf[40];
@@ -1602,9 +1479,9 @@
switch (csiescseq.mode[0]) {
default:
unknown:
- fprintf(stderr, "erresc: unknown csi ");
+ fprint(2, "erresc: unknown csi ");
csidump();
- /* die(""); */
+ /* sysfatal(""); */
break;
case '@': /* ICH -- Insert <n> blank char */
DEFAULT(csiescseq.arg[0], 1);
@@ -1771,7 +1648,7 @@
break;
case 'n': /* DSR – Device Status Report (cursor position) */
if (csiescseq.arg[0] == 6) {
- len = snprintf(buf, sizeof(buf), "\033[%i;%iR",
+ len = snprint(buf, sizeof(buf), "\033[%d;%dR",
term.c.y+1, term.c.x+1);
ttywrite(buf, len, 0);
}
@@ -1805,37 +1682,37 @@
}
}
-void
+static void
csidump(void)
{
- size_t i;
+ usize i;
uint c;
- fprintf(stderr, "ESC[");
+ fprint(2, "ESC[");
for (i = 0; i < csiescseq.len; i++) {
c = csiescseq.buf[i] & 0xff;
if (isprint(c)) {
- putc(c, stderr);
+ fprint(2, "%c",c);
} else if (c == '\n') {
- fprintf(stderr, "(\\n)");
+ fprint(2, "(\\n)");
} else if (c == '\r') {
- fprintf(stderr, "(\\r)");
+ fprint(2, "(\\r)");
} else if (c == 0x1b) {
- fprintf(stderr, "(\\e)");
+ fprint(2, "(\\e)");
} else {
- fprintf(stderr, "(%02x)", c);
+ fprint(2, "(%02x)", c);
}
}
- putc('\n', stderr);
+ fprint(2, "\n");
}
-void
+static void
csireset(void)
{
memset(&csiescseq, 0, sizeof(csiescseq));
}
-void
+static void
osc_color_response(int num, int index, int is_osc4)
{
int n;
@@ -1843,16 +1720,16 @@
unsigned char r, g, b;
if (xgetcolor(is_osc4 ? num : index, &r, &g, &b)) {
- fprintf(stderr, "erresc: failed to fetch %s color %d\n",
+ fprint(2, "erresc: failed to fetch %s color %d\n",
is_osc4 ? "osc4" : "osc",
is_osc4 ? num : index);
return;
}
- n = snprintf(buf, sizeof buf, "\033]%s%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
+ n = snprint(buf, sizeof buf, "\033]%s%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
is_osc4 ? "4;" : "", num, r, r, g, g, b, b);
if (n < 0 || n >= sizeof(buf)) {
- fprintf(stderr, "error: %s while printing %s response\n",
+ fprint(2, "error: %s while printing %s response\n",
n < 0 ? "snprintf failed" : "truncation occurred",
is_osc4 ? "osc4" : "osc");
} else {
@@ -1860,10 +1737,10 @@
}
}
-void
+static void
strhandle(void)
{
- char *p = NULL, *dec;
+ char *p = nil, *dec;
int j, narg, par;
const struct { int idx; char *str; } osc_table[] = {
{ defaultfg, "foreground" },
@@ -1899,7 +1776,7 @@
xsetsel(dec);
xclipcopy();
} else {
- fprintf(stderr, "erresc: invalid base64\n");
+ fprint(2, "erresc: invalid base64\n");
}
}
return;
@@ -1915,7 +1792,7 @@
if (!strcmp(p, "?")) {
osc_color_response(par, osc_table[j].idx, 0);
} else if (xsetcolorname(osc_table[j].idx, p)) {
- fprintf(stderr, "erresc: invalid %s color: %s\n",
+ fprint(2, "erresc: invalid %s color: %s\n",
osc_table[j].str, p);
} else {
tfulldirt();
@@ -1934,7 +1811,7 @@
} else if (xsetcolorname(j, p)) {
if (par == 104 && narg <= 1)
return; /* color reset without parameter */
- fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
+ fprint(2, "erresc: invalid color j=%d, p=%s\n",
j, p ? p : "(null)");
} else {
/*
@@ -1955,11 +1832,11 @@
return;
}
- fprintf(stderr, "erresc: unknown str ");
+ fprint(2, "erresc: unknown str ");
strdump();
}
-void
+static void
strparse(void)
{
int c;
@@ -1981,53 +1858,54 @@
}
}
-void
+static void
strdump(void)
{
- size_t i;
+ usize i;
uint c;
- fprintf(stderr, "ESC%c", strescseq.type);
+ fprint(2, "ESC%c", strescseq.type);
for (i = 0; i < strescseq.len; i++) {
c = strescseq.buf[i] & 0xff;
if (c == '\0') {
- putc('\n', stderr);
+ fprint(2, "\n");
return;
} else if (isprint(c)) {
- putc(c, stderr);
+ fprint(2, "%c", c);
} else if (c == '\n') {
- fprintf(stderr, "(\\n)");
+ fprint(2, "(\\n)");
} else if (c == '\r') {
- fprintf(stderr, "(\\r)");
+ fprint(2, "(\\r)");
} else if (c == 0x1b) {
- fprintf(stderr, "(\\e)");
+ fprint(2, "(\\e)");
} else {
- fprintf(stderr, "(%02x)", c);
+ fprint(2, "(%02x)", c);
}
}
- fprintf(stderr, "ESC\\\n");
+ fprint(2, "ESC\\\n");
}
-void
+static void
strreset(void)
{
- strescseq = (STREscape){
- .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
- .siz = STR_BUF_SIZ,
- };
+ memset(&strescseq, 0, sizeof(strescseq));
+ strescseq.buf = xrealloc(strescseq.buf, STR_BUF_SIZ);
+ strescseq.siz = STR_BUF_SIZ;
}
void
sendbreak(const Arg *arg)
{
- if (tcsendbreak(cmdfd, 0))
- perror("Error sending break");
+ USED(arg);
+
+ /* TODO */
+ abort();
}
-void
-tprinter(char *s, size_t len)
+static void
+tprinter(char *s, usize len)
{
- if (iofd != -1 && xwrite(iofd, s, len) < 0) {
+ if (iofd != -1 && write(iofd, s, len) < 0) {
perror("Error writing to output file");
close(iofd);
iofd = -1;
@@ -2037,6 +1915,7 @@
void
toggleprinter(const Arg *arg)
{
+ USED(arg);
term.mode ^= MODE_PRINT;
}
@@ -2043,6 +1922,7 @@
void
printscreen(const Arg *arg)
{
+ USED(arg);
tdump();
}
@@ -2049,10 +1929,11 @@
void
printsel(const Arg *arg)
{
+ USED(arg);
tdumpsel();
}
-void
+static void
tdumpsel(void)
{
char *ptr;
@@ -2063,7 +1944,7 @@
}
}
-void
+static void
tdumpline(int n)
{
char buf[UTF_SIZ];
@@ -2078,7 +1959,7 @@
tprinter("\n", 1);
}
-void
+static void
tdump(void)
{
int i;
@@ -2087,7 +1968,7 @@
tdumpline(i);
}
-void
+static void
tputtab(int n)
{
uint x = term.c.x;
@@ -2101,10 +1982,10 @@
for (--x; x > 0 && !term.tabs[x]; --x)
/* nothing */ ;
}
- term.c.x = LIMIT(x, 0, term.col-1);
+ term.c.x = CLAMP(x, 0, term.col-1);
}
-void
+static void
tdefutf8(char ascii)
{
if (ascii == 'G')
@@ -2113,7 +1994,7 @@
term.mode &= ~MODE_UTF8;
}
-void
+static void
tdeftran(char ascii)
{
static char cs[] = "0B";
@@ -2120,14 +2001,14 @@
static int vcs[] = {CS_GRAPHIC0, CS_USA};
char *p;
- if ((p = strchr(cs, ascii)) == NULL) {
- fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
+ if ((p = strchr(cs, ascii)) == nil) {
+ fprint(2, "esc unhandled charset: ESC ( %c\n", ascii);
} else {
term.trantbl[term.icharset] = vcs[p - cs];
}
}
-void
+static void
tdectest(char c)
{
int x, y;
@@ -2140,7 +2021,7 @@
}
}
-void
+static void
tstrsequence(uchar c)
{
switch (c) {
@@ -2162,7 +2043,7 @@
term.esc |= ESC_STR;
}
-void
+static void
tcontrolcode(uchar ascii)
{
switch (ascii) {
@@ -2263,7 +2144,7 @@
* returns 1 when the sequence is finished and it hasn't to read
* more characters for this sequence, otherwise 0
*/
-int
+static int
eschandle(uchar ascii)
{
switch (ascii) {
@@ -2339,7 +2220,7 @@
strhandle();
break;
default:
- fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
+ fprint(2, "erresc: unknown sequence ESC 0x%02X '%c'\n",
(uchar) ascii, isprint(ascii)? ascii:'.');
break;
}
@@ -2346,7 +2227,17 @@
return 1;
}
-void
+static int
+wcwidth(Rune u)
+{
+ USED(u);
+
+ /* TODO */
+
+ return 1;
+}
+
+static void
tputc(Rune u)
{
char c[UTF_SIZ];
@@ -2354,6 +2245,7 @@
int width, len;
Glyph *gp;
+ width = 0;
control = ISCONTROL(u);
if (u < 127 || !IS_SET(MODE_UTF8)) {
c[0] = u;
@@ -2424,7 +2316,7 @@
if (term.esc & ESC_CSI) {
csiescseq.buf[csiescseq.len++] = u;
if (BETWEEN(u, 0x40, 0x7E)
- || csiescseq.len >= \
+ || csiescseq.len >=
sizeof(csiescseq.buf)-1) {
term.esc = 0;
csiparse();
@@ -2488,7 +2380,7 @@
}
}
-int
+static int
twrite(const char *buf, int buflen, int show_ctrl)
{
int charsize;
@@ -2530,8 +2422,8 @@
TCursor c;
if (col < 1 || row < 1) {
- fprintf(stderr,
- "tresize: error resizing to %dx%d\n", col, row);
+ fprint(2,
+ "tresize: error resizing to %dx%d\n", col, row);
return;
}
@@ -2544,7 +2436,7 @@
free(term.line[i]);
free(term.alt[i]);
}
- /* ensure that both src and dst are not NULL */
+ /* ensure that both src and dst are not nil */
if (i > 0) {
memmove(term.line, term.line + i, row * sizeof(Line));
memmove(term.alt, term.alt + i, row * sizeof(Line));
@@ -2605,10 +2497,10 @@
void
resettitle(void)
{
- xsettitle(NULL);
+ xsettitle(nil);
}
-void
+static void
drawregion(int x1, int y1, int x2, int y2)
{
int y;
@@ -2623,7 +2515,7 @@
}
void
-draw(void)
+tdraw(void)
{
int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
@@ -2631,8 +2523,8 @@
return;
/* adjust cursor position */
- LIMIT(term.ocx, 0, term.col-1);
- LIMIT(term.ocy, 0, term.row-1);
+ term.ocx = CLAMP(term.ocx, 0, term.col-1);
+ term.ocy = CLAMP(term.ocy, 0, term.row-1);
if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY)
term.ocx--;
if (term.line[term.c.y][cx].mode & ATTR_WDUMMY)
@@ -2652,5 +2544,5 @@
redraw(void)
{
tfulldirt();
- draw();
+ tdraw();
}
--- a/st.h
+++ b/st.h
@@ -1,8 +1,5 @@
/* See LICENSE for license details. */
-#include <stdint.h>
-#include <sys/types.h>
-
/* macros */
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
@@ -10,7 +7,7 @@
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#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 CLAMP(x, a,b) (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
#define ATTRCMP(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 + \
@@ -57,14 +54,12 @@
typedef unsigned long ulong;
typedef unsigned short ushort;
-typedef uint_least32_t Rune;
-
#define Glyph Glyph_
typedef struct {
Rune u; /* character code */
ushort mode; /* attribute flags */
- uint32_t fg; /* foreground */
- uint32_t bg; /* background */
+ ulong fg; /* foreground */
+ ulong bg; /* background */
} Glyph;
typedef Glyph *Line;
@@ -92,9 +87,9 @@
void tsetdirtattr(int);
void ttyhangup(void);
int ttynew(const char *, char *, const char *, char **);
-size_t ttyread(void);
+usize ttyread(void);
void ttyresize(int, int);
-void ttywrite(const char *, size_t, int);
+void ttywrite(const char *, usize, int);
void resettitle(void);
@@ -105,10 +100,10 @@
int selected(int, int);
char *getsel(void);
-size_t utf8encode(Rune, char *);
+usize utf8encode(Rune, char *);
-void *xmalloc(size_t);
-void *xrealloc(void *, size_t);
+void *xmalloc(usize);
+void *xrealloc(void *, usize);
char *xstrdup(const char *);
/* config.h globals */
@@ -116,7 +111,7 @@
extern char *scroll;
extern char *stty_args;
extern char *vtiden;
-extern wchar_t *worddelimiters;
+extern Rune *worddelimiters;
extern int allowaltscreen;
extern int allowwindowops;
extern char *termname;
--- a/win.h
+++ b/win.h
@@ -19,8 +19,7 @@
MODE_MOUSEMANY = 1 << 15,
MODE_BRCKTPASTE = 1 << 16,
MODE_NUMLOCK = 1 << 17,
- MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
- |MODE_MOUSEMANY,
+ MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10|MODE_MOUSEMANY,
};
void xbell(void);
--- /dev/null
+++ b/x.c
@@ -1,0 +1,89 @@
+#include <u.h>
+#include <libc.h>
+
+#include "st.h"
+#include "win.h"
+
+void
+xbell(void)
+{
+}
+
+void
+xclipcopy(void)
+{
+}
+
+void
+xdrawcursor(int, int, Glyph, int, int, Glyph)
+{
+}
+
+void
+xdrawline(Line, int, int, int)
+{
+}
+
+void
+xfinishdraw(void)
+{
+}
+
+void
+xloadcols(void)
+{
+}
+
+int
+xsetcolorname(int, const char *)
+{
+ return 0;
+}
+
+int
+xgetcolor(int, unsigned char *, unsigned char *, unsigned char *)
+{
+ return 0;
+}
+
+void
+xseticontitle(char *)
+{
+}
+
+void
+xsettitle(char *)
+{
+}
+
+int
+xsetcursor(int)
+{
+ return 0;
+}
+
+void
+xsetmode(int, unsigned int)
+{
+}
+
+void
+xsetpointermotion(int)
+{
+}
+
+void
+xsetsel(char *)
+{
+}
+
+int
+xstartdraw(void)
+{
+ return 0;
+}
+
+void
+xximspot(int, int)
+{
+}