summaryrefslogblamecommitdiffstats
path: root/src/include/gpxe/settings.h
blob: 7198399ef212b109e4158ab76b0fefc2826ab706 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                         
                        

                        
 

                
 


















                                                                         
          

                                                                    
           













                                                                    


   
                 
  

                                                                
   
                     




                                                                
                                          
          











                                                                            
                                                
                                                                            
           
                                                                     
                                                 


                                           

                                                          

   
                  
  

                                                                    
   
                      






                                                                     

                                
                                 
                         
                        



                                                                 
                                  


                                      




















                                                                            
 
   
                       
  




                                                              
   


                                                                            
 
 
   
                 
  

                                          

                                          


                                                             

 















                                                                    

   
                       
  
                                       

                                          

                                                             
 

                             
#ifndef _GPXE_SETTINGS_H
#define _GPXE_SETTINGS_H

/** @file
 *
 * Configuration settings
 *
 */

#include <stdint.h>
#include <gpxe/tables.h>
#include <gpxe/list.h>
#include <gpxe/refcnt.h>

struct settings;
struct in_addr;

/** Settings block operations */
struct settings_operations {
	/** Set value of setting
	 *
	 * @v settings		Settings block
	 * @v tag		Setting tag number
	 * @v data		Setting data, or NULL to clear setting
	 * @v len		Length of setting data
	 * @ret rc		Return status code
	 */
	int ( * set ) ( struct settings *settings, unsigned int tag,
			const void *data, size_t len );
	/** Get value of setting
	 *
	 * @v settings		Settings block
	 * @v tag		Setting tag number
	 * @v data		Buffer to fill with setting data
	 * @v len		Length of buffer
	 * @ret len		Length of setting data, or negative error
	 *
	 * The actual length of the setting will be returned even if
	 * the buffer was too small.
	 */
	int ( * get ) ( struct settings *settings, unsigned int tag,
			void *data, size_t len );
};

/** A settings block */
struct settings {
	/** Reference counter */
	struct refcnt *refcnt;
	/** Name */
	char name[16];
	/** List of all settings */
	struct list_head list;
	/** Settings block operations */
	struct settings_operations *op;
};

/**
 * A setting type
 *
 * This represents a type of setting (e.g. string, IPv4 address,
 * etc.).
 */
struct setting_type {
	/** Name
	 *
	 * This is the name exposed to the user (e.g. "string").
	 */
	const char *name;
	/** Parse and set value of setting
	 *
	 * @v settings		Settings block
	 * @v tag		Setting tag number
	 * @v value		Formatted setting data
	 * @ret rc		Return status code
	 */
	int ( * setf ) ( struct settings *settings, unsigned int tag,
			 const char *value );
	/** Get and format value of setting
	 *
	 * @v settings		Settings block, or NULL to search all blocks
	 * @v tag		Setting tag number
	 * @v buf		Buffer to contain formatted value
	 * @v len		Length of buffer
	 * @ret len		Length of formatted value, or negative error
	 */
	int ( * getf ) ( struct settings *settings, unsigned int tag,
			 char *buf, size_t len );
};

/** Declare a configuration setting type */
#define	__setting_type \
	__table ( struct setting_type, setting_types, 01 )

/**
 * A named setting
 *
 * This represents a single setting (e.g. "hostname"), encapsulating
 * the information about the setting's tag number and type.
 */
struct named_setting {
	/** Name
	 *
	 * This is the human-readable name for the setting.  Where
	 * possible, it should match the name used in dhcpd.conf (see
	 * dhcp-options(5)).
	 */
	const char *name;
	/** Description */
	const char *description;
	/** Setting tag number */
	unsigned int tag;
	/** Setting type
	 *
	 * This identifies the type of setting (e.g. string, IPv4
	 * address, etc.).
	 */
	struct setting_type *type;
};

/** Declare a configuration setting */
#define	__named_setting __table ( struct named_setting, named_settings, 01 )

extern struct settings interactive_settings;

extern int get_setting ( struct settings *settings, unsigned int tag,
			 void *data, size_t len );
extern int get_setting_len ( struct settings *settings, unsigned int tag );
extern int get_string_setting ( struct settings *settings, unsigned int tag,
				char *data, size_t len );
extern int get_ipv4_setting ( struct settings *settings, unsigned int tag,
			      struct in_addr *inp );
extern int get_int_setting ( struct settings *settings, unsigned int tag,
			     long *value );
extern int get_uint_setting ( struct settings *settings, unsigned int tag,
			      unsigned long *value );
extern struct settings * find_settings ( const char *name );
extern int set_typed_setting ( struct settings *settings,
			       unsigned int tag, struct setting_type *type,
			       const char *value );
extern int set_named_setting ( const char *name, const char *value );
extern int get_named_setting ( const char *name, char *buf, size_t len );

/**
 * Set value of setting
 *
 * @v settings		Settings block
 * @v tag		Setting tag number
 * @v data		Setting data, or NULL to clear setting
 * @v len		Length of setting data
 * @ret rc		Return status code
 */
static inline int set_setting ( struct settings *settings, unsigned int tag,
				const void *data, size_t len ) {
	return settings->op->set ( settings, tag, data, len );
}

/**
 * Delete setting
 *
 * @v settings		Settings block
 * @v tag		Setting tag number
 * @ret rc		Return status code
 */
static inline int delete_setting ( struct settings *settings,
				   unsigned int tag ) {
	return set_setting ( settings, tag, NULL, 0 );
}

/**
 * Get and format value of setting
 *
 * @v settings		Settings block, or NULL to search all blocks
 * @v tag		Setting tag number
 * @v type		Settings type
 * @v buf		Buffer to contain formatted value
 * @v len		Length of buffer
 * @ret len		Length of formatted value, or negative error
 */
static inline int get_typed_setting ( struct settings *settings,
				      unsigned int tag,
				      struct setting_type *type,
				      char *buf, size_t len ) {
	return type->getf ( settings, tag, buf, len );
}

/**
 * Delete named setting
 *
 * @v name		Name of setting
 * @ret rc		Return status code
 */
static inline int delete_named_setting ( const char *name ) {
	return set_named_setting ( name, NULL );
}

#endif /* _GPXE_SETTINGS_H */