0% found this document useful (0 votes)
31 views

Week 1 Byteshell 1

The document provides an overview of the Byteshell project. It discusses setting up WSL and using VS Code as an editor. It then outlines the timeline for implementing the Byteshell shell over three weeks, including basic commands like ls, help and exit in the second week. The document concludes by describing the basic lifetime and loop of a shell, including reading input, parsing commands, and executing them.

Uploaded by

Harshil Sajan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Week 1 Byteshell 1

The document provides an overview of the Byteshell project. It discusses setting up WSL and using VS Code as an editor. It then outlines the timeline for implementing the Byteshell shell over three weeks, including basic commands like ls, help and exit in the second week. The document concludes by describing the basic lifetime and loop of a shell, including reading input, parsing commands, and executing them.

Uploaded by

Harshil Sajan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ACM IIT ROORKEE

ByteShell: Make your own shell

Session 1 : Setup and Basics


WSL

● WSL is a Windows
feature that allows
Linux programs to
run on Windows
computer
CODE EDITOR </>

● VS Code is a lightweight yet powerful


source code editor offering a wide range of
features and extensions that enhance
productivity and simplify the development
process
Shell

● A shell is an operating system's command-line interface


that allows users to interact with the computer and
execute commands. It provides a way to control and
manage the operating system by accepting and executing
user inputs in the form of commands. The shell acts as an
intermediary between the user and the operating system,
interpreting commands and facilitating their execution. It
enables users to navigate the file system, run programs,
manage processes, and perform various tasks on the
operating system.
Time Line : Byteshell

1st week 2nd week 3rd week

● WSL Setup ● Understanding child parent ● History command


● Understanding Basic C process implementation
● Making basic shell loop ● Wait, fork and execute calls ● Some more commands
● Read a line ● Implement some command implementation
● Parsing a line like ls , help and exit. ● New feature addition
Basic Lifetime of Shell

• Initialize: In this step, a typical shell would read and


int main(int argc, char **argv)
execute its configuration files. These change
{
aspects of the shell’s behavior.
• Interpret: Next, the shell reads commands from
  ACMShell_loop();
stdin (which could be interactive, or a file) and
executes them.
• Terminate: After its commands are executed, the   return EXIT_SUCCESS;
shell executes any shutdown commands, frees up }
any memory, and terminates

Note: We have not implemented initialize and terminate . You


can do so if you wish on your own.
Basic loop of a shell

● Read: Read a line from command input.


● Parse: Converting the command string into list of
arguments.
● Execute: Run the parsed command.
  char *line;
  char **args;
  int status;

  do
  { printf("> ");
    line = ACMShell_read_line();
BASIC ACM-Shell     args = ACMShell_split_line(line);
Loop  
 
        add_to_hist(args);
  status = ACMShell_execute(args);

    free(line);
    free(args);
  } while (status);
Read a line

int bufsize = 1024;


int position = 0;
char *buffer = malloc(sizeof(char) * bufsize); // contains input
int c;

if (!buffer)
{
fprintf(stderr, "ACMShell: allocation error\n");
exit(EXIT_FAILURE);
}
while (1)

c = getchar();
if (c == '\n' )
buffer[position] = '\0';
return buffer;
else
buffer[position] = c;}
Position++;
if (position >= bufsize)
bufsize += ACMShell_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer)
fprintf(stderr, "ACMShell: allocation error\n");
exit(EXIT_FAILURE);
Parsing a line:

int bufsize = 64, position = 0;


char **tokens = malloc(bufsize * sizeof(char *));
char *token;
if (!tokens)
{
fprintf(stderr, "ACMShell: allocation error\n");
exit(EXIT_FAILURE);
}

token = strtok(line, ACMShell_TOK_DELIM);


Use of strtok():
#include <string.h>
#include <stdio.h>

int main () {
   char str[80] = "This is - openproject - Byteshell";
   const char s[2] = "-";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
   
      token = strtok(NULL, s);
   }
   
   return(0);
}
while (token != NULL)
tokens[position] = token;
position++;
if (position >= bufsize)
bufsize += ACMShell_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
fprintf(stderr, "ACMShell: allocation error\n");
exit(EXIT_FAILURE);
token = strtok(NULL, ACMShell_TOK_DELIM);
tokens[position] = NULL;
return tokens;
THANKS

You might also like