Added simple script parsing function

master
Chris Dorman 2020-11-24 07:53:15 +08:00
parent 55513bcad0
commit 95bd87a73b
4 changed files with 34 additions and 27 deletions

View File

@ -16,6 +16,11 @@ I do plan to give this the ability to be scripted and further as well has
allowing some customization to the prompt, and per user file. I also plan to
form processing of for, while, and if statements.
Current features
-------
Simple top down script parsing functions
Compiling & Running
-------

View File

@ -242,7 +242,7 @@ void cdsh_loop(void)
int status;
do {
printf("> ");
printf("$ ");
line = cdsh_read_line();
args = cdsh_split_line(line);
status = cdsh_execute(args);
@ -261,19 +261,17 @@ void cdsh_loop(void)
int main(int argc, char **argv)
{
// Load config files, if any.
if(argc < 2 || !strcmp(argv[1], "-?") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
if (argc == 1) {
// If cdsh is called by itself, just pull prompt up
cdsh_loop();
} else if(argc > 2 || !strcmp(argv[1], "-?") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
printf("usage: cdsh [file location]\n"
"Example: cdsh /path/to/script.sh \n");
cdsh_exit(0); // exit
} else if (argc == 2) {
fflush(stdout);
cdsh_exit(0); // exit
} else if (argc == 2 && file_exists(argv[1])==0) {
parse(argv[1]);
cdsh_exit(0);
}
// Run command loop.
cdsh_loop();
// Perform any shutdown/cleanup.
return EXIT_SUCCESS;

View File

@ -53,35 +53,35 @@ void parse(char *filename)
// If its the crunchbang, note for now
if (strncmp("#!",line,2)==0) {
printf("cdsh: found the crunchbang\n");
continue;
}
// if line is commented out, skip
if (strncmp("#",line,1)==0) {
printf("cdsh: skipping comment line in file...\n");
continue;
}
if (strncmp(newline,line,1)==0) {
printf("cdsh: skipping empty line in file...\n");
continue;
}
if (strncmp("exit",cfline,4)==0 || strncmp("EXIT",cfline,4)==0) {
cdsh_exit(0);
} /*else {
int x;
char *command;
for(x = 0; x++; x>=strlen(cfline)) {
strncat(command, &cfline[x], 1);
}
cdsh_execute(&command);
} */
char *argvlist[50];
int argc = 0;
char *moocow = cfline;
// Replace tabs with spaces
while((cfline = strtok(moocow, space)) != NULL) {
moocow = NULL;
argvlist[argc++] = cfline;
}
cdsh_execute(argvlist);
} // End while
} // End if file
fclose(file);
fclose(file);
cdsh_exit(0);
}

6
test.sh Normal file → Executable file
View File

@ -2,4 +2,8 @@
# This is a comment to skip
# skip this one too
exit
ls /
cd /usr
cd /