diff --git a/ChangeLog b/ChangeLog index 82534de641cf36e29fde0e297463f0f45d5e1719..e9d92f476cc85d322b6b430edb8e5171d40cd969 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 e7ae45b99b0f7ac6780ff2a45d28674c98c7c022..3bdf2d290c05fd1f6ef940a351786d1e9ea159e2 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 9fa4d361f28decd7cbd0d1d5e87b1592277ee9a2..7af1c071138afb1267ed0730446f884a81933049 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 728a9a5a2b57c397b6828938a04776a157c8c056..69e5f8bc2cc3759c8fbb1e41bf261856dcf8b829 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 fc4a78209e0cf7a0e381e529f03184d84cd0b1e7..d69d747a05584d49407cfbeb5c53a1680c1bc710 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 39b08b903bf35ee612b516a799eb9580f1b39ff4..92e70b9cfd8c48483cc3f799d97893fdfd81e980 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. */