- while (!feof(handle)) {
- if (fgets(buffer, 1024, handle) == NULL) {
- /* fgets returns NULL on EOF and on error, so see which one it is. */
- if (feof(handle))
- break;
- die("Could not read configuration file\n");
- }
-
- if (config.terminal != NULL)
- replace_variable(buffer, "$terminal", config.terminal);
-
- /* Replace all custom variables */
- struct Variable *current;
- SLIST_FOREACH(current, &variables, variables)
- replace_variable(buffer, current->key, current->value);
-
- /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
- if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
- key[0] == '#' || strlen(key) < 3)
- continue;
-
- OPTION_STRING(terminal);
- OPTION_STRING(font);
-
- /* Colors */
- OPTION_COLORTRIPLE("client.focused", client.focused);
- OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
- OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
- OPTION_COLORTRIPLE("bar.focused", bar.focused);
- OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
-
- /* exec-lines (autostart) */
- if (strcasecmp(key, "exec") == 0) {
- struct Autostart *new = smalloc(sizeof(struct Autostart));
- new->command = sstrdup(value);
- TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
- continue;
- }
-
- /* key bindings */
- if (strcasecmp(key, "bind") == 0) {
- #define CHECK_MODIFIER(name) \
- if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
- modifiers |= BIND_##name; \
- walk += strlen(#name) + 1; \
- continue; \
- }
- char *walk = value, *rest;
- uint32_t modifiers = 0;
-
- while (*walk != '\0') {
- /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
- CHECK_MODIFIER(SHIFT);
- CHECK_MODIFIER(CONTROL);
- CHECK_MODIFIER(MODE_SWITCH);
- CHECK_MODIFIER(MOD1);
- CHECK_MODIFIER(MOD2);
- CHECK_MODIFIER(MOD3);
- CHECK_MODIFIER(MOD4);
- CHECK_MODIFIER(MOD5);
-
- /* No modifier found? Then we’re done with this step */
- break;
- }
-
- /* Now check for the keycode */
- int keycode = strtol(walk, &rest, 10);
- if (!rest || *rest != ' ')
- die("Invalid binding\n");
- rest++;
- LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
- Binding *new = smalloc(sizeof(Binding));
- new->keycode = keycode;
- new->mods = modifiers;
- new->command = sstrdup(rest);
- TAILQ_INSERT_TAIL(&bindings, new, bindings);
- continue;
- }
-
- if (strcasecmp(key, "floating_modifier") == 0) {
- char *walk = value;
- uint32_t modifiers = 0;
-
- while (*walk != '\0') {
- /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
- CHECK_MODIFIER(SHIFT);
- CHECK_MODIFIER(CONTROL);
- CHECK_MODIFIER(MODE_SWITCH);
- CHECK_MODIFIER(MOD1);
- CHECK_MODIFIER(MOD2);
- CHECK_MODIFIER(MOD3);
- CHECK_MODIFIER(MOD4);
- CHECK_MODIFIER(MOD5);
-
- /* No modifier found? Then we’re done with this step */
- break;
- }
-
- LOG("Floating modifiers = %d\n", modifiers);
- config.floating_modifier = modifiers;
- continue;
- }
-
- /* name "workspace number" "name of the workspace" */
- if (strcasecmp(key, "name") == 0) {
- LOG("name workspace: %s\n",value);
- char *ws_str = sstrdup(value);
- char *end = strchr(ws_str, ' ');
- if (end == NULL)
- die("Malformed name, couln't find terminating space\n");
- *end = '\0';
-
- /* Strip trailing whitespace */
- while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
- value[strlen(value)-1] = '\0';
-
- int ws_num = atoi(ws_str);
-
- if (ws_num < 1 || ws_num > 10)
- die("Malformed name, invalid workspace number\n");
-
- /* find the name */
- char *name = value;
- name += strlen(ws_str) + 1;
-
- if (name == '\0') {
- free(ws_str);
- continue;
- }
-
- workspace_set_name(&(workspaces[ws_num - 1]), name);
- free(ws_str);
- continue;
- }
-
- /* assign window class[/window title] → workspace */
- if (strcasecmp(key, "assign") == 0) {
- LOG("assign: \"%s\"\n", value);
- char *class_title;
- char *target;
- char *end;
-
- /* If the window class/title is quoted we skip quotes */
- if (value[0] == '"') {
- class_title = sstrdup(value+1);
- end = strchr(class_title, '"');
- } else {
- class_title = sstrdup(value);
- /* If it is not quoted, we terminate it at the first space */
- end = strchr(class_title, ' ');
- }
- if (end == NULL)
- die("Malformed assignment, couldn't find terminating quote\n");
- *end = '\0';
-
- /* Strip trailing whitespace */
- while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
- value[strlen(value)-1] = '\0';
-
- /* The target is the last argument separated by a space */
- if ((target = strrchr(value, ' ')) == NULL)
- die("Malformed assignment, couldn't find target (\"%s\")\n", value);
- target++;
-
- if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
- die("Malformed assignment, invalid workspace number\n");
-
- LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
-
- struct Assignment *new = scalloc(sizeof(struct Assignment));
- new->windowclass_title = class_title;
- if (strchr(target, '~') != NULL)
- new->floating = ASSIGN_FLOATING_ONLY;
-
- while (*target == '~')
- target++;
-
- if (atoi(target) >= 1 && atoi(target) <= 10) {
- if (new->floating == ASSIGN_FLOATING_ONLY)
- new->floating = ASSIGN_FLOATING;
- new->workspace = atoi(target);
- }
- TAILQ_INSERT_TAIL(&assignments, new, assignments);
-
- LOG("Assignment loaded: \"%s\":\n", class_title);
- if (new->floating != ASSIGN_FLOATING_ONLY)
- LOG(" to workspace %d\n", new->workspace);
-
- if (new->floating != ASSIGN_FLOATING_NO)
- LOG(" will be floating\n");
-
- continue;
- }
-
- /* set a custom variable */
- if (strcasecmp(key, "set") == 0) {
- if (value[0] != '$')
- die("Malformed variable assignment, name has to start with $\n");
-
- /* get key/value for this variable */
- char *v_key = value, *v_value;
- if ((v_value = strstr(value, " ")) == NULL)
- die("Malformed variable assignment, need a value\n");
-
- *(v_value++) = '\0';
-
- struct Variable *new = scalloc(sizeof(struct Variable));
- new->key = sstrdup(v_key);
- new->value = sstrdup(v_value);
- SLIST_INSERT_HEAD(&variables, new, variables);
- LOG("Got new variable %s = %s\n", v_key, v_value);
- continue;
- }
-
- if (strcasecmp(key, "ipc-socket") == 0) {
- config.ipc_socket_path = sstrdup(value);
- continue;
- }
-
- die("Unknown configfile option: %s\n", key);