source: neercs/trunk/src/configuration.c @ 2463

Last change on this file since 2463 was 2463, checked in by Jean-Yves Lamoureux, 15 years ago
  • Added configuration file and routines
File size: 6.3 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: neercs.h 2458 2008-06-19 21:50:29Z pterjan $
8 *
9 *  This program is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <unistd.h>
22#include <errno.h>
23#include <string.h>
24
25#include "neercs.h"
26
27
28int read_configuration_file(char *filename, struct screen_list *screen_list)
29{
30    FILE *fp;
31    struct stat st;
32    int size = 0, i = 0, total = 0, offset = 0;
33    char *buffer = NULL;
34
35    /* Check if file exist */
36    if(stat(filename, &st) < 0)
37    {
38        return -1;
39    }
40    /* Get its size  */
41    size = st.st_size;
42    if(!size)
43    {
44        fprintf(stderr, "File too short\n");
45        return -1;
46    }
47
48    /* Open it */
49    fp = fopen(filename, "r");
50    if(!fp)
51    {
52        return -1;
53    }
54
55    buffer = malloc(size);
56    if(!buffer)
57    {
58        fclose(fp);
59        printf("Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
60        return -1;
61    }
62    /* Read it */
63    while((i = fread(buffer+total, 1, size, fp)) > 0)
64    {
65        total+=i;
66    }
67
68    fclose(fp);
69
70    screen_list->config = NULL;
71    i=0;
72
73    /* Parse it */
74    while((i = parse_conf_line(buffer + offset, total-offset, screen_list)) > 0)
75    {
76        offset+=i;
77    }
78
79    free(buffer);
80
81    /* Fill neercs configuration with it */
82    fill_config(screen_list);
83
84    return 1;
85}
86
87
88int parse_conf_line(char *buf, int size, struct screen_list *screen_list)
89{
90    int i, s = 0, eol = 0, c = 0;
91    char *line = NULL;
92    int l = 0;
93    int in_quote = 0, end_spaces = 0;
94    static struct option *prev = NULL;
95
96    if(size <= 0) return -1;
97
98    /* Find EOL  */
99    for(i=0; i<size; i++)
100    {
101        if(buf[i] == '\n')
102        {
103            s = i + 1;
104            break;
105        }
106    }
107
108    /* Strip comments and trailing spaces */
109    for(i=0; i<s; i++)
110    {
111        if(buf[i] == ';' && !in_quote)
112        {
113            eol = i;
114            break;
115        }
116        else if(buf[i] == '\n')
117        {
118            eol = i;
119            break;
120        }
121        else if(buf[i] == ' ' && !c) {}
122        else
123        {
124            if(line == NULL)
125            {
126                line = malloc(2);
127                if(!line)
128                {
129                    printf("Can't allocate memory at %s:%d\n",
130                           __FUNCTION__, __LINE__);
131                    return -1;
132                }
133            }
134            else
135            {
136                line = realloc(line, l+2);
137                if(!line)
138                {
139                    printf("Can't allocate memory at %s:%d\n",
140                           __FUNCTION__, __LINE__);
141                    return -1;
142                }
143            }
144            if(buf[i] == '"')
145                in_quote = !in_quote;
146            if(buf[i] == ' ')
147                end_spaces++;
148            else
149                end_spaces = 0;
150
151            line[l]   = buf[i];
152            line[l+1] = 0;
153            l++;
154            c = 1;
155        }
156    }
157
158    if(c==0)
159    {
160        /* This line is empty, do nothing */
161    }
162    else
163    {
164        struct option *option = malloc(sizeof(struct option));
165        option->next = NULL;
166        l-=end_spaces;
167        line = realloc(line, l+1);
168        line[l] = 0;
169        get_key_value(line, option);
170
171        if(!screen_list->config)
172            screen_list->config = option;
173
174        if(prev)
175            prev->next = option;
176
177        prev = option;
178    }
179    free(line);
180    return s;
181}
182
183int get_key_value(char *line, struct option *option)
184{
185    unsigned int i, o = 0, b=0;
186    char *cur = NULL;
187    option->value = NULL;
188    option->key = NULL;
189
190    /* Line is a section delimiter */
191    if(line[0] == '[')
192    {
193        option->value = malloc(strlen(line) - 1);
194        memcpy(option->value, line+1, strlen(line) - 1);
195        option->value[strlen(line) - 2] = 0;
196        return 0;
197    }
198
199    cur = malloc(1);
200    cur[0] = 0;
201
202    for(i=0; i<strlen(line); i++)
203    {
204        if(line[i] == ' ' && !b) continue;
205
206        if(line[i] == '=')
207        {
208            b = 0;
209            cur[o] = 0;
210            o = 0;
211            option->key = cur;
212            cur = malloc(1);
213        }
214        else
215        {
216            cur = realloc(cur, o+2);
217            if(!cur)
218            {
219                printf("Can't allocate memory at %s:%d\n",
220                       __FUNCTION__, __LINE__);
221            }
222            cur[o]   = line[i];
223            o++;
224            b = 1;
225        }
226    }
227
228    cur[o] = 0;
229    option->value = cur;
230    return 0;
231}
232
233
234#define IS_TOKEN(t) (!strncmp(option->key, t, strlen(t)))
235#define IS_VALUE(t) (!strncmp(option->value, t, strlen(t)))
236
237
238int fill_config(struct screen_list *screen_list)
239{
240    int i = 0;
241    struct option *option = screen_list->config;
242    char *section = NULL;
243
244    while(option)
245    {
246        if(option->key == NULL)
247        {
248            section = option->key;
249            option = option->next;
250            continue;
251        }
252
253        if(IS_TOKEN("window_manager"))
254        {
255            if     (IS_VALUE("full"))   screen_list->wm_type = WM_FULL;
256            else if(IS_VALUE("hsplit")) screen_list->wm_type = WM_HSPLIT;
257            else if(IS_VALUE("vsplit")) screen_list->wm_type = WM_VSPLIT;
258            else if(IS_VALUE("card"))   screen_list->wm_type = WM_CARD;
259
260            else fprintf(stderr, "Unknow window manager '%s'\n", option->key);
261
262        } else if(IS_TOKEN("thumbnails"))
263        {
264            if     (IS_VALUE("true") || IS_VALUE("1")) screen_list->mini = 1;
265            else                                       screen_list->mini = 0;
266
267        } else if(IS_TOKEN("status"))
268        {
269            if     (IS_VALUE("true") || IS_VALUE("1")) screen_list->status = 1;
270            else                                       screen_list->status = 0;
271
272        } else
273        {
274            fprintf(stderr, "Unknow option '%s'\n", option->key);
275        }
276        option = option->next;
277    }
278
279    return i;
280}
Note: See TracBrowser for help on using the repository browser.