| 1 |
#define _POSIX_C_SOURCE 200809L |
| 2 |
#include <errno.h> |
| 3 |
#include <fcntl.h> |
| 4 |
#include <histedit.h> |
| 5 |
#include <locale.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <wchar.h> |
| 11 |
|
| 12 |
/* This function works exactly like fmemopen(), except that the memory stream |
| 13 |
* has an underlying file descriptor and hence can be used with libedit. |
| 14 |
* The mode argument is included for symmetry, but we really don't need it; |
| 15 |
* we always call it specified as "r" in this simple demo program. |
| 16 |
* As always, we return NULL with errno set on failure. */ |
| 17 |
static FILE *fmemopen_with_fd(const void *restrict bytes, size_t len, const char mode[restrict static 1]) { |
| 18 |
if(mode[0] != 'r' || mode[1]) { |
| 19 |
errno = EINVAL; |
| 20 |
return NULL; |
| 21 |
} |
| 22 |
|
| 23 |
int pip[2]; |
| 24 |
if(pipe(pip) == -1) { |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
/* We are not going to read from the pipe until we've completely written to it. |
| 29 |
* Because of this, we avoid a deadlock if the pipe buffer gets full. */ |
| 30 |
if(fcntl(pip[1], F_SETFD, O_NONBLOCK) == -1) { |
| 31 |
closebothends: |
| 32 |
const int esave = errno; |
| 33 |
close(pip[1]); |
| 34 |
close(pip[0]); |
| 35 |
errno = esave; |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
ssize_t k; |
| 40 |
size_t bytes_written = 0; |
| 41 |
do { |
| 42 |
k = write(pip[1], bytes_written + (char*)bytes, len - bytes_written); |
| 43 |
if(k == -1) { |
| 44 |
break; |
| 45 |
} else { |
| 46 |
bytes_written += k; |
| 47 |
} |
| 48 |
} while(bytes_written < len); |
| 49 |
if(k == -1) { |
| 50 |
if(errno == EAGAIN) { |
| 51 |
errno = ERANGE; |
| 52 |
} |
| 53 |
goto closebothends; |
| 54 |
} |
| 55 |
|
| 56 |
if(close(pip[1]) == -1) { |
| 57 |
const int esave = errno; |
| 58 |
close(pip[0]); |
| 59 |
errno = esave; |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
FILE *const stream = fdopen(pip[0], "r"); |
| 64 |
if(!stream) { |
| 65 |
const int esave = errno; |
| 66 |
close(pip[0]); |
| 67 |
errno = esave; |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
return stream; |
| 71 |
} |
| 72 |
|
| 73 |
int main(void) { |
| 74 |
if(!setlocale(LC_ALL, "")) { |
| 75 |
fputs("Failed to enable default locale\n", stderr); |
| 76 |
exit(EXIT_FAILURE); |
| 77 |
} |
| 78 |
|
| 79 |
const char *const mbstest = "Test string"; |
| 80 |
wchar_t *const wcstest = L"Test string"; |
| 81 |
FILE *const instream = fmemopen_with_fd(mbstest, strlen(mbstest), "r"); |
| 82 |
if(!instream) { |
| 83 |
perror("Failed to create stream"); |
| 84 |
exit(EXIT_FAILURE); |
| 85 |
} |
| 86 |
EditLine *const el = el_init("test", instream, stdout, stderr); |
| 87 |
if(!el) { |
| 88 |
fputs("Failed to initialize libedit\n", stderr); |
| 89 |
if(fclose(instream) == EOF) { |
| 90 |
perror("Failed to close stream"); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
const char *line = el_gets(el, &(int){0}); |
| 95 |
if(!line) { |
| 96 |
if(feof(instream)) { |
| 97 |
fputs("End of file\n", stderr); |
| 98 |
} else { |
| 99 |
perror("Failed to read line"); |
| 100 |
} |
| 101 |
el_end(el); |
| 102 |
if(fclose(instream) == EOF) { |
| 103 |
perror("Failed to close stream"); |
| 104 |
} |
| 105 |
exit(EXIT_FAILURE); |
| 106 |
} |
| 107 |
if(fclose(instream) == EOF) { |
| 108 |
perror("Failed to close stream"); |
| 109 |
el_end(el); |
| 110 |
exit(EXIT_FAILURE); |
| 111 |
} |
| 112 |
|
| 113 |
if(strcmp(mbstest, line)) { |
| 114 |
fputs("Strings did not match\n", stderr); |
| 115 |
abort(); |
| 116 |
} |
| 117 |
el_end(el); |
| 118 |
|
| 119 |
/* Now let's try a wide-oriented stream. |
| 120 |
* Note: We assume here that the external representation for a wide-oriented stream |
| 121 |
* is the ordinary multibyte encoding. This may not be a good assumption for some locales. */ |
| 122 |
FILE *const widestream = fmemopen_with_fd(mbstest, strlen(mbstest), "r"); |
| 123 |
if(!widestream) { |
| 124 |
perror("Failed to create stream"); |
| 125 |
exit(EXIT_FAILURE); |
| 126 |
} |
| 127 |
|
| 128 |
errno = 0; |
| 129 |
if(fwide(widestream, 1) <= 0) { |
| 130 |
if(errno) { |
| 131 |
perror("Failed to set wide orientation of stream"); |
| 132 |
} else { |
| 133 |
fputs("Failed to set wide orientation of stream\n", stderr); |
| 134 |
} |
| 135 |
if(fclose(widestream) == EOF) { |
| 136 |
perror("Failed to close stream"); |
| 137 |
} |
| 138 |
exit(EXIT_FAILURE); |
| 139 |
} |
| 140 |
|
| 141 |
EditLine *const elw = el_init("test", widestream, stdout, stderr); |
| 142 |
if(!elw) { |
| 143 |
fputs("Failed to initialize libedit\n", stderr); |
| 144 |
if(fclose(widestream) == EOF) { |
| 145 |
perror("Failed to close stream"); |
| 146 |
} |
| 147 |
exit(EXIT_FAILURE); |
| 148 |
} |
| 149 |
|
| 150 |
const wchar_t *const wline = el_wgets(elw, &(int){0}); |
| 151 |
if(!wline) { |
| 152 |
if(feof(widestream)) { |
| 153 |
fputs("Reached end of file\n", stderr); |
| 154 |
} else { |
| 155 |
puts("Failed to read line"); |
| 156 |
} |
| 157 |
el_end(elw); |
| 158 |
if(fclose(widestream) == EOF) { |
| 159 |
perror("Failed to close stream"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if(wcscmp(wline, wcstest)) { |
| 164 |
fputs("String comparison failed\n", stderr); |
| 165 |
abort(); |
| 166 |
} |
| 167 |
|
| 168 |
el_end(elw); |
| 169 |
} |