]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/pistorm-dev/pistorm_dev_amiga/simple_interact.c
Add Pi->Amiga file transfer to PiStorm interaction device
[pistorm] / platforms / amiga / pistorm-dev / pistorm_dev_amiga / simple_interact.c
index eaf2903be818e550e59e43bc68f327e0f49dc70c..b9ea1a919180dade02f67285b53f3cc24f9e65da 100644 (file)
@@ -3,6 +3,11 @@
 #include "../pistorm-dev-enums.h"
 #include "pistorm_dev.h"
 
+//#define SHUTUP_VSCODE
+
+#ifdef SHUTUP_VSCODE
+#define __stdargs
+#else
 #include <exec/resident.h>
 #include <exec/errors.h>
 #include <exec/memory.h>
@@ -23,6 +28,7 @@
 #include <proto/exec.h>
 #include <proto/disk.h>
 #include <proto/expansion.h>
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,7 +37,7 @@
 #define LOADLIB(a, b) if ((a = (struct a*)OpenLibrary(b,0L))==NULL) { \
     printf("Failed to load %s.\n", b); \
     return 1; \
-  } \
+  }
 
 void print_usage(char *exe);
 int get_command(char *cmd);
@@ -89,6 +95,37 @@ int __stdargs main (int argc, char *argv[]) {
             }
             pi_handle_config(cmd_arg, argv[2]);
             break;
+        case PI_CMD_TRANSFERFILE:
+            if (argc < 4) {
+                printf ("Please specify a source and destination filename in addition to the command.\n");
+                printf ("Example: %s --transfer platforms/platform.h snakes.h\n", argv[0]);
+            }
+            if (pi_get_filesize(argv[2], &tmpvalue) == PI_RES_FILENOTFOUND) {
+                printf ("File %s not found on the Pi side.\n", argv[2]);
+            } else {
+                unsigned int filesize = tmpvalue;
+                unsigned char *dest = malloc(filesize);
+
+                if (dest == NULL) {
+                    printf ("Failed to allocate memory buffer for file. Aborting file transfer.\n");
+                } else {
+                    printf ("Found a %d byte file on the Pi side. Eating it.\n", filesize);
+                    if (pi_transfer_file(argv[2], dest) != PI_RES_OK) {
+                        printf ("Something went horribly wrong during the file transfer.\n");
+                    } else {
+                        FILE *out = fopen(argv[3], "wb+");
+                        if (out == NULL) {
+                            printf ("Failed to open output file %s for writing.\n", argv[3]);
+                        } else {
+                            fwrite(dest, filesize, 1, out);
+                            fclose(out);
+                            printf ("%d bytes transferred to file %s.\n", filesize, argv[3]);
+                        }
+                    }
+                    free(dest);
+                }
+            }
+            break;
         default:
             printf ("Unhandled command %s.\n", argv[1]);
             return 1;
@@ -117,6 +154,9 @@ int get_command(char *cmd) {
         cmd_arg = PICFG_DEFAULT;
         return PI_CMD_SWITCHCONFIG;
     }
+    if (strcmp(cmd, "--transfer-file") == 0 || strcmp(cmd, "--transfer") == 0 || strcmp(cmd, "--getfile") == 0) {
+        return PI_CMD_TRANSFERFILE;
+    }
 
     return -1;
 }