+/* See LICENSE for licence details. */
+#define _XOPEN_SOURCE 600
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.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/Xatom.h>
+#include <X11/keysym.h>
+#include <X11/Xutil.h>
+
+#if defined(__linux)
+ #include <pty.h>
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
+ #include <util.h>
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+ #include <libutil.h>
+#endif
+
+#define USAGE \
+ "st-" VERSION ", (c) 2010 st engineers\n" \
+ "usage: st [-t title] [-e cmd] [-v]\n"
+
+/* Arbitrary sizes */
+#define ESC_TITLE_SIZ 256
+#define ESC_BUF_SIZ 256
+#define ESC_ARG_SIZ 16
+#define DRAW_BUF_SIZ 1024
+
+#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)
+#define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
+#define IS_SET(flag) (term.mode & (flag))
+
+/* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
+enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
+enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
+ CURSOR_SAVE, CURSOR_LOAD };
+enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
+enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
+enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
+ MODE_CRLF=16 };
+enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
+enum { SCREEN_UPDATE, SCREEN_REDRAW };
+enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
+
+typedef struct {
+ char c; /* character code */
+ char mode; /* attribute flags */
+ int fg; /* foreground */
+ int bg; /* background */
+ char state; /* state flags */
+} Glyph;
+
+typedef Glyph* Line;
+
+typedef struct {
+ Glyph attr; /* current char attributes */
+ int x;
+ int y;
+ char state;
+} TCursor;
+
+/* CSI Escape sequence structs */
+/* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
+typedef struct {
+ char buf[ESC_BUF_SIZ]; /* raw string */
+ int len; /* raw string length */
+ char priv;
+ int arg[ESC_ARG_SIZ];
+ int narg; /* nb of args */
+ char mode;
+} CSIEscape;
+
+/* Internal representation of the screen */
+typedef struct {
+ int row; /* nb row */
+ int col; /* nb col */
+ Line* line; /* screen */
+ Line* alt; /* alternate screen */
+ TCursor c; /* cursor */
+ int top; /* top scroll limit */
+ int bot; /* bottom scroll limit */
+ int mode; /* terminal mode flags */
+ int esc; /* escape state flags */
+ char title[ESC_TITLE_SIZ];
+ int titlelen;
+} Term;
+
+/* Purely graphic info */
+typedef struct {
+ Display* dis;
+ Colormap cmap;
+ Window win;
+ Pixmap buf;
+ XIM xim;
+ XIC xic;
+ int scr;
+ int w; /* window width */
+ int h; /* window height */
+ int bufw; /* pixmap width */
+ int bufh; /* pixmap height */
+ int ch; /* char height */
+ int cw; /* char width */
+ char state; /* focus, redraw, visible */
+} XWindow;
+
+typedef struct {
+ KeySym k;
+ char s[ESC_BUF_SIZ];
+} Key;
+
+/* Drawing Context */
+typedef struct {
+ unsigned long col[256];
+ XFontStruct* font;
+ XFontStruct* bfont;
+ GC gc;
+} DC;
+
+/* TODO: use better name for vars... */
+typedef struct {
+ int mode;
+ int bx, by;
+ int ex, ey;
+ struct {int x, y;} b, e;
+ char *clip;
+} Selection;
+
+#include "config.h"
+
+static void die(const char *errstr, ...);
+static void draw(int);
+static void execsh(void);
+static void sigchld(int);
+static void run(void);
+
+static void csidump(void);
+static void csihandle(void);
+static void csiparse(void);
+static void csireset(void);
+
+static void tclearregion(int, int, int, 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(int);
+static void tputtab(void);
+static void tputc(char);
+static void tputs(char*, int);
+static void treset(void);
+static void tresize(int, int);
+static void tscrollup(int, int);
+static void tscrolldown(int, int);
+static void tsetattr(int*, int);
+static void tsetchar(char);
+static void tsetscroll(int, int);
+static void tswapscreen(void);
+
+static void ttynew(void);
+static void ttyread(void);
+static void ttyresize(int, int);
+static void ttywrite(const char *, size_t);
+
+static void xdraws(char *, Glyph, int, int, int);
+static void xhints(void);
+static void xclear(int, int, int, int);
+static void xdrawcursor(void);
+static void xinit(void);
+static void xloadcols(void);
+static void xseturgency(int);
+
+static void expose(XEvent *);
+static void visibility(XEvent *);
+static void unmap(XEvent *);
+static char* kmap(KeySym);
+static void kpress(XEvent *);
+static void resize(XEvent *);
+static void focus(XEvent *);
+static void brelease(XEvent *);
+static void bpress(XEvent *);
+static void bmotion(XEvent *);
+static void selection_notify(XEvent *);
+static void selection_request(XEvent *);
+
+static void (*handler[LASTEvent])(XEvent *) = {
+ [KeyPress] = kpress,
+ [ConfigureNotify] = resize,
+ [VisibilityNotify] = visibility,
+ [UnmapNotify] = unmap,
+ [Expose] = expose,
+ [FocusIn] = focus,
+ [FocusOut] = focus,
+ [MotionNotify] = bmotion,
+ [ButtonPress] = bpress,
+ [ButtonRelease] = brelease,
+ [SelectionNotify] = selection_notify,
+ [SelectionRequest] = selection_request,
+};