/* * neercs console-based window manager * Copyright (c) 2006 Sam Hocevar * 2008 Jean-Yves Lamoureux * All Rights Reserved * * $Id: neercs.h 2458 2008-06-19 21:50:29Z pterjan $ * * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ #include #include #include #include #include #include #include #include #include "neercs.h" int read_configuration_file(char *filename, struct screen_list *screen_list) { FILE *fp; struct stat st; int size = 0, i = 0, total = 0, offset = 0; char *buffer = NULL; /* Check if file exist */ if(stat(filename, &st) < 0) { return -1; } /* Get its size */ size = st.st_size; if(!size) { fprintf(stderr, "File too short\n"); return -1; } /* Open it */ fp = fopen(filename, "r"); if(!fp) { return -1; } buffer = malloc(size); if(!buffer) { fclose(fp); printf("Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); return -1; } /* Read it */ while((i = fread(buffer+total, 1, size, fp)) > 0) { total+=i; } fclose(fp); screen_list->config = NULL; i=0; /* Parse it */ while((i = parse_conf_line(buffer + offset, total-offset, screen_list)) > 0) { offset+=i; } free(buffer); /* Fill neercs configuration with it */ fill_config(screen_list); return 1; } int parse_conf_line(char *buf, int size, struct screen_list *screen_list) { int i, s = 0, eol = 0, c = 0; char *line = NULL; int l = 0; int in_quote = 0, end_spaces = 0; static struct option *prev = NULL; if(size <= 0) return -1; /* Find EOL */ for(i=0; inext = NULL; l-=end_spaces; line = realloc(line, l+1); line[l] = 0; get_key_value(line, option); if(!screen_list->config) screen_list->config = option; if(prev) prev->next = option; prev = option; } free(line); return s; } int get_key_value(char *line, struct option *option) { unsigned int i, o = 0, b=0; char *cur = NULL; option->value = NULL; option->key = NULL; /* Line is a section delimiter */ if(line[0] == '[') { option->value = malloc(strlen(line) - 1); memcpy(option->value, line+1, strlen(line) - 1); option->value[strlen(line) - 2] = 0; return 0; } cur = malloc(1); cur[0] = 0; for(i=0; ivalue = cur; return 0; } #define IS_TOKEN(t) (!strncmp(option->key, t, strlen(t))) #define IS_VALUE(t) (!strncmp(option->value, t, strlen(t))) int fill_config(struct screen_list *screen_list) { int i = 0; struct option *option = screen_list->config; char *section = NULL; while(option) { if(option->key == NULL) { section = option->key; option = option->next; continue; } if(IS_TOKEN("window_manager")) { if (IS_VALUE("full")) screen_list->wm_type = WM_FULL; else if(IS_VALUE("hsplit")) screen_list->wm_type = WM_HSPLIT; else if(IS_VALUE("vsplit")) screen_list->wm_type = WM_VSPLIT; else if(IS_VALUE("card")) screen_list->wm_type = WM_CARD; else fprintf(stderr, "Unknow window manager '%s'\n", option->key); } else if(IS_TOKEN("thumbnails")) { if (IS_VALUE("true") || IS_VALUE("1")) screen_list->mini = 1; else screen_list->mini = 0; } else if(IS_TOKEN("status")) { if (IS_VALUE("true") || IS_VALUE("1")) screen_list->status = 1; else screen_list->status = 0; } else { fprintf(stderr, "Unknow option '%s'\n", option->key); } option = option->next; } return i; }