From c53ab2a8541df51305ea10c00478daa77e01c16f Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Wed, 4 Aug 2004 18:24:53 +0000 Subject: [PATCH] tweak parse_num() to return a bool, and fix backwards test of its value so that numeric values will be preserved when switching to the "Go To Line" prompt again git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1881 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 4 +++- src/nano.c | 4 ++-- src/proto.h | 4 ++-- src/rcfile.c | 4 ++-- src/search.c | 6 +++--- src/utils.c | 9 +++++---- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 82534de6..e9d92f47 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,7 +32,9 @@ CVS code - - Convert nano to use the new parse_num() function to read in numeric values at the command line and in the rcfile, and duplicate the messages used in the rcfile in the command line - for consistency. (David Benbennick) DLR: Convert tabsize, + for consistency. (David Benbennick) DLR: Tweak parse_num() to + parse ssize_t values instead of ints and to return a bool + indicating whether parsing succeeded. Convert tabsize, wrap_at, and fill to ssize_t in order to work with parse_num() properly and also to increase their capacity while keeping the ability to hold negative numbers in case of diff --git a/src/nano.c b/src/nano.c index e7ae45b9..3bdf2d29 100644 --- a/src/nano.c +++ b/src/nano.c @@ -3154,7 +3154,7 @@ int main(int argc, char *argv[]) break; #endif case 'T': - if (parse_num(optarg, &tabsize) == -1 || tabsize <= 0) { + if (!parse_num(optarg, &tabsize) || tabsize <= 0) { fprintf(stderr, _("Requested tab size %s invalid\n"), optarg); exit(1); } @@ -3202,7 +3202,7 @@ int main(int argc, char *argv[]) break; #ifndef DISABLE_WRAPJUSTIFY case 'r': - if (parse_num(optarg, &wrap_at) == -1) { + if (!parse_num(optarg, &wrap_at)) { fprintf(stderr, _("Requested fill size %s invalid\n"), optarg); exit(1); } diff --git a/src/proto.h b/src/proto.h index 9fa4d361..7af1c071 100644 --- a/src/proto.h +++ b/src/proto.h @@ -402,7 +402,7 @@ char *replace_line(const char *needle); int do_replace_loop(const char *needle, const filestruct *real_current, size_t *real_current_x, int wholewords); void do_replace(void); -void do_gotoline(int line, int save_pos); +void do_gotoline(int line, bool save_pos); void do_gotoline_void(void); #if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER) void do_gotopos(int line, int pos_x, int pos_y, size_t pos_pww); @@ -437,7 +437,7 @@ int is_blank_char(int c); #endif int is_cntrl_char(int c); int num_of_digits(int n); -int parse_num(const char *str, ssize_t *val); +bool parse_num(const char *str, ssize_t *val); void align(char **strp); void null_at(char **data, size_t index); void unsunder(char *str, size_t true_len); diff --git a/src/rcfile.c b/src/rcfile.c index 728a9a5a..69e5f8bc 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -561,7 +561,7 @@ void parse_rcfile(FILE *rcstream) #endif #ifndef DISABLE_WRAPJUSTIFY if (strcasecmp(rcopts[i].name, "fill") == 0) { - if (parse_num(option, &wrap_at) == -1) { + if (!parse_num(option, &wrap_at)) { rcfile_error(N_("Requested fill size %s invalid\n"), option); wrap_at = -CHARS_FROM_EOL; } @@ -609,7 +609,7 @@ void parse_rcfile(FILE *rcstream) else #endif if (strcasecmp(rcopts[i].name, "tabsize") == 0) { - if (parse_num(option, &tabsize) == -1 || tabsize <= 0) + if (!parse_num(option, &tabsize) || tabsize <= 0) rcfile_error(N_("Requested tab size %s invalid\n"), option); tabsize = -1; } diff --git a/src/search.c b/src/search.c index fc4a7820..d69d747a 100644 --- a/src/search.c +++ b/src/search.c @@ -240,7 +240,7 @@ int search_init(int replacing) #endif /* If answer parses as an integer, put it up on the * statusbar. */ - do_gotoline(parse_num(answer, NULL), FALSE); + do_gotoline(parse_num(answer, NULL) ? -1 : 0, FALSE); /* Fall through. */ default: return -1; @@ -835,7 +835,7 @@ void do_replace(void) replace_abort(); } -void do_gotoline(int line, int save_pos) +void do_gotoline(int line, bool save_pos) { if (line <= 0) { /* Ask for it. */ char *ans = mallocstrcpy(NULL, answer); @@ -856,7 +856,7 @@ void do_gotoline(int line, int save_pos) } /* Bounds check. */ - if (parse_num(answer, &line) == -1 || line < 0) { + if (!parse_num(answer, &line) || line < 0) { statusbar(_("Come on, be reasonable")); display_main_list(); return; diff --git a/src/utils.c b/src/utils.c index 39b08b90..92e70b9c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -85,8 +85,9 @@ int num_of_digits(int n) } /* Read an int from str, and store it in *val (if val is not NULL). On - * error, we return -1 and don't change *val. */ -int parse_num(const char *str, ssize_t *val) + * error, we return FALSE and don't change *val. Otherwise, we return + * TRUE. */ +bool parse_num(const char *str, ssize_t *val) { char *first_error; ssize_t j; @@ -94,10 +95,10 @@ int parse_num(const char *str, ssize_t *val) assert(str != NULL); j = (ssize_t)strtol(str, &first_error, 10); if (errno == ERANGE || *str == '\0' || *first_error != '\0') - return -1; + return FALSE; if (val != NULL) *val = j; - return 0; + return TRUE; } /* Fix the memory allocation for a string. */