TAILQ_ENTRY(Barconfig) configs;
};
+/**
+ * Finds the configuration file to use (either the one specified by
+ * override_configpath), the user’s one or the system default) and calls
+ * parse_file().
+ *
+ * If you specify override_configpath, only this path is used to look for a
+ * configuration file.
+ *
+ * If use_nagbar is false, don't try to start i3-nagbar but log the errors to
+ * stdout/stderr instead.
+ *
+ */
+bool parse_configuration(const char *override_configpath, bool use_nagbar);
+
/**
* Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
*
/**
* Parses the given file by first replacing the variables, then calling
- * parse_config and possibly launching i3-nagbar.
+ * parse_config and launching i3-nagbar if use_nagbar is true.
+ *
+ * The return value is a boolean indicating whether there were errors during
+ * parsing.
*
*/
-void parse_file(const char *f);
+bool parse_file(const char *f, bool use_nagbar);
i3Font font;
font.type = FONT_TYPE_NONE;
+ /* No XCB connction, return early because we're just validating the
+ * configuration file. */
+ if (conn == NULL) {
+ return font;
+ }
+
#if PANGO_SUPPORT
/* Try to load a pango font if specified */
if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
* parse_file().
*
*/
-static void parse_configuration(const char *override_configpath) {
+bool parse_configuration(const char *override_configpath, bool use_nagbar) {
char *path = get_config_path(override_configpath);
LOG("Parsing configfile %s\n", path);
FREE(current_configpath);
current_configpath = path;
- parse_file(path);
+
+ /* initialize default bindings if we're just validating the config file */
+ if (!use_nagbar && bindings == NULL) {
+ bindings = scalloc(sizeof(struct bindings_head));
+ TAILQ_INIT(bindings);
+ }
+
+ return parse_file(path, use_nagbar);
}
/*
if (config.workspace_urgency_timer == 0)
config.workspace_urgency_timer = 0.5;
- parse_configuration(override_configpath);
+ parse_configuration(override_configpath, true);
if (reload) {
translate_keysyms();
* parse_config and possibly launching i3-nagbar.
*
*/
-void parse_file(const char *f) {
+bool parse_file(const char *f, bool use_nagbar) {
SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
int fd, ret, read_bytes = 0;
struct stat stbuf;
check_for_duplicate_bindings(context);
- if (context->has_errors || context->has_warnings) {
+ if (use_nagbar && (context->has_errors || context->has_warnings)) {
ELOG("FYI: You are using i3 version " I3_VERSION "\n");
if (version == 3)
ELOG("Please convert your configfile first, then fix any remaining errors (see above).\n");
free(pageraction);
}
+ bool has_errors = context->has_errors;
+
FREE(context->line_copy);
free(context);
free(new);
SLIST_REMOVE_HEAD(&variables, variables);
FREE(current);
}
+
+ return !has_errors;
}
#endif
/* We hope that those are supported and set them to true */
bool xcursor_supported = true;
-/* This will be set to true when -C is used so that functions can behave
- * slightly differently. We don’t want i3-nagbar to be started when validating
- * the config, for example. */
-bool only_check_config = false;
-
/*
* This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
* See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
bool force_xinerama = false;
char *fake_outputs = NULL;
bool disable_signalhandler = false;
+ bool only_check_config = false;
static struct option long_options[] = {
{"no-autostart", no_argument, 0, 'a'},
{"config", required_argument, 0, 'c'},
}
}
+ if (only_check_config) {
+ exit(parse_configuration(override_configpath, false) ? 0 : 1);
+ }
+
/* If the user passes more arguments, we act like i3-msg would: Just send
* the arguments as an IPC message to i3. This allows for nice semantic
* commands such as 'i3 border none'. */
- if (!only_check_config && optind < argc) {
+ if (optind < argc) {
/* We enable verbose mode so that the user knows what’s going on.
* This should make it easier to find mistakes when the user passes
* arguments by mistake. */
xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
load_configuration(conn, override_configpath, false);
- if (only_check_config) {
- LOG("Done checking configuration file. Exiting.\n");
- exit(0);
- }
if (config.ipc_socket_path == NULL) {
/* Fall back to a file name in /tmp/ based on the PID */
--- /dev/null
+#!perl
+# vim:ts=4:sw=4:expandtab
+#
+# Please read the following documents before working on tests:
+# • http://build.i3wm.org/docs/testsuite.html
+# (or docs/testsuite)
+#
+# • http://build.i3wm.org/docs/lib-i3test.html
+# (alternatively: perldoc ./testcases/lib/i3test.pm)
+#
+# • http://build.i3wm.org/docs/ipc.html
+# (or docs/ipc)
+#
+# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
+# (unless you are already familiar with Perl)
+#
+# Check whether the -C option works without a display and doesn't
+# accidentally start the nagbar.
+#
+use i3test i3_autostart => 0;
+use File::Temp qw(tempfile);
+
+sub check_config {
+ my ($config) = @_;
+ my ($fh, $tmpfile) = tempfile(UNLINK => 1);
+ print $fh $config;
+ my $output = qx(DISPLAY= ../i3 -C -c $tmpfile 2>&1);
+ my $retval = $?;
+ $fh->flush;
+ close($fh);
+ return ($retval >> 8, $output);
+}
+
+################################################################################
+# 1: test with a bogus configuration file
+################################################################################
+
+my $cfg = <<EOT;
+# i3 config file (v4)
+i_am_an_unknown_config option
+EOT
+
+my ($ret, $out) = check_config($cfg);
+is($ret, 1, "exit code == 1");
+like($out, qr/ERROR: *CONFIG: *[Ee]xpected.*tokens/, 'bogus config file');
+
+################################################################################
+# 2: test with a valid configuration file
+################################################################################
+
+my $cfg = <<EOT;
+# i3 config file (v4)
+font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
+EOT
+
+my ($ret, $out) = check_config($cfg);
+is($ret, 0, "exit code == 0");
+is($out, "", 'valid config file');
+
+done_testing;