windows: getline fixes & discard trailing CRLF

This commit is contained in:
dzaima 2023-01-08 20:35:20 +02:00
parent c88b7c6a14
commit 381460e92e
2 changed files with 4 additions and 3 deletions

View File

@ -908,6 +908,7 @@ int main(int argc, char* argv[]) {
i64 read = getline(&ln, &gl, stdin); i64 read = getline(&ln, &gl, stdin);
if (read<=0 || ln[0]==0) { if(!silentREPL) putchar('\n'); break; } if (read<=0 || ln[0]==0) { if(!silentREPL) putchar('\n'); break; }
if (ln[read-1]==10) ln[--read] = 0; if (ln[read-1]==10) ln[--read] = 0;
if (ln[read-1]==13) ln[--read] = 0;
cbqn_runLine(ln, read); cbqn_runLine(ln, read);
free(ln); free(ln);
} }

View File

@ -19,7 +19,7 @@ ssize_t getline (char **lptr, size_t *n, FILE *fp) {
goto error; goto error;
} }
convertResult = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL); convertResult = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (convertResult == 0) { if (convertResult == 0) {
fprintf(stderr, "Failed to get MultiByte length: %d", GetLastError()); fprintf(stderr, "Failed to get MultiByte length: %d", GetLastError());
goto error; goto error;
@ -27,13 +27,13 @@ ssize_t getline (char **lptr, size_t *n, FILE *fp) {
m = *lptr = (char*) calloc(convertResult, sizeof(char)); m = *lptr = (char*) calloc(convertResult, sizeof(char));
if (WideCharToMultiByte(CP_ACP, 0, buf, -1, m, convertResult, NULL, NULL) == 0 ) { if (WideCharToMultiByte(CP_UTF8, 0, buf, -1, m, convertResult, NULL, NULL) == 0 ) {
fprintf(stderr, "Failed to convert wide characters: %d", GetLastError()); fprintf(stderr, "Failed to convert wide characters: %d", GetLastError());
free(m); free(m);
goto error; goto error;
} }
return convertResult; return convertResult-1;
error: error:
CloseHandle(hIn); CloseHandle(hIn);