Skip to content

Restore terminal background colour on quit #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Restores terminal background colour on exit
NOTE: Standard output from Direwolf is not affected.
  • Loading branch information
Nate Morrison committed Mar 22, 2022
commit bc07681df9686d380241063ffdff69242d28e4b9
13 changes: 13 additions & 0 deletions src/aclients.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ int main (int argc, char *argv[])

if (num_clients < 1 || num_clients > MAX_CLIENTS) {
printf ("Specify up to %d TNCs on the command line.\n", MAX_CLIENTS);
dw_printf ("\e[0m\e\n\e[0J\e");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like dw_printf ("\e[0m\e\n\e[0J\e"); always goes with exit (x);
Consider to combine those two. It might look like:

// Exit from DW and restore terminal color
void dw_exit(int exit_code)
{
    dw_printf ("\e[0m\e\n\e[0J\e");
    exit (exit_code);
}
dw_exit(1);

exit (1);
}

Expand All @@ -236,13 +237,15 @@ int main (int argc, char *argv[])
p = strtok (stemp, "=");
if (p == NULL) {
printf ("Internal error 1\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
strlcpy (hostname[j], "localhost", sizeof(hostname[j]));
strlcpy (port[j], p, sizeof(port[j]));
p = strtok (NULL, "=");
if (p == NULL) {
printf ("Missing description after %s\n", port[j]);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
strlcpy (description[j], p, sizeof(description[j]));
Expand Down Expand Up @@ -291,6 +294,7 @@ int main (int argc, char *argv[])
}
if (client_th[j] == NULL) {
printf ("Internal error: Could not create client thread %d.\n", j);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
#else
Expand All @@ -302,6 +306,7 @@ int main (int argc, char *argv[])
}
if (e != 0) {
perror("Internal error: Could not create client thread.");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
#endif
Expand Down Expand Up @@ -438,6 +443,7 @@ static void * client_thread_net (void *arg)
hostname[my_index], gai_strerror(err));
#endif
freeaddrinfo(ai_head);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down Expand Up @@ -536,6 +542,7 @@ static void * client_thread_net (void *arg)

printf("Client %d unable to connect to %s on %s (%s), port %s\n",
my_index, description[my_index], hostname[my_index], ipaddr_str, port[my_index] );
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down Expand Up @@ -563,6 +570,7 @@ static void * client_thread_net (void *arg)

if (n != sizeof(mon_cmd)) {
printf ("Read error, client %d received %d command bytes. Terminating.\n", my_index, n);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -577,6 +585,7 @@ static void * client_thread_net (void *arg)

if (n != mon_cmd.data_len) {
printf ("Read error, client %d received %d data bytes.\n", my_index, n);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
}
Expand Down Expand Up @@ -682,6 +691,7 @@ static void * client_thread_serial (void *arg)
if (fd == MYFDERROR) {
printf("Client %d unable to connect to %s on %s.\n",
my_index, description[my_index], port[my_index] );
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down Expand Up @@ -732,6 +742,7 @@ static void * client_thread_serial (void *arg)
if (fd == MYFDERROR) {
printf("Client %d unable to connect to %s on %s.\n",
my_index, description[my_index], port[my_index] );
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down Expand Up @@ -781,6 +792,7 @@ static void * client_thread_serial (void *arg)
if (! ReadFile (fd, &ch, 1, &n, NULL)) {
printf ("Read error on %s.\n", description[my_index]);
CloseHandle (fd);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -790,6 +802,7 @@ static void * client_thread_serial (void *arg)
if ( ( n = read(fd, & ch, 1)) < 0) {
printf ("Read error on %s.\n", description[my_index]);
close (fd);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/appserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static void usage()
dw_printf (" mycall is required because that is the callsign for \n");
dw_printf (" which the TNC will accept connections. \n");
dw_printf (" \n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand Down Expand Up @@ -192,6 +193,7 @@ int main (int argc, char *argv[])
if (agwlib_init (tnc_hostname, tnc_port, agwlib_G_ask_port_information) != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not attach to network TNC %s:%s.\n", tnc_hostname, tnc_port);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand Down
16 changes: 16 additions & 0 deletions src/atest.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ int main (int argc, char *argv[])
if (decimate < 1 || decimate > 8) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Unreasonable value for -D.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}
dw_printf ("Divide audio sample rate by %d\n", decimate);
Expand All @@ -331,6 +332,7 @@ int main (int argc, char *argv[])
if (upsample < 1 || upsample > 4) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Unreasonable value for -U.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}
dw_printf ("Multiply audio sample rate by %d\n", upsample);
Expand All @@ -344,6 +346,7 @@ int main (int argc, char *argv[])
if (my_audio_config.achan[0].fix_bits < RETRY_NONE || my_audio_config.achan[0].fix_bits >= RETRY_MAX) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid Fix Bits level.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}
break;
Expand Down Expand Up @@ -424,6 +427,7 @@ int main (int argc, char *argv[])
if (my_audio_config.achan[0].baud < MIN_BAUD || my_audio_config.achan[0].baud > MAX_BAUD) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Use a more reasonable bit rate in range of %d - %d.\n", MIN_BAUD, MAX_BAUD);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand Down Expand Up @@ -547,6 +551,7 @@ int main (int argc, char *argv[])
text_color_set(DW_COLOR_ERROR);
dw_printf ("Couldn't open file for read: %s\n", argv[optind]);
//perror ("more info?");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand All @@ -561,6 +566,7 @@ int main (int argc, char *argv[])
if (strncmp(header.riff, "RIFF", 4) != 0 || strncmp(header.wave, "WAVE", 4) != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("This is not a .WAV format file.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand All @@ -574,11 +580,13 @@ int main (int argc, char *argv[])
if (strncmp(chunk.id, "fmt ", 4) != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("WAV file error: Found \"%4.4s\" where \"fmt \" was expected.\n", chunk.id);
dw_printf ("\e[0m\e\n\e[0J\e");
exit(EXIT_FAILURE);
}
if (chunk.datasize != 16 && chunk.datasize != 18) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("WAV file error: Need fmt chunk datasize of 16 or 18. Found %d.\n", chunk.datasize);
dw_printf ("\e[0m\e\n\e[0J\e");
exit(EXIT_FAILURE);
}

Expand All @@ -589,24 +597,28 @@ int main (int argc, char *argv[])
if (strncmp(wav_data.data, "data", 4) != 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("WAV file error: Found \"%4.4s\" where \"data\" was expected.\n", wav_data.data);
dw_printf ("\e[0m\e\n\e[0J\e");
exit(EXIT_FAILURE);
}

if (format.wformattag != 1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Sorry, I only understand audio format 1 (PCM). This file has %d.\n", format.wformattag);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

if (format.nchannels != 1 && format.nchannels != 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Sorry, I only understand 1 or 2 channels. This file has %d.\n", format.nchannels);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

if (format.wbitspersample != 8 && format.wbitspersample != 16) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Sorry, I only understand 8 or 16 bits per sample. This file has %d.\n", format.wbitspersample);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

Expand Down Expand Up @@ -709,14 +721,17 @@ int main (int argc, char *argv[])
if (error_if_less_than != -1 && packets_decoded_total < error_if_less_than) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("\n * * * TEST FAILED: number decoded is less than %d * * * \n", error_if_less_than);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}
if (error_if_greater_than != -1 && packets_decoded_total > error_if_greater_than) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("\n * * * TEST FAILED: number decoded is greater than %d * * * \n", error_if_greater_than);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
}

dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_SUCCESS);
}

Expand Down Expand Up @@ -1008,6 +1023,7 @@ static void usage (void) {
dw_printf (" Try different combinations of options to compare decoding\n");
dw_printf (" performance.\n");

dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down
1 change: 1 addition & 0 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ int audio_get (int a)
if (res <= 0) {
text_color_set(DW_COLOR_INFO);
dw_printf ("\nEnd of file on stdin. Exiting.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (0);
}

Expand Down
1 change: 1 addition & 0 deletions src/audio_portaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ int audio_get (int a)
if (res <= 0) {
text_color_set(DW_COLOR_INFO);
dw_printf ("\nEnd of file on stdin. Exiting.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (0);
}

Expand Down
1 change: 1 addition & 0 deletions src/audio_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ int audio_get (int a)
if (res <= 0) {
text_color_set(DW_COLOR_INFO);
dw_printf ("\nEnd of file on stdin. Exiting.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (0);
}

Expand Down
1 change: 1 addition & 0 deletions src/ax25_pad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ int main ()
dw_printf ("\n----------\n\n");
dw_printf ("\nSUCCESS!\n");

dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_SUCCESS);

} /* end main */
Expand Down
1 change: 1 addition & 0 deletions src/cm108.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ int main (int argc, char *argv[])
if (argc != 2) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Specify HID path on command line.\n");
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down
3 changes: 3 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,7 @@ void config_init (char *fname, struct audio_s *p_audio_config,
#if __WIN32__
text_color_set(DW_COLOR_ERROR);
dw_printf ("Config file line %d: Windows version of direwolf does not support HAMLIB.\n", line);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
#else
text_color_set(DW_COLOR_ERROR);
Expand Down Expand Up @@ -1919,6 +1920,7 @@ void config_init (char *fname, struct audio_s *p_audio_config,
dw_printf ("You must rebuild direwolf with CM108 Audio Adapter GPIO PTT support.\n");
dw_printf ("See User Guide for details.\n");
#endif
dw_printf ("\e[0m\e\n\e[0J\e");
exit (EXIT_FAILURE);
#endif
}
Expand Down Expand Up @@ -4219,6 +4221,7 @@ void config_init (char *fname, struct audio_s *p_audio_config,
}
}
//dw_printf ("DEBUG server=%s port=%d\n", p_igate_config->t2_server_name, p_igate_config->t2_server_port);
//dw_printf ("\e[0m\e\n\e[0J\e");
//exit (0);
}

Expand Down
1 change: 1 addition & 0 deletions src/decode_aprs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4805,6 +4805,7 @@ int main (int argc, char *argv[])
if (argc >= 2) {
if (freopen (argv[1], "r", stdin) == NULL) {
fprintf(stderr, "Can't open %s for read.\n", argv[1]);
dw_printf ("\e[0m\e\n\e[0J\e");
exit(1);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/demod.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ void demod_process_sample (int chan, int subchan, int sam)
text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid combination of options. Exiting.\n");
// Would probably work but haven't thought about it or tested yet.
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}
else {
Expand Down
4 changes: 4 additions & 0 deletions src/demod_afsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ void demod_afsk_init (int samples_per_sec, int baud, int mark_freq,

text_color_set(DW_COLOR_ERROR);
dw_printf ("Invalid filter profile = %c\n", profile);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down Expand Up @@ -352,6 +353,7 @@ void demod_afsk_init (int samples_per_sec, int baud, int mark_freq,
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -362,6 +364,7 @@ void demod_afsk_init (int samples_per_sec, int baud, int mark_freq,
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -374,6 +377,7 @@ void demod_afsk_init (int samples_per_sec, int baud, int mark_freq,
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down
3 changes: 3 additions & 0 deletions src/demod_psk.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ void demod_psk_init (enum modem_t modem_type, enum v26_e v26_alt, int samples_pe
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -455,6 +456,7 @@ void demod_psk_init (enum modem_t modem_type, enum v26_e v26_alt, int samples_pe
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -464,6 +466,7 @@ void demod_psk_init (enum modem_t modem_type, enum v26_e v26_alt, int samples_pe
dw_printf ("Decrease the audio sample rate or increase the baud rate or\n");
dw_printf ("recompile the application with MAX_FILTER_SIZE larger than %d.\n",
MAX_FILTER_SIZE);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down
2 changes: 2 additions & 0 deletions src/digipeater.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ int main (int argc, char *argv[])
regerror (e, &alias_re, message, sizeof(message));
text_color_set(DW_COLOR_ERROR);
dw_printf ("\n%s\n\n", message);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand All @@ -745,6 +746,7 @@ int main (int argc, char *argv[])
regerror (e, &wide_re, message, sizeof(message));
text_color_set(DW_COLOR_ERROR);
dw_printf ("\n%s\n\n", message);
dw_printf ("\e[0m\e\n\e[0J\e");
exit (1);
}

Expand Down
Loading