summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Albers <thomas@thomaslabs.org>2023-08-10 11:12:23 +0200
committerThomas Albers <thomas@thomaslabs.org>2023-08-10 11:12:23 +0200
commit932424af0aadcfcc9dcf3f66ff844fea6ac53986 (patch)
tree9f8ae78e06e3457ffdc0a0214da0b39638e65811
parent312dea1d673365656cf98d282fb2bce64289a11d (diff)
Add progress option to write command
-rw-r--r--cmd.c47
-rw-r--r--zup.c28
-rw-r--r--zup.h1
3 files changed, 53 insertions, 23 deletions
diff --git a/cmd.c b/cmd.c
index 42a184d..a460808 100644
--- a/cmd.c
+++ b/cmd.c
@@ -11,6 +11,8 @@
#include <readline/history.h>
#endif // USE_READLINE
+#include <sys/stat.h>
+
struct command;
enum args_type {
@@ -168,7 +170,34 @@ cmd_write(int fd, const struct command *cmd, int argc, char **args)
if (err < 0)
return -1;
- if (param.human_readable) {
+ if (pathname) {
+ off_t n = 0;
+ FILE *fp;
+ struct stat st;
+
+ if (!(fp = fopen(pathname, "r")) || (fstat(fileno(fp), &st) < 0))
+ goto file_error;
+
+ printf("Uploading %s :\n[ 0%%]", pathname);
+ fflush(stdout);
+
+ // FIXME: fread MAX_PACKET_SIZE insted of 8
+ // Split file into packets
+ while ((length = fread(buf, sizeof(uint8_t), 8, fp))) {
+ if ((err = z_write(fd, bank, address + n, length, buf)))
+ break;
+
+ n += length;
+
+ if (param.show_progress) {
+ printf("\r[% 3d%%]", 100 * n / st.st_size);
+ fflush(stdout);
+ }
+ }
+
+ puts("\nDone!");
+ fclose(fp);
+ } else if (param.human_readable) {
do {
// TODO: Handle fgets error
fgets(line, LEN(line), stdin);
@@ -193,26 +222,20 @@ cmd_write(int fd, const struct command *cmd, int argc, char **args)
return err;
}
} else {
- FILE *fp = pathname ? fopen(pathname, "r") : stdin;
-
- if (!fp) {
- perror("File could not be opened");
- return -1;
- }
-
// FIXME: fread MAX_PACKET_SIZE insted of 8
// Split file into packets
- while ((length = fread(buf, sizeof(uint8_t), 8, fp))) {
+ while ((length = fread(buf, sizeof(uint8_t), 8, stdin))) {
if ((err = z_write(fd, bank, address, length, buf)))
break;
address += length;
}
-
- if (fp != stdin)
- fclose(fp);
}
return err;
+
+file_error:
+ perror("File could not be opened");
+ return -1;
}
static int
diff --git a/zup.c b/zup.c
index 844a693..d3acc54 100644
--- a/zup.c
+++ b/zup.c
@@ -97,17 +97,18 @@ parse_baud(const char *str)
static int
parse_options(int argc, char *const argv[])
{
- const char *sopts = "b:p:hHVRrv";
+ const char *sopts = "b:p:hHVPRrv";
const struct option lopts[] = {
- { "baud", required_argument, 0, 'b'},
- { "verbose", no_argument, 0, 'v'},
- { "version", no_argument, 0, 'V'},
- { "raw", no_argument, 0, 'r'},
- { "repl", no_argument, 0, 'R'},
- { "port", required_argument, 0, 'p'},
- { "human-readable", no_argument, 0, 'H'},
- { "help", no_argument, 0, 'h'},
- { 0, 0, 0, 0 }
+ {"baud", required_argument, 0, 'b'},
+ {"verbose", no_argument, 0, 'v'},
+ {"version", no_argument, 0, 'V'},
+ {"raw", no_argument, 0, 'r'},
+ {"repl", no_argument, 0, 'R'},
+ {"port", required_argument, 0, 'p'},
+ {"progress", no_argument, 0, 'P'},
+ {"human-readable", no_argument, 0, 'H'},
+ {"help", no_argument, 0, 'h'},
+ { 0, 0, 0, 0 }
};
int c;
@@ -130,6 +131,10 @@ parse_options(int argc, char *const argv[])
strncpy(param.port, optarg, LEN(param.port));
break;
+ case 'P':
+ param.show_progress = 1;
+ break;
+
case 'R':
param.repl = 1;
break;
@@ -215,7 +220,8 @@ struct param param = {
.baud = B9600,
.verbose = 0,
.human_readable = 0,
- .repl = 0
+ .repl = 0,
+ .show_progress = 0
};
// TODO: Support stdin/stdout
diff --git a/zup.h b/zup.h
index 463f121..8fd5f7d 100644
--- a/zup.h
+++ b/zup.h
@@ -25,6 +25,7 @@ struct param {
unsigned int repl : 1;
unsigned int human_readable : 1;
unsigned int verbose : 2;
+ unsigned int show_progress : 1;
};
#ifdef __GNUC__