Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVojtech Kral <vojtech@kral.hk>2018-05-04 17:06:53 +0300
committerVojtech Kral <vojtech@kral.hk>2018-05-21 19:58:22 +0300
commit07817c8ee53e1690d0de8e848fc4b42388b630a5 (patch)
treefa24b43700e59f24cc1470295fd0b644be1da48f /xs/src/avrdude
parent68a570221b995b91acf3a0489a538c95c27965a6 (diff)
avrdude: Compilation on Windows/MSVC
Diffstat (limited to 'xs/src/avrdude')
-rw-r--r--xs/src/avrdude/CMakeLists.txt69
-rw-r--r--xs/src/avrdude/ac_cfg.h7
-rw-r--r--xs/src/avrdude/avr.c5
-rw-r--r--xs/src/avrdude/avr910.c5
-rw-r--r--xs/src/avrdude/avrdude-slic3r.cpp5
-rw-r--r--xs/src/avrdude/avrdude-slic3r.hpp2
-rw-r--r--xs/src/avrdude/avrdude.h32
-rw-r--r--xs/src/avrdude/libavrdude.h5
-rw-r--r--xs/src/avrdude/main.c6
-rw-r--r--xs/src/avrdude/pgm_type.c4
-rw-r--r--xs/src/avrdude/ppiwin.c2
-rw-r--r--xs/src/avrdude/ser_win32.c2
-rw-r--r--xs/src/avrdude/serbb_win32.c2
-rw-r--r--xs/src/avrdude/stk500.c3
-rw-r--r--xs/src/avrdude/stk500v2.c1093
-rw-r--r--xs/src/avrdude/windows/getopt.c1258
-rw-r--r--xs/src/avrdude/windows/getopt.h188
-rw-r--r--xs/src/avrdude/windows/unistd.cpp44
-rw-r--r--xs/src/avrdude/windows/unistd.h85
19 files changed, 2251 insertions, 566 deletions
diff --git a/xs/src/avrdude/CMakeLists.txt b/xs/src/avrdude/CMakeLists.txt
new file mode 100644
index 000000000..c74884057
--- /dev/null
+++ b/xs/src/avrdude/CMakeLists.txt
@@ -0,0 +1,69 @@
+
+
+remove_definitions(-D_UNICODE -DUNICODE)
+
+
+set(AVRDUDE_SOURCES
+ ${LIBDIR}/avrdude/arduino.c
+ ${LIBDIR}/avrdude/avr.c
+ # ${LIBDIR}/avrdude/avrftdi.c
+ # ${LIBDIR}/avrdude/avrftdi_tpi.c
+ ${LIBDIR}/avrdude/avrpart.c
+ ${LIBDIR}/avrdude/avr910.c
+ ${LIBDIR}/avrdude/bitbang.c
+ ${LIBDIR}/avrdude/buspirate.c
+ ${LIBDIR}/avrdude/butterfly.c
+ ${LIBDIR}/avrdude/config.c
+ ${LIBDIR}/avrdude/config_gram.c
+ # ${LIBDIR}/avrdude/confwin.c
+ ${LIBDIR}/avrdude/crc16.c
+ # ${LIBDIR}/avrdude/dfu.c
+ ${LIBDIR}/avrdude/fileio.c
+ # ${LIBDIR}/avrdude/flip1.c
+ # ${LIBDIR}/avrdude/flip2.c
+ # ${LIBDIR}/avrdude/ft245r.c
+ # ${LIBDIR}/avrdude/jtagmkI.c
+ # ${LIBDIR}/avrdude/jtagmkII.c
+ # ${LIBDIR}/avrdude/jtag3.c
+ ${LIBDIR}/avrdude/lexer.c
+ ${LIBDIR}/avrdude/linuxgpio.c
+ ${LIBDIR}/avrdude/lists.c
+ # ${LIBDIR}/avrdude/par.c
+ ${LIBDIR}/avrdude/pgm.c
+ ${LIBDIR}/avrdude/pgm_type.c
+ ${LIBDIR}/avrdude/pickit2.c
+ ${LIBDIR}/avrdude/pindefs.c
+ # ${LIBDIR}/avrdude/ppi.c
+ # ${LIBDIR}/avrdude/ppiwin.c
+ ${LIBDIR}/avrdude/safemode.c
+ ${LIBDIR}/avrdude/ser_avrdoper.c
+ ${LIBDIR}/avrdude/serbb_posix.c
+ ${LIBDIR}/avrdude/serbb_win32.c
+ ${LIBDIR}/avrdude/ser_posix.c
+ ${LIBDIR}/avrdude/ser_win32.c
+ ${LIBDIR}/avrdude/stk500.c
+ ${LIBDIR}/avrdude/stk500generic.c
+ ${LIBDIR}/avrdude/stk500v2.c
+ ${LIBDIR}/avrdude/term.c
+ ${LIBDIR}/avrdude/update.c
+ # ${LIBDIR}/avrdude/usbasp.c
+ # ${LIBDIR}/avrdude/usb_hidapi.c
+ # ${LIBDIR}/avrdude/usb_libusb.c
+ # ${LIBDIR}/avrdude/usbtiny.c
+ ${LIBDIR}/avrdude/wiring.c
+
+ ${LIBDIR}/avrdude/main.c
+ ${LIBDIR}/avrdude/avrdude-slic3r.hpp
+ ${LIBDIR}/avrdude/avrdude-slic3r.cpp
+)
+if (WIN32)
+ set(AVRDUDE_SOURCES ${AVRDUDE_SOURCES}
+ ${LIBDIR}/avrdude/windows/unistd.cpp
+ ${LIBDIR}/avrdude/windows/getopt.c
+ )
+endif()
+add_library(avrdude STATIC ${AVRDUDE_SOURCES})
+if (WIN32)
+ target_compile_definitions(avrdude PRIVATE WIN32NATIVE=1)
+ target_include_directories(avrdude SYSTEM PRIVATE ${LIBDIR}/avrdude/windows) # So that sources find the getopt.h windows drop-in
+endif()
diff --git a/xs/src/avrdude/ac_cfg.h b/xs/src/avrdude/ac_cfg.h
index ff1aff5cc..2461bf307 100644
--- a/xs/src/avrdude/ac_cfg.h
+++ b/xs/src/avrdude/ac_cfg.h
@@ -17,7 +17,12 @@
#define HAVE_FCNTL_H 1
/* Define to 1 if you have the `gettimeofday' function. */
+#if defined (WIN32NATIVE)
+/* #undef HAVE_GETTIMEOFDAY */
+// We have a gettimeofday() replacement in unistd.cpp (there is also one in ppiwin.c, but that file is written for Cygwin/MinGW)
+#else
#define HAVE_GETTIMEOFDAY 1
+#endif
/* Define to 1 if you have the <hidapi/hidapi.h> header file. */
/* #undef HAVE_HIDAPI_HIDAPI_H */
@@ -89,7 +94,7 @@
#define HAVE_MEMSET 1
/* parallel port access enabled */
-#define HAVE_PARPORT 1
+// #define HAVE_PARPORT 1
/* Define to 1 if you have the <pthread.h> header file. */
// #define HAVE_PTHREAD_H 1
diff --git a/xs/src/avrdude/avr.c b/xs/src/avrdude/avr.c
index 8df586a33..a9dd2a7cd 100644
--- a/xs/src/avrdude/avr.c
+++ b/xs/src/avrdude/avr.c
@@ -25,9 +25,12 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
-#include <sys/time.h>
#include <time.h>
+#if !defined(WIN32NATIVE)
+# include <sys/time.h>
+#endif
+
#include "avrdude.h"
#include "libavrdude.h"
diff --git a/xs/src/avrdude/avr910.c b/xs/src/avrdude/avr910.c
index bcc71d60a..aa5cc07a9 100644
--- a/xs/src/avrdude/avr910.c
+++ b/xs/src/avrdude/avr910.c
@@ -32,9 +32,12 @@
#include <string.h>
#include <errno.h>
#include <ctype.h>
-#include <sys/time.h>
#include <unistd.h>
+#if !defined(WIN32NATIVE)
+# include <sys/time.h>
+#endif
+
#include "avrdude.h"
#include "libavrdude.h"
diff --git a/xs/src/avrdude/avrdude-slic3r.cpp b/xs/src/avrdude/avrdude-slic3r.cpp
index 37da0a530..9f9093aa2 100644
--- a/xs/src/avrdude/avrdude-slic3r.cpp
+++ b/xs/src/avrdude/avrdude-slic3r.cpp
@@ -16,19 +16,20 @@ static void avrdude_message_handler_ostream(const char *msg, unsigned size, void
os << msg;
}
-int main(std::vector<std::string> args, std::string sys_config, std::ostream &stderr)
+int main(std::vector<std::string> args, std::string sys_config, std::ostream &os)
{
std::vector<char *> c_args {{ const_cast<char*>(PACKAGE_NAME) }};
for (const auto &arg : args) {
c_args.push_back(const_cast<char*>(arg.data()));
}
- ::avrdude_message_handler_set(avrdude_message_handler_ostream, reinterpret_cast<void*>(&stderr));
+ ::avrdude_message_handler_set(avrdude_message_handler_ostream, reinterpret_cast<void*>(&os));
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data(), sys_config.c_str());
::avrdude_message_handler_set(nullptr, nullptr);
return res;
}
+
}
}
diff --git a/xs/src/avrdude/avrdude-slic3r.hpp b/xs/src/avrdude/avrdude-slic3r.hpp
index 37be88b94..d26a0edda 100644
--- a/xs/src/avrdude/avrdude-slic3r.hpp
+++ b/xs/src/avrdude/avrdude-slic3r.hpp
@@ -8,7 +8,7 @@
namespace Slic3r {
namespace AvrDude {
- int main(std::vector<std::string> args, std::string sys_config, std::ostream &stderr);
+ int main(std::vector<std::string> args, std::string sys_config, std::ostream &os);
}
}
diff --git a/xs/src/avrdude/avrdude.h b/xs/src/avrdude/avrdude.h
index 496b70e54..7763d1464 100644
--- a/xs/src/avrdude/avrdude.h
+++ b/xs/src/avrdude/avrdude.h
@@ -43,25 +43,31 @@ int avrdude_main(int argc, char * argv [], const char *sys_config);
#if defined(WIN32NATIVE)
-#include "ac_cfg.h"
+// #include "ac_cfg.h"
+
#include <windows.h>
+#include <unistd.h>
-#ifdef __cplusplus
-extern "C" {
+#ifdef UNICODE
+#error "UNICODE should not be defined for avrdude bits on Windows"
#endif
-#if !defined(HAVE_USLEEP)
-int usleep(unsigned int us);
-#endif
+// #ifdef __cplusplus
+// extern "C" {
+// #endif
-#if !defined(HAVE_GETTIMEOFDAY)
-struct timezone;
-int gettimeofday(struct timeval *tv, struct timezone *tz);
-#endif /* HAVE_GETTIMEOFDAY */
+// #if !defined(HAVE_USLEEP)
+// int usleep(unsigned int us);
+// #endif
-#ifdef __cplusplus
-}
-#endif
+// #if !defined(HAVE_GETTIMEOFDAY)
+// struct timezone;
+// int gettimeofday(struct timeval *tv, struct timezone *tz);
+// #endif /* HAVE_GETTIMEOFDAY */
+
+// #ifdef __cplusplus
+// }
+// #endif
#endif /* defined(WIN32NATIVE) */
#endif
diff --git a/xs/src/avrdude/libavrdude.h b/xs/src/avrdude/libavrdude.h
index 8541c51ad..19f4de24c 100644
--- a/xs/src/avrdude/libavrdude.h
+++ b/xs/src/avrdude/libavrdude.h
@@ -41,6 +41,11 @@ typedef unsigned long pinmask_t;
#endif
+#if defined(WIN32NATIVE)
+#define PATH_MAX 1024 // Tad arbitrary, but should be ok for avrdude's pruposes
+#endif
+
+
/* formerly lists.h */
/*----------------------------------------------------------------------
diff --git a/xs/src/avrdude/main.c b/xs/src/avrdude/main.c
index 3ed2e3e56..03c237d75 100644
--- a/xs/src/avrdude/main.c
+++ b/xs/src/avrdude/main.c
@@ -42,8 +42,10 @@
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
+
+#if !defined(WIN32NATIVE)
+# include <sys/time.h>
+#endif
#include "avrdude.h"
#include "libavrdude.h"
diff --git a/xs/src/avrdude/pgm_type.c b/xs/src/avrdude/pgm_type.c
index cc4814f3c..ef27cf900 100644
--- a/xs/src/avrdude/pgm_type.c
+++ b/xs/src/avrdude/pgm_type.c
@@ -40,7 +40,7 @@
// #include "jtagmkII.h"
// #include "jtag3.h"
#include "linuxgpio.h"
-#include "par.h"
+// #include "par.h"
#include "pickit2.h"
#include "ppi.h"
#include "serbb.h"
@@ -80,7 +80,7 @@ const PROGRAMMER_TYPE programmers_types[] = {
// {"jtagice3_dw", jtag3_dw_initpgm, jtag3_dw_desc},
// {"jtagice3_isp", stk500v2_jtag3_initpgm, stk500v2_jtag3_desc},
{"linuxgpio", linuxgpio_initpgm, linuxgpio_desc},
- {"par", par_initpgm, par_desc},
+ // {"par", par_initpgm, par_desc},
{"pickit2", pickit2_initpgm, pickit2_desc},
{"serbb", serbb_initpgm, serbb_desc},
{"stk500", stk500_initpgm, stk500_desc},
diff --git a/xs/src/avrdude/ppiwin.c b/xs/src/avrdude/ppiwin.c
index bedbaf92f..7811288c0 100644
--- a/xs/src/avrdude/ppiwin.c
+++ b/xs/src/avrdude/ppiwin.c
@@ -152,7 +152,7 @@ static int winnt_pp_open(void)
}
else if(ver_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
- HANDLE h = CreateFile(DRIVERNAME,
+ HANDLE h = CreateFileA(DRIVERNAME,
GENERIC_READ,
0,
NULL,
diff --git a/xs/src/avrdude/ser_win32.c b/xs/src/avrdude/ser_win32.c
index fcaa55e61..a284774c2 100644
--- a/xs/src/avrdude/ser_win32.c
+++ b/xs/src/avrdude/ser_win32.c
@@ -254,7 +254,7 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
port = newname;
}
- hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+ hComPort = CreateFileA(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hComPort == INVALID_HANDLE_VALUE) {
diff --git a/xs/src/avrdude/serbb_win32.c b/xs/src/avrdude/serbb_win32.c
index e7e8e3039..4f1069c85 100644
--- a/xs/src/avrdude/serbb_win32.c
+++ b/xs/src/avrdude/serbb_win32.c
@@ -262,7 +262,7 @@ static int serbb_open(PROGRAMMER *pgm, char *port)
if (bitbang_check_prerequisites(pgm) < 0)
return -1;
- hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+ hComPort = CreateFileA(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hComPort == INVALID_HANDLE_VALUE) {
diff --git a/xs/src/avrdude/stk500.c b/xs/src/avrdude/stk500.c
index a2bf1fb5b..24052940b 100644
--- a/xs/src/avrdude/stk500.c
+++ b/xs/src/avrdude/stk500.c
@@ -35,6 +35,7 @@
#include <string.h>
#include <errno.h>
#include <unistd.h>
+#include <malloc.h>
#include "avrdude.h"
#include "libavrdude.h"
@@ -759,7 +760,7 @@ static int stk500_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size,
unsigned int addr, unsigned int n_bytes)
{
- unsigned char buf[page_size + 16];
+ unsigned char *buf = alloca(page_size + 16);
int memtype;
int a_div;
int block_size;
diff --git a/xs/src/avrdude/stk500v2.c b/xs/src/avrdude/stk500v2.c
index 7e991f971..7edca8100 100644
--- a/xs/src/avrdude/stk500v2.c
+++ b/xs/src/avrdude/stk500v2.c
@@ -44,9 +44,12 @@
#include <errno.h>
#include <limits.h>
#include <unistd.h>
-#include <sys/time.h>
#include <time.h>
+#if !defined(WIN32NATIVE)
+# include <sys/time.h>
+#endif
+
#include "avrdude.h"
#include "libavrdude.h"
@@ -303,48 +306,48 @@ void stk500v2_setup(PROGRAMMER * pgm)
static void stk500v2_jtagmkII_setup(PROGRAMMER * pgm)
{
- void *mycookie, *theircookie;
-
- if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
- avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
- progname);
- exit(1);
- }
- memset(pgm->cookie, 0, sizeof(struct pdata));
- PDATA(pgm)->command_sequence = 1;
-
- /*
- * Now, have the JTAG ICE mkII backend allocate its own private
- * data. Store our own cookie in a safe place for the time being.
- */
- mycookie = pgm->cookie;
- jtagmkII_setup(pgm);
- theircookie = pgm->cookie;
- pgm->cookie = mycookie;
- PDATA(pgm)->chained_pdata = theircookie;
+ // void *mycookie, *theircookie;
+
+ // if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
+ // progname);
+ // exit(1);
+ // }
+ // memset(pgm->cookie, 0, sizeof(struct pdata));
+ // PDATA(pgm)->command_sequence = 1;
+
+ // /*
+ // * Now, have the JTAG ICE mkII backend allocate its own private
+ // * data. Store our own cookie in a safe place for the time being.
+ // */
+ // mycookie = pgm->cookie;
+ // jtagmkII_setup(pgm);
+ // theircookie = pgm->cookie;
+ // pgm->cookie = mycookie;
+ // PDATA(pgm)->chained_pdata = theircookie;
}
static void stk500v2_jtag3_setup(PROGRAMMER * pgm)
{
- void *mycookie, *theircookie;
-
- if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
- avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
- progname);
- exit(1);
- }
- memset(pgm->cookie, 0, sizeof(struct pdata));
- PDATA(pgm)->command_sequence = 1;
-
- /*
- * Now, have the JTAGICE3 backend allocate its own private
- * data. Store our own cookie in a safe place for the time being.
- */
- mycookie = pgm->cookie;
- jtag3_setup(pgm);
- theircookie = pgm->cookie;
- pgm->cookie = mycookie;
- PDATA(pgm)->chained_pdata = theircookie;
+ // void *mycookie, *theircookie;
+
+ // if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
+ // progname);
+ // exit(1);
+ // }
+ // memset(pgm->cookie, 0, sizeof(struct pdata));
+ // PDATA(pgm)->command_sequence = 1;
+
+ // /*
+ // * Now, have the JTAGICE3 backend allocate its own private
+ // * data. Store our own cookie in a safe place for the time being.
+ // */
+ // mycookie = pgm->cookie;
+ // jtag3_setup(pgm);
+ // theircookie = pgm->cookie;
+ // pgm->cookie = mycookie;
+ // PDATA(pgm)->chained_pdata = theircookie;
}
void stk500v2_teardown(PROGRAMMER * pgm)
@@ -354,24 +357,24 @@ void stk500v2_teardown(PROGRAMMER * pgm)
static void stk500v2_jtagmkII_teardown(PROGRAMMER * pgm)
{
- void *mycookie;
+ // void *mycookie;
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtagmkII_teardown(pgm);
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtagmkII_teardown(pgm);
- free(mycookie);
+ // free(mycookie);
}
static void stk500v2_jtag3_teardown(PROGRAMMER * pgm)
{
- void *mycookie;
+ // void *mycookie;
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtag3_teardown(pgm);
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtag3_teardown(pgm);
- free(mycookie);
+ // free(mycookie);
}
@@ -414,48 +417,49 @@ static unsigned short get_jtagisp_return_size(unsigned char cmd)
*/
static int stk500v2_jtagmkII_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
- unsigned char *cmdbuf;
- int rv;
- unsigned short sz;
- void *mycookie;
-
- sz = get_jtagisp_return_size(data[0]);
- if (sz == 0) {
- avrdude_message(MSG_INFO, "%s: unsupported encapsulated ISP command: %#x\n",
- progname, data[0]);
- return -1;
- }
- if (sz == SZ_READ_FLASH_EE) {
- /*
- * For CMND_READ_FLASH_ISP and CMND_READ_EEPROM_ISP, extract the
- * size of the return data from the request. Note that the
- * request itself has the size in big endian format, while we are
- * supposed to deliver it in little endian.
- */
- sz = 3 + (data[1] << 8) + data[2];
- } else if (sz == SZ_SPI_MULTI) {
- /*
- * CMND_SPI_MULTI has the Rx size encoded in its 3rd byte.
- */
- sz = 3 + data[2];
- }
-
- if ((cmdbuf = malloc(len + 3)) == NULL) {
- avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
- progname);
- exit(1);
- }
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- cmdbuf[0] = CMND_ISP_PACKET;
- cmdbuf[1] = sz & 0xff;
- cmdbuf[2] = (sz >> 8) & 0xff;
- memcpy(cmdbuf + 3, data, len);
- rv = jtagmkII_send(pgm, cmdbuf, len + 3);
- free(cmdbuf);
- pgm->cookie = mycookie;
-
- return rv;
+ return -1;
+ // unsigned char *cmdbuf;
+ // int rv;
+ // unsigned short sz;
+ // void *mycookie;
+
+ // sz = get_jtagisp_return_size(data[0]);
+ // if (sz == 0) {
+ // avrdude_message(MSG_INFO, "%s: unsupported encapsulated ISP command: %#x\n",
+ // progname, data[0]);
+ // return -1;
+ // }
+ // if (sz == SZ_READ_FLASH_EE) {
+ // /*
+ // * For CMND_READ_FLASH_ISP and CMND_READ_EEPROM_ISP, extract the
+ // * size of the return data from the request. Note that the
+ // * request itself has the size in big endian format, while we are
+ // * supposed to deliver it in little endian.
+ // */
+ // sz = 3 + (data[1] << 8) + data[2];
+ // } else if (sz == SZ_SPI_MULTI) {
+ // /*
+ // * CMND_SPI_MULTI has the Rx size encoded in its 3rd byte.
+ // */
+ // sz = 3 + data[2];
+ // }
+
+ // if ((cmdbuf = malloc(len + 3)) == NULL) {
+ // avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
+ // progname);
+ // exit(1);
+ // }
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // cmdbuf[0] = CMND_ISP_PACKET;
+ // cmdbuf[1] = sz & 0xff;
+ // cmdbuf[2] = (sz >> 8) & 0xff;
+ // memcpy(cmdbuf + 3, data, len);
+ // rv = jtagmkII_send(pgm, cmdbuf, len + 3);
+ // free(cmdbuf);
+ // pgm->cookie = mycookie;
+
+ // return rv;
}
/*
@@ -463,24 +467,25 @@ static int stk500v2_jtagmkII_send(PROGRAMMER * pgm, unsigned char * data, size_t
*/
static int stk500v2_jtag3_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
- unsigned char *cmdbuf;
- int rv;
- void *mycookie;
-
- if ((cmdbuf = malloc(len + 1)) == NULL) {
- avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
- progname);
- exit(1);
- }
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- cmdbuf[0] = SCOPE_AVR_ISP;
- memcpy(cmdbuf + 1, data, len);
- rv = jtag3_send(pgm, cmdbuf, len + 1);
- free(cmdbuf);
- pgm->cookie = mycookie;
-
- return rv;
+ return -1;
+ // unsigned char *cmdbuf;
+ // int rv;
+ // void *mycookie;
+
+ // if ((cmdbuf = malloc(len + 1)) == NULL) {
+ // avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
+ // progname);
+ // exit(1);
+ // }
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // cmdbuf[0] = SCOPE_AVR_ISP;
+ // memcpy(cmdbuf + 1, data, len);
+ // rv = jtag3_send(pgm, cmdbuf, len + 1);
+ // free(cmdbuf);
+ // pgm->cookie = mycookie;
+
+ // return rv;
}
static int stk500v2_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
@@ -543,78 +548,80 @@ static int stk500v2_recv_mk2(PROGRAMMER * pgm, unsigned char *msg,
static int stk500v2_jtagmkII_recv(PROGRAMMER * pgm, unsigned char *msg,
size_t maxsize)
{
- int rv;
- unsigned char *jtagmsg;
- void *mycookie;
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- rv = jtagmkII_recv(pgm, &jtagmsg);
- pgm->cookie = mycookie;
- if (rv <= 0) {
- avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): error in jtagmkII_recv()\n",
- progname);
- return -1;
- }
- if (rv - 1 > maxsize) {
- avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): got %u bytes, have only room for %u bytes\n",
- progname, (unsigned)rv - 1, (unsigned)maxsize);
- rv = maxsize;
- }
- switch (jtagmsg[0]) {
- case RSP_SPI_DATA:
- break;
- case RSP_FAILED:
- avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): failed\n",
- progname);
- return -1;
- case RSP_ILLEGAL_MCU_STATE:
- avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): illegal MCU state\n",
- progname);
- return -1;
- default:
- avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): unknown status %d\n",
- progname, jtagmsg[0]);
- return -1;
- }
- memcpy(msg, jtagmsg + 1, rv - 1);
- return rv;
+ return -1;
+ // int rv;
+ // unsigned char *jtagmsg;
+ // void *mycookie;
+
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // rv = jtagmkII_recv(pgm, &jtagmsg);
+ // pgm->cookie = mycookie;
+ // if (rv <= 0) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): error in jtagmkII_recv()\n",
+ // progname);
+ // return -1;
+ // }
+ // if (rv - 1 > maxsize) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): got %u bytes, have only room for %u bytes\n",
+ // progname, (unsigned)rv - 1, (unsigned)maxsize);
+ // rv = maxsize;
+ // }
+ // switch (jtagmsg[0]) {
+ // case RSP_SPI_DATA:
+ // break;
+ // case RSP_FAILED:
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): failed\n",
+ // progname);
+ // return -1;
+ // case RSP_ILLEGAL_MCU_STATE:
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): illegal MCU state\n",
+ // progname);
+ // return -1;
+ // default:
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): unknown status %d\n",
+ // progname, jtagmsg[0]);
+ // return -1;
+ // }
+ // memcpy(msg, jtagmsg + 1, rv - 1);
+ // return rv;
}
static int stk500v2_jtag3_recv(PROGRAMMER * pgm, unsigned char *msg,
size_t maxsize)
{
- int rv;
- unsigned char *jtagmsg;
- void *mycookie;
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- rv = jtag3_recv(pgm, &jtagmsg);
- pgm->cookie = mycookie;
- if (rv <= 0) {
- avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): error in jtagmkII_recv()\n",
- progname);
- return -1;
- }
- /* Getting more data than expected is a normal case for the EDBG
- implementation of JTAGICE3, as they always request a full 512
- octets from the ICE. Thus, only complain at high verbose
- levels. */
- if (rv - 1 > maxsize) {
- avrdude_message(MSG_DEBUG, "%s: stk500v2_jtag3_recv(): got %u bytes, have only room for %u bytes\n",
- progname, (unsigned)rv - 1, (unsigned)maxsize);
- rv = maxsize;
- }
- if (jtagmsg[0] != SCOPE_AVR_ISP) {
- avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): message is not AVR ISP: 0x%02x\n",
- progname, jtagmsg[0]);
- free(jtagmsg);
- return -1;
- }
- memcpy(msg, jtagmsg + 1, rv - 1);
- free(jtagmsg);
- return rv;
+ return -1;
+ // int rv;
+ // unsigned char *jtagmsg;
+ // void *mycookie;
+
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // rv = jtag3_recv(pgm, &jtagmsg);
+ // pgm->cookie = mycookie;
+ // if (rv <= 0) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): error in jtagmkII_recv()\n",
+ // progname);
+ // return -1;
+ // }
+ // /* Getting more data than expected is a normal case for the EDBG
+ // implementation of JTAGICE3, as they always request a full 512
+ // octets from the ICE. Thus, only complain at high verbose
+ // levels. */
+ // if (rv - 1 > maxsize) {
+ // avrdude_message(MSG_DEBUG, "%s: stk500v2_jtag3_recv(): got %u bytes, have only room for %u bytes\n",
+ // progname, (unsigned)rv - 1, (unsigned)maxsize);
+ // rv = maxsize;
+ // }
+ // if (jtagmsg[0] != SCOPE_AVR_ISP) {
+ // avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): message is not AVR ISP: 0x%02x\n",
+ // progname, jtagmsg[0]);
+ // free(jtagmsg);
+ // return -1;
+ // }
+ // memcpy(msg, jtagmsg + 1, rv - 1);
+ // free(jtagmsg);
+ // return rv;
}
static int stk500v2_recv(PROGRAMMER * pgm, unsigned char *msg, size_t maxsize) {
@@ -1147,51 +1154,52 @@ retry:
break;
case PGMTYPE_JTAGICE3:
- if (buf[1] == STATUS_CMD_FAILED &&
- (p->flags & AVRPART_HAS_DW) != 0) {
- void *mycookie;
- unsigned char cmd[4], *resp;
-
- /* Try debugWIRE, and MONCON_DISABLE */
- avrdude_message(MSG_NOTICE2, "%s: No response in ISP mode, trying debugWIRE\n",
- progname);
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
-
- cmd[0] = PARM3_CONN_DW;
- if (jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CONNECTION, cmd, 1) < 0) {
- pgm->cookie = mycookie;
- break;
- }
-
- cmd[0] = SCOPE_AVR;
-
- cmd[1] = CMD3_SIGN_ON;
- cmd[2] = cmd[3] = 0;
- if (jtag3_command(pgm, cmd, 4, &resp, "AVR sign-on") >= 0) {
- free(resp);
-
- cmd[1] = CMD3_START_DW_DEBUG;
- if (jtag3_command(pgm, cmd, 4, &resp, "start DW debug") >= 0) {
- free(resp);
-
- cmd[1] = CMD3_MONCON_DISABLE;
- if (jtag3_command(pgm, cmd, 3, &resp, "MonCon disable") >= 0)
- free(resp);
- }
- }
- pgm->cookie = mycookie;
- if (tries++ > 3) {
- avrdude_message(MSG_INFO, "%s: Failed to return from debugWIRE to ISP.\n",
- progname);
- break;
- }
- avrdude_message(MSG_INFO, "%s: Target prepared for ISP, signed off.\n"
- "%s: Now retrying without power-cycling the target.\n",
- progname, progname);
- goto retry;
- }
+ return -1;
+ // if (buf[1] == STATUS_CMD_FAILED &&
+ // (p->flags & AVRPART_HAS_DW) != 0) {
+ // void *mycookie;
+ // unsigned char cmd[4], *resp;
+
+ // /* Try debugWIRE, and MONCON_DISABLE */
+ // avrdude_message(MSG_NOTICE2, "%s: No response in ISP mode, trying debugWIRE\n",
+ // progname);
+
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+
+ // cmd[0] = PARM3_CONN_DW;
+ // if (jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CONNECTION, cmd, 1) < 0) {
+ // pgm->cookie = mycookie;
+ // break;
+ // }
+
+ // cmd[0] = SCOPE_AVR;
+
+ // cmd[1] = CMD3_SIGN_ON;
+ // cmd[2] = cmd[3] = 0;
+ // if (jtag3_command(pgm, cmd, 4, &resp, "AVR sign-on") >= 0) {
+ // free(resp);
+
+ // cmd[1] = CMD3_START_DW_DEBUG;
+ // if (jtag3_command(pgm, cmd, 4, &resp, "start DW debug") >= 0) {
+ // free(resp);
+
+ // cmd[1] = CMD3_MONCON_DISABLE;
+ // if (jtag3_command(pgm, cmd, 3, &resp, "MonCon disable") >= 0)
+ // free(resp);
+ // }
+ // }
+ // pgm->cookie = mycookie;
+ // if (tries++ > 3) {
+ // avrdude_message(MSG_INFO, "%s: Failed to return from debugWIRE to ISP.\n",
+ // progname);
+ // break;
+ // }
+ // avrdude_message(MSG_INFO, "%s: Target prepared for ISP, signed off.\n"
+ // "%s: Now retrying without power-cycling the target.\n",
+ // progname, progname);
+ // goto retry;
+ // }
break;
default:
@@ -1338,88 +1346,89 @@ static int stk500v2_initialize(PROGRAMMER * pgm, AVRPART * p)
*/
static int stk500v2_jtag3_initialize(PROGRAMMER * pgm, AVRPART * p)
{
- unsigned char parm[4], *resp;
- LNODEID ln;
- AVRMEM * m;
- void *mycookie;
-
- if ((p->flags & AVRPART_HAS_PDI) ||
- (p->flags & AVRPART_HAS_TPI)) {
- avrdude_message(MSG_INFO, "%s: jtag3_initialize(): part %s has no ISP interface\n",
- progname, p->desc);
- return -1;
- }
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
-
- if (p->flags & AVRPART_HAS_DW)
- parm[0] = PARM3_ARCH_TINY;
- else
- parm[0] = PARM3_ARCH_MEGA;
- if (jtag3_setparm(pgm, SCOPE_AVR, 0, PARM3_ARCH, parm, 1) < 0) {
- pgm->cookie = mycookie;
- return -1;
- }
-
- parm[0] = PARM3_SESS_PROGRAMMING;
- if (jtag3_setparm(pgm, SCOPE_AVR, 0, PARM3_SESS_PURPOSE, parm, 1) < 0) {
- pgm->cookie = mycookie;
- return -1;
- }
-
- parm[0] = PARM3_CONN_ISP;
- if (jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CONNECTION, parm, 1) < 0) {
- pgm->cookie = mycookie;
- return -1;
- }
-
- parm[0] = SCOPE_AVR_ISP;
- parm[1] = 0x1e;
- jtag3_send(pgm, parm, 2);
-
- if (jtag3_recv(pgm, &resp) > 0)
- free(resp);
-
- pgm->cookie = mycookie;
-
- /*
- * Examine the avrpart's memory definitions, and initialize the page
- * caches. For devices/memory that are not page oriented, treat
- * them as page size 1 for EEPROM, and 2 for flash.
- */
- PDATA(pgm)->flash_pagesize = 2;
- PDATA(pgm)->eeprom_pagesize = 1;
- for (ln = lfirst(p->mem); ln; ln = lnext(ln)) {
- m = ldata(ln);
- if (strcmp(m->desc, "flash") == 0) {
- if (m->page_size > 0) {
- if (m->page_size > 256)
- PDATA(pgm)->flash_pagesize = 256;
- else
- PDATA(pgm)->flash_pagesize = m->page_size;
- }
- } else if (strcmp(m->desc, "eeprom") == 0) {
- if (m->page_size > 0)
- PDATA(pgm)->eeprom_pagesize = m->page_size;
- }
- }
- free(PDATA(pgm)->flash_pagecache);
- free(PDATA(pgm)->eeprom_pagecache);
- if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) {
- avrdude_message(MSG_INFO, "%s: stk500hv_initialize(): Out of memory\n",
- progname);
- return -1;
- }
- if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) {
- avrdude_message(MSG_INFO, "%s: stk500hv_initialize(): Out of memory\n",
- progname);
- free(PDATA(pgm)->flash_pagecache);
- return -1;
- }
- PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L;
-
- return pgm->program_enable(pgm, p);
+ return -1;
+ // unsigned char parm[4], *resp;
+ // LNODEID ln;
+ // AVRMEM * m;
+ // void *mycookie;
+
+ // if ((p->flags & AVRPART_HAS_PDI) ||
+ // (p->flags & AVRPART_HAS_TPI)) {
+ // avrdude_message(MSG_INFO, "%s: jtag3_initialize(): part %s has no ISP interface\n",
+ // progname, p->desc);
+ // return -1;
+ // }
+
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+
+ // if (p->flags & AVRPART_HAS_DW)
+ // parm[0] = PARM3_ARCH_TINY;
+ // else
+ // parm[0] = PARM3_ARCH_MEGA;
+ // if (jtag3_setparm(pgm, SCOPE_AVR, 0, PARM3_ARCH, parm, 1) < 0) {
+ // pgm->cookie = mycookie;
+ // return -1;
+ // }
+
+ // parm[0] = PARM3_SESS_PROGRAMMING;
+ // if (jtag3_setparm(pgm, SCOPE_AVR, 0, PARM3_SESS_PURPOSE, parm, 1) < 0) {
+ // pgm->cookie = mycookie;
+ // return -1;
+ // }
+
+ // parm[0] = PARM3_CONN_ISP;
+ // if (jtag3_setparm(pgm, SCOPE_AVR, 1, PARM3_CONNECTION, parm, 1) < 0) {
+ // pgm->cookie = mycookie;
+ // return -1;
+ // }
+
+ // parm[0] = SCOPE_AVR_ISP;
+ // parm[1] = 0x1e;
+ // jtag3_send(pgm, parm, 2);
+
+ // if (jtag3_recv(pgm, &resp) > 0)
+ // free(resp);
+
+ // pgm->cookie = mycookie;
+
+ // /*
+ // * Examine the avrpart's memory definitions, and initialize the page
+ // * caches. For devices/memory that are not page oriented, treat
+ // * them as page size 1 for EEPROM, and 2 for flash.
+ // */
+ // PDATA(pgm)->flash_pagesize = 2;
+ // PDATA(pgm)->eeprom_pagesize = 1;
+ // for (ln = lfirst(p->mem); ln; ln = lnext(ln)) {
+ // m = ldata(ln);
+ // if (strcmp(m->desc, "flash") == 0) {
+ // if (m->page_size > 0) {
+ // if (m->page_size > 256)
+ // PDATA(pgm)->flash_pagesize = 256;
+ // else
+ // PDATA(pgm)->flash_pagesize = m->page_size;
+ // }
+ // } else if (strcmp(m->desc, "eeprom") == 0) {
+ // if (m->page_size > 0)
+ // PDATA(pgm)->eeprom_pagesize = m->page_size;
+ // }
+ // }
+ // free(PDATA(pgm)->flash_pagecache);
+ // free(PDATA(pgm)->eeprom_pagecache);
+ // if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) {
+ // avrdude_message(MSG_INFO, "%s: stk500hv_initialize(): Out of memory\n",
+ // progname);
+ // return -1;
+ // }
+ // if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) {
+ // avrdude_message(MSG_INFO, "%s: stk500hv_initialize(): Out of memory\n",
+ // progname);
+ // free(PDATA(pgm)->flash_pagecache);
+ // return -1;
+ // }
+ // PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L;
+
+ // return pgm->program_enable(pgm, p);
}
@@ -3269,19 +3278,21 @@ static void stk500v2_print_parms1(PROGRAMMER * pgm, const char * p)
void *mycookie;
if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE_MKII) {
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtagmkII_getparm(pgm, PAR_OCD_VTARGET, vtarget_jtag);
- pgm->cookie = mycookie;
- avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p,
- b2_to_u16(vtarget_jtag) / 1000.0);
+ return;
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtagmkII_getparm(pgm, PAR_OCD_VTARGET, vtarget_jtag);
+ // pgm->cookie = mycookie;
+ // avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p,
+ // b2_to_u16(vtarget_jtag) / 1000.0);
} else if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE3) {
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtag3_getparm(pgm, SCOPE_GENERAL, 1, PARM3_VTARGET, vtarget_jtag, 2);
- pgm->cookie = mycookie;
- avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p,
- b2_to_u16(vtarget_jtag) / 1000.0);
+ return;
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtag3_getparm(pgm, SCOPE_GENERAL, 1, PARM3_VTARGET, vtarget_jtag, 2);
+ // pgm->cookie = mycookie;
+ // avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p,
+ // b2_to_u16(vtarget_jtag) / 1000.0);
} else {
stk500v2_getparm(pgm, PARAM_VTARGET, &vtarget);
@@ -3401,71 +3412,72 @@ static int stk500v2_perform_osccal(PROGRAMMER * pgm)
*/
static int stk500v2_jtagmkII_open(PROGRAMMER * pgm, char * port)
{
- union pinfo pinfo;
- void *mycookie;
- int rv;
-
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtagmkII_open()\n", progname);
-
- /*
- * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
- * attaching. If the config file or command-line parameters specify
- * a higher baud rate, we switch to it later on, after establishing
- * the connection with the ICE.
- */
- pinfo.baud = 19200;
-
- /*
- * If the port name starts with "usb", divert the serial routines
- * to the USB ones. The serial_open() function for USB overrides
- * the meaning of the "baud" parameter to be the USB device ID to
- * search for.
- */
- if (strncmp(port, "usb", 3) == 0) {
-#if defined(HAVE_LIBUSB)
- serdev = &usb_serdev;
- pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
- pinfo.usbinfo.flags = 0;
- pinfo.usbinfo.pid = USB_DEVICE_JTAGICEMKII;
- pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
- pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
- pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
- pgm->fd.usb.eep = 0; /* no seperate EP for events */
-#else
- avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
- return -1;
-#endif
- }
-
- strcpy(pgm->port, port);
- if (serial_open(port, pinfo, &pgm->fd)==-1) {
- return -1;
- }
-
- /*
- * drain any extraneous input
- */
- stk500v2_drain(pgm, 0);
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- if ((rv = jtagmkII_getsync(pgm, EMULATOR_MODE_SPI)) != 0) {
- if (rv != JTAGII_GETSYNC_FAIL_GRACEFUL)
- avrdude_message(MSG_INFO, "%s: failed to sync with the JTAG ICE mkII in ISP mode\n",
- progname);
- pgm->cookie = mycookie;
- return -1;
- }
- pgm->cookie = mycookie;
-
- PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
-
- if (pgm->bitclock != 0.0) {
- if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
- return -1;
- }
-
- return 0;
+ return -1;
+// union pinfo pinfo;
+// void *mycookie;
+// int rv;
+
+// avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtagmkII_open()\n", progname);
+
+// /*
+// * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
+// * attaching. If the config file or command-line parameters specify
+// * a higher baud rate, we switch to it later on, after establishing
+// * the connection with the ICE.
+// */
+// pinfo.baud = 19200;
+
+// /*
+// * If the port name starts with "usb", divert the serial routines
+// * to the USB ones. The serial_open() function for USB overrides
+// * the meaning of the "baud" parameter to be the USB device ID to
+// * search for.
+// */
+// if (strncmp(port, "usb", 3) == 0) {
+// #if defined(HAVE_LIBUSB)
+// serdev = &usb_serdev;
+// pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
+// pinfo.usbinfo.flags = 0;
+// pinfo.usbinfo.pid = USB_DEVICE_JTAGICEMKII;
+// pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
+// pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
+// pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
+// pgm->fd.usb.eep = 0; /* no seperate EP for events */
+// #else
+// avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
+// return -1;
+// #endif
+// }
+
+// strcpy(pgm->port, port);
+// if (serial_open(port, pinfo, &pgm->fd)==-1) {
+// return -1;
+// }
+
+// /*
+// * drain any extraneous input
+// */
+// stk500v2_drain(pgm, 0);
+
+// mycookie = pgm->cookie;
+// pgm->cookie = PDATA(pgm)->chained_pdata;
+// if ((rv = jtagmkII_getsync(pgm, EMULATOR_MODE_SPI)) != 0) {
+// if (rv != JTAGII_GETSYNC_FAIL_GRACEFUL)
+// avrdude_message(MSG_INFO, "%s: failed to sync with the JTAG ICE mkII in ISP mode\n",
+// progname);
+// pgm->cookie = mycookie;
+// return -1;
+// }
+// pgm->cookie = mycookie;
+
+// PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
+
+// if (pgm->bitclock != 0.0) {
+// if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
+// return -1;
+// }
+
+// return 0;
}
@@ -3474,14 +3486,14 @@ static int stk500v2_jtagmkII_open(PROGRAMMER * pgm, char * port)
*/
static void stk500v2_jtagmkII_close(PROGRAMMER * pgm)
{
- void *mycookie;
+ // void *mycookie;
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtagmkII_close()\n", progname);
+ // avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtagmkII_close()\n", progname);
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtagmkII_close(pgm);
- pgm->cookie = mycookie;
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtagmkII_close(pgm);
+ // pgm->cookie = mycookie;
}
@@ -3490,14 +3502,14 @@ static void stk500v2_jtagmkII_close(PROGRAMMER * pgm)
*/
static void stk500v2_jtag3_close(PROGRAMMER * pgm)
{
- void *mycookie;
+ // void *mycookie;
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtag3_close()\n", progname);
+ // avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtag3_close()\n", progname);
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- jtag3_close(pgm);
- pgm->cookie = mycookie;
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // jtag3_close(pgm);
+ // pgm->cookie = mycookie;
}
@@ -3513,69 +3525,70 @@ static void stk500v2_jtag3_close(PROGRAMMER * pgm)
*/
static int stk500v2_dragon_isp_open(PROGRAMMER * pgm, char * port)
{
- union pinfo pinfo;
- void *mycookie;
-
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_dragon_isp_open()\n", progname);
-
- /*
- * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
- * attaching. If the config file or command-line parameters specify
- * a higher baud rate, we switch to it later on, after establishing
- * the connection with the ICE.
- */
- pinfo.baud = 19200;
-
- /*
- * If the port name starts with "usb", divert the serial routines
- * to the USB ones. The serial_open() function for USB overrides
- * the meaning of the "baud" parameter to be the USB device ID to
- * search for.
- */
- if (strncmp(port, "usb", 3) == 0) {
-#if defined(HAVE_LIBUSB)
- serdev = &usb_serdev;
- pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
- pinfo.usbinfo.flags = 0;
- pinfo.usbinfo.pid = USB_DEVICE_AVRDRAGON;
- pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
- pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
- pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
- pgm->fd.usb.eep = 0; /* no seperate EP for events */
-#else
- avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
- return -1;
-#endif
- }
-
- strcpy(pgm->port, port);
- if (serial_open(port, pinfo, &pgm->fd)==-1) {
- return -1;
- }
-
- /*
- * drain any extraneous input
- */
- stk500v2_drain(pgm, 0);
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- if (jtagmkII_getsync(pgm, EMULATOR_MODE_SPI) != 0) {
- avrdude_message(MSG_INFO, "%s: failed to sync with the AVR Dragon in ISP mode\n",
- progname);
- pgm->cookie = mycookie;
- return -1;
- }
- pgm->cookie = mycookie;
-
- PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
-
- if (pgm->bitclock != 0.0) {
- if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
- return -1;
- }
-
- return 0;
+ return -1;
+// union pinfo pinfo;
+// void *mycookie;
+
+// avrdude_message(MSG_NOTICE2, "%s: stk500v2_dragon_isp_open()\n", progname);
+
+// /*
+// * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
+// * attaching. If the config file or command-line parameters specify
+// * a higher baud rate, we switch to it later on, after establishing
+// * the connection with the ICE.
+// */
+// pinfo.baud = 19200;
+
+// /*
+// * If the port name starts with "usb", divert the serial routines
+// * to the USB ones. The serial_open() function for USB overrides
+// * the meaning of the "baud" parameter to be the USB device ID to
+// * search for.
+// */
+// if (strncmp(port, "usb", 3) == 0) {
+// #if defined(HAVE_LIBUSB)
+// serdev = &usb_serdev;
+// pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
+// pinfo.usbinfo.flags = 0;
+// pinfo.usbinfo.pid = USB_DEVICE_AVRDRAGON;
+// pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
+// pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
+// pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
+// pgm->fd.usb.eep = 0; /* no seperate EP for events */
+// #else
+// avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
+// return -1;
+// #endif
+// }
+
+// strcpy(pgm->port, port);
+// if (serial_open(port, pinfo, &pgm->fd)==-1) {
+// return -1;
+// }
+
+// /*
+// * drain any extraneous input
+// */
+// stk500v2_drain(pgm, 0);
+
+// mycookie = pgm->cookie;
+// pgm->cookie = PDATA(pgm)->chained_pdata;
+// if (jtagmkII_getsync(pgm, EMULATOR_MODE_SPI) != 0) {
+// avrdude_message(MSG_INFO, "%s: failed to sync with the AVR Dragon in ISP mode\n",
+// progname);
+// pgm->cookie = mycookie;
+// return -1;
+// }
+// pgm->cookie = mycookie;
+
+// PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
+
+// if (pgm->bitclock != 0.0) {
+// if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
+// return -1;
+// }
+
+// return 0;
}
@@ -3591,69 +3604,70 @@ static int stk500v2_dragon_isp_open(PROGRAMMER * pgm, char * port)
*/
static int stk500v2_dragon_hv_open(PROGRAMMER * pgm, char * port)
{
- union pinfo pinfo;
- void *mycookie;
-
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_dragon_hv_open()\n", progname);
-
- /*
- * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
- * attaching. If the config file or command-line parameters specify
- * a higher baud rate, we switch to it later on, after establishing
- * the connection with the ICE.
- */
- pinfo.baud = 19200;
-
- /*
- * If the port name starts with "usb", divert the serial routines
- * to the USB ones. The serial_open() function for USB overrides
- * the meaning of the "baud" parameter to be the USB device ID to
- * search for.
- */
- if (strncmp(port, "usb", 3) == 0) {
-#if defined(HAVE_LIBUSB)
- serdev = &usb_serdev;
- pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
- pinfo.usbinfo.flags = 0;
- pinfo.usbinfo.pid = USB_DEVICE_AVRDRAGON;
- pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
- pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
- pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
- pgm->fd.usb.eep = 0; /* no seperate EP for events */
-#else
- avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
- return -1;
-#endif
- }
-
- strcpy(pgm->port, port);
- if (serial_open(port, pinfo, &pgm->fd)==-1) {
- return -1;
- }
-
- /*
- * drain any extraneous input
- */
- stk500v2_drain(pgm, 0);
-
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- if (jtagmkII_getsync(pgm, EMULATOR_MODE_HV) != 0) {
- avrdude_message(MSG_INFO, "%s: failed to sync with the AVR Dragon in HV mode\n",
- progname);
- pgm->cookie = mycookie;
- return -1;
- }
- pgm->cookie = mycookie;
-
- PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
-
- if (pgm->bitclock != 0.0) {
- if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
- return -1;
- }
-
- return 0;
+ return -1;
+// union pinfo pinfo;
+// void *mycookie;
+
+// avrdude_message(MSG_NOTICE2, "%s: stk500v2_dragon_hv_open()\n", progname);
+
+// /*
+// * The JTAG ICE mkII always starts with a baud rate of 19200 Bd upon
+// * attaching. If the config file or command-line parameters specify
+// * a higher baud rate, we switch to it later on, after establishing
+// * the connection with the ICE.
+// */
+// pinfo.baud = 19200;
+
+// /*
+// * If the port name starts with "usb", divert the serial routines
+// * to the USB ones. The serial_open() function for USB overrides
+// * the meaning of the "baud" parameter to be the USB device ID to
+// * search for.
+// */
+// if (strncmp(port, "usb", 3) == 0) {
+// #if defined(HAVE_LIBUSB)
+// serdev = &usb_serdev;
+// pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
+// pinfo.usbinfo.flags = 0;
+// pinfo.usbinfo.pid = USB_DEVICE_AVRDRAGON;
+// pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_MKII;
+// pgm->fd.usb.rep = USBDEV_BULK_EP_READ_MKII;
+// pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_MKII;
+// pgm->fd.usb.eep = 0; /* no seperate EP for events */
+// #else
+// avrdude_message(MSG_INFO, "avrdude was compiled without usb support.\n");
+// return -1;
+// #endif
+// }
+
+// strcpy(pgm->port, port);
+// if (serial_open(port, pinfo, &pgm->fd)==-1) {
+// return -1;
+// }
+
+// /*
+// * drain any extraneous input
+// */
+// stk500v2_drain(pgm, 0);
+
+// mycookie = pgm->cookie;
+// pgm->cookie = PDATA(pgm)->chained_pdata;
+// if (jtagmkII_getsync(pgm, EMULATOR_MODE_HV) != 0) {
+// avrdude_message(MSG_INFO, "%s: failed to sync with the AVR Dragon in HV mode\n",
+// progname);
+// pgm->cookie = mycookie;
+// return -1;
+// }
+// pgm->cookie = mycookie;
+
+// PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE_MKII;
+
+// if (pgm->bitclock != 0.0) {
+// if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
+// return -1;
+// }
+
+// return 0;
}
/*
@@ -3668,33 +3682,34 @@ static int stk500v2_dragon_hv_open(PROGRAMMER * pgm, char * port)
*/
static int stk500v2_jtag3_open(PROGRAMMER * pgm, char * port)
{
- void *mycookie;
- int rv;
+ return -1;
+ // void *mycookie;
+ // int rv;
- avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtag3_open()\n", progname);
+ // avrdude_message(MSG_NOTICE2, "%s: stk500v2_jtag3_open()\n", progname);
- if (jtag3_open_common(pgm, port) < 0)
- return -1;
+ // if (jtag3_open_common(pgm, port) < 0)
+ // return -1;
- mycookie = pgm->cookie;
- pgm->cookie = PDATA(pgm)->chained_pdata;
- if ((rv = jtag3_getsync(pgm, 42)) != 0) {
- if (rv != JTAGII_GETSYNC_FAIL_GRACEFUL)
- avrdude_message(MSG_INFO, "%s: failed to sync with the JTAGICE3 in ISP mode\n",
- progname);
- pgm->cookie = mycookie;
- return -1;
- }
- pgm->cookie = mycookie;
+ // mycookie = pgm->cookie;
+ // pgm->cookie = PDATA(pgm)->chained_pdata;
+ // if ((rv = jtag3_getsync(pgm, 42)) != 0) {
+ // if (rv != JTAGII_GETSYNC_FAIL_GRACEFUL)
+ // avrdude_message(MSG_INFO, "%s: failed to sync with the JTAGICE3 in ISP mode\n",
+ // progname);
+ // pgm->cookie = mycookie;
+ // return -1;
+ // }
+ // pgm->cookie = mycookie;
- PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE3;
+ // PDATA(pgm)->pgmtype = PGMTYPE_JTAGICE3;
- if (pgm->bitclock != 0.0) {
- if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
- return -1;
- }
+ // if (pgm->bitclock != 0.0) {
+ // if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)
+ // return -1;
+ // }
- return 0;
+ // return 0;
}
diff --git a/xs/src/avrdude/windows/getopt.c b/xs/src/avrdude/windows/getopt.c
new file mode 100644
index 000000000..cfa302335
--- /dev/null
+++ b/xs/src/avrdude/windows/getopt.c
@@ -0,0 +1,1258 @@
+/* Getopt for GNU.
+ NOTE: getopt is now part of the C library, so if you don't know what
+ "Keep this file name-space clean" means, talk to drepper@gnu.org
+ before changing it!
+ Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
+ Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
+ Ditto for AIX 3.2 and <stdlib.h>. */
+#ifndef _NO_PROTO
+# define _NO_PROTO
+#endif
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if !defined __STDC__ || !__STDC__
+/* This is a separate conditional since some stdc systems
+ reject `defined (const)'. */
+# ifndef const
+# define const
+# endif
+#endif
+
+#include <stdio.h>
+
+/* Comment out all this code if we are using the GNU C Library, and are not
+ actually compiling the library itself. This code is part of the GNU C
+ Library, but also included in many other GNU distributions. Compiling
+ and linking in this code is a waste when using the GNU C library
+ (especially if it is a shared library). Rather than having every GNU
+ program understand `configure --with-gnu-libc' and omit the object files,
+ it is simpler to just do this in the source for each such file. */
+
+#define GETOPT_INTERFACE_VERSION 2
+#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
+# include <gnu-versions.h>
+# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
+# define ELIDE_CODE
+# endif
+#endif
+
+#ifndef ELIDE_CODE
+
+
+/* This needs to come after some library #include
+ to get __GNU_LIBRARY__ defined. */
+#ifdef __GNU_LIBRARY__
+/* Don't include stdlib.h for non-GNU C libraries because some of them
+ contain conflicting prototypes for getopt. */
+# include <stdlib.h>
+# include <unistd.h>
+#endif /* GNU C library. */
+
+#ifdef VMS
+# include <unixlib.h>
+# if HAVE_STRING_H - 0
+# include <string.h>
+# endif
+#endif
+
+#ifndef _
+/* This is for other GNU distributions with internationalized messages. */
+# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
+# include <libintl.h>
+# ifndef _
+# define _(msgid) gettext (msgid)
+# endif
+# else
+# define _(msgid) (msgid)
+# endif
+# if defined _LIBC && defined USE_IN_LIBIO
+# include <wchar.h>
+# endif
+#endif
+
+/* This version of `getopt' appears to the caller like standard Unix `getopt'
+ but it behaves differently for the user, since it allows the user
+ to intersperse the options with the other arguments.
+
+ As `getopt' works, it permutes the elements of ARGV so that,
+ when it is done, all the options precede everything else. Thus
+ all application programs are extended to handle flexible argument order.
+
+ Setting the environment variable POSIXLY_CORRECT disables permutation.
+ Then the behavior is completely standard.
+
+ GNU application programs can use a third alternative mode in which
+ they can distinguish the relative order of options and other arguments. */
+
+#include "getopt.h"
+
+/* For communication from `getopt' to the caller.
+ When `getopt' finds an option that takes an argument,
+ the argument value is returned here.
+ Also, when `ordering' is RETURN_IN_ORDER,
+ each non-option ARGV-element is returned here. */
+
+char *optarg;
+
+/* Index in ARGV of the next element to be scanned.
+ This is used for communication to and from the caller
+ and for communication between successive calls to `getopt'.
+
+ On entry to `getopt', zero means this is the first call; initialize.
+
+ When `getopt' returns -1, this is the index of the first of the
+ non-option elements that the caller should itself scan.
+
+ Otherwise, `optind' communicates from one call to the next
+ how much of ARGV has been scanned so far. */
+
+/* 1003.2 says this must be 1 before any call. */
+int optind = 1;
+
+/* Formerly, initialization of getopt depended on optind==0, which
+ causes problems with re-calling getopt as programs generally don't
+ know that. */
+
+int __getopt_initialized;
+
+/* The next char to be scanned in the option-element
+ in which the last option character we returned was found.
+ This allows us to pick up the scan where we left off.
+
+ If this is zero, or a null string, it means resume the scan
+ by advancing to the next ARGV-element. */
+
+static char *nextchar;
+
+/* Callers store zero here to inhibit the error message
+ for unrecognized options. */
+
+int opterr = 1;
+
+/* Set to an option character which was unrecognized.
+ This must be initialized on some systems to avoid linking in the
+ system's own getopt implementation. */
+
+int optopt = '?';
+
+/* Describe how to deal with options that follow non-option ARGV-elements.
+
+ If the caller did not specify anything,
+ the default is REQUIRE_ORDER if the environment variable
+ POSIXLY_CORRECT is defined, PERMUTE otherwise.
+
+ REQUIRE_ORDER means don't recognize them as options;
+ stop option processing when the first non-option is seen.
+ This is what Unix does.
+ This mode of operation is selected by either setting the environment
+ variable POSIXLY_CORRECT, or using `+' as the first character
+ of the list of option characters.
+
+ PERMUTE is the default. We permute the contents of ARGV as we scan,
+ so that eventually all the non-options are at the end. This allows options
+ to be given in any order, even with programs that were not written to
+ expect this.
+
+ RETURN_IN_ORDER is an option available to programs that were written
+ to expect options and other ARGV-elements in any order and that care about
+ the ordering of the two. We describe each non-option ARGV-element
+ as if it were the argument of an option with character code 1.
+ Using `-' as the first character of the list of option characters
+ selects this mode of operation.
+
+ The special argument `--' forces an end of option-scanning regardless
+ of the value of `ordering'. In the case of RETURN_IN_ORDER, only
+ `--' can cause `getopt' to return -1 with `optind' != ARGC. */
+
+static enum
+{
+ REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
+} ordering;
+
+/* Value of POSIXLY_CORRECT environment variable. */
+static char *posixly_correct;
+
+#ifdef __GNU_LIBRARY__
+/* We want to avoid inclusion of string.h with non-GNU libraries
+ because there are many ways it can cause trouble.
+ On some systems, it contains special magic macros that don't work
+ in GCC. */
+# include <string.h>
+# define my_index strchr
+#else
+
+# if HAVE_STRING_H || WIN32 /* Pete Wilson mod 7/28/02 */
+# include <string.h>
+# else
+# include <strings.h>
+# endif
+
+/* Avoid depending on library functions or files
+ whose names are inconsistent. */
+
+#ifndef getenv
+extern char *getenv ();
+#endif
+
+static char *
+my_index (str, chr)
+ const char *str;
+ int chr;
+{
+ while (*str)
+ {
+ if (*str == chr)
+ return (char *) str;
+ str++;
+ }
+ return 0;
+}
+
+/* If using GCC, we can safely declare strlen this way.
+ If not using GCC, it is ok not to declare it. */
+#ifdef __GNUC__
+/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
+ That was relevant to code that was here before. */
+# if (!defined __STDC__ || !__STDC__) && !defined strlen
+/* gcc with -traditional declares the built-in strlen to return int,
+ and has done so at least since version 2.4.5. -- rms. */
+extern int strlen (const char *);
+# endif /* not __STDC__ */
+#endif /* __GNUC__ */
+
+#endif /* not __GNU_LIBRARY__ */
+
+/* Handle permutation of arguments. */
+
+/* Describe the part of ARGV that contains non-options that have
+ been skipped. `first_nonopt' is the index in ARGV of the first of them;
+ `last_nonopt' is the index after the last of them. */
+
+static int first_nonopt;
+static int last_nonopt;
+
+#ifdef _LIBC
+/* Stored original parameters.
+ XXX This is no good solution. We should rather copy the args so
+ that we can compare them later. But we must not use malloc(3). */
+extern int __libc_argc;
+extern char **__libc_argv;
+
+/* Bash 2.0 gives us an environment variable containing flags
+ indicating ARGV elements that should not be considered arguments. */
+
+# ifdef USE_NONOPTION_FLAGS
+/* Defined in getopt_init.c */
+extern char *__getopt_nonoption_flags;
+
+static int nonoption_flags_max_len;
+static int nonoption_flags_len;
+# endif
+
+# ifdef USE_NONOPTION_FLAGS
+# define SWAP_FLAGS(ch1, ch2) \
+ if (nonoption_flags_len > 0) \
+ { \
+ char __tmp = __getopt_nonoption_flags[ch1]; \
+ __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
+ __getopt_nonoption_flags[ch2] = __tmp; \
+ }
+# else
+# define SWAP_FLAGS(ch1, ch2)
+# endif
+#else /* !_LIBC */
+# define SWAP_FLAGS(ch1, ch2)
+#endif /* _LIBC */
+
+/* Exchange two adjacent subsequences of ARGV.
+ One subsequence is elements [first_nonopt,last_nonopt)
+ which contains all the non-options that have been skipped so far.
+ The other is elements [last_nonopt,optind), which contains all
+ the options processed since those non-options were skipped.
+
+ `first_nonopt' and `last_nonopt' are relocated so that they describe
+ the new indices of the non-options in ARGV after they are moved. */
+
+#if defined __STDC__ && __STDC__
+static void exchange (char **);
+#endif
+
+static void
+exchange (argv)
+ char **argv;
+{
+ int bottom = first_nonopt;
+ int middle = last_nonopt;
+ int top = optind;
+ char *tem;
+
+ /* Exchange the shorter segment with the far end of the longer segment.
+ That puts the shorter segment into the right place.
+ It leaves the longer segment in the right place overall,
+ but it consists of two parts that need to be swapped next. */
+
+#if defined _LIBC && defined USE_NONOPTION_FLAGS
+ /* First make sure the handling of the `__getopt_nonoption_flags'
+ string can work normally. Our top argument must be in the range
+ of the string. */
+ if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
+ {
+ /* We must extend the array. The user plays games with us and
+ presents new arguments. */
+ char *new_str = malloc (top + 1);
+ if (new_str == NULL)
+ nonoption_flags_len = nonoption_flags_max_len = 0;
+ else
+ {
+ memset (__mempcpy (new_str, __getopt_nonoption_flags,
+ nonoption_flags_max_len),
+ '\0', top + 1 - nonoption_flags_max_len);
+ nonoption_flags_max_len = top + 1;
+ __getopt_nonoption_flags = new_str;
+ }
+ }
+#endif
+
+ while (top > middle && middle > bottom)
+ {
+ if (top - middle > middle - bottom)
+ {
+ /* Bottom segment is the short one. */
+ int len = middle - bottom;
+ register int i;
+
+ /* Swap it with the top part of the top segment. */
+ for (i = 0; i < len; i++)
+ {
+ tem = argv[bottom + i];
+ argv[bottom + i] = argv[top - (middle - bottom) + i];
+ argv[top - (middle - bottom) + i] = tem;
+ SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
+ }
+ /* Exclude the moved bottom segment from further swapping. */
+ top -= len;
+ }
+ else
+ {
+ /* Top segment is the short one. */
+ int len = top - middle;
+ register int i;
+
+ /* Swap it with the bottom part of the bottom segment. */
+ for (i = 0; i < len; i++)
+ {
+ tem = argv[bottom + i];
+ argv[bottom + i] = argv[middle + i];
+ argv[middle + i] = tem;
+ SWAP_FLAGS (bottom + i, middle + i);
+ }
+ /* Exclude the moved top segment from further swapping. */
+ bottom += len;
+ }
+ }
+
+ /* Update records for the slots the non-options now occupy. */
+
+ first_nonopt += (optind - last_nonopt);
+ last_nonopt = optind;
+}
+
+/* Initialize the internal data when the first call is made. */
+
+#if defined __STDC__ && __STDC__
+static const char *_getopt_initialize (int, char *const *, const char *);
+#endif
+static const char *
+_getopt_initialize (argc, argv, optstring)
+ int argc;
+ char *const *argv;
+ const char *optstring;
+{
+ /* Start processing options with ARGV-element 1 (since ARGV-element 0
+ is the program name); the sequence of previously skipped
+ non-option ARGV-elements is empty. */
+
+ first_nonopt = last_nonopt = optind;
+
+ nextchar = NULL;
+
+ posixly_correct = getenv ("POSIXLY_CORRECT");
+
+ /* Determine how to handle the ordering of options and nonoptions. */
+
+ if (optstring[0] == '-')
+ {
+ ordering = RETURN_IN_ORDER;
+ ++optstring;
+ }
+ else if (optstring[0] == '+')
+ {
+ ordering = REQUIRE_ORDER;
+ ++optstring;
+ }
+ else if (posixly_correct != NULL)
+ ordering = REQUIRE_ORDER;
+ else
+ ordering = PERMUTE;
+
+#if defined _LIBC && defined USE_NONOPTION_FLAGS
+ if (posixly_correct == NULL
+ && argc == __libc_argc && argv == __libc_argv)
+ {
+ if (nonoption_flags_max_len == 0)
+ {
+ if (__getopt_nonoption_flags == NULL
+ || __getopt_nonoption_flags[0] == '\0')
+ nonoption_flags_max_len = -1;
+ else
+ {
+ const char *orig_str = __getopt_nonoption_flags;
+ int len = nonoption_flags_max_len = strlen (orig_str);
+ if (nonoption_flags_max_len < argc)
+ nonoption_flags_max_len = argc;
+ __getopt_nonoption_flags =
+ (char *) malloc (nonoption_flags_max_len);
+ if (__getopt_nonoption_flags == NULL)
+ nonoption_flags_max_len = -1;
+ else
+ memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
+ '\0', nonoption_flags_max_len - len);
+ }
+ }
+ nonoption_flags_len = nonoption_flags_max_len;
+ }
+ else
+ nonoption_flags_len = 0;
+#endif
+
+ return optstring;
+}
+
+/* Scan elements of ARGV (whose length is ARGC) for option characters
+ given in OPTSTRING.
+
+ If an element of ARGV starts with '-', and is not exactly "-" or "--",
+ then it is an option element. The characters of this element
+ (aside from the initial '-') are option characters. If `getopt'
+ is called repeatedly, it returns successively each of the option characters
+ from each of the option elements.
+
+ If `getopt' finds another option character, it returns that character,
+ updating `optind' and `nextchar' so that the next call to `getopt' can
+ resume the scan with the following option character or ARGV-element.
+
+ If there are no more option characters, `getopt' returns -1.
+ Then `optind' is the index in ARGV of the first ARGV-element
+ that is not an option. (The ARGV-elements have been permuted
+ so that those that are not options now come last.)
+
+ OPTSTRING is a string containing the legitimate option characters.
+ If an option character is seen that is not listed in OPTSTRING,
+ return '?' after printing an error message. If you set `opterr' to
+ zero, the error message is suppressed but we still return '?'.
+
+ If a char in OPTSTRING is followed by a colon, that means it wants an arg,
+ so the following text in the same ARGV-element, or the text of the following
+ ARGV-element, is returned in `optarg'. Two colons mean an option that
+ wants an optional arg; if there is text in the current ARGV-element,
+ it is returned in `optarg', otherwise `optarg' is set to zero.
+
+ If OPTSTRING starts with `-' or `+', it requests different methods of
+ handling the non-option ARGV-elements.
+ See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
+
+ Long-named options begin with `--' instead of `-'.
+ Their names may be abbreviated as long as the abbreviation is unique
+ or is an exact match for some defined option. If they have an
+ argument, it follows the option name in the same ARGV-element, separated
+ from the option name by a `=', or else the in next ARGV-element.
+ When `getopt' finds a long-named option, it returns 0 if that option's
+ `flag' field is nonzero, the value of the option's `val' field
+ if the `flag' field is zero.
+
+ The elements of ARGV aren't really const, because we permute them.
+ But we pretend they're const in the prototype to be compatible
+ with other systems.
+
+ LONGOPTS is a vector of `struct option' terminated by an
+ element containing a name which is zero.
+
+ LONGIND returns the index in LONGOPT of the long-named option found.
+ It is only valid when a long-named option has been found by the most
+ recent call.
+
+ If LONG_ONLY is nonzero, '-' as well as '--' can introduce
+ long-named options. */
+
+int
+_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
+ int argc;
+ char *const *argv;
+ const char *optstring;
+ const struct option *longopts;
+ int *longind;
+ int long_only;
+{
+ int print_errors = opterr;
+ if (optstring[0] == ':')
+ print_errors = 0;
+
+ if (argc < 1)
+ return -1;
+
+ optarg = NULL;
+
+ if (optind == 0 || !__getopt_initialized)
+ {
+ if (optind == 0)
+ optind = 1; /* Don't scan ARGV[0], the program name. */
+ optstring = _getopt_initialize (argc, argv, optstring);
+ __getopt_initialized = 1;
+ }
+
+ /* Test whether ARGV[optind] points to a non-option argument.
+ Either it does not have option syntax, or there is an environment flag
+ from the shell indicating it is not an option. The later information
+ is only used when the used in the GNU libc. */
+#if defined _LIBC && defined USE_NONOPTION_FLAGS
+# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
+ || (optind < nonoption_flags_len \
+ && __getopt_nonoption_flags[optind] == '1'))
+#else
+# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
+#endif
+
+ if (nextchar == NULL || *nextchar == '\0')
+ {
+ /* Advance to the next ARGV-element. */
+
+ /* Give FIRST_NONOPT and LAST_NONOPT rational values if OPTIND has been
+ moved back by the user (who may also have changed the arguments). */
+ if (last_nonopt > optind)
+ last_nonopt = optind;
+ if (first_nonopt > optind)
+ first_nonopt = optind;
+
+ if (ordering == PERMUTE)
+ {
+ /* If we have just processed some options following some non-options,
+ exchange them so that the options come first. */
+
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
+ exchange ((char **) argv);
+ else if (last_nonopt != optind)
+ first_nonopt = optind;
+
+ /* Skip any additional non-options
+ and extend the range of non-options previously skipped. */
+
+ while (optind < argc && NONOPTION_P)
+ optind++;
+ last_nonopt = optind;
+ }
+
+ /* The special ARGV-element `--' means premature end of options.
+ Skip it like a null option,
+ then exchange with previous non-options as if it were an option,
+ then skip everything else like a non-option. */
+
+ if (optind != argc && !strcmp (argv[optind], "--"))
+ {
+ optind++;
+
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
+ exchange ((char **) argv);
+ else if (first_nonopt == last_nonopt)
+ first_nonopt = optind;
+ last_nonopt = argc;
+
+ optind = argc;
+ }
+
+ /* If we have done all the ARGV-elements, stop the scan
+ and back over any non-options that we skipped and permuted. */
+
+ if (optind == argc)
+ {
+ /* Set the next-arg-index to point at the non-options
+ that we previously skipped, so the caller will digest them. */
+ if (first_nonopt != last_nonopt)
+ optind = first_nonopt;
+ return -1;
+ }
+
+ /* If we have come to a non-option and did not permute it,
+ either stop the scan or describe it to the caller and pass it by. */
+
+ if (NONOPTION_P)
+ {
+ if (ordering == REQUIRE_ORDER)
+ return -1;
+ optarg = argv[optind++];
+ return 1;
+ }
+
+ /* We have found another option-ARGV-element.
+ Skip the initial punctuation. */
+
+ nextchar = (argv[optind] + 1
+ + (longopts != NULL && argv[optind][1] == '-'));
+ }
+
+ /* Decode the current option-ARGV-element. */
+
+ /* Check whether the ARGV-element is a long option.
+
+ If long_only and the ARGV-element has the form "-f", where f is
+ a valid short option, don't consider it an abbreviated form of
+ a long option that starts with f. Otherwise there would be no
+ way to give the -f short option.
+
+ On the other hand, if there's a long option "fubar" and
+ the ARGV-element is "-fu", do consider that an abbreviation of
+ the long option, just like "--fu", and not "-f" with arg "u".
+
+ This distinction seems to be the most useful approach. */
+
+ if (longopts != NULL
+ && (argv[optind][1] == '-'
+ || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
+ {
+ char *nameend;
+ const struct option *p;
+ const struct option *pfound = NULL;
+ int exact = 0;
+ int ambig = 0;
+ int indfound = -1;
+ int option_index;
+
+ for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
+ /* Do nothing. */ ;
+
+ /* Test all long options for either exact match
+ or abbreviated matches. */
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
+ if (!strncmp (p->name, nextchar, nameend - nextchar))
+ {
+ if ((unsigned int) (nameend - nextchar)
+ == (unsigned int) strlen (p->name))
+ {
+ /* Exact match found. */
+ pfound = p;
+ indfound = option_index;
+ exact = 1;
+ break;
+ }
+ else if (pfound == NULL)
+ {
+ /* First nonexact match found. */
+ pfound = p;
+ indfound = option_index;
+ }
+ else if (long_only
+ || pfound->has_arg != p->has_arg
+ || pfound->flag != p->flag
+ || pfound->val != p->val)
+ /* Second or later nonexact match found. */
+ ambig = 1;
+ }
+
+ if (ambig && !exact)
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
+ argv[0], argv[optind]);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
+ argv[0], argv[optind]);
+#endif
+ }
+ nextchar += strlen (nextchar);
+ optind++;
+ optopt = 0;
+ return '?';
+ }
+
+ if (pfound != NULL)
+ {
+ option_index = indfound;
+ optind++;
+ if (*nameend)
+ {
+ /* Don't test has_arg with >, because some C compilers don't
+ allow it to be used on enums. */
+ if (pfound->has_arg)
+ optarg = nameend + 1;
+ else
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+#endif
+
+ if (argv[optind - 1][1] == '-')
+ {
+ /* --option */
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("\
+%s: option `--%s' doesn't allow an argument\n"),
+ argv[0], pfound->name);
+#else
+ fprintf (stderr, _("\
+%s: option `--%s' doesn't allow an argument\n"),
+ argv[0], pfound->name);
+#endif
+ }
+ else
+ {
+ /* +option or -option */
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("\
+%s: option `%c%s' doesn't allow an argument\n"),
+ argv[0], argv[optind - 1][0],
+ pfound->name);
+#else
+ fprintf (stderr, _("\
+%s: option `%c%s' doesn't allow an argument\n"),
+ argv[0], argv[optind - 1][0], pfound->name);
+#endif
+ }
+
+#if defined _LIBC && defined USE_IN_LIBIO
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#endif
+ }
+
+ nextchar += strlen (nextchar);
+
+ optopt = pfound->val;
+ return '?';
+ }
+ }
+ else if (pfound->has_arg == 1)
+ {
+ if (optind < argc)
+ optarg = argv[optind++];
+ else
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf,
+ _("%s: option `%s' requires an argument\n"),
+ argv[0], argv[optind - 1]);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr,
+ _("%s: option `%s' requires an argument\n"),
+ argv[0], argv[optind - 1]);
+#endif
+ }
+ nextchar += strlen (nextchar);
+ optopt = pfound->val;
+ return optstring[0] == ':' ? ':' : '?';
+ }
+ }
+ nextchar += strlen (nextchar);
+ if (longind != NULL)
+ *longind = option_index;
+ if (pfound->flag)
+ {
+ *(pfound->flag) = pfound->val;
+ return 0;
+ }
+ return pfound->val;
+ }
+
+ /* Can't find it as a long option. If this is not getopt_long_only,
+ or the option starts with '--' or is not a valid short
+ option, then it's an error.
+ Otherwise interpret it as a short option. */
+ if (!long_only || argv[optind][1] == '-'
+ || my_index (optstring, *nextchar) == NULL)
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+#endif
+
+ if (argv[optind][1] == '-')
+ {
+ /* --option */
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
+ argv[0], nextchar);
+#else
+ fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
+ argv[0], nextchar);
+#endif
+ }
+ else
+ {
+ /* +option or -option */
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
+ argv[0], argv[optind][0], nextchar);
+#else
+ fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
+ argv[0], argv[optind][0], nextchar);
+#endif
+ }
+
+#if defined _LIBC && defined USE_IN_LIBIO
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#endif
+ }
+ nextchar = (char *) "";
+ optind++;
+ optopt = 0;
+ return '?';
+ }
+ }
+
+ /* Look at and handle the next short option-character. */
+
+ {
+ char c = *nextchar++;
+ char *temp = my_index (optstring, c);
+
+ /* Increment `optind' when we start to process its last character. */
+ if (*nextchar == '\0')
+ ++optind;
+
+ if (temp == NULL || c == ':')
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+#endif
+
+ if (posixly_correct)
+ {
+ /* 1003.2 specifies the format of this message. */
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("%s: illegal option -- %c\n"),
+ argv[0], c);
+#else
+ fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
+#endif
+ }
+ else
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ __asprintf (&buf, _("%s: invalid option -- %c\n"),
+ argv[0], c);
+#else
+ fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
+#endif
+ }
+
+#if defined _LIBC && defined USE_IN_LIBIO
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#endif
+ }
+ optopt = c;
+ return '?';
+ }
+ /* Convenience. Treat POSIX -W foo same as long option --foo */
+ if (temp[0] == 'W' && temp[1] == ';')
+ {
+ char *nameend;
+ const struct option *p;
+ const struct option *pfound = NULL;
+ int exact = 0;
+ int ambig = 0;
+ int indfound = 0;
+ int option_index;
+
+ /* This is an option that requires an argument. */
+ if (*nextchar != '\0')
+ {
+ optarg = nextchar;
+ /* If we end this ARGV-element by taking the rest as an arg,
+ we must advance to the next element now. */
+ optind++;
+ }
+ else if (optind == argc)
+ {
+ if (print_errors)
+ {
+ /* 1003.2 specifies the format of this message. */
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf, _("%s: option requires an argument -- %c\n"),
+ argv[0], c);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr, _("%s: option requires an argument -- %c\n"),
+ argv[0], c);
+#endif
+ }
+ optopt = c;
+ if (optstring[0] == ':')
+ c = ':';
+ else
+ c = '?';
+ return c;
+ }
+ else
+ /* We already incremented `optind' once;
+ increment it again when taking next ARGV-elt as argument. */
+ optarg = argv[optind++];
+
+ /* optarg is now the argument, see if it's in the
+ table of longopts. */
+
+ for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
+ /* Do nothing. */ ;
+
+ /* Test all long options for either exact match
+ or abbreviated matches. */
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
+ if (!strncmp (p->name, nextchar, nameend - nextchar))
+ {
+ if ((unsigned int) (nameend - nextchar) == strlen (p->name))
+ {
+ /* Exact match found. */
+ pfound = p;
+ indfound = option_index;
+ exact = 1;
+ break;
+ }
+ else if (pfound == NULL)
+ {
+ /* First nonexact match found. */
+ pfound = p;
+ indfound = option_index;
+ }
+ else
+ /* Second or later nonexact match found. */
+ ambig = 1;
+ }
+ if (ambig && !exact)
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
+ argv[0], argv[optind]);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
+ argv[0], argv[optind]);
+#endif
+ }
+ nextchar += strlen (nextchar);
+ optind++;
+ return '?';
+ }
+ if (pfound != NULL)
+ {
+ option_index = indfound;
+ if (*nameend)
+ {
+ /* Don't test has_arg with >, because some C compilers don't
+ allow it to be used on enums. */
+ if (pfound->has_arg)
+ optarg = nameend + 1;
+ else
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf, _("\
+%s: option `-W %s' doesn't allow an argument\n"),
+ argv[0], pfound->name);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr, _("\
+%s: option `-W %s' doesn't allow an argument\n"),
+ argv[0], pfound->name);
+#endif
+ }
+
+ nextchar += strlen (nextchar);
+ return '?';
+ }
+ }
+ else if (pfound->has_arg == 1)
+ {
+ if (optind < argc)
+ optarg = argv[optind++];
+ else
+ {
+ if (print_errors)
+ {
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf, _("\
+%s: option `%s' requires an argument\n"),
+ argv[0], argv[optind - 1]);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr,
+ _("%s: option `%s' requires an argument\n"),
+ argv[0], argv[optind - 1]);
+#endif
+ }
+ nextchar += strlen (nextchar);
+ return optstring[0] == ':' ? ':' : '?';
+ }
+ }
+ nextchar += strlen (nextchar);
+ if (longind != NULL)
+ *longind = option_index;
+ if (pfound->flag)
+ {
+ *(pfound->flag) = pfound->val;
+ return 0;
+ }
+ return pfound->val;
+ }
+ nextchar = NULL;
+ return 'W'; /* Let the application handle it. */
+ }
+ if (temp[1] == ':')
+ {
+ if (temp[2] == ':')
+ {
+ /* This is an option that accepts an argument optionally. */
+ if (*nextchar != '\0')
+ {
+ optarg = nextchar;
+ optind++;
+ }
+ else
+ optarg = NULL;
+ nextchar = NULL;
+ }
+ else
+ {
+ /* This is an option that requires an argument. */
+ if (*nextchar != '\0')
+ {
+ optarg = nextchar;
+ /* If we end this ARGV-element by taking the rest as an arg,
+ we must advance to the next element now. */
+ optind++;
+ }
+ else if (optind == argc)
+ {
+ if (print_errors)
+ {
+ /* 1003.2 specifies the format of this message. */
+#if defined _LIBC && defined USE_IN_LIBIO
+ char *buf;
+
+ __asprintf (&buf,
+ _("%s: option requires an argument -- %c\n"),
+ argv[0], c);
+
+ if (_IO_fwide (stderr, 0) > 0)
+ __fwprintf (stderr, L"%s", buf);
+ else
+ fputs (buf, stderr);
+
+ free (buf);
+#else
+ fprintf (stderr,
+ _("%s: option requires an argument -- %c\n"),
+ argv[0], c);
+#endif
+ }
+ optopt = c;
+ if (optstring[0] == ':')
+ c = ':';
+ else
+ c = '?';
+ }
+ else
+ /* We already incremented `optind' once;
+ increment it again when taking next ARGV-elt as argument. */
+ optarg = argv[optind++];
+ nextchar = NULL;
+ }
+ }
+ return c;
+ }
+}
+
+int
+getopt (argc, argv, optstring)
+ int argc;
+ char *const *argv;
+ const char *optstring;
+{
+ return _getopt_internal (argc, argv, optstring,
+ (const struct option *) 0,
+ (int *) 0,
+ 0);
+}
+
+#endif /* Not ELIDE_CODE. */
+
+
+/* Compile with -DTEST to make an executable for use in testing
+ the above definition of `getopt'. */
+
+/* #define TEST */ /* Pete Wilson mod 7/28/02 */
+#ifdef TEST
+
+#ifndef exit /* Pete Wilson mod 7/28/02 */
+ int exit(int); /* Pete Wilson mod 7/28/02 */
+#endif /* Pete Wilson mod 7/28/02 */
+
+int
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ int c;
+ int digit_optind = 0;
+
+ while (1)
+ {
+ int this_option_optind = optind ? optind : 1;
+
+ c = getopt (argc, argv, "abc:d:0123456789");
+ if (c == -1)
+ break;
+
+ switch (c)
+ {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ if (digit_optind != 0 && digit_optind != this_option_optind)
+ printf ("digits occur in two different argv-elements.\n");
+ digit_optind = this_option_optind;
+ printf ("option %c\n", c);
+ break;
+
+ case 'a':
+ printf ("option a\n");
+ break;
+
+ case 'b':
+ printf ("option b\n");
+ break;
+
+ case 'c':
+ printf ("option c with value `%s'\n", optarg);
+ break;
+
+ case '?':
+ break;
+
+ default:
+ printf ("?? getopt returned character code 0%o ??\n", c);
+ }
+ }
+
+ if (optind < argc)
+ {
+ printf ("non-option ARGV-elements: ");
+ while (optind < argc)
+ printf ("%s ", argv[optind++]);
+ printf ("\n");
+ }
+
+ exit (0);
+}
+
+#endif /* TEST */
diff --git a/xs/src/avrdude/windows/getopt.h b/xs/src/avrdude/windows/getopt.h
new file mode 100644
index 000000000..8a45c8b13
--- /dev/null
+++ b/xs/src/avrdude/windows/getopt.h
@@ -0,0 +1,188 @@
+
+/* getopt.h */
+/* Declarations for getopt.
+ Copyright (C) 1989-1994, 1996-1999, 2001 Free Software
+ Foundation, Inc. This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute
+ it and/or modify it under the terms of the GNU Lesser
+ General Public License as published by the Free Software
+ Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will
+ be useful, but WITHOUT ANY WARRANTY; without even the
+ implied warranty of MERCHANTABILITY or FITNESS FOR A
+ PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with the GNU C Library; if not, write
+ to the Free Software Foundation, Inc., 59 Temple Place,
+ Suite 330, Boston, MA 02111-1307 USA. */
+
+
+
+
+#ifndef _GETOPT_H
+
+#ifndef __need_getopt
+# define _GETOPT_H 1
+#endif
+
+/* If __GNU_LIBRARY__ is not already defined, either we are being used
+ standalone, or this is the first header included in the source file.
+ If we are being used with glibc, we need to include <features.h>, but
+ that does not exist if we are standalone. So: if __GNU_LIBRARY__ is
+ not defined, include <ctype.h>, which will pull in <features.h> for us
+ if it's from glibc. (Why ctype.h? It's guaranteed to exist and it
+ doesn't flood the namespace with stuff the way some other headers do.) */
+#if !defined __GNU_LIBRARY__
+# include <ctype.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* For communication from `getopt' to the caller.
+ When `getopt' finds an option that takes an argument,
+ the argument value is returned here.
+ Also, when `ordering' is RETURN_IN_ORDER,
+ each non-option ARGV-element is returned here. */
+
+extern char *optarg;
+
+/* Index in ARGV of the next element to be scanned.
+ This is used for communication to and from the caller
+ and for communication between successive calls to `getopt'.
+
+ On entry to `getopt', zero means this is the first call; initialize.
+
+ When `getopt' returns -1, this is the index of the first of the
+ non-option elements that the caller should itself scan.
+
+ Otherwise, `optind' communicates from one call to the next
+ how much of ARGV has been scanned so far. */
+
+extern int optind;
+
+/* Callers store zero here to inhibit the error message `getopt' prints
+ for unrecognized options. */
+
+extern int opterr;
+
+/* Set to an option character which was unrecognized. */
+
+extern int optopt;
+
+#ifndef __need_getopt
+/* Describe the long-named options requested by the application.
+ The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ of `struct option' terminated by an element containing a name which is
+ zero.
+
+ The field `has_arg' is:
+ no_argument (or 0) if the option does not take an argument,
+ required_argument (or 1) if the option requires an argument,
+ optional_argument (or 2) if the option takes an optional argument.
+
+ If the field `flag' is not NULL, it points to a variable that is set
+ to the value given in the field `val' when the option is found, but
+ left unchanged if the option is not found.
+
+ To have a long-named option do something other than set an `int' to
+ a compiled-in constant, such as set a value from `optarg', set the
+ option's `flag' field to zero and its `val' field to a nonzero
+ value (the equivalent single-letter option character, if there is
+ one). For long options that have a zero `flag' field, `getopt'
+ returns the contents of the `val' field. */
+
+struct option
+{
+# if (defined __STDC__ && __STDC__) || defined __cplusplus
+ const char *name;
+# else
+ char *name;
+# endif
+ /* has_arg can't be an enum because some compilers complain about
+ type mismatches in all the code that assumes it is an int. */
+ int has_arg;
+ int *flag;
+ int val;
+};
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+# define no_argument 0
+# define required_argument 1
+# define optional_argument 2
+#endif /* need getopt */
+
+
+/* Get definitions and prototypes for functions to process the
+ arguments in ARGV (ARGC of them, minus the program name) for
+ options given in OPTS.
+
+ Return the option character from OPTS just read. Return -1 when
+ there are no more options. For unrecognized options, or options
+ missing arguments, `optopt' is set to the option letter, and '?' is
+ returned.
+
+ The OPTS string is a list of characters which are recognized option
+ letters, optionally followed by colons, specifying that that letter
+ takes an argument, to be placed in `optarg'.
+
+ If a letter in OPTS is followed by two colons, its argument is
+ optional. This behavior is specific to the GNU `getopt'.
+
+ The argument `--' causes premature termination of argument
+ scanning, explicitly telling `getopt' that there are no more
+ options.
+
+ If OPTS begins with `--', then non-option arguments are treated as
+ arguments to the option '\0'. This behavior is specific to the GNU
+ `getopt'. */
+
+#if (defined __STDC__ && __STDC__) || defined __cplusplus
+# ifdef __GNU_LIBRARY__
+/* Many other libraries have conflicting prototypes for getopt, with
+ differences in the consts, in stdlib.h. To avoid compilation
+ errors, only prototype getopt for the GNU C library. */
+extern int getopt (int ___argc, char *const *___argv, const char *__shortopts);
+# else /* not __GNU_LIBRARY__ */
+extern int getopt ();
+# endif /* __GNU_LIBRARY__ */
+
+# ifndef __need_getopt
+extern int getopt_long (int ___argc, char *const *___argv,
+ const char *__shortopts,
+ const struct option *__longopts, int *__longind);
+extern int getopt_long_only (int ___argc, char *const *___argv,
+ const char *__shortopts,
+ const struct option *__longopts, int *__longind);
+
+/* Internal only. Users should not call this directly. */
+extern int _getopt_internal (int ___argc, char *const *___argv,
+ const char *__shortopts,
+ const struct option *__longopts, int *__longind,
+ int __long_only);
+# endif
+#else /* not __STDC__ */
+extern int getopt ();
+# ifndef __need_getopt
+extern int getopt_long ();
+extern int getopt_long_only ();
+
+extern int _getopt_internal ();
+# endif
+#endif /* __STDC__ */
+
+#ifdef __cplusplus
+}
+#endif
+
+/* Make sure we later can get all the definitions and declarations. */
+#undef __need_getopt
+
+#endif /* getopt.h */
diff --git a/xs/src/avrdude/windows/unistd.cpp b/xs/src/avrdude/windows/unistd.cpp
new file mode 100644
index 000000000..e47c0b659
--- /dev/null
+++ b/xs/src/avrdude/windows/unistd.cpp
@@ -0,0 +1,44 @@
+#include "unistd.h"
+
+#include <cstdint>
+#include <chrono>
+#include <thread>
+
+#include <windows.h>
+
+
+extern "C" {
+
+
+int usleep(unsigned usec)
+{
+ std::this_thread::sleep_for(std::chrono::microseconds(usec));
+ return 0;
+}
+
+
+// SO: https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
+int gettimeofday(struct timeval *tp, struct timezone *tzp)
+{
+ // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
+ // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
+ // until 00:00:00 January 1, 1970
+ static const std::uint64_t EPOCH = ((std::uint64_t) 116444736000000000ULL);
+
+ SYSTEMTIME system_time;
+ FILETIME file_time;
+ std::uint64_t time;
+
+ GetSystemTime(&system_time);
+ SystemTimeToFileTime(&system_time, &file_time);
+ time = ((std::uint64_t)file_time.dwLowDateTime);
+ time += ((std::uint64_t)file_time.dwHighDateTime) << 32;
+
+ tp->tv_sec = (long)((time - EPOCH) / 10000000L);
+ tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
+ return 0;
+}
+
+
+
+}
diff --git a/xs/src/avrdude/windows/unistd.h b/xs/src/avrdude/windows/unistd.h
new file mode 100644
index 000000000..b1bc6bf7f
--- /dev/null
+++ b/xs/src/avrdude/windows/unistd.h
@@ -0,0 +1,85 @@
+#ifndef SLIC3R_AVRDUDE_UNISTD_H
+#define SLIC3R_AVRDUDE_UNISTD_H 1
+
+/* This is intended as a drop-in replacement for unistd.h on Windows.
+ * Please add functionality as neeeded.
+ * https://stackoverflow.com/a/826027/1202830
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#include <stdlib.h>
+#include <io.h>
+#include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */
+#include <process.h> /* for getpid() and the exec..() family */
+#include <direct.h> /* for _getcwd() and _chdir() */
+#include <sys/types.h>
+#include <sys/stat.h> /* both for stat() */
+
+#ifndef __cplusplus
+#define inline __inline
+#endif
+
+#define __func__ __FUNCTION__
+
+#define srandom srand
+#define random rand
+
+/* Values for the second argument to access.
+ These may be OR'd together. */
+#define R_OK 4 /* Test for read permission. */
+#define W_OK 2 /* Test for write permission. */
+//#define X_OK 1 /* execute permission - unsupported in windows*/
+#define F_OK 0 /* Test for existence. */
+
+#define access _access
+#define dup2 _dup2
+#define execve _execve
+#define ftruncate _chsize
+#define unlink _unlink
+#define fileno _fileno
+#define getcwd _getcwd
+#define chdir _chdir
+#define isatty _isatty
+#define lseek _lseek
+#define snprintf _snprintf
+#define strncasecmp _strnicmp
+#define strcasecmp _stricmp
+#define stat _stat
+/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
+
+#ifdef _WIN64
+#define ssize_t __int64
+#else
+#define ssize_t long
+#endif
+
+#define STDIN_FILENO 0
+#define STDOUT_FILENO 1
+#define STDERR_FILENO 2
+
+#ifndef __cplusplus
+/* should be in some equivalent to <sys/types.h> */
+typedef __int8 int8_t;
+typedef __int16 int16_t;
+typedef __int32 int32_t;
+typedef __int64 int64_t;
+typedef unsigned __int8 uint8_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+#endif
+
+
+int usleep(unsigned usec);
+int gettimeofday(struct timeval *tp, struct timezone *tzp);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* unistd.h */