1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-07-31 21:59:25 -04:00

Reset location when importing, correctly resolve relative path

This commit is contained in:
Dave Davenport 2017-03-13 08:54:55 +01:00
parent b7f4b7484f
commit be036f086d
3 changed files with 28 additions and 9 deletions

View file

@ -29,12 +29,14 @@ typedef struct _ParseObject {
/** File pointer */ /** File pointer */
FILE *filein; FILE *filein;
char *filename;
/** Length of string */ /** Length of string */
int str_len; int str_len;
/** String */ /** String */
const char *input_str; const char *input_str;
/** Position in file */
YYLTYPE location;
} ParseObject; } ParseObject;
GQueue *file_queue = NULL; GQueue *file_queue = NULL;
@ -182,23 +184,37 @@ if ( queue == NULL ){
<INCLUDE>\"{STRING}\" { <INCLUDE>\"{STRING}\" {
yytext[yyleng-1] = '\0'; yytext[yyleng-1] = '\0';
ParseObject *top = g_queue_peek_head ( file_queue );
g_assert ( top != NULL );
char *filename = rofi_expand_path ( &yytext[1] ); char *filename = rofi_expand_path ( &yytext[1] );
// If no absolute path specified, expand it.
if ( ! g_path_is_absolute ( filename ) && top->type == PT_FILE ) {
char *basedir = g_path_get_dirname ( top->filename );
char *path = g_build_filename ( basedir, filename, NULL );
g_free ( filename);
filename = path;
g_free ( basedir );
}
FILE *f = fopen ( filename, "rb" ); FILE *f = fopen ( filename, "rb" );
if ( f ) { if ( f ) {
top->location = *yylloc;
ParseObject *po = g_malloc0(sizeof(ParseObject)); ParseObject *po = g_malloc0(sizeof(ParseObject));
po->type = PT_FILE; po->type = PT_FILE;
po->filename = filename;
po->filein = f; po->filein = f;
current = po; current = po;
g_queue_push_head ( file_queue, po ); g_queue_push_head ( file_queue, po );
yypush_buffer_state (yy_create_buffer ( 0, YY_BUF_SIZE )); yypush_buffer_state (yy_create_buffer ( 0, YY_BUF_SIZE ));
yylloc->first_line = yylloc->last_line = 1;
yylloc->first_column = yylloc->last_column = 1;
} else { } else {
char *str = g_markup_printf_escaped ( "Failed to open theme: <i>%s</i>\nError: <b>%s</b>", char *str = g_markup_printf_escaped ( "Failed to open theme: <i>%s</i>\nError: <b>%s</b>",
filename, strerror ( errno ) ); filename, strerror ( errno ) );
rofi_add_error_message ( g_string_new ( str ) ); rofi_add_error_message ( g_string_new ( str ) );
g_free ( str ); g_free ( str );
g_free(filename);
} }
g_free(filename);
// Pop out of include. */ // Pop out of include. */
BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue ))); BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue )));
} }
@ -408,6 +424,7 @@ if ( queue == NULL ){
if ( po ) { if ( po ) {
if ( po->type == PT_FILE ){ if ( po->type == PT_FILE ){
fclose ( po->filein ); fclose ( po->filein );
g_free ( po->filename );
} }
g_free ( po ); g_free ( po );
} }
@ -420,6 +437,7 @@ if ( queue == NULL ){
} else { } else {
yypop_buffer_state(); yypop_buffer_state();
current = po; current = po;
*yylloc = current->location;
BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue )));
} }
} }
@ -447,8 +465,8 @@ if ( queue == NULL ){
<DEFAULTS>. { <DEFAULTS>. {
return T_ERROR_DEFAULTS; return T_ERROR_DEFAULTS;
} }
<*>. { <INCLUDE>. {
return T_ERROR; return T_ERROR_INCLUDE;
} }
%% %%
@ -469,13 +487,13 @@ gboolean rofi_theme_parse_file ( const char *file )
file_queue = g_queue_new (); file_queue = g_queue_new ();
ParseObject *po = g_malloc0(sizeof(ParseObject)); ParseObject *po = g_malloc0(sizeof(ParseObject));
po->type = PT_FILE; po->type = PT_FILE;
po->filename = filename;
po->filein = yyin; po->filein = yyin;
current = po; current = po;
g_queue_push_head ( file_queue, po ); g_queue_push_head ( file_queue, po );
int parser_retv = yyparse ( file ); int parser_retv = yyparse ( file );
yylex_destroy (); yylex_destroy ();
g_free ( filename );
yyin = NULL; yyin = NULL;
// Free up. // Free up.

View file

@ -1,9 +1,9 @@
%define api.pure %define api.pure
%define parse.error verbose
%locations %locations
%glr-parser %glr-parser
%skeleton "glr.c" %skeleton "glr.c"
%debug %debug
%error-verbose
%parse-param {const char *what} %parse-param {const char *what}
%code requires { %code requires {
#include "theme.h" #include "theme.h"
@ -25,7 +25,7 @@ int yylex (YYSTYPE *, YYLTYPE *);
char *sval; char *sval;
int bval; int bval;
ThemeColor colorval; ThemeColor colorval;
ThemeWidget *theme; ThemeWidget *theme;
GList *name_path; GList *name_path;
Property *property; Property *property;
GHashTable *property_list; GHashTable *property_list;
@ -38,6 +38,7 @@ int yylex (YYSTYPE *, YYLTYPE *);
%token <ival> T_ERROR_ENTRY 3 "invalid property name" %token <ival> T_ERROR_ENTRY 3 "invalid property name"
%token <ival> T_ERROR_NAMESTRING 4 "invalid element name" %token <ival> T_ERROR_NAMESTRING 4 "invalid element name"
%token <ival> T_ERROR_DEFAULTS 5 "invalid defaults name" %token <ival> T_ERROR_DEFAULTS 5 "invalid defaults name"
%token <ival> T_ERROR_INCLUDE 6 "invalid import value"
%token <ival> T_INT %token <ival> T_INT
%token <fval> T_DOUBLE %token <fval> T_DOUBLE
%token <sval> T_STRING %token <sval> T_STRING
@ -56,7 +57,7 @@ int yylex (YYSTYPE *, YYLTYPE *);
%token PSEP "property separator" %token PSEP "property separator"
%token PCLOSE "property close" %token PCLOSE "property close"
%token NSEP "Name separator" %token NSEP "Name separator"
%token NAME_PREFIX "Name element prefix ('#')" %token NAME_PREFIX "Element section ('# {name} { ... }')"
%token WHITESPACE "White space" %token WHITESPACE "White space"
%token PDEFAULTS "Default settings section ( '* { ... }')" %token PDEFAULTS "Default settings section ( '* { ... }')"

View file

@ -267,7 +267,7 @@ void yyerror ( YYLTYPE *yylloc, const char *what, const char* s )
{ {
char *what_esc = g_markup_escape_text ( what, -1 ); char *what_esc = g_markup_escape_text ( what, -1 );
GString *str = g_string_new ( "" ); GString *str = g_string_new ( "" );
g_string_printf ( str, "<big><b>Error while parsing them:</b></big> <i>%s</i>\n", what_esc ); g_string_printf ( str, "<big><b>Error while parsing theme:</b></big> <i>%s</i>\n", what_esc );
g_free ( what_esc ); g_free ( what_esc );
char *esc = g_markup_escape_text ( s, -1 ); char *esc = g_markup_escape_text ( s, -1 );
g_string_append_printf ( str, "\tParser error: <i>%s</i>\n", esc ); g_string_append_printf ( str, "\tParser error: <i>%s</i>\n", esc );