package com.jcomeau.cestart; import ewex.registry.*; import ewe.sys.Vm; public class Start { public static String commandString(String filename) { String extension = null; RegistryKey extensionKey = null; int rootKey = Registry.HKEY_CLASSES_ROOT; String command = null; try { extension = filename.substring(filename.lastIndexOf('.')); Vm.debug("extension=" + extension); extensionKey = Registry.getLocalKey(rootKey, extension, false, false); Vm.debug("extensionKey=" + extensionKey); if (extensionKey == null) throw new Exception("invalid key for extension " + extension); command = (String)Registry.getLocalKey(rootKey, extensionKey.getValue(null) + "\\Shell\\Open\\Command", false, false).getValue(null); return command; } catch (Exception ignored) { Vm.debug("caught: " + ignored); return null; } } public static boolean start(String program, String args) { ewe.sys.Process process = null; String command = commandString(program); int errorlevel = 0; Vm.debug("program: " + program + ", args: " + args); Vm.debug("command: " + command); if (command != null) { try { command = fixup(command, program, args); process = Vm.exec(command); process.waitFor(); if ((errorlevel = process.exitValue()) > 0) throw new Exception( "Process failed with errorlevel " + errorlevel); } catch (Exception ignored) { Vm.debug("Failed executing '" + command + "': " + ignored); return false; } } else return false; return true; } public static String join(String joint, int start, String[] parts) { String joined = parts[start]; for (int i = start + 1; i < parts.length; i++) { joined = joined + joint + parts[i]; } return joined; } public static String replace(String source, String variable, String replacement) { int index = 0; String result = source; while ((index = result.indexOf(variable)) >= 0) { result = result.substring(0, index) + replacement + result.substring(index + variable.length()); } return result; } public static String fixup(String command, String filename, String args) { String result = command; result = replace(result, "%1", filename); result = replace(result, "%*", args); return result; } public static void main(String[] args) { Vm.startEwe(args); Vm.debug("Platform: " + Vm.getPlatform() + ", Debugging: " + Vm.getProperty("DEBUGGING", "false")); Vm.debug("args: " + args.length + ": " + args); if (args.length == 1) { start(args[0], null); } else if (args.length > 1) { start(args[0], join(" ", 1, args)); } else { Vm.debug("Usage: Start filename.ext [ARG [...]]"); } if (Vm.getPlatform() != "Win32" || Vm.getProperty("DEBUGGING", null) == null) Vm.exit(0); } }