peek(1): update style
This commit is contained in:
parent
3862a95151
commit
d892fa1cac
91
src/peek.c
91
src/peek.c
@ -20,60 +20,67 @@
|
|||||||
#include <stdio.h> /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin,
|
#include <stdio.h> /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin,
|
||||||
* stdout, EOF, NULL */
|
* stdout, EOF, NULL */
|
||||||
#include <stdlib.h> /* exit(3), EXIT_FAILURE */
|
#include <stdlib.h> /* exit(3), EXIT_FAILURE */
|
||||||
#if !defined EX_IOERR || !defined EX_OK || !defined EX_USAGE
|
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_USAGE */
|
||||||
# include <sysexits.h>
|
|
||||||
#endif
|
|
||||||
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
|
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
|
||||||
#include <unistd.h> /* getopt(3), isatty(3), STDIN_FILENO */
|
#include <unistd.h> /* getopt(3), isatty(3), STDIN_FILENO */
|
||||||
|
|
||||||
static char *program_name = "peek";
|
char *program_name = "peek";
|
||||||
|
|
||||||
/* Restores terminal echo; otherwise when a user ^Cs the terminal would
|
/* 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
|
* continue to not display typed text. If sig isn't zero, this will terminate
|
||||||
* the program. */
|
* the program. */
|
||||||
static void restore_echo(int sig){
|
static void
|
||||||
|
restore_echo(int sig) {
|
||||||
static struct termios t;
|
static struct termios t;
|
||||||
|
|
||||||
tcgetattr(STDIN_FILENO, &t);
|
/* Failure isn't reported because this is the termination routine anyway;
|
||||||
t.c_lflag |= ECHO;
|
* errors will be obvious. */
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
if (tcgetattr(STDIN_FILENO, &t) == 0) {
|
||||||
|
t.c_lflag |= ECHO;
|
||||||
|
(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||||
|
}
|
||||||
|
|
||||||
/* If, for whatever ungodly reason, exit(3) returns, the user will notice
|
if (sig != 0) { exit(EXIT_FAILURE); } /* Terminated by signal. */
|
||||||
* their typed characters on the screen. */
|
|
||||||
if(sig != 0)
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ioerr(char *s){ perror(s); restore_echo(0); return EX_IOERR; }
|
static int
|
||||||
static int usage(char *s){
|
ioerr(char *argv0) {
|
||||||
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n", s);
|
perror(argv0);
|
||||||
|
restore_echo(0);
|
||||||
|
return EX_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
usage(char *argv0) {
|
||||||
|
(void)fprintf(stderr, "Usage: %s [-i]\n", argv0);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
|
if (argc > 0) { /* option parsing */
|
||||||
if(argc < 1)
|
|
||||||
return usage(program_name);
|
|
||||||
|
|
||||||
{ /* option parsing */
|
|
||||||
char allow_nonterminals;
|
char allow_nonterminals;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
|
program_name = argv[0];
|
||||||
|
|
||||||
allow_nonterminals = 0;
|
allow_nonterminals = 0;
|
||||||
while((c = getopt(argc, argv, "i")) != -1)
|
while ((c = getopt(argc, argv, "i")) != -1) {
|
||||||
switch(c){
|
switch (c) {
|
||||||
case 'i': allow_nonterminals = 1; break;
|
case 'i': allow_nonterminals = 1; break;
|
||||||
default: return usage(argv[0]);
|
default: return usage(argv[0]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(argc > optind)
|
if (argc > optind) { return usage(argv[0]); }
|
||||||
return usage(argv[0]);
|
|
||||||
|
|
||||||
if(!allow_nonterminals && isatty(STDIN_FILENO) != 1){
|
if (!allow_nonterminals && isatty(STDIN_FILENO) != 1) {
|
||||||
fprintf(stderr, "%s: Must be run in a terminal"
|
(void)fprintf(
|
||||||
" (option -i skips this check)\n", argv[0]);
|
stderr,
|
||||||
|
"%s: Must be run in a terminal (option -i skips this check)\n",
|
||||||
|
argv[0]
|
||||||
|
);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,28 +95,30 @@ int main(int argc, char *argv[]){
|
|||||||
struct sigaction act = { 0 };
|
struct sigaction act = { 0 };
|
||||||
|
|
||||||
act.sa_handler = restore_echo;
|
act.sa_handler = restore_echo;
|
||||||
if(sigaction(SIGINT, &act, NULL) != 0)
|
if(sigaction(SIGINT, &act, NULL) != 0) { perror(program_name); }
|
||||||
perror(argv[0]);
|
|
||||||
#else
|
#else
|
||||||
if(signal(SIGINT, restore_echo) == SIG_ERR)
|
if(signal(SIGINT, restore_echo) == SIG_ERR) { perror(program_name); }
|
||||||
perror(argv[0]);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
{ /* banish terminal echo */
|
/* Banish terminal echo; this terminates when it fails, because this is the
|
||||||
|
* whole point of the program. */
|
||||||
|
{
|
||||||
struct termios t;
|
struct termios t;
|
||||||
|
|
||||||
tcgetattr(STDIN_FILENO, &t);
|
if (tcgetattr(STDIN_FILENO, &t) != 0) { return ioerror(program_name); }
|
||||||
t.c_lflag ^= ECHO;
|
t.c_lflag ^= ECHO;
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) != 0) {
|
||||||
|
return ioerror(program_name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{ /* actual input loop */
|
{ /* Input loop */
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while((c = fgetc(stdin)) != EOF)
|
while ((c = fgetc(stdin)) != EOF) {
|
||||||
if(fputc(c, stdout) == EOF)
|
if(fputc(c, stdout) == EOF) { return ioerr(program_name); }
|
||||||
return ioerr(argv[0]);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
restore_echo(0);
|
restore_echo(0);
|
||||||
|
Loading…
Reference in New Issue
Block a user