X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Fprompter.c;h=ae0c0c63cd6fd2efd00eecc1de74013b65c37e2a;hp=823760ef49b91e48b3be607a1541281e8ecdf178;hb=8268f67015f9659a2f63dc26702a5a49d3fa6c2a;hpb=ccf4f175ef4c4e7522f9510a4a1149c15d810dd9 diff --git a/uip/prompter.c b/uip/prompter.c index 823760e..ae0c0c6 100644 --- a/uip/prompter.c +++ b/uip/prompter.c @@ -12,6 +12,11 @@ #include #include #include +#include +#include +#include +#include +#include static struct swit switches[] = { #define PREPSW 0 @@ -34,8 +39,8 @@ static struct swit switches[] = { }; -static int wtuser = 0; -static int sigint = 0; +volatile sig_atomic_t wtuser = 0; +volatile sig_atomic_t sigint = 0; static jmp_buf sigenv; /* @@ -71,19 +76,20 @@ main(int argc, char **argv) switch (smatch(++cp, switches)) { case AMBIGSW: ambigsw(cp, switches); - done(1); + exit(EX_USAGE); case UNKWNSW: - adios(NULL, "-%s unknown", cp); + adios(EX_USAGE, NULL, "-%s unknown", cp); case HELPSW: snprintf(buffer, sizeof(buffer), "%s [switches] file", invo_name); print_help(buffer, switches, 1); - done(1); + exit(argc == 2 ? EX_OK : EX_USAGE); + case VERSIONSW: print_version(invo_name); - done(1); + exit(argc == 2 ? EX_OK : EX_USAGE); case PREPSW: prepend++; @@ -111,14 +117,17 @@ main(int argc, char **argv) } } - if (!drft) - adios(NULL, "usage: %s [switches] file", invo_name); - if ((in = fopen(drft, "r")) == NULL) - adios(drft, "unable to open"); + if (!drft) { + adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name); + } + if ((in = fopen(drft, "r")) == NULL) { + adios(EX_IOERR, drft, "unable to open"); + } tfile = m_mktemp2(NULL, invo_name, NULL, &out); - if (tfile == NULL) - adios("prompter", "unable to create temporary file"); + if (tfile == NULL) { + adios(EX_CANTCREAT, "prompter", "unable to create temporary file"); + } chmod(tmpfil, 0600); strncpy(tmpfil, tfile, sizeof(tmpfil)); @@ -160,7 +169,7 @@ main(int argc, char **argv) if (i == -1) { abort: unlink(tmpfil); - done(1); + exit(EX_DATAERR); } if (i || (field[0]!='\n' && field[0]!='\0')) { fprintf(out, "%s:", name); @@ -231,7 +240,7 @@ has_no_body: break; default: - adios(NULL, "skeleton is poorly formatted"); + adios(EX_DATAERR, NULL, "skeleton is poorly formatted"); } break; } @@ -244,18 +253,19 @@ has_no_body: fclose(out); SIGNAL(SIGINT, SIG_IGN); - if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) - adios(tmpfil, "unable to re-open"); - if ((fdo = creat(drft, m_gmprot())) == NOTOK) - adios(drft, "unable to write"); + if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) { + adios(EX_IOERR, tmpfil, "unable to re-open"); + } + if ((fdo = creat(drft, m_gmprot())) == NOTOK) { + adios(EX_IOERR, drft, "unable to write"); + } cpydata(fdi, fdo, tmpfil, drft); close(fdi); close(fdo); unlink(tmpfil); context_save(); /* save the context file */ - done(0); - return 1; + return EX_OK; } @@ -263,23 +273,25 @@ int getln(char *buffer, int n) { int c; + sig_atomic_t psigint = sigint; char *cp; cp = buffer; *cp = '\0'; switch (setjmp(sigenv)) { - case OK: + case 0: wtuser = 1; break; - case DONE: - wtuser = 0; - return 0; - default: wtuser = 0; - return NOTOK; + if (sigint == psigint) { + return 0; + } else { + sigint = psigint; + return NOTOK; + } } for (;;) { @@ -311,7 +323,8 @@ getln(char *buffer, int n) static void intrser(int i) { - if (wtuser) - longjmp(sigenv, NOTOK); + if (wtuser) { + close(STDIN_FILENO); + } sigint++; }