fxlink: add --folder option to fxlink -s to select output folder

This commit is contained in:
Lephenixnoir 2024-01-30 22:19:19 +01:00
parent ecf04cb634
commit da79a6a0e8
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
4 changed files with 23 additions and 6 deletions

View file

@ -28,7 +28,8 @@ int main_list(struct fxlink_filter *filter, delay_t *delay,
int main_blocks(struct fxlink_filter *filter, delay_t *delay); int main_blocks(struct fxlink_filter *filter, delay_t *delay);
/* Main function for -s */ /* Main function for -s */
int main_send(struct fxlink_filter *filter, delay_t *delay, char **files); int main_send(struct fxlink_filter *filter, delay_t *delay, char **files,
char *outfolder);
/* Main function for -i */ /* Main function for -i */
int main_interactive(struct fxlink_filter *filter, delay_t *delay, int main_interactive(struct fxlink_filter *filter, delay_t *delay,

View file

@ -20,7 +20,7 @@ static const char *help_string =
"usage: %1$s (-l|-b|-t) [General options]\n" "usage: %1$s (-l|-b|-t) [General options]\n"
" %1$s -i [-r] [--fxlink-log[=<FILE>]] [General options]\n" " %1$s -i [-r] [--fxlink-log[=<FILE>]] [General options]\n"
" %1$s -p <FILE> [General options]\n" " %1$s -p <FILE> [General options]\n"
" %1$s -s <FILES>... [General options]\n" " %1$s -s <FILES>... [--folder=OUTFOLDER] [General options]\n"
"\n" "\n"
"fxlink interacts with CASIO calculators of the fx and fx-CG families over\n" "fxlink interacts with CASIO calculators of the fx and fx-CG families over\n"
"the USB port, using libusb. It can also transfer files for Mass Storage\n" "the USB port, using libusb. It can also transfer files for Mass Storage\n"
@ -47,6 +47,7 @@ static const char *help_string =
" --fxlink-log[=FILE] -i: Append fxlink text messages to FILE. Without\n" " --fxlink-log[=FILE] -i: Append fxlink text messages to FILE. Without\n"
" argument, a unique name is generated.\n" " argument, a unique name is generated.\n"
" -r, --repeat -i: Reconnect if the calc disconnects (implies -w)\n" " -r, --repeat -i: Reconnect if the calc disconnects (implies -w)\n"
" --folder=FOLDER -s: Select destination folder for files\n"
"\n" "\n"
"Device filters:\n" "Device filters:\n"
" A device filter narrows down what devices we list or connect to by\n" " A device filter narrows down what devices we list or connect to by\n"
@ -67,6 +68,7 @@ int main(int argc, char **argv)
delay_t delay = delay_seconds(0); delay_t delay = delay_seconds(0);
struct fxlink_filter *filter = NULL; struct fxlink_filter *filter = NULL;
bool repeat = false; bool repeat = false;
char *outfolder = NULL;
options.log_file = NULL; options.log_file = NULL;
options.verbose = false; options.verbose = false;
@ -77,7 +79,7 @@ int main(int argc, char **argv)
// Command-line argument parsing // Command-line argument parsing
//--- //---
enum { LIBUSB_LOG=1, LOG_TO_FILE=2 }; enum { LIBUSB_LOG=1, LOG_TO_FILE=2, OUT_FOLDER=3 };
const struct option longs[] = { const struct option longs[] = {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "list", no_argument, NULL, 'l' }, { "list", no_argument, NULL, 'l' },
@ -90,6 +92,7 @@ int main(int argc, char **argv)
{ "fxlink-log", optional_argument, NULL, LOG_TO_FILE }, { "fxlink-log", optional_argument, NULL, LOG_TO_FILE },
{ "repeat", no_argument, NULL, 'r' }, { "repeat", no_argument, NULL, 'r' },
{ "verbose", no_argument, NULL, 'v' }, { "verbose", no_argument, NULL, 'v' },
{ "folder", required_argument, NULL, OUT_FOLDER },
/* Deprecated options ignored for compatibility: */ /* Deprecated options ignored for compatibility: */
{ "quiet", no_argument, NULL, 'q' }, { "quiet", no_argument, NULL, 'q' },
{ "unmount", no_argument, NULL, 'u' }, { "unmount", no_argument, NULL, 'u' },
@ -165,6 +168,9 @@ int main(int argc, char **argv)
case 'f': case 'f':
filter = fxlink_filter_parse(optarg); filter = fxlink_filter_parse(optarg);
break; break;
case OUT_FOLDER:
outfolder = strdup(optarg);
break;
case '?': case '?':
error = 1; error = 1;
} }
@ -219,7 +225,7 @@ int main(int argc, char **argv)
} }
else if(mode == 's') { else if(mode == 's') {
#ifndef FXLINK_DISABLE_UDISKS2 #ifndef FXLINK_DISABLE_UDISKS2
rc = main_send(filter, &delay, argv + optind); rc = main_send(filter, &delay, argv + optind, outfolder);
#else #else
rc = elog("this fxlink was built without UDisks2; -s is disabled"); rc = elog("this fxlink was built without UDisks2; -s is disabled");
#endif #endif

View file

@ -44,11 +44,13 @@ static void handle_new_message(struct fxlink_device *fdev,
if(options.verbose) if(options.verbose)
printf("------------------\n"); printf("------------------\n");
fwrite(str, 1, msg->size, stdout); fwrite(str, 1, msg->size, stdout);
#if 0
if(str[msg->size - 1] != '\n') { if(str[msg->size - 1] != '\n') {
if(!options.verbose) if(!options.verbose)
printf("\e[30;47m%%\e[0m"); printf("\e[30;47m%%\e[0m");
printf("\n"); printf("\n");
} }
#endif
if(options.verbose) { if(options.verbose) {
printf("------------------\n"); printf("------------------\n");
} }

View file

@ -16,7 +16,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
int main_send(struct fxlink_filter *filter, delay_t *delay, char **files) int main_send(struct fxlink_filter *filter, delay_t *delay, char **files,
char *outfolder)
{ {
fxlink_filter_clean_udisks2(filter); fxlink_filter_clean_udisks2(filter);
GError *error = NULL; GError *error = NULL;
@ -57,6 +58,10 @@ int main_send(struct fxlink_filter *filter, delay_t *delay, char **files)
printf("Already mounted at %s.\n", folder); printf("Already mounted at %s.\n", folder);
} }
gchar *outpath = folder;
if(outfolder)
asprintf(&outpath, "%s/%s/", folder, outfolder);
/* Copy files with external cp(1) */ /* Copy files with external cp(1) */
int file_count = 0; int file_count = 0;
while(files[file_count]) file_count++; while(files[file_count]) file_count++;
@ -69,7 +74,7 @@ int main_send(struct fxlink_filter *filter, delay_t *delay, char **files)
argv[0] = "cp"; argv[0] = "cp";
for(int i = 0; files[i]; i++) for(int i = 0; files[i]; i++)
argv[i+1] = files[i]; argv[i+1] = files[i];
argv[file_count+1] = folder; argv[file_count+1] = outpath;
argv[file_count+2] = NULL; argv[file_count+2] = NULL;
/* Print command */ /* Print command */
@ -97,6 +102,9 @@ int main_send(struct fxlink_filter *filter, delay_t *delay, char **files)
} }
} }
if(outfolder)
free(outpath);
/* Unmount the filesystem and eject the device */ /* Unmount the filesystem and eject the device */
GVariant *args = g_variant_new("a{sv}", NULL); GVariant *args = g_variant_new("a{sv}", NULL);
udisks_filesystem_call_unmount_sync(fs, args, NULL, &error); udisks_filesystem_call_unmount_sync(fs, args, NULL, &error);