Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 490f09a

Browse files
committed
Code Update
1 parent 9570575 commit 490f09a

File tree

10 files changed

+210
-450
lines changed

10 files changed

+210
-450
lines changed

commons/src/net/bc100dev/commons/utils/Utility.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package net.bc100dev.commons.utils;
22

3+
import net.bc100dev.commons.utils.io.FileUtil;
4+
35
import java.io.File;
6+
import java.io.IOException;
47
import java.io.PrintWriter;
58
import java.io.StringWriter;
69
import java.net.URISyntaxException;
10+
import java.util.ArrayList;
711
import java.util.Base64;
12+
import java.util.List;
813
import java.util.Random;
914

15+
import static net.bc100dev.commons.utils.RuntimeEnvironment.getOperatingSystem;
16+
1017
public class Utility {
1118

1219
public static byte[] base64EncodeStr(byte[] data, int times) {
@@ -144,4 +151,50 @@ public static int getRandomInteger(int min, int max) {
144151
return new Random().nextInt(max - min + 1) + min;
145152
}
146153

154+
public static File getBinaryPath(String name) throws IOException {
155+
String pathEnv = System.getenv("PATH");
156+
if (pathEnv == null)
157+
throw new NullPointerException("PATH has been received as null");
158+
159+
List<String> passedPathEntries = new ArrayList<>();
160+
List<String> passedPathContents = new ArrayList<>();
161+
162+
String[] pathArr = pathEnv.split(File.pathSeparator);
163+
for (String pathEntry : pathArr) {
164+
if (passedPathEntries.contains(pathEntry))
165+
continue;
166+
167+
passedPathEntries.add(pathEntry);
168+
169+
File fPathEntry = new File(pathEntry);
170+
if (!fPathEntry.exists())
171+
continue;
172+
173+
if (!fPathEntry.isDirectory())
174+
continue;
175+
176+
List<String> pathContents = FileUtil.listDirectory(pathEntry, true, false);
177+
for (String pathContent : pathContents) {
178+
if (passedPathContents.contains(pathContent))
179+
continue;
180+
181+
passedPathContents.add(pathContent);
182+
183+
File bin = new File(pathContent);
184+
if ((bin.exists() && bin.canExecute()) && (getOperatingSystem() == OperatingSystem.WINDOWS ?
185+
bin.getName().equalsIgnoreCase(name) : bin.getName().equals(name))) {
186+
passedPathContents.clear();
187+
passedPathEntries.clear();
188+
189+
return bin;
190+
}
191+
}
192+
}
193+
194+
passedPathContents.clear();
195+
passedPathEntries.clear();
196+
197+
return null;
198+
}
199+
147200
}

modapi/src/osintgram4j/api/sh/Shell.java

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package osintgram4j.api.sh;
22

33
import net.bc100dev.commons.*;
4+
import net.bc100dev.commons.utils.RuntimeEnvironment;
45
import net.bc100dev.commons.utils.Utility;
56
import net.bc100dev.commons.utils.io.UserIO;
67
import osintgram4j.commons.ShellConfig;
@@ -14,8 +15,7 @@
1415
import java.util.jar.JarFile;
1516
import java.util.jar.Manifest;
1617

17-
import static net.bc100dev.commons.Terminal.TermColor.RED;
18-
import static net.bc100dev.commons.Terminal.TermColor.YELLOW;
18+
import static net.bc100dev.commons.Terminal.TermColor.*;
1919
import static net.bc100dev.commons.utils.RuntimeEnvironment.*;
2020
import static osintgram4j.commons.AppConstants.log;
2121

@@ -177,7 +177,7 @@ public void addCommandsFromJar(File file) throws IOException, ShellException {
177177
* @param line The line to get a configuration parsed
178178
*/
179179
private void assignCfg(String line) {
180-
log.info("adding \"" + line + "\"");
180+
log.info("AddEnv \"" + line + "\"");
181181

182182
if (line.contains("=")) {
183183
String[] opt = line.split("=", 2);
@@ -233,6 +233,88 @@ private void assignCfg(String line) {
233233
}
234234
}
235235

236+
private void assignAlias(String line) {
237+
line = line.replaceFirst("%", "");
238+
239+
String[] opts = Tools.translateCmdLine(line);
240+
if (opts.length == 0) {
241+
log.warning("AliasCreationError(Length == 0)");
242+
Terminal.errPrintln(RED, "no alias name given", true);
243+
return;
244+
}
245+
246+
String aliasName = opts[0];
247+
String cmdName = null;
248+
List<String> execArgs = new ArrayList<>();
249+
250+
if (opts.length > 1) {
251+
cmdName = opts[1];
252+
253+
if (opts.length > 2)
254+
execArgs.addAll(Arrays.asList(opts).subList(2, opts.length));
255+
}
256+
257+
if (opts.length == 1) {
258+
aliasName = opts[0];
259+
260+
for (ShellAlias alias : shellAliases) {
261+
if (!aliasName.equals(alias.getAliasCmd()))
262+
continue;
263+
264+
Terminal.print(CYAN, aliasName + ": ", false);
265+
Terminal.print(BLUE, "aliased to ", false);
266+
Terminal.print(CYAN, alias.getCaller().getCommand(), false);
267+
Terminal.print(BLUE, ", with arguments \"", false);
268+
269+
String[] _args = alias.getExecutionArgs();
270+
for (int i = 0; i < _args.length; i++) {
271+
Terminal.print(CYAN, _args[i], false);
272+
273+
if (i != _args.length - 1)
274+
Terminal.print(CYAN, " ", false);
275+
}
276+
277+
Terminal.println(CYAN, "\"", true);
278+
break;
279+
}
280+
281+
return;
282+
}
283+
284+
ShellCaller execCall = null;
285+
for (ShellCaller caller : shellCallers) {
286+
if (cmdName.equals(caller.getCommand()))
287+
execCall = caller;
288+
}
289+
290+
if (execCall == null) {
291+
Terminal.errPrintln(RED, "No command by the name of \"" + cmdName + "\" found", true);
292+
return;
293+
}
294+
295+
String[] sExecArgs = new String[execArgs.size()];
296+
for (int i = 0; i < execArgs.size(); i++)
297+
sExecArgs[i] = execArgs.get(i);
298+
299+
for (int i = 0; i < shellAliases.size(); i++) {
300+
ShellAlias alias = shellAliases.get(i);
301+
302+
if (aliasName.equals(alias.getAliasCmd())) {
303+
ShellAlias nAlias = new ShellAlias(aliasName, execCall, sExecArgs);
304+
nAlias.allowPlatformSupport(getOperatingSystem(), true);
305+
shellAliases.set(i, nAlias);
306+
307+
return;
308+
}
309+
}
310+
311+
ShellAlias nAlias = new ShellAlias(aliasName, execCall, sExecArgs);
312+
nAlias.allowPlatformSupport(getOperatingSystem(), true);
313+
shellAliases.add(nAlias);
314+
315+
log.info(String.format("MkAlias(\"%s\", Cmd=\"%s\", ArgsLen=\"%d\")", aliasName, cmdName, execArgs.size()));
316+
}
317+
236318
private ShellExecution getExecutionLine(String line) {
237319
String[] lnSplits = Tools.translateCmdLine(line);
238320

@@ -267,6 +349,11 @@ private void cmd() {
267349
continue;
268350
}
269351

352+
if (ln.startsWith("%")) {
353+
assignAlias(ln);
354+
continue;
355+
}
356+
270357
ShellExecution execution = getExecutionLine(ln);
271358
if (execution == null)
272359
continue;
@@ -276,7 +363,7 @@ private void cmd() {
276363

277364
switch (exec) {
278365
case "exit", "quit", "close" -> stopShell();
279-
default -> execCommand(shellConfigList, exec, givenArgs);
366+
default -> execCommand(exec, givenArgs);
280367
}
281368
} catch (NoSuchElementException ignore) {
282369
// This exception can be ignored: can occur, when a pipe is being used in the Shell Execution Code
@@ -292,11 +379,10 @@ private void cmd() {
292379
/**
293380
* Executes a command from the parsed line.
294381
*
295-
* @param env The environment parameter
296382
* @param exec The command parameter
297383
* @param args Given Shell command parameters
298384
*/
299-
private void execCommand(List<ShellConfig> env, String exec, String[] args) {
385+
private void execCommand(String exec, String[] args) {
300386
log.info(String.format("CreateCommandExec(Command=\"%s\", Args=\"%s\")", exec, Arrays.toString(args)));
301387

302388
int rnd = Utility.getRandomInteger(0, 100000);
@@ -324,7 +410,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
324410
Terminal.println(YELLOW, String.format("%s: command deprecated", exec), true);
325411
}
326412

327-
int code = caller.execute(args, env);
413+
int code = caller.execute(args, shellConfigList);
328414

329415
log.info("CommandExecution(Code=" + code + ", Cmd=" + exec + ")");
330416

@@ -349,7 +435,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
349435

350436
try {
351437
log.info("CommandRun(MC_Alternate; " + exec + ", " + Arrays.toString(args) + ")");
352-
int code = caller.execute(args, env);
438+
int code = caller.execute(args, shellConfigList);
353439
log.info("CommandExecution(MC_Alternate; Code=" + code + ", Cmd=" + exec + ")");
354440

355441
if (code != 0) {
@@ -375,7 +461,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
375461
if (cmdFound) {
376462
try {
377463
log.info("AliasExecute(Command=" + alias.getCaller().getCommand() + "; DefArgs=" + Arrays.toString(alias.getExecutionArgs()) + "; AdditionalArgs=" + Arrays.toString(args) + ")");
378-
int code = alias.execute(args, env);
464+
int code = alias.execute(args, shellConfigList);
379465
log.info("AliasExecution(Code=" + code + ")");
380466
} catch (ShellException ex) {
381467
Terminal.println(Terminal.TermColor.RED, Utility.throwableToString(ex), true);

modapi/src/osintgram4j/api/sh/ShellAlias.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public ShellAlias(String aliasCmd, ShellCaller caller, String[] executionArgs) {
2222
this.aliasCmd = aliasCmd;
2323
this.caller = caller;
2424
this.executionArgs = executionArgs;
25+
26+
this.osSupportList.put(OperatingSystem.LINUX, Boolean.FALSE);
27+
this.osSupportList.put(OperatingSystem.WINDOWS, Boolean.FALSE);
28+
this.osSupportList.put(OperatingSystem.MAC_OS, Boolean.FALSE);
2529
}
2630

2731
public String getAliasCmd() {

modapi/src/osintgram4j/api/sh/ShellCaller.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package osintgram4j.api.sh;
22

3-
import osintgram4j.api.Launcher;
43
import osintgram4j.commons.ShellConfig;
54

6-
import java.io.IOException;
75
import java.lang.reflect.InvocationTargetException;
86
import java.lang.reflect.Method;
97
import java.util.List;
@@ -33,8 +31,8 @@ public ShellCaller(boolean deprecated, String command, String helpDesc, String c
3331
this.callableClass = Class.forName(callableClassName);
3432
this.classInstance = callableClass.getDeclaredConstructor().newInstance();
3533

36-
if (!callableClass.isAssignableFrom(Command.class))
37-
throw new ShellException("Launch class " + callableClassName + " is not extending \"Launcher.class\"");
34+
if (callableClass.getSuperclass() != Command.class)
35+
throw new ShellException("Launch class " + callableClassName + " is not extending \"Command.class\"");
3836

3937
Method[] callableMethodArr = this.callableClass.getDeclaredMethods();
4038
if (callableMethodArr.length == 0)

modapi/src/osintgram4j/api/sh/ShellCommandEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected static ShellCommandEntry parseJsonFile(String contents) throws ShellEx
128128
alternates[j] = alternatesList.get(j);
129129

130130
String cmd, description, _class;
131-
if (cmdObj.get("cmd") instanceof String ss0 &&
131+
if (cmdObj.get("command") instanceof String ss0 &&
132132
cmdObj.get("description") instanceof String ss1 &&
133133
cmdObj.get("class") instanceof String ss2) {
134134
cmd = ss0;

0 commit comments

Comments
 (0)