mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-05-25 15:47:25 +08:00
avrdude: Fix string encoding handling on Windows
This commit is contained in:
parent
c426c99af0
commit
bc1aaaa146
@ -1,5 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.0)
|
cmake_minimum_required(VERSION 3.0)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
add_definitions(-D_BSD_SOURCE -D_DEFAULT_SOURCE) # To enable various useful macros and functions on Unices
|
add_definitions(-D_BSD_SOURCE -D_DEFAULT_SOURCE) # To enable various useful macros and functions on Unices
|
||||||
remove_definitions(-D_UNICODE -DUNICODE)
|
remove_definitions(-D_UNICODE -DUNICODE)
|
||||||
@ -68,6 +70,7 @@ set(AVRDUDE_SOURCES
|
|||||||
)
|
)
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
set(AVRDUDE_SOURCES ${AVRDUDE_SOURCES}
|
set(AVRDUDE_SOURCES ${AVRDUDE_SOURCES}
|
||||||
|
windows/utf8.c
|
||||||
windows/unistd.cpp
|
windows/unistd.cpp
|
||||||
windows/getopt.c
|
windows/getopt.c
|
||||||
)
|
)
|
||||||
@ -90,7 +93,7 @@ add_custom_target(gen_conf_h
|
|||||||
add_library(avrdude STATIC ${AVRDUDE_SOURCES})
|
add_library(avrdude STATIC ${AVRDUDE_SOURCES})
|
||||||
add_dependencies(avrdude gen_conf_h)
|
add_dependencies(avrdude gen_conf_h)
|
||||||
|
|
||||||
add_executable(avrdude-slic3r main-standalone.c)
|
add_executable(avrdude-slic3r main-standalone.cpp)
|
||||||
target_link_libraries(avrdude-slic3r avrdude)
|
target_link_libraries(avrdude-slic3r avrdude)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
|
@ -40,6 +40,9 @@
|
|||||||
#include "avrdude.h"
|
#include "avrdude.h"
|
||||||
#include "libavrdude.h"
|
#include "libavrdude.h"
|
||||||
|
|
||||||
|
#if defined(WIN32NATIVE)
|
||||||
|
#include "windows/utf8.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#define IHEX_MAXDATA 256
|
#define IHEX_MAXDATA 256
|
||||||
|
|
||||||
@ -110,7 +113,7 @@ FILE *fopen_utf8(const char *filename, const char *mode)
|
|||||||
static wchar_t mode_buffer[MAX_MODE_LEN];
|
static wchar_t mode_buffer[MAX_MODE_LEN];
|
||||||
|
|
||||||
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, fname_buffer, PATH_MAX) == 0) { return NULL; }
|
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, fname_buffer, PATH_MAX) == 0) { return NULL; }
|
||||||
if (MultiByteToWideChar(CP_ACP, 0, mode, -1, mode_buffer, MAX_MODE_LEN) == 0) { return NULL; }
|
if (MultiByteToWideChar(CP_UTF8, 0, mode, -1, mode_buffer, MAX_MODE_LEN) == 0) { return NULL; }
|
||||||
|
|
||||||
return _wfopen(fname_buffer, mode_buffer);
|
return _wfopen(fname_buffer, mode_buffer);
|
||||||
#else
|
#else
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
#include "avrdude.h"
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
return avrdude_main(argc, argv);
|
|
||||||
}
|
|
54
src/avrdude/main-standalone.cpp
Normal file
54
src/avrdude/main-standalone.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
extern "C" {
|
||||||
|
#include "avrdude.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "windows/utf8.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ArgvUtf8 : std::vector<char*>
|
||||||
|
{
|
||||||
|
int argc;
|
||||||
|
|
||||||
|
ArgvUtf8(int argc_w, wchar_t *argv_w[]) : std::vector<char*>(argc_w + 1, nullptr), argc(0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < argc_w; i++) {
|
||||||
|
char *arg_utf8 = ::wstr_to_utf8(argv_w[i], -1);
|
||||||
|
if (arg_utf8 != nullptr) {
|
||||||
|
operator[](i) = arg_utf8;
|
||||||
|
argc = i + 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~ArgvUtf8()
|
||||||
|
{
|
||||||
|
for (char *arg : *this) {
|
||||||
|
if (arg != nullptr) {
|
||||||
|
::free(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int wmain(int argc_w, wchar_t *argv_w[])
|
||||||
|
{
|
||||||
|
ArgvUtf8 argv_utf8(argc_w, argv_w);
|
||||||
|
return ::avrdude_main(argv_utf8.argc, &argv_utf8[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
return ::avrdude_main(argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -812,7 +812,6 @@ int avrdude_main(int argc, char * argv [])
|
|||||||
|
|
||||||
// rc = read_config(sys_config);
|
// rc = read_config(sys_config);
|
||||||
rc = read_config_builtin();
|
rc = read_config_builtin();
|
||||||
avrdude_message(MSG_NOTICE, "\n\nread_config_builtin: %d\n\n", rc);
|
|
||||||
if (rc) {
|
if (rc) {
|
||||||
// avrdude_message(MSG_INFO, "%s: error reading system wide configuration file \"%s\"\n",
|
// avrdude_message(MSG_INFO, "%s: error reading system wide configuration file \"%s\"\n",
|
||||||
// progname, sys_config);
|
// progname, sys_config);
|
||||||
|
@ -34,16 +34,63 @@
|
|||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <ctype.h> /* for isprint */
|
#include <ctype.h> /* for isprint */
|
||||||
#include <errno.h> /* ENOTTY */
|
#include <errno.h> /* ENOTTY */
|
||||||
|
|
||||||
#include "avrdude.h"
|
#include "avrdude.h"
|
||||||
#include "libavrdude.h"
|
#include "libavrdude.h"
|
||||||
|
#include "windows/utf8.h"
|
||||||
|
|
||||||
long serial_recv_timeout = 5000; /* ms */
|
long serial_recv_timeout = 5000; /* ms */
|
||||||
|
|
||||||
#define W32SERBUFSIZE 1024
|
#define W32SERBUFSIZE 1024
|
||||||
|
|
||||||
|
|
||||||
|
// Get last error message string in UTF-8
|
||||||
|
// Always return a valid null-terminated string
|
||||||
|
// The returned string should be freed by the caller
|
||||||
|
char* last_error_string(int wsa)
|
||||||
|
{
|
||||||
|
LPWSTR wbuffer = NULL;
|
||||||
|
|
||||||
|
(void)wsa;
|
||||||
|
|
||||||
|
DWORD wbuffer_len = FormatMessageW(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
#ifdef HAVE_LIBWS2_32
|
||||||
|
wsa ? WSAGetLastError() : GetLastError(),
|
||||||
|
#else
|
||||||
|
GetLastError(),
|
||||||
|
#endif
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
|
(LPWSTR)&wbuffer,
|
||||||
|
0,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (wbuffer_len == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *res = wstr_to_utf8(wbuffer, wbuffer_len);
|
||||||
|
|
||||||
|
LocalFree(wbuffer);
|
||||||
|
|
||||||
|
if (res == NULL) {
|
||||||
|
// If we get here, conversion to UTF-8 failed
|
||||||
|
res = strdup("(could not get error message)");
|
||||||
|
if (res == NULL) {
|
||||||
|
avrdude_oom("last_error_string(): out of memory\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
struct baud_mapping {
|
struct baud_mapping {
|
||||||
long baud;
|
long baud;
|
||||||
DWORD speed;
|
DWORD speed;
|
||||||
@ -129,7 +176,6 @@ static int
|
|||||||
net_open(const char *port, union filedescriptor *fdp)
|
net_open(const char *port, union filedescriptor *fdp)
|
||||||
{
|
{
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
LPVOID lpMsgBuf;
|
|
||||||
|
|
||||||
char *hstr, *pstr, *end;
|
char *hstr, *pstr, *end;
|
||||||
unsigned int pnum;
|
unsigned int pnum;
|
||||||
@ -175,18 +221,10 @@ net_open(const char *port, union filedescriptor *fdp)
|
|||||||
free(hstr);
|
free(hstr);
|
||||||
|
|
||||||
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
FormatMessage(
|
const char *error = last_error_string(1);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: net_open(): Cannot open socket: %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL,
|
|
||||||
WSAGetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR)&lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: net_open(): Cannot open socket: %s\n", progname, (char *)lpMsgBuf);
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,18 +234,9 @@ net_open(const char *port, union filedescriptor *fdp)
|
|||||||
memcpy(&(sockaddr.sin_addr.s_addr), hp->h_addr, sizeof(struct in_addr));
|
memcpy(&(sockaddr.sin_addr.s_addr), hp->h_addr, sizeof(struct in_addr));
|
||||||
|
|
||||||
if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
|
if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
|
||||||
FormatMessage(
|
const char *error = last_error_string(1);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: net_open(): Connect failed: %s\n", progname);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL,
|
|
||||||
WSAGetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR)&lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: net_open(): Connect failed: %s\n", progname, (char *)lpMsgBuf);
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +250,6 @@ net_open(const char *port, union filedescriptor *fdp)
|
|||||||
|
|
||||||
static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
|
static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
|
||||||
{
|
{
|
||||||
LPVOID lpMsgBuf;
|
|
||||||
HANDLE hComPort=INVALID_HANDLE_VALUE;
|
HANDLE hComPort=INVALID_HANDLE_VALUE;
|
||||||
char *newname = 0;
|
char *newname = 0;
|
||||||
|
|
||||||
@ -261,19 +289,9 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
|
|||||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
|
||||||
if (hComPort == INVALID_HANDLE_VALUE) {
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
||||||
FormatMessage(
|
const char *error = last_error_string(0);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: ser_open(): can't open device \"%s\": %s\n", progname, port, error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL,
|
|
||||||
GetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
||||||
(LPTSTR) &lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: ser_open(): can't open device \"%s\": %s\n",
|
|
||||||
progname, port, (char*)lpMsgBuf);
|
|
||||||
LocalFree( lpMsgBuf );
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,14 +364,13 @@ static int ser_set_dtr_rts(union filedescriptor *fd, int is_on)
|
|||||||
#ifdef HAVE_LIBWS2_32
|
#ifdef HAVE_LIBWS2_32
|
||||||
static int net_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen)
|
static int net_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen)
|
||||||
{
|
{
|
||||||
LPVOID lpMsgBuf;
|
|
||||||
int rc;
|
int rc;
|
||||||
const unsigned char *p = buf;
|
const unsigned char *p = buf;
|
||||||
size_t len = buflen;
|
size_t len = buflen;
|
||||||
|
|
||||||
if (fd->ifd < 0) {
|
if (fd->ifd < 0) {
|
||||||
avrdude_message(MSG_NOTICE, "%s: net_send(): connection not open\n", progname);
|
avrdude_message(MSG_NOTICE, "%s: net_send(): connection not open\n", progname);
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!len) {
|
if (!len) {
|
||||||
@ -382,19 +399,10 @@ static int net_send(union filedescriptor *fd, const unsigned char * buf, size_t
|
|||||||
while (len) {
|
while (len) {
|
||||||
rc = send(fd->ifd, p, (len > 1024) ? 1024 : len, 0);
|
rc = send(fd->ifd, p, (len > 1024) ? 1024 : len, 0);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
FormatMessage(
|
const char *error = last_error_string(1);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: net_send(): send error: %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
return -1;
|
||||||
NULL,
|
|
||||||
WSAGetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR)&lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: net_send(): send error: %s\n", progname, (char *)lpMsgBuf);
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
p += rc;
|
p += rc;
|
||||||
len -= rc;
|
len -= rc;
|
||||||
@ -423,8 +431,7 @@ static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t
|
|||||||
HANDLE hComPort=(HANDLE)fd->pfd;
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
||||||
|
|
||||||
if (hComPort == INVALID_HANDLE_VALUE) {
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
||||||
avrdude_message(MSG_INFO, "%s: ser_send(): port not open\n",
|
avrdude_message(MSG_INFO, "%s: ser_send(): port not open\n", progname);
|
||||||
progname);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -452,15 +459,15 @@ static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t
|
|||||||
|
|
||||||
serial_w32SetTimeOut(hComPort,500);
|
serial_w32SetTimeOut(hComPort,500);
|
||||||
|
|
||||||
if (!WriteFile (hComPort, buf, buflen, &written, NULL)) {
|
if (!WriteFile(hComPort, buf, buflen, &written, NULL)) {
|
||||||
avrdude_message(MSG_INFO, "%s: ser_send(): write error: %s\n",
|
const char *error = last_error_string(0);
|
||||||
progname, "sorry no info avail"); // TODO
|
avrdude_message(MSG_INFO, "%s: ser_send(): write error: %s\n", progname, error);
|
||||||
|
free(error);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (written != buflen) {
|
if (written != buflen) {
|
||||||
avrdude_message(MSG_INFO, "%s: ser_send(): size/send mismatch\n",
|
avrdude_message(MSG_INFO, "%s: ser_send(): size/send mismatch\n", progname);
|
||||||
progname);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,7 +478,6 @@ static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t
|
|||||||
#ifdef HAVE_LIBWS2_32
|
#ifdef HAVE_LIBWS2_32
|
||||||
static int net_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen)
|
static int net_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen)
|
||||||
{
|
{
|
||||||
LPVOID lpMsgBuf;
|
|
||||||
struct timeval timeout, to2;
|
struct timeval timeout, to2;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
int nfds;
|
int nfds;
|
||||||
@ -481,7 +487,7 @@ static int net_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen
|
|||||||
|
|
||||||
if (fd->ifd < 0) {
|
if (fd->ifd < 0) {
|
||||||
avrdude_message(MSG_INFO, "%s: net_recv(): connection not open\n", progname);
|
avrdude_message(MSG_INFO, "%s: net_recv(): connection not open\n", progname);
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.tv_sec = serial_recv_timeout / 1000L;
|
timeout.tv_sec = serial_recv_timeout / 1000L;
|
||||||
@ -504,37 +510,19 @@ reselect:
|
|||||||
avrdude_message(MSG_NOTICE, "%s: ser_recv(): programmer is not responding, reselecting\n", progname);
|
avrdude_message(MSG_NOTICE, "%s: ser_recv(): programmer is not responding, reselecting\n", progname);
|
||||||
goto reselect;
|
goto reselect;
|
||||||
} else {
|
} else {
|
||||||
FormatMessage(
|
const char *error = last_error_string(1);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: ser_recv(): select(): %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
return -1;
|
||||||
NULL,
|
|
||||||
WSAGetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR)&lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: ser_recv(): select(): %s\n", progname, (char *)lpMsgBuf);
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = recv(fd->ifd, p, (buflen - len > 1024) ? 1024 : buflen - len, 0);
|
rc = recv(fd->ifd, p, (buflen - len > 1024) ? 1024 : buflen - len, 0);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
FormatMessage(
|
const char *error = last_error_string(1);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
free(error);
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
return -1;
|
||||||
NULL,
|
|
||||||
WSAGetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR)&lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n", progname, (char *)lpMsgBuf);
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
p += rc;
|
p += rc;
|
||||||
len += rc;
|
len += rc;
|
||||||
@ -581,35 +569,22 @@ static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen
|
|||||||
HANDLE hComPort=(HANDLE)fd->pfd;
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
||||||
|
|
||||||
if (hComPort == INVALID_HANDLE_VALUE) {
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
||||||
avrdude_message(MSG_INFO, "%s: ser_read(): port not open\n",
|
avrdude_message(MSG_INFO, "%s: ser_read(): port not open\n", progname);
|
||||||
progname);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial_w32SetTimeOut(hComPort, serial_recv_timeout);
|
serial_w32SetTimeOut(hComPort, serial_recv_timeout);
|
||||||
|
|
||||||
if (!ReadFile(hComPort, buf, buflen, &read, NULL)) {
|
if (!ReadFile(hComPort, buf, buflen, &read, NULL)) {
|
||||||
LPVOID lpMsgBuf;
|
const char *error = last_error_string(0);
|
||||||
FormatMessage(
|
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
free(error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL,
|
|
||||||
GetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
||||||
(LPTSTR) &lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL );
|
|
||||||
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n",
|
|
||||||
progname, (char*)lpMsgBuf);
|
|
||||||
LocalFree( lpMsgBuf );
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* time out detected */
|
/* time out detected */
|
||||||
if (read == 0) {
|
if (read == 0) {
|
||||||
avrdude_message(MSG_NOTICE2, "%s: ser_recv(): programmer is not responding\n",
|
avrdude_message(MSG_NOTICE2, "%s: ser_recv(): programmer is not responding\n", progname);
|
||||||
progname);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,20 +639,9 @@ static int ser_drain(union filedescriptor *fd, int display)
|
|||||||
|
|
||||||
readres=ReadFile(hComPort, buf, 1, &read, NULL);
|
readres=ReadFile(hComPort, buf, 1, &read, NULL);
|
||||||
if (!readres) {
|
if (!readres) {
|
||||||
LPVOID lpMsgBuf;
|
const char *error = last_error_string(0);
|
||||||
FormatMessage(
|
avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n", progname, error);
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
free(error);
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL,
|
|
||||||
GetLastError(),
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
||||||
(LPTSTR) &lpMsgBuf,
|
|
||||||
0,
|
|
||||||
NULL );
|
|
||||||
avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n",
|
|
||||||
progname, (char*)lpMsgBuf);
|
|
||||||
LocalFree( lpMsgBuf );
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
45
src/avrdude/windows/utf8.c
Normal file
45
src/avrdude/windows/utf8.c
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include "utf8.h"
|
||||||
|
|
||||||
|
|
||||||
|
char* wstr_to_utf8(LPWSTR wstr, int len)
|
||||||
|
{
|
||||||
|
char *res = NULL;
|
||||||
|
|
||||||
|
int res_size = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
|
||||||
|
if (res_size > 0) {
|
||||||
|
// Note: WideCharToMultiByte doesn't null-terminate if real (ie. > 0) buffer length is passed
|
||||||
|
res = malloc(len != - 1 ? res_size + 1 : res_size);
|
||||||
|
if (res == NULL) { return NULL; }
|
||||||
|
|
||||||
|
if (WideCharToMultiByte(CP_UTF8, 0, wstr, len, res, res_size, NULL, NULL) == res_size) {
|
||||||
|
if (len != -1) { res[res_size] = '\0'; }
|
||||||
|
} else {
|
||||||
|
free(res);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
LPWSTR utf8_to_wstr(const char *str, int len)
|
||||||
|
{
|
||||||
|
LPWSTR res = NULL;
|
||||||
|
|
||||||
|
int res_size = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
|
||||||
|
if (res_size > 0) {
|
||||||
|
// Note: MultiByteToWideChar doesn't null-terminate if real (ie. > 0) buffer length is passed
|
||||||
|
res = malloc(len != - 1 ? res_size + 1 : res_size);
|
||||||
|
|
||||||
|
if (res == NULL) { return NULL; }
|
||||||
|
|
||||||
|
if (MultiByteToWideChar(CP_UTF8, 0, str, len, res, res_size) == res_size) {
|
||||||
|
if (len != -1) { res[res_size] = L'\0'; }
|
||||||
|
} else {
|
||||||
|
free(res);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
10
src/avrdude/windows/utf8.h
Normal file
10
src/avrdude/windows/utf8.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef SLIC3R_AVRDUDE_UTF8_H
|
||||||
|
#define SLIC3R_AVRDUDE_UTF8_H
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
extern char* wstr_to_utf8(LPWSTR wstr, int len);
|
||||||
|
extern LPWSTR utf8_to_wstr(const char *str, int len);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SLIC3R_AVRDUDE_UTF8_H
|
Loading…
x
Reference in New Issue
Block a user