Re: [CODE] Line input function

From: Mike Breuer (
Date: 06/07/01

From: "Jim Persson" <jim@LINUX.NU>
> char line_input(char *prompt) {
>  // code here
> }

This won't really work exactly the way you are specifying because of the
serialized IO in Circle.  If the function were to actually wait for user
input, it would block all other activity on the MUD.

Assuming you are looking for something that you can use in various places
throughout your code, I would handle this by adding a callback function to
struct descriptor_data.


struct descriptor_data {
   socket_t descriptor; /* file descriptor for socket  */
+  void (*callback)(struct descriptor_data *, char *);

Then add a unique state called CON_LINE_INPUT

#define CON_DELCNF2  16 /* Delete confirmation 2  */
#define CON_DISCONNECT  17 /* In-game link loss (leave character) */
+#define CON_LINE_INPUT 18 /* Used for parsing a single line of user input */

Next, you will need to add code to the nanny function in interpreter.c to
handle your input.  Add the following case to the big switch statement:

  case CON_CLOSE:

+   if (d->callback)
+     d->callback(d, arg);
+   else
+     log("SYSERR: No callback function specified for state CON_LINE_INPUT");
+   d->callback = NULL;
+ break;

Then your line_input function will look like this:

+void line_input( struct descriptor_data *d, const char *prompt,
+                 void (*callback)(struct descriptor_data *, char *) ) {
+  d->callback = callback;
+  SEND_TO_Q(prompt, d);

To use all this, you will need to create a callback function each time you
want to parse the result of the input.  As a simple example:

+void greet_player( struct descriptor_data *d, char *name) {
+  sprintf(buf, "Hi %s!\r\n", name);
+  SEND_TO_Q(buf, d);

And then somewhere in your code:

  line_input(ch->desc, "Enter your name: ", greet_player);

Good luck with it, and feel free to ask any questions.


   | FAQ: |
   | Archives: |

This archive was generated by hypermail 2b30 : 12/05/01 PST