Fossil SCM
Modify linenoise.c so that it can build with -std=c89. Baseline linenoise.c relied on snprintf(), strdup(), and strcasecmp() which are technically not in C89. Since linenoise.c's only purpose (at present) is to implement the SQLite shell, make use of the SQLite compatibility functions. Following this change, "fossil clean -f && CFLAGS='-std=c89 -Wall' ./configure && make" builds without warnings or errors on gcc 4.4.7 20120313 (Red Hat 4.4.7-3) on CentOS release 6.4 (Final). Unrelated note: discovered during testing that "CFLAGS=-Werror ./configure" fails to add -lm to link step due to conflicting types for built-in function "sin" being regarded as an error.
Commit
8133501ecbf7810ad5da386db3b1f2a642de9c66
Parent
e826eadd9cf7d3a…
1 file changed
+34
+34
| --- src/linenoise.c | ||
| +++ src/linenoise.c | ||
| @@ -105,10 +105,11 @@ | ||
| 105 | 105 | * |
| 106 | 106 | */ |
| 107 | 107 | |
| 108 | 108 | #include <termios.h> |
| 109 | 109 | #include <unistd.h> |
| 110 | +#include <stdarg.h> | |
| 110 | 111 | #include <stdlib.h> |
| 111 | 112 | #include <stdio.h> |
| 112 | 113 | #include <errno.h> |
| 113 | 114 | #include <string.h> |
| 114 | 115 | #include <stdlib.h> |
| @@ -115,10 +116,11 @@ | ||
| 115 | 116 | #include <ctype.h> |
| 116 | 117 | #include <sys/types.h> |
| 117 | 118 | #include <sys/ioctl.h> |
| 118 | 119 | #include <unistd.h> |
| 119 | 120 | #include "linenoise.h" |
| 121 | +#include "sqlite3.h" | |
| 120 | 122 | |
| 121 | 123 | #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 |
| 122 | 124 | #define LINENOISE_MAX_LINE 4096 |
| 123 | 125 | static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; |
| 124 | 126 | static linenoiseCompletionCallback *completionCallback = NULL; |
| @@ -191,10 +193,42 @@ | ||
| 191 | 193 | fflush(lndebug_fp); \ |
| 192 | 194 | } while (0) |
| 193 | 195 | #else |
| 194 | 196 | #define lndebug(fmt, arg1) |
| 195 | 197 | #endif |
| 198 | + | |
| 199 | +/* =========================== C89 compatibility ============================ */ | |
| 200 | + | |
| 201 | +/* snprintf() is not C89, but sqlite3_vsnprintf() can be adapted. */ | |
| 202 | +static int linenoiseSnprintf(char *str, size_t size, const char *format, ...) { | |
| 203 | + va_list ap; | |
| 204 | + int result; | |
| 205 | + | |
| 206 | + va_start(ap,format); | |
| 207 | + result = strlen(sqlite3_vsnprintf((int)size,str,format,ap)); | |
| 208 | + va_end(ap); | |
| 209 | + | |
| 210 | + return result; | |
| 211 | +} | |
| 212 | +#undef snprintf | |
| 213 | +#define snprintf linenoiseSnprintf | |
| 214 | + | |
| 215 | +/* strdup() is technically not standard C89 despite being in POSIX. */ | |
| 216 | +static char *linenoiseStrdup(const char *s) { | |
| 217 | + int size = strlen(s)+1; | |
| 218 | + char *result = malloc(size); | |
| 219 | + | |
| 220 | + if (result) memcpy(result,s,size); | |
| 221 | + | |
| 222 | + return result; | |
| 223 | +} | |
| 224 | +#undef strdup | |
| 225 | +#define strdup linenoiseStrdup | |
| 226 | + | |
| 227 | +/* strcasecmp() is not standard C89. SQLite offers a direct replacement. */ | |
| 228 | +#undef strcasecmp | |
| 229 | +#define strcasecmp sqlite3_stricmp | |
| 196 | 230 | |
| 197 | 231 | /* ======================= Low level terminal handling ====================== */ |
| 198 | 232 | |
| 199 | 233 | /* Set if to use or not the multi line mode. */ |
| 200 | 234 | void linenoiseSetMultiLine(int ml) { |
| 201 | 235 |
| --- src/linenoise.c | |
| +++ src/linenoise.c | |
| @@ -105,10 +105,11 @@ | |
| 105 | * |
| 106 | */ |
| 107 | |
| 108 | #include <termios.h> |
| 109 | #include <unistd.h> |
| 110 | #include <stdlib.h> |
| 111 | #include <stdio.h> |
| 112 | #include <errno.h> |
| 113 | #include <string.h> |
| 114 | #include <stdlib.h> |
| @@ -115,10 +116,11 @@ | |
| 115 | #include <ctype.h> |
| 116 | #include <sys/types.h> |
| 117 | #include <sys/ioctl.h> |
| 118 | #include <unistd.h> |
| 119 | #include "linenoise.h" |
| 120 | |
| 121 | #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 |
| 122 | #define LINENOISE_MAX_LINE 4096 |
| 123 | static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; |
| 124 | static linenoiseCompletionCallback *completionCallback = NULL; |
| @@ -191,10 +193,42 @@ | |
| 191 | fflush(lndebug_fp); \ |
| 192 | } while (0) |
| 193 | #else |
| 194 | #define lndebug(fmt, arg1) |
| 195 | #endif |
| 196 | |
| 197 | /* ======================= Low level terminal handling ====================== */ |
| 198 | |
| 199 | /* Set if to use or not the multi line mode. */ |
| 200 | void linenoiseSetMultiLine(int ml) { |
| 201 |
| --- src/linenoise.c | |
| +++ src/linenoise.c | |
| @@ -105,10 +105,11 @@ | |
| 105 | * |
| 106 | */ |
| 107 | |
| 108 | #include <termios.h> |
| 109 | #include <unistd.h> |
| 110 | #include <stdarg.h> |
| 111 | #include <stdlib.h> |
| 112 | #include <stdio.h> |
| 113 | #include <errno.h> |
| 114 | #include <string.h> |
| 115 | #include <stdlib.h> |
| @@ -115,10 +116,11 @@ | |
| 116 | #include <ctype.h> |
| 117 | #include <sys/types.h> |
| 118 | #include <sys/ioctl.h> |
| 119 | #include <unistd.h> |
| 120 | #include "linenoise.h" |
| 121 | #include "sqlite3.h" |
| 122 | |
| 123 | #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 |
| 124 | #define LINENOISE_MAX_LINE 4096 |
| 125 | static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; |
| 126 | static linenoiseCompletionCallback *completionCallback = NULL; |
| @@ -191,10 +193,42 @@ | |
| 193 | fflush(lndebug_fp); \ |
| 194 | } while (0) |
| 195 | #else |
| 196 | #define lndebug(fmt, arg1) |
| 197 | #endif |
| 198 | |
| 199 | /* =========================== C89 compatibility ============================ */ |
| 200 | |
| 201 | /* snprintf() is not C89, but sqlite3_vsnprintf() can be adapted. */ |
| 202 | static int linenoiseSnprintf(char *str, size_t size, const char *format, ...) { |
| 203 | va_list ap; |
| 204 | int result; |
| 205 | |
| 206 | va_start(ap,format); |
| 207 | result = strlen(sqlite3_vsnprintf((int)size,str,format,ap)); |
| 208 | va_end(ap); |
| 209 | |
| 210 | return result; |
| 211 | } |
| 212 | #undef snprintf |
| 213 | #define snprintf linenoiseSnprintf |
| 214 | |
| 215 | /* strdup() is technically not standard C89 despite being in POSIX. */ |
| 216 | static char *linenoiseStrdup(const char *s) { |
| 217 | int size = strlen(s)+1; |
| 218 | char *result = malloc(size); |
| 219 | |
| 220 | if (result) memcpy(result,s,size); |
| 221 | |
| 222 | return result; |
| 223 | } |
| 224 | #undef strdup |
| 225 | #define strdup linenoiseStrdup |
| 226 | |
| 227 | /* strcasecmp() is not standard C89. SQLite offers a direct replacement. */ |
| 228 | #undef strcasecmp |
| 229 | #define strcasecmp sqlite3_stricmp |
| 230 | |
| 231 | /* ======================= Low level terminal handling ====================== */ |
| 232 | |
| 233 | /* Set if to use or not the multi line mode. */ |
| 234 | void linenoiseSetMultiLine(int ml) { |
| 235 |