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>
2014-04-28 04:34:51 +08:00
# include <set>
2014-06-10 03:07:40 +08:00
# include "wutil.h"
2005-10-04 23:11:39 +08:00
# include "util.h"
2014-04-26 07:09:26 +08:00
# include "env.h"
2005-09-20 21:26:39 +08:00
/**
2014-06-10 03:07:40 +08:00
The different types of messages found in the fishd file
2005-09-20 21:26:39 +08:00
*/
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 ,
2014-06-17 03:25:33 +08:00
SET_EXPORT ,
ERASE
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
/**
2014-06-10 03:07:40 +08:00
The size of the buffer used for reading from the file
2006-10-07 02:45:39 +08:00
*/
# define ENV_UNIVERSAL_BUFFER_SIZE 1024
2014-06-16 08:30:50 +08:00
typedef std : : vector < struct callback_data_t > callback_data_list_t ;
2005-09-23 04:16:52 +08:00
/**
2014-06-16 08:30:50 +08:00
Callback data , reflecting a change in universal variables
2005-09-23 04:16:52 +08:00
*/
2014-06-16 08:30:50 +08:00
struct callback_data_t
{
fish_message_type_t type ;
wcstring key ;
wcstring val ;
callback_data_t ( fish_message_type_t t , const wcstring & k , const wcstring & v ) : type ( t ) , key ( k ) , val ( v )
{
}
} ;
2014-04-30 02:28:00 +08:00
2014-04-26 07:09:26 +08:00
/** Class representing universal variables */
class env_universal_t
{
2014-04-28 04:34:51 +08:00
/* Current values */
2014-04-26 07:09:26 +08:00
var_table_t vars ;
2014-04-28 04:34:51 +08:00
/* Keys that have been modified, and need to be written. A value here that is not present in vars indicates a deleted value. */
std : : set < wcstring > modified ;
/* Path that we save to. If empty, use the default */
const wcstring explicit_vars_path ;
2014-04-26 07:09:26 +08:00
mutable pthread_mutex_t lock ;
2014-04-27 02:41:34 +08:00
bool tried_renaming ;
2014-04-30 02:28:00 +08:00
bool load_from_path ( const wcstring & path , callback_data_list_t * callbacks ) ;
void load_from_fd ( int fd , callback_data_list_t * callbacks ) ;
2014-04-27 02:41:34 +08:00
2014-04-28 04:34:51 +08:00
void set_internal ( const wcstring & key , const wcstring & val , bool exportv , bool overwrite ) ;
2014-06-14 06:15:11 +08:00
bool remove_internal ( const wcstring & name ) ;
2014-04-28 04:34:51 +08:00
/* Functions concerned with saving */
bool open_and_acquire_lock ( const wcstring & path , int * out_fd ) ;
bool open_temporary_file ( const wcstring & directory , wcstring * out_path , int * out_fd ) ;
2014-06-10 03:57:44 +08:00
bool write_to_fd ( int fd , const wcstring & path ) ;
2014-04-28 04:34:51 +08:00
bool move_new_vars_file_into_place ( const wcstring & src , const wcstring & dst ) ;
/* File id from which we last read */
file_id_t last_read_file ;
2014-06-17 03:25:33 +08:00
/* Given a variable table, generate callbacks representing the difference between our vars and the new vars */
void generate_callbacks ( const var_table_t & new_vars , callback_data_list_t * callbacks ) const ;
/* Given a variable table, copy unmodified values into self. May destructively modified vars_to_acquire. */
void acquire_variables ( var_table_t * vars_to_acquire ) ;
static void parse_message_internal ( const wcstring & msg , var_table_t * vars , wcstring * storage ) ;
static var_table_t read_message_internal ( int fd ) ;
2014-04-27 02:41:34 +08:00
2014-04-26 07:09:26 +08:00
public :
2014-04-28 04:34:51 +08:00
env_universal_t ( const wcstring & path ) ;
2014-04-26 07:09:26 +08:00
~ env_universal_t ( ) ;
/* Get the value of the variable with the specified name */
env_var_t get ( const wcstring & name ) const ;
/* Returns whether the variable with the given name is exported, or false if it does not exist */
bool get_export ( const wcstring & name ) const ;
/* Sets a variable */
void set ( const wcstring & key , const wcstring & val , bool exportv ) ;
2014-06-14 06:15:11 +08:00
/* Removes a variable. Returns true if it was found, false if not. */
bool remove ( const wcstring & name ) ;
2014-04-26 07:09:26 +08:00
/* Gets variable names */
wcstring_list_t get_names ( bool show_exported , bool show_unexported ) const ;
2014-04-27 02:41:34 +08:00
/** Loads variables at the correct path */
bool load ( ) ;
2014-04-30 08:03:00 +08:00
/** Reads and writes variables at the correct path. Returns true if modified variables were written. */
2014-04-30 02:28:00 +08:00
bool sync ( callback_data_list_t * callbacks ) ;
2014-04-26 07:09:26 +08:00
} ;
2014-05-08 05:22:05 +08:00
/** The "universal notifier" is an object responsible for broadcasting and receiving universal variable change notifications. These notifications do not contain the change, but merely indicate that the uvar file has changed. It is up to the uvar subsystem to re-read the file.
We support a few notificatins strategies . Not all strategies are supported on all platforms .
Notifiers may request polling , and / or provide a file descriptor to be watched for readability in select ( ) .
To request polling , the notifier overrides usec_delay_between_polls ( ) to return a positive value . That value will be used as the timeout in select ( ) . When select returns , the loop invokes poll ( ) . poll ( ) should return true to indicate that the file may have changed .
To provide a file descriptor , the notifier overrides notification_fd ( ) to return a non - negative fd . This will be added to the " read " file descriptor list in select ( ) . If the fd is readable , notification_fd_became_readable ( ) will be called ; that function should be overridden to return true if the file may have changed .
*/
2014-04-30 05:14:50 +08:00
class universal_notifier_t
{
public :
enum notifier_strategy_t
{
2014-05-08 05:22:05 +08:00
// Default meta-strategy to use the 'best' notifier for the system
2014-04-30 05:14:50 +08:00
strategy_default ,
2014-05-08 05:22:05 +08:00
// Use a value in shared memory. Simple, but requires polling and therefore semi-frequent wakeups.
2014-04-30 05:14:50 +08:00
strategy_shmem_polling ,
2014-05-08 05:22:05 +08:00
// Strategy that uses a named pipe. Somewhat complex, but portable and doesn't require polling most of the time.
2014-05-05 06:06:40 +08:00
strategy_named_pipe ,
2014-05-30 00:57:56 +08:00
2014-05-08 05:22:05 +08:00
// Strategy that uses notify(3). Simple and efficient, but OS X only.
2014-05-14 15:03:44 +08:00
strategy_notifyd ,
// Null notifier, does nothing
strategy_null
2014-04-30 05:14:50 +08:00
} ;
protected :
universal_notifier_t ( ) ;
private :
/* No copying */
universal_notifier_t & operator = ( const universal_notifier_t & ) ;
universal_notifier_t ( const universal_notifier_t & x ) ;
static notifier_strategy_t resolve_default_strategy ( ) ;
public :
virtual ~ universal_notifier_t ( ) ;
/* Factory constructor. Free with delete */
2014-05-02 07:44:37 +08:00
static universal_notifier_t * new_notifier_for_strategy ( notifier_strategy_t strat , const wchar_t * test_path = NULL ) ;
2014-04-30 05:14:50 +08:00
2014-04-30 08:03:00 +08:00
/* Default instance. Other instances are possible for testing. */
2014-04-30 05:14:50 +08:00
static universal_notifier_t & default_notifier ( ) ;
/* Does a fast poll(). Returns true if changed. */
virtual bool poll ( ) ;
/* Triggers a notification */
virtual void post_notification ( ) ;
2014-04-30 08:03:00 +08:00
/* Recommended delay between polls. A value of 0 means no polling required (so no timeout) */
virtual unsigned long usec_delay_between_polls ( ) const ;
2014-05-01 06:50:03 +08:00
2014-05-05 06:06:40 +08:00
/* Returns the fd from which to watch for events, or -1 if none */
virtual int notification_fd ( ) ;
2014-05-02 07:44:37 +08:00
/* The notification_fd is readable; drain it. Returns true if a notification is considered to have been posted. */
2014-05-06 14:33:05 +08:00
virtual bool notification_fd_became_readable ( int fd ) ;
2014-04-30 05:14:50 +08:00
} ;
2014-05-15 10:49:06 +08:00
bool universal_log_enabled ( ) ;
# define UNIVERSAL_LOG(x) if (universal_log_enabled()) fprintf(stderr, "UNIVERSAL LOG: %s\n", x)
2014-05-08 05:22:05 +08:00
/* Environment variable for requesting a particular universal notifier. See fetch_default_strategy_from_environment for names. */
# define UNIVERSAL_NOTIFIER_ENV_NAME "fish_universal_notifier"
2014-05-15 10:49:06 +08:00
/* Environment variable for enabling universal variable logging (to stderr) */
# define UNIVERSAL_LOGGING_ENV_NAME "fish_universal_log"
/* Environment variable for enabling fishd */
# define UNIVERSAL_USE_FISHD "fish_use_fishd"
2005-09-20 21:26:39 +08:00
# endif