polybar/src/components/config_parser.cpp

334 lines
9.2 KiB
C++
Raw Permalink Normal View History

#include "components/config_parser.hpp"
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
#include <algorithm>
#include <cerrno>
#include <cstring>
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
#include <fstream>
#include "utils/file.hpp"
#include "utils/string.hpp"
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
POLYBAR_NS
config_parser::config_parser(const logger& logger, string&& file)
: m_log(logger), m_config_file(file_util::expand(file)) {}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
config config_parser::parse(string barname) {
m_log.notice("Parsing config file: %s", m_config_file);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
parse_file(m_config_file, {});
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
sectionmap_t sections = create_sectionmap();
vector<string> bars = get_bars(sections);
if (barname.empty()) {
if (bars.size() == 1) {
barname = bars[0];
} else if (bars.empty()) {
throw application_error("The config file contains no bar.");
} else {
throw application_error("The config file contains multiple bars, but no bar name was given. Available bars: " +
string_util::join(bars, ", "));
}
} else if (sections.find("bar/" + barname) == sections.end()) {
if (bars.empty()) {
throw application_error("Undefined bar: " + barname + ". The config file contains no bar.");
} else {
throw application_error(
"Undefined bar: " + barname + ". Available bars: " + string_util::join(get_bars(sections), ", "));
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
/*
* The first element in the files vector is always the main config file and
* because it has unique filenames, we can use all the elements from the
* second element onwards for the included list
*/
file_list included(m_files.begin() + 1, m_files.end());
config conf(m_log, move(m_config_file), move(barname));
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
conf.set_sections(move(sections));
conf.set_included(move(included));
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
if (use_xrm) {
conf.use_xrm();
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
return conf;
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
sectionmap_t config_parser::create_sectionmap() {
sectionmap_t sections{};
string current_section{};
for (const line_t& line : m_lines) {
if (!line.useful) {
continue;
}
if (line.is_header) {
current_section = line.header;
} else {
// The first valid line in the config is not a section definition
if (current_section.empty()) {
throw syntax_error("First valid line in config must be section header", m_files[line.file_index], line.line_no);
}
const string& key = line.key;
const string& value = line.value;
valuemap_t& valuemap = sections[current_section];
if (valuemap.find(key) == valuemap.end()) {
valuemap.emplace(key, value);
} else {
// Key already exists in this section
throw syntax_error("Duplicate key name \"" + key + "\" defined in section \"" + current_section + "\"",
m_files[line.file_index], line.line_no);
}
}
}
return sections;
}
/**
* Get the bars declared
*/
vector<string> config_parser::get_bars(const sectionmap_t& sections) const {
vector<string> bars;
for (const auto& it : sections) {
if (it.first.find(config::BAR_PREFIX) == 0) {
bars.push_back(it.first.substr(strlen(config::BAR_PREFIX)));
}
}
return bars;
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
void config_parser::parse_file(const string& file, file_list path) {
if (std::find(path.begin(), path.end(), file) != path.end()) {
string path_str{};
for (const auto& p : path) {
path_str += ">\t" + p + "\n";
}
path_str += ">\t" + file;
// We have already parsed this file in this path, so there are cyclic dependencies
throw application_error("include-file: Dependency cycle detected:\n" + path_str);
}
if (!file_util::exists(file)) {
throw application_error("Failed to open config file " + file + ": " + strerror(errno));
}
if (file_util::is_dir(file)) {
throw application_error("Config file " + file + " is a directory");
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
m_log.trace("config_parser: Parsing %s", file);
int file_index;
auto found = std::find(m_files.begin(), m_files.end(), file);
if (found == m_files.end()) {
file_index = m_files.size();
m_files.push_back(file);
} else {
/*
* `file` is already in the `files` vector so we calculate its index.
*
* This means that the file was already parsed, this can happen without
* cyclic dependencies, if the file is included twice
*/
file_index = found - m_files.begin();
}
path.push_back(file);
int line_no = 0;
string line_str{};
std::ifstream in(file);
if (!in) {
throw application_error("Failed to open config file " + file + ": " + strerror(errno));
}
auto dirname = file_util::dirname(file);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
while (std::getline(in, line_str)) {
line_no++;
line_t line;
line.file_index = file_index;
line.line_no = line_no;
parse_line(line, line_str);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
// Skip useless lines (comments, empty lines)
if (!line.useful) {
continue;
}
if (!line.is_header && line.key == "include-file") {
parse_file(file_util::expand(line.value, dirname), path);
} else if (!line.is_header && line.key == "include-directory") {
const string expanded_path = file_util::expand(line.value, dirname);
vector<string> file_list = file_util::list_files(expanded_path);
sort(file_list.begin(), file_list.end());
for (const auto& filename : file_list) {
parse_file(expanded_path + "/" + filename, path);
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
} else {
m_lines.push_back(line);
}
}
}
void config_parser::parse_line(line_t& line, const string& line_str) {
if (string_util::contains(line_str, "\ufeff")) {
throw syntax_error(
"This config file uses UTF-8 with BOM, which is not supported. Please use plain UTF-8 without BOM.",
m_files[line.file_index], line.line_no);
}
string line_trimmed = string_util::trim(line_str, isspace);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
line_type type = get_line_type(line_trimmed);
if (type == line_type::EMPTY || type == line_type::COMMENT) {
line.useful = false;
return;
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
if (type == line_type::UNKNOWN) {
throw syntax_error("Unknown line type: " + line_trimmed, m_files[line.file_index], line.line_no);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
line.useful = true;
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
if (type == line_type::HEADER) {
line.is_header = true;
line.header = parse_header(line, line_trimmed);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
} else if (type == line_type::KEY) {
line.is_header = false;
auto key_value = parse_key(line, line_trimmed);
line.key = key_value.first;
line.value = key_value.second;
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
}
line_type config_parser::get_line_type(const string& line) {
if (line.empty()) {
return line_type::EMPTY;
}
switch (line[0]) {
case '[':
return line_type::HEADER;
case ';':
case '#':
return line_type::COMMENT;
default: {
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
if (string_util::contains(line, "=")) {
return line_type::KEY;
} else {
return line_type::UNKNOWN;
}
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
}
string config_parser::parse_header(const line_t& line, const string& line_str) {
if (line_str.back() != ']') {
throw syntax_error("Missing ']' in header '" + line_str + "'", m_files[line.file_index], line.line_no);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
// Stripping square brackets
string header = line_str.substr(1, line_str.size() - 2);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
if (!is_valid_name(header)) {
throw invalid_name_error("Section", header, m_files[line.file_index], line.line_no);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
if (m_reserved_section_names.find(header) != m_reserved_section_names.end()) {
throw syntax_error(
"'" + header + "' is reserved and cannot be used as a section name", m_files[line.file_index], line.line_no);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
return header;
}
std::pair<string, string> config_parser::parse_key(const line_t& line, const string& line_str) {
size_t pos = line_str.find_first_of('=');
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
string key = string_util::trim(line_str.substr(0, pos), isspace);
string value = string_util::trim(line_str.substr(pos + 1), isspace);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
if (!is_valid_name(key)) {
throw invalid_name_error("Key", key, m_files[line.file_index], line.line_no);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
value = parse_escaped_value(line, move(value), key);
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
/*
* Only if the string is surrounded with double quotes, do we treat them
* not as part of the value and remove them.
*/
if (value.size() >= 2 && value.front() == '"' && value.back() == '"') {
value = value.substr(1, value.size() - 2);
}
// TODO check value for references
#if WITH_XRM
// Use xrm, if at least one value is an xrdb reference
if (!use_xrm && value.find("${xrdb") == 0) {
use_xrm = true;
}
#endif
return {move(key), move(value)};
}
bool config_parser::is_valid_name(const string& name) {
if (name.empty()) {
return false;
}
for (const char c : name) {
// Names with forbidden chars or spaces are not valid
if (isspace(c) || m_forbidden_chars.find_first_of(c) != string::npos) {
return false;
}
}
return true;
}
string config_parser::parse_escaped_value(const line_t& line, string&& value, const string& key) {
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
string cfg_value = value;
bool log = false;
auto backslash_pos = value.find('\\');
while (backslash_pos != string::npos) {
if (backslash_pos == value.size() - 1 || value[backslash_pos + 1] != '\\') {
log = true;
} else {
value = value.replace(backslash_pos, 2, "\\");
}
backslash_pos = value.find('\\', backslash_pos + 1);
}
if (log) {
m_log.err(
"%s:%d: Value '%s' of key '%s' contains one or more unescaped backslashes, please prepend them with the "
"backslash "
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
"escape character.",
m_files[line.file_index], line.line_no, cfg_value, key);
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
}
return move(value);
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
POLYBAR_NS_END