/* * * gramscii: a simple editor for ASCII box-and-arrow charts * * Copyright (c) 2019 Vincenzo "KatolaZ" Nicosia * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. Please see the attached file COPYING. * Otherwise, please visit . * */ #define _POSIX_C_SOURCE 200112L #include #include #include #include "arg.h" #include "gramscii.h" /** global variables **/ lineset_t screen; /* what is visualised */ lineset_t cutbuf; /* cut/paste buffer */ lineset_t *undo; /* undo list */ pos_t marks[26]; /* position marks */ char mark_map[26]; /* marks map */ int undo_sz;/* allocated size of undo list*/ int undo_cur;/* undo position */ int undo_lst;/* last valid undo position */ int WIDTH, HEIGHT; int mode;/* mode */ int dir;/* line direction */ int x; int y; int step;/* current step */ int mult;/* current multiplier */ int force_new; char corner; /* number of available markers for each type */ int hlines_sz; int vlines_sz; int corners_sz; int stmarks_sz; int endmarks_sz; /**/ /* line and arrow markers */ int cur_hl, cur_vl, cur_corn, cur_start, cur_end; char line_h; char line_v; char mark_st; char mark_end; /**/ char modified; /* set to 1 if screen modified since last save */ char fname[256]; char script; /* set to 1 in script-mode */ char autoend; /* set to 1 in auto-arrow mode */ /* Used by draw_arrow to identify the bounding box */ int a_miny; int a_maxy; /**/ struct termios t1, t2, t3; /** End of global variables **/ char *argv0; void cleanup(int s){ if (!script){ printf("\033[;H\033[2J"); tcsetattr(0, TCSANOW, &t1); } else dump_lines(screen, stdout); fflush(stdout); exit(s); } /*** Initialisation ***/ void init(){ signal(SIGHUP, cleanup); signal(SIGINT, cleanup); signal(SIGTERM, cleanup); signal(SIGQUIT, cleanup); if (!script){ tcgetattr(0, &t1); t2 = t1; t2.c_lflag &= ~(ICANON | ECHO); tcsetattr(0, TCSANOW, &t2); } init_screen(); x = 0; y = 0; step = 1; modified = 0; fname[0] = '\0'; redraw(); } /*** Commands ***/ void commands(FILE *fc){ int c; while((c=fgetc(fc))!=EOF){ if (!change_style(c) && !move_around(c, fc, 1)){ #ifdef DEBUG fprintf(stderr, "got command: %c\n", c); #endif switch(c){ case 'i': mode = TEXT; get_text(fc); break; case 'R': redraw(); break; case 'b': mode = BOX; get_box(fc, BOX_RECT); break; case 'A': autoend=1; case 'a': mode = ARROW; get_arrow(fc); autoend = 0; break; case 'W': force_new = 1;/** FALLTHROUGH **/ case 'w': write_file(fc); break; case 'e': check_modified(fc);/** FALLTHROUGH **/ case 'E': load_file(fc); break; case 'N': new_file(fc); break; case 'x': mode = DEL; erase(fc); break; case 'v': mode = VIS; visual_box(fc); break; case 'C': crop_to_nonblank(); break; case 'p': paste(); break; case 'u': undo_change(); break; case 'U': redo_change(); break; case 'r': read_file_at(fc, x, y); break; case 't': mode = TRP; get_box(fc, BOX_TRAP_UC); break; case 'z': mode = PAR; get_box(fc, BOX_PARR); break; case '#': mode = REM; get_comment(fc); break; case 'M': mark_pos(fc); break; case 'q': check_modified(fc);/** FALLTHROUGH **/ case 'Q': cleanup(0); break; } } check_bound(&x, &y); status_bar(); show_cursor(); step = 1; force_new = 0; } } void usage(){ fprintf(stderr, "Usage: %s [-s] [-h] [file ...]\n", argv0); exit(1); } int main(int argc, char *argv[]){ FILE *fc; ARGBEGIN { case 's': script = 1; break; case 'h': /* FALLTHROUGH */ default: usage(); } ARGEND; init(); while (argc){ fc = fopen(argv[0], "r"); if (fc == NULL){ fprintf(stderr, "Error opening file %s\n", argv[0]); } else { commands(fc); fflush(fc); fclose(fc); redraw(); } argv++; argc--; } commands(stdin); cleanup(0); }