@ -518,7 +518,7 @@ char **yon_char_parse(char *parameters, int *size, char *divider){
}
char * * yon_char_parsed_rip ( char * * char_string , int * size , int item_to_delete ) {
if ( char_string & & ( * size ) > 0 & & ( * size ) > item_to_delete & & item_to_delete > 0 ) {
if ( char_string & & ( * size ) > 0 & & ( * size ) > item_to_delete & & item_to_delete > = 0 ) {
char * * new_char_parsed = NULL ;
new_char_parsed = malloc ( sizeof ( char * ) * ( ( * size ) - 1 ) ) ;
int flag = 0 ;
@ -745,6 +745,18 @@ char *yon_char_parsed_to_string(config_str parsed, int size, char *divider_repla
} else if ( size = = 0 & & ! parsed ) return " " ;
}
int yon_char_parsed_find_element ( config_str parsed , int size , char * target ) {
if ( parsed & & size ) {
for ( int i = 0 ; i < size ; i + + ) {
if ( strstr ( parsed [ i ] , target ) ) {
return i ;
}
}
return - 1 ;
}
return - 1 ;
}
int yon_ubl_check_root ( ) {
if ( getuid ( ) = = 0 ) return 1 ;
else return 0 ;
@ -868,6 +880,8 @@ typedef struct yon_config_parameter
int flag1 ;
char * section ;
int ignore ;
char * save_command ;
char * load_command ;
} yon_config_parameter ;
yon_config_parameter * yon_config_parameter_new_with_data ( char * key , void * data ) {
@ -881,6 +895,8 @@ yon_config_parameter *yon_config_parameter_new_with_data(char *key, void *data){
param - > prev = NULL ;
param - > section = NULL ;
param - > ignore = 0 ;
param - > save_command = NULL ;
param - > load_command = NULL ;
return param ;
}
@ -900,7 +916,7 @@ dictionary *__yon_config_ignored = NULL;
# define check_config if(__yon__config__strings&&__yon__config__strings->data_type==DICTIONARY_CHAR_TYPE)
# define check_default_config if(__yon__config__default__strings)
# define for_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*) __yon__config__strings)
# define for_config yon_config_parameter *temp = NULL; for_dictionaries(temp, __yon__config__strings)
# define for_default_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*)__yon__config__default__strings)
# define yon_config_parameter_add_or_create_if_exists_with_data(dict,key,value) {if (dict){\
yon_config_parameter * dct = ( yon_config_parameter * ) yon_dictionary_get ( ( dictionary * * ) & dict , key ) ; \
@ -914,6 +930,53 @@ dictionary *__yon_config_ignored = NULL;
} \
else dict = yon_config_parameter_new_with_data ( key , value ) ; }
char * yon_config_get_type_path ( YON_CONFIG_TYPE type ) {
switch ( type ) {
case YON_CONFIG_DEFAULT :
return " default " ;
break ;
case YON_CONFIG_BOTH :
return " " ;
break ;
case YON_CONFIG_GLOBAL :
return " global " ;
break ;
case YON_CONFIG_LOCAL :
return " system " ;
break ;
}
return NULL ;
}
void yon_config_parameter_set_load_command ( char * key , char * command ) {
dictionary * found = yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ;
if ( found ) {
( ( yon_config_parameter * ) found ) - > load_command = yon_char_new ( command ) ;
}
}
void yon_config_parameter_set_save_command ( char * key , char * command ) {
dictionary * found = yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ;
if ( found ) {
( ( yon_config_parameter * ) found ) - > save_command = yon_char_new ( command ) ;
}
}
char * yon_config_parameter_get_load_command ( char * key ) {
dictionary * found = yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ;
if ( found ) {
return ( ( yon_config_parameter * ) found ) - > load_command ;
}
return NULL ;
}
char * yon_config_parameter_get_save_command ( char * key ) {
dictionary * found = yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ;
if ( found ) {
return ( ( yon_config_parameter * ) found ) - > save_command ;
}
return NULL ;
}
int yon_config_set_ignore ( char * key ) {
if ( ! yon_dictionary_get ( & __yon_config_ignored , key ) ) {
@ -958,7 +1021,7 @@ int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *secti
char * command = NULL ;
dictionary * dict ;
for_dictionaries ( dict , sections ) {
command = yon_char_unite ( ubconfig_load_command , config_type = = YON_CONFIG_GLOBAL ? " global get " : config_type = = YON_CONFIG_LOCAL ? " system get " : " default get " , dict - > key , " " , yon_dictionary_get_data ( dict , char * ) , NULL ) ;
command = yon_char_unite ( ubconfig_load_command _old , config_type = = YON_CONFIG_GLOBAL ? " global get " : config_type = = YON_CONFIG_LOCAL ? " system get " : " default get " , dict - > key , " " , yon_dictionary_get_data ( dict , char * ) , NULL ) ;
FILE * output = popen ( command , " r " ) ;
int i = 0 ;
char str [ 4096 ] ;
@ -1023,6 +1086,79 @@ int yon_config_parse_parameter(char *parameter,char **key, char **value){
return 0 ;
}
int yon_char_remove_brackets ( char * string ) {
int done = 0 ;
if ( string [ 0 ] = = ' \' ' | | string [ 0 ] = = ' \" ' ) { done = 1 ; free ( yon_char_divide ( string , 0 ) ) ; }
if ( string [ strlen ( string ) - 1 ] = = ' \' ' | | string [ strlen ( string ) - 1 ] = = ' \" ' ) { done = 1 ; string [ strlen ( string ) - 1 ] = ' \0 ' ; }
return done ;
}
char * yon_config_replace_parameter ( char * command , char * parameter , int place ) {
if ( ! yon_char_is_empty ( command ) ) ;
int size = 0 ;
config_str parsed = yon_char_parse ( command , & size , " " ) ;
if ( place < size ) {
parsed [ place ] = yon_char_new ( parameter ) ;
}
char * final = yon_char_parsed_to_string ( parsed , size , " " ) ;
yon_char_parsed_free ( parsed , size ) ;
return final ;
}
int yon_config_command_prepare ( config_str * commands , int * commands_size , char * command ) {
if ( ! yon_char_is_empty ( command ) ) {
int parsed_size ;
int done = 0 ;
config_str parsed = yon_char_parse ( command , & parsed_size , " " ) ;
if ( parsed_size > 5 ) {
for ( int j = 5 ; j < parsed_size ; j + + ) {
int new_size ;
config_str new_element = yon_char_parsed_new ( & new_size , parsed [ 0 ] , parsed [ 1 ] , parsed [ 2 ] , parsed [ 3 ] , parsed [ 4 ] , parsed [ j ] , NULL ) ;
* commands = yon_char_parsed_append ( * commands , commands_size , yon_char_parsed_to_string ( new_element , new_size , " " ) ) ;
done = 1 ;
}
}
return done ;
}
return 0 ;
}
int yon_config_load_config ( YON_CONFIG_TYPE config_type , . . . ) {
if ( config_type ! = YON_CONFIG_BOTH ) {
va_list args ;
va_start ( args , config_type ) ;
char * current = NULL ;
int command_size = 0 ;
config_str command = NULL ;
while ( ( current = va_arg ( args , char * ) ) ) {
yon_config_command_prepare ( & command , & command_size , current ) ;
}
for ( int i = 0 ; i < command_size ; i + + ) {
int parsed_size ;
config_str parsed = yon_config_load ( command [ i ] , & parsed_size ) ;
if ( parsed_size > 0 ) {
for ( int j = 0 ; j < parsed_size ; j + + ) {
if ( ! yon_char_is_empty ( parsed [ j ] ) & & strcmp ( parsed [ j ] , " (null) \n " ) ) {
if ( parsed [ j ] [ strlen ( parsed [ j ] ) - 1 ] = = ' \n ' ) parsed [ j ] [ strlen ( parsed [ j ] ) - 1 ] = ' \0 ' ;
char * current_value = yon_char_new ( parsed [ j ] ) ;
char * key = yon_char_divide_search ( current_value , " = " , - 1 ) ;
yon_char_remove_brackets ( current_value ) ;
char * current_command = yon_char_new ( command [ i ] ) ;
current_command = yon_config_replace_parameter ( current_command , key , 5 ) ;
yon_config_register ( key , current_command , current_value ) ;
if ( config_type = = YON_CONFIG_DEFAULT ) {
yon_config_set_status ( key , - 2 ) ;
}
}
}
}
yon_char_parsed_free ( parsed , parsed_size ) ;
}
}
}
int yon_config_load_register ( YON_CONFIG_TYPE config_type , char * section , char * parameter , . . . ) {
if ( config_type ! = YON_CONFIG_BOTH ) {
if ( __yon__config__strings ) {
@ -1042,7 +1178,7 @@ int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *par
char * command = NULL ;
dictionary * dict ;
for_dictionaries ( dict , sections ) {
command = yon_char_unite ( ubconfig_load_command , config_type = = YON_CONFIG_GLOBAL ? " global get " : config_type = = YON_CONFIG_LOCAL ? " system get " : " default get " , dict - > key , " " , yon_dictionary_get_data ( dict , char * ) , NULL ) ;
command = yon_char_unite ( ubconfig_load_command _old , config_type = = YON_CONFIG_GLOBAL ? " global get " : config_type = = YON_CONFIG_LOCAL ? " system get " : " default get " , dict - > key , " " , yon_dictionary_get_data ( dict , char * ) , NULL ) ;
FILE * output = popen ( command , " r " ) ;
int i = 0 ;
char str [ 4096 ] ;
@ -1283,7 +1419,9 @@ int yon_config_clean(){
else return 0 ;
}
void yon_config_register ( char * key , char * config_section , void * data ) {
void yon_config_register ( char * key , char * config_load , void * data ) {
key = yon_char_new ( key ) ;
config_load = yon_char_new ( config_load ) ;
if ( ! __yon__config__strings | | ! yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ) {
{
if ( __yon__config__strings ) {
@ -1305,17 +1443,25 @@ void yon_config_register(char *key, char *config_section, void *data){
__yon__config__strings = ( yon_config_parameter * ) yon_dictionary_get_last ( ( dictionary * ) __yon__config__strings ) ;
__yon__config__strings - > flag1 = 1 ;
__yon__config__strings - > data_type = DICTIONARY_CHAR_TYPE ;
__yon__config__strings - > section = yon_char_new ( config_section ) ;
__yon__config__strings - > load_command = config_load ;
int size = 0 ;
config_str section = yon_char_parse ( config_load , & size , " " ) ;
__yon__config__strings - > section = yon_char_new ( section [ 4 ] ) ;
yon_char_parsed_free ( section , size ) ;
}
else if ( yon_dictionary_get ( ( dictionary * * ) & __yon__config__strings , key ) ) {
if ( data ! = __yon__config__strings - > data & & strcmp ( __yon__config__strings - > data , data ) ) {
__yon__config__strings - > data = yon_char_new ( data ) ;
__yon__config__strings - > flag1 = 1 ;
__yon__config__strings - > data_type = DICTIONARY_CHAR_TYPE ;
__yon__config__strings - > section = yon_char_new ( config_section ) ;
if ( yon_dictionary_get ( & __yon_config_ignored , __yon__config__strings - > key ) ) {
yon_dictionary_rip ( __yon_config_ignored ) ;
}
__yon__config__strings - > load_command = config_load ;
if ( yon_dictionary_get ( & __yon_config_ignored , __yon__config__strings - > key ) ) {
yon_dictionary_rip ( __yon_config_ignored ) ;
}
int size = 0 ;
config_str section = yon_char_parse ( config_load , & size , " " ) ;
__yon__config__strings - > section = yon_char_new ( section [ 4 ] ) ;
yon_char_parsed_free ( section , size ) ;
}
}
}
@ -1347,6 +1493,255 @@ config_str yon_config_load(char *command, int *str_len){
}
}
config_str yon_config_load_file ( FILE * file , int * str_len ) {
FILE * output = file ;
char * * output_strings = NULL ;
output_strings = malloc ( sizeof ( char ) ) ;
int i = 0 ;
char str [ 4096 ] ;
memset ( str , 0 , 4096 ) ;
while ( fgets ( str , 4096 , output ) )
{
if ( strcmp ( str , " " ) ! = 0 )
{
output_strings = realloc ( output_strings , sizeof ( char * ) * ( i + 1 ) ) ;
output_strings [ i ] = NULL ;
output_strings [ i ] = yon_char_new ( str ) ;
memset ( str , 0 , 4096 ) ;
i + + ;
}
}
if ( i > 0 ) {
* str_len = i ;
return output_strings ;
} else {
* str_len = - 1 ;
return NULL ;
}
}
char * yon_config_parameter_to_string ( yon_config_parameter * parameter , int insert_section ) {
if ( parameter ) {
char * param_string = NULL ;
param_string = yon_char_unite ( insert_section ? parameter - > section : " " , insert_section ? " " : " " , parameter - > key , parameter - > flag1 = = - 1 ? NULL : " " , " = \' " , parameter - > data , " \' " , NULL ) ;
return param_string ;
}
return NULL ;
}
config_str yon_config_get_load_parameters_by_list ( int * size , config_str parameters , int params_size ) {
va_list list ;
( * size ) = 0 ;
config_str updated = NULL ;
int final_size ;
config_str final = NULL ;
char * current_str = NULL ;
for ( int i = 0 ; i < params_size ; i + + ) {
current_str = parameters [ i ] ;
for_config {
if ( ! strcmp ( temp - > key , current_str ) ) {
int position = yon_char_parsed_find_element ( final , * size , ( ( yon_config_parameter * ) temp ) - > section ) ;
if ( position > = 0 ) {
char * string = yon_char_unite ( ( final ) [ position ] , " " , current_str , NULL ) ;
free ( ( final ) [ position ] ) ;
( final ) [ position ] = string ;
} else {
char * string = yon_char_unite ( temp - > section , " " , current_str , NULL ) ;
yon_char_parsed_add_or_create_if_exists ( final , size , string ) ;
}
}
}
}
return final ;
}
config_str yon_config_get_save_parameters_by_list ( int * size , config_str parameters , int params_size ) {
va_list list ;
( * size ) = 0 ;
int removed_size ;
config_str removed = NULL ;
int updated_size ;
config_str updated = NULL ;
config_str final = NULL ;
char * current_str = NULL ;
for ( int i = 0 ; i < params_size ; i + + ) {
current_str = parameters [ i ] ;
for_config {
if ( ! strcmp ( temp - > key , current_str ) ) {
if ( ( ( yon_config_parameter * ) temp ) - > flag1 ! = - 2 ) {
char * action = NULL ;
config_str * current = NULL ;
int * current_size = NULL ;
switch ( ( ( yon_config_parameter * ) temp ) - > flag1 ) {
case - 1 :
action = " remove " ;
current = & removed ;
current_size = & removed_size ;
break ;
case 1 :
action = " set " ;
current = & updated ;
current_size = & updated_size ;
break ;
}
int position = yon_char_parsed_find_element ( * current , * current_size , ( ( yon_config_parameter * ) temp ) - > section ) ;
if ( position > = 0 ) {
char * string = yon_char_unite ( ( * current ) [ position ] , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 0 ) , NULL ) ;
free ( ( * current ) [ position ] ) ;
( * current ) [ position ] = string ;
} else {
char * string = yon_char_unite ( action , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 1 ) , NULL ) ;
yon_char_parsed_add_or_create_if_exists ( * current , current_size , string ) ;
}
}
}
}
}
final = yon_char_parsed_merge ( updated , updated_size , removed , removed_size , size ) ;
return final ;
}
config_str yon_config_get_save_parameters_by_key ( int * size , char * parameter , . . . ) {
va_list list ;
( * size ) = 0 ;
va_start ( list , parameter ) ;
int removed_size ;
config_str removed = NULL ;
int updated_size ;
config_str updated = NULL ;
config_str final = NULL ;
char * current_str = NULL ;
yon_va_while ( list , char * , current_str ) {
for_config {
if ( ! strcmp ( temp - > key , current_str ) ) {
if ( ( ( yon_config_parameter * ) temp ) - > flag1 ! = - 2 ) {
char * action = NULL ;
config_str * current = NULL ;
int * current_size = NULL ;
switch ( ( ( yon_config_parameter * ) temp ) - > flag1 ) {
case - 1 :
action = " remove " ;
current = & removed ;
current_size = & removed_size ;
break ;
case 1 :
action = " set " ;
current = & updated ;
current_size = & updated_size ;
break ;
}
int position = yon_char_parsed_find_element ( * current , * current_size , ( ( yon_config_parameter * ) temp ) - > section ) ;
if ( position > = 0 ) {
char * string = yon_char_unite ( ( * current ) [ position ] , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 0 ) , NULL ) ;
free ( ( * current ) [ position ] ) ;
( * current ) [ position ] = string ;
} else {
char * string = yon_char_unite ( action , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 1 ) , NULL ) ;
yon_char_parsed_add_or_create_if_exists ( * current , current_size , string ) ;
}
}
}
}
}
final = yon_char_parsed_merge ( updated , updated_size , removed , removed_size , size ) ;
return final ;
}
config_str yon_config_get_save_parameters ( int * size ) {
( * size ) = 0 ;
int removed_size ;
config_str removed = NULL ;
int updated_size ;
config_str updated = NULL ;
config_str final = NULL ;
for_config {
if ( ( ( yon_config_parameter * ) temp ) - > flag1 ! = 0 & & ( ( yon_config_parameter * ) temp ) - > flag1 ! = - 2 ) {
char * action = NULL ;
config_str * current = NULL ;
int * current_size = NULL ;
switch ( ( ( yon_config_parameter * ) temp ) - > flag1 ) {
case - 1 :
action = " remove " ;
current = & removed ;
current_size = & removed_size ;
break ;
case 1 :
action = " set " ;
current = & updated ;
current_size = & updated_size ;
break ;
}
int position = yon_char_parsed_find_element ( * current , * current_size , ( ( yon_config_parameter * ) temp ) - > section ) ;
if ( position > = 0 ) {
char * string = yon_char_unite ( ( * current ) [ position ] , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 0 ) , NULL ) ;
free ( ( * current ) [ position ] ) ;
( * current ) [ position ] = string ;
} else {
char * string = yon_char_unite ( action , " " , yon_config_parameter_to_string ( ( yon_config_parameter * ) temp , 1 ) , NULL ) ;
yon_char_parsed_add_or_create_if_exists ( * current , current_size , string ) ;
}
}
}
final = yon_char_parsed_merge ( updated , updated_size , removed , removed_size , size ) ;
return final ;
}
char * yon_config_save_simple ( YON_CONFIG_TYPE target , char * path ) {
int parameters_size = 0 ;
config_str parameters = yon_config_get_save_parameters ( & parameters_size ) ;
if ( parameters ) {
yon_char_parsed_prepend_strings ( parameters , parameters_size , ubconfig_set_command ( path ) ) ;
char * final_command = yon_char_parsed_to_string ( parameters , parameters_size , " ; " ) ;
printf ( " %s \n " , final_command ) ;
FILE * file = popen ( final_command , " r " ) ;
if ( file ) {
int file_size = 0 ;
config_str file_output = yon_config_load_file ( file , & file_size ) ;
if ( file_output ) {
char * final_string = yon_char_parsed_to_string ( file_output , file_size , " " ) ;
if ( ! yon_char_is_empty ( final_string ) ) {
return final_string ;
}
}
}
}
return NULL ;
}
char * yon_config_parameter_prepare_command ( char * command , char * path , char * section , char * parameter ) {
if ( path | | parameter ) {
int size = 0 ;
config_str parsed = yon_char_parse ( command , & size , " " ) ;
if ( path ) {
if ( size > 4 & & ! strcmp ( parsed [ 1 ] , " --source " ) ) {
free ( parsed [ 2 ] ) ;
parsed [ 2 ] = yon_char_new ( path ) ;
}
}
if ( section ) {
if ( size > 4 ) {
free ( parsed [ 4 ] ) ;
parsed [ 4 ] = yon_char_new ( section ) ;
}
}
if ( parameter ) {
if ( size > 5 ) {
free ( parsed [ 5 ] ) ;
parsed [ 5 ] = yon_char_new ( parameter ) ;
}
}
char * final = yon_char_parsed_to_string ( parsed , size , " " ) ;
yon_char_parsed_free ( parsed , size ) ;
return final ;
}
}
int yon_config_save_registered ( char * path ) {
check_config {
dictionary * dct ;
@ -1372,7 +1767,6 @@ int yon_config_save_registered(char *path){
for_dictionaries ( dct , sections_add ) {
char * command = yon_dictionary_get_data ( dct , char * ) ;
yon_launch ( command ) ;
if ( debug_output )
printf ( " %s \n " , command ) ;
}
yon_dictionary_free_all ( sections_add , free ) ;
@ -1380,7 +1774,6 @@ int yon_config_save_registered(char *path){
for_dictionaries ( dct , sections_remove ) {
char * command = yon_dictionary_get_data ( dct , char * ) ;
yon_launch ( command ) ;
if ( debug_output )
printf ( " %s \n " , command ) ;
}
yon_dictionary_free_all ( sections_remove , free ) ;