Refactored to C++

This commit unfortunately touches almost every line.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Simplify by storing scroll line rather than byte

All scrolling is vertial, and it turns out storting the scroll byte offset
leads to more complex math.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Introduce effective_attribute()

The real attribute was calculated from `reverse' and `curattr' in multiple
places, and in some cases skipped.  This patch unifies everything around
effective_attribute().

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Retire memset16 in favor of std::fill

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Get rid of a couple of calls to calloc

Moved to new[], in the hope that we can use std::unique_ptr<> eventually.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Separated I/O, drawing and ASCII/ANSI parsing

This should make it easier to write test programs.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Get rid of global variables

The variables were: font, palette and home_fd

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Keep buffers in smart pointers

Prior to this commit, Terminal objects' character and attribute buffers were
not freed when the objects were destroyed.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Make Terminal::ProcessData take const void*

There's no solid reason to force the caller to use const unsigned char*.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Correct Terminal::InsertChars

The new behavior was copied from xterm, and doesn't contain a crash bug.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Fix crash bug in "delete characters" handler

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Add fuzz tester

The crash bugs fixed in the last two commits were discovered using this fuzz
tester.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Avoid writing beyond end of last line in buffer

Bug discovered by Valgrind and fuzz tester.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Include a 100 line scrollback buffer in fuzz test

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Calculate scroll offset in draw state correctly

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
fuzz: Run tests with and without scrollback buffer

The results of parsing with a scrollback buffer are compared to the results
without a scrollback buffer, as a sanity test.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Prevent scrolltop from being below scrollbottom

Without this patch, the terminal can be crashed by invalid input.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Include random number of parameters in fuzz test

Cursor positioning and coloring needs parameters.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Fixed \033[{,0,1,2}J behavior, and added \033#8

Clearing has been completely broken since d648b2683a.

It turns out \033[J is not supposed to only clear complete line.  \033#8 fills
the screen with gray 'E's, which makes debugging the clear functions easier.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Avoid fast path when using the alternate charset

The fast path would cause "──" to be rendered as "qq", noticeable in e.g.
alsamixer.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>