diff options
author | KatolaZ <katolaz@freaknet.org> | 2019-07-27 06:57:12 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2019-07-27 06:57:12 +0100 |
commit | 8e3d23b921d9bbcb7c53017bacff8a3050a34b55 (patch) | |
tree | fc26ae9768e6c72827b03e973dbf0479b0a0431d /main.c | |
parent | 3d53fcefe3ca3e6f25d1731c2caa0c1c0e676453 (diff) |
reorganise code
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 175 |
1 files changed, 175 insertions, 0 deletions
@@ -0,0 +1,175 @@ +/* +* +* gramscii: a simple editor for ASCII box-and-arrow charts +* +* Copyright (c) 2019 Vincenzo "KatolaZ" Nicosia <katolaz@freaknet.org> +* +* 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 <https://www.gnu.org/licenses/>. +* +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> + +#include "arg.h" +#include "gramscii.h" + + +char *argv0; + +void dump_lines(){ + int i; + for (i=0; i<HEIGHT; i++){ + printf("%s\n", screen[i].s); + } +} + +void cleanup(int s){ + + if (!silent) + printf("\033[;H\033[2J"); + else + dump_lines(); + tcsetattr(0, TCSANOW, &t1); + fflush(stdout); + exit(0); +} + +void exit_cleanup(void){ + cleanup(0); +} + +/*** Initialisation ***/ + +void init(){ + + signal(SIGHUP, cleanup); + signal(SIGINT, cleanup); + signal(SIGTERM, cleanup); + signal(SIGQUIT, cleanup); + atexit(exit_cleanup); + + tcgetattr(0, &t1); + t2 = t1; + t2.c_lflag &= ~(ICANON | ECHO); + tcsetattr(0, TCSANOW, &t2); + + init_screen(); + x = 0; + y = 0; + modified = 0; + fname[0] = '\0'; + redraw(); +} + + +/*** Commands ***/ + +void commands(FILE *fc){ + + char c; + while((c=fgetc(fc))!=EOF){ + if (!change_style(c) && !move_around(c, fc)){ + switch(c){ + case 'i': + mode = TEXT; + get_text(fc); + break; + case 'R': + redraw(); + break; + case 'b': + mode = BOX; + get_box(fc); + 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 'q': + check_modified(fc);/** FALLTHROUGH **/ + case 'Q': + exit(0); + break; + } + } + check_bound(); + 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': + silent = 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); + fclose(fc); + redraw(); + } + argv++; + argc--; + } + commands(stdin); + return 0; +} |