Re: Command Line Interpreter

jimbo@radiks.net
27 Jul 2000 21:30:27 -0400

          From comp.compilers

Related articles
Command Line Interpreter bernfarr@my-deja.com (2000-07-23)
Re: Command Line Interpreter jimbo@radiks.net (2000-07-27)
Re: Command Line Interpreter jkahrs@castor.atlas.de (Juergen Kahrs) (2000-07-27)
Re: Command Line Interpreter bernfarr@my-deja.com (2000-07-27)
Re: Command Line Interpreter adamo@dblab.ece.ntua.gr (2000-07-29)
Re: Command Line Interpreter sdm7g@elvis.med.virginia.edu (Steven D. Majewski) (2000-07-29)
Re: Command Line Interpreter bernfarr@my-deja.com (2000-07-31)
Re: Command Line Interpreter gnb@hellcat.itga.com.au (Gregory Bond) (2000-08-04)
[4 later articles]
| List of all articles for this month |
From: jimbo@radiks.net
Newsgroups: comp.compilers
Date: 27 Jul 2000 21:30:27 -0400
Organization: Deja.com - Before you buy.
References: 00-07-055
Keywords: parse

    bernfarr@my-deja.com wrote:
> I'm looking for an example of a compiler/interpreter that
> will handle a command line language.


Not trying to litter this NG with source code, but...


// Tiny command-line Interpreter
// jimbo@radiks.net
#include <stdio.h>
#include <string.h>
#include <conio.h>


int main(int argc,char **argv) {
      char buff[1024];
      FILE *fp;
      char *cmd;
      fp=fopen(argv[1],"r");
      while(fgets(buff,1023,fp)!=NULL) {
            cmd=strtok(buff," \t\r\n");
            if(!stricmp(cmd,"echo"))
                  printf("%s\n",strtok(NULL,"\r\n"));
            else
            if(!stricmp(cmd,"pause")) {
                  printf("Press ENTER to continue.\n");
                  getch();
            }
            else
            if(!stricmp(cmd,"exit")) break;
            else
            if(*cmd == '#') ;
      }
      fclose(fp);
}




The above is a very basic command-line interpreter that
understands the commands echo, pause, exit, and # ( #
is a comment character ). The code could be more clean
and certainly needs some error-checking.


Try compiling the above and then enter the following on
a command-line:


      tinycli test.txt


Where test.txt contains the following:


      # This is a tiny interpreter
      echo Hello there!
      pause
      echo This is a very simple interpreter
      pause
      echo Buh-bye!
      exit


Jim Lawless
jimbo@radiks.net
http://www.radiks.net/jimbo


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.