libvt100

A library for heling in console programming.
git clone git://r-36.net/libvt100
Log | Files | Refs | LICENSE

commit 6ffddc2a6352ed97d31522ff646d433248efdd19
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed,  6 Apr 2011 21:56:58 +0200

Initial commit.

Diffstat:
LICENSE | 21+++++++++++++++++++++
Makefile | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
README.md | 13+++++++++++++
config.mk | 23+++++++++++++++++++++++
vt100.c | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
vt100.h | 33+++++++++++++++++++++++++++++++++
6 files changed, 273 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2011 Christoph Lohmann <20h@r-36.net> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,69 @@ +# libvt100 - a simple vt100 control library +# See LICENSE file for copyright and license details. + +include config.mk + +SRC = vt100.c +OBJ = ${SRC:.c=.o} +SOUT = ${NAME}.a +DOUT = ${NAME}.so + +all: options ${SOUT} ${DOUT} + +options: + @echo ${NAME} build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.mk + +${SOUT}: ${OBJ} + @ar rcs ${SOUT} ${OBJ} + +${DOUT}: ${OBJ} + @${CC} -shared ${OBJ} -o ${DOUT} + +vt100test: ${OBJ} vt100test.o + @echo CC -o $@ + @${CC} -o $@ ${OBJ} vt100test.o ${LDFLAGS} + +test: vt100test + @echo running vt100test + ./vt100test + +clean: + @echo cleaning + @rm -f *.so *.a ${NAME} ${OBJ} vt100test vt100test.o ${NAME}-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p ${NAME}-${VERSION} + @cp -R LICENSE Makefile README.md config.mk \ + ${SRC} *.h ${NAME}-${VERSION} + @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} + @gzip ${NAME}-${VERSION}.tar + @rm -rf ${NAME}-${VERSION} + +install: all + @echo installing libraries to ${DESTDIR}${PREFIX}/lib + @mkdir -p ${DESTDIR}${PREFIX}/lib + @cp -f ${NAME}.a ${NAME}.so ${DESTDIR}${PREFIX}/lib + @chmod 755 ${DESTDIR}${PREFIX}/lib/${NAME}.* + @echo installing header file to ${DESTDIR}${PREFIX}/include + @mkdir -p ${DESTDIR}${PREFIX}/include + @cp -f vt100.h ${DESTDIR}${PREFIX}/include + @chmod 644 ${DESTDIR}${PREFIX}/include/vt100.h + +uninstall: + @echo removing libraries from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/lib/${NAME}.* + @echo removing header file from ${DESTDIR}${PREFIX}/include + @rm -f ${DESTDIR}${PREFIX}/include/vt100.h + +.PHONY: all options clean dist install uninstall test +# DO NOT DELETE diff --git a/README.md b/README.md @@ -0,0 +1,13 @@ +# libvt100 - a simple vt100 library + +## Installation + + make + make install + +## How to use it + +The names in vt100.h should be obvious. + +Have fun! + diff --git a/config.mk b/config.mk @@ -0,0 +1,23 @@ +# libvt100 metadata +NAME = libvt100 +VERSION = 0.2 + +# Customize below to fit your system + +# paths +PREFIX ?= /usr +MANPREFIX = ${PREFIX}/share/man + +# includes and libs +INCS = -I. -I/usr/include +LIBS = -L/usr/lib -lc + +# flags +CPPFLAGS = -DVERSION=\"${VERSION}\" +CFLAGS = -g -fPIC -std=gnu99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} +LDFLAGS = -g ${LIBS} +#LDFLAGS = -s ${LIBS} + +# compiler and linker +CC = cc + diff --git a/vt100.c b/vt100.c @@ -0,0 +1,114 @@ +/* + * Copy me if you can. + * by 20h + */ + +#include <termios.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <strings.h> + +#include "vt100.h" + +int termrows; +int termcols; + +struct termios tioin; +struct termios tioout; + +void +term_setpos(int row, int col) +{ + printf("\003[%d;%dH", row, col); +} + +void +term_erasepos(void) +{ + printf("\033[OK"); +} + +void +term_clrscr(void) +{ + printf("\033[2J"); +} + +void +term_clrrow(int row) +{ + printf("\033[%dC|\n\r", row); +} + +void +term_rst(void) +{ + printf("\x033[?47l"); + printf("\x033[%d;1H", termrows); + tcsetattr(0, TCSANOW, &tioin); + tcsetattr(1, TCSANOW, &tioout); +} + +void +term_init(void) +{ + char buf[65]; + struct termios tio; + int i, ro, co; + + bzero(buf, sizeof(buf)); + + tcgetattr(1, &tioout); + tcgetattr(0, &tio); + memmove(&tioin, &tio, sizeof(tio)); + cfmakeraw(&tio); + tcsetattr(0, TCSAFLUSH, &tio); + tcgetattr(1, &tio); + cfmakeraw(&tio); + tcsetattr(1, TCSADRAIN, &tio); + + printf("\033[18t"); + for (i = 0; read(0, &buf[i], 1) && i < sizeof(buf)-1; i++) { + if (buf[i] == '\t') { + buf[i] = '\0'; + break; + } + } + if (i >= sizeof(buf)-1) { + termrows = 24; + termcols = 80; + } else { + sscanf(&buf[4], "%d;%d", &ro, &co); + termrows = ro; + termcols = co; + printf("\033[?37h"); + } +} + +void +term_error(char *fmt, ...) +{ + va_list fmtargs; + + printf("\033[%d:%dH", termrows, 1); + va_start(fmtargs, fmt); + vfprintf(stdout, fmt, fmtargs); + va_end(fmtargs); + printf("\n\r"); +} + +void +term_printf(int row, int col, char *fmt, ...) +{ + va_list fmtargs; + + term_setpos(row, col); + va_start(fmtargs, fmt); + vfprintf(stdout, fmt, fmtargs); + va_end(fmtargs); + printf("\n\r"); +} + diff --git a/vt100.h b/vt100.h @@ -0,0 +1,33 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef __LIBVT100_H__ +#define __LIBVT100_H__ + +extern int termrows; +extern int termcols; + +typedef struct termwin_t termwin_t; +struct termwin_t { + int br, bc, r, c; + int cr, cc; + + int id; + + termwin_t *next; + termwin_t *prev; +}; + +typedef struct termwm_t termwm_t; +struct termwm_t { + int n; + + termwin_t *first; + termwin_t *last; + termwin_t *root; +}; + +#endif +