/* 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/keysym.h>
#include <X11/Xutil.h>
#define TNAME "xterm"
/* Arbitrary sizes */
#define TITLESIZ 256
#define ESCSIZ 256
#define ESCARGSIZ 16
#define MAXDRAWBUF 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)
/* 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, TMtitle=4 };
enum { ESCin = 1, ESCcsi = 2, ESCosc = 4, ESCtitle = 8 };
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;
/* CSI Escape sequence structs */
/* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
typedef struct {
char buf[ESCSIZ]; /* raw string */
int len; /* raw string length */
char priv;
int arg[ESCARGSIZ];
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 */
TCursor c; /* cursor */
int top; /* top scroll limit */
int bot; /* bottom scroll limit */
int mode; /* terminal mode */
int esc;
char title[TITLESIZ];
int titlelen;
} 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 void csidump(void);
static void csihandle(void);
static void csiparse(void);
static void csireset(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
};
/* Globals */
static DC dc;
static XWindow xw;
static Term term;
static CSIEscape escseq;
static int cmdfd;
static pid_t pid;
static int running;
#ifdef DEBUG
void
tdump(void) {
int row, col;
Glyph c;
for(row = 0; row < term.row; row++) {
for(col = 0; col < term.col; col++) {
if(col == term.c.x && row == term.c.y)
putchar('#');
else {
c = term.line[row][col];
putchar(c.state & CRset ? c.c : '.');
}
}
putchar('\n');
}
}
#endif
<
|