]> git.mdlowis.com Git - projs/tide.git/commitdiff
switched from ruby script to tfetch
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 25 Jul 2017 01:18:42 +0000 (21:18 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 25 Jul 2017 01:18:42 +0000 (21:18 -0400)
tfetch.c
tide-fetch.rb [deleted file]
tide.c

index bf567572a02fb2e8aee80faa0ec54233dadd1e6a..86185eaedc37548eb11d11ccdc6d637233d1f87f 100644 (file)
--- a/tfetch.c
+++ b/tfetch.c
@@ -15,7 +15,7 @@
 typedef struct {
     enum {
         COMPLETE=0, MATCHES, IS, ISSET, ISDIR, ISFILE,
-        SET, UNSET, FINDFILE, EXEC, LAUNCH, CHECK
+        SET, UNSET, FINDFILE, EXEC, LAUNCH
     } type;
     char* arg1;
     char* arg2;
@@ -40,7 +40,7 @@ Rule* BuiltinRules[] = {
     (Rule[]){ // If it's an existing text file, open it with editor
         { ISSET, "EDITOR", NULL },
         { ISFILE, "$data", NULL },
-        { CHECK, "file --mime '$file' | grep -q 'text/'", NULL },
+        { EXEC, "file --mime '$file' | grep -q 'text/'", NULL },
         { LAUNCH, "$EDITOR '$file'", NULL },
         { COMPLETE, NULL, NULL }
     },
@@ -124,7 +124,7 @@ bool var_isdir(char* var) {
     if ((stat(path, &st) < 0) && (errno == ENOENT)) {
         return false;
     } else if (S_ISDIR(st.st_mode)) {
-        setenv("file", var, 1);
+        setenv("dir", var, 1);
         return true;
     } else {
         return false;
@@ -137,6 +137,7 @@ bool var_isfile(char* var) {
     if ((stat(eval(var), &st) < 0) && (errno == ENOENT)) {
         return false;
     } else if (!S_ISDIR(st.st_mode)) {
+        setenv("file", path, 1);
         return true;
     } else {
         return false;
@@ -155,24 +156,31 @@ bool find_file(char* file) {
     return false;
 }
 
+void runcmd(char* cmd) {
+    char* shellcmd[] = { getvar("SHELL"), "-c", NULL, NULL };
+    if (!shellcmd[0]) shellcmd[0] = "/bin/sh";
+    shellcmd[2] = eval(cmd);
+    _exit(execvp(shellcmd[0], shellcmd));
+}
+
 bool exec(char* cmd) {
+    int pid, status, outpipe[2];
+    if ((pid = fork()) < 0) return false;
+    if (pid == 0) {
+        runcmd(cmd);
+    } else {
+        waitpid(pid, &status, 0);
+        return (status == 0);
+    }
     return false;
 }
 
 bool launch(char* cmd) {
     int pid = fork();
-    if (pid > 0) {
+    if (pid > 0)
         return true;
-    } else if (pid == 0) {
-        char* shellcmd[] = { getvar("SHELL"), "-c", NULL, NULL };
-        if (!shellcmd[0]) shellcmd[0] = "/bin/sh";
-        shellcmd[2] = eval(cmd);
-        exit(execvp(shellcmd[0], shellcmd));
-    }
-    return false;
-}
-
-bool checkcmd(char* cmd) {
+    else if (pid == 0)
+        runcmd(cmd);
     return false;
 }
 
@@ -189,7 +197,6 @@ bool apply_rule(Rule* rule) {
         case FINDFILE: return find_file(rule->arg1);
         case EXEC:     return exec(rule->arg1);
         case LAUNCH:   return launch(rule->arg1);
-        case CHECK:    return checkcmd(rule->arg1);
     }
     return false;
 }
@@ -210,7 +217,7 @@ int main(int argc, char** argv) {
             if (!apply_rule(rule))
                 break;
         }
-        puts("");
+        //puts("");
     }
     return 1;
 }
diff --git a/tide-fetch.rb b/tide-fetch.rb
deleted file mode 100755 (executable)
index 287b4ae..0000000
+++ /dev/null
@@ -1,106 +0,0 @@
-#!/usr/bin/env ruby
-
-if not ARGV[0] then
-  $stderr.puts "Usage: tide-fetch.rb [ITEM]"
-  exit 1
-end
-
-$item  = ARGV[0]
-$attr  = {}
-$match = []
-UserRules = "#{ENV["HOME"]}/.config/tide/fetch-rules.rb"
-Rules = []
-Apps = {}
-
-# Define the Rule Language
-#-------------------------------------------------------------------------------
-class RuleError < StandardError; end
-
-def rule(&block)
-  Rules << block
-end
-
-def match(regex)
-  $match = $item.match(regex)
-  if not $match then
-    raise RuleError.new()
-  end
-end
-
-def spawn(cmd)
-  job = fork { exec cmd }
-  Process.detach(job)
-end
-
-def open_file
-  spawn("xdg-open #{$item}")
-end
-
-def open_with(app)
-  app = Apps[app] || ENV[app.to_s.upcase]
-  raise RuleError.new() if not app
-  spawn("#{app} #{$item}")
-end
-
-def find_file(file)
-  file = file.gsub(/^~/, ENV["HOME"])
-  if not file.match(/^\.?\//)
-    file = `find . -ipath '*/build/*' -prune -o -path '*#{file}' -print -quit`.chomp
-  end
-  raise RuleError.new() if (file.length == 0 || (not File.exist?(file)))
-  file
-end
-
-def mimetype(regex)
-  mtype = `file --mime-type #{$item} | cut -d' ' -f2`
-  raise RuleError.new() if not mtype.match(regex)
-  mtype
-end
-
-# Builtin Rules
-#-------------------------------------------------------------------------------
-
-# Run user rules first
-if File.exists?(UserRules)
-  load UserRules
-end
-
-# open urls in the browser
-rule do
-  match /(https?|ftp):\/\/[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*\/?[a-zA-Z0-9_?,%#~&\/\-+=]+([:.][a-zA-Z0-9_?,%#~&\/\-+=]+)*/
-  open_with :browser
-end
-
-# open html files with browser
-rule do
-  match /^.+\.html?/
-  $item = find_file($item)
-  open_with :browser
-end
-
-# open files with address in the text editor
-rule do
-  match /^([^:]+):([0-9]+)/
-  f = find_file($match[1])
-  $item = "#{f}:#{$match[2]}"
-  open_with :editor
-end
-
-# if the file is a text file, edit it
-rule do
-  $item = find_file($item)
-  mimetype /^text\//
-  open_with :editor
-end
-
-# Main Execution
-#-------------------------------------------------------------------------------
-
-Rules.each do |rule|
-  begin
-    rule.call($item)
-    exit 0 # Found a match, positive response
-  rescue RuleError
-  end
-end
-exit 1 # No match return error
diff --git a/tide.c b/tide.c
index c73eff149a2a0b05fe4d846a7ada1cd51e485658..6d0ea9ede7fff21b589991589ddf955231561990 100644 (file)
--- a/tide.c
+++ b/tide.c
@@ -36,7 +36,7 @@ char* PickTagCmd[] = { "picktag", NULL, "tags", NULL, NULL };
 char* OpenCmd[] = { "tide", NULL, NULL };
 
 /* Try to fetch the text with tide-fetch */
-char* FetchCmd[] = { "tide-fetch.rb", NULL, NULL };
+char* FetchCmd[] = { "tfetch", NULL, NULL };
 
 /* Tag/Cmd Execution
  ******************************************************************************/