Skip to content

Commit

Permalink
Constify.
Browse files Browse the repository at this point in the history
Add "<name>c" variant of some utility functions for const strings. Yuck.
  • Loading branch information
gwsw committed Nov 7, 2023
1 parent 42e1b42 commit f9f1681
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmdbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ public int cmd_char(int c)
*/
public LINENUM cmd_int(long *frac)
{
char *p;
constant char *p;
LINENUM n = 0;
int err;

Expand Down
11 changes: 7 additions & 4 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ static int mca_opt_first_char(LWCHAR c)
static int mca_opt_nonfirst_char(LWCHAR c)
{
char *p;
char *oname;
constant char *cp;
constant char *oname;
int err;

if (curropt != NULL)
Expand All @@ -417,7 +418,9 @@ static int mca_opt_nonfirst_char(LWCHAR c)
return (MCA_MORE);
opt_lower = ASCII_IS_LOWER(p[0]);
err = 0;
curropt = findopt_name(&p, &oname, &err);
cp = (constant char *) p; /*{{const-issue}}*/
curropt = findopt_name(&cp, &oname, &err);
p = (char *) cp;
if (curropt != NULL)
{
/*
Expand All @@ -427,9 +430,9 @@ static int mca_opt_nonfirst_char(LWCHAR c)
*/
cmd_reset();
mca_opt_toggle();
for (p = oname; *p != '\0'; p++)
for (cp = oname; *cp != '\0'; cp++)
{
c = *p;
c = *cp;
if (!opt_lower && ASCII_IS_LOWER(c))
c = ASCII_TO_UPPER(c);
if (cmd_char(c) != CC_OK)
Expand Down
3 changes: 3 additions & 0 deletions less.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,9 @@ void inttoa(int, char*, int);
int lstrtoi(char*, char**, int);
POSITION lstrtopos(char*, char**, int);
unsigned long lstrtoul(char*, char**, int);
int lstrtoic(constant char*, constant char**, int);
POSITION lstrtoposc(constant char*, constant char**, int);
unsigned long lstrtoulc(constant char*, constant char**, int);
#if MSDOS_COMPILER==WIN32C
int pclose(FILE*);
#endif
8 changes: 4 additions & 4 deletions lsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ extern IFILE curr_ifile;
* Pass the specified command to a shell to be executed.
* Like plain "system()", but handles resetting terminal modes, etc.
*/
public void lsystem(constant char *cmd, char *donemsg)
public void lsystem(constant char *cmd, constant char *donemsg)
{
int inp;
#if HAVE_SHELL
char *shell;
constant char *shell;
char *p;
#endif
IFILE save_ifile;
Expand Down Expand Up @@ -247,7 +247,7 @@ public void lsystem(constant char *cmd, char *donemsg)
* If the mark is on the current screen, or if the mark is ".",
* the whole current screen is piped.
*/
public int pipe_mark(int c, char *cmd)
public int pipe_mark(int c, constant char *cmd)
{
POSITION mpos, tpos, bpos;

Expand Down Expand Up @@ -278,7 +278,7 @@ public int pipe_mark(int c, char *cmd)
* Create a pipe to the given shell command.
* Feed it the file contents between the positions spos and epos.
*/
public int pipe_data(char *cmd, POSITION spos, POSITION epos)
public int pipe_data(constant char *cmd, POSITION spos, POSITION epos)
{
FILE *f;
int c;
Expand Down
2 changes: 1 addition & 1 deletion mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void mark_check_ifile(IFILE ifile)
/*
* Save marks to history file.
*/
public void save_marks(FILE *fout, char *hdr)
public void save_marks(FILE *fout, constant char *hdr)
{
int i;

Expand Down
4 changes: 2 additions & 2 deletions optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void opt__O(int type, char *s)
}
#endif

static int toggle_fraction(int *num, long *frac, char *s, char *printopt, void (*calc)(void))
static int toggle_fraction(int *num, long *frac, constant char *s, char *printopt, void (*calc)(void))
{
int err;
if (s == NULL)
Expand All @@ -176,7 +176,7 @@ static int toggle_fraction(int *num, long *frac, char *s, char *printopt, void (
(*calc)();
} else
{
int tnum = getnum(&s, printopt, &err);
int tnum = getnumc(&s, printopt, &err);
if (err)
{
error("Invalid number", NULL_PARG);
Expand Down
44 changes: 26 additions & 18 deletions option.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
static struct loption *pendopt;
public int plusoption = FALSE;

static char *optstring(char *s, char **p_str, constant char *printopt, char *validchars);
static constant char *optstring(constant char *s, char **p_str, constant char *printopt, char *validchars);
static int flip_triple(int val, int lc);

extern int less_is_more;
Expand Down Expand Up @@ -60,11 +60,11 @@ public constant char * propt(int c)
* Scan an argument (either from the command line or from the
* LESS environment variable) and process it.
*/
public void scan_option(char *s)
public void scan_option(constant char *s)
{
struct loption *o;
int optc;
char *optname;
constant char *optname;
constant char *printopt;
char *str;
int set_default;
Expand All @@ -89,11 +89,11 @@ public void scan_option(char *s)
switch (pendopt->otype & OTYPE)
{
case STRING:
(*pendopt->ofunc)(INIT, s);
(*pendopt->ofunc)(INIT, (char*) s); /*{{const-issue}}*/
break;
case NUMBER:
printopt = opt_desc(pendopt);
*(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
*(pendopt->ovar) = getnumc(&s, printopt, (int*)NULL);
break;
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public void scan_option(char *s)
* EVERY input file.
*/
plusoption = TRUE;
s = optstring(s, &str, propt('+'), NULL);
s = optstring(s, &str, propt('+'), NULL); /*{{const-issue}}*/
if (s == NULL)
return;
if (*str == '+')
Expand Down Expand Up @@ -281,7 +281,7 @@ public void scan_option(char *s)
}
if (o->otype & UNSUPPORTED)
break;
*(o->ovar) = getnum(&s, printopt, (int*)NULL);
*(o->ovar) = getnumc(&s, printopt, (int*)NULL);
break;
}
/*
Expand Down Expand Up @@ -571,9 +571,9 @@ public void nopendopt(void)
* "d" indicates a string of one or more digits (0-9)
* "," indicates a comma-separated list of digit strings is allowed
*/
static char * optstring(char *s, char **p_str, constant char *printopt, char *validchars)
static constant char * optstring(constant char *s, char **p_str, constant char *printopt, char *validchars)
{
char *p;
constant char *p;
char *out;

if (*s == '\0')
Expand Down Expand Up @@ -657,13 +657,13 @@ static int num_error(constant char *printopt, int *errp, int overflow)
* Like atoi(), but takes a pointer to a char *, and updates
* the char * to point after the translated number.
*/
public int getnum(char **sp, constant char *printopt, int *errp)
public int getnumc(constant char **sp, constant char *printopt, int *errp)
{
constant char *s;
constant char *s = *sp;
int n;
int neg;

s = skipsp(*sp);
s = skipspc(s);
neg = FALSE;
if (*s == '-')
{
Expand All @@ -673,7 +673,7 @@ public int getnum(char **sp, constant char *printopt, int *errp)
if (*s < '0' || *s > '9')
return (num_error(printopt, errp, FALSE));

n = lstrtoi((char*)s, sp, 10); /*{{const-issue}}*/
n = lstrtoic(s, sp, 10);
if (n < 0)
return (num_error(printopt, errp, TRUE));
if (errp != NULL)
Expand All @@ -683,19 +683,27 @@ public int getnum(char **sp, constant char *printopt, int *errp)
return (n);
}

public int getnum(char **sp, constant char *printopt, int *errp)
{
constant char *cs = *sp;
int r = getnumc(&cs, printopt, errp);
*sp = (char *) cs;
return r;
}

/*
* Translate a string into a fraction, represented by the part of a
* number which would follow a decimal point.
* The value of the fraction is returned as parts per NUM_FRAC_DENOM.
* That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
*/
public long getfraction(char **sp, constant char *printopt, int *errp)
public long getfraction(constant char **sp, constant char *printopt, int *errp)
{
constant char *s;
long frac = 0;
int fraclen = 0;

s = skipsp(*sp);
s = skipspc(*sp);
if (*s < '0' || *s > '9')
return (num_error(printopt, errp, FALSE));

Expand All @@ -708,7 +716,7 @@ public long getfraction(char **sp, constant char *printopt, int *errp)
}
while (fraclen++ < NUM_LOG_FRAC_DENOM)
frac *= 10;
*sp = (char*) s; /*{{const-issue}}*/
*sp = /*(char*)*/ s; /*{{const-issue}}*/
if (errp != NULL)
*errp = FALSE;
return (frac);
Expand All @@ -720,13 +728,13 @@ public long getfraction(char **sp, constant char *printopt, int *errp)
*/
public void init_unsupport(void)
{
char *s = lgetenv("LESS_UNSUPPORT");
constant char *s = lgetenv("LESS_UNSUPPORT");
if (isnullenv(s))
return;
for (;;)
{
struct loption *opt;
s = skipsp(s);
s = skipspc(s);
if (*s == '\0') break;
if (*s == '-' && *++s == '\0') break;
if (*s == '-') /* long option name */
Expand Down
4 changes: 2 additions & 2 deletions opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,9 @@ static int is_optchar(char c)
* is updated to point after the matched name.
* p_oname if non-NULL is set to point to the full option name.
*/
public struct loption * findopt_name(char **p_optname, char **p_oname, int *p_err)
public struct loption * findopt_name(constant char **p_optname, constant char **p_oname, int *p_err)
{
char *optname = *p_optname;
constant char *optname = *p_optname;
struct loption *o;
struct optname *oname;
int len;
Expand Down
17 changes: 12 additions & 5 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ TYPE_TO_A_FUNC(inttoa, int)
/*
* Convert a string to an integral type. Return ((type) -1) on overflow.
*/
#define STR_TO_TYPE_FUNC(funcname, type) \
type funcname(char *buf, char **ebuf, int radix) \
#define STR_TO_TYPE_FUNC(funcname, cfuncname, type) \
type cfuncname(constant char *buf, constant char **ebuf, int radix) \
{ \
type val = 0; \
int v = 0; \
Expand All @@ -489,11 +489,18 @@ type funcname(char *buf, char **ebuf, int radix) \
} \
if (ebuf != NULL) *ebuf = buf; \
return v ? -1 : val; \
} \
type funcname(char *buf, char **ebuf, int radix) \
{ \
constant char *cbuf = buf; \
int r = cfuncname(cbuf, &cbuf, radix); \
*ebuf = (char *) cbuf; /*{{const-issue}}*/ \
return r; \
}

STR_TO_TYPE_FUNC(lstrtopos, POSITION)
STR_TO_TYPE_FUNC(lstrtoi, int)
STR_TO_TYPE_FUNC(lstrtoul, unsigned long)
STR_TO_TYPE_FUNC(lstrtopos, lstrtoposc, POSITION)
STR_TO_TYPE_FUNC(lstrtoi, lstrtoic, int)
STR_TO_TYPE_FUNC(lstrtoul, lstrtoulc, unsigned long)

/*
* Print an integral type.
Expand Down

0 comments on commit f9f1681

Please sign in to comment.