+void term_reset(void)
+{
+ term_puts("\033c");
+ term_flush();
+}
+
+void term_getsize(int *width, int *height)
+{
+ *width = term_width;
+ *height = term_height;
+}
+
+void term_resize_func(void (*func)(int, int))
+{
+ cb_resized = func;
+}
+
+
+static char termbuf[1024];
+static int termbuf_len;
+
+void term_send(const char *s, int size)
+{
+ if(size >= sizeof termbuf) {
+ /* too large, just flush the buffer and write directly to the tty */
+ term_flush();
+ write(ttyfd, s, size);
+ } else {
+ if(size >= sizeof termbuf - termbuf_len) {
+ term_flush();
+ }
+ memcpy(termbuf + termbuf_len, s, size);
+ termbuf_len += size;
+ }
+}
+
+void term_putchar(char c)
+{
+ term_send(&c, 1);
+}
+
+void term_puts(const char *s)
+{
+ term_send(s, strlen(s));
+}
+
+void term_printf(const char *fmt, ...)
+{
+ static char *buf;
+ static long bufsz;
+ va_list ap;
+ long len;
+
+ if(!buf) {
+ bufsz = 512;
+ if(!(buf = malloc(bufsz))) {
+ return;
+ }
+ }
+
+ for(;;) {
+ va_start(ap, fmt);
+ len = vsnprintf(buf, bufsz, fmt, ap);
+ va_end(ap);
+
+ if(len < bufsz) break;
+ if(len < 0) {
+ void *tmp;
+ long n = bufsz << 1;
+ if(!(tmp = realloc(buf, n))) {
+ break; /* if realloc fails, will result in truncated output */
+ }
+ }
+ }
+
+ term_send(buf, len);
+}
+
+void term_flush(void)
+{
+ if(termbuf_len > 0) {
+ write(ttyfd, termbuf, termbuf_len);
+ termbuf_len = 0;
+ }
+}
+