Added simple script parsing function

This commit is contained in:
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 allowing some customization to the prompt, and per user file. I also plan to
form processing of for, while, and if statements. form processing of for, while, and if statements.
Current features
-------
Simple top down script parsing functions
Compiling & Running Compiling & Running
------- -------

View File

@ -242,7 +242,7 @@ void cdsh_loop(void)
int status; int status;
do { do {
printf("> "); printf("$ ");
line = cdsh_read_line(); line = cdsh_read_line();
args = cdsh_split_line(line); args = cdsh_split_line(line);
status = cdsh_execute(args); status = cdsh_execute(args);
@ -261,20 +261,18 @@ void cdsh_loop(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// Load config files, if any. // 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" printf("usage: cdsh [file location]\n"
"Example: cdsh /path/to/script.sh \n"); "Example: cdsh /path/to/script.sh \n");
cdsh_exit(0); // exit fflush(stdout);
} else if (argc == 2) { cdsh_exit(0); // exit
} else if (argc == 2 && file_exists(argv[1])==0) {
parse(argv[1]); parse(argv[1]);
cdsh_exit(0);
} }
// Run command loop.
cdsh_loop();
// Perform any shutdown/cleanup. // Perform any shutdown/cleanup.
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

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