Thread: Smallsh program to change shell. need help understanding the code and tracing it

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    Smallsh program to change shell. need help understanding the code and tracing it

    i have attached all files, if someone can help me understand the code
    Code:
    #include "smallsh.h"
    
    char *prompt = "Command> ";
    
    int userin(char *p);
    void procline(void);
    
    int main()
    {
    	while (userin(prompt) != EOF)
    		procline();
    }
    Code:
    #include "smallsh.h"
    
    int gettok(char **outptr);
    int runcommand(char **cline, int where);
    
    
    void procline(void) 
    {
    	char *arg[MAXARG + 1]; 
    	int toktype; 
    	int narg; 
    	int type; 
    
    	narg = 0;
    
    	for (;;) 
    	{
    	   switch (toktype = gettok(&arg[narg])) {
    		case ARG:
    			if (narg < MAXARG)
    				narg++;
    			break;
    		case EOL:
    		case SEMICOLON:
    		case AMPERSAND:
    			if (toktype == AMPERSAND)
    				type = BACKGROUND;
    			else
    				type = FOREGROUND;
    			if (narg != 0)
    			{
    				arg[narg] = NULL;
    				runcommand(arg, type);
    			}
    			if (toktype == EOL)
    				return;
    			narg = 0;
    			break;
    		}
    	}
    }
    Code:
    //*****************************************************************
    #include <sys/wait.h>
    #include <stdio.h>
    //#include <sys/wait.h>
    
    #define EOL 1 
    #define ARG 2 
    
    #define AMPERSAND 3 
    #define SEMICOLON 4 
    #define MAXARG 512 
    #define MAXBUF 512 
    
    #define FOREGROUND 0 
    #define BACKGROUND 1
    Code:
    #include "smallsh.h"
    
    static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF],
    *ptr = inpbuf, *tok = tokbuf;
    
    int inarg(char c);
    
    int userin(char *p)
    {
    	int c, count;
    	ptr = inpbuf;
    	tok = tokbuf;
    	
    	printf("%s", p);
    	count = 0;
    	while (1)
    	{
    		if ((c = getchar()) == EOF)
    			return(EOF);
    		if (count < MAXBUF)
    			inpbuf[count++] = c;
    		if (c == '\n' && count < MAXBUF)
    		{
    			inpbuf[count] = '\0';
    			return count;
    		}
    		
    		if (c == '\n')
    		{
    			printf("smallsh: input line too long\n");
    			count = 0;
    			printf("%s ", p);
    		}
    	}
    }
    
    int gettok(char **outptr)
    {
    	int type;
    	
    	*outptr = tok;
    	
    	while (*ptr == ' ' || *ptr == '\t')
    		ptr++;
    	
    	*tok++ = *ptr;
    	
    	switch (*ptr++) {
    	case '\n':
    		type = EOL;
    		break;
    	case '&':
    		type = AMPERSAND;
    		break;
    	case ';':
    		type = SEMICOLON;
    		break;
    	default:
    		type = ARG;
    		
    		while (inarg(*ptr))
    			*tok++ = *ptr++;
    	}
    	*tok++ = '\0';
    	return type;
    }
    
    //****************************************************************************************
    static char special[] = { ' ', '\t', '&', ';', '\n', '\0' };
    int inarg(char c)
    {
    	char *wrk;
    	for (wrk = special; *wrk; wrk++)
    	{
    		if (c == *wrk)
    			return (0);
    	}
    	return (1);
    }
    Attached Files Attached Files
    Last edited by Salem; 10-23-2019 at 01:58 PM. Reason: Because....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about stating a specific question about what you don't understand.

    Rather than us assuming you know nothing, and expecting a large essay of a response explaining everything in nauseating detail (which isn't going to happen).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2015, 11:29 AM
  2. need little help in understanding a program code.
    By jewelson in forum C Programming
    Replies: 5
    Last Post: 04-20-2015, 01:24 AM
  3. Help Tracing through code
    By NICKD54 in forum C Programming
    Replies: 15
    Last Post: 06-19-2011, 09:03 AM
  4. what is the best method of tracing this code on paper..
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 04-10-2009, 12:46 PM
  5. what is the best way of tracing code on paper
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 03-29-2009, 05:06 PM

Tags for this Thread