The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
simple shell - how to get a parameter typed in a shell script cmitulescu Shell Programming and Scripting 4 01-09-2009 08:45 PM
Linux Shell Question: how to print the shell script name ? meili100 UNIX for Dummies Questions & Answers 3 07-01-2008 01:55 PM
Designing Interface to handle click event chetan2309 High Level Programming 2 02-10-2007 05:17 PM
Difference between writing Unix Shell script and AIX Shell Scripts haroonec AIX 0 04-12-2006 02:27 AM
Unix Kernel Designing vibhory2j UNIX for Advanced & Expert Users 2 08-20-2004 05:33 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-26-2002
newtoallthis newtoallthis is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 1
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0