Thread: CLI (Command Line Interperator)

  1. #1
    Registered User
    Join Date
    Aug 2001

    Question CLI (Command Line Interperator)

    Can any1 please help me in CLI

    I am having trouble in writing this program.

    The program repeatedly accepts a "command line" from its standard input and executes it.

    More than one command line can be executed per invocation of the program.

    I am trying to "exec" the command line from
    within a child process of my program.

    P.S. I am running it on unix

    As, I am new to programming. any help would be much appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2001


    First you tell one thing,

    You want to execute only commands of the shell? or You want to create your own commands ?

  3. #3
    you want to start writing the structure of the CLI on a pattern like this one. Then it's all easy.

    #include <stdio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <unistd.h>
    #define SIZE 255
    /*#define DEBUG*/
    int main(int argc, char *argv[])
       char inputLine[ SIZE ];
       int  pid;
       while( 1 ) {
          if ( fgets( inputLine, SIZE-1, stdin ) == NULL ) {
             fprintf( stderr, "fgets error.\n");
          inputLine[ strlen(inputLine)-1 ] = 0; /* remove \n */
    #ifdef DEBUG
          /* some debug informations */
          fprintf( stdout, "read cmd line: %s\n", inputLine );
          if ( strcasecmp( inputLine, "exit" ) == 0 ) {
             /* by typing "exit", the users exits */
             fprintf( stdout, "exiting\n");
          /* fork to exec the command line */
          if ( (pid =  fork()) < 0 ) {
             fprintf( stderr, "fork error, errno = %d\n", errno );
          } else if ( pid == 0 ) {
    #ifdef DEBUG
             fprintf( stdout, "child %d will execute %s\n", getpid(), inputLine );
             system( inputLine );
          } else {
    #ifdef DEBUG
             fprintf( stdout, "parent started child %d\n", pid);
       fprintf( stdout, "bye bye..\n");
       return 0;
    that's just one simple example. All the command are executed in the background, due to the fork.

  4. #4
    Registered User
    Join Date
    Aug 2001

    CLI (Command Line Interperator)

    Yes quark,

    I need to implement that and execute commands of the shell.

    and the second part is to create your own commands

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gnome GTK / Spawn CLI app - read output
    By MtOzOr in forum Linux Programming
    Replies: 7
    Last Post: 09-08-2008, 06:41 AM
  2. Cli / Clr / Mfc / Atl / Gdi / Wtf?
    By Dino in forum Tech Board
    Replies: 14
    Last Post: 02-15-2008, 01:38 PM
  3. Replies: 12
    Last Post: 09-04-2007, 11:44 AM
  4. CLI threads allowed or not?
    By VirtualAce in forum A Brief History of
    Replies: 45
    Last Post: 07-14-2006, 10:55 PM
  5. incomming call (CLI)
    By Laeeqhamid in forum Windows Programming
    Replies: 0
    Last Post: 01-30-2003, 08:57 PM