1
1
package osintgram4j .api .sh ;
2
2
3
3
import net .bc100dev .commons .*;
4
+ import net .bc100dev .commons .utils .RuntimeEnvironment ;
4
5
import net .bc100dev .commons .utils .Utility ;
5
6
import net .bc100dev .commons .utils .io .UserIO ;
6
7
import osintgram4j .commons .ShellConfig ;
14
15
import java .util .jar .JarFile ;
15
16
import java .util .jar .Manifest ;
16
17
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 .*;
19
19
import static net .bc100dev .commons .utils .RuntimeEnvironment .*;
20
20
import static osintgram4j .commons .AppConstants .log ;
21
21
@@ -177,7 +177,7 @@ public void addCommandsFromJar(File file) throws IOException, ShellException {
177
177
* @param line The line to get a configuration parsed
178
178
*/
179
179
private void assignCfg (String line ) {
180
- log .info ("adding \" " + line + "\" " );
180
+ log .info ("AddEnv \" " + line + "\" " );
181
181
182
182
if (line .contains ("=" )) {
183
183
String [] opt = line .split ("=" , 2 );
@@ -233,6 +233,88 @@ private void assignCfg(String line) {
233
233
}
234
234
}
235
235
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
+
236
318
private ShellExecution getExecutionLine (String line ) {
237
319
String [] lnSplits = Tools .translateCmdLine (line );
238
320
@@ -267,6 +349,11 @@ private void cmd() {
267
349
continue ;
268
350
}
269
351
352
+ if (ln .startsWith ("%" )) {
353
+ assignAlias (ln );
354
+ continue ;
355
+ }
356
+
270
357
ShellExecution execution = getExecutionLine (ln );
271
358
if (execution == null )
272
359
continue ;
@@ -276,7 +363,7 @@ private void cmd() {
276
363
277
364
switch (exec ) {
278
365
case "exit" , "quit" , "close" -> stopShell ();
279
- default -> execCommand (shellConfigList , exec , givenArgs );
366
+ default -> execCommand (exec , givenArgs );
280
367
}
281
368
} catch (NoSuchElementException ignore ) {
282
369
// 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() {
292
379
/**
293
380
* Executes a command from the parsed line.
294
381
*
295
- * @param env The environment parameter
296
382
* @param exec The command parameter
297
383
* @param args Given Shell command parameters
298
384
*/
299
- private void execCommand (List < ShellConfig > env , String exec , String [] args ) {
385
+ private void execCommand (String exec , String [] args ) {
300
386
log .info (String .format ("CreateCommandExec(Command=\" %s\" , Args=\" %s\" )" , exec , Arrays .toString (args )));
301
387
302
388
int rnd = Utility .getRandomInteger (0 , 100000 );
@@ -324,7 +410,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
324
410
Terminal .println (YELLOW , String .format ("%s: command deprecated" , exec ), true );
325
411
}
326
412
327
- int code = caller .execute (args , env );
413
+ int code = caller .execute (args , shellConfigList );
328
414
329
415
log .info ("CommandExecution(Code=" + code + ", Cmd=" + exec + ")" );
330
416
@@ -349,7 +435,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
349
435
350
436
try {
351
437
log .info ("CommandRun(MC_Alternate; " + exec + ", " + Arrays .toString (args ) + ")" );
352
- int code = caller .execute (args , env );
438
+ int code = caller .execute (args , shellConfigList );
353
439
log .info ("CommandExecution(MC_Alternate; Code=" + code + ", Cmd=" + exec + ")" );
354
440
355
441
if (code != 0 ) {
@@ -375,7 +461,7 @@ private void execCommand(List<ShellConfig> env, String exec, String[] args) {
375
461
if (cmdFound ) {
376
462
try {
377
463
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 );
379
465
log .info ("AliasExecution(Code=" + code + ")" );
380
466
} catch (ShellException ex ) {
381
467
Terminal .println (Terminal .TermColor .RED , Utility .throwableToString (ex ), true );
0 commit comments