Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit c199b92

Browse files
author
NerdOfCode
committed
Added file/directory autocomplete
1 parent 03992f0 commit c199b92

7 files changed

Lines changed: 51 additions & 90 deletions

File tree

Bin/cmd_src/cd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void audit_dir();
1212

1313
int main(int argc, char *argv[]){
1414

15-
int result;
15+
int result = 0;
1616
char *copy = argv[1];
1717

1818
if( argc > 1 ){

Bin/cmd_src/nano.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ then
1414
exit -1
1515
fi
1616

17+
#Check how many args
18+
if [[ $# -lt 1 ]]
19+
then
20+
echo "Formatting: nano test.sh"
21+
exit -1
22+
fi
23+
1724
path="$(readlink -f $1)"
1825

1926
if [[ "$path" =~ "$DEFAULT_LOCATION"* ]]

Bin/nano

Lines changed: 0 additions & 32 deletions
This file was deleted.

Bin/version

0 Bytes
Binary file not shown.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ clean:
99
shell:
1010
rm -f Src/shell~
1111

12-
$C -std=gnu11 -O -o Src/shell Src/shell.c
12+
$C -std=gnu11 -O -o Src/shell Src/shell.c -lreadline
1313

1414
commands:
1515

Src/globals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
//Use 43m for background shading
5959
#define RESET "\x1B[0m"
6060

61-
#define LATEST_VERSION "0.011"
61+
#define LATEST_VERSION "0.020"
6262

6363
//Warning: If you're considering changing the below...
6464
//Make sure to adjust the Bin/nano file... Thank you!

Src/shell.c

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <sys/stat.h>
4040
#include <sys/wait.h>
4141
#include <sys/types.h>
42+
#include <readline/readline.h>
43+
#include <readline/history.h>
4244
#include "globals.h"
4345

4446

@@ -72,7 +74,8 @@ unsigned int mini_kernel_panic_counter;
7274

7375
int main ( int argc, char argv[64] ){
7476

75-
char input[64] = "";
77+
char input[256] = "";
78+
char *pinput = "";
7679
char *hostname = "";
7780

7881
//Test if user is allowed to use pwd and if allowed show the working directory
@@ -137,12 +140,13 @@ int main ( int argc, char argv[64] ){
137140
puts("Error getting 'current working directory'.");
138141
//Remove all characters up to last one...
139142
short_pwd = remove_char_until(pwd_buffer, "/");
140-
printf(YELLOW_TEXT "%s@%s[%s]: " RESET, logged_in_user,hostname,short_pwd);
141-
//printf("CD BUFFER: %s\n",cd_buffer);
142-
143-
if(fgets(input,sizeof(input),stdin) == NULL)
144-
if(DEBUG)
145-
puts("Error retrieving input.");
143+
printf(YELLOW_TEXT "%s@%s[%s] " RESET, logged_in_user,hostname,short_pwd);
144+
pinput = readline("->");
145+
add_history(pinput);
146+
strncpy(input, pinput, 64);
147+
// if(fgets(input,sizeof(input),stdin) == NULL)
148+
// if(DEBUG)
149+
// puts("Error retrieving input.");
146150

147151
}else if(adv_desc_access.whoami_allowed == TRUE){
148152
printf(YELLOW_TEXT "%s@%s: " RESET, logged_in_user,hostname);
@@ -169,14 +173,14 @@ int main ( int argc, char argv[64] ){
169173

170174
//Check to see if user wants to exit before re-running loop
171175
//Have to check for newline too, because of fgets for input
172-
if(strncmp(input,"exit\n",sizeof("exit\n")) == 0){
176+
if(strncmp(input,"exit",sizeof("exit")) == 0){
173177
clean_up();
174178
exit(1);
175-
}else if(strncmp(input,"help\n",sizeof("help\n")) == 0){
179+
}else if(strncmp(input,"help",sizeof("help")) == 0){
176180
help_commands();
177-
}else if(strncmp(input,"cmds\n",sizeof("cmds\n")) == 0){
181+
}else if(strncmp(input,"cmds",sizeof("cmds")) == 0){
178182
commands();
179-
}else if(strncmp(input," \n",sizeof(" \n")) == 0){
183+
}else if(strncmp(input," ",sizeof(" ")) == 0){
180184
printf("He - He\n");
181185
}else{
182186
if(check_empty_beginning(input) <= -1){
@@ -329,7 +333,7 @@ char *remove_char_until(char specified_buffer[128],char remove_char[2]){
329333
return remove_char_result;
330334
}
331335

332-
int check_empty_beginning(char input[64]){
336+
int check_empty_beginning(char input[256]){
333337
//0 --> No space at beggining
334338
//-1 --> A single space at index 0
335339
//-2 --> Two spaces at beggining
@@ -346,18 +350,18 @@ int check_empty_beginning(char input[64]){
346350
}
347351
}
348352

349-
int parseCommand(char input[64]){
353+
int parseCommand(char input[256]){
350354

351-
char *pfilename;
352-
char *pcommand;
355+
char *pfilename = "";
356+
char *pcommand = "" ;
353357
bool command_args = FALSE;
354358
int command_status = 0;
355359

356360
//The pcommand is for the user input but without any arguments attached
357361

358362
//Dynamic memory allocation
359-
pfilename = malloc(64 * sizeof(char));
360-
pcommand = malloc(64 * sizeof(char));
363+
pfilename = malloc(256 * sizeof(char));
364+
pcommand = malloc(256 * sizeof(char));
361365

362366
//Check for any allocation errors before saving input
363367
if(pfilename == NULL || pcommand == NULL){
@@ -366,7 +370,7 @@ int parseCommand(char input[64]){
366370
}
367371

368372
//Just in case, check for an empty command
369-
if(input[0] == '\n'){
373+
if(input[0] == '\0'){
370374
return -1;
371375
}
372376

@@ -384,73 +388,64 @@ int parseCommand(char input[64]){
384388
//Remove all arguments
385389
for(int i = 0; i <= strlen(input); ++i){
386390
if(input[i] != ' '){
391+
//Delete args
387392
pcommand[i] = input[i];
388-
command_args = TRUE;
389-
}else{
393+
pcommand[i+1] = '\0';
390394
command_args = FALSE;
395+
}else{
396+
command_args = TRUE;
397+
pcommand[i+1] = '\0';
391398
break;
392399
}
393-
394400
}
401+
402+
403+
if(DEBUG)
404+
printf("command_args: %s\n",command_args ? "False" : "True");
395405

396406
//Obliterate pfilename
397407
//And check if command exists relative to its filename
408+
//Convert pcommand into pfilename with absolute path
398409
memset(pfilename, 0, sizeof(pfilename));
399410
strncat(pfilename,CMD_BIN, sizeof(CMD_BIN) + sizeof(pfilename));
400411
strncat(pfilename,pcommand, sizeof(pfilename) + sizeof(pcommand));
401412

402-
//Only remove newline if command has arguments
403-
if(command_args){
404-
pfilename[strlen(pfilename)-1] = '\0';
405-
}
406-
407-
pcommand[strlen(pcommand)-1] = '\0';
408-
input[strlen(input)-1] = '\0';
409-
410413
//TEMPORARY FIX!!! 4/20/18
411414
//SPECIAL CASE: If command is cd, give a heads up to update the cwd
412-
if(strncmp(pcommand,"c", sizeof(pcommand)) == 0){
415+
if(strncmp(pcommand,"cd", sizeof(pcommand)) == 0){
413416
update_new_cd(1);
414417
}
415418

416419
//If command or rather file is found, proceed
417420
if(access(pfilename, F_OK) == 0){
418421
//Since the command exists we can try running the arguments the user has provided
419422
//Check if args and no args are different
420-
if(input != pcommand){
421-
423+
if(strncmp(input, pcommand, sizeof(pcommand)) != 0){
424+
//ARGS
422425
//Reset to default users args
423-
memset(pfilename, 0, 64);
426+
memset(pfilename, 0, 256);
424427
strncat(pfilename, CMD_BIN, sizeof(CMD_BIN) + sizeof(pfilename));
425428
strncat(pfilename, input, sizeof(CMD_BIN) + sizeof(pfilename));
429+
426430
if(system(pfilename) == -1)
427431
if(DEBUG)
428432
printf("Error executing: %s\n",pfilename);
429433
}else{
434+
//NO ARGS
430435
if(system(pfilename) == -1 )
431436
if(DEBUG)
432437
printf("Error executing: %s\n",pfilename);
433438
}
434439
}else{
435-
//TODO
440+
//Could not find file
436441

437-
//Check if command is an alias
438-
439-
if(access(pfilename, F_OK) == 0){
440-
441-
if(system(pfilename) == -1)
442-
if(DEBUG)
443-
puts("Error checking alias.");
444-
445-
}else{
446-
447442
puts(RED_TEXT"Command Not Found!"RESET);
448443
if(DEBUG){
449444
//Will show the pathway to file
450445
//We can safely ignore return value
451-
int xyz = system(pfilename);
446+
int ignore_me = system(pfilename);
452447
}
453-
}
448+
//}
454449
}
455450

456451

@@ -509,15 +504,6 @@ int update_new_cd( int update ){
509504
short int ret = chdir(cd_buffer);
510505
}
511506

512-
513-
/*
514-
if(getcwd(cwd, sizeof(cwd)) != NULL){
515-
printf("Current working dir: %s\n", cwd);
516-
}else{
517-
fprintf(stderr, "cwd() error");
518-
}
519-
*/
520-
521507
return 0;
522508
}
523509

0 commit comments

Comments
 (0)