about summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
authorComradeCrow <comradecrow@vivaldi.net>2023-05-06 23:48:40 -0700
committerComradeCrow <comradecrow@vivaldi.net>2023-05-06 23:48:40 -0700
commit86ec02fa985112b3ba72ac64f14f452281f29719 (patch)
tree6cde7e9d947f65f4552260838140c36d32d2ed34 /src/main.cpp
parent3212791b7ac63f01da8bb115e68c43a36c3b67a7 (diff)
downloadytcpp-86ec02fa985112b3ba72ac64f14f452281f29719.tar.gz
working on inv again
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp96
1 files changed, 62 insertions, 34 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 3ee071a..ec86677 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -9,14 +9,13 @@
 #include <fstream>
 #include <filesystem>
 #include <stdlib.h>
-#ifdef __unix
-#include <unistd.h>
-#include <spawn.h>
-#elif __posix
+#include <cstring>
+#ifdef _WIN32
+#include <windows.h>
+#elif (__unix || __posix)
 #include <unistd.h>
 #include <spawn.h>
-#elif _WIN32
-#include <windows.h>
+#include <sys/wait.h>
 #endif
 
 #include "tui.hpp"
@@ -62,39 +61,17 @@ void validateStructConversions() {
     }
 }
 
-void parseSysArgs(int argc, char **argv) {
-
-    for (int i{0}; i < argc; ++i) {
-        cout << *(argv + i) << endl;
-    }
-}
-
-
-
-int main(int argc, char **argv) {
+void spawn(const char* url, bool wait) {
 
-    parseSysArgs(argc, argv);
     #ifdef _WIN32
+
         start mpv
         bool CreateProcessA()
-    #elif __unix
-        extern char **environ;
-
-        int i{0};
-        while ((environ[i]) != nullptr) {
+    #elif (__unix || __posix)
 
-            cout << environ[i++] << endl;
-        }
+        extern char **environ;
         pid_t pid{0};
-        fs::path vidFile("It's Time to Celebrate [K3qjudqERKU].webm");
-        char *const mpvArgs[] = {strdup("mpv"), strdup(fs::absolute(vidFile).c_str()), nullptr};
-
-        i = 0;
-        while ((mpvArgs[i]) != nullptr) {
-
-            cout << mpvArgs[i++] << " ";
-        } 
-        cout << endl;
+        char *const mpvArgs[] = {strdup("mpv"), strdup(url), nullptr};
 
         int result = posix_spawnp(&pid, mpvArgs[0], NULL, NULL, mpvArgs, environ);
         if (result != 0) { 
@@ -103,8 +80,59 @@ int main(int argc, char **argv) {
         } else {
 
             cout << "pid: " << pid << endl;
-        }
+            if (wait) {
+
+                do {
+                    if (waitpid(pid, &result, 0) != -1) {
 
+                        cout << "Child status: " << WEXITSTATUS(result) << endl;
+                    } else {
+
+                        perror("waitpid");
+                        exit(1);
+                    }
+                } while (!WIFEXITED(result) && !WIFSIGNALED(result));
+            }
+        }
     #endif
+}
+
+void parseSysArgs(int argc, char **argv) {
+
+    for (int i{0}; i < argc; ++i) {
+        cout << argv[i] << " ";
+    } cout << endl;
+
+    string currentArg; 
+    for (int i{0}; i < argc; ++i) {
+        if (strncmp(argv[i], "-", 1) == 0) {
+
+            currentArg = argv[i];
+        } 
+        else {
+
+            if (currentArg == "") {
+
+                cout << argv[i] << endl;
+                if (strncmp(argv[i], "http://", 7) == 0 || strncmp(argv[i], "https://", 8) == 0) {
+                    
+                    spawn(argv[i], true);
+                }
+            } else if (currentArg == "-S" || currentArg == "--search") {
+
+                cout << "searching for: " << argv[i] << endl;
+                currentArg = "";
+            } else {
+
+                cout << currentArg << ": " << argv[i] << endl;
+            }
+        }
+    }
+}
+
+int main(int argc, char **argv) {
+
+    parseSysArgs(argc, argv);
+    
 
 }
\ No newline at end of file