summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-05-18 06:24:22 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-05-18 06:24:22 -0700
commitb13a869dddb06218151f00a0abec077fff9cdd4a (patch)
tree36bc99e1331499e91bed8a9a4a9898b896c95c06
parenta7916fbac400497b6ac35da8a689a3c2cb68141d (diff)
downloadtxr-b13a869dddb06218151f00a0abec077fff9cdd4a.tar.gz
txr-b13a869dddb06218151f00a0abec077fff9cdd4a.tar.bz2
txr-b13a869dddb06218151f00a0abec077fff9cdd4a.zip
listener: Cygwin fix.
We cannot pass raw C wide literals to static_str; this breaks on platforms where wchar_t is two bytes and strings are aligned to only two byte boundaries. That's why TXR has the wli() macro. We don't want to introduce the wli() macro into linenoise, so the two choices are: duplicate the incoming mode strings into dynamic storage with string(), or pass some enum to specify file mode and locally convert to static mode string. Let's go with the latter. * linenoise.c (edit_in_editor, lino_hist_save): Use enum constant for file mode instead of mode string. * linenoise.h (enum lino_file_mode, line_file_mode_t): New enum. (struct lino_os): File opening functions use lino_file_mode_t instead of mode string. * parser.c (lino_mode_str): New static array. (lino_open, lino_open8, lino_fdopen): Take enum mode instead of string. Convert to literal through lino_mode_str table, and pass that literal to static_str.
-rw-r--r--linenoise/linenoise.c10
-rw-r--r--linenoise/linenoise.h11
-rw-r--r--parser.c16
3 files changed, 23 insertions, 14 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index 005cdb1d..1cdb1997 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -1825,10 +1825,10 @@ static void edit_in_editor(lino_t *l) {
#if HAVE_MKSTEMPS
if ((fd = mkstemps(path, strlen(suffix))) != -1)
- fo = lino_os.fdopen_fn(fd, L"w");
+ fo = lino_os.fdopen_fn(fd, lino_overwrite);
#else
if ((fd = mkstemp(path)) != -1)
- fo = lino_os.fdopen_fn(fd, L"w");
+ fo = lino_os.fdopen_fn(fd, lino_overwrite);
#endif
if (!fo && fd != -1)
@@ -1846,7 +1846,7 @@ static void edit_in_editor(lino_t *l) {
lino_os.close_fn(fo);
fo = 0;
- if (system(cmd) == 0 && (fi = lino_os.open8_fn(path, L"r")) != 0) {
+ if (system(cmd) == 0 && (fi = lino_os.open8_fn(path, lino_read)) != 0) {
size_t len;
(void) lino_os.gets_fn(fi, l->data, nelem(l->data));
lino_os.close_fn(fi);
@@ -2619,7 +2619,7 @@ int lino_hist_set_max_len(lino_t *ls, int len) {
/* Save the history in the specified file. On success 0 is returned
* otherwise -1 is returned. */
int lino_hist_save(lino_t *ls, const wchar_t *filename) {
- mem_t *fp = lino_os.open_fn(filename, L"w");
+ mem_t *fp = lino_os.open_fn(filename, lino_overwrite);
int j;
if (fp == NULL) {
@@ -2642,7 +2642,7 @@ int lino_hist_save(lino_t *ls, const wchar_t *filename) {
* If the file exists and the operation succeeded 0 is returned, otherwise
* on error -1 is returned. */
int lino_hist_load(lino_t *ls, const wchar_t *filename) {
- mem_t *fp = lino_os.open_fn(filename, L"r");
+ mem_t *fp = lino_os.open_fn(filename, lino_read);
wchar_t buf[LINENOISE_MAX_LINE];
if (fp == NULL) {
diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h
index 8cc0a234..3c887a33 100644
--- a/linenoise/linenoise.h
+++ b/linenoise/linenoise.h
@@ -54,6 +54,11 @@ typedef unsigned char mem_t;
#define MEM_T_DEFINED
#endif
+typedef enum lino_file_mode {
+ lino_read,
+ lino_overwrite,
+} lino_file_mode_t;
+
typedef struct lino_os {
mem_t *(*alloc_fn)(size_t n);
mem_t *(*realloc_fn)(mem_t *old, size_t size);
@@ -67,9 +72,9 @@ typedef struct lino_os {
wchar_t *(*getl_fn)(mem_t *stream, wchar_t *buf, size_t nchar);
wchar_t *(*gets_fn)(mem_t *stream, wchar_t *buf, size_t nchar);
int (*eof_fn)(mem_t *stream);
- mem_t *(*open_fn)(const wchar_t *name, const wchar_t *mode);
- mem_t *(*open8_fn)(const char *name, const wchar_t *mode);
- mem_t *(*fdopen_fn)(int fd, const wchar_t *mode);
+ mem_t *(*open_fn)(const wchar_t *name, lino_file_mode_t mode);
+ mem_t *(*open8_fn)(const char *name, lino_file_mode_t mode);
+ mem_t *(*fdopen_fn)(int fd, lino_file_mode_t mode);
void (*close_fn)(mem_t *stream);
int (*wide_display_fn)(wchar_t);
} lino_os_t;
diff --git a/parser.c b/parser.c
index 575ea1e3..ddd72fd1 100644
--- a/parser.c
+++ b/parser.c
@@ -1489,23 +1489,27 @@ static int lino_feof(mem_t *stream_in)
return get_error(stream) == t;
}
-static mem_t *lino_open(const wchar_t *name_in, const wchar_t *mode_in)
+static const wchli_t *lino_mode_str[] = {
+ wli("r"), wli("w")
+};
+
+static mem_t *lino_open(const wchar_t *name_in, lino_file_mode_t mode_in)
{
val name = string(name_in);
- val mode = static_str(coerce(const wchli_t *, mode_in));
+ val mode = static_str(lino_mode_str[mode_in]);
return coerce(mem_t *, open_file(name, mode));
}
-static mem_t *lino_open8(const char *name_in, const wchar_t *mode_in)
+static mem_t *lino_open8(const char *name_in, lino_file_mode_t mode_in)
{
val name = string_utf8(name_in);
- val mode = static_str(coerce(const wchli_t *, mode_in));
+ val mode = static_str(lino_mode_str[mode_in]);
return coerce(mem_t *, open_file(name, mode));
}
-static mem_t *lino_fdopen(int fd, const wchar_t *mode_in)
+static mem_t *lino_fdopen(int fd, lino_file_mode_t mode_in)
{
- val mode = static_str(coerce(const wchli_t *, mode_in));
+ val mode = static_str(lino_mode_str[mode_in]);
return coerce(mem_t *, open_fileno(num(fd), mode));
}