From a75cec9dba4ed341aeef6c5071eb04e3c0ce529a Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Fri, 18 Mar 2011 03:47:51 -0400 Subject: [PATCH] Added some casts to make it easier to include in a c++ project --- Makefile | 3 +++ linenoise.c | 18 +++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 7e50716..4c218d4 100644 --- a/Makefile +++ b/Makefile @@ -6,5 +6,8 @@ linenoise_example: linenoise.h linenoise.c example.c linenoise_utf8_example: linenoise.c utf8.c example.c $(CC) -DNO_COMPLETION -DUSE_UTF8 -Wall -W -Os -g -o $@ linenoise.c utf8.c example.c +test_cpp_compile: linenoise.h linenoise.c + g++ -Wall -W -Os -g -c -o linenoise.o linenoise.c + clean: rm -f linenoise_example linenoise_utf8_example diff --git a/linenoise.c b/linenoise.c index 63a7512..b4fb56f 100644 --- a/linenoise.c +++ b/linenoise.c @@ -115,7 +115,7 @@ #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 -static char *unsupported_term[] = {"dumb","cons25",NULL}; +static const char *unsupported_term[] = {"dumb","cons25",NULL}; static struct termios orig_termios; /* in order to restore at exit */ static int rawmode = 0; /* for atexit() function to check if restore is needed*/ @@ -208,7 +208,7 @@ struct current { }; /* gcc/glibc insists that we care about the return code of write! */ -#define IGNORE_RC(EXPR) ((EXPR) < 0 ? -1 : 0) +#define IGNORE_RC(EXPR) if (EXPR) {} /* This is fd_printf() on some systems, but use a different * name to avoid conflicts @@ -504,7 +504,7 @@ void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) { } void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) { - lc->cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1)); + lc->cvec = (char **)realloc(lc->cvec,sizeof(char*)*(lc->len+1)); lc->cvec[lc->len++] = strdup(str); } @@ -1046,7 +1046,7 @@ int linenoiseHistoryAdd(const char *line) { if (history_max_len == 0) return 0; if (history == NULL) { - history = malloc(sizeof(char*)*history_max_len); + history = (char**)malloc(sizeof(char*)*history_max_len); if (history == NULL) return 0; memset(history,0,(sizeof(char*)*history_max_len)); } @@ -1063,18 +1063,18 @@ int linenoiseHistoryAdd(const char *line) { } int linenoiseHistorySetMaxLen(int len) { - char **new; + char **newHistory; if (len < 1) return 0; if (history) { int tocopy = history_len; - new = malloc(sizeof(char*)*len); - if (new == NULL) return 0; + newHistory = (char**)malloc(sizeof(char*)*len); + if (newHistory == NULL) return 0; if (len < tocopy) tocopy = len; - memcpy(new,history+(history_max_len-tocopy), sizeof(char*)*tocopy); + memcpy(newHistory,history+(history_max_len-tocopy), sizeof(char*)*tocopy); free(history); - history = new; + history = newHistory; } history_max_len = len; if (history_len > history_max_len) -- 2.44.0