Visit Our UNIX and Linux User Community


designing a shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting designing a shell
# 1  
Old 09-26-2002
designing a shell

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:--------------------------------------------------------------------------------
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  
Old 09-26-2002
Please read our rules, and notice the rule against posting homework assignments.
# 3  
Old 09-26-2002
Search the site for keywords and start reading about various commands.

I assume you have a book / reading assignments for that book / notes / everything else you get when you're taking a class. The best thing you could do is reference all that stuff and try to learn...

Previous Thread | Next Thread
Test Your Knowledge in Computers #1000
Difficulty: Medium
Trusted Computer System Evaluation Criteria (TCSEC) is frequently referred to as the Red Book.
True or False?

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Designing

Dear all, Do you guys use any tool to design shell scripts?. My requirement is to simplify the logic/design of shell scripts before you actually write it. In other words i need to put the design i have in mind to words before i start writing it. Thanks in advance. (3 Replies)
Discussion started by: earlysame55
3 Replies

2. Programming

Designing Interface to handle click event

Hi All, Problem statement:- I want to incorporate for my project few click events which will be able to play files in M-player through C. What I want if a user initiate a "PLAY" button from html code , this should trigger my C code... which will help me playing files on M-player I am not... (2 Replies)
Discussion started by: chetan2309
2 Replies

3. UNIX for Advanced & Expert Users

Unix Kernel Designing

Hello, I want to know what a kernel is? what are its functions and uses. what is a unix kernel? can i make a new kernel like unix or any other OS. if yes what type of programming is needed or can i use C programming language.if there is C, then what kind of C is required (hardware like... (2 Replies)
Discussion started by: vibhory2j
2 Replies

Featured Tech Videos