port over DB's number parsing code and related bits, with a few minor

changes of mine, and add a few related variable type changes


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1853 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2004-07-12 16:07:14 +00:00
parent 576bf331ef
commit 49c3f2406d
7 changed files with 69 additions and 70 deletions

View File

@ -28,6 +28,16 @@ CVS code -
with all the other lists, have the replace list accept all the with all the other lists, have the replace list accept all the
same function keys as the search list, and clarify a few same function keys as the search list, and clarify a few
shortcut descriptions. shortcut descriptions.
- 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,
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
errors. Also exit instead of calling usage() in the event of
an invalid fill value, for consistency with how an invalid
tabsize value is handled.
- files.c: - files.c:
close_open_file() close_open_file()
- Tweak to no longer rely on the return values of - Tweak to no longer rely on the return values of
@ -73,6 +83,12 @@ CVS code -
nregcomp() nregcomp()
- Rename the variable flags to eflags so as not to conflict with - Rename the variable flags to eflags so as not to conflict with
the global flags. (DLR) the global flags. (DLR)
- utils.c:
parse_num()
- New function to parse numeric values, so that we don't have to
duplicate code that calls strtol() all over the place. (David
Benbennick) DLR: Renamed from parse_int() to parse_num() and
converted to use ssize_t instead of int.
- winio.c: - winio.c:
get_control_kbinput() get_control_kbinput()
- Fix erroneous debugging statement so that nano compiles with - Fix erroneous debugging statement so that nano compiles with

View File

@ -30,8 +30,9 @@
/* Global variables */ /* Global variables */
#ifndef DISABLE_WRAPJUSTIFY #ifndef DISABLE_WRAPJUSTIFY
/* wrap_at might be set in rcfile.c or nano.c */ /* wrap_at might be set in rcfile.c or nano.c. */
int wrap_at = -CHARS_FROM_EOL;/* Right justified fill value, allows resize */ ssize_t wrap_at = -CHARS_FROM_EOL; /* Right justified fill value,
allows resize */
#endif #endif
char *last_search = NULL; /* Last string we searched for */ char *last_search = NULL; /* Last string we searched for */
@ -94,8 +95,8 @@ int placewewant = 0; /* The column we'd like the cursor
to jump to when we go to the to jump to when we go to the
next or previous line */ next or previous line */
int tabsize = -1; /* Our internal tabsize variable. The ssize_t tabsize = -1; /* Our internal tabsize variable. The
default value 8 is set in main(). */ default value is set in main(). */
char *hblank = NULL; /* A horizontal blank line */ char *hblank = NULL; /* A horizontal blank line */
#ifndef DISABLE_HELP #ifndef DISABLE_HELP

View File

@ -58,7 +58,8 @@
#endif #endif
#ifndef DISABLE_WRAPJUSTIFY #ifndef DISABLE_WRAPJUSTIFY
static int fill = 0; /* Fill - where to wrap lines, basically */ static ssize_t fill = 0; /* Fill - where to wrap lines,
basically */
#endif #endif
#ifndef DISABLE_WRAPPING #ifndef DISABLE_WRAPPING
static int same_line_wrap = FALSE; /* Whether wrapped text should static int same_line_wrap = FALSE; /* Whether wrapped text should
@ -3173,22 +3174,11 @@ int main(int argc, char *argv[])
break; break;
#endif #endif
case 'T': case 'T':
{ if (parse_num(optarg, &tabsize) == -1 || tabsize <= 0) {
int i; fprintf(stderr, _("Requested tab size %s invalid"), optarg);
char *first_error; fprintf(stderr, "\n");
/* Using strtol() instead of atoi() lets us accept 0
* while checking other errors. */
i = (int)strtol(optarg, &first_error, 10);
if (errno == ERANGE || *optarg == '\0' || *first_error != '\0')
usage();
else
tabsize = i;
if (tabsize <= 0) {
fprintf(stderr, _("Tab size is too small for nano...\n"));
exit(1); exit(1);
} }
}
break; break;
case 'V': case 'V':
version(); version();
@ -3233,17 +3223,10 @@ int main(int argc, char *argv[])
break; break;
#ifndef DISABLE_WRAPJUSTIFY #ifndef DISABLE_WRAPJUSTIFY
case 'r': case 'r':
{ if (parse_num(optarg, &wrap_at) == -1) {
int i; fprintf(stderr, _("Requested fill size %s invalid"), optarg);
char *first_error; fprintf(stderr, "\n");
exit(1);
/* Using strtol() instead of atoi() lets us accept 0
* while checking other errors. */
i = (int)strtol(optarg, &first_error, 10);
if (errno == ERANGE || *optarg == '\0' || *first_error != '\0')
usage();
else
wrap_at = i;
} }
fill_flag_used = TRUE; fill_flag_used = TRUE;
break; break;
@ -3298,7 +3281,7 @@ int main(int argc, char *argv[])
char *operating_dir_cpy = operating_dir; char *operating_dir_cpy = operating_dir;
#endif #endif
#ifndef DISABLE_WRAPPING #ifndef DISABLE_WRAPPING
int wrap_at_cpy = wrap_at; ssize_t wrap_at_cpy = wrap_at;
#endif #endif
#ifndef NANO_SMALL #ifndef NANO_SMALL
char *backup_dir_cpy = backup_dir; char *backup_dir_cpy = backup_dir;
@ -3309,7 +3292,7 @@ int main(int argc, char *argv[])
#ifndef DISABLE_SPELLER #ifndef DISABLE_SPELLER
char *alt_speller_cpy = alt_speller; char *alt_speller_cpy = alt_speller;
#endif #endif
int tabsize_cpy = tabsize; ssize_t tabsize_cpy = tabsize;
long flags_cpy = flags; long flags_cpy = flags;
#ifndef DISABLE_OPERATINGDIR #ifndef DISABLE_OPERATINGDIR
@ -3528,7 +3511,7 @@ int main(int argc, char *argv[])
titlebar(NULL); titlebar(NULL);
if (startline > 0) if (startline > 0)
do_gotoline(startline, 0); do_gotoline(startline, FALSE);
#ifndef NANO_SMALL #ifndef NANO_SMALL
/* Return here after a SIGWINCH. */ /* Return here after a SIGWINCH. */

View File

@ -22,15 +22,13 @@
/* Externs. */ /* Externs. */
#include <sys/stat.h> #include <sys/stat.h>
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
#include <regex.h> #include <regex.h>
#endif #endif
#include "nano.h" #include "nano.h"
#ifndef DISABLE_WRAPJUSTIFY #ifndef DISABLE_WRAPJUSTIFY
extern int wrap_at; extern ssize_t wrap_at;
#endif #endif
extern int editwinrows; extern int editwinrows;
extern int current_x, current_y, totlines; extern int current_x, current_y, totlines;
@ -40,7 +38,7 @@ extern int mark_beginx;
#endif #endif
extern long totsize; extern long totsize;
extern long flags; extern long flags;
extern int tabsize; extern ssize_t tabsize;
extern int search_last_line; extern int search_last_line;
extern int currslen; extern int currslen;
@ -403,7 +401,7 @@ char *replace_line(const char *needle);
int do_replace_loop(const char *needle, const filestruct *real_current, int do_replace_loop(const char *needle, const filestruct *real_current,
size_t *real_current_x, int wholewords); size_t *real_current_x, int wholewords);
void do_replace(void); void do_replace(void);
void do_gotoline(int line, int save_pos); void do_gotoline(ssize_t line, int save_pos);
void do_gotoline_void(void); void do_gotoline_void(void);
#if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER) #if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER)
void do_gotopos(int line, int pos_x, int pos_y, int pos_placewewant); void do_gotopos(int line, int pos_x, int pos_y, int pos_placewewant);
@ -438,6 +436,7 @@ int is_blank_char(int c);
#endif #endif
int is_cntrl_char(int c); int is_cntrl_char(int c);
int num_of_digits(int n); int num_of_digits(int n);
int parse_num(const char *str, ssize_t *val);
void align(char **strp); void align(char **strp);
void null_at(char **data, size_t index); void null_at(char **data, size_t index);
void unsunder(char *str, size_t true_len); void unsunder(char *str, size_t true_len);

View File

@ -476,7 +476,7 @@ void parse_colors(char *ptr)
void parse_rcfile(FILE *rcstream) void parse_rcfile(FILE *rcstream)
{ {
char *buf, *ptr, *keyword, *option; char *buf, *ptr, *keyword, *option;
int set = 0, i, j; int set = 0, i;
buf = charalloc(1024); buf = charalloc(1024);
while (fgets(buf, 1023, rcstream) != 0) { while (fgets(buf, 1023, rcstream) != 0) {
@ -571,18 +571,10 @@ void parse_rcfile(FILE *rcstream)
#endif #endif
#ifndef DISABLE_WRAPJUSTIFY #ifndef DISABLE_WRAPJUSTIFY
if (!strcasecmp(rcopts[i].name, "fill")) { if (!strcasecmp(rcopts[i].name, "fill")) {
char *first_error; if (parse_num(option, &wrap_at) == -1)
/* Using strtol() instead of atoi() lets
* us accept 0 while checking other
* errors. */
j = (int)strtol(option, &first_error, 10);
if (errno == ERANGE || *option == '\0' || *first_error != '\0')
rcfile_error( rcfile_error(
N_("Requested fill size %d invalid"), N_("Requested fill size %s invalid"),
j); option);
else
wrap_at = j;
} else } else
#endif #endif
#ifndef NANO_SMALL #ifndef NANO_SMALL
@ -630,17 +622,10 @@ void parse_rcfile(FILE *rcstream)
else else
#endif #endif
if (!strcasecmp(rcopts[i].name, "tabsize")) { if (!strcasecmp(rcopts[i].name, "tabsize")) {
char *first_error; if (parse_num(option, &tabsize) == -1)
rcfile_error(
/* Using strtol instead of atoi lets us N_("Requested tab size %s invalid"),
* accept 0 while checking other option);
* errors. */
j = (int)strtol(option, &first_error, 10);
if (errno == ERANGE || *option == '\0' || *first_error != '\0')
rcfile_error(N_("Requested tab size %d invalid"),
j);
else
tabsize = j;
} }
} else } else
SET(rcopts[i].flag); SET(rcopts[i].flag);

View File

@ -238,11 +238,9 @@ int search_init(int replacing)
#ifndef NANO_SMALL #ifndef NANO_SMALL
search_history.current = search_history.next; search_history.current = search_history.next;
#endif #endif
i = (int)strtol(answer, &buf, 10); /* Just testing answer here. */ /* If answer parses as an integer, put it up on the
if (!(errno == ERANGE || *answer == '\0' || *buf != '\0')) * statusbar. */
do_gotoline(-1, FALSE); do_gotoline(parse_num(answer, NULL), FALSE);
else
do_gotoline_void();
/* Fall through. */ /* Fall through. */
default: default:
return -1; return -1;
@ -828,7 +826,7 @@ void do_replace(void)
replace_abort(); replace_abort();
} }
void do_gotoline(int line, int save_pos) void do_gotoline(ssize_t line, int save_pos)
{ {
if (line <= 0) { /* Ask for it */ if (line <= 0) { /* Ask for it */
char *ans = mallocstrcpy(NULL, answer); char *ans = mallocstrcpy(NULL, answer);

View File

@ -21,11 +21,12 @@
#include "config.h" #include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <assert.h> #include <assert.h>
#include "proto.h" #include "proto.h"
#include "nano.h" #include "nano.h"
@ -83,6 +84,22 @@ int num_of_digits(int n)
return i; return i;
} }
/* 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)
{
char *first_error;
ssize_t j;
assert(str != NULL);
j = (ssize_t)strtol(str, &first_error, 10);
if (errno == ERANGE || *str == '\0' || *first_error != '\0')
return -1;
if (val != NULL)
*val = j;
return 0;
}
/* Fix the memory allocation for a string. */ /* Fix the memory allocation for a string. */
void align(char **strp) void align(char **strp)
{ {