I want to build an own interpreter using c programming and I want to call it in command prompt. How would i do this? I just want to start a command "display" which can display the contents of a file like type in cmd.

Assuming that the intended syntax is "'display', followed by one or more spaces, followed by the name of the file", you read a line of input, check whether the line starts with "display" followed by one or more spaces. If it doesn't, you display a "no such command" error. If it does, you iterate up to the first non-space character after "display", which is where the file name starts. Then you open the file with that name, read each line of the file until you hit the end of the file and print each line as you read it.

Then you put the whole thing into a loop, so it asks for commands until the window is closed or the user hits Ctrl-z or Ctrl-c.

How do I read the string that was typed in the cmd in my c program? For example I typed "display file.txt". How to detect that text that was typed in cmd?

Wait, do you want to create your own command line program that you start from the command line and into which you can then enter the command "display bla.txt" (which is what I though you meant) or do you want to create a display command so that you can enter "display bla.txt" into cmd?

If you want to build an interpreter, make a repl.

Repl stands for: Read, Evaluate, print and loop.

Generally thats how pipeline programs work, and it's not unusual fotprograms that deal with real time data, as well as (of course) programming language interpreters.

For simple programs it might look something like:

loop
    cmd = getline
    tokenize(cmd)

    switch cmd[0]
        case "display"
            print contents of file cmd[1]
        case "write"
            write cmd[1] to file cmd[2]
        ...

Of course you'll also want to check to make sure that it uses the proper number of arguments, and print productive errors. Also, if you want it faster, you'll probably want to use a hash instead of a switch case (which can also be used for user defines values/functions). You might want to make a list of hashes if there will be some scoping as well.

At a minimum, a command interpreter does the following three things:

  1. Display a prompt
  2. Read a line of input
  3. Loop to #1

A functional command interpreter adds a new step that executes the command read during step #2 and before looping back to step #1.

I C, you have a few options to read a line of input from the user. I would suggest getline or fgets.

After the line is read you have to parse the line (usually by whitespace). You can do this with strtok, sscanf or some other mechanism. It really depends on how difficult this will get for you. If all you ever want to parse is a command and an argument then something like

sscanf (input, "%s %s", &command, &argument);

would be sufficient. If you want to get more fancy you need to start thinking about how to tokenize a string.

How familiar are you with handling input from a user in C?

sepp2k:
I want to create my own command "display". I want that it can be called in the windows cmd. Like if I type in the windows cmd "display file.txt", it will display the contents of file.txt just like the "type" command in cmd.

I can do this function using c programming but what I want is that I want to call it in the cmd. Where I will place my compiled program (the .c file and .exe file)? And how do I read the string that was typed in the cmd?

Hiroshe and L7Sqr:
I just want to have an idea on how can I call the function "display" in the windows cmd .And how will I get the string that was typed in the cmd ?

basically i want to know how to get the string typed in the cmd using c .how will i do this?

Oh, you don't want to create your own interpreter. You want to let your program to be useable from the windows command line.

This doesn't really have anything to do with C, and more to do with the Windows Operating system.

All you need to do is put your .exe somewhere in your path. Then you can use it from cmd. Somewhere in your path might be C:\Windows. It's probably better to make a new folder for your program, and add that folder to your path though.

To add something to your path: Right click on "My Computer", click properties -> Advanced system settings -> environment variables, then edit the system path or your own path to include the executable.

If you planning on distributing your program, you should use something like innosetup or WiX or NSIS to install the program and set up the path for your user.

thank you for all your replies .i just realized that i just want to create a simple command to be used in cmd .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.