From: Eric Gillespie Date: Sun, 3 Apr 2016 08:23:11 +0000 (+0000) Subject: Rename and document functions that read from stdin. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/840b3f0e45a43b8378e88c66f004b2f186662e35?hp=961d5bf9387fcaa225caa2bc72c71d8bc8d64849 Rename and document functions that read from stdin. - gans becomes read_switch - getans becomes read_switch_multiword - getans_via_readline becomes read_switch_multiword_via_readline - getanswer becomes read_yes_or_no_if_tty - install-mh.c geta becomes read_line --- diff --git a/Makefile.am b/Makefile.am index ec523ec4..413a6e29 100644 --- a/Makefile.am +++ b/Makefile.am @@ -582,7 +582,7 @@ sbr_libmh_a_SOURCES = sbr/addrsbr.c sbr/ambigsw.c sbr/atooi.c sbr/arglist.c \ sbr/folder_read.c sbr/folder_realloc.c sbr/gans.c \ sbr/getans.c sbr/getanswer.c sbr/getarguments.c \ sbr/getcpy.c sbr/geteditor.c sbr/getfolder.c \ - sbr/getpass.c \ + sbr/getpass.c sbr/read_line.c \ sbr/fmt_addr.c sbr/fmt_compile.c sbr/fmt_new.c \ sbr/fmt_rfc2047.c sbr/fmt_scan.c \ sbr/icalparse.y sbr/icalendar.l sbr/datetime.c \ diff --git a/h/prototypes.h b/h/prototypes.h index 3399384e..441863ac 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -20,13 +20,13 @@ struct msgs_array; void add_profile_entry (const char *, const char *); void adios (char *, char *, ...) NORETURN; void admonish (char *, char *, ...); -void advertise (char *, char *, char *, va_list); -void advise (char *, char *, ...); +void advertise (const char *, char *, char *, va_list); +void advise (const char *, char *, ...); char **argsplit (char *, char **, int *); void argsplit_msgarg (struct msgs_array *, char *, char **); void argsplit_insert (struct msgs_array *, char *, char **); void arglist_free (char *, char **); -void ambigsw (char *, struct swit *); +void ambigsw (const char *, const struct swit *); int atooi(char *); char **brkstring (char *, char *, char *); @@ -118,12 +118,44 @@ int folder_pack (struct msgs **, int); struct msgs *folder_read (char *name, int lockflag); struct msgs *folder_realloc (struct msgs *, int, int); -int gans (char *, struct swit *); -char **getans (char *, struct swit *); + +/* + * Flush standard output, read a line from standard input into a static buffer, + * zero out the newline, and return a pointer to the buffer. + * On error, return NULL. + */ +const char *read_line(void); + +/* + * Print null-terminated PROMPT to and flush standard output. Read answers from + * standard input until one matches an entry in SWITCHES. When one matches, + * return its swret field. Return 0 on EOF. + */ +int read_switch(const char *PROMPT, const struct swit *SWITCHES); + +/* + * If standard input is not a tty, return 1 without printing anything. Else, + * print null-terminated PROMPT to and flush standard output. Read answers from + * standard input until one is "yes" or "no", returning 1 for "yes" and 0 for + * "no". Also return 0 on EOF. + */ +int read_yes_or_no_if_tty(const char *PROMPT); + +/* + * Print null-terminated PROMPT to and flush standard output. Read multi-word + * answers from standard input until a first word matches an entry in SWITCHES. + * When one matches, return a pointer to an array of pointers to the words. + * Return NULL on EOF, interrupt, or other error. + */ +char **read_switch_multiword(const char *PROMPT, const struct swit *SWITCHES); + +/* + * Same as read_switch_multiword but using readline(3) for input. + */ #ifdef READLINE_SUPPORT -char **getans_via_readline (char *, struct swit *); +char **read_switch_multiword_via_readline (char *, struct swit *); #endif /* READLINE_SUPPORT */ -int getanswer (char *); + char **getarguments (char *, int, char **, int); /* @@ -247,7 +279,7 @@ void m_popen(char *name, int savestdout); void m_pclose(void); void m_unknown(m_getfld_state_t *, FILE *); -int makedir (char *); +int makedir (const char *); char *message_id (time_t, int); /* @@ -282,7 +314,7 @@ int pidwait (pid_t, int); int pidstatus (int, FILE *, char *); char *pluspath(char *); void print_help (char *, struct swit *, int); -void print_sw (char *, struct swit *, char *, FILE *); +void print_sw (const char *, const struct swit *, char *, FILE *); void print_version (char *); void push (void); char *pwd (void); @@ -328,7 +360,7 @@ void seq_setcur (struct msgs *, int); void seq_setprev (struct msgs *); void seq_setunseen (struct msgs *, int); int showfile (char **, char *); -int smatch(char *, struct swit *); +int smatch(const char *, const struct swit *); /* * Convert a set of bit flags to printable format. @@ -363,7 +395,7 @@ int smatch(char *, struct swit *); * from least significant bit to most significant. */ char *snprintb (char *buffer, size_t size, unsigned flags, char *bitfield); -int ssequal (char *, char *); +int ssequal (const char *, const char *); int stringdex (char *, char *); char *trimcpy (char *); int unputenv (char *); diff --git a/sbr/ambigsw.c b/sbr/ambigsw.c index c11b9e75..15ff34cb 100644 --- a/sbr/ambigsw.c +++ b/sbr/ambigsw.c @@ -11,7 +11,7 @@ void -ambigsw (char *arg, struct swit *swp) +ambigsw (const char *arg, const struct swit *swp) { advise (NULL, "-%s ambiguous. It matches", arg); print_sw (arg, swp, "-", stderr); diff --git a/sbr/context_read.c b/sbr/context_read.c index 80340688..de14b0d5 100644 --- a/sbr/context_read.c +++ b/sbr/context_read.c @@ -107,7 +107,7 @@ context_read (void) cp = concat ("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL); - if (!getanswer(cp)) + if (!read_yes_or_no_if_tty(cp)) adios (NULL, "unable to access MH-directory \"%s\"", nd); free (cp); diff --git a/sbr/error.c b/sbr/error.c index 0b6d7777..6fb0d5cc 100644 --- a/sbr/error.c +++ b/sbr/error.c @@ -17,7 +17,7 @@ * print out error message */ void -advise (char *what, char *fmt, ...) +advise (const char *what, char *fmt, ...) { va_list ap; @@ -60,7 +60,7 @@ admonish (char *what, char *fmt, ...) * main routine for printing error messages. */ void -advertise (char *what, char *tail, char *fmt, va_list ap) +advertise (const char *what, char *tail, char *fmt, va_list ap) { int eindex = errno; char buffer[BUFSIZ], err[BUFSIZ]; @@ -85,7 +85,7 @@ advertise (char *what, char *tail, char *fmt, va_list ap) if (*what) { iov->iov_len = strlen (iov->iov_base = " "); iov++; - iov->iov_len = strlen (iov->iov_base = what); + iov->iov_len = strlen (iov->iov_base = (void*)what); iov++; iov->iov_len = strlen (iov->iov_base = ": "); iov++; diff --git a/sbr/gans.c b/sbr/gans.c index 70234029..f5ad194a 100644 --- a/sbr/gans.c +++ b/sbr/gans.c @@ -11,11 +11,11 @@ int -gans (char *prompt, struct swit *ansp) +read_switch (const char *prompt, const struct swit *ansp) { register int i; register char *cp; - register struct swit *ap; + const register struct swit *ap; char ansbuf[BUFSIZ]; for (;;) { diff --git a/sbr/getans.c b/sbr/getans.c index 0800a3c0..d388e2b9 100644 --- a/sbr/getans.c +++ b/sbr/getans.c @@ -21,7 +21,7 @@ static void intrser (int); char ** -getans (char *prompt, struct swit *ansp) +read_switch_multiword (const char *prompt, const struct swit *ansp) { int i; SIGNAL_HANDLER istat = NULL; diff --git a/sbr/getansreadline.c b/sbr/getansreadline.c index 5d80dd97..bc2b5636 100644 --- a/sbr/getansreadline.c +++ b/sbr/getansreadline.c @@ -26,7 +26,7 @@ static char ansbuf[BUFSIZ]; */ char ** -getans_via_readline(char *prompt, struct swit *ansp) +read_switch_multiword_via_readline(char *prompt, struct swit *ansp) { char *ans, **cpp; diff --git a/sbr/getanswer.c b/sbr/getanswer.c index 73922158..490d833e 100644 --- a/sbr/getanswer.c +++ b/sbr/getanswer.c @@ -11,12 +11,12 @@ int -getanswer (char *prompt) +read_yes_or_no_if_tty (const char *prompt) { static int interactive = -1; if (interactive < 0) interactive = isatty (fileno (stdin)) ? 1 : 0; - return (interactive ? gans (prompt, anoyes) : 1); + return (interactive ? read_switch (prompt, anoyes) : 1); } diff --git a/sbr/makedir.c b/sbr/makedir.c index 562949be..6d3f0dc3 100644 --- a/sbr/makedir.c +++ b/sbr/makedir.c @@ -15,7 +15,7 @@ #include int -makedir (char *dir) +makedir (const char *dir) { char path[PATH_MAX]; char* folder_perms_ASCII; diff --git a/sbr/print_sw.c b/sbr/print_sw.c index 6d475aba..d790db69 100644 --- a/sbr/print_sw.c +++ b/sbr/print_sw.c @@ -11,7 +11,7 @@ void -print_sw (char *substr, struct swit *swp, char *prefix, FILE *fp) +print_sw (const char *substr, const struct swit *swp, char *prefix, FILE *fp) { int len, optno; register int i; diff --git a/sbr/read_line.c b/sbr/read_line.c new file mode 100644 index 00000000..35d71a7a --- /dev/null +++ b/sbr/read_line.c @@ -0,0 +1,16 @@ +#include + +const char * +read_line(void) +{ + char *cp; + static char line[BUFSIZ]; + + fflush(stdout); + if (fgets(line, sizeof(line), stdin) == NULL) + return NULL; + if ((cp = strchr(line, '\n'))) + *cp = 0; + return line; +} + diff --git a/sbr/smatch.c b/sbr/smatch.c index 4d1d3d68..8ec6e3f2 100644 --- a/sbr/smatch.c +++ b/sbr/smatch.c @@ -11,11 +11,11 @@ int -smatch(char *string, struct swit *swp) +smatch(const char *string, const struct swit *swp) { - char *sp, *tcp; + const char *sp, *tcp; int firstone, len; - struct swit *tp; + const struct swit *tp; firstone = UNKWNSW; diff --git a/sbr/ssequal.c b/sbr/ssequal.c index 5ccf28fd..cb765203 100644 --- a/sbr/ssequal.c +++ b/sbr/ssequal.c @@ -23,7 +23,7 @@ */ int -ssequal (char *s1, char *s2) +ssequal (const char *s1, const char *s2) { if (!s1) s1 = ""; diff --git a/sbr/utils.c b/sbr/utils.c index c6f75e07..6ab5f534 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -200,7 +200,7 @@ void create_folder(char *folder, int autocreate, void (*done_callback)(int)) if (autocreate == 0) { /* ask before creating folder */ cp = concat ("Create folder \"", folder, "\"? ", NULL); - if (!getanswer (cp)) + if (!read_yes_or_no_if_tty (cp)) done_callback (1); free (cp); } else if (autocreate == -1) { diff --git a/uip/comp.c b/uip/comp.c index 13d73c1b..a11f1b7d 100644 --- a/uip/comp.c +++ b/uip/comp.c @@ -337,7 +337,8 @@ try_it_again: adios (drft, "unable to stat"); printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); for (i = LISTDSW; i != YESW;) { - if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul))) + if (!(argp = read_switch_multiword ("\nDisposition? ", + isdf ? aqrunl : aqrul))) done (1); switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) { case NOSW: diff --git a/uip/dist.c b/uip/dist.c index 5a35e59a..09c501a8 100644 --- a/uip/dist.c +++ b/uip/dist.c @@ -232,7 +232,8 @@ try_it_again: if (stat (drft, &st) != NOTOK) { printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); for (i = LISTDSW; i != YESW;) { - if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl))) + if (!(argp = read_switch_multiword ("\nDisposition? ", + isdf ? aqrnl : aqrl))) done (1); switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) { case NOSW: diff --git a/uip/forw.c b/uip/forw.c index 12a323bb..c4e1dde5 100644 --- a/uip/forw.c +++ b/uip/forw.c @@ -313,7 +313,8 @@ try_it_again: if (!buildsw && stat (drft, &st) != NOTOK) { printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); for (i = LISTDSW; i != YESW;) { - if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl))) + if (!(argp = read_switch_multiword ("\nDisposition? ", + isdf ? aqrnl : aqrl))) done (1); switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) { case NOSW: diff --git a/uip/inc.c b/uip/inc.c index 7d128420..41e785ff 100644 --- a/uip/inc.c +++ b/uip/inc.c @@ -584,7 +584,7 @@ go_to_it: if (errno != ENOENT) adios (packfile, "error on file"); cp = concat ("Create file \"", packfile, "\"? ", NULL); - if (noisy && !getanswer (cp)) + if (noisy && !read_yes_or_no_if_tty (cp)) done (1); free (cp); } diff --git a/uip/install-mh.c b/uip/install-mh.c index ce8e2940..627c48e2 100644 --- a/uip/install-mh.c +++ b/uip/install-mh.c @@ -24,17 +24,13 @@ DEFINE_SWITCH_ENUM(INSTALLMH); DEFINE_SWITCH_ARRAY(INSTALLMH, switches); #undef X -/* - * static prototypes - */ -static char *geta(void); - int main (int argc, char **argv) { int autof = 0; - char *cp, *pathname, buf[BUFSIZ]; + char *cp, buf[BUFSIZ]; + const char *pathname; char *dp, **arguments, **argp; struct node *np; struct passwd *pw; @@ -122,7 +118,7 @@ main (int argc, char **argv) done(1); } - if (!autof && gans ("Do you want help? ", anoyes)) { + if (!autof && read_switch ("Do you want help? ", anoyes)) { (void)printf( "\n" "Prior to using nmh, it is necessary to have a file in your login\n" @@ -138,7 +134,7 @@ main (int argc, char **argv) if (S_ISDIR(st.st_mode)) { cp = concat ("You already have the standard nmh directory \"", cp, "\".\nDo you want to use it for nmh? ", NULL); - if (gans (cp, anoyes)) + if (read_switch (cp, anoyes)) pathname = "Mail"; else goto query; @@ -151,17 +147,20 @@ main (int argc, char **argv) else cp = concat ("Do you want the standard nmh path \"", mypath, "/", "Mail\"? ", NULL); - if (autof || gans (cp, anoyes)) + if (autof || read_switch (cp, anoyes)) pathname = "Mail"; else { query: - if (gans ("Do you want a path below your login directory? ", + if (read_switch ("Do you want a path below your login directory? ", anoyes)) { printf ("What is the path? %s/", mypath); - pathname = geta (); + pathname = read_line (); + if (pathname == NULL) done (1); } else { printf ("What is the whole path? /"); - pathname = concat ("/", geta (), NULL); + pathname = read_line (); + if (pathname == NULL) done (1); + pathname = concat ("/", pathname, NULL); } } } @@ -171,7 +170,7 @@ query: } if (chdir (pathname) == NOTOK) { cp = concat ("\"", pathname, "\" doesn't exist; Create it? ", NULL); - if (autof || gans (cp, anoyes)) + if (autof || read_switch (cp, anoyes)) if (makedir (pathname) == 0) adios (NULL, "unable to create %s", pathname); } else { @@ -223,18 +222,3 @@ query: done (0); return 1; } - - -static char * -geta (void) -{ - char *cp; - static char line[BUFSIZ]; - - fflush(stdout); - if (fgets(line, sizeof(line), stdin) == NULL) - done (1); - if ((cp = strchr(line, '\n'))) - *cp = 0; - return line; -} diff --git a/uip/mhcachesbr.c b/uip/mhcachesbr.c index ae563c2f..7afbadd5 100644 --- a/uip/mhcachesbr.c +++ b/uip/mhcachesbr.c @@ -258,7 +258,7 @@ got_it: snprintf (bp, buflen, "\n in file %s? ", buffer); /* Now, check answer */ - if (!getanswer (query)) + if (!read_yes_or_no_if_tty (query)) status = NOTOK; } diff --git a/uip/mhmisc.c b/uip/mhmisc.c index 8b1f0d00..2d6ec569 100644 --- a/uip/mhmisc.c +++ b/uip/mhmisc.c @@ -146,7 +146,7 @@ losing_directory: } ep = concat ("Create directory \"", file, "\"? ", NULL); - answer = getanswer (ep); + answer = read_yes_or_no_if_tty (ep); free (ep); if (!answer) diff --git a/uip/mhparse.c b/uip/mhparse.c index 1193d564..ca5558ae 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -2583,7 +2583,7 @@ openFTP (CT ct, char **file) /* * Now, check the answer */ - if (!getanswer (buffer)) + if (!read_yes_or_no_if_tty (buffer)) return NOTOK; if (e->eb_flags) { @@ -2770,7 +2770,7 @@ openMail (CT ct, char **file) e->eb_subject ? e->eb_subject : e->eb_body); /* Now, check answer */ - if (!getanswer (buffer)) + if (!read_yes_or_no_if_tty (buffer)) return NOTOK; vecp = 0; diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index 62b2f4af..62a5ba43 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -1285,7 +1285,7 @@ clobber_check (char *original_file, mhstoreinfo_t info) { if (isatty (fileno (stdin))) { char *prompt = concat ("Overwrite \"", file, "\" [y/n/rename]? ", NULL); - ans = getans (prompt, answer); + ans = read_switch_multiword (prompt, answer); free (prompt); } else { /* Overwrite, that's what nmh used to do. And warn. */ diff --git a/uip/packf.c b/uip/packf.c index 30f0f484..d8e037db 100644 --- a/uip/packf.c +++ b/uip/packf.c @@ -111,7 +111,7 @@ main (int argc, char **argv) if (errno != ENOENT) adios (file, "error on file"); cp = concat ("Create file \"", file, "\"? ", NULL); - if (!getanswer (cp)) + if (!read_yes_or_no_if_tty (cp)) done (1); free (cp); } diff --git a/uip/repl.c b/uip/repl.c index ebe72ff2..5686b5bf 100644 --- a/uip/repl.c +++ b/uip/repl.c @@ -370,7 +370,8 @@ try_it_again: if (!buildsw && stat (drft, &st) != NOTOK) { printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); for (i = LISTDSW; i != YESW;) { - if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl))) + if (!(argp = read_switch_multiword ("\nDisposition? ", + isdf ? aqrnl : aqrl))) done (1); switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) { case NOSW: diff --git a/uip/replsbr.c b/uip/replsbr.c index c4ca0c4b..6380884c 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -402,7 +402,7 @@ insert (struct mailname *np) if (querysw) { snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np)); - if (!gans (buffer, anoyes)) + if (!read_switch (buffer, anoyes)) return 0; } mp->m_next = np; diff --git a/uip/rmf.c b/uip/rmf.c index c7e6426c..f714c472 100644 --- a/uip/rmf.c +++ b/uip/rmf.c @@ -103,7 +103,7 @@ main (int argc, char **argv) if (interactive) { cp = concat ("Remove folder \"", folder, "\"? ", NULL); - if (!getanswer (cp)) + if (!read_yes_or_no_if_tty (cp)) done (0); free (cp); } diff --git a/uip/send.c b/uip/send.c index 26ef89a8..f4c49106 100644 --- a/uip/send.c +++ b/uip/send.c @@ -301,7 +301,7 @@ main (int argc, char **argv) adios (msgs[0], "unable to stat draft file"); cp = concat ("Use \"", msgs[0], "\"? ", NULL); for (status = LISTDSW; status != YESW;) { - if (!(argp = getans (cp, anyl))) + if (!(argp = read_switch_multiword (cp, anyl))) done (1); switch (status = smatch (*argp, anyl)) { case NOSW: diff --git a/uip/whatnowsbr.c b/uip/whatnowsbr.c index d8862dc9..62320f94 100644 --- a/uip/whatnowsbr.c +++ b/uip/whatnowsbr.c @@ -233,9 +233,9 @@ WhatNow (int argc, char **argv) snprintf (prompt, sizeof(prompt), myprompt, invo_name); for (;;) { #ifdef READLINE_SUPPORT - if (!(argp = getans_via_readline (prompt, aleqs))) { + if (!(argp = read_switch_multiword_via_readline (prompt, aleqs))) { #else /* ! READLINE_SUPPORT */ - if (!(argp = getans (prompt, aleqs))) { + if (!(argp = read_switch_multiword (prompt, aleqs))) { #endif /* READLINE_SUPPORT */ (void) m_unlink (LINK); done (1);