Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad8d610

Browse files
authored
Merge pull request #4 from cpq/curl
Allow URLs as file names, use curl to download
2 parents 6e1834c + 144a8f7 commit ad8d610

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

esputil.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ static size_t slip_recv(unsigned char c, struct slip *slip) {
153153
return res;
154154
}
155155

156-
void signal_handler(int signo) {
157-
s_signo = signo;
158-
}
156+
void signal_handler(int signo) { s_signo = signo; }
159157

160158
static int fail(const char *fmt, ...) {
161159
va_list ap;
@@ -266,9 +264,7 @@ static uint8_t checksum(const uint8_t *buf, size_t len) {
266264
}
267265

268266
#ifdef _WIN32 // Windows - specific routines
269-
static void sleep_ms(int milliseconds) {
270-
Sleep(milliseconds);
271-
}
267+
static void sleep_ms(int milliseconds) { Sleep(milliseconds); }
272268

273269
static void flushio(int fd) {
274270
PurgeComm((HANDLE) _get_osfhandle(fd), PURGE_RXCLEAR | PURGE_TXCLEAR);
@@ -339,13 +335,9 @@ static void set_dtr(int fd, bool value) {
339335
ioctl(fd, value ? TIOCMBIS : TIOCMBIC, &v);
340336
}
341337

342-
static void flushio(int fd) {
343-
tcflush(fd, TCIOFLUSH);
344-
}
338+
static void flushio(int fd) { tcflush(fd, TCIOFLUSH); }
345339

346-
static void sleep_ms(int milliseconds) {
347-
usleep(milliseconds * 1000);
348-
}
340+
static void sleep_ms(int milliseconds) { usleep(milliseconds * 1000); }
349341

350342
// clang-format off
351343
static speed_t termios_baud(int baud) {
@@ -848,6 +840,16 @@ static void flashbin(struct ctx *ctx, uint16_t flash_params,
848840
fclose(fp);
849841
}
850842

843+
static const char *download(const char *url) {
844+
char cmd[2048];
845+
const char *slash = strrchr(url, '/');
846+
if (slash == NULL) fail("Invalid URL: %s\n", url);
847+
snprintf(cmd, sizeof(cmd), "curl -sL %s -o %s", url, slash + 1);
848+
printf("%s\n", cmd);
849+
if (system(cmd) != 0) fail("Download failed\n");
850+
return slash + 1;
851+
}
852+
851853
static void flash(struct ctx *ctx, const char **args) {
852854
uint16_t flash_params = 0;
853855
if (!chip_connect(ctx)) fail("Error connecting\n");
@@ -883,6 +885,10 @@ static void flash(struct ctx *ctx, const char **args) {
883885
// A .hex file is fed to us. Unhex it first into a temp dir
884886
char file_list[8192], tmpdir[1024], *s = file_list;
885887
size_t n;
888+
889+
bool is_url = (strncmp(args[0], "http", 4) == 0);
890+
if (is_url) args[0] = download(args[0]);
891+
886892
snprintf(tmpdir, sizeof(tmpdir), "%s.tmp", args[0]);
887893
unhex(args[0], tmpdir, file_list, sizeof(file_list));
888894
// Now iterate over the unhexed files, and flash each
@@ -893,10 +899,14 @@ static void flash(struct ctx *ctx, const char **args) {
893899
flashbin(ctx, flash_params, strtoul(slash ? slash + 1 : s, NULL, 0), s);
894900
s = p;
895901
}
896-
rmrf(tmpdir); // Cleanup temp dir
897-
args += 1; // Move to next file
902+
if (is_url) remove(args[0]); // Remove downloaded file
903+
rmrf(tmpdir); // Cleanup temp dir
904+
args += 1; // Move to next file
898905
} else if (args[1] != NULL) {
906+
bool is_url = (strncmp(args[0], "http", 4) == 0);
907+
if (is_url) args[1] = download(args[1]);
899908
flashbin(ctx, flash_params, strtoul(args[0], NULL, 0), args[1]);
909+
if (is_url) remove(args[0]); // Remove downloaded file
900910
args += 2;
901911
}
902912
}

0 commit comments

Comments
 (0)