![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|