#include <errno.h>
#include <fcntl.h>
#include <limits.h>
-#include <locale.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
-#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/select.h>
-#include <sys/stat.h>
-#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
-#include <time.h>
#include <unistd.h>
-#include <libgen.h>
#include <wchar.h>
#include "st.h"
/* Arbitrary sizes */
#define UTF_INVALID 0xFFFD
+#define UTF_SIZ 4
#define ESC_BUF_SIZ (128*UTF_SIZ)
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ ESC_BUF_SIZ
ESC_DCS =128,
};
+typedef struct {
+ Glyph 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;
+} Term;
+
/* CSI Escape sequence structs */
/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
typedef struct {
int narg; /* nb of args */
} STREscape;
-
-static void execsh(char **);
+static void execsh(char *, char **);
static void stty(char **);
static void sigchld(int);
static void ttywriteraw(const char *, size_t);
static void tdeftran(char);
static void tstrsequence(uchar);
+static void drawregion(int, int, int, int);
+
+static void selnormalize(void);
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 char *utf8strchr(char *s, Rune u);
+static char *utf8strchr(char *, Rune);
static size_t utf8validate(Rune *, size_t);
static char *base64dec(const char *);
+static char base64dec_getc(const char **);
static ssize_t xwrite(int, const char *, size_t);
/* Globals */
-Term term;
-int cmdfd;
-pid_t pid;
-int oldbutton = 3; /* button event on startup: 3 = release */
-
+static Term term;
static Selection sel;
static CSIEscape csiescseq;
static STREscape strescseq;
static int iofd = 1;
+static int cmdfd;
+static pid_t pid;
static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
}
void
-execsh(char **args)
+execsh(char *cmd, char **args)
{
char *sh, *prog;
const struct passwd *pw;
}
if ((sh = getenv("SHELL")) == NULL)
- sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
+ sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
if (args)
prog = args[0];
perror("Couldn't call stty");
}
-void
-ttynew(char *line, char *out, char **args)
+int
+ttynew(char *line, char *cmd, char *out, char **args)
{
int m, s;
die("open line failed: %s\n", strerror(errno));
dup2(cmdfd, 0);
stty(args);
- return;
+ return cmdfd;
}
/* seems to work fine on linux, openbsd and freebsd */
die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
close(s);
close(m);
- execsh(args);
+ execsh(cmd, args);
break;
default:
close(s);
signal(SIGCHLD, sigchld);
break;
}
+ return cmdfd;
}
size_t
fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
}
+void
+ttyhangup()
+{
+ /* Send SIGHUP to shell */
+ kill(pid, SIGHUP);
+}
+
int
tattrset(int attr)
{
tputtab(csiescseq.arg[0]);
break;
case 'J': /* ED -- Clear screen */
- selclear();
switch (csiescseq.arg[0]) {
case 0: /* below */
tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
xsettitle(NULL);
}
+void
+drawregion(int x1, int y1, int x2, int y2)
+{
+ int y;
+ for (y = y1; y < y2; y++) {
+ if (!term.dirty[y])
+ continue;
+
+ term.dirty[y] = 0;
+ xdrawline(term.line[y], x1, y, x2);
+ }
+}
+
+void
+draw(void)
+{
+ int cx = term.c.x;
+
+ if (!xstartdraw())
+ return;
+
+ /* adjust cursor position */
+ LIMIT(term.ocx, 0, term.col-1);
+ LIMIT(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)
+ cx--;
+
+ drawregion(0, 0, term.col, term.row);
+ xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
+ term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
+ term.ocx = cx, term.ocy = term.c.y;
+ xfinishdraw();
+}
+
void
redraw(void)
{