1
0

peek(1): handle ^C elegantly

This commit is contained in:
dtb 2024-04-17 14:21:11 -06:00
parent 3ba22dbaf7
commit 6b90e0e1da

View File

@ -1,6 +1,7 @@
#include <signal.h> /* sigaction(2), struct sigaction, SIGINT */
#include <stdio.h> /* fprintf(3), getc(3), perror(3), putc(3), stderr, stdin, #include <stdio.h> /* fprintf(3), getc(3), perror(3), putc(3), stderr, stdin,
* stdout, EOF, NULL */ * stdout, EOF, NULL */
#include <stdlib.h> /* size_t */ #include <stdlib.h> /* exit(3), size_t, EXIT_FAILURE */
#include <string.h> /* strerror(3) */ #include <string.h> /* strerror(3) */
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE #if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
# include <sysexits.h> # include <sysexits.h>
@ -12,16 +13,31 @@
static int oserr(char *s){ perror(s); return EX_OSERR; } static int oserr(char *s){ perror(s); return EX_OSERR; }
static char *program_name = "peek"; static char *program_name = "peek";
static int usage(char *s){ static int usage(char *s){
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n", fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n",
s == NULL ? program_name : s); s == NULL ? program_name : s);
return EX_USAGE; return EX_USAGE;
} }
/* Restores terminal echo; otherwise when a user ^Cs the terminal would
* continue to not display typed text. If sig isn't zero, this will terminate
* the program. */
void restore_echo(int sig){
static struct termios t;
tcgetattr(STDIN_FILENO, &t);
t.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
if(sig != 0)
exit(EXIT_FAILURE);
else
return;
}
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int c;
int eof; int eof;
size_t i;
char include_eof; char include_eof;
FILE *outputs[] = { FILE *outputs[] = {
NULL /* stdout */, NULL /* stdout */,
@ -29,31 +45,34 @@ int main(int argc, char *argv[]){
NULL /* -p */ NULL /* -p */
}; };
int p[2] = {0, 0}; int p[2] = {0, 0};
struct termios t;
if(argc < 1) if(argc < 1)
usage(argv[0]); usage(argv[0]);
eof = EOF; eof = EOF;
include_eof = 0; include_eof = 0;
while((c = getopt(argc, argv, "1enopt")) != -1) { /* options parsing */
switch(c){ int c;
case '1': eof = '\n'; break;
case 'n': include_eof = 1; break; while((c = getopt(argc, argv, "1enopt")) != -1)
case 'o': outputs[0] = stdout; break; switch(c){
case 'e': outputs[1] = stderr; break; case '1': eof = '\n'; break;
case 'p': case 'n': include_eof = 1; break;
if(pipe(p) != 0 || (outputs[2] = fdopen(p[1], "ab")) == NULL) case 'o': outputs[0] = stdout; break;
return oserr(argv[0]); case 'e': outputs[1] = stderr; break;
break; case 'p':
case 't': if(pipe(p) != 0 || (outputs[2] = fdopen(p[1], "ab")) == NULL)
if(isatty(STDIN_FILENO) != 1){ return oserr(argv[0]);
fprintf(stderr, "%s: Must be run in a terminal" break;
" (option -t specified)\n", argv[0]); case 't':
return EX_USAGE; if(isatty(STDIN_FILENO) != 1){
fprintf(stderr, "%s: Must be run in a terminal"
" (option -t specified)\n", argv[0]);
return EX_USAGE;
}
default: return usage(argv[0]);
} }
default: return usage(argv[0]); }
}
/* If -p is used there must be additional arguments. getopt(3) wouldn't /* If -p is used there must be additional arguments. getopt(3) wouldn't
* work for this because optarg would have to be one string to give to * work for this because optarg would have to be one string to give to
@ -75,23 +94,39 @@ int main(int argc, char *argv[]){
return oserr(argv[0]); return oserr(argv[0]);
} }
/* terminal echo */ { /* install signal handler for ^C to avoid terminal phooeyness */
tcgetattr(STDIN_FILENO, &t); struct sigaction act = { 0 };
t.c_lflag ^= ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
do{ if((c = getc(stdin)) != eof || include_eof) act.sa_handler = restore_echo;
for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i) /* This error isn't terminating because the worst that happens is some
if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){ * terminal phooeyness if things go awry. */
if(outputs[i] != stdout && outputs[i] != stderr) if(sigaction(SIGINT, &act, NULL) != 0)
fclose(outputs[i]); perror(argv[0]);
outputs[i] = NULL; }
}
}while(c != eof); { /* banish terminal echo */
struct termios t;
tcgetattr(STDIN_FILENO, &t); tcgetattr(STDIN_FILENO, &t);
t.c_lflag |= ECHO; t.c_lflag ^= ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
}
{ /* actual input loop */
int c;
size_t i;
do{ if((c = getc(stdin)) != eof || include_eof)
for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i)
if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){
if(outputs[i] != stdout && outputs[i] != stderr)
fclose(outputs[i]);
outputs[i] = NULL;
}
}while(c != eof);
}
restore_echo(0);
return EX_OK; return EX_OK;
} }