Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Inputting into another program (C/C++)

Name: Anonymous 2007-01-12 9:16

So, how do you do it? I'm trying to create a NetHack character generator, but for that I need my program to input text into it in a predetermined way. Everything I read about interprocess/interprogram communication is far too complex for what I need, I'd rather start slowly and then go building up the concepts. Finally, cheating is not my main concern here (changing the source would be much easier), learning is, so don't go crazy because I asked!

Name: Anonymous 2007-01-12 10:34

>>1
If I understood correctly, you want a program that will act as the client of another, as if you were typing stuff into this other program? This can be simple to complex, depending on how's that other program. Is it a text-based program (runs in a terminal, or command prompt window) and you don't mind exiting it at the end of your program, or you need to let the user take over the commanded program afterwards? Do you need to read what the other program says, or you can send everything blindly?

Save for a very simple case where you could use the standard output, IPC is out of the question as it has to be supported by both processes, i.e. the commanded process would have to support it. At worst you'll have to call some operating-system dependent functions to simulate user input right into the target process' input buffer.

But where does NetHack write your characters? It's 95% likely that the simplest solution to generate characters would be to generate or edit a savegame, not to control another process while it's running.

Name: Anonymous 2007-01-12 11:03

>>1
Please, you HAVE to tell us what platform you're running. You wouldn't use the same techniques on Linux as you would on Windows.

Also, if I understand things you want to do something like:

game: What kind of character would you like to create? a) Warrior b) Priest ...
program: a

game: Strength 21 Dexterity 4 Intelligence 5 ... a) OK r) Reroll
program: r

game: Strength 4 Dexterity 10 Intelligence 5 ... a) OK r) Reroll
program: r

...

Is that it?

Name: Anonymous 2007-01-12 11:20

>>2
First of all, thanks for the reply. The specifics:
*It's text-based (command prompt in my case)
*I don't mind exiting (it's necessary, actually)
*I want to send everything blindly

This should be easy, then? You don't have to do everything, give me some help and I'll try to walk with my own legs!

About the other cheating solutions, yes, I agree, they're easier. But communicating with another program seems to be an useful concept to grasp, so I'm trying to kill two birds with one stone. The problem is everything I could find about it was based on client/server programming. I'd like to start slow, with something like this. If you provide some info and I understand this (seemingly simple) case, I may tackle the rest later.

>>3
I'm on Windows (sorry, I thought this was simple and the platform wouldn't matter).
I was going for something different than that. Since nethack saves the game when you exit, I wanted to input the number of characters to be generated, let's say 100. Then, the program would run nethack, generate, exit, rename the savefile, run, generate, exit, rename... 100 times. Then I'd select the best savefile and use it. I was planning on writing the statistics of each character generated in a text file, but for that the program would need to read the prompt, so I don't know how much that would complicate things.

Either way is fine by me. Pick the one you consider the easiest and tell me how to do it!

Name: Anonymous 2007-01-12 15:31

>>4
Things like How Do I Quicksorted Array are the same between platforms. Interaplication communication isn't. Unfortunately I can't help with the Windows way, so I'll bow out here, but just creating a hundred files should be the easiest way.

Name: Anonymous 2007-01-12 16:01 (sage)

why not just write your input to a file and then just do 'nethack < file'?

Name: Anonymous 2007-01-12 16:43

>>5
Ok then, at least you clarified some things.

>>6
First thing I tried, but didn't work. When you do that, nethack stops receiving input (what you type doesn't appear on the screen), and the only thing you can do is quit with ^c.

Name: Anonymous 2007-01-13 13:55

>>7
When you do that, nethack stops receiving input

That's the idea, nethack should receive the input from the file instead of the keyboard.

Name: Anonymous 2007-01-13 15:17 (sage)

#!/usr/bin/expect -f

set timeout -1
while true {
    spawn /usr/bin/nethack -uAnonymous-Hum-Mal-Neu-Wiz
    send "  "
    send "i"

    expect \
        "spellbook of create monster" {
            expect \
                "ring of slow digestion" { break } \
                "end" {
                    send " "
                    send "#qyq"
                    close
                    wait
                }
        } \
        "end" {
            send " "
            send "#qyq"
            close
            wait
        }
}
interact

Name: Anonymous 2007-01-13 17:07

>>9
what

Name: Anonymous 2007-01-13 17:13 (sage)

>>10
Here's an improved version (who would have guessed that this would work):
#!/usr/bin/expect -f
while true {
    spawn /usr/bin/nethack -uAnonymous-Hum-Mal-Neu-Wiz
    send "\x1b\x1b\x1b"
    send "i"
    expect {
        -re "spellbook of create monster.*ring of slow digestion" break
        "(end)" { send " "; send "#qyq"; close }
    }
}
interact

Name: Anonymous 2007-01-13 20:06

>>9
>>11
I'm on Windows, and those seem to be for Linux. Also, that's not C, is it some kind of script?

Name: Anonymous 2007-01-13 20:15

Name: Anonymous 2007-01-13 23:47

>>13
Looks like that's what I needed! Thanks man!
Now, I better get started with the reading...

Name: Anonymous 2007-01-15 19:06

XMLZORZ

Name: Anonymous 2009-01-14 13:24

LISP

Name: Anonymous 2011-12-09 8:13

Rise from you're grave!

Name: Anonymous 2011-12-09 20:13

Fuck. I haven't written for expect in 10 years. I've been reminded of it no less than 3 times this week. Can we just forget this shit ever existed?

Name: Sgt.Kabu䌇拞kiman玍㛜 2012-05-28 22:14

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List