|
Post by Afgncaap5 on Oct 8, 2005 20:00:29 GMT -5
Okay, maybe the conversation is a little bitter....but man, I couldn't for the life of me find the code that runs a standard Linux terminal!
I mean, you'd think that'd be one of the important parts of the source files. But for all my searching, I can't find it.
Can someone with more experience with Linux point me to where such mystical documentation might exist? If I'm going to look into this "open source" stuff, gosh darn it the source better actually be open.
-EDIT-
And in the event that "terminal" is being too vague, I'm talking about the Command Line. The part that looks kinda like DOS.
|
|
|
Post by Mitchell on Oct 9, 2005 7:57:04 GMT -5
Isn't the command prompt part of the kernel? The entire kernel should be found here: www.kernel.org/
|
|
|
Post by Afgncaap5 on Oct 9, 2005 13:55:28 GMT -5
Hmmm...I've already been there, but I couldn't find anything. Maybe I'm just not looking hard enough.
|
|
|
Post by Mitchell on Oct 9, 2005 14:34:58 GMT -5
What exactly are you trying to do?
|
|
|
Post by Afgncaap5 on Oct 9, 2005 20:58:07 GMT -5
Well, I'm basically looking for the code that intrprets commands, plus a little extra. The commands that interpret | to mean piping, the commands that read >, <, and >> as I/O redirection, and the commands that know how to background programs when & is typed.
I'm basically looking to create my own dumbed down version of such stuff...but before I can start doing that I need to see what works now.
|
|
|
Post by Mitchell on Oct 10, 2005 10:46:47 GMT -5
|
|
|
Post by Afgncaap5 on Oct 11, 2005 9:49:38 GMT -5
Oh, man...I needed this two weeks ago. Thanks, Mitchell! :-)
It's crazy hectic, let's see if I can show what I've got at the moment....
#include <sys/wait.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/utsname.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <sys/stat.h>
#define MAXARGS 64 #define BUFLEN 81
struct buffer_t{ char linebuf[BUFLEN+1]; char *arglist[MAXARGS]; };
struct buffer_t buffer;
int main(int argc, char * argv[]){ int pid, bkg; char *(args[MAXARGS+1]); char **args2;
while(1){ /*check_exit_status(); goes here*/ printf("JML>"); bkg = find_background(args); if(fgets(buffer.linebuf, BUFLEN, stdin) == NULL) break; if(!parse_args(buffer.linebuf, args)) continue;
/* if(find_pipe(args, &args2)){ run_piped_children(args, args2, bkg); } */ else{ if((pid=fork()) == -1) perror("fork"); else if(pid == 0) childproc(args); else parentproc(pid, bkg); }//end else }//end while exit(0); } // end main
//int find_pipe
int parse_args(args){ char whitespace[] = " \t\n\r"; int nargs; nargs = 0; buffer.arglist[nargs] = strtok(buffer.linebuf, whitespace); while (buffer.arglist[nargs] != NULL){ buffer.arglist[++nargs] = strtok(NULL, whitespace); }//end while return 1; }//end parse_args
int internal_command(args){ extern char **environ; if(strcmp(buffer.arglist[0], "environ")==0){ char **envp = environ; while (*envp){ printf("%s\n", *envp++); }//end while return 1; }//end if else if(strcmp(buffer.linebuf, "path")==0){ char *envpath; envpath=getenv("PATH"); printf("PATH:\n"); printf("%s\n", envpath); return 1; }//end else if else if(strcmp(buffer.linebuf, "exit")==0){ exit(0); }//end else if
return 0; }//end internal_command
int childproc(args){ int err; int nargs=0; int tempfd; int i; err=execvp(buffer.linebuf, buffer.arglist); for (i = 0; buffer.arglist != NULL; i++) if (strcmp(buffer.arglist, "<") == 0) { i++; tempfd = open(buffer.arglist, O_RDONLY, 0); /*CHECK FOR ERRORS HERE*/ close(0); dup2(tempfd, 0); close(tempfd); } if (strcmp(buffer.arglist, ">") == 0) { i++; tempfd = open(buffer.arglist, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); /*CHECK FOR ERRORS HERE*/ close(1); dup2(tempfd, 1); close(tempfd); } if (strcmp(buffer.arglist, ">>") == 0) { //SHOULD APPEND, NOT CHANGE STANDARD ERROR! CHANGE QUICKLY! i++; tempfd = open(buffer.arglist, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR); /*CHECK FOR ERRORS HERE*/ close(2); dup2(tempfd, 2); close(tempfd); } return 1; } // end childproc
int parentproc(pid, bkg){ int status = 0; // Not sure about status... pid = waitpid(-1, &status, WNOHANG); if (pid == -1) //status is requested for child process ; //Just so that the computer's not confused else if (pid > 0) //pid specifies the pid of the child process for which status is requested ; else if (pid == 0) //status is requested for any child process whose process group ID is //equal to that of the calling process. ; else if (pid < -1) //status is requested for any child process whose process group ID is //equal to the absolute value of pid. ; /* How non-backgrounding did it... while (wait(&bkg) != pid); return 1; */ }//end parentproc
int find_background(args){ //Returns 1 if the process is to be backgrounded, //0 if it is not. Backgrounding is signified with // an ampersand, "&". int j; for (j = 0; buffer.arglist != NULL; j++) { if (strcmp(buffer.arglist[j], "&") == 0) { return 1; } else j++; } return 0; } //end find_background
|
|
|
Post by Afgncaap5 on Oct 11, 2005 9:50:33 GMT -5
That's where I ran into a stumbling block after looking at that link of yours. I can't figure out how to proceed from here, and as it is the code just dumps the core.
|
|