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

Last change on this file since 2479 was 2474, checked in by Pascal Terjan, 15 years ago
  • First more or less working version of attach
File size: 6.9 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, l = 1;
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 + 1);
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    buffer[total] = '\n';
68
69    fclose(fp);
70
71    screen_list->config = NULL;
72    i=0;
73
74    /* Parse it */
75    while((i = parse_conf_line(buffer + offset, total-offset, screen_list)) > 0)
76    {
77        offset+=i;
78        l++;
79    }
80
81    free(buffer);
82
83    /* Fill neercs configuration with it */
84    fill_config(screen_list);
85
86    return 1;
87}
88
89
90int parse_conf_line(char *buf, int size, struct screen_list *screen_list)
91{
92    int i, s = 0, eol = 0, c = 0;
93    char *line = NULL;
94    int l = 0;
95    int in_quote = 0, end_spaces = 0;
96    static struct option *prev = NULL;
97
98    if(size <= 0) return -1;
99
100    /* Find EOL  */
101    for(i=0; i<size; i++)
102    {
103        if(buf[i] == '\n')
104        {
105            s = i + 1;
106            break;
107        }
108    }
109
110    /* Strip comments and trailing spaces */
111    for(i=0; i<s; i++)
112    {
113        if(buf[i] == ';' && !in_quote)
114        {
115            eol = i;
116            break;
117        }
118        else if(buf[i] == '\n')
119        {
120            eol = i;
121            break;
122        }
123        else if(buf[i] == ' ' && !c) {}
124        else
125        {
126            if(line == NULL)
127            {
128                line = malloc(2);
129                if(!line)
130                {
131                    printf("Can't allocate memory at %s:%d\n",
132                           __FUNCTION__, __LINE__);
133                    return -1;
134                }
135            }
136            else
137            {
138                line = realloc(line, l+2);
139                if(!line)
140                {
141                    printf("Can't allocate memory at %s:%d\n",
142                           __FUNCTION__, __LINE__);
143                    return -1;
144                }
145            }
146            if(buf[i] == '"')
147                in_quote = !in_quote;
148            if(buf[i] == ' ')
149                end_spaces++;
150            else
151                end_spaces = 0;
152
153            line[l]   = buf[i];
154            line[l+1] = 0;
155            l++;
156            c = 1;
157        }
158    }
159
160    if(c==0)
161    {
162        /* This line is empty, do nothing */
163    }
164    else
165    {
166        struct option *option = malloc(sizeof(struct option));
167        option->next = NULL;
168        l-=end_spaces;
169        line = realloc(line, l+1);
170        line[l] = 0;
171
172        get_key_value(line, option);
173
174        if(!screen_list->config)
175            screen_list->config = option;
176
177        if(prev)
178            prev->next = option;
179
180        prev = option;
181    }
182    free(line);
183    return s;
184}
185
186int get_key_value(char *line, struct option *option)
187{
188    unsigned int i, o = 0, b=0, end_spaces = 0;
189    char *cur = NULL;
190    option->value = NULL;
191    option->key = NULL;
192
193    /* Line is a section delimiter */
194    if(line[0] == '[')
195    {
196        option->value = malloc(strlen(line) - 1);
197        memcpy(option->value, line+1, strlen(line) - 1);
198        option->value[strlen(line) - 2] = 0;
199        return 0;
200    }
201
202    cur = malloc(1);
203    cur[0] = 0;
204
205    for(i=0; i<strlen(line); i++)
206    {
207        if(line[i] == ' ' && !b) continue;
208
209
210        if(line[i] == '=')
211        {
212            b = 0;
213            cur[o-end_spaces] = 0;
214            cur = realloc(cur, (o-end_spaces)+1);
215            o = 0;
216            option->key = cur;
217            cur = malloc(1);
218        }
219        else
220        {
221            if(line[i] == ' ') end_spaces++;
222            else end_spaces = 0;
223
224            cur = realloc(cur, o+2);
225            if(!cur)
226            {
227                printf("Can't allocate memory at %s:%d\n",
228                       __FUNCTION__, __LINE__);
229            }
230            cur[o]   = line[i];
231            o++;
232            b = 1;
233
234        }
235    }
236    cur[o] = 0;
237    option->value = cur;
238    return 0;
239}
240
241
242#define IS_TOKEN(t) (!strncmp(option->key, t, strlen(option->key)))
243#define IS_VALUE(t) (!strncmp(option->value, t, strlen(option->value)))
244
245
246int fill_config(struct screen_list *screen_list)
247{
248    int i = 0;
249    struct option *option = screen_list->config;
250    char *section = NULL;
251
252    while(option)
253    {
254        if(option->key == NULL)
255        {
256            section = option->key;
257            option = option->next;
258            continue;
259        }
260
261        if(IS_TOKEN("window_manager"))
262        {
263            if     (IS_VALUE("full"))   screen_list->wm_type = WM_FULL;
264            else if(IS_VALUE("hsplit")) screen_list->wm_type = WM_HSPLIT;
265            else if(IS_VALUE("vsplit")) screen_list->wm_type = WM_VSPLIT;
266            else if(IS_VALUE("card"))   screen_list->wm_type = WM_CARD;
267
268            else fprintf(stderr, "Unknow window manager '%s'\n", option->key);
269
270        } else if(IS_TOKEN("thumbnails"))
271        {
272            if     (IS_VALUE("true") || IS_VALUE("1")) screen_list->mini = 1;
273            else                                       screen_list->mini = 0;
274
275        } else if(IS_TOKEN("status_bar"))
276        {
277            if     (IS_VALUE("true") || IS_VALUE("1")) screen_list->status = 1;
278            else                                       screen_list->status = 0;
279
280        } else if(IS_TOKEN("screensaver_timeout"))
281        {
282            screen_list->screensaver_timeout = atoi(option->value) * 1000000;
283            /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */
284            if(!screen_list->screensaver_timeout) screen_list->screensaver_timeout-=1;
285        } else if(IS_TOKEN("socket_dir"))
286        {
287            screen_list->socket_dir = option->value;
288        } else
289        {
290            fprintf(stderr, "Unknown option '%s'\n", option->key);
291        }
292        option = option->next;
293    }
294
295    return i;
296}
Note: See TracBrowser for help on using the repository browser.