Crossfire Client, Trunk
script.c File Reference
#include "client.h"
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include "external.h"
#include "mapdata.h"
#include "p_cmd.h"
#include "script.h"
+ Include dependency graph for script.c:

Go to the source code of this file.

Data Structures

struct  script
 

Functions

static int script_by_name (const char *name)
 
static void script_dead (int i)
 
void script_fdset (int *maxfd, fd_set *set)
 
void script_init (const char *cparams)
 
void script_kill (const char *params)
 
void script_killall (void)
 
void script_killall_wrapper (const char *params)
 
void script_list (void)
 
void script_monitor (const char *command, int repeat, int must_send)
 
void script_monitor_str (const char *command)
 
void script_process (fd_set *set)
 
static void script_process_cmd (int i)
 
static void script_send_item (int i, const char *head, const item *it)
 
void script_sync (int commdiff)
 
void script_tell (const char *params)
 
void script_watch (const char *cmd, const guint8 *data_initial, const int data_len, const enum CmdFormat format)
 
static void send_map (int i, int x, int y)
 

Variables

static int num_scripts = 0
 
static struct scriptscripts = NULL
 

Detailed Description

Handles the client-side scripting interface.

Each script is an external process that keeps two pipes open between the client and the script (one in each direction). When the script starts, it defaults to receiving no data from the client. Normally, the first command it sends to the client will be a request to have certain types of data sent to the script as the client receives them from the server (such as drawinfo commands). The script can also request current information from the client, such as the contents of the inventory or the map data (either live or last viewed "fog-of-war" data). The script can also send commands for the client to pass to the server.

Script Commands:

watch {command type} whenever the server sends the given command type to the client, also send a copy to the script. Note that this checked before the client processes the command, so it will automatically handle new options that may be added in the future. If the command type is NULL, all commands are watched.

unwatch {command type} turn off a previous watch command. There may be a slight delay in response before the command is processed, so some unwanted data may still be sent to the script.

request {data type} have the client send the given data to the script.

issue [{repeat} {must_send}] {command} issue the specified command to the server. if {repeat} isn't numeric then the command is sent directly For "lock" and "mark" only, the parameters are converted to binary.

draw {color} {text} display the text in the specified color as if the server had sent a drawinfo command.

monitor send the script a copy of every command that is sent to the server.

unmonitor turn off monitoring.

sync {#} wait until the server has acknowledged all but {#} commands have been received

To implement this:

Processing script commands: gtk/gx11.c:do_network() and x11/x11.c:event_loop() are modified to also watch for input from scripts in the select() call, in which case script_process(fd) in this file is called.

Handling watches: common/client.c:DoClient() is modified to pass a copy of each command to script_watch() before checking for it in the table.

Handling of monitor: common/player.c:send_command() is modified to pass a copy of each command to script_monitor() before sending to the server.

Handling of requests: global variables are directly accessed from within this file.

Handling of issues: send_command() is called directly. Note that this command will be sent to any scripts that are monitoring output.

Launching new scripts: common/player.c:extended_command() is extended to add a check for "script <scriptname>" as an additional command, calling script_init(). Also added is the "scripts" command to list all running scripts, the "scriptkill" command to terminate a script (close the pipes and assume it takes the hint), and the "scripttell" command to send a message to a running script.

Definition in file script.c.

Function Documentation

◆ script_by_name()

static int script_by_name ( const char *  name)
static

Definition at line 997 of file script.c.

References script::name, num_scripts, and scripts.

Referenced by script_kill(), and script_tell().

+ Here is the caller graph for this function:

◆ script_dead()

static void script_dead ( int  i)
static

Definition at line 1028 of file script.c.

References script::in_fd, script::name, num_scripts, script::num_watch, script::out_fd, script::params, scripts, and script::watch.

Referenced by script_kill(), script_killall(), and script_process().

+ Here is the caller graph for this function:

◆ script_fdset()

void script_fdset ( int *  maxfd,
fd_set *  set 
)

Definition at line 558 of file script.c.

References script::in_fd, maxfd, num_scripts, and scripts.

Referenced by do_network().

+ Here is the caller graph for this function:

◆ script_init()

void script_init ( const char *  cparams)

◆ script_kill()

void script_kill ( const char *  params)

Definition at line 515 of file script.c.

References draw_ext_info(), MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_SCRIPT, NDI_BLACK, NDI_RED, num_scripts, script::params, script::pid, script_by_name(), script_dead(), and scripts.

+ Here is the call graph for this function:

◆ script_killall()

void script_killall ( void  )

Definition at line 541 of file script.c.

References draw_ext_info(), MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_SCRIPT, NDI_RED, num_scripts, script::pid, script_dead(), and scripts.

Referenced by menu_quit_character(), menu_quit_program(), on_button_exit_client_clicked(), on_window_destroy_event(), and script_killall_wrapper().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_killall_wrapper()

void script_killall_wrapper ( const char *  params)

Definition at line 536 of file script.c.

References script_killall().

+ Here is the call graph for this function:

◆ script_list()

void script_list ( void  )

Definition at line 493 of file script.c.

References draw_ext_info(), MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_SCRIPT, script::name, NDI_BLACK, num_scripts, script::params, and scripts.

Referenced by do_script_list().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_monitor()

void script_monitor ( const char *  command,
int  repeat,
int  must_send 
)

Definition at line 927 of file script.c.

References script::monitor, num_scripts, script::out_fd, and scripts.

Referenced by send_command().

+ Here is the caller graph for this function:

◆ script_monitor_str()

void script_monitor_str ( const char *  command)

Definition at line 943 of file script.c.

References script::monitor, num_scripts, script::out_fd, and scripts.

Referenced by cs_print_string(), inscribe_magical_scroll(), send_mark_obj(), and toggle_locked().

+ Here is the caller graph for this function:

◆ script_process()

void script_process ( fd_set *  set)

Definition at line 573 of file script.c.

References script::cmd, script::cmd_count, script::in_fd, num_scripts, script_dead(), script_process_cmd(), and scripts.

Referenced by do_network().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_process_cmd()

static void script_process_cmd ( int  i)
static

Process a single script command from the given script. This function removes the processed command from the buffer when finished.

Parameters
iIndex of the script to process a command from

Definition at line 1090 of file script.c.

References Stat_struct::ac, item_struct::applied, Stat_struct::attuned, Player_Struct::below, Stat_struct::Cha, client_send_apply(), client_send_move(), script::cmd, script::cmd_count, ClientSocket::command_received, ClientSocket::command_sent, Stat_struct::Con, CONFIG_MAPHEIGHT, CONFIG_MAPWIDTH, Player_Struct::container, cpl, CS_NUM_SKILLS, cs_print_string(), CS_STAT_SKILLINFO, csocket, Stat_struct::dam, Spell_struct::dam, Stat_struct::denied, Stat_struct::Dex, draw_ext_info(), Stat_struct::exp, ClientSocket::fd, Player_Struct::fire_on, Stat_struct::flags, Stat_struct::food, Stat_struct::grace, Spell_struct::grace, handle_local_command(), Stat_struct::hp, Stat_struct::Int, item_struct::inv, Stat_struct::level, Spell_struct::level, mapdata_size(), MAX_BUF, MAX_SKILL, Stat_struct::maxgrace, Stat_struct::maxhp, Stat_struct::maxsp, script::monitor, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_SCRIPT, script::name, Spell_struct::name, NDI_RED, item_struct::next, Spell_struct::next, Player_Struct::no_echo, script::num_watch, Player_Struct::ob, script::out_fd, Spell_struct::path, pl_pos, Stat_struct::Pow, Player_Struct::range, Stat_struct::repelled, Stat_struct::resists, Player_Struct::run_on, script_pos, script_send_item(), script_sync(), scripts, send_command(), send_map(), Stat_struct::skill_exp, Stat_struct::skill_level, skill_names, Spell_struct::skill_number, SockList_AddChar(), SockList_AddInt(), SockList_AddString(), SockList_Init(), SockList_Send(), Stat_struct::sp, Spell_struct::sp, Stat_struct::speed, Player_Struct::spelldata, Player_Struct::stats, Stat_struct::Str, script::sync_watch, item_struct::tag, Spell_struct::tag, Spell_struct::time, Player_Struct::title, use_config, script::watch, Stat_struct::wc, Stat_struct::weapon_sp, item_struct::weight, Stat_struct::weight_limit, Stat_struct::Wis, PlayerPosition::x, and PlayerPosition::y.

Referenced by script_process().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ script_send_item()

◆ script_sync()

void script_sync ( int  commdiff)

Definition at line 475 of file script.c.

References num_scripts, script::out_fd, scripts, and script::sync_watch.

Referenced by CompleteCmd(), and script_process_cmd().

+ Here is the caller graph for this function:

◆ script_tell()

void script_tell ( const char *  params)

Definition at line 959 of file script.c.

References draw_ext_info(), MAX_BUF, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_SCRIPT, NDI_BLACK, NDI_RED, script::out_fd, script::params, script_by_name(), and scripts.

+ Here is the call graph for this function:

◆ script_watch()

◆ send_map()

static void send_map ( int  i,
int  x,
int  y 
)
static

Definition at line 1061 of file script.c.

References mapdata_cell(), mapdata_contains(), script::out_fd, and scripts.

Referenced by script_process_cmd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ num_scripts

◆ scripts