fix read of command scripts and script-mode - gramscii - A simple editor for ASCII box-and-arrow charts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0522ef659553292f546fb1f3af43cfd2f4b07bb2
 (DIR) parent a46183e96042cf751199f0a06d437a599f7f5bf8
 (HTM) Author: KatolaZ <katolaz@freaknet.org>
       Date:   Mon, 12 Aug 2019 09:02:50 +0100
       
       fix read of command scripts and script-mode
       
       Diffstat:
         M TODO                                |       2 +-
         M gramscii.1                          |       4 ----
         M gramscii.c                          |      28 ++++++++++++++++++----------
         M gramscii.h                          |       2 +-
         M lineset.c                           |       2 +-
         M screen.c                            |      34 +++++++++++++++++--------------
       
       6 files changed, 40 insertions(+), 32 deletions(-)
       ---
 (DIR) diff --git a/TODO b/TODO
       @@ -1,5 +1,4 @@
        + optimize redraws (redraw only the modified rectangle)
       -- fix bug in reading commands from files
        - add screen geometry option (-g 25x80?)
        - maybe move "text" mode to "t"
        - implement ellipse
       @@ -15,6 +14,7 @@
        - allow scrolling (both vertical and horizontal)
        - catch SIGWINCH and react appropriately (after scrolling is 
          enabled)
       +* fix bug in reading commands from files
        * fix bug in visual crop
        * read file at point
          * read output of command (!)
 (DIR) diff --git a/gramscii.1 b/gramscii.1
       @@ -575,10 +575,6 @@ would automatically save the screen into "filename".
        gramscii currently manages only a fixed screen of the same size of the
        screen where it starts from. This will be changed in a future release to
        support scrolling and "virtual" screens of any (reasonable) size.
       -.PP
       -There is currently a bug with files read before accepting commands: the
       -result of the commands read from the files is not show immediately, and not
       -shown correctly.
        .SH AUTHORS
        gramscii is written and maintained by Vincenzo "KatolaZ" Nicosia
        <katolaz@freaknet.org>. You can use, copy, modify, and redistribute
 (DIR) diff --git a/gramscii.c b/gramscii.c
       @@ -35,11 +35,12 @@ char *argv0;
        
        void cleanup(int s){
                
       -        if (!silent)
       +        if (!script){
                        printf("\033[;H\033[2J");
       +                tcsetattr(0, TCSANOW, &t1);
       +        }
                else
                        dump_lines(screen, stdout);
       -        tcsetattr(0, TCSANOW, &t1);
                fflush(stdout);
                exit(s);
        }
       @@ -53,11 +54,12 @@ void init(){
                signal(SIGTERM, cleanup);
                signal(SIGQUIT, cleanup);
        
       -        tcgetattr(0, &t1);
       -        t2 = t1;
       -        t2.c_lflag &= ~(ICANON | ECHO);
       -        tcsetattr(0, TCSANOW, &t2);
       -
       +        if (!script){
       +                tcgetattr(0, &t1);
       +                t2 = t1;
       +                t2.c_lflag &= ~(ICANON | ECHO);
       +                tcsetattr(0, TCSANOW, &t2);
       +        }
                init_screen();
                x = 0;
                y = 0;
       @@ -72,9 +74,12 @@ void init(){
        
        void commands(FILE *fc){
        
       -        char c;
       +        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;
       @@ -156,7 +161,7 @@ int main(int argc, char *argv[]){
        
                ARGBEGIN {
                        case 's':
       -                        silent = 1;
       +                        script = 1;
                                break;
                        case 'h': /* FALLTHROUGH */
                        default:
       @@ -171,12 +176,15 @@ int main(int argc, char *argv[]){
                        }
                        else {
                                commands(fc);
       +                        fflush(fc);
                                fclose(fc);
                                redraw();
                        }
                        argv++;
                        argc--;
                }
       -        commands(stdin);
       +        if (!script)
       +                commands(stdin);
       +        cleanup(0);
                return 0;
        }
 (DIR) diff --git a/gramscii.h b/gramscii.h
       @@ -129,7 +129,7 @@ char modified; /* set to 1 if screen modified since last save */
        char fname[256];
        
        
       -char silent; /* set to 1 in script-mode */
       +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 */
 (DIR) diff --git a/lineset.c b/lineset.c
       @@ -62,7 +62,7 @@ void ensure_num_lines(lineset_t *ls, int n){
        void dump_lines(lineset_t ls, FILE *f){
                int i;
                for (i=0; i<ls.num ;i++){
       -                fprintf(f, "%d:%s\n", i, ls.l[i].s);
       +                fprintf(f, "%s\n", ls.l[i].s);
                }
                fflush(f);
        }
 (DIR) diff --git a/screen.c b/screen.c
       @@ -58,7 +58,7 @@ char get_mark(char dir){
        
        void status_bar(){
        
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[%d;1f\033[7m", HEIGHT+1);
                printf("%*s", WIDTH-1, "");
       @@ -78,7 +78,7 @@ void status_bar(){
        
        char get_key(FILE *fc, char *msg){
        
       -        if (silent)
       +        if (script)
                        return 0;
                printf("\033[%d;1f\033[7m", HEIGHT+1);
                printf("%*s", WIDTH, "");
       @@ -92,7 +92,7 @@ char get_key(FILE *fc, char *msg){
        
        void get_string(FILE *fc, char *msg, char *s, int sz){
        
       -        if (!silent){
       +        if (!script){
                        printf("\033[%d;1f\033[7m", HEIGHT+1);
                        printf("%*s", WIDTH, "");
                        printf("\033[%d;1f\033[7m", HEIGHT+1);
       @@ -106,9 +106,10 @@ void get_string(FILE *fc, char *msg, char *s, int sz){
                }
                fgets(s, sz, fc);
                s[strlen(s)-1] = '\0';
       -        tcsetattr(0, TCSANOW, &t2);
       -        if (!silent)
       +        if (!script){
       +                tcsetattr(0, TCSANOW, &t2);
                        fflush(stdout);
       +        }
        }
        
        int is_yes(char c){
       @@ -119,7 +120,7 @@ int is_yes(char c){
        
        
        void show_cursor(){
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[%d;%df", y+1, x+1);
                fflush(stdout);
       @@ -144,7 +145,7 @@ void set_cur(char c){
        
        void draw_xy(int x, int y, char c){
                /* FIXME: check if x and y are valid!!!! */
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[%d;%df",y+1,x+1);
                putchar(c);
       @@ -152,7 +153,7 @@ void draw_xy(int x, int y, char c){
        }
        
        void update_current(){
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[%d;%df",y+1,x+1);
                putchar(screen.l[y].s[x]);
       @@ -226,7 +227,7 @@ void reset_styles(){
        void redraw(){
                int i;
        
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[2J\033[1;1H");
                for (i=0;i<HEIGHT;i++){
       @@ -254,9 +255,9 @@ void go_to(int where){
                show_cursor();
        }
        
       -void handle_goto(char global){
       +void handle_goto(FILE *fc, char global){
                char c;        
       -        c=getchar();
       +        c=fgetc(fc);
                switch(c){
                        case 'h':
                                dir = DIR_L;
       @@ -348,6 +349,9 @@ int move_around(char c, FILE *fc, char global){
                        mult += c - '0';
                        return 0;
                }
       +#ifdef DEBUG
       +        fprintf(stderr, "got char: %c\n", c);
       +#endif
                switch(c){
                        case 27: /* control sequence? */
                                c = get_escape(fc);
       @@ -382,11 +386,11 @@ int move_around(char c, FILE *fc, char global){
                                break;
                        case 'g':
        #ifdef DEBUG
       -                        fprintf(stderr, "before global: step: %d x: %d y: %d\n", step, x, y);
       +                        fprintf(stderr, "before handle_goto: step: %d x: %d y: %d global: %d\n", step, x, y, global);
        #endif
       -                        handle_goto(global);
       +                        handle_goto(fc, global);
        #ifdef DEBUG
       -                        fprintf(stderr, "after global: step: %d x: %d y: %d\n", step, x, y);
       +                        fprintf(stderr, "after handle_goto: step: %d x: %d y: %d global: %d\n", step, x, y, global);
        #endif
                                break;
                        default:
       @@ -398,7 +402,7 @@ int move_around(char c, FILE *fc, char global){
        
        
        void set_video(int v){
       -        if (silent)
       +        if (script)
                        return;
                printf("\033[%dm", v);
                fflush(stdout);