diff --git a/cligen_buf.c b/cligen_buf.c index 4a1f0cd..0f78da4 100644 --- a/cligen_buf.c +++ b/cligen_buf.c @@ -312,7 +312,7 @@ vcprintf(cbuf *cb, */ int cbuf_append_str(cbuf *cb, - char *str) + const char *str) { size_t len0; size_t len; diff --git a/cligen_buf.h b/cligen_buf.h index f922ea0..13ccbc1 100644 --- a/cligen_buf.h +++ b/cligen_buf.h @@ -69,7 +69,7 @@ int cprintf(cbuf *cb, const char *format, ...) __attribute__ ((format (prin int vcprintf(cbuf *cb, const char *format, va_list ap); void cbuf_reset(cbuf *cb); int cbuf_append(cbuf *cb, int c); -int cbuf_append_str(cbuf *cb, char *str); +int cbuf_append_str(cbuf *cb, const char *str); int cbuf_append_buf(cbuf *cb, void *src, size_t n); int cbuf_trunc(cbuf *cb, size_t i); diff --git a/cligen_cv.c b/cligen_cv.c index 8c4e2fb..6281048 100644 --- a/cligen_cv.c +++ b/cligen_cv.c @@ -68,7 +68,7 @@ /* * URL protocol strings */ -static char *cg_urlprotostr[] = { +static const char *cg_urlprotostr[] = { NULL, "file", "flash", @@ -1336,7 +1336,7 @@ parse_dec64(char *str, if (n<=0 || n>18){ if (reason != NULL) - if ((*reason = cligen_reason("%s: fraction-digit=%d given but should be in interval [1:18]", __FUNCTION__, n)) == NULL){ + if ((*reason = cligen_reason("%s: fraction-digit=%d given but should be in interval [1:18]", __func__, n)) == NULL){ goto done; } retval = 0; @@ -2089,7 +2089,7 @@ cv_str2type(const char *str) const char * cv_type2str(enum cv_type type) { - char *str = NULL; + const char *str = NULL; switch (type){ case CGV_ERR: str="err"; @@ -2164,7 +2164,7 @@ cv_type2str(enum cv_type type) str="empty"; break; default: - fprintf(stderr, "%s: invalid type: %d\n", __FUNCTION__, type); + fprintf(stderr, "%s: invalid type: %d\n", __func__, type); break; } return str; @@ -2510,14 +2510,14 @@ cv2str(cg_var *cv, cv->var_ipv4masklen); break; case CGV_IPV6ADDR: - if (inet_ntop(AF_INET6, &cv->var_ipv6addr, straddr, sizeof(straddr)) < 0){ + if (NULL == inet_ntop(AF_INET6, &cv->var_ipv6addr, straddr, sizeof(straddr))){ fprintf(stderr, "inet_ntop: %s\n", strerror(errno)); return -1; } len = snprintf(str, size, "%s", straddr); break; case CGV_IPV6PFX: - if (inet_ntop(AF_INET6, &cv->var_ipv6addr, straddr, sizeof(straddr)) < 0){ + if (NULL == inet_ntop(AF_INET6, &cv->var_ipv6addr, straddr, sizeof(straddr))){ fprintf(stderr, "inet_ntop: %s\n", strerror(errno)); return -1; } diff --git a/cligen_cvec.c b/cligen_cvec.c index dc1bf4a..1592324 100644 --- a/cligen_cvec.c +++ b/cligen_cvec.c @@ -765,7 +765,7 @@ next_token(char **s0, s = *s0; if (s==NULL){ - fprintf(stderr, "%s: null string\n", __FUNCTION__); + fprintf(stderr, "%s: null string\n", __func__); return -1; } for (s=*s0; *s; s++){ /* First iterate through delimiters */ @@ -818,7 +818,7 @@ next_token(char **s0, } } if ((token=malloc(len+1)) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); return -1; } memcpy(token, st, len); diff --git a/cligen_expand.c b/cligen_expand.c index 5da9e52..c37af21 100644 --- a/cligen_expand.c +++ b/cligen_expand.c @@ -106,12 +106,12 @@ co_expand_sub(cg_obj *co0, co_up_set(con, coparent); if (co0->co_command) if ((con->co_command = strdup(co0->co_command)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_prefix) if ((con->co_prefix = strdup(co0->co_prefix)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_cvec) @@ -126,7 +126,7 @@ co_expand_sub(cg_obj *co0, if (co0->co_type == CO_VARIABLE){ if (co0->co_expand_fn_str) if ((con->co_expand_fn_str = strdup(co0->co_expand_fn_str)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_expand_fn_vec) @@ -134,12 +134,12 @@ co_expand_sub(cg_obj *co0, goto done; if (co0->co_translate_fn_str) if ((con->co_translate_fn_str = strdup(co0->co_translate_fn_str)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_show) if ((con->co_show = strdup(co0->co_show)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_rangecvv_low) @@ -150,12 +150,12 @@ co_expand_sub(cg_obj *co0, goto done; if (co0->co_choice) if ((con->co_choice = strdup(co0->co_choice)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } if (co0->co_regex) if ((con->co_regex = cvec_dup(co0->co_regex)) == NULL){ - fprintf(stderr, "%s: cvec_dup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: cvec_dup: %s\n", __func__, strerror(errno)); goto done; } } /* CO_VARIABLE */ @@ -962,7 +962,7 @@ pt_expand(cligen_handle h, */ cligen_parsetree_sort(ptn, 0); if (cligen_logsyntax(h) > 0){ - fprintf(stderr, "%s:\n", __FUNCTION__); + fprintf(stderr, "%s:\n", __func__); pt_print1(stderr, ptn, 0); } ok: diff --git a/cligen_getline.c b/cligen_getline.c index 31c1e83..1fb89b8 100644 --- a/cligen_getline.c +++ b/cligen_getline.c @@ -55,18 +55,18 @@ static void gl_init1(void); /* prepare to edit a line */ static void gl_cleanup(void); /* to undo gl_init1 */ void gl_char_init(void); /* get ready for no echo input */ void gl_char_cleanup(void); /* undo gl_char_init */ -static size_t (*gl_strlen)(const char *) = (size_t(*)())strlen; +static size_t (*gl_strlen)(const char *) = strlen; /* returns printable prompt width */ static int gl_addchar(cligen_handle h, int c); /* install specified char */ static void gl_del(cligen_handle h, int loc); /* del, either left (-1) or cur (0) */ -static inline void gl_fixup(cligen_handle h, char*,int,int);/* fixup state variables and screen */ +static inline void gl_fixup(cligen_handle h, const char*,int,int);/* fixup state variables and screen */ static int gl_getc(cligen_handle h); /* read one char from terminal */ static void gl_kill(cligen_handle h, int pos); /* delete to EOL */ static void gl_kill_begin(cligen_handle h, int pos); /* delete to BEGIN of line */ static int gl_kill_word(cligen_handle h, int pos); /* delete word */ static void gl_newline(cligen_handle); /* handle \n or \r */ -static int gl_puts(char *buf); /* write a line to terminal */ +static int gl_puts(const char *buf); /* write a line to terminal */ static void gl_transpose(cligen_handle h); /* transpose two chars */ static int gl_yank(cligen_handle h); /* yank killed text */ @@ -488,7 +488,7 @@ gl_putc(int c) /******************** fairly portable part *********************************/ static int -gl_puts(char *buf) +gl_puts(const char *buf) { int len; @@ -586,7 +586,7 @@ gl_getline(cligen_handle h, int c; int loc; int tmp; - char *gl_prompt; + const char *gl_prompt; int escape = 0; #ifdef __unix__ int sig; @@ -1172,7 +1172,7 @@ gl_redraw(cligen_handle h) */ static void gl_fixup_noscroll(cligen_handle h, - char *prompt, + const char *prompt, int change, int cursor) { @@ -1286,7 +1286,7 @@ gl_fixup_noscroll(cligen_handle h, */ static void gl_fixup_scroll(cligen_handle h, - char *prompt, + const char *prompt, int change, int cursor) { @@ -1396,20 +1396,20 @@ gl_fixup_scroll(cligen_handle h, static inline void gl_fixup(cligen_handle h, - char *prompt, + const char *prompt, int change, int cursor) { if (gl_scrolling_mode) - return gl_fixup_scroll(h, prompt, change, cursor); + gl_fixup_scroll(h, prompt, change, cursor); else - return gl_fixup_noscroll(h, prompt, change, cursor); + gl_fixup_noscroll(h, prompt, change, cursor); } /******************* strlen stuff **************************************/ void -gl_strwidth(size_t (*func)()) +gl_strwidth(size_t (*func)(const char*)) { if (func != 0) { gl_strlen = func; @@ -1509,7 +1509,7 @@ search_back(cligen_handle h, int new_search) { int found = 0; - char *p, *loc; + const char *p, *loc; int last; search_forw_flg = 0; @@ -1550,7 +1550,7 @@ search_forw(cligen_handle h, int new_search) { int found = 0; - char *p, *loc; + const char *p, *loc; int last; search_forw_flg = 1; diff --git a/cligen_getline.h b/cligen_getline.h index 3580d55..287100d 100644 --- a/cligen_getline.h +++ b/cligen_getline.h @@ -23,7 +23,7 @@ /* * Types */ -typedef size_t (*gl_strwidth_proc)(char *); +typedef size_t (*gl_strwidth_proc)(const char *); /* * Prototypes diff --git a/cligen_handle.c b/cligen_handle.c index 4835946..f10ba07 100644 --- a/cligen_handle.c +++ b/cligen_handle.c @@ -155,7 +155,7 @@ cligen_init(void) struct sigaction sigh; if ((ch = malloc(sizeof(*ch))) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); goto done; } memset(ch, 0, sizeof(*ch)); @@ -303,7 +303,7 @@ cligen_prompt(cligen_handle h) */ int cligen_prompt_set(cligen_handle h, - char *prompt) + const char *prompt) { struct cligen_handle *ch = handle(h); @@ -372,7 +372,7 @@ cligen_pt_head_active_set(cligen_handle h, * bar @bar; * y; */ -char* +const char* cligen_treename_keyword(cligen_handle h) { struct cligen_handle *ch = handle(h); @@ -1106,12 +1106,12 @@ cligen_buf_init(cligen_handle h) struct cligen_handle *ch = handle(h); if ((ch->ch_buf = malloc(_getline_bufsize)) == NULL){ - fprintf(stderr, "%s malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s malloc: %s\n", __func__, strerror(errno)); return -1; } memset(ch->ch_buf, 0, _getline_bufsize); if ((ch->ch_killbuf = malloc(_getline_killbufsize)) == NULL){ - fprintf(stderr, "%s malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s malloc: %s\n", __func__, strerror(errno)); return -1; } memset(ch->ch_killbuf, 0, _getline_killbufsize); @@ -1139,7 +1139,7 @@ cligen_buf_increase(cligen_handle h, while (_getline_bufsize < len1 + 1) _getline_bufsize *= 2; if ((ch->ch_buf = realloc(ch->ch_buf, _getline_bufsize)) == NULL){ - fprintf(stderr, "%s realloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s realloc: %s\n", __func__, strerror(errno)); return -1; } memset(ch->ch_buf+len0, 0, _getline_bufsize-len0); @@ -1163,7 +1163,7 @@ cligen_killbuf_increase(cligen_handle h, while (_getline_killbufsize < len1 + 1) _getline_killbufsize *= 2; if ((ch->ch_killbuf = realloc(ch->ch_killbuf, _getline_killbufsize)) == NULL){ - fprintf(stderr, "%s realloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s realloc: %s\n", __func__, strerror(errno)); return -1; } memset(ch->ch_killbuf+len0, 0, _getline_killbufsize-len0); diff --git a/cligen_handle.h b/cligen_handle.h index b09afd3..bd1acfa 100644 --- a/cligen_handle.h +++ b/cligen_handle.h @@ -71,9 +71,9 @@ * @retval -1 Error * @code * void *wh = NULL; - * cligen_eval_wrap_cb(h, &wh, "myfn", __FUNCTION__); + * cligen_eval_wrap_cb(h, &wh, "myfn", __func__); * .. User callback - * cligen_eval_wrap_cb(h, &wh, "myfn", __FUNCTION__); + * cligen_eval_wrap_cb(h, &wh, "myfn", __func__); * See cligen_eval */ typedef int (cligen_eval_wrap_fn)(void *arg, void **wh, const char *name, const char *fn); @@ -117,7 +117,7 @@ char cligen_comment(cligen_handle h); int cligen_comment_set(cligen_handle h, char c); char* cligen_prompt(cligen_handle h); -int cligen_prompt_set(cligen_handle h, char *prompt); +int cligen_prompt_set(cligen_handle h, const char *prompt); pt_head *cligen_pt_head_get(cligen_handle h); int cligen_pt_head_set(cligen_handle h, pt_head *ph); @@ -125,7 +125,7 @@ int cligen_pt_head_set(cligen_handle h, pt_head *ph); pt_head *cligen_pt_head_active_get(cligen_handle h); int cligen_pt_head_active_set(cligen_handle h, pt_head *ph); -char *cligen_treename_keyword(cligen_handle h); +const char *cligen_treename_keyword(cligen_handle h); int cligen_treename_keyword_set(cligen_handle h, char *name); /* After an evaluation (callback), which node in the parse-tree? */ diff --git a/cligen_hello.c b/cligen_hello.c index 32795b1..a4f38e9 100644 --- a/cligen_hello.c +++ b/cligen_hello.c @@ -66,7 +66,7 @@ str2fn(char *name, } /*! The command syntax specification */ -static char *hello_syntax = "prompt=\"hello> \";\n" +static const char *hello_syntax = "prompt=\"hello> \";\n" "hello(\"Greet the world\") world, cb(\"Hello World!\");" ; diff --git a/cligen_history.c b/cligen_history.c index 2adfe1a..4e1e60e 100644 --- a/cligen_history.c +++ b/cligen_history.c @@ -75,6 +75,13 @@ #include "cligen_history_internal.h" #include "cligen_history.h" +/* + * A well-defined empty string, used whenever one is needed as a placeholder, + * avoiding a malloc. By having it explicit, one can (more) easily spot a + * free(a) where a == CH_EMPTY_STR error. + */ +static char CH_EMPTY_STR[] = ""; + /*! Makes a copy of the string * * @param[in] p String input @@ -82,7 +89,7 @@ * @retval NULL Error */ static char * -hist_save(char *p) +hist_save(const char *p) { char *s = NULL; int len = strlen(p)+1; @@ -111,11 +118,11 @@ hist_save(char *p) */ int hist_add(cligen_handle h, - char *buf) + const char *buf) { struct cligen_handle *ch = handle(h); int retval = -1; - char *p = buf; + const char *p = buf; int len; if (strlen(buf) >= cligen_buf_size(h)) @@ -136,7 +143,7 @@ hist_add(cligen_handle h, if (ch->ch_hist_buf[ch->ch_hist_last] && *ch->ch_hist_buf[ch->ch_hist_last]) { free(ch->ch_hist_buf[ch->ch_hist_last]); } - ch->ch_hist_buf[ch->ch_hist_last] = ""; /* NB not-malloced, check in hist_free */ + ch->ch_hist_buf[ch->ch_hist_last] = CH_EMPTY_STR; } } ch->ch_hist_cur = ch->ch_hist_last; @@ -170,11 +177,11 @@ hist_exit(cligen_handle h) * * @param[in] h CLIgen handle */ -char * +const char * hist_prev(cligen_handle h) { struct cligen_handle *ch = handle(h); - char *p = 0; + const char *p = 0; int next = (ch->ch_hist_cur - 1 + ch->ch_hist_size) % ch->ch_hist_size; if (ch->ch_hist_buf[ch->ch_hist_cur] != 0 && next != ch->ch_hist_last) { @@ -192,11 +199,11 @@ hist_prev(cligen_handle h) * * @param[in] h CLIgen handle */ -char * +const char * hist_next(cligen_handle h) { struct cligen_handle *ch = handle(h); - char *p = 0; + const char *p = 0; if (ch->ch_hist_cur != ch->ch_hist_last) { ch->ch_hist_cur = (ch->ch_hist_cur+1) % ch->ch_hist_size; @@ -263,7 +270,7 @@ hist_copy(cligen_handle h, int hist_copy_prev(cligen_handle h) { - char *ptr = hist_prev(h); + const char *ptr = hist_prev(h); strncpy(cligen_buf(h), ptr, cligen_buf_size(h)); return 0; @@ -289,7 +296,7 @@ hist_copy_pos(cligen_handle h) int hist_copy_next(cligen_handle h) { - char *ptr = hist_next(h); + const char *ptr = hist_next(h); strncpy(cligen_buf(h), ptr, cligen_buf_size(h)); return 0; @@ -322,7 +329,7 @@ cligen_hist_init(cligen_handle h, ch->ch_hist_size = lines+1; /* circular buffer needs an extra element */ for (i=0; i < old_size; i++) if (ch->ch_hist_buf[i]){ - if (strlen(ch->ch_hist_buf[i])) + if (CH_EMPTY_STR != ch->ch_hist_buf[i]) free(ch->ch_hist_buf[i]); ch->ch_hist_buf[i] = NULL; } @@ -331,7 +338,7 @@ cligen_hist_init(cligen_handle h, ch->ch_hist_cur = 0; ch->ch_hist_last = 0; ch->ch_hist_pre = 0; - ch->ch_hist_buf[0] = ""; /* NB not-malloced, check in hist_free */ + ch->ch_hist_buf[0] = CH_EMPTY_STR; for (i=1; i < ch->ch_hist_size; i++) /* reset all entries */ ch->ch_hist_buf[i] = (char *)0; retval = 0; diff --git a/cligen_history_internal.h b/cligen_history_internal.h index bfda04c..b869b47 100644 --- a/cligen_history_internal.h +++ b/cligen_history_internal.h @@ -55,10 +55,10 @@ /* * Prototypes */ -int hist_add(cligen_handle h, char *); +int hist_add(cligen_handle h, const char *); int hist_exit(cligen_handle h); -char *hist_next(cligen_handle h); -char *hist_prev(cligen_handle h); +const char *hist_next(cligen_handle h); +const char *hist_prev(cligen_handle h); int hist_pos_set(cligen_handle h, int pos); int hist_pos(cligen_handle h); int hist_last_get(cligen_handle h); diff --git a/cligen_match.c b/cligen_match.c index a4c21dd..d31fa47 100644 --- a/cligen_match.c +++ b/cligen_match.c @@ -761,7 +761,7 @@ callbacks_merge(cg_obj *coref, strcmp(coref->co_callbacks->cc_fn_str, "prepend_me") == 0){ /* Assume only single coref parameter */ if ((cv = cvec_i(coref->co_callbacks->cc_cvec, 0)) == NULL){ - fprintf(stderr, "%s no first element in coref cvec\n", __FUNCTION__); + fprintf(stderr, "%s no first element in coref cvec\n", __func__); goto done; } cvv = cvec_from_var(cv); @@ -838,7 +838,7 @@ match_pattern_sets(cligen_handle h, token = cvec_i_str(cvt, level+1); /* for debugging */ #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s level: %d token:%s\npt:\n", __FUNCTION__, level*3,"", + fprintf(stderr, "%s %*s level: %d token:%s\npt:\n", __func__, level*3,"", level, strlen(token)?token:"\"\""); pt_print(stderr, pt); #endif @@ -846,7 +846,7 @@ match_pattern_sets(cligen_handle h, if (match_pattern_sets_local(h, cvt, cvr, pt, level, best, cvv, &mr0) < 0) goto done; #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s matchnr:%d\n", __FUNCTION__, level*3,"", mr_pt_len_get(mr0)); + fprintf(stderr, "%s %*s matchnr:%d\n", __func__, level*3,"", mr_pt_len_get(mr0)); #endif if (mr_pt_len_get(mr0) != 1){ /* If not unique match exit here */ *mrp = mr0; @@ -882,7 +882,7 @@ match_pattern_sets(cligen_handle h, goto done; } #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s match co:%s\n", __FUNCTION__, level*3,"", co_match->co_command); + fprintf(stderr, "%s %*s match co:%s\n", __func__, level*3,"", co_match->co_command); #endif if (mr_last_get(mr0) && (strcmp(token, "") != 0)){ mr_flags_set_co_match(mr0, co_match); @@ -931,7 +931,7 @@ match_pattern_sets(cligen_handle h, } if (pt_sets_get(ptn)){ /* For sets, iterate */ #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s sets:\n", __FUNCTION__, level*3,""); + fprintf(stderr, "%s %*s sets:\n", __func__, level*3,""); #endif while (!last_level(cvt, level)){ if (mrc != NULL) @@ -946,12 +946,12 @@ match_pattern_sets(cligen_handle h, &mrc) < 0) goto done; #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s sets matchnr: %d\n", __FUNCTION__, level*3,"", mr_pt_len_get(mrc)); + fprintf(stderr, "%s %*s sets matchnr: %d\n", __func__, level*3,"", mr_pt_len_get(mrc)); #endif if (mr_pt_len_get(mrc) != 1) break; #ifdef _DEBUG_SETS - fprintf(stderr, "%s %*s sets match co: %s\n", __FUNCTION__, level*3,"", + fprintf(stderr, "%s %*s sets match co: %s\n", __func__, level*3,"", mr_pt_i_get(mrc, 0)->co_command); #endif if (mrcprev != NULL) diff --git a/cligen_object.c b/cligen_object.c index 804dac5..7db51dc 100644 --- a/cligen_object.c +++ b/cligen_object.c @@ -1228,7 +1228,7 @@ co_value_set(cg_obj *co, } if (str != NULL) if ((co->co_value = strdup(str)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); return -1; } return 0; diff --git a/cligen_object.h b/cligen_object.h index 4cdca4e..3179afe 100644 --- a/cligen_object.h +++ b/cligen_object.h @@ -181,7 +181,7 @@ struct cg_obj{ struct cg_obj_common co_common; /* Inline of common fields accessed by macros below */ /*--- Up to here common with cg_obj_cmd */ union { /* depends on co_type: */ - struct { } cou_cmd; /* CO_COMMAND */ + // struct { } cou_cmd; /* CO_COMMAND */ struct cg_varspec cou_var; /* CO_VARIABLE */ // /* CO_REFERENCE */ } u; diff --git a/cligen_parse.h b/cligen_parse.h index c01c683..67f9969 100644 --- a/cligen_parse.h +++ b/cligen_parse.h @@ -63,10 +63,10 @@ struct cgy_list{ /*! CLIgen yacc parse structure, with all accumulated state of a parse session */ struct cligen_parse_yacc{ cligen_handle cy_handle; /* cligen_handle */ - char *cy_name; /* Name of syntax (for error string) */ + const char *cy_name; /* Name of syntax (for error string) */ char *cy_treename; /* Name of syntax (for error string) */ int cy_linenum; /* Number of \n in parsed buffer */ - char *cy_parse_string; /* original (copy of) parse string */ + const char *cy_parse_string; /* original (copy of) parse string */ void *cy_lexbuf; /* internal parse buffer from lex */ cvec *cy_globals; /* global variables after parsing */ cvec *cy_cvec; /* local variables (per-command) */ @@ -99,7 +99,7 @@ int cgy_exit(cligen_yacc *cy); int cligen_parselex(void *_ya); int cligen_parseparse(void *); -void cligen_parseerror(void *_ya, char*); +void cligen_parseerror(void *_ya, const char*); int cligen_parse_debug(int d); #endif /* _CLIGEN_PARSE_H_ */ diff --git a/cligen_parse.l b/cligen_parse.l index 36e7fee..a6bf6b1 100644 --- a/cligen_parse.l +++ b/cligen_parse.l @@ -77,7 +77,7 @@ stripdup(char *s0) char *s; if ((s1 = strdup(s0)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); return NULL; } while ((s = index(s1, '\\')) != NULL) @@ -231,8 +231,9 @@ cgl_init(cligen_yacc *cy) BEGIN(INITIAL); cy->cy_lexbuf = yy_scan_string (cy->cy_parse_string); #if 1 /* XXX: just to use unput to avoid warning */ + char bogus[] = ""; if (0) - yyunput(0, ""); + yyunput(0, bogus); #endif return 0; diff --git a/cligen_parse.y b/cligen_parse.y index 2137670..2d235f2 100644 --- a/cligen_parse.y +++ b/cligen_parse.y @@ -126,7 +126,7 @@ cligen_parse_debug(int d) * @param[in] cy CLIgen yacc parse struct */ void cligen_parseerror(void *_cy, - char *s) + const char *s) { cligen_yacc *cy = (cligen_yacc *)_cy; @@ -147,7 +147,7 @@ void cligen_parseerror(void *_cy, */ static cg_var * create_cv(cligen_yacc *cy, - char *type, + const char *type, char *str) { cg_var *cv = NULL; @@ -184,16 +184,16 @@ cgy_flag(cligen_yacc *cy, int retval = -1; if (debug) - fprintf(stderr, "%s: %s 1\n", __FUNCTION__, var); + fprintf(stderr, "%s: %s 1\n", __func__, var); if (cs){ /* XXX: why cs? */ if (cy->cy_cvec == NULL){ if ((cy->cy_cvec = cvec_new(0)) == NULL){ - fprintf(stderr, "%s: cvec_new:%s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: cvec_new:%s\n", __func__, strerror(errno)); goto done; } } if ((cv = cvec_add(cy->cy_cvec, CGV_INT32)) == NULL){ - fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: realloc:%s\n", __func__, strerror(errno)); goto done; } cv_name_set(cv, var); @@ -260,7 +260,7 @@ cgy_treename(cligen_yacc *cy, if (cy->cy_treename) free(cy->cy_treename); if ((cy->cy_treename = strdup(name)) == NULL){ - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: strdup: %s\n", __func__, strerror(errno)); goto done; } retval = 0; @@ -281,7 +281,7 @@ cgy_assignment(cligen_yacc *cy, struct cgy_stack *cs = cy->cy_stack; int retval = -1; cg_var *cv; - char *treename_keyword; + const char *treename_keyword; cligen_handle h = cy->cy_handle; if (cs == NULL){ @@ -289,16 +289,16 @@ cgy_assignment(cligen_yacc *cy, goto done; } if (debug) - fprintf(stderr, "%s: %s=%s\n", __FUNCTION__, var, val); + fprintf(stderr, "%s: %s=%s\n", __func__, var, val); if (cs->cs_next != NULL){ /* local */ if (cy->cy_cvec == NULL) if ((cy->cy_cvec = cvec_new(0)) == NULL){ - fprintf(stderr, "%s: cvec_new:%s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: cvec_new:%s\n", __func__, strerror(errno)); goto done; } if ((cv = cvec_add(cy->cy_cvec, CGV_STRING)) == NULL){ - fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: realloc:%s\n", __func__, strerror(errno)); goto done; } cv_name_set(cv, var); @@ -314,7 +314,7 @@ cgy_assignment(cligen_yacc *cy, else { if ((cv = cvec_find(cy->cy_globals, var)) == NULL){ if ((cv = cvec_add(cy->cy_globals, CGV_STRING)) == NULL){ - fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: realloc:%s\n", __func__, strerror(errno)); goto done; } cv_name_set(cv, var); @@ -339,14 +339,14 @@ cgy_callback(cligen_yacc *cy, cg_callback *cc, **ccp; if (debug) - fprintf(stderr, "%s: %s\n", __FUNCTION__, cb_str); + fprintf(stderr, "%s: %s\n", __func__, cb_str); if (cs == NULL) return 0; ccp = &cy->cy_callbacks; while (*ccp != NULL) ccp = &((*ccp)->cc_next); if ((cc = malloc(sizeof(*cc))) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); cligen_parseerror1(cy, "Allocating cligen callback"); return -1; } @@ -458,9 +458,9 @@ cgy_list_push(cg_obj *co, struct cgy_list *cl; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); if ((cl = malloc(sizeof(*cl))) == NULL) { - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); return -1; } cl->cl_next = *cl0; @@ -502,7 +502,7 @@ cgy_var_create(cligen_yacc *cy) /* Create unassigned variable object */ if ((co = cov_new(CGV_ERR, NULL)) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); cligen_parseerror1(cy, "Allocating cligen object"); return NULL; } @@ -510,7 +510,7 @@ cgy_var_create(cligen_yacc *cy) co_flags_set(co, CO_FLAGS_OPTION); } if (debug) - fprintf(stderr, "%s: pre\n", __FUNCTION__); + fprintf(stderr, "%s: pre\n", __func__); return co; } @@ -527,7 +527,7 @@ cgy_var_name_type(cligen_yacc *cy, cy->cy_var->co_command = name; if ((cy->cy_var->co_vtype = cv_str2type(type)) == CGV_ERR){ cligen_parseerror1(cy, "Invalid type"); - fprintf(stderr, "%s: Invalid type: %s\n", __FUNCTION__, type); + fprintf(stderr, "%s: Invalid type: %s\n", __func__, type); return -1; } return 0; @@ -555,7 +555,7 @@ cgy_var_post(cligen_yacc *cy) coy->co_vtype = cv_str2type(coy->co_command); #endif if (debug) - fprintf(stderr, "%s: cmd:%s vtype:%d\n", __FUNCTION__, + fprintf(stderr, "%s: cmd:%s vtype:%d\n", __func__, coy->co_command, coy->co_vtype ); if (coy->co_vtype == CGV_ERR){ @@ -611,7 +611,7 @@ cgy_cmd(cligen_yacc *cy, cop = cl->cl_obj; if (debug) fprintf(stderr, "%s: %s parent:%s\n", - __FUNCTION__, cmd, cop->co_command); + __func__, cmd, cop->co_command); if ((conew = co_new(cmd, cop)) == NULL) { cligen_parseerror1(cy, "Allocating cligen object"); return -1; @@ -739,7 +739,7 @@ cgy_var_choice_append(cligen_yacc *cy, len = strlen(str)+strlen(app) + 2; if ((s = realloc(str, len)) == NULL) { - fprintf(stderr, "%s: realloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: realloc: %s\n", __func__, strerror(errno)); return NULL; } strncat(s, "|", len-1); @@ -789,7 +789,7 @@ cgy_terminal(cligen_yacc *cy) if (co->co_cvec) cvec_free(co->co_cvec); if ((co->co_cvec = cvec_dup(cy->cy_cvec)) == NULL){ /* this leaks */ - fprintf(stderr, "%s: cvec_dup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: cvec_dup: %s\n", __func__, strerror(errno)); goto done; } } @@ -843,10 +843,10 @@ ctx_push(cligen_yacc *cy, cg_obj *co; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); /* Create new stack element */ if ((cs = malloc(sizeof(*cs))) == NULL) { - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); return -1; } memset(cs, 0, sizeof(*cs)); @@ -879,7 +879,7 @@ ctx_peek_swap(cligen_yacc *cy) cg_obj *co; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); if ((cs = cy->cy_stack) == NULL){ #if 1 cligen_parseerror1(cy, "No surrounding () or []"); @@ -918,7 +918,7 @@ ctx_peek_swap2(cligen_yacc *cy) cg_obj *co; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); if ((cs = cy->cy_stack) == NULL){ #if 1 cligen_parseerror1(cy, "No surrounding () or []"); @@ -962,9 +962,9 @@ ctx_pop_add(cligen_yacc *cy) cg_obj *co; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); if ((cs = cy->cy_stack) == NULL){ - fprintf(stderr, "%s: cgy_stack empty\n", __FUNCTION__); + fprintf(stderr, "%s: cgy_stack empty\n", __func__); return -1; /* shouldnt happen */ } cy->cy_stack = cs->cs_next; @@ -997,9 +997,9 @@ ctx_pop(cligen_yacc *cy) cg_obj *co; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); if ((cs = cy->cy_stack) == NULL){ - fprintf(stderr, "%s: cgy_stack empty\n", __FUNCTION__); + fprintf(stderr, "%s: cgy_stack empty\n", __func__); return -1; /* shouldnt happen */ } cy->cy_stack = cs->cs_next; @@ -1094,7 +1094,7 @@ cg_range_create(cligen_yacc *cy, if ((yv->co_rangecvv_low = cvec_from_var(cv1)) == NULL) goto done; } - else if (cvec_append_var(yv->co_rangecvv_low, cv1) < 0) + else if (cvec_append_var(yv->co_rangecvv_low, cv1) == NULL) goto done; /* Then create upper bound cv */ if ((cv2 = cv_new(cvtype)) == NULL){ @@ -1122,7 +1122,7 @@ cg_range_create(cligen_yacc *cy, if ((yv->co_rangecvv_upp = cvec_from_var(cv2)) == NULL) goto done; } - else if (cvec_append_var(yv->co_rangecvv_upp, cv2) < 0) + else if (cvec_append_var(yv->co_rangecvv_upp, cv2) == NULL) goto done; /* Then increment range vector length */ yv->co_rangelen++; @@ -1212,7 +1212,7 @@ cgy_init(cligen_yacc *cy, cg_obj *co_top) { if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); /* Add top-level object */ if (cgy_list_push(co_top, &cy->cy_list) < 0) return -1; @@ -1230,7 +1230,7 @@ cgy_exit(cligen_yacc *cy) struct cgy_stack *cs; if (debug) - fprintf(stderr, "%s\n", __FUNCTION__); + fprintf(stderr, "%s\n", __func__); cy->cy_var = NULL; cgy_list_delete(&cy->cy_list); diff --git a/cligen_parsetree.c b/cligen_parsetree.c index f614586..7d8abbd 100644 --- a/cligen_parsetree.c +++ b/cligen_parsetree.c @@ -363,7 +363,7 @@ pt_copy(parse_tree *pt, } if (pt_len_get(ptn) && (ptn->pt_vec = (cg_obj **)malloc(pt_len_get(ptn)*sizeof(cg_obj *))) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); goto done; } j=0; diff --git a/cligen_print.c b/cligen_print.c index 79d1b1e..4487493 100644 --- a/cligen_print.c +++ b/cligen_print.c @@ -361,7 +361,7 @@ pt_dump1(FILE *f, int i; cg_obj *co; - cligen_output(f, "%*s %p pt", indent*3, "", pt); + cligen_output(f, "%*s %p pt", indent*3, "", (void*)pt); cligen_output(f, " [%d]", pt_len_get(pt)); cligen_output(f, "\n"); for (i=0; ico_type){ case CO_COMMAND: - cligen_output(f, "%*s %p co %s", indent*3, "", co, co->co_command); + cligen_output(f, "%*s %p co %s", indent*3, "", (void*)co, co->co_command); if (co_sets_get(co)) cligen_output(f, " SETS"); if (co->co_ref) - cligen_output(f, " ref:%p", co->co_ref); + cligen_output(f, " ref:%p", (void*)co->co_ref); break; case CO_REFERENCE: - cligen_output(f, "%*s %p co @%s", indent*3, "", co, co->co_command); + cligen_output(f, "%*s %p co @%s", indent*3, "", (void*)co, co->co_command); break; case CO_VARIABLE: - cligen_output(f, "%*s %p co <%s> ", indent*3, "", co, co->co_command); + cligen_output(f, "%*s %p co <%s> ", indent*3, "", (void*)co, co->co_command); if (co->co_ref) - cligen_output(f, " ref:%p", co->co_ref); + cligen_output(f, " ref:%p", (void*)co->co_ref); if (co->co_treeref_orig) - cligen_output(f, " treeref:%p", co->co_treeref_orig); + cligen_output(f, " treeref:%p", (void*)co->co_treeref_orig); break; case CO_EMPTY: - cligen_output(f, "%*s %p empty", indent*3, "", co); + cligen_output(f, "%*s %p empty", indent*3, "", (void*)co); break; } if (co->co_flags & CO_FLAGS_TOPOFTREE) diff --git a/cligen_read.c b/cligen_read.c index f19a138..1c3888f 100644 --- a/cligen_read.c +++ b/cligen_read.c @@ -321,7 +321,7 @@ show_help_columns(cligen_handle h, if (mr_pt_len_get(mr) > 0){ /* min, max only defined if matchlen > 0 */ /* Go through match vector and collect commands and helps */ if ((chvec = calloc(mr_pt_len_get(mr), sizeof(struct cligen_help))) ==NULL){ - fprintf(stderr, "%s calloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s calloc: %s\n", __func__, strerror(errno)); goto done; } nrcmd = 0; @@ -348,7 +348,7 @@ show_help_columns(cligen_handle h, continue; ch = &chvec[nrcmd]; if ((ch->ch_cmd = strdup(cmd)) == NULL){ - fprintf(stderr, "%s strdup: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s strdup: %s\n", __func__, strerror(errno)); goto done; } if (co->co_helpstring && cligen_txt2cvv(co->co_helpstring, &ch->ch_helpvec) < 0) @@ -529,12 +529,12 @@ cli_complete(cligen_handle h, string = cligen_buf(h); if (string == NULL){ - fprintf(stderr, "%s Input string NULL\n", __FUNCTION__); + fprintf(stderr, "%s Input string NULL\n", __func__); goto done; } slen = cligen_buf_size(h); if ((s = malloc(slen)) == NULL){ /* s is a temporary copy */ - fprintf(stderr, "%s malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s malloc: %s\n", __func__, strerror(errno)); goto done; } strncpy(s, string, slen); @@ -661,7 +661,7 @@ cliread_parse(cligen_handle h, goto done; } if (cligen_logsyntax(h) > 0){ - fprintf(stderr, "%s:\n", __FUNCTION__); + fprintf(stderr, "%s:\n", __func__); pt_print1(stderr, pt, 0); } cli_trim(&string, cligen_comment(h)); @@ -888,7 +888,7 @@ cligen_eval_pipe_pre(cligen_handle h, signal_unblock(SIGINT); if ((retval = (*fn)(cligen_userhandle(h)?cligen_userhandle(h):h, cvv, cc->cc_cvec)) != 0) - fprintf(stderr, "%s child retval:%d\n", __FUNCTION__, retval); + fprintf(stderr, "%s child retval:%d\n", __func__, retval); exit(retval); } /* parent */ @@ -1048,7 +1048,7 @@ cligen_eval(cligen_handle h, cligen_fn_str_set(h, cc->cc_fn_str); /* Eval wrapper function so upper layers can make checks before and after callback */ if (wrapfn) - (*wrapfn)(wraparg, &wh, cc->cc_fn_str, __FUNCTION__); + (*wrapfn)(wraparg, &wh, cc->cc_fn_str, __func__); if ((*fn)(cligen_userhandle(h)?cligen_userhandle(h):h, cvv1, argv) < 0){ @@ -1058,7 +1058,7 @@ cligen_eval(cligen_handle h, goto done; } if (wrapfn && wh != NULL) - (*wrapfn)(wraparg, &wh, cc->cc_fn_str, __FUNCTION__); + (*wrapfn)(wraparg, &wh, cc->cc_fn_str, __func__); if (argv != NULL) cvec_free(argv); cligen_fn_str_set(h, NULL); diff --git a/cligen_syntax.c b/cligen_syntax.c index 4ced2c9..7b0e441 100644 --- a/cligen_syntax.c +++ b/cligen_syntax.c @@ -71,8 +71,8 @@ */ int clispec_parse_str(cligen_handle h, - char *str, - char *name, + const char *str, + const char *name, char *treename, parse_tree *ptp, cvec *cvv) @@ -111,7 +111,7 @@ clispec_parse_str(cligen_handle h, cy.cy_globals = cvv; else if ((cy.cy_globals = cvec_new(0)) == NULL){ - fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno)); goto done; } if (strlen(str)){ /* Not empty */ @@ -176,7 +176,7 @@ clispec_parse_str(cligen_handle h, int clispec_parse_file(cligen_handle h, FILE *f, - char *name, + const char *name, char *treename, parse_tree *pt, cvec *cvv) @@ -200,7 +200,7 @@ clispec_parse_file(cligen_handle h, break; if (i == len-1){ if ((buf = realloc(buf, 2*len)) == NULL){ - fprintf(stderr, "%s: realloc: %s\n", __FUNCTION__, strerror(errno)); + fprintf(stderr, "%s: realloc: %s\n", __func__, strerror(errno)); goto done; } memset(buf+len, 0, len); @@ -263,7 +263,7 @@ cligen_callbackv_str2fn(parse_tree *pt, co_callback_fn_set(cc, str2fn(cc->cc_fn_str, arg, &callback_err)); if (callback_err != NULL){ fprintf(stderr, "%s: error: No such function: %s (%s)\n", - __FUNCTION__, cc->cc_fn_str, callback_err); + __func__, cc->cc_fn_str, callback_err); goto done; } } @@ -317,7 +317,7 @@ cligen_expandv_str2fn(parse_tree *pt, co->co_expandv_fn = str2fn(co->co_expand_fn_str, arg, &callback_err); if (callback_err != NULL){ fprintf(stderr, "%s: error: No such function: %s\n", - __FUNCTION__, co->co_expand_fn_str); + __func__, co->co_expand_fn_str); goto done; } } @@ -356,7 +356,7 @@ cligen_translate_str2fn(parse_tree *pt, co->co_translate_fn = str2fn(co->co_translate_fn_str, arg, &callback_err); if (callback_err != NULL){ fprintf(stderr, "%s: error: No such function: %s\n", - __FUNCTION__, co->co_translate_fn_str); + __func__, co->co_translate_fn_str); goto done; } } diff --git a/cligen_syntax.h b/cligen_syntax.h index 9f8a261..b14f928 100644 --- a/cligen_syntax.h +++ b/cligen_syntax.h @@ -60,15 +60,15 @@ typedef translate_cb_t *(translate_str2fn_t)(char *str, void *arg, char **err); */ int clispec_parse_str(cligen_handle h, - char *str, - char *name, + const char *str, + const char *name, char *treename, parse_tree *pt, cvec *globals); int clispec_parse_file(cligen_handle h, FILE *f, - char *name, + const char *name, char *treename, parse_tree *obsolete, cvec *globals); diff --git a/configure b/configure index 73cf9c2..230418b 100755 --- a/configure +++ b/configure @@ -2490,10 +2490,10 @@ fi { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: debug is $ac_enable_debug" >&5 printf "%s\n" "debug is $ac_enable_debug" >&6; } if test "$ac_enable_debug" = "yes"; then - : ${CFLAGS="-g -Wall"} + : ${CFLAGS="-g -Wall -Wwrite-strings -Wpedantic"} INSTALLFLAGS="" else - : ${CFLAGS="-O2 -Wall"} + : ${CFLAGS="-O2 -Wall -Wwrite-strings -Wpedantic"} fi ac_config_headers="$ac_config_headers cligen_config.h" diff --git a/configure.ac b/configure.ac index ce89fc5..27bb6f5 100644 --- a/configure.ac +++ b/configure.ac @@ -56,10 +56,10 @@ AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug],[Build with debug symbols, AC_MSG_RESULT(debug is $ac_enable_debug) if test "$ac_enable_debug" = "yes"; then - : ${CFLAGS="-g -Wall"} + : ${CFLAGS="-g -Wall -Wwrite-strings -Wpedantic"} INSTALLFLAGS="" else - : ${CFLAGS="-O2 -Wall"} + : ${CFLAGS="-O2 -Wall -Wwrite-strings -Wpedantic"} fi AC_CONFIG_HEADERS([cligen_config.h])