Skip to content

Commit

Permalink
Constify.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Nov 7, 2023
1 parent 987ebdc commit f1bc6a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
26 changes: 13 additions & 13 deletions lesskey.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@
#include "lesskey.h"
#include "cmd.h"

char fileheader[] = {
constant char fileheader[] = {
C0_LESSKEY_MAGIC,
C1_LESSKEY_MAGIC,
C2_LESSKEY_MAGIC,
C3_LESSKEY_MAGIC
};
char filetrailer[] = {
constant char filetrailer[] = {
C0_END_LESSKEY_MAGIC,
C1_END_LESSKEY_MAGIC,
C2_END_LESSKEY_MAGIC
};
char cmdsection[1] = { CMD_SECTION };
char editsection[1] = { EDIT_SECTION };
char varsection[1] = { VAR_SECTION };
char endsection[1] = { END_SECTION };
constant char cmdsection[1] = { CMD_SECTION };
constant char editsection[1] = { EDIT_SECTION };
constant char varsection[1] = { VAR_SECTION };
constant char endsection[1] = { END_SECTION };

char *infile = NULL;
char *outfile = NULL ;
Expand All @@ -114,7 +114,7 @@ static void usage(void)
exit(1);
}

void lesskey_parse_error(char *s)
void lesskey_parse_error(constant char *s)
{
fprintf(stderr, "%s\n", s);
}
Expand All @@ -140,7 +140,7 @@ void * ecalloc(int count, unsigned int size)
return (p);
}

static char * mkpathname(char *dirname, char *filename)
static char * mkpathname(constant char *dirname, constant char *filename)
{
char *pathname;

Expand All @@ -154,7 +154,7 @@ static char * mkpathname(char *dirname, char *filename)
/*
* Figure out the name of a default file (in the user's HOME directory).
*/
char * homefile(char *filename)
char * homefile(constant char *filename)
{
char *p;
char *pathname;
Expand Down Expand Up @@ -246,7 +246,7 @@ static void parse_args(int argc, char **argv)
/*
* Output some bytes.
*/
static void fputbytes(FILE *fd, char *buf, int len)
static void fputbytes(FILE *fd, constant char *buf, int len)
{
while (len-- > 0)
{
Expand Down Expand Up @@ -338,16 +338,16 @@ int main(int argc, char *argv[])
/* Command key section */
fputbytes(out, cmdsection, sizeof(cmdsection));
fputint(out, tables.cmdtable.buf.end);
fputbytes(out, (char *)tables.cmdtable.buf.data, tables.cmdtable.buf.end);
fputbytes(out, xbuf_char_data(&tables.cmdtable.buf), tables.cmdtable.buf.end);
/* Edit key section */
fputbytes(out, editsection, sizeof(editsection));
fputint(out, tables.edittable.buf.end);
fputbytes(out, (char *)tables.edittable.buf.data, tables.edittable.buf.end);
fputbytes(out, xbuf_char_data(&tables.edittable.buf), tables.edittable.buf.end);

/* Environment variable section */
fputbytes(out, varsection, sizeof(varsection));
fputint(out, tables.vartable.buf.end);
fputbytes(out, (char *)tables.vartable.buf.data, tables.vartable.buf.end);
fputbytes(out, xbuf_char_data(&tables.vartable.buf), tables.vartable.buf.end);

/* File trailer */
fputbytes(out, endsection, sizeof(endsection));
Expand Down
14 changes: 10 additions & 4 deletions lesskey.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
* For more information, see the README file.
*/

#if HAVE_CONST
#define constant const
#else
#define constant
#endif

#include "xbuf.h"

/*
Expand Down Expand Up @@ -41,13 +47,13 @@

struct lesskey_cmdname
{
char *cn_name;
constant char *cn_name;
int cn_action;
};

struct lesskey_table
{
struct lesskey_cmdname *names;
constant struct lesskey_cmdname *names;
struct xbuffer buf;
int is_var;
};
Expand All @@ -60,8 +66,8 @@ struct lesskey_tables
struct lesskey_table vartable;
};

extern int parse_lesskey(char *infile, struct lesskey_tables *tables);
extern int parse_lesskey_content(char *content, struct lesskey_tables *tables);
extern int parse_lesskey(constant char *infile, struct lesskey_tables *tables);
extern int parse_lesskey_content(constant char *content, struct lesskey_tables *tables);

/* keep in sync with less.h */
#if HAVE_SNPRINTF
Expand Down
26 changes: 13 additions & 13 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ extern char version[];
static int linenum;
static int errors;
static int less_version = 0;
static char *lesskey_file;
static char *lesskey_file = NULL;

static struct lesskey_cmdname cmdnames[] =
static constant struct lesskey_cmdname cmdnames[] =
{
{ "back-bracket", A_B_BRACKET },
{ "back-line", A_B_LINE },
Expand Down Expand Up @@ -99,7 +99,7 @@ static struct lesskey_cmdname cmdnames[] =
{ NULL, 0 }
};

static struct lesskey_cmdname editnames[] =
static constant struct lesskey_cmdname editnames[] =
{
{ "back-complete", EC_B_COMPLETE },
{ "backspace", EC_BACKSPACE },
Expand Down Expand Up @@ -127,7 +127,7 @@ static struct lesskey_cmdname editnames[] =
/*
* Print a parse error message.
*/
static void parse_error(char *fmt, char *arg1)
static void parse_error(constant char *fmt, constant char *arg1)
{
char buf[1024];
int n = SNPRINTF2(buf, sizeof(buf), "%s: line %d: ", lesskey_file, linenum);
Expand Down Expand Up @@ -185,7 +185,7 @@ static char * increment_pointer(char *p)
/*
* Parse one character of a string.
*/
static char * tstr(char **pp, int xlate)
static constant char * tstr(char **pp, int xlate)
{
char *p;
char ch;
Expand Down Expand Up @@ -353,7 +353,7 @@ static void erase_cmd_char(struct lesskey_tables *tables)
/*
* Add a string to the output command table.
*/
static void add_cmd_str(char *s, struct lesskey_tables *tables)
static void add_cmd_str(constant char *s, struct lesskey_tables *tables)
{
for ( ; *s != '\0'; s++)
add_cmd_char(*s, tables);
Expand Down Expand Up @@ -475,7 +475,7 @@ static void parse_cmdline(char *p, struct lesskey_tables *tables)
{
char *actname;
int action;
char *s;
constant char *s;
char c;

/*
Expand Down Expand Up @@ -539,7 +539,7 @@ static void parse_cmdline(char *p, struct lesskey_tables *tables)
*/
static void parse_varline(char *line, struct lesskey_tables *tables)
{
char *s;
constant char *s;
char *p = line;
char *eq;

Expand Down Expand Up @@ -612,13 +612,12 @@ static void parse_line(char *line, struct lesskey_tables *tables)
/*
* Parse a lesskey source file and store result in tables.
*/
int parse_lesskey(char *infile, struct lesskey_tables *tables)
int parse_lesskey(constant char *ainfile, struct lesskey_tables *tables)
{
FILE *desc;
char line[1024];

if (infile == NULL)
infile = homefile(DEF_LESSKEYINFILE);
char *infile = (ainfile != NULL) ? strdup(ainfile) : homefile(DEF_LESSKEYINFILE);
if (lesskey_file != NULL) free(lesskey_file);
lesskey_file = infile;

init_tables(tables);
Expand All @@ -637,6 +636,7 @@ int parse_lesskey(char *infile, struct lesskey_tables *tables)
/* parse_error("cannot open lesskey file %s", infile); */
return (-1);
}
free(infile);

/*
* Read and parse the input file, one line at a time.
Expand All @@ -653,7 +653,7 @@ int parse_lesskey(char *infile, struct lesskey_tables *tables)
/*
* Parse a lesskey source content and store result in tables.
*/
int parse_lesskey_content(char *content, struct lesskey_tables *tables)
int parse_lesskey_content(constant char *content, struct lesskey_tables *tables)
{
int cx = 0;

Expand Down

0 comments on commit f1bc6a9

Please sign in to comment.