dodanie kolorow
historia komend zmiana na strtok i readline
This commit is contained in:
parent
08b7d9962a
commit
672691d576
@ -1,4 +1,4 @@
|
||||
microshell: microshell.c
|
||||
gcc -Wall -ansi -o microshell microshell.c
|
||||
gcc -Wall -ansi -o microshell microshell.c -lreadline -lncurses
|
||||
clean:
|
||||
rm microshell
|
||||
|
@ -7,9 +7,9 @@ Index number=s444341
|
||||
Limits
|
||||
Max length of line=1000
|
||||
Max number of arguments=100
|
||||
Max length of argument=100
|
||||
-------------
|
||||
Working commands
|
||||
up/down arrow - commands history
|
||||
help - manual
|
||||
exit - ends microshell
|
||||
cd - changing directory
|
||||
@ -20,9 +20,11 @@ spath:
|
||||
0 - turns off displaying current directory
|
||||
1 - turns on displaying current directory
|
||||
PATH commands
|
||||
if you want colors use
|
||||
for example ls --color
|
||||
|
||||
WARNING
|
||||
dont use path shorcuts
|
||||
dont use enviroment shorcuts
|
||||
example cd ~/
|
||||
or echo $HOME
|
||||
-------------
|
||||
|
Binary file not shown.
@ -7,35 +7,21 @@
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
||||
const int max = 1000;
|
||||
|
||||
int cut(char **argv, char *command){
|
||||
int i = 0, w = 0, x = 0;
|
||||
char argument[100];
|
||||
while (1){
|
||||
if(command[i] == 10){
|
||||
argv[w] = NULL;
|
||||
break;
|
||||
}
|
||||
else if (command[i] == 32){
|
||||
i+=1;
|
||||
x=0;
|
||||
continue;
|
||||
}
|
||||
else{
|
||||
while(command[i] != 32 && command[i]!=10){
|
||||
argument[x]=command[i];
|
||||
x+=1;
|
||||
i+=1;
|
||||
}
|
||||
argument[x] = '\0';
|
||||
argv[w]=malloc(x+1 * sizeof(char));
|
||||
strcpy(argv[w], argument);
|
||||
w+=1;
|
||||
}
|
||||
void cut(char **argv, char *command){
|
||||
int w = 0;
|
||||
char sep[] = " ";
|
||||
char *ptr = strtok(command, sep);
|
||||
while (ptr != NULL){
|
||||
argv[w]=ptr;
|
||||
w+=1;
|
||||
ptr = strtok(NULL, sep);
|
||||
}
|
||||
return w+1;
|
||||
argv[w]=NULL;
|
||||
}
|
||||
|
||||
void cd (char **argv){
|
||||
@ -78,21 +64,39 @@ void spathf (char **argv, int *spath){
|
||||
}
|
||||
|
||||
int main (){
|
||||
int ile, i, spath = 1;
|
||||
int spath = 1;
|
||||
using_history();
|
||||
while (1){
|
||||
char* argv[100];
|
||||
char komenda[max], directory[max];
|
||||
char *komenda, *prompt, directory[max];
|
||||
if (getcwd(directory, sizeof(directory)) == NULL){
|
||||
perror("getcwd() error");
|
||||
return -1;
|
||||
}
|
||||
struct passwd *p = getpwuid(getuid());
|
||||
/*dodac obsluge bledow do get?*/
|
||||
if (spath)
|
||||
printf("[%s]", directory);
|
||||
printf("%s$ ", p->pw_name); /* mozna zrobic (*p).pw_name */
|
||||
fgets(komenda, max, stdin);
|
||||
ile = cut(argv, komenda);
|
||||
|
||||
if (spath){
|
||||
prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char));
|
||||
prompt[0]='\0';
|
||||
strcat(prompt, "\033[1;32m");
|
||||
strcat(prompt, "[");
|
||||
strcat(prompt, directory);
|
||||
strcat(prompt, "]\033[0m");
|
||||
strcat(prompt, p->pw_name);
|
||||
}
|
||||
else{
|
||||
prompt = (char*)malloc(strlen(p->pw_name)+3 *sizeof(char));
|
||||
strcat(prompt, p->pw_name);
|
||||
}
|
||||
strcat(prompt, "$ ");
|
||||
|
||||
if (!(komenda = readline(prompt)))
|
||||
break;
|
||||
if (komenda[0] == 0)
|
||||
continue;
|
||||
|
||||
add_history(komenda);
|
||||
cut(argv, komenda);
|
||||
|
||||
if (argv[0]==NULL)
|
||||
continue;
|
||||
@ -124,8 +128,8 @@ int main (){
|
||||
else
|
||||
wait(NULL);
|
||||
|
||||
for(i=0; i<ile; i++)
|
||||
free(argv[i]);
|
||||
free(komenda);
|
||||
free(prompt);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
24
Microshell/opis
Normal file
24
Microshell/opis
Normal file
@ -0,0 +1,24 @@
|
||||
pobralem biblioteke zrobilem
|
||||
./configure
|
||||
make
|
||||
sudo make install
|
||||
rownie dobrze mozna zrobic apt-install
|
||||
|
||||
ldconfig -v | head
|
||||
patrze czy jest zlinkowane jak nie to dodaje ldconfig -n "sciezka lib"
|
||||
sudo ldconfig
|
||||
|
||||
dorzucam do gcc -lreadline
|
||||
ale i tak nic to nie daje
|
||||
|
||||
nie byo ncurses
|
||||
instaluje apt install
|
||||
i dorzucam do gcc -ncurses
|
||||
|
||||
|
||||
|
||||
z komentarzy
|
||||
p->pw_name
|
||||
mozna zrobic (*p).pw_name
|
||||
|
||||
/*fgets(komenda, max, stdin);*/
|
Loading…
Reference in New Issue
Block a user