+/* See LICENSE for licence details. */
+#define _XOPEN_SOURCE 600
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <locale.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/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+#include <X11/Xutil.h>
+
+#define TNAME "st"
+
+/* Arbitrary sizes */
+#define ESCSIZ 256
+#define ESCARG 16
+
+#define SERRNO strerror(errno)
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) < (b) ? (b) : (a))
+#define LEN(a) (sizeof(a) / sizeof(a[0]))
+#define DEFAULT(a, b) (a) = (a) ? (a) : (b)
+#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
+#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
+
+/* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
+enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 };
+enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload };
+enum { CRset=1, CRupdate=2 };
+enum { TMwrap=1, TMinsert=2 };
+enum { SCupdate, SCredraw };
+
+typedef int Color;
+
+typedef struct {
+ char c; /* character code */
+ char mode; /* attribute flags */
+ Color fg; /* foreground */
+ Color bg; /* background */
+ char state; /* state flag */
+} Glyph;
+
+typedef Glyph* Line;
+
+typedef struct {
+ Glyph attr; /* current char attributes */
+ char hidden;
+ int x;
+ int y;
+} TCursor;
+
+/* Escape sequence structs */
+/* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
+typedef struct {
+ char buf[ESCSIZ+1]; /* raw string */
+ int len; /* raw string length */
+ char pre;
+ char priv;
+ int arg[ESCARG+1];
+ int narg; /* nb of args */
+ char mode;
+} Escseq;
+
+/* Internal representation of the screen */
+typedef struct {
+ int row; /* nb row */
+ int col; /* nb col */
+ Line* line; /* screen */
+ TCursor c; /* cursor */
+ int top; /* top scroll limit */
+ int bot; /* bottom scroll limit */
+ int mode; /* terminal mode */
+} Term;
+
+/* Purely graphic info */
+typedef struct {
+ Display* dis;
+ Window win;
+ int scr;
+ int w; /* window width */
+ int h; /* window height */
+ int ch; /* char height */
+ int cw; /* char width */
+} XWindow;
+
+typedef struct {
+ KeySym k;
+ char s[ESCSIZ];
+} Key;
+
+#include "config.h"
+
+/* Drawing Context */
+typedef struct {
+ unsigned long col[LEN(colorname)];
+ XFontStruct* font;
+ GC gc;
+} DC;
+
+static void die(const char *errstr, ...);
+static void draw(int);
+static void execsh(void);
+static void sigchld(int);
+static void run(void);
+
+static int escaddc(char);
+static int escfinal(char);
+static void escdump(void);
+static void eschandle(void);
+static void escparse(void);
+static void escreset(void);
+
+static void tclearregion(int, int, int, int);
+static void tcpos(int);
+static void tcursor(int);
+static void tdeletechar(int);
+static void tdeleteline(int);
+static void tinsertblank(int);
+static void tinsertblankline(int);
+static void tmoveto(int, int);
+static void tnew(int, int);
+static void tnewline(void);
+static void tputc(char);
+static void tputs(char*, int);
+static void tresize(int, int);
+static void tscroll(void);
+static void tsetattr(int*, int);
+static void tsetchar(char);
+static void tsetscroll(int, int);
+
+static void ttynew(void);
+static void ttyread(void);
+static void ttyresize(int, int);
+static void ttywrite(const char *, size_t);
+
+static unsigned long xgetcol(const char *);
+static void xclear(int, int, int, int);
+static void xcursor(int);
+static void xdrawc(int, int, Glyph);
+static void xinit(void);
+static void xscroll(void);
+
+static void expose(XEvent *);
+static char * kmap(KeySym);
+static void kpress(XEvent *);
+static void resize(XEvent *);
+
+static void (*handler[LASTEvent])(XEvent *) = {
+ [KeyPress] = kpress,
+ [Expose] = expose,
+ [ConfigureNotify] = resize
+};