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;
(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 }
},
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;
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;
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;
}
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;
}
if (!apply_rule(rule))
break;
}
- puts("");
+ //puts("");
}
return 1;
}
+++ /dev/null
-#!/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