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
|
microshell: microshell.c
|
||||||
gcc -Wall -ansi -o microshell microshell.c
|
gcc -Wall -ansi -o microshell microshell.c -lreadline -lncurses
|
||||||
clean:
|
clean:
|
||||||
rm microshell
|
rm microshell
|
||||||
|
@ -7,9 +7,9 @@ Index number=s444341
|
|||||||
Limits
|
Limits
|
||||||
Max length of line=1000
|
Max length of line=1000
|
||||||
Max number of arguments=100
|
Max number of arguments=100
|
||||||
Max length of argument=100
|
|
||||||
-------------
|
-------------
|
||||||
Working commands
|
Working commands
|
||||||
|
up/down arrow - commands history
|
||||||
help - manual
|
help - manual
|
||||||
exit - ends microshell
|
exit - ends microshell
|
||||||
cd - changing directory
|
cd - changing directory
|
||||||
@ -20,9 +20,11 @@ spath:
|
|||||||
0 - turns off displaying current directory
|
0 - turns off displaying current directory
|
||||||
1 - turns on displaying current directory
|
1 - turns on displaying current directory
|
||||||
PATH commands
|
PATH commands
|
||||||
|
if you want colors use
|
||||||
|
for example ls --color
|
||||||
|
|
||||||
WARNING
|
WARNING
|
||||||
dont use path shorcuts
|
dont use enviroment shorcuts
|
||||||
example cd ~/
|
example cd ~/
|
||||||
or echo $HOME
|
or echo $HOME
|
||||||
-------------
|
-------------
|
||||||
|
Binary file not shown.
@ -7,35 +7,21 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
|
||||||
const int max = 1000;
|
const int max = 1000;
|
||||||
|
|
||||||
int cut(char **argv, char *command){
|
void cut(char **argv, char *command){
|
||||||
int i = 0, w = 0, x = 0;
|
int w = 0;
|
||||||
char argument[100];
|
char sep[] = " ";
|
||||||
while (1){
|
char *ptr = strtok(command, sep);
|
||||||
if(command[i] == 10){
|
while (ptr != NULL){
|
||||||
argv[w] = NULL;
|
argv[w]=ptr;
|
||||||
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;
|
w+=1;
|
||||||
|
ptr = strtok(NULL, sep);
|
||||||
}
|
}
|
||||||
}
|
argv[w]=NULL;
|
||||||
return w+1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cd (char **argv){
|
void cd (char **argv){
|
||||||
@ -78,21 +64,39 @@ void spathf (char **argv, int *spath){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main (){
|
int main (){
|
||||||
int ile, i, spath = 1;
|
int spath = 1;
|
||||||
|
using_history();
|
||||||
while (1){
|
while (1){
|
||||||
char* argv[100];
|
char* argv[100];
|
||||||
char komenda[max], directory[max];
|
char *komenda, *prompt, directory[max];
|
||||||
if (getcwd(directory, sizeof(directory)) == NULL){
|
if (getcwd(directory, sizeof(directory)) == NULL){
|
||||||
perror("getcwd() error");
|
perror("getcwd() error");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
struct passwd *p = getpwuid(getuid());
|
struct passwd *p = getpwuid(getuid());
|
||||||
/*dodac obsluge bledow do get?*/
|
|
||||||
if (spath)
|
if (spath){
|
||||||
printf("[%s]", directory);
|
prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char));
|
||||||
printf("%s$ ", p->pw_name); /* mozna zrobic (*p).pw_name */
|
prompt[0]='\0';
|
||||||
fgets(komenda, max, stdin);
|
strcat(prompt, "\033[1;32m");
|
||||||
ile = cut(argv, komenda);
|
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)
|
if (argv[0]==NULL)
|
||||||
continue;
|
continue;
|
||||||
@ -124,8 +128,8 @@ int main (){
|
|||||||
else
|
else
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
|
|
||||||
for(i=0; i<ile; i++)
|
free(komenda);
|
||||||
free(argv[i]);
|
free(prompt);
|
||||||
}
|
}
|
||||||
return 0;
|
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