Skip to content

Commit

Permalink
cleaner exit on error
Browse files Browse the repository at this point in the history
also, on all exits, call rl_deprep_terminal to make sure we've
re-enabled echoing and stuff.

fixes #12
  • Loading branch information
codekitchen committed Jun 10, 2020
1 parent 6457a91 commit 0bb6db4
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,38 @@ const size_t prompt_width = 10;
bool truncate_lines = false;
int s_lines, s_cols;

// valid terminfo short commands are available at `man 5 terminfo`
// https://man7.org/linux/man-pages/man5/terminfo.5.html
char cmdbuf[100];
void termput0(char *cmd) {
char *b = cmdbuf;
putp(tgetstr(cmd, &b));
}
void termput1(char *cmd, int arg1) {
char *b = cmdbuf;
char *o = tgetstr(cmd, &b);
putp(tgoto(o, 0, arg1));
}

void cleanup_abort(int ecode) {
printf("\n");
termput0("cd");
rl_deprep_terminal();
exit(ecode);
}

int abort_ltz(int res) {
if (res < 0) {
perror(NULL);
exit(res);
cleanup_abort(res);
}
return res;
}

int abort_nz(int res) {
if (res != 0) {
perror(NULL);
exit(res);
cleanup_abort(res);
}
return res;
}
Expand All @@ -51,24 +71,11 @@ int abort_nz(int res) {
typeof(ptr) _ptr = ptr; \
if (!_ptr) { \
perror(NULL); \
exit(-1); \
cleanup_abort(-1); \
} \
_ptr; \
})

// valid terminfo short commands are available at `man 5 terminfo`
// https://man7.org/linux/man-pages/man5/terminfo.5.html
char cmdbuf[100];
void termput0(char *cmd) {
char *b = cmdbuf;
putp(tgetstr(cmd, &b));
}
void termput1(char *cmd, int arg1) {
char *b = cmdbuf;
char *o = tgetstr(cmd, &b);
putp(tgoto(o, 0, arg1));
}

// Read a single line from s and print it out. Once max_display_len is reached,
// keep scanning for the rest of the line but don't print anymore.
ssize_t read_line(FILE *s, size_t max_display_len) {
Expand Down Expand Up @@ -257,9 +264,7 @@ int setup() {
}

void cleanup(int sig) {
printf("\n");
termput0("cd");
exit(0);
cleanup_abort(0);
}

static struct option const long_options[] = {
Expand Down

0 comments on commit 0bb6db4

Please sign in to comment.