diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2006-04-18 03:35:58 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2006-04-18 03:35:58 +0000 |
commit | 68c7e7f117826f0b39de48ccc0bb99a5fd91f9fa (patch) | |
tree | 2e89ac8b2281964e971a0f264eae922e2d8822a0 /usr.bin | |
parent | 3bc6824010a033693ea165e10bf97012892f6995 (diff) |
Combine code to prompt user for input into a shared function,
reducing some complex fats from ci.c.
OK joris@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/rcs/ci.c | 53 | ||||
-rw-r--r-- | usr.bin/rcs/rcsprog.c | 73 | ||||
-rw-r--r-- | usr.bin/rcs/rcsprog.h | 3 |
3 files changed, 52 insertions, 77 deletions
diff --git a/usr.bin/rcs/ci.c b/usr.bin/rcs/ci.c index 010d733eab3..b47abd81a88 100644 --- a/usr.bin/rcs/ci.c +++ b/usr.bin/rcs/ci.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ci.c,v 1.152 2006/04/18 02:52:18 ray Exp $ */ +/* $OpenBSD: ci.c,v 1.153 2006/04/18 03:35:57 ray Exp $ */ /* * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org> * All rights reserved. @@ -69,7 +69,6 @@ static int checkin_attach_symbol(struct checkin_params *); static int checkin_checklock(struct checkin_params *); static char *checkin_diff_file(struct checkin_params *); static char *checkin_getdesc(void); -static char *checkin_getinput(const char *); static char *checkin_getlogmsg(RCSNUM *, RCSNUM *); static int checkin_init(struct checkin_params *); static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **, @@ -381,6 +380,8 @@ static char * checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2) { char *rcs_msg, nrev[16], prev[16]; + const char *prompt = + "enter log message, terminated with a single '.' or end of file:\n"; RCSNUM *tmprev; rcs_msg = NULL; @@ -397,8 +398,8 @@ checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2) printf("new revision: %s; previous revision: %s\n", nrev, prev); - rcs_msg = checkin_getinput("enter log message, terminated with a " - "single '.' or end of file:\n>> "); + rcs_msg = rcs_prompt(prompt); + return (rcs_msg); } @@ -413,47 +414,13 @@ static char * checkin_getdesc() { char *description; + const char *prompt = + "enter description, terminated with single '.' or end of file:\n" + "NOTE: This is NOT the log message!\n"; - description = checkin_getinput("enter description, terminated with " - "single '.' or end of file:\n" - "NOTE: This is NOT the log message!\n>> "); - return (description); -} - -/* - * checkin_getinput() - * - * Get some input from the user, in RCS style, prompting with message <prompt>. - * Returns pointer to a char array on success, NULL on failure. - */ -static char * -checkin_getinput(const char *prompt) -{ - BUF *inputbuf; - char *input, buf[128]; + description = rcs_prompt(prompt); - if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) { - cvs_log(LP_ERR, "failed to allocate input buffer"); - return (NULL); - } - - if (isatty(STDIN_FILENO)) - fprintf(stderr, "%s", prompt); - - for (;;) { - fgets(buf, (int)sizeof(buf), stdin); - if (feof(stdin) || ferror(stdin) || buf[0] == '.') - break; - cvs_buf_append(inputbuf, buf, strlen(buf)); - - if (isatty(STDIN_FILENO)) - fprintf(stderr, ">> "); - } - - cvs_buf_putc(inputbuf, '\0'); - input = (char *)cvs_buf_release(inputbuf); - - return (input); + return (description); } /* diff --git a/usr.bin/rcs/rcsprog.c b/usr.bin/rcs/rcsprog.c index 2e51e82d38d..c7c9ebda9dd 100644 --- a/usr.bin/rcs/rcsprog.c +++ b/usr.bin/rcs/rcsprog.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsprog.c,v 1.106 2006/04/18 02:46:21 ray Exp $ */ +/* $OpenBSD: rcsprog.c,v 1.107 2006/04/18 03:35:57 ray Exp $ */ /* * Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org> * All rights reserved. @@ -32,8 +32,7 @@ #define RCSPROG_OPTSTRING "A:a:b::c:e::ik:Ll::m:Mn:N:o:qt::TUu::Vx::z::" #define DESC_PROMPT "enter description, terminated with single '.' " \ - "or end of file:\nNOTE: This is NOT the log message!" \ - "\n>> " + "or end of file:\nNOTE: This is NOT the log message!\n" const char rcs_version[] = "OpenCVS RCS version 3.6"; int verbose = 1; @@ -860,47 +859,55 @@ static void rcs_set_description(RCSFILE *file, const char *in) { BUF *bp; - char *content, buf[128]; + char *content; - content = NULL; /* Description is in file <in>. */ - if (in != NULL && *in != '-') + if (in != NULL && *in != '-') { bp = cvs_buf_load(in, BUF_AUTOEXT); + cvs_buf_putc(bp, '\0'); + content = cvs_buf_release(bp); /* Description is in <in>. */ - else if (in != NULL) { - size_t len; - const char *desc; - + } else if (in != NULL) /* Skip leading `-'. */ - desc = in + 1; - len = strlen(desc); - - bp = cvs_buf_alloc(len + 1, BUF_AUTOEXT); - cvs_buf_append(bp, desc, len); + content = xstrdup(in + 1); /* Get description from stdin. */ - } else { - bp = cvs_buf_alloc(64, BUF_AUTOEXT); + else + content = rcs_prompt(DESC_PROMPT); + + rcs_desc_set(file, content); + xfree(content); +} + +/* + * Prompt for and store user's input in an allocated string. + * + * Returns the string's pointer. + */ +char * +rcs_prompt(const char *prompt) +{ + BUF *bp; + size_t len; + char *buf; + + bp = cvs_buf_alloc(0, BUF_AUTOEXT); + if (isatty(STDIN_FILENO)) + (void)fprintf(stderr, "%s", prompt); + if (isatty(STDIN_FILENO)) + (void)fprintf(stderr, ">> "); + while ((buf = fgetln(stdin, &len)) != NULL) { + /* The last line may not be EOL terminated. */ + if (buf[0] == '.' && (len == 1 || buf[1] == '\n')) + break; + else + cvs_buf_append(bp, buf, len); if (isatty(STDIN_FILENO)) - (void)fprintf(stderr, "%s", DESC_PROMPT); - for (;;) { - /* XXX - fgetln() may be more elegant. */ - fgets(buf, sizeof(buf), stdin); - if (feof(stdin) || ferror(stdin) || - strcmp(buf, ".\n") == 0 || - strcmp(buf, ".") == 0) - break; - cvs_buf_append(bp, buf, strlen(buf)); - if (isatty(STDIN_FILENO)) - (void)fprintf(stderr, ">> "); - } + (void)fprintf(stderr, ">> "); } - cvs_buf_putc(bp, '\0'); - content = cvs_buf_release(bp); - rcs_desc_set(file, content); - xfree(content); + return (cvs_buf_release(bp)); } u_int diff --git a/usr.bin/rcs/rcsprog.h b/usr.bin/rcs/rcsprog.h index 5e0cb328fa7..1faa42f41e1 100644 --- a/usr.bin/rcs/rcsprog.h +++ b/usr.bin/rcs/rcsprog.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsprog.h,v 1.48 2006/04/16 12:30:00 niallo Exp $ */ +/* $OpenBSD: rcsprog.h,v 1.49 2006/04/18 03:35:57 ray Exp $ */ /* * Copyright (c) 2005 Joris Vink <joris@openbsd.org> * All rights reserved. @@ -114,6 +114,7 @@ char *rcs_choosefile(const char *); int rcs_statfile(char *, char *, size_t); time_t rcs_get_mtime(const char *); RCSNUM *rcs_getrevnum(const char *, RCSFILE *); +char *rcs_prompt(const char *); u_int rcs_rev_select(RCSFILE *, char *); void rcs_set_rev(const char *, RCSNUM **); void rcs_setrevstr(char **, char *); |