C++ Help with creating a shell.
Results 1 to 6 of 6

Thread: C++ Help with creating a shell.

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351

    C++ Help with creating a shell.

    I am still learning C++ as I go, but I am working on creating a shell like program. It's going to be a simple DOS clone...but interestingly enough, will probably require Windows.
    Anyways, I'm going to learn at some point how to retrieve the passwords from a file, but for now, and for my practice purposes, you are asked for a new password everytime you run the EXE. And then the new password is checked against what you used to login with a strcmp() statement.
    Now, I'd like to know how I can go about adding commands to this that the user can type in. And then eventually add parameters to these commands.
    I know of a couple ways in my head, but they are sluggish. I know that while it sits at the prompt, the user can type in a string, and using gets(), I can check the string using a buttload of if-then statements, or I could use a switch statement. But this will cause performance to be disgusting, as the CPU must check and invalidate every possible command, before it finds the one that works.

    So my main idea right now is something like:
    Code:
    ...
    ..
    .
    prompt()
    {
       char cmd[41];
       char ch[4];
       cout << "C:\\>";
       gets(cmd);
    
       if(!strcmp(cmd, "exit"))
       {
             cout << "Are you sure? (y or n)\n";
             gets(ch);
             if(!strcmp(ch, "y"))
                  {
                        return 0;
                  }
            else 
                  {
                       prompt();
                  }
       }
       if(cmd = "dir")
       {
    .
    ..
    ...
    }
    so as you can see, the coding would be incredibly foul and sluggish. is there any way to improve upon this? The only improvements I have in mind are
    1) use switch instead of if
    2) use a 2D array that contains the number of a command, and the command, so when the user enters a command, it checks it against the array to see if it exists, if not then an error message is displayed. if it is, then the command is carried out.

    AxessTerminated
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    switch statements are definatly the way to go, they tidy up code nicely and allow for a default state, so if the user enters an inncorrect command the program produces a nice little error message,

    .........is not recognized as an internal or external command, operable program or batch file.
    you might like to look at os development as there are quite a few tutorials regarding the creation of shells there.

    i2c

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Even if you use the array that contains command numbers, you're still going to have to use some if statements before you can use your switch command because of the fact that you can only switch integers. This sounds like an interesting problem, though, and I'll have a think about it and post a reply as soon as I have any sort of idea.

    ac

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Well depending on your list of commands, you might be interested in a binary search with an array, sorted by first letter. In brief, what you do is compare the first character of your variable with the first character of the middle of the array. It can be higher or lower, so you move in whichever side of the array you want [left or right]. So on, so forth, you're bound to have exponentially less operations to search for a command. The only thing you need to do is build the command array on your own and make sure it's sorted, otherwise the algorithm can execute and fail. Secondary, you might not be able to use the C++ function for binary search [I don't remember if that one is for ints only], but constructing your own function should be piece of cake. Depending on your intended commands, you might want to compare not only the first characters of two words... But for now I'll leave that to you, you understand where I'm getting at.

    Hope this helps at all.
    /\\

  5. #5
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    damn you hypronix...beat me to it. I thought about that after posting last night but couldn't be bothered turning my comp back on :P

    Yeah, binary search trees are the way to go. I believe there's a tutorial on this technique at www.cprogramming.com if you want more information.

    ac

  6. #6
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    thanks alot for your help. looks like ive got alot of of C++ to learn before i can go much further.
    Geek isn't just a four-letter word; it's a six-figure income.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides