diff options
Diffstat (limited to 'programs/c-ports/subeditor.c')
-rw-r--r-- | programs/c-ports/subeditor.c | 605 |
1 files changed, 605 insertions, 0 deletions
diff --git a/programs/c-ports/subeditor.c b/programs/c-ports/subeditor.c new file mode 100644 index 0000000..2d748b9 --- /dev/null +++ b/programs/c-ports/subeditor.c @@ -0,0 +1,605 @@ +#include <curses.h> +#include "subeditor.h" +#define clr_bitabl() {\ + bitabl[0x00] = 0;\ + bitabl[0x01] = 0;\ + bitabl[0x02] = 0;\ + bitabl[0x03] = 0;\ + bitabl[0x04] = 0;\ + bitabl[0x05] = 0;\ + bitabl[0x06] = 0;\ + bitabl[0x07] = 0;\ + bitabl[0x08] = 0;\ + bitabl[0x09] = 0;\ + bitabl[0x0A] = 0;\ + bitabl[0x0B] = 0;\ + bitabl[0x0C] = 0;\ + bitabl[0x0D] = 0;\ + bitabl[0x0E] = 0;\ + bitabl[0x0F] = 0;\ +} + +WINDOW *scr; + +const char *tok = "dab"; +const char *msg = "oof, you divided a, and b on me.\n"; +const char *string = "Please, type something.\n"; +const char *string2 = "You typed, "; + +const uint8_t bits[8] = { + 0x80, + 0x40, + 0x20, + 0x10, + 0x08, + 0x04, + 0x02, + 0x01 +}; + +char *buffer; +char *cmd_buf; + +uint8_t scr_row = 0; +uint8_t scr_col = 0; +uint8_t scr_trow = 0; +uint8_t scr_tcol = 0; +uint16_t scr_ptr = 0; + +uint8_t byte = 0; +uint8_t mask = 0; + +uint8_t a = 0; +uint8_t b = 0; +uint8_t c = 0; +uint8_t d = 0; +uint8_t e = 0; +uint8_t f = 0; + +uint8_t bitmask = 0; +uint8_t bitabl[16]; + +uint8_t scr_str = 0; +uint8_t scr_end = 23; +uint8_t wrapped = 0; + +void clr_buf() { + uint16_t i = 0; + for (; i < 0x2000;) { + buffer[i+0x00] = 0; + buffer[i+0x01] = 0; + buffer[i+0x02] = 0; + buffer[i+0x03] = 0; + buffer[i+0x04] = 0; + buffer[i+0x05] = 0; + buffer[i+0x06] = 0; + buffer[i+0x07] = 0; + buffer[i+0x08] = 0; + buffer[i+0x09] = 0; + buffer[i+0x0A] = 0; + buffer[i+0x0B] = 0; + buffer[i+0x0C] = 0; + buffer[i+0x0D] = 0; + buffer[i+0x0E] = 0; + buffer[i+0x0F] = 0; + buffer[i+0x10] = 0; + buffer[i+0x11] = 0; + buffer[i+0x12] = 0; + buffer[i+0x13] = 0; + buffer[i+0x14] = 0; + buffer[i+0x15] = 0; + buffer[i+0x16] = 0; + buffer[i+0x17] = 0; + buffer[i+0x18] = 0; + buffer[i+0x19] = 0; + buffer[i+0x1A] = 0; + buffer[i+0x1B] = 0; + buffer[i+0x1C] = 0; + buffer[i+0x1D] = 0; + buffer[i+0x1E] = 0; + buffer[i+0x1F] = 0; + i+=0x20; + } +} + +void clr_cbuf() { + uint16_t i = 0; + for (; i < 0x400;) { + cmd_buf[i+0x00] = 0; + cmd_buf[i+0x01] = 0; + cmd_buf[i+0x02] = 0; + cmd_buf[i+0x03] = 0; + cmd_buf[i+0x04] = 0; + cmd_buf[i+0x05] = 0; + cmd_buf[i+0x06] = 0; + cmd_buf[i+0x07] = 0; + cmd_buf[i+0x08] = 0; + cmd_buf[i+0x09] = 0; + cmd_buf[i+0x0A] = 0; + cmd_buf[i+0x0B] = 0; + cmd_buf[i+0x0C] = 0; + cmd_buf[i+0x0D] = 0; + cmd_buf[i+0x0E] = 0; + cmd_buf[i+0x0F] = 0; + cmd_buf[i+0x10] = 0; + cmd_buf[i+0x11] = 0; + cmd_buf[i+0x12] = 0; + cmd_buf[i+0x13] = 0; + cmd_buf[i+0x14] = 0; + cmd_buf[i+0x15] = 0; + cmd_buf[i+0x16] = 0; + cmd_buf[i+0x17] = 0; + cmd_buf[i+0x18] = 0; + cmd_buf[i+0x19] = 0; + cmd_buf[i+0x1A] = 0; + cmd_buf[i+0x1B] = 0; + cmd_buf[i+0x1C] = 0; + cmd_buf[i+0x1D] = 0; + cmd_buf[i+0x1E] = 0; + cmd_buf[i+0x1F] = 0; + i+=0x20; + } +} + +void update_pos() { + scr_ptr = (scr_row+scr_str)*80; + scr_ptr += scr_col; + #if !debug + wmove(scr, scr_row, scr_col); + #endif +} + +void rdrw_row() { + uint8_t i = 0; + uint16_t ptr; + scr_col = 0; + update_pos(); + ptr = scr_ptr; + for (;i < 80;i++) { + if (buffer[ptr+i]) { + #if !debug + waddch(scr, buffer[ptr+i]); + #endif + } + scr_col++; + if (!buffer[ptr+i]) { + update_pos(); + } + } + scr_col = 0; +} + +void scrl_down() { + scr_str++; + scr_end++; + #if !debug + wscrl(scr, 1); + #endif + rdrw_row(); + update_pos(); + wrapped = 0; +} + +void scrl_up() { + scr_str--; + scr_end--; + #if !debug + wscrl(scr, -1); + #endif + scr_trow = scr_row; + scr_tcol = scr_col; + rdrw_row(); + scr_row = scr_trow; + scr_col = scr_tcol; + update_pos(); + wrapped = 0; +} + +void bitpos(uint8_t row) { + uint8_t bit; + bitmask = row; + bit = row & 7; + mask = bits[bit]; + byte = bitmask >> 3; + #if debug + mvwprintw(scr, 2, 0, "bit: $%02X, row: $%02X, byte: $%02X, mask: $%02X, bitmask: $%02X\r", bit, row, byte, mask, bitmask); + #endif +} + +void clrbit (uint8_t row) { + uint8_t tmp; + uint8_t invmsk; + bitpos(row); + invmsk = ~mask; + tmp = (invmsk & (bitabl[byte])); + bitabl[byte] = tmp; +} + +void setbit (uint8_t row) { + uint8_t tmp; + bitpos(row); + tmp = (mask | (bitabl[byte])); + bitabl[byte] = tmp; +} + +uint8_t getbit() { + if (scr_str) { + bitpos(scr_row+scr_str); + } else { + bitpos(scr_row); + } + return (mask & bitabl[byte]); +} + +void findst() { + while (getbit()) { + scr_row--; + if ((int8_t)scr_row < 0) { + scr_row++; + } + } +} + +void print_char(char ch) { + uint8_t is_wrap = 0; + uint8_t is_scroll = 0; + uint8_t is_esc = 0; + uint8_t done = 0; + a = ch; + switch (ch) { + case 0x1B: + if (wgetch(scr) == 0x1B) { + is_esc = 1; + } + ch = wgetch(scr); + if (!ch) + break; + switch (ch) { + case 'A': + if (!scr_row) { + if (scr_str) { + scrl_up(); + update_pos(); + break; + } else { + break; + } + } + scr_row--; + update_pos(); + break; + case 'B': + if (scr_row == 23) { + scr_trow = scr_row; + scr_tcol = scr_col; + scrl_down(); + scr_row = scr_trow; + scr_col = scr_tcol; + update_pos(); + break; + } + scr_row++; + update_pos(); + break; + case 'C': + if (scr_col >= 79) { + ++scr_row; + if (getbit()) { + scr_col = 0; + if (scr_row >= 23) { + if (scr_str) { + wrapped = 1; + scrl_down(); + } + is_wrap = 0; + done = 1; + } else { + is_wrap = 1; + done = 1; + } + } else { + is_wrap = 0; + done = 1; + } + } else { + scr_col++; + is_wrap = 1; + done = 1; + } + if (!is_wrap) { + --scr_row; + is_wrap = 0; + } + if (done) { + update_pos(); + done = 0; + } + wrapped = 0; + break; + case 'D': + /* isleft. */ + if (!scr_col) { + /* isleft_wrp. */ + if (getbit()) { + /* wrap_dec. */ + if (scr_row) { + wrapped = 1; + scr_row--; + } + /* wrap_dec1. */ + scr_col = 79; + if (!scr_row) { + /* isleft_scrl. */ + if (wrapped) { + done = 1; + } else if (!scr_str) { + done = 0; + } else { + scrl_up(); + done = 0; + } + } else { + done = 1; + } + } else { + done = 0; + } + } else { + scr_col--; + done = 1; + } + if (done) { + update_pos(); + done = 0; + } + break; + } + break; + case '\n': + buffer[scr_ptr] = 0; + scr_col = 0; + if (scr_row == 23) { + scrl_down(); + } else { + scr_row++; + update_pos(); + } + a = '\n'; + wrefresh(scr); + break; + case 0x0C: + scr_end = 23; + scr_str = 0; + clr_bitabl(); + clr_buf(); + clr_cbuf(); + scr_row = 0; + scr_col = 0; + update_pos(); + werase(scr); + break; + case 19: + break; + case 18: + break; + case '\b': + case 0x7F: + if (!scr_col) { + if (getbit()) { + if (!scr_row) { + if (scr_str) { + scrl_up(); + scr_row++; + } else { + break; + } + } + clrbit(scr_row+scr_str); + scr_row--; + scr_col = 80; + update_pos(); + } else { + break; + } + } + scr_col--; + update_pos(); + buffer[scr_ptr] = 0; + #if !debug + wdelch(scr); + #endif + break; + default: + a = ch; + buffer[scr_ptr] = ch; + scr_col++; + #if !debug + waddch(scr, ch); + #endif + if (scr_col >= 80) { + if (scr_row >= 23) { + is_scroll = 1; + scrl_down(); + } else { + is_scroll = 0; + scr_row++; + } + scr_col = 0; + setbit(scr_row+scr_str); + update_pos(); + } + update_pos(); + break; + } +} + +uint8_t getkey(char ch) { + e = 0; + if (ch == '\n') { + uint16_t i = 0; + uint16_t ptr; + e = scr_row; + findst(scr_row); + ptr = (scr_row+scr_str)*80; + for (;buffer[ptr+i] != '\0';i++) { + cmd_buf[i] = buffer[ptr+i]; + } + } + if (e) { + scr_row = e; + } + print_char(ch); + wrefresh(scr); + if (a == '\n') { + return 0; + } else { + return 1; + } +} + +void print_str(const char *str) { + uint16_t i = 0; + for (;str[i] != '\0'; i++) { + print_char(str[i]); + } +} + +uint8_t dabbed() { + uint8_t i = 0; + uint8_t done = 0; + while (!done) { + if (!cmd_buf[i]) { + done = 1; + } else if (cmd_buf[i] == tok[i]) { + i++; + if (i == 3) { + print_str(msg); + break; + } + } else { + done = 1; + } + } + return done; +} + +int main() { + if(!scr) + scr = initscr(); + nodelay(stdscr, 0); + crmode(); + noecho(); + nl(); + curs_set(1); + werase(scr); + scrollok(scr, 1); + wrefresh(scr); + start_color(); + use_default_colors(); + init_pair(1, COLOR_WHITE, -1); + attron(COLOR_PAIR(1) | A_BOLD); + wmove(scr, 0, 0); + buffer = malloc(0x2000); + cmd_buf = malloc(0x400); + clr_bitabl(); + clr_buf(); + uint8_t end = 0; + uint8_t next = 1; + char ch; + while (!end) { + if (next) { + clr_cbuf(); + print_str(string); + next = 0; + } + #if debug + uint8_t ln = 0; + mvwprintw(scr, ln++, 0, "scr_row: $%02X, scr_col: $%02X\r", scr_row, scr_col); + mvwprintw(scr, ln++, 0, "scr_str: $%02X, scr_end: $%02X, scr_ptr: $%04X\r", scr_str, scr_end, scr_ptr); + ln+=2; + mvwprintw(scr, ln++, 0, "buffer:\r"); + for (uint16_t i = 0; i < 120; i+=5) { + mvwprintw(scr, ln++, 0, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x, i: $%02X\r" + , buffer[(0x00)+(i*0x10)], buffer[(0x01)+(i*0x10)], buffer[(0x02)+(i*0x10)], buffer[(0x03)+(i*0x10)] + , buffer[(0x04)+(i*0x10)], buffer[(0x05)+(i*0x10)], buffer[(0x06)+(i*0x10)], buffer[(0x07)+(i*0x10)] + , buffer[(0x08)+(i*0x10)], buffer[(0x09)+(i*0x10)], buffer[(0x0A)+(i*0x10)], buffer[(0x0B)+(i*0x10)] + , buffer[(0x0C)+(i*0x10)], buffer[(0x0D)+(i*0x10)], buffer[(0x0E)+(i*0x10)], buffer[(0x0F)+(i*0x10)] + , buffer[(0x10)+(i*0x10)], buffer[(0x11)+(i*0x10)], buffer[(0x12)+(i*0x10)], buffer[(0x13)+(i*0x10)] + , buffer[(0x14)+(i*0x10)], buffer[(0x15)+(i*0x10)], buffer[(0x16)+(i*0x10)], buffer[(0x17)+(i*0x10)] + , buffer[(0x18)+(i*0x10)], buffer[(0x19)+(i*0x10)], buffer[(0x1A)+(i*0x10)], buffer[(0x1B)+(i*0x10)] + , buffer[(0x1C)+(i*0x10)], buffer[(0x1D)+(i*0x10)], buffer[(0x1E)+(i*0x10)], buffer[(0x1F)+(i*0x10)] + , buffer[(0x20)+(i*0x10)], buffer[(0x21)+(i*0x10)], buffer[(0x22)+(i*0x10)], buffer[(0x23)+(i*0x10)] + , buffer[(0x24)+(i*0x10)], buffer[(0x25)+(i*0x10)], buffer[(0x26)+(i*0x10)], buffer[(0x27)+(i*0x10)] + , buffer[(0x28)+(i*0x10)], buffer[(0x29)+(i*0x10)], buffer[(0x2A)+(i*0x10)], buffer[(0x2B)+(i*0x10)] + , buffer[(0x2C)+(i*0x10)], buffer[(0x2D)+(i*0x10)], buffer[(0x2E)+(i*0x10)], buffer[(0x2F)+(i*0x10)] + , buffer[(0x30)+(i*0x10)], buffer[(0x31)+(i*0x10)], buffer[(0x32)+(i*0x10)], buffer[(0x33)+(i*0x10)] + , buffer[(0x34)+(i*0x10)], buffer[(0x35)+(i*0x10)], buffer[(0x36)+(i*0x10)], buffer[(0x37)+(i*0x10)] + , buffer[(0x38)+(i*0x10)], buffer[(0x39)+(i*0x10)], buffer[(0x3A)+(i*0x10)], buffer[(0x3B)+(i*0x10)] + , buffer[(0x3C)+(i*0x10)], buffer[(0x3D)+(i*0x10)], buffer[(0x3E)+(i*0x10)], buffer[(0x3F)+(i*0x10)] + , buffer[(0x40)+(i*0x10)], buffer[(0x41)+(i*0x10)], buffer[(0x42)+(i*0x10)], buffer[(0x43)+(i*0x10)] + , buffer[(0x44)+(i*0x10)], buffer[(0x45)+(i*0x10)], buffer[(0x46)+(i*0x10)], buffer[(0x47)+(i*0x10)] + , buffer[(0x48)+(i*0x10)], buffer[(0x49)+(i*0x10)], buffer[(0x4A)+(i*0x10)], buffer[(0x4B)+(i*0x10)] + , buffer[(0x4C)+(i*0x10)], buffer[(0x4D)+(i*0x10)], buffer[(0x4E)+(i*0x10)], buffer[(0x4F)+(i*0x10)] + , i); + } + mvwprintw(scr, ln++, 0, "cmd_buf:\r"); + for (uint16_t i = 0; i < 12; i+=5) { + mvwprintw(scr, ln++, 0, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x, i: $%02X\r" + , cmd_buf[(0x00)+(i*0x10)], cmd_buf[(0x01)+(i*0x10)], cmd_buf[(0x02)+(i*0x10)], cmd_buf[(0x03)+(i*0x10)] + , cmd_buf[(0x04)+(i*0x10)], cmd_buf[(0x05)+(i*0x10)], cmd_buf[(0x06)+(i*0x10)], cmd_buf[(0x07)+(i*0x10)] + , cmd_buf[(0x08)+(i*0x10)], cmd_buf[(0x09)+(i*0x10)], cmd_buf[(0x0A)+(i*0x10)], cmd_buf[(0x0B)+(i*0x10)] + , cmd_buf[(0x0C)+(i*0x10)], cmd_buf[(0x0D)+(i*0x10)], cmd_buf[(0x0E)+(i*0x10)], cmd_buf[(0x0F)+(i*0x10)] + , cmd_buf[(0x10)+(i*0x10)], cmd_buf[(0x11)+(i*0x10)], cmd_buf[(0x12)+(i*0x10)], cmd_buf[(0x13)+(i*0x10)] + , cmd_buf[(0x14)+(i*0x10)], cmd_buf[(0x15)+(i*0x10)], cmd_buf[(0x16)+(i*0x10)], cmd_buf[(0x17)+(i*0x10)] + , cmd_buf[(0x18)+(i*0x10)], cmd_buf[(0x19)+(i*0x10)], cmd_buf[(0x1A)+(i*0x10)], cmd_buf[(0x1B)+(i*0x10)] + , cmd_buf[(0x1C)+(i*0x10)], cmd_buf[(0x1D)+(i*0x10)], cmd_buf[(0x1E)+(i*0x10)], cmd_buf[(0x1F)+(i*0x10)] + , cmd_buf[(0x20)+(i*0x10)], cmd_buf[(0x21)+(i*0x10)], cmd_buf[(0x22)+(i*0x10)], cmd_buf[(0x23)+(i*0x10)] + , cmd_buf[(0x24)+(i*0x10)], cmd_buf[(0x25)+(i*0x10)], cmd_buf[(0x26)+(i*0x10)], cmd_buf[(0x27)+(i*0x10)] + , cmd_buf[(0x28)+(i*0x10)], cmd_buf[(0x29)+(i*0x10)], cmd_buf[(0x2A)+(i*0x10)], cmd_buf[(0x2B)+(i*0x10)] + , cmd_buf[(0x2C)+(i*0x10)], cmd_buf[(0x2D)+(i*0x10)], cmd_buf[(0x2E)+(i*0x10)], cmd_buf[(0x2F)+(i*0x10)] + , cmd_buf[(0x30)+(i*0x10)], cmd_buf[(0x31)+(i*0x10)], cmd_buf[(0x32)+(i*0x10)], cmd_buf[(0x33)+(i*0x10)] + , cmd_buf[(0x34)+(i*0x10)], cmd_buf[(0x35)+(i*0x10)], cmd_buf[(0x36)+(i*0x10)], cmd_buf[(0x37)+(i*0x10)] + , cmd_buf[(0x38)+(i*0x10)], cmd_buf[(0x39)+(i*0x10)], cmd_buf[(0x3A)+(i*0x10)], cmd_buf[(0x3B)+(i*0x10)] + , cmd_buf[(0x3C)+(i*0x10)], cmd_buf[(0x3D)+(i*0x10)], cmd_buf[(0x3E)+(i*0x10)], cmd_buf[(0x3F)+(i*0x10)] + , cmd_buf[(0x40)+(i*0x10)], cmd_buf[(0x41)+(i*0x10)], cmd_buf[(0x42)+(i*0x10)], cmd_buf[(0x43)+(i*0x10)] + , cmd_buf[(0x44)+(i*0x10)], cmd_buf[(0x45)+(i*0x10)], cmd_buf[(0x46)+(i*0x10)], cmd_buf[(0x47)+(i*0x10)] + , cmd_buf[(0x48)+(i*0x10)], cmd_buf[(0x49)+(i*0x10)], cmd_buf[(0x4A)+(i*0x10)], cmd_buf[(0x4B)+(i*0x10)] + , cmd_buf[(0x4C)+(i*0x10)], cmd_buf[(0x4D)+(i*0x10)], cmd_buf[(0x4E)+(i*0x10)], cmd_buf[(0x4F)+(i*0x10)] + , i); + } + mvwprintw(scr, ln++, 0, "bitabl:\r"); + mvwprintw(scr, ln++, 0, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\r" + , bitabl[0x00], bitabl[0x01], bitabl[0x02], bitabl[0x03] + , bitabl[0x04], bitabl[0x05], bitabl[0x06], bitabl[0x07] + , bitabl[0x08], bitabl[0x09], bitabl[0x0A], bitabl[0x0B] + , bitabl[0x0C], bitabl[0x0D], bitabl[0x0E], bitabl[0x0F]); + + wrefresh(scr); + #endif + ch = wgetch(scr); + if (ch == 0x11) { + end = 1; + continue; + } + if (!getkey(ch)) { + if (dabbed()) { + print_str(string2); + print_str(cmd_buf); + print_char('\n'); + } + next = 1; + } + } + endwin(); + free(buffer); + free(cmd_buf); + return 0; +} |