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);
}