Thread: simple shell coding

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    simple shell coding

    Hi

    I need to expand some shell code so it would be able to do more than it is doing now. Currently it can do stuff like quit when q is pressed, display a prompt but not much more. I have to make the shell have capabilities like
    cd (changing directories)
    redirection of input and output, for example date > storeddate.txt
    pipelines, for example ps -ax | grep programname
    background processes, for example gcc -c verylargeprogram.c

    etc..

    this has to be done under a POSIX 1 compliant system.

    i have been given the following code:

    -----------------------------
    Code:
    /* 
     * 	simplesh.h
     */
    
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/wait.h>
    #include <limits.h>
    
    #define  DELIMITERSET " ><|&"
    
    #ifndef  MAX_CANON
     #define MAX_CANON 256
    #endif
    
    #define  TRUE 1
    #define  FALSE 0
    #define  BLANK_STRING " "
    #define  PROMPT_STRING "-> "
    #define  QUIT_STRING "q"
    #define  BACK_STRING "&"
    #define  PIPE_STRING "|"
    #define  NEWLINE_STRING "\n"
    #define  IN_REDIRECT_SYMBOL '<'
    #define  OUT_REDIRECT_SYMBOL '>'
    #define  NULL_SYMBOL '\0'
    #define  PIPE_SYMBOL '|'
    #define  BACK_SYMBOL '&'
    #define  NEWLINE_SYMBOL '\n'
    
    int      makeargarray (char *s, char *delimiters, char ***argvp);
    int      parsefile (char *inbuf, char delimiter, char **v);
    int      redirect (char *infilename, char *outfilename);
    void     executecmdline (char *cmd);
    int      connectpipeline (char *cmd, int frontfd[], int backfd[]);
    
    -----------------------------------------------------------------------
    
    /* makeargarray.c */
    
    #include "simplesh.h"
    
    /* Make arg array (*arvp) for tokens in s which are separated by
     * delimiters. Return -1 on error, or the number of tokens otherwise.
     */
    
    int
    makeargv (char *s, char *delimiters, char ***argvp)
    {
        char *t;
        char *snew;
        int   numtokens;
        int   i;
    
      /* snew is the start of string after skipping leading delimiters */
        snew = s + strspn (s, delimiters);
    
        /* Create space for a copy of snew in t */
        if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL)
        {
            *argvp = NULL;
             numtokens = -1;
        }    
        else  /* count the number of tokens in snew */
        {
            strcpy (t, snew);
            if (strtok(t, delimiters) == NULL)
                numtokens = 0;
            else
                for (numtokens=1; strtok(NULL,delimiters) != NULL; numtokens++)
                ;
            /* create an argument array to contain pointers to tokens */
            if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL)
            {
                free (t);
                numtokens = -1;
            }
            else /* insert pointers to tokens into the array */
            {
                if (numtokens > 0)
                {
                    strcpy (t, snew);
                    **argvp = strtok (t, delimiters);
                    for (i = 1; i < numtokens + 1; i++)
                        *((*argvp) + i) = strtok (NULL, delimiters);
                }
                else
                {
                    **argvp = NULL;
                    free (t);
                }
            }
        }
        return numtokens;
    }
    
    
    --------------------------------------------------------------------------------
    
    
    /*
     * 	simplesh2.c
     *
     * 	A simple shell - second attempt
     */
    
    #include "simplesh.h"
    
    int
    main (void)
    {
        char inbuff[MAX_CANON+1];
        pid_t child_pid;
    
    
        while (1)
        {
            fputs (PROMPT_STRING, stdout);
            if (fgets(inbuff, MAX_CANON, stdin) == NULL)
                break;
    
            if (*(inbuff + strlen(inbuff) - 1) == NEWLINE_SYMBOL)
                *(inbuff + strlen(inbuff) - 1) = 0;
    
            if (strcmp(inbuff, QUIT_STRING) == 0)
                break;
            else
            {
                if ((child_pid = fork()) == 0)
                {
                    char **chargv;
    
                    if (makeargv(inbuff, BLANK_STRING, &chargv) > 0)
                    {
                        if (execvp(chargv[0], chargv) == -1)
                        {
                            perror ("Invalid command");
                            exit (1);
                        }
                    }
                    exit (1);    
                }
                else if (child_pid > 0)
                    wait (NULL);
            }
        }
        return 0;
    }
    -------------------------------------------------------------------




    how do i do this? where do i find info on this sort of stuff?

    thanx everyone

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    In the furture please use Code Tags. I added them for you this time.

    Information on code tags may be found at:
    http://www.cprogramming.com/cboard/s...threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I don't know if this will help you too much but as far as I know, the shell is the user interface to the operating system. I believe that a shell performs a great deal of system calls, so definately research system calls and use them extensively in your shell.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    k sorry bout that code thing - ill try to use the tags fro now on.
    i have more questions.
    i need to get the bit of code after the fork() call to go into a different file called executecmdline. this is sorf of aranged for in the .h file.
    so i've been trying to get this bit (more or less):
    Code:
    char **chargv;
    
                    if (makeargv(inbuff, BLANK_STRING, &chargv) > 0)
                    {
                        if (execvp(chargv[0], chargv) == -1)
                        {
                            perror ("Invalid command");
                            exit (1);
                        }
                    }
                    exit (1);
    into that void executecmdline (char *cmd) c file but it dont wanna compile. ive been trying to change the various variables and play with that pointer stuff but i dont know much about low level c prog. any help?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    anyone?

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    don't bump your own post like this please. People really fon't like it. People read very far down the list i assure you.

    Of course by posting this I will cause more people to read it but oh well...paradoxes oh paradoxes .

    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try restating your question, together with some error messages from your compiler (and the appropriate code, of course).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: A Simple Shell
    By roaming_builder in forum C Programming
    Replies: 6
    Last Post: 02-10-2008, 10:12 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Efficiency coding; a simple top-level window
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 07-09-2003, 09:15 AM
  4. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM