From 268b3d1d41596a0a524ad0a888eacbda96b2a144 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 24 Jul 2017 21:18:42 -0400 Subject: [PATCH] switched from ruby script to tfetch --- tfetch.c | 39 +++++++++++-------- tide-fetch.rb | 106 -------------------------------------------------- tide.c | 2 +- 3 files changed, 24 insertions(+), 123 deletions(-) delete mode 100755 tide-fetch.rb diff --git a/tfetch.c b/tfetch.c index bf56757..86185ea 100644 --- 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 index 287b4ae..0000000 --- a/tide-fetch.rb +++ /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 c73eff1..6d0ea9e 100644 --- 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 ******************************************************************************/ -- 2.49.0