From ede07ef15743414a522957151fa3ca8e118e8aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Fri, 3 Apr 2020 17:36:47 +0200 Subject: [PATCH] Add const to some function args and variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2020-04-03 Tim Rühsen * src/pk-cmd.c (skip_blanks): Take const char *, return const char *. (pk_atoi): Add const to p. (pk_cmd_exec_1): Add const to str param and to several vars. (pk_cmd_exec): Add const to param and several vars, use cmd_alloc for allocation. * src/pk-cmd.h (pk_cmd_exec): Add const to param. * pkl-parser.c (pkl_parse_buffer): Add const to 'buffer' and 'end'. * src/pkl-parser.h (pkl_parse_buffer): Likewise. * src/pkl.c (pkl_compile_buffer): Add const to 'buffer' and 'end'. (pkl_compile_statement): Likewise. (pkl_compile_expression): Likewise. * src/pkl.h (pkl_compile_buffer): Add const to 'buffer' and 'end'. (pkl_compile_statement): Likewise. (pkl_compile_expression): Likewise. --- ChangeLog | 21 +++++++++++++++++++++ src/pk-cmd.c | 43 +++++++++++++++++++++++-------------------- src/pk-cmd.h | 2 +- src/pkl-parser.c | 2 +- src/pkl-parser.h | 2 +- src/pkl.c | 7 ++++--- src/pkl.h | 6 +++--- 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/pk-cmd.c b/src/pk-cmd.c index bcd34330..3b88ed16 100644 --- a/src/pk-cmd.c +++ b/src/pk-cmd.c @@ -86,8 +86,8 @@ static const struct pk_cmd *dot_cmds[] = /* Convenience macros and functions for parsing. */ -static inline char * -skip_blanks (char *p) +static inline const char * +skip_blanks (const char *p) { while (isblank (*p)) p++; @@ -95,7 +95,7 @@ skip_blanks (char *p) } static inline int -pk_atoi (char **p, int64_t *number) +pk_atoi (const char **p, int64_t *number) { long int li; char *end; @@ -259,7 +259,7 @@ pk_print_trie (int indent, struct pk_trie *trie) #define MAX_CMD_NAME 18 static int -pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) +pk_cmd_exec_1 (const char *str, struct pk_trie *cmds_trie, char *prefix) { #define GOTO_USAGE() \ do { \ @@ -269,7 +269,8 @@ pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) } while (1) int ret = 1; size_t i; - char cmd_name[MAX_CMD_NAME], *p; + char cmd_name[MAX_CMD_NAME]; + const char *p; const struct pk_cmd *cmd; int argc = 0; struct pk_cmd_arg argv[8]; @@ -371,7 +372,7 @@ pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) the next argument or the end of the input is found. */ while (*a != ',' && *a != '\0') { - char *beg = p; + const char *beg = p; switch (*a) { @@ -379,8 +380,8 @@ pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) { /* Compile a poke program. */ pvm_routine routine; - char *end; - char *program_string; + const char *end; + const char *program_string; program_string = p; routine = pkl_compile_expression (poke_compiler, @@ -452,7 +453,8 @@ pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) { /* Parse a string. */ - char *end, *str; + const char *end; + char *str; size_t size; p = skip_blanks (p); @@ -466,9 +468,9 @@ pk_cmd_exec_1 (char *str, struct pk_trie *cmds_trie, char *prefix) /* Trim trailing space. */ if (size) { - end = str + size - 1; - while (end > str && isspace ((unsigned char) *end)) - *end-- = '\0'; + char *e = str + size - 1; + while (e > str && isspace ((unsigned char) *e)) + *e-- = '\0'; } argv[argc].type = PK_CMD_ARG_STR; @@ -605,19 +607,20 @@ extern struct pk_trie *set_trie; /* pk-set.c */ static struct pk_trie *cmds_trie; int -pk_cmd_exec (char *str) +pk_cmd_exec (const char *str) { /* If the first non-blank character in STR is a dot ('.'), then this is a poke command. Dispatch it with pk_cmd_exec_1. Otherwise, compile a Poke declaration or a statement and execute it. */ - char *cmd = skip_blanks (str); + const char *cmd = skip_blanks (str); if (*cmd == '.') return pk_cmd_exec_1 (cmd + 1, cmds_trie, NULL); else { - char *ecmd = cmd, *end; + const char *ecmd = cmd, *end; + char *cmd_alloc = NULL; pvm_val val; int what; /* 0 -> declaration, 1 -> statement */ int retval = 1; @@ -642,9 +645,10 @@ pk_cmd_exec (char *str) && strncmp (ecmd, "defun\t", 6) != 0) { size_t len = strlen (cmd); - ecmd = xmalloc (len + 2); - memcpy (ecmd, cmd, len); - memcpy (ecmd + len, ";", 2); /* incl. trailing 0 */ + cmd_alloc = xmalloc (len + 2); + memcpy (cmd_alloc, cmd, len); + memcpy (cmd_alloc + len, ";", 2); /* incl. trailing 0 */ + ecmd = cmd_alloc; } if (what == 0) @@ -676,8 +680,7 @@ pk_cmd_exec (char *str) } cleanup: - if (ecmd != cmd) - free (ecmd); + free (cmd_alloc); return retval; } } diff --git a/src/pk-cmd.h b/src/pk-cmd.h index c2ecc3fa..67abb115 100644 --- a/src/pk-cmd.h +++ b/src/pk-cmd.h @@ -94,7 +94,7 @@ struct pk_cmd /* Parse STR and execute a command. Return 1 if the command was executed successfully, 0 otherwise. */ -int pk_cmd_exec (char *str); +int pk_cmd_exec (const char *str); /* Execute commands from the given FILENAME. Return 1 if all the commands were executed successfully, 0 otherwise. */ diff --git a/src/pkl-parser.c b/src/pkl-parser.c index 4ca1d1ec..3526c462 100644 --- a/src/pkl-parser.c +++ b/src/pkl-parser.c @@ -106,7 +106,7 @@ pkl_parse_file (pkl_compiler compiler, pkl_env *env, int pkl_parse_buffer (pkl_compiler compiler, pkl_env *env, - pkl_ast *ast, int what, const char *buffer, char **end) + pkl_ast *ast, int what, const char *buffer, const char **end) { YY_BUFFER_STATE yybuffer; struct pkl_parser *parser; diff --git a/src/pkl-parser.h b/src/pkl-parser.h index d9b39b40..6287249d 100644 --- a/src/pkl-parser.h +++ b/src/pkl-parser.h @@ -57,7 +57,7 @@ struct pkl_parser int pkl_parse_file (pkl_compiler compiler, pkl_env *env, pkl_ast *ast, FILE *fd, const char *fname); int pkl_parse_buffer (pkl_compiler compiler, pkl_env *env, pkl_ast *ast, - int what, const char *buffer, char **end); + int what, const char *buffer, const char **end); diff --git a/src/pkl.c b/src/pkl.c index 9b7188a1..ade522ff 100644 --- a/src/pkl.c +++ b/src/pkl.c @@ -219,7 +219,7 @@ rest_of_compilation (pkl_compiler compiler, int pkl_compile_buffer (pkl_compiler compiler, - char *buffer, char **end) + const char *buffer, const char **end) { pkl_ast ast = NULL; pvm_routine routine; @@ -274,7 +274,7 @@ pkl_compile_buffer (pkl_compiler compiler, int pkl_compile_statement (pkl_compiler compiler, - char *buffer, char **end, + const char *buffer, const char **end, pvm_val *val) { pkl_ast ast = NULL; @@ -325,7 +325,8 @@ pkl_compile_statement (pkl_compiler compiler, pvm_routine pkl_compile_expression (pkl_compiler compiler, - char *buffer, char **end, void **pointers) + const char *buffer, const char **end, + void **pointers) { pkl_ast ast = NULL; pvm_routine routine; diff --git a/src/pkl.h b/src/pkl.h index 2e349f1a..d8cde375 100644 --- a/src/pkl.h +++ b/src/pkl.h @@ -78,12 +78,12 @@ int pkl_compile_file (pkl_compiler compiler, const char *fname); END is set to the first character in BUFFER that is not part of the compiled entity. */ -int pkl_compile_buffer (pkl_compiler compiler, char *buffer, char **end); +int pkl_compile_buffer (pkl_compiler compiler, const char *buffer, const char **end); /* Like pkl_compile_buffer but compile a single Poke statement, which may generate a value in VAL if it is an "expression statement". */ -int pkl_compile_statement (pkl_compiler compiler, char *buffer, char **end, +int pkl_compile_statement (pkl_compiler compiler, const char *buffer, const char **end, pvm_val *val); @@ -92,7 +92,7 @@ int pkl_compile_statement (pkl_compiler compiler, char *buffer, char **end, return NULL. */ pvm_routine pkl_compile_expression (pkl_compiler compiler, - char *buffer, char **end, + const char *buffer, const char **end, void **pointers); /* Return the current compile-time environment in COMPILER. */ -- 2.26.0