Initial revision
diff --git a/include/bio.h b/include/bio.h
new file mode 100644
index 0000000..c481239
--- /dev/null
+++ b/include/bio.h
@@ -0,0 +1,79 @@
+#ifndef _BIOH_
+#define _BIOH_ 1
+
+#include <sys/types.h>	/* for off_t */
+#include <fcntl.h>	/* for O_RDONLY, O_WRONLY */
+
+typedef	struct	Biobuf	Biobuf;
+
+enum
+{
+	Bsize		= 8*1024,
+	Bungetsize	= 4,		/* space for ungetc */
+	Bmagic		= 0x314159,
+	Beof		= -1,
+	Bbad		= -2,
+
+	Binactive	= 0,		/* states */
+	Bractive,
+	Bwactive,
+	Bracteof,
+
+	Bend
+};
+
+struct	Biobuf
+{
+	int	icount;		/* neg num of bytes at eob */
+	int	ocount;		/* num of bytes at bob */
+	int	rdline;		/* num of bytes after rdline */
+	int	runesize;		/* num of bytes of last getrune */
+	int	state;		/* r/w/inactive */
+	int	fid;		/* open file */
+	int	flag;		/* magic if malloc'ed */
+	off_t	offset;		/* offset of buffer in file */
+	int	bsize;		/* size of buffer */
+	unsigned char*	bbuf;		/* pointer to beginning of buffer */
+	unsigned char*	ebuf;		/* pointer to end of buffer */
+	unsigned char*	gbuf;		/* pointer to good data in buf */
+	unsigned char	b[Bungetsize+Bsize];
+};
+
+#define	BGETC(bp)\
+	((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))
+#define	BPUTC(bp,c)\
+	((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))
+#define	BOFFSET(bp)\
+	(((bp)->state==Bractive)?\
+		(bp)->offset + (bp)->icount:\
+	(((bp)->state==Bwactive)?\
+		(bp)->offset + ((bp)->bsize + (bp)->ocount):\
+		-1))
+#define	BLINELEN(bp)\
+	(bp)->rdline
+#define	BFILDES(bp)\
+	(bp)->fid
+
+int	Bbuffered(Biobuf*);
+int	Bfildes(Biobuf*);
+int	Bflush(Biobuf*);
+int	Bgetc(Biobuf*);
+int	Bgetd(Biobuf*, double*);
+int	Binit(Biobuf*, int, int);
+int	Binits(Biobuf*, int, int, unsigned char*, int);
+int	Blinelen(Biobuf*);
+off_t	Boffset(Biobuf*);
+Biobuf*	Bopen(char*, int);
+int	Bprint(Biobuf*, char*, ...);
+int	Bputc(Biobuf*, int);
+void*	Brdline(Biobuf*, int);
+long	Bread(Biobuf*, void*, long);
+off_t	Bseek(Biobuf*, off_t, int);
+int	Bterm(Biobuf*);
+int	Bungetc(Biobuf*);
+long	Bwrite(Biobuf*, void*, long);
+char*	Brdstr(Biobuf*, int, int);
+long	Bgetrune(Biobuf*);
+int	Bputrune(Biobuf*, long);
+
+#endif
diff --git a/include/cursor.h b/include/cursor.h
new file mode 100644
index 0000000..105cd0e
--- /dev/null
+++ b/include/cursor.h
@@ -0,0 +1,7 @@
+typedef struct Cursor Cursor;
+struct	Cursor
+{
+	Point	offset;
+	uchar	clr[2*16];
+	uchar	set[2*16];
+};
diff --git a/include/draw.h b/include/draw.h
new file mode 100644
index 0000000..0f9ba63
--- /dev/null
+++ b/include/draw.h
@@ -0,0 +1,520 @@
+typedef struct	Cachefont Cachefont;
+typedef struct	Cacheinfo Cacheinfo;
+typedef struct	Cachesubf Cachesubf;
+typedef struct	Display Display;
+typedef struct	Font Font;
+typedef struct	Fontchar Fontchar;
+typedef struct	Image Image;
+typedef struct	Mouse Mouse;
+typedef struct	Point Point;
+typedef struct	Rectangle Rectangle;
+typedef struct	RGB RGB;
+typedef struct	Screen Screen;
+typedef struct	Subfont Subfont;
+
+extern	int	Rfmt(Fmt*);
+extern	int	Pfmt(Fmt*);
+
+enum
+{
+	DOpaque		= 0xFFFFFFFF,
+	DTransparent	= 0x00000000,		/* only useful for allocimage, memfillcolor */
+	DBlack		= 0x000000FF,
+	DWhite		= 0xFFFFFFFF,
+	DRed		= 0xFF0000FF,
+	DGreen		= 0x00FF00FF,
+	DBlue		= 0x0000FFFF,
+	DCyan		= 0x00FFFFFF,
+	DMagenta		= 0xFF00FFFF,
+	DYellow		= 0xFFFF00FF,
+	DPaleyellow	= 0xFFFFAAFF,
+	DDarkyellow	= 0xEEEE9EFF,
+	DDarkgreen	= 0x448844FF,
+	DPalegreen	= 0xAAFFAAFF,
+	DMedgreen	= 0x88CC88FF,
+	DDarkblue	= 0x000055FF,
+	DPalebluegreen= 0xAAFFFFFF,
+	DPaleblue		= 0x0000BBFF,
+	DBluegreen	= 0x008888FF,
+	DGreygreen	= 0x55AAAAFF,
+	DPalegreygreen	= 0x9EEEEEFF,
+	DYellowgreen	= 0x99994CFF,
+	DMedblue		= 0x000099FF,
+	DGreyblue	= 0x005DBBFF,
+	DPalegreyblue	= 0x4993DDFF,
+	DPurpleblue	= 0x8888CCFF,
+
+	DNotacolor	= 0xFFFFFF00,
+	DNofill		= DNotacolor,
+	
+};
+
+enum
+{
+	Displaybufsize	= 8000,
+	ICOSSCALE	= 1024,
+	Borderwidth =	4,
+};
+
+enum
+{
+	/* refresh methods */
+	Refbackup	= 0,
+	Refnone		= 1,
+	Refmesg		= 2
+};
+#define	NOREFRESH	((void*)-1)
+
+enum
+{
+	/* line ends */
+	Endsquare	= 0,
+	Enddisc		= 1,
+	Endarrow	= 2,
+	Endmask		= 0x1F
+};
+
+#define	ARROW(a, b, c)	(Endarrow|((a)<<5)|((b)<<14)|((c)<<23))
+
+typedef enum
+{
+	/* Porter-Duff compositing operators */
+	Clear	= 0,
+
+	SinD	= 8,
+	DinS	= 4,
+	SoutD	= 2,
+	DoutS	= 1,
+
+	S		= SinD|SoutD,
+	SoverD	= SinD|SoutD|DoutS,
+	SatopD	= SinD|DoutS,
+	SxorD	= SoutD|DoutS,
+
+	D		= DinS|DoutS,
+	DoverS	= DinS|DoutS|SoutD,
+	DatopS	= DinS|SoutD,
+	DxorS	= DoutS|SoutD,	/* == SxorD */
+
+	Ncomp = 12,
+} Drawop;
+
+/*
+ * image channel descriptors 
+ */
+enum {
+	CRed = 0,
+	CGreen,
+	CBlue,
+	CGrey,
+	CAlpha,
+	CMap,
+	CIgnore,
+	NChan,
+};
+
+#define __DC(type, nbits)	((((type)&15)<<4)|((nbits)&15))
+#define CHAN1(a,b)	__DC(a,b)
+#define CHAN2(a,b,c,d)	(CHAN1((a),(b))<<8|__DC((c),(d)))
+#define CHAN3(a,b,c,d,e,f)	(CHAN2((a),(b),(c),(d))<<8|__DC((e),(f)))
+#define CHAN4(a,b,c,d,e,f,g,h)	(CHAN3((a),(b),(c),(d),(e),(f))<<8|__DC((g),(h)))
+
+#define NBITS(c) ((c)&15)
+#define TYPE(c) (((c)>>4)&15)
+
+enum {
+	GREY1	= CHAN1(CGrey, 1),
+	GREY2	= CHAN1(CGrey, 2),
+	GREY4	= CHAN1(CGrey, 4),
+	GREY8	= CHAN1(CGrey, 8),
+	CMAP8	= CHAN1(CMap, 8),
+	RGB15	= CHAN4(CIgnore, 1, CRed, 5, CGreen, 5, CBlue, 5),
+	RGB16	= CHAN3(CRed, 5, CGreen, 6, CBlue, 5),
+	RGB24	= CHAN3(CRed, 8, CGreen, 8, CBlue, 8),
+	BGR24	= CHAN3(CBlue, 8, CGreen, 8, CRed, 8),
+	RGBA32	= CHAN4(CRed, 8, CGreen, 8, CBlue, 8, CAlpha, 8),
+	ARGB32	= CHAN4(CAlpha, 8, CRed, 8, CGreen, 8, CBlue, 8),	/* stupid VGAs */
+	XRGB32  = CHAN4(CIgnore, 8, CRed, 8, CGreen, 8, CBlue, 8),
+	XBGR32  = CHAN4(CIgnore, 8, CBlue, 8, CGreen, 8, CRed, 8),
+};
+
+extern	char*	chantostr(char*, u32int);
+extern	u32int	strtochan(char*);
+extern	int		chantodepth(u32int);
+
+struct	Point
+{
+	int	x;
+	int	y;
+};
+
+struct Rectangle
+{
+	Point	min;
+	Point	max;
+};
+
+typedef void	(*Reffn)(Image*, Rectangle, void*);
+
+struct Screen
+{
+	Display	*display;	/* display holding data */
+	int	id;		/* id of system-held Screen */
+	Image	*image;		/* unused; for reference only */
+	Image	*fill;		/* color to paint behind windows */
+};
+
+struct Display
+{
+	QLock		qlock;
+	int		locking;	/*program is using lockdisplay */
+	int		dirno;
+	int		imageid;
+	int		local;
+	void		(*error)(Display*, char*);
+	char		*devdir;
+	char		*windir;
+	char		oldlabel[64];
+	u32int		dataqid;
+	Image		*image;
+	Image		*white;
+	Image		*black;
+	Image		*opaque;
+	Image		*transparent;
+	uchar		*buf;
+	int		bufsize;
+	uchar		*bufp;
+	uchar		*obuf;
+	int		obufsize;
+	uchar		*obufp;
+	Font		*defaultfont;
+	Subfont		*defaultsubfont;
+	Image		*windows;
+	Image		*screenimage;
+	int		_isnewdisplay;
+};
+
+struct Image
+{
+	Display		*display;	/* display holding data */
+	int		id;		/* id of system-held Image */
+	Rectangle	r;		/* rectangle in data area, local coords */
+	Rectangle 	clipr;		/* clipping region */
+	int		depth;		/* number of bits per pixel */
+	u32int	chan;
+	int		repl;		/* flag: data replicates to tile clipr */
+	Screen		*screen;	/* 0 if not a window */
+	Image		*next;	/* next in list of windows */
+};
+
+struct RGB
+{
+	u32int	red;
+	u32int	green;
+	u32int	blue;
+};
+
+/*
+ * Subfonts
+ *
+ * given char c, Subfont *f, Fontchar *i, and Point p, one says
+ *	i = f->info+c;
+ *	draw(b, Rect(p.x+i->left, p.y+i->top,
+ *		p.x+i->left+((i+1)->x-i->x), p.y+i->bottom),
+ *		color, f->bits, Pt(i->x, i->top));
+ *	p.x += i->width;
+ * to draw characters in the specified color (itself an Image) in Image b.
+ */
+
+struct	Fontchar
+{
+	int		x;		/* left edge of bits */
+	uchar		top;		/* first non-zero scan-line */
+	uchar		bottom;		/* last non-zero scan-line + 1 */
+	char		left;		/* offset of baseline */
+	uchar		width;		/* width of baseline */
+};
+
+struct	Subfont
+{
+	char		*name;
+	short		n;		/* number of chars in font */
+	uchar		height;		/* height of image */
+	char		ascent;		/* top of image to baseline */
+	Fontchar 	*info;		/* n+1 character descriptors */
+	Image		*bits;		/* of font */
+	int		ref;
+};
+
+enum
+{
+	/* starting values */
+	LOG2NFCACHE =	6,
+	NFCACHE =	(1<<LOG2NFCACHE),	/* #chars cached */
+	NFLOOK =	5,			/* #chars to scan in cache */
+	NFSUBF =	2,			/* #subfonts to cache */
+	/* max value */
+	MAXFCACHE =	1024+NFLOOK,		/* upper limit */
+	MAXSUBF =	50,			/* generous upper limit */
+	/* deltas */
+	DSUBF = 	4,
+	/* expiry ages */
+	SUBFAGE	=	10000,
+	CACHEAGE =	10000
+};
+
+struct Cachefont
+{
+	Rune		min;	/* lowest rune value to be taken from subfont */
+	Rune		max;	/* highest rune value+1 to be taken from subfont */
+	int		offset;	/* position in subfont of character at min */
+	char		*name;			/* stored in font */
+	char		*subfontname;		/* to access subfont */
+};
+
+struct Cacheinfo
+{
+	ushort		x;		/* left edge of bits */
+	uchar		width;		/* width of baseline */
+	schar		left;		/* offset of baseline */
+	Rune		value;	/* value of character at this slot in cache */
+	ushort		age;
+};
+
+struct Cachesubf
+{
+	u32int		age;	/* for replacement */
+	Cachefont	*cf;	/* font info that owns us */
+	Subfont		*f;	/* attached subfont */
+};
+
+struct Font
+{
+	char		*name;
+	Display		*display;
+	short		height;	/* max height of image, interline spacing */
+	short		ascent;	/* top of image to baseline */
+	short		width;	/* widest so far; used in caching only */	
+	short		nsub;	/* number of subfonts */
+	u32int		age;	/* increasing counter; used for LRU */
+	int		maxdepth;	/* maximum depth of all loaded subfonts */
+	int		ncache;	/* size of cache */
+	int		nsubf;	/* size of subfont list */
+	Cacheinfo	*cache;
+	Cachesubf	*subf;
+	Cachefont	**sub;	/* as read from file */
+	Image		*cacheimage;
+};
+
+#define	Dx(r)	((r).max.x-(r).min.x)
+#define	Dy(r)	((r).max.y-(r).min.y)
+
+/*
+ * Image management
+ */
+extern Image*	_allocimage(Image*, Display*, Rectangle, u32int, int, u32int, int, int);
+extern Image*	allocimage(Display*, Rectangle, u32int, int, u32int);
+extern uchar*	bufimage(Display*, int);
+extern int	bytesperline(Rectangle, int);
+extern void	closedisplay(Display*);
+extern void	drawerror(Display*, char*);
+extern int	flushimage(Display*, int);
+extern int	freeimage(Image*);
+extern int	_freeimage1(Image*);
+extern int	geninitdraw(char*, void(*)(Display*, char*), char*, char*, char*, int);
+extern int	initdraw(void(*)(Display*, char*), char*, char*);
+extern int	newwindow(char*);
+extern int	loadimage(Image*, Rectangle, uchar*, int);
+extern int	cloadimage(Image*, Rectangle, uchar*, int);
+extern int	getwindow(Display*, int);
+extern int	gengetwindow(Display*, char*, Image**, Screen**, int);
+extern Image* readimage(Display*, int, int);
+extern Image* creadimage(Display*, int, int);
+extern int	unloadimage(Image*, Rectangle, uchar*, int);
+extern int	wordsperline(Rectangle, int);
+extern int	writeimage(int, Image*, int);
+extern Image*	namedimage(Display*, char*);
+extern int	nameimage(Image*, char*, int);
+extern Image* allocimagemix(Display*, u32int, u32int);
+
+/*
+ * Colors
+ */
+extern	void	readcolmap(Display*, RGB*);
+extern	void	writecolmap(Display*, RGB*);
+extern	u32int	setalpha(u32int, uchar);
+
+/*
+ * Windows
+ */
+extern Screen*	allocscreen(Image*, Image*, int);
+extern Image*	_allocwindow(Image*, Screen*, Rectangle, int, u32int);
+extern Image*	allocwindow(Screen*, Rectangle, int, u32int);
+extern void	bottomnwindows(Image**, int);
+extern void	bottomwindow(Image*);
+extern int	freescreen(Screen*);
+extern Screen*	publicscreen(Display*, int, u32int);
+extern void	topnwindows(Image**, int);
+extern void	topwindow(Image*);
+extern int	originwindow(Image*, Point, Point);
+
+/*
+ * Geometry
+ */
+extern Point		Pt(int, int);
+extern Rectangle	Rect(int, int, int, int);
+extern Rectangle	Rpt(Point, Point);
+extern Point		addpt(Point, Point);
+extern Point		subpt(Point, Point);
+extern Point		divpt(Point, int);
+extern Point		mulpt(Point, int);
+extern int		eqpt(Point, Point);
+extern int		eqrect(Rectangle, Rectangle);
+extern Rectangle	insetrect(Rectangle, int);
+extern Rectangle	rectaddpt(Rectangle, Point);
+extern Rectangle	rectsubpt(Rectangle, Point);
+extern Rectangle	canonrect(Rectangle);
+extern int		rectXrect(Rectangle, Rectangle);
+extern int		rectinrect(Rectangle, Rectangle);
+extern void		combinerect(Rectangle*, Rectangle);
+extern int		rectclip(Rectangle*, Rectangle);
+extern int		ptinrect(Point, Rectangle);
+extern void		replclipr(Image*, int, Rectangle);
+extern int		drawreplxy(int, int, int);	/* used to be drawsetxy */
+extern Point	drawrepl(Rectangle, Point);
+extern int		rgb2cmap(int, int, int);
+extern int		cmap2rgb(int);
+extern int		cmap2rgba(int);
+extern void		icossin(int, int*, int*);
+extern void		icossin2(int, int, int*, int*);
+
+/*
+ * Graphics
+ */
+extern void	draw(Image*, Rectangle, Image*, Image*, Point);
+extern void	drawop(Image*, Rectangle, Image*, Image*, Point, Drawop);
+extern void	gendraw(Image*, Rectangle, Image*, Point, Image*, Point);
+extern void	gendrawop(Image*, Rectangle, Image*, Point, Image*, Point, Drawop);
+extern void	line(Image*, Point, Point, int, int, int, Image*, Point);
+extern void	lineop(Image*, Point, Point, int, int, int, Image*, Point, Drawop);
+extern void	poly(Image*, Point*, int, int, int, int, Image*, Point);
+extern void	polyop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
+extern void	fillpoly(Image*, Point*, int, int, Image*, Point);
+extern void	fillpolyop(Image*, Point*, int, int, Image*, Point, Drawop);
+extern Point	string(Image*, Point, Image*, Point, Font*, char*);
+extern Point	stringop(Image*, Point, Image*, Point, Font*, char*, Drawop);
+extern Point	stringn(Image*, Point, Image*, Point, Font*, char*, int);
+extern Point	stringnop(Image*, Point, Image*, Point, Font*, char*, int, Drawop);
+extern Point	runestring(Image*, Point, Image*, Point, Font*, Rune*);
+extern Point	runestringop(Image*, Point, Image*, Point, Font*, Rune*, Drawop);
+extern Point	runestringn(Image*, Point, Image*, Point, Font*, Rune*, int);
+extern Point	runestringnop(Image*, Point, Image*, Point, Font*, Rune*, int, Drawop);
+extern Point	stringbg(Image*, Point, Image*, Point, Font*, char*, Image*, Point);
+extern Point	stringbgop(Image*, Point, Image*, Point, Font*, char*, Image*, Point, Drawop);
+extern Point	stringnbg(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point);
+extern Point	stringnbgop(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point, Drawop);
+extern Point	runestringbg(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point);
+extern Point	runestringbgop(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point, Drawop);
+extern Point	runestringnbg(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point);
+extern Point	runestringnbgop(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point, Drawop);
+extern Point	_string(Image*, Point, Image*, Point, Font*, char*, Rune*, int, Rectangle, Image*, Point, Drawop);
+extern Point	stringsubfont(Image*, Point, Image*, Subfont*, char*);
+extern int		bezier(Image*, Point, Point, Point, Point, int, int, int, Image*, Point);
+extern int		bezierop(Image*, Point, Point, Point, Point, int, int, int, Image*, Point, Drawop);
+extern int		bezspline(Image*, Point*, int, int, int, int, Image*, Point);
+extern int		bezsplineop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
+extern int		bezsplinepts(Point*, int, Point**);
+extern int		fillbezier(Image*, Point, Point, Point, Point, int, Image*, Point);
+extern int		fillbezierop(Image*, Point, Point, Point, Point, int, Image*, Point, Drawop);
+extern int		fillbezspline(Image*, Point*, int, int, Image*, Point);
+extern int		fillbezsplineop(Image*, Point*, int, int, Image*, Point, Drawop);
+extern void	ellipse(Image*, Point, int, int, int, Image*, Point);
+extern void	ellipseop(Image*, Point, int, int, int, Image*, Point, Drawop);
+extern void	fillellipse(Image*, Point, int, int, Image*, Point);
+extern void	fillellipseop(Image*, Point, int, int, Image*, Point, Drawop);
+extern void	arc(Image*, Point, int, int, int, Image*, Point, int, int);
+extern void	arcop(Image*, Point, int, int, int, Image*, Point, int, int, Drawop);
+extern void	fillarc(Image*, Point, int, int, Image*, Point, int, int);
+extern void	fillarcop(Image*, Point, int, int, Image*, Point, int, int, Drawop);
+extern void	border(Image*, Rectangle, int, Image*, Point);
+extern void	borderop(Image*, Rectangle, int, Image*, Point, Drawop);
+
+/*
+ * Font management
+ */
+extern Font*	openfont(Display*, char*);
+extern Font*	buildfont(Display*, char*, char*);
+extern void	freefont(Font*);
+extern Font*	mkfont(Subfont*, Rune);
+extern int	cachechars(Font*, char**, Rune**, ushort*, int, int*, char**);
+extern void	agefont(Font*);
+extern Subfont*	allocsubfont(char*, int, int, int, Fontchar*, Image*);
+extern Subfont*	lookupsubfont(Display*, char*);
+extern void	installsubfont(char*, Subfont*);
+extern void	uninstallsubfont(Subfont*);
+extern void	freesubfont(Subfont*);
+extern Subfont*	readsubfont(Display*, char*, int, int);
+extern Subfont*	readsubfonti(Display*, char*, int, Image*, int);
+extern int	writesubfont(int, Subfont*);
+extern void	_unpackinfo(Fontchar*, uchar*, int);
+extern Point	stringsize(Font*, char*);
+extern int	stringwidth(Font*, char*);
+extern int	stringnwidth(Font*, char*, int);
+extern Point	runestringsize(Font*, Rune*);
+extern int	runestringwidth(Font*, Rune*);
+extern int	runestringnwidth(Font*, Rune*, int);
+extern Point	strsubfontwidth(Subfont*, char*);
+extern int	loadchar(Font*, Rune, Cacheinfo*, int, int, char**);
+extern char*	subfontname(char*, char*, int);
+extern Subfont*	_getsubfont(Display*, char*);
+extern Subfont*	getdefont(Display*);
+extern void		lockdisplay(Display*);
+extern void	unlockdisplay(Display*);
+extern int		drawlsetrefresh(u32int, int, void*, void*);
+
+/*
+ * Predefined 
+ */
+extern	uchar	defontdata[];
+extern	int		sizeofdefont;
+extern	Point		ZP;
+extern	Rectangle	ZR;
+
+/*
+ * Set up by initdraw()
+ */
+extern	Display	*display;
+extern	Font		*font;
+extern	Image	*screen;
+extern	Screen	*_screen;
+extern	int	_cursorfd;
+extern	int	_drawdebug;	/* set to 1 to see errors from flushimage */
+extern	void	_setdrawop(Display*, Drawop);
+extern	Display	*_initdisplay(void(*)(Display*,char*), char*);
+
+#define	BGSHORT(p)		(((p)[0]<<0) | ((p)[1]<<8))
+#define	BGLONG(p)		((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
+#define	BPSHORT(p, v)		((p)[0]=(v), (p)[1]=((v)>>8))
+#define	BPLONG(p, v)		(BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
+
+/*
+ * Compressed image file parameters and helper routines
+ */
+#define	NMATCH	3		/* shortest match possible */
+#define	NRUN	(NMATCH+31)	/* longest match possible */
+#define	NMEM	1024		/* window size */
+#define	NDUMP	128		/* maximum length of dump */
+#define	NCBLOCK	6000		/* size of compressed blocks */
+extern	void	_twiddlecompressed(uchar*, int);
+extern	int	_compblocksize(Rectangle, int);
+
+/* XXX backwards helps; should go */
+extern	int		log2[];
+extern	u32int	drawld2chan[];
+extern	void		drawsetdebug(int);
+
+/*
+ * Port magic.
+ */
+int	_drawmsgread(Display*, void*, int);
+int	_drawmsgwrite(Display*, void*, int);
diff --git a/include/event.h b/include/event.h
new file mode 100644
index 0000000..e74183d
--- /dev/null
+++ b/include/event.h
@@ -0,0 +1,63 @@
+typedef struct	Event Event;
+typedef struct	Menu Menu;
+
+enum
+{
+	Emouse		= 1,
+	Ekeyboard	= 2,
+};
+
+enum
+{
+	MAXSLAVE = 32,
+	EMAXMSG = 128+8192,	/* size of 9p header+data */
+};
+
+struct	Mouse
+{
+	int	buttons;	/* bit array: LMR=124 */
+	Point	xy;
+	ulong	msec;
+};
+
+struct	Event
+{
+	int	kbdc;
+	Mouse	mouse;
+	int	n;		/* number of characters in message */
+	void	*v;		/* data unpacked by general event-handling function */
+	uchar	data[EMAXMSG];	/* message from an arbitrary file descriptor */
+};
+
+struct Menu
+{
+	char	**item;
+	char	*(*gen)(int);
+	int	lasthit;
+};
+
+/*
+ * Events
+ */
+extern void	 einit(ulong);
+extern ulong	 estart(ulong, int, int);
+extern ulong	 estartfn(ulong, int, int, int (*fn)(int, Event*, uchar*, int));
+extern ulong	 etimer(ulong, int);
+extern ulong	 event(Event*);
+extern ulong	 eread(ulong, Event*);
+extern Mouse	 emouse(void);
+extern int	 ekbd(void);
+extern int	 ecanread(ulong);
+extern int	 ecanmouse(void);
+extern int	 ecankbd(void);
+extern void	 eresized(int);	/* supplied by user */
+extern int	 emenuhit(int, Mouse*, Menu*);
+extern int	eatomouse(Mouse*, char*, int);
+extern Rectangle	getrect(int, Mouse*);
+struct Cursor;
+extern void	 esetcursor(struct Cursor*);
+extern void	 emoveto(Point);
+extern Rectangle	egetrect(int, Mouse*);
+extern void		edrawgetrect(Rectangle, int);
+extern int		ereadmouse(Mouse*);
+extern int		eatomouse(Mouse*, char*, int);
diff --git a/include/fmt.h b/include/fmt.h
new file mode 100644
index 0000000..c913e14
--- /dev/null
+++ b/include/fmt.h
@@ -0,0 +1,100 @@
+
+/*
+ * The authors of this software are Rob Pike and Ken Thompson.
+ *              Copyright (c) 2002 by Lucent Technologies.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose without fee is hereby granted, provided that this entire notice
+ * is included in all copies of any software which is or includes a copy
+ * or modification of this software and in all copies of the supporting
+ * documentation for such software.
+ * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
+ * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
+ * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
+ */
+
+#ifndef _FMTH_
+#define _FMTH_ 1
+
+#include <stdarg.h>
+
+#ifndef _UTFH_
+#include <utf.h>
+#endif
+
+typedef struct Fmt	Fmt;
+struct Fmt{
+	unsigned char	runes;		/* output buffer is runes or chars? */
+	void	*start;			/* of buffer */
+	void	*to;			/* current place in the buffer */
+	void	*stop;			/* end of the buffer; overwritten if flush fails */
+	int	(*flush)(Fmt *);	/* called when to == stop */
+	void	*farg;			/* to make flush a closure */
+	int	nfmt;			/* num chars formatted so far */
+	va_list	args;			/* args passed to dofmt */
+	int	r;			/* % format Rune */
+	int	width;
+	int	prec;
+	unsigned long	flags;
+};
+
+enum{
+	FmtWidth	= 1,
+	FmtLeft		= FmtWidth << 1,
+	FmtPrec		= FmtLeft << 1,
+	FmtSharp	= FmtPrec << 1,
+	FmtSpace	= FmtSharp << 1,
+	FmtSign		= FmtSpace << 1,
+	FmtZero		= FmtSign << 1,
+	FmtUnsigned	= FmtZero << 1,
+	FmtShort	= FmtUnsigned << 1,
+	FmtLong		= FmtShort << 1,
+	FmtVLong	= FmtLong << 1,
+	FmtComma	= FmtVLong << 1,
+	FmtByte		= FmtComma << 1,
+	FmtLDouble	= FmtByte << 1,
+
+	FmtFlag		= FmtLDouble << 1
+};
+
+extern	int	print(char*, ...);
+extern	char*	seprint(char*, char*, char*, ...);
+extern	char*	vseprint(char*, char*, char*, va_list);
+extern	int	snprint(char*, int, char*, ...);
+extern	int	vsnprint(char*, int, char*, va_list);
+extern	char*	smprint(char*, ...);
+extern	char*	vsmprint(char*, va_list);
+extern	int	sprint(char*, char*, ...);
+extern	int	fprint(int, char*, ...);
+extern	int	vfprint(int, char*, va_list);
+
+extern	int	runesprint(Rune*, char*, ...);
+extern	int	runesnprint(Rune*, int, char*, ...);
+extern	int	runevsnprint(Rune*, int, char*, va_list);
+extern	Rune*	runeseprint(Rune*, Rune*, char*, ...);
+extern	Rune*	runevseprint(Rune*, Rune*, char*, va_list);
+extern	Rune*	runesmprint(char*, ...);
+extern	Rune*	runevsmprint(char*, va_list);
+
+extern	int	fmtfdinit(Fmt*, int, char*, int);
+extern	int	fmtfdflush(Fmt*);
+extern	int	fmtstrinit(Fmt*);
+extern	char*	fmtstrflush(Fmt*);
+extern	int	runefmtstrinit(Fmt*);
+
+extern	int	quotestrfmt(Fmt *f);
+extern	void	quotefmtinstall(void);
+extern	int	(*fmtdoquote)(int);
+
+
+extern	int	fmtinstall(int, int (*)(Fmt*));
+extern	int	dofmt(Fmt*, char*);
+extern	int	fmtprint(Fmt*, char*, ...);
+extern	int	fmtvprint(Fmt*, char*, va_list);
+extern	int	fmtrune(Fmt*, int);
+extern	int	fmtstrcpy(Fmt*, char*);
+
+extern	double	fmtstrtod(const char *, char **);
+extern	double	fmtcharstod(int(*)(void*), void*);
+
+#endif
diff --git a/include/frame.h b/include/frame.h
new file mode 100644
index 0000000..9b8194b
--- /dev/null
+++ b/include/frame.h
@@ -0,0 +1,85 @@
+typedef struct Frbox Frbox;
+typedef struct Frame Frame;
+
+enum{
+	BACK,
+	HIGH,
+	BORD,
+	TEXT,
+	HTEXT,
+	NCOL
+};
+
+#define	FRTICKW	3
+
+struct Frbox
+{
+	long		wid;		/* in pixels */
+	long		nrune;		/* <0 ==> negate and treat as break char */
+	uchar	*ptr;
+	short	bc;	/* break char */
+	short	minwid;
+};
+
+struct Frame
+{
+	Font		*font;		/* of chars in the frame */
+	Display		*display;	/* on which frame appears */
+	Image		*b;		/* on which frame appears */
+	Image		*cols[NCOL];	/* text and background colors */
+	Rectangle	r;		/* in which text appears */
+	Rectangle	entire;		/* of full frame */
+	void			(*scroll)(Frame*, int);	/* scroll function provided by application */
+	Frbox		*box;
+	ulong		p0, p1;		/* selection */
+	ushort		nbox, nalloc;
+	ushort		maxtab;		/* max size of tab, in pixels */
+	ushort		nchars;		/* # runes in frame */
+	ushort		nlines;		/* # lines with text */
+	ushort		maxlines;	/* total # lines in frame */
+	ushort		lastlinefull;	/* last line fills frame */
+	ushort		modified;	/* changed since frselect() */
+	Image		*tick;	/* typing tick */
+	Image		*tickback;	/* saved image under tick */
+	int			ticked;	/* flag: is tick onscreen? */
+};
+
+ulong	frcharofpt(Frame*, Point);
+Point	frptofchar(Frame*, ulong);
+int	frdelete(Frame*, ulong, ulong);
+void	frinsert(Frame*, Rune*, Rune*, ulong);
+void	frselect(Frame*, Mousectl*);
+void	frselectpaint(Frame*, Point, Point, Image*);
+void	frdrawsel(Frame*, Point, ulong, ulong, int);
+void	frdrawsel0(Frame*, Point, ulong, ulong, Image*, Image*);
+void	frinit(Frame*, Rectangle, Font*, Image*, Image**);
+void	frsetrects(Frame*, Rectangle, Image*);
+void	frclear(Frame*, int);
+
+uchar	*_frallocstr(Frame*, unsigned);
+void	_frinsure(Frame*, int, unsigned);
+Point	_frdraw(Frame*, Point);
+void	_frgrowbox(Frame*, int);
+void	_frfreebox(Frame*, int, int);
+void	_frmergebox(Frame*, int);
+void	_frdelbox(Frame*, int, int);
+void	_frsplitbox(Frame*, int, int);
+int	_frfindbox(Frame*, int, ulong, ulong);
+void	_frclosebox(Frame*, int, int);
+int	_frcanfit(Frame*, Point, Frbox*);
+void	_frcklinewrap(Frame*, Point*, Frbox*);
+void	_frcklinewrap0(Frame*, Point*, Frbox*);
+void	_fradvance(Frame*, Point*, Frbox*);
+int	_frnewwid(Frame*, Point, Frbox*);
+int	_frnewwid0(Frame*, Point, Frbox*);
+void	_frclean(Frame*, Point, int, int);
+void	_frredraw(Frame*, Point);
+void	_fraddbox(Frame*, int, int);
+Point	_frptofcharptb(Frame*, ulong, Point, int);
+Point	_frptofcharnb(Frame*, ulong, int);
+int	_frstrlen(Frame*, int);
+void	frtick(Frame*, Point, int);
+void	frinittick(Frame*);
+
+#define	NRUNE(b)	((b)->nrune<0? 1 : (b)->nrune)
+#define	NBYTE(b)	strlen((char*)(b)->ptr)
diff --git a/include/keyboard.h b/include/keyboard.h
new file mode 100644
index 0000000..a6d99bf
--- /dev/null
+++ b/include/keyboard.h
@@ -0,0 +1,36 @@
+typedef struct 	Keyboardctl Keyboardctl;
+
+struct	Keyboardctl
+{
+	struct Channel	*c;	/* chan(Rune)[20] */
+
+	char		*file;
+	int		consfd;		/* to cons file */
+	int		ctlfd;		/* to ctl file */
+	int		pid;		/* of slave proc */
+};
+
+
+extern	Keyboardctl*	initkeyboard(char*);
+extern	int			ctlkeyboard(Keyboardctl*, char*);
+extern	void			closekeyboard(Keyboardctl*);
+
+enum {
+	KF=	0xF000,	/* Rune: beginning of private Unicode space */
+	/* KF|1, KF|2, ..., KF|0xC is F1, F2, ..., F12 */
+	Khome=	KF|0x0D,
+	Kup=	KF|0x0E,
+	Kpgup=	KF|0x0F,
+	Kprint=	KF|0x10,
+	Kleft=	KF|0x11,
+	Kright=	KF|0x12,
+	Kdown=	0x80,
+	Kview=	0x80,
+	Kpgdown=	KF|0x13,
+	Kins=	KF|0x14,
+	Kend=	'\r',	/* [sic] */
+
+	Kalt=		KF|0x15,
+	Kshift=	KF|0x16,
+	Kctl=		KF|0x17,
+};
diff --git a/include/lib9.h b/include/lib9.h
new file mode 100644
index 0000000..dd62448
--- /dev/null
+++ b/include/lib9.h
@@ -0,0 +1,254 @@
+/*
+ * Lib9 is miscellany from the Plan 9 C library that doesn't
+ * fit into libutf or into libfmt, but is still missing from traditional
+ * Unix C libraries.
+ */
+#ifndef _LIB9H_
+#define _LIB9H_ 1
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+                                                                                
+
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <setjmp.h>
+
+#ifndef _FMTH_
+#	include <fmt.h>
+#endif
+
+#define	nil	((void*)0)
+#define	nelem(x)	(sizeof(x)/sizeof((x)[0]))
+
+#define _NEEDUCHAR 1
+#define _NEEDUSHORT 1
+#define _NEEDUINT 1
+#define _NEEDULONG 1
+
+#if defined(__linux__)
+#	include <sys/types.h>
+#	if defined(__USE_MISC)
+#		undef _NEEDUSHORT
+#		undef _NEEDUINT
+#		undef _NEEDULONG
+#	endif
+#endif
+#if defined(__FreeBSD__)
+#	include <sys/types.h>
+#	if !defined(_POSIX_SOURCE)
+#		undef _NEEDUSHORT
+#		undef _NEEDUINT
+#	endif
+#endif
+
+typedef signed char schar;
+typedef unsigned int u32int;
+#ifdef _NEEDUCHAR
+	typedef unsigned char uchar;
+#endif
+#ifdef _NEEDUSHORT
+	typedef unsigned short ushort;
+#endif
+#ifdef _NEEDUINT
+	typedef unsigned int uint;
+#endif
+#ifdef _NEEDULONG
+	typedef unsigned long ulong;
+#endif
+typedef unsigned long long uvlong;
+typedef long long vlong;
+
+/* rfork to create new process running fn(arg) */
+
+#if defined(__FreeBSD__)
+#undef RFFDG
+#undef RFNOTEG
+#undef RFPROC
+#undef RFMEM
+#undef RFNOWAIT
+#undef RFCFDG
+#endif
+
+enum
+{
+/*	RFNAMEG		= (1<<0), */
+/*	RFENVG		= (1<<1), */
+	RFFDG		= (1<<2),
+	RFNOTEG		= (1<<3),
+	RFPROC		= (1<<4),
+	RFMEM		= (1<<5),
+	RFNOWAIT	= (1<<6),
+/*	RFCNAMEG	= (1<<10), */
+/*	RFCENVG		= (1<<11), */
+	RFCFDG		= (1<<12),
+/*	RFREND		= (1<<13), */
+/*	RFNOMNT		= (1<<14) */
+};
+extern int		ffork(int, void(*)(void*), void*);
+
+/* wait for processes */
+#define wait _p9wait
+typedef struct Waitmsg Waitmsg;
+struct Waitmsg
+{
+	int pid;	/* of loved one */
+	ulong time[3];	/* of loved one & descendants */
+	char	*msg;
+};
+extern	int		await(char*, int);
+extern	Waitmsg*	wait(void);
+
+/* synchronization */
+typedef struct Lock Lock;
+struct Lock
+{
+	int val;
+};
+
+extern int		_tas(void*);
+extern void	lock(Lock*);
+extern void	unlock(Lock*);
+extern int		canlock(Lock*);
+
+typedef struct QLp QLp;
+struct QLp
+{
+	int		inuse;
+	QLp		*next;
+	int		state;
+};
+
+typedef struct QLock QLock;
+struct QLock
+{
+	Lock	lock;
+	int		locked;
+	QLp		*head;
+	QLp		*tail;
+};
+
+extern void	qlock(QLock*);
+extern void	qunlock(QLock*);
+extern int		canqlock(QLock*);
+extern void	_qlockinit(ulong (*)(ulong, ulong));
+
+typedef struct RWLock RWLock;
+struct RWLock
+{
+	Lock	lock;
+	int		readers;
+	int		writer;
+	QLp		*head;
+	QLp		*tail;
+};
+
+extern void	rlock(RWLock*);
+extern void	runlock(RWLock*);
+extern int		canrlock(RWLock*);
+extern void	wlock(RWLock*);
+extern void	wunlock(RWLock*);
+extern int		canwlock(RWLock*);
+
+typedef struct Rendez Rendez;
+struct Rendez
+{
+	QLock	*l;
+	QLp		*head;
+	QLp		*tail;
+};
+
+extern void	rsleep(Rendez*);
+extern int		rwakeup(Rendez*);
+extern int		rwakeupall(Rendez*);
+
+extern ulong	rendezvous(ulong, ulong);
+
+/* one of a kind */
+extern void	sysfatal(char*, ...);
+extern int		nrand(int);
+extern void	setmalloctag(void*, ulong);
+extern void	setrealloctag(void*, ulong);
+extern void	*mallocz(ulong, int);
+extern long	readn(int, void*, long);
+extern void	exits(char*);
+extern void	_exits(char*);
+extern ulong	getcallerpc(void*);
+
+/* string routines */
+extern char*	strecpy(char*, char*, char*);
+extern int		tokenize(char*, char**, int);
+extern int		cistrncmp(char*, char*, int);
+extern int		cistrcmp(char*, char*);
+extern char*	cistrstr(char*, char*);
+extern int		getfields(char*, char**, int, int, char*);
+extern int		gettokens(char *, char **, int, char *);
+
+/* formatting helpers */
+extern int		dec64(uchar*, int, char*, int);
+extern int		enc64(char*, int, uchar*, int);
+extern int		dec32(uchar*, int, char*, int);
+extern int		enc32(char*, int, uchar*, int);
+extern int		dec16(uchar*, int, char*, int);
+extern int		enc16(char*, int, uchar*, int);
+extern int		encodefmt(Fmt*);
+
+/* error string */
+enum
+{
+	ERRMAX = 128
+};
+extern void	rerrstr(char*, uint);
+extern void	werrstr(char*, ...);
+extern int		errstr(char*, uint);
+
+/* compiler directives on plan 9 */
+#define	USED(x)	if(x){}else{}
+#define	SET(x)	((x)=0)
+
+/* command line */
+extern char	*argv0;
+#define	ARGBEGIN	for((argv0||(argv0=*argv)),argv++,argc--;\
+			    argv[0] && argv[0][0]=='-' && argv[0][1];\
+			    argc--, argv++) {\
+				char *_args, *_argt;\
+				Rune _argc;\
+				_args = &argv[0][1];\
+				if(_args[0]=='-' && _args[1]==0){\
+					argc--; argv++; break;\
+				}\
+				_argc = 0;\
+				while(*_args && (_args += chartorune(&_argc, _args)))\
+				switch(_argc)
+#define	ARGEND		SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
+#define	ARGF()		(_argt=_args, _args="",\
+				(*_argt? _argt: argv[1]? (argc--, *++argv): 0))
+#define	EARGF(x)	(_argt=_args, _args="",\
+				(*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
+
+#define	ARGC()		_argc
+
+#define OREAD O_RDONLY
+#define OWRITE O_WRONLY
+#define AEXIST 0
+#define AREAD 4
+#define AWRITE 2
+#define AEXEC 1
+#define ORCLOSE 8
+#define OCEXEC 16
+
+#define dup dup2
+#define exec execv
+#define seek lseek
+#define getwd getcwd
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif	/* _LIB9H_ */
diff --git a/include/libc.h b/include/libc.h
new file mode 100644
index 0000000..25b042c
--- /dev/null
+++ b/include/libc.h
@@ -0,0 +1,2 @@
+#include <lib9.h>
+
diff --git a/include/mouse.h b/include/mouse.h
new file mode 100644
index 0000000..f0a0f69
--- /dev/null
+++ b/include/mouse.h
@@ -0,0 +1,44 @@
+typedef struct	Menu Menu;
+typedef struct 	Mousectl Mousectl;
+
+struct	Mouse
+{
+	int	buttons;	/* bit array: LMR=124 */
+	Point	xy;
+	ulong	msec;
+};
+
+struct Mousectl
+{
+	Mouse	m;
+	struct Channel	*c;	/* chan(Mouse) */
+	struct Channel	*resizec;	/* chan(int)[2] */
+			/* buffered in case client is waiting for a mouse action before handling resize */
+
+	char		*file;
+	int		mfd;		/* to mouse file */
+	int		cfd;		/* to cursor file */
+	int		pid;		/* of slave proc */
+	Display		*display;
+	/*Image*	image;	/ * of associated window/display */
+};
+
+struct Menu
+{
+	char	**item;
+	char	*(*gen)(int);
+	int	lasthit;
+};
+
+/*
+ * Mouse
+ */
+extern Mousectl*	initmouse(char*, Image*);
+extern void		moveto(Mousectl*, Point);
+extern int			readmouse(Mousectl*);
+extern void		closemouse(Mousectl*);
+struct Cursor;
+extern void		setcursor(Mousectl*, struct Cursor*);
+extern void		drawgetrect(Rectangle, int);
+extern Rectangle	getrect(int, Mousectl*);
+extern int	 		menuhit(int, Mousectl*, Menu*, Screen*);
diff --git a/include/regexp9.h b/include/regexp9.h
new file mode 100644
index 0000000..e25658a
--- /dev/null
+++ b/include/regexp9.h
@@ -0,0 +1,71 @@
+#ifndef _REGEXP9H_
+
+#define _REGEXP9H_ 1
+#include <utf.h>
+
+typedef struct Resub		Resub;
+typedef struct Reclass		Reclass;
+typedef struct Reinst		Reinst;
+typedef struct Reprog		Reprog;
+
+/*
+ *	Sub expression matches
+ */
+struct Resub{
+	union
+	{
+		char *sp;
+		Rune *rsp;
+	}s;
+	union
+	{
+		char *ep;
+		Rune *rep;
+	}e;
+};
+
+/*
+ *	character class, each pair of rune's defines a range
+ */
+struct Reclass{
+	Rune	*end;
+	Rune	spans[64];
+};
+
+/*
+ *	Machine instructions
+ */
+struct Reinst{
+	int	type;
+	union	{
+		Reclass	*cp;		/* class pointer */
+		Rune	r;		/* character */
+		int	subid;		/* sub-expression id for RBRA and LBRA */
+		Reinst	*right;		/* right child of OR */
+	}u1;
+	union {	/* regexp relies on these two being in the same union */
+		Reinst *left;		/* left child of OR */
+		Reinst *next;		/* next instruction for CAT & LBRA */
+	}u2;
+};
+
+/*
+ *	Reprogram definition
+ */
+struct Reprog{
+	Reinst	*startinst;	/* start pc */
+	Reclass	class[16];	/* .data */
+	Reinst	firstinst[5];	/* .text */
+};
+
+extern Reprog	*regcomp(char*);
+extern Reprog	*regcomplit(char*);
+extern Reprog	*regcompnl(char*);
+extern void	regerror(char*);
+extern int	regexec(Reprog*, char*, Resub*, int);
+extern void	regsub(char*, char*, int, Resub*, int);
+
+extern int	rregexec(Reprog*, Rune*, Resub*, int);
+extern void	rregsub(Rune*, Rune*, Resub*, int);
+
+#endif
diff --git a/include/thread.h b/include/thread.h
new file mode 100644
index 0000000..10aac28
--- /dev/null
+++ b/include/thread.h
@@ -0,0 +1,132 @@
+#ifndef _THREADH_
+#define _THREADH_ 1
+
+/* avoid conflicts with socket library */
+#undef send
+#define send _threadsend
+#undef recv
+#define recv _threadrecv
+
+typedef struct Alt	Alt;
+typedef struct Channel	Channel;
+typedef struct Ref	Ref;
+
+/* Channel structure.  S is the size of the buffer.  For unbuffered channels
+ * s is zero.  v is an array of s values.  If s is zero, v is unused.
+ * f and n represent the state of the queue pointed to by v.
+ */
+
+enum {
+	Nqwds = 2,
+	Nqshift = 5,	// 2log #of bits in long
+	Nqmask =  - 1,
+	Nqbits = (1 << Nqshift) * 2,
+};
+
+struct Channel {
+	int			s;		// Size of the channel (may be zero)
+	unsigned int	f;		// Extraction point (insertion pt: (f + n) % s)
+	unsigned int	n;		// Number of values in the channel
+	int			e;		// Element size
+	int			freed;	// Set when channel is being deleted
+	volatile Alt	**qentry;	// Receivers/senders waiting (malloc)
+	volatile int	nentry;	// # of entries malloc-ed
+	unsigned char		v[1];		// Array of s values in the channel
+};
+
+
+/* Channel operations for alt: */
+typedef enum {
+	CHANEND,
+	CHANSND,
+	CHANRCV,
+	CHANNOP,
+	CHANNOBLK,
+} ChanOp;
+
+struct Alt {
+	Channel	*c;		/* channel */
+	void		*v;		/* pointer to value */
+	ChanOp	op;		/* operation */
+
+	/* the next variables are used internally to alt
+	 * they need not be initialized
+	 */
+	Channel	**tag;	/* pointer to rendez-vous tag */
+	int		entryno;	/* entry number */
+};
+
+struct Ref {
+	long ref;
+};
+
+int		alt(Alt alts[]);
+Channel*	chancreate(int elemsize, int bufsize);
+int		chaninit(Channel *c, int elemsize, int elemcnt);
+void		chanfree(Channel *c);
+int		chanprint(Channel *, char *, ...);
+long		decref(Ref *r);		/* returns 0 iff value is now zero */
+void		incref(Ref *r);
+int		nbrecv(Channel *c, void *v);
+void*		nbrecvp(Channel *c);
+unsigned long		nbrecvul(Channel *c);
+int		nbsend(Channel *c, void *v);
+int		nbsendp(Channel *c, void *v);
+int		nbsendul(Channel *c, unsigned long v);
+int		proccreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
+int		procrfork(void (*f)(void *arg), void *arg, unsigned int stacksize, int flag);
+void**		procdata(void);
+void		procexec(Channel *, char *, char *[]);
+void		procexecl(Channel *, char *, ...);
+int		recv(Channel *c, void *v);
+void*		recvp(Channel *c);
+unsigned long		recvul(Channel *c);
+int		send(Channel *c, void *v);
+int		sendp(Channel *c, void *v);
+int		sendul(Channel *c, unsigned long v);
+int		threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
+void**		threaddata(void);
+void		threadexits(char *);
+void		threadexitsall(char *);
+int		threadgetgrp(void);	/* return thread group of current thread */
+char*		threadgetname(void);
+void		threadint(int);	/* interrupt thread */
+void		threadintgrp(int);	/* interrupt threads in grp */
+void		threadkill(int);	/* kill thread */
+void		threadkillgrp(int);	/* kill threads in group */
+void		threadmain(int argc, char *argv[]);
+void		threadnonotes(void);
+int		threadnotify(int (*f)(void*, char*), int in);
+int		threadid(void);
+int		threadpid(int);
+int		threadsetgrp(int);	/* set thread group, return old */
+void		threadsetname(char *name);
+Channel*	threadwaitchan(void);
+int	tprivalloc(void);
+void	tprivfree(int);
+void	**tprivaddr(int);
+void		yield(void);
+
+long		threadstack(void);
+
+extern	int		mainstacksize;
+
+/* slave I/O processes */
+typedef struct Ioproc Ioproc;
+
+Ioproc*	ioproc(void);
+void		closeioproc(Ioproc*);
+void		iointerrupt(Ioproc*);
+
+int		ioclose(Ioproc*, int);
+int		iodial(Ioproc*, char*, char*, char*, int*);
+int		ioopen(Ioproc*, char*, int);
+long		ioread(Ioproc*, int, void*, long);
+long		ioreadn(Ioproc*, int, void*, long);
+long		iowrite(Ioproc*, int, void*, long);
+int		iosleep(Ioproc*, long);
+
+long		iocall(Ioproc*, long (*)(va_list*), ...);
+void		ioret(Ioproc*, int);
+
+#endif	/* _THREADH_ */
diff --git a/include/u.h b/include/u.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/u.h
diff --git a/include/utf.h b/include/utf.h
new file mode 100644
index 0000000..623bfda
--- /dev/null
+++ b/include/utf.h
@@ -0,0 +1,51 @@
+#ifndef _UTFH_
+#define _UTFH_ 1
+
+typedef unsigned short Rune;	/* 16 bits */
+
+enum
+{
+	UTFmax		= 3,		/* maximum bytes per rune */
+	Runesync	= 0x80,		/* cannot represent part of a UTF sequence (<) */
+	Runeself	= 0x80,		/* rune and UTF sequences are the same (<) */
+	Runeerror	= 0x80,		/* decoding error in UTF */
+};
+
+/*
+ * rune routines
+ */
+extern	int	runetochar(char*, Rune*);
+extern	int	chartorune(Rune*, char*);
+extern	int	runelen(long);
+extern	int	runenlen(Rune*, int);
+extern	int	fullrune(char*, int);
+extern	int	utflen(char*);
+extern	int	utfnlen(char*, long);
+extern	char*	utfrune(char*, long);
+extern	char*	utfrrune(char*, long);
+extern	char*	utfutf(char*, char*);
+extern	char*	utfecpy(char*, char*, char*);
+
+extern	Rune*	runestrcat(Rune*, Rune*);
+extern	Rune*	runestrchr(Rune*, Rune);
+extern	int	runestrcmp(Rune*, Rune*);
+extern	Rune*	runestrcpy(Rune*, Rune*);
+extern	Rune*	runestrncpy(Rune*, Rune*, long);
+extern	Rune*	runestrecpy(Rune*, Rune*, Rune*);
+extern	Rune*	runestrdup(Rune*);
+extern	Rune*	runestrncat(Rune*, Rune*, long);
+extern	int	runestrncmp(Rune*, Rune*, long);
+extern	Rune*	runestrrchr(Rune*, Rune);
+extern	long	runestrlen(Rune*);
+extern	Rune*	runestrstr(Rune*, Rune*);
+
+extern	Rune	tolowerrune(Rune);
+extern	Rune	totitlerune(Rune);
+extern	Rune	toupperrune(Rune);
+extern	int	isalpharune(Rune);
+extern	int	islowerrune(Rune);
+extern	int	isspacerune(Rune);
+extern	int	istitlerune(Rune);
+extern	int	isupperrune(Rune);
+
+#endif