2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_ENV_UNIVERSAL_COMMON_H
|
|
|
|
#define FISH_ENV_UNIVERSAL_COMMON_H
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#include <wchar.h>
|
2011-12-27 13:39:08 +08:00
|
|
|
#include <queue>
|
2012-08-06 02:58:17 +08:00
|
|
|
#include <string>
|
2005-10-04 23:11:39 +08:00
|
|
|
#include "util.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The set command
|
|
|
|
*/
|
|
|
|
#define SET_STR L"SET"
|
|
|
|
|
2005-09-23 04:16:52 +08:00
|
|
|
/**
|
|
|
|
The set_export command
|
|
|
|
*/
|
|
|
|
#define SET_EXPORT_STR L"SET_EXPORT"
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
The erase command
|
|
|
|
*/
|
|
|
|
#define ERASE_STR L"ERASE"
|
|
|
|
|
2005-09-23 04:16:52 +08:00
|
|
|
/**
|
|
|
|
The barrier command
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
#define BARRIER_STR L"BARRIER"
|
2005-09-23 04:16:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The barrier_reply command
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
#define BARRIER_REPLY_STR L"BARRIER_REPLY"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
The filename to use for univeral variables. The username is appended
|
|
|
|
*/
|
|
|
|
#define SOCK_FILENAME "fishd.socket."
|
|
|
|
|
|
|
|
/**
|
|
|
|
The different types of commands that can be sent between client/server
|
|
|
|
*/
|
2012-10-09 05:47:25 +08:00
|
|
|
typedef enum
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
SET,
|
|
|
|
SET_EXPORT,
|
|
|
|
ERASE,
|
|
|
|
BARRIER,
|
|
|
|
BARRIER_REPLY,
|
2012-10-09 05:47:25 +08:00
|
|
|
} fish_message_type_t;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-10-07 02:45:39 +08:00
|
|
|
/**
|
|
|
|
The size of the buffer used for reading from the socket
|
|
|
|
*/
|
|
|
|
#define ENV_UNIVERSAL_BUFFER_SIZE 1024
|
|
|
|
|
2011-12-27 13:39:08 +08:00
|
|
|
/**
|
|
|
|
A struct representing a message to be sent between client and server
|
|
|
|
*/
|
2012-11-18 18:23:22 +08:00
|
|
|
typedef struct
|
2011-12-27 13:39:08 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
/**
|
|
|
|
Number of queues that contain this message. Once this reaches zero, the message should be deleted
|
|
|
|
*/
|
|
|
|
int count;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/**
|
|
|
|
Message body. The message must be allocated using enough memory to actually contain the message.
|
|
|
|
*/
|
|
|
|
std::string body;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-08-06 02:58:17 +08:00
|
|
|
} message_t;
|
2011-12-27 13:39:08 +08:00
|
|
|
|
|
|
|
typedef std::queue<message_t *> message_queue_t;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
This struct represents a connection between a universal variable server/client
|
|
|
|
*/
|
|
|
|
typedef struct connection
|
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
/**
|
|
|
|
The file descriptor this socket lives on
|
|
|
|
*/
|
|
|
|
int fd;
|
|
|
|
/**
|
|
|
|
Queue of onsent messages
|
|
|
|
*/
|
2011-12-27 13:39:08 +08:00
|
|
|
message_queue_t *unsent;
|
2012-11-19 08:30:30 +08:00
|
|
|
/**
|
|
|
|
Set to one when this connection should be killed
|
|
|
|
*/
|
|
|
|
int killme;
|
|
|
|
/**
|
|
|
|
The input string. Input from the socket goes here. When a
|
|
|
|
newline is encountered, the buffer is parsed and cleared.
|
|
|
|
*/
|
|
|
|
std::vector<char> input;
|
|
|
|
|
|
|
|
/**
|
|
|
|
The read buffer.
|
|
|
|
*/
|
|
|
|
char buffer[ENV_UNIVERSAL_BUFFER_SIZE];
|
|
|
|
|
|
|
|
/**
|
|
|
|
Number of bytes that have already been consumed.
|
|
|
|
*/
|
|
|
|
size_t buffer_consumed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Number of bytes that have been read into the buffer.
|
|
|
|
*/
|
|
|
|
size_t buffer_used;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Link to the next connection
|
|
|
|
*/
|
|
|
|
struct connection *next;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
connection_t;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Read all available messages on this connection
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void read_message(connection_t *);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Send as many messages as possible without blocking to the connection
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void try_send_all(connection_t *c);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Create a messge with the specified properties
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
message_t *create_message(fish_message_type_t type, const wchar_t *key, const wchar_t *val);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Init the library
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_t *key, const wchar_t *val));
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destroy library data
|
|
|
|
*/
|
|
|
|
void env_universal_common_destroy();
|
|
|
|
|
2005-09-23 04:16:52 +08:00
|
|
|
/**
|
|
|
|
Add all variable names to the specified list
|
2006-11-15 20:34:47 +08:00
|
|
|
|
|
|
|
This function operate agains the local copy of all universal
|
|
|
|
variables, it does not communicate with any other process.
|
2005-09-23 04:16:52 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void env_universal_common_get_names(wcstring_list_t &lst,
|
|
|
|
int show_exported,
|
|
|
|
int show_unexported);
|
2005-09-23 04:16:52 +08:00
|
|
|
|
2006-11-15 20:34:47 +08:00
|
|
|
/**
|
|
|
|
Perform the specified variable assignment.
|
|
|
|
|
|
|
|
This function operate agains the local copy of all universal
|
|
|
|
variables, it does not communicate with any other process.
|
|
|
|
|
|
|
|
Do not call this function. Create a message to do it. This function
|
|
|
|
is only to be used when fishd is dead.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void env_universal_common_set(const wchar_t *key, const wchar_t *val, int exportv);
|
2006-11-15 20:34:47 +08:00
|
|
|
|
|
|
|
/**
|
2012-11-18 18:23:22 +08:00
|
|
|
Remove the specified variable.
|
2006-11-15 20:34:47 +08:00
|
|
|
|
|
|
|
This function operate agains the local copy of all universal
|
|
|
|
variables, it does not communicate with any other process.
|
|
|
|
|
|
|
|
Do not call this function. Create a message to do it. This function
|
|
|
|
is only to be used when fishd is dead.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void env_universal_common_remove(const wcstring &key);
|
2006-11-15 20:34:47 +08:00
|
|
|
|
2005-09-23 04:16:52 +08:00
|
|
|
/**
|
|
|
|
Get the value of the variable with the specified name
|
2006-11-15 20:34:47 +08:00
|
|
|
|
|
|
|
This function operate agains the local copy of all universal
|
|
|
|
variables, it does not communicate with any other process.
|
2005-09-23 04:16:52 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t *env_universal_common_get(const wcstring &name);
|
2005-09-23 04:16:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the export flag of the variable with the specified
|
|
|
|
name. Returns 0 if the variable doesn't exist.
|
2006-11-15 20:34:47 +08:00
|
|
|
|
|
|
|
This function operate agains the local copy of all universal
|
|
|
|
variables, it does not communicate with any other process.
|
2005-09-23 04:16:52 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
int env_universal_common_get_export(const wcstring &name);
|
2005-09-23 04:16:52 +08:00
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Add messages about all existing variables to the specified connection
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void enqueue_all(connection_t *c);
|
2005-09-23 04:16:52 +08:00
|
|
|
|
2006-11-15 20:34:47 +08:00
|
|
|
/**
|
|
|
|
Fill in the specified connection_t struct. Use the specified file
|
|
|
|
descriptor for communication.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void connection_init(connection_t *c, int fd);
|
2006-10-19 00:44:38 +08:00
|
|
|
|
2006-11-15 20:34:47 +08:00
|
|
|
/**
|
|
|
|
Close and destroy the specified connection struct. This frees
|
|
|
|
allstructures allocated by the connection, such as ques of unsent
|
|
|
|
messages.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void connection_destroy(connection_t *c);
|
2005-09-23 04:16:52 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#endif
|