summaryrefslogtreecommitdiffstats
path: root/apps/plugins/shortcuts/shortcuts_common.c
blob: fbf6d81d61c2f1ad91208900319e8aa00dac7b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Bryan Childs
 * Copyright (c) 2007 Alexander Levin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "shortcuts.h"
MEM_FUNCTION_WRAPPERS;

#define SHORTCUTS_FILENAME "/shortcuts.link"

#define PATH_DISP_SEPARATOR "\t"
#define PATH_DISP_SEPARATOR_LEN 1 /* strlen(PATH_DISP_SEPARATOR) */
#define CONTROL_PREFIX "#"
#define CONTROL_PREFIX_LEN 1 /* strlen(CONTROL_PREFIX) */
#define NAME_VALUE_SEPARATOR "="
#define NAME_VALUE_SEPARATOR_LEN 1 /* strlen(NAME_VALUE_SEPARATOR) */

#define INSTR_DISPLAY_LAST_SEGMENTS "Display last path segments"

/* Memory (will be used for entries) */
void *memory_buf;
long memory_bufsize; /* Size of memory_buf in bytes */


/* The file we're processing */
sc_file_t sc_file;

bool parse_entry_content(char *line, sc_entry_t *entry, int last_segm);
char *last_segments(char *path, int nsegm);
bool is_control(char *line, sc_file_t *file);
bool starts_with(char *string, char *prefix);
bool parse_name_value(char *line, char *name, int namesize,
        char *value, int valuesize);
void write_entry_to_file(int fd, sc_entry_t *entry);
void write_int_instruction_to_file(int fd, char *instr, int value);


void allocate_memory(void **buf, size_t *bufsize)
{
    *buf = rb->plugin_get_buffer(bufsize);
    DEBUGF("Got %ld bytes of memory\n", *bufsize);
}


void init_sc_file(sc_file_t *file, void *buf, size_t bufsize)
{
    file->entries = (sc_entry_t*)buf;
    file->max_entries = bufsize / sizeof(sc_entry_t);
    DEBUGF("Buffer capacity: %d entries\n", file->max_entries);
    file->entry_cnt = 0;
    file->show_last_segments = -1;
}


bool load_sc_file(sc_file_t *file, char *filename, bool must_exist,
        void *entry_buf, size_t entry_bufsize)
{
    int fd = -1;
    bool ret_val = false; /* Assume bad case */
    int amountread = 0;
    char sc_content[2*MAX_PATH];
    sc_entry_t entry;

    /* We start to load a new file -> prepare it */
    init_sc_file(&sc_file, entry_buf, entry_bufsize);

    fd = rb->open(filename, O_RDONLY);
    if (fd < 0) {
        /* The file didn't exist on disk */
        if (!must_exist) {
            DEBUGF("Trying to create link file '%s'...\n", filename);
            fd = rb->creat(filename);
            if (fd < 0){
                /* For some reason we couldn't create the file,
                 * so return an error message and exit */
                rb->splashf(HZ*2, "Couldn't create the shortcuts file %s",
                           filename);
                goto end_of_proc;
            }
            /* File created, but there's nothing in it, so just exit */
            ret_val = true;
            goto end_of_proc;
        } else {
            rb->splashf(HZ, "Couldn't open %s", filename);
            goto end_of_proc;
        }
    }

    while ((amountread=rb->read_line(fd,sc_content, sizeof(sc_content)))) {
        if (is_control(sc_content, file)) {
            continue;
        }
        if (file->entry_cnt >= file->max_entries) {
            rb->splashf(HZ*2, "Too many entries in the file, max allowed: %d",
                       file->max_entries);
            goto end_of_proc;
        }
        if (!parse_entry_content(sc_content, &entry,file->show_last_segments)) {
            /* Could not parse the entry (too long path?) -> ignore */
            DEBUGF("Could not parse '%s' -> ignored\n", sc_content);
            continue;
        }
        DEBUGF("Parsed entry: path=%s, disp=%s\n", entry.path, entry.disp);
        append_entry(file, &entry);
    }

#ifdef SC_DEBUG
    print_file(file);
#endif
    
    ret_val = true; /* Everything went ok */

end_of_proc:
    if (fd >= 0) {
        rb->close(fd);
        fd = -1;
    }
    return ret_val;
}

#ifdef SC_DEBUG
void print_file(sc_file_t *file)
{
    DEBUGF("Number of entries : %d\n", file->entry_cnt);
    DEBUGF("Show Last Segments: %d\n", file->show_last_segments);
    int i;
    sc_entry_t *entry;
    for (i=0, entry=file->entries; i<file->entry_cnt; i++,entry++) {
        if (entry->explicit_disp) {
            DEBUGF("%2d. '%s', show as '%s'\n", i+1, entry->path, entry->disp);
        } else {
            DEBUGF("%2d. '%s' (%s)\n", i+1, entry->path, entry->disp);
        }
    }
}
#endif


bool append_entry(sc_file_t *file, sc_entry_t *entry)
{
    if (file->entry_cnt >= file->max_entries) {
        return false;
    }
    rb->memcpy(file->entries+file->entry_cnt, entry, sizeof(*entry));
    file->entry_cnt++;
    return true;
}


bool remove_entry(sc_file_t *file, int entry_idx)
{
    if ((entry_idx<0) || (entry_idx>=file->entry_cnt)) {
        return false;
    }
    sc_entry_t *start = file->entries + entry_idx;
    rb->memmove(start, start + 1,
            (file->entry_cnt-entry_idx-1) * sizeof(sc_entry_t));
    file->entry_cnt--;
    return true;
}


bool is_valid_index(sc_file_t *file, int entry_idx)
{
    return (entry_idx >= 0) && (entry_idx < file->entry_cnt);
}


bool parse_entry_content(char *line, sc_entry_t *entry, int last_segm)
{
    char *sep;
    char *path, *disp;
    unsigned int path_len, disp_len;
    bool expl;
    
    sep = rb->strcasestr(line, PATH_DISP_SEPARATOR);
    expl = (sep != NULL);
    if (expl) {
        /* Explicit name for the entry is specified -> use it */
        path = line;
        path_len = sep - line;
        disp = sep + PATH_DISP_SEPARATOR_LEN;
        disp_len = rb->strlen(disp);
    } else {
        /* No special name to display */
        path = line;
        path_len = rb->strlen(line);
        if (last_segm <= 0) {
            disp = path;
        } else {
            disp = last_segments(line, last_segm);
        }
        disp_len = rb->strlen(disp);
    }
    
    if (path_len >= sizeof(entry->path) || disp_len >= sizeof(entry->disp)) {
        DEBUGF("Bad entry: pathlen=%d, displen=%d\n", path_len, disp_len);
        return false;
    }
    rb->strncpy(entry->path, path, path_len);
    entry->path[path_len] = '\0';
    rb->strcpy(entry->disp, disp); /* Safe since we've checked the length */
    entry->explicit_disp = expl;
    return true;
}


char *last_segments(char *path, int nsegm)
{
    char *p = rb->strrchr(path, PATH_SEPARATOR[0]); /* Hack */
    int seg_cnt;
    if (p == NULL)
        return path; /* No separator??? */
    seg_cnt = 0;
    while ((p > path) && (seg_cnt < nsegm)) {
        p--;
        if (!starts_with(p, PATH_SEPARATOR)) {
            continue;
        }
        seg_cnt++;
        if (seg_cnt == nsegm && p > path) {
            p++; /* Eat the '/' to show that something has been truncated */
        }
    }
    return p;
}


bool is_control(char *line, sc_file_t *file)
{
    char name[MAX_PATH], value[MAX_PATH];
    if (!starts_with(line, CONTROL_PREFIX)) {
        return false;
    }
    line += CONTROL_PREFIX_LEN;
    
    if (!parse_name_value(line, name, sizeof(name),
            value, sizeof(value))) {
        DEBUGF("Bad processing instruction: '%s'\n", line);
        return true;
    }
    
    /* Process control instruction */
    if (rb->strcasestr(name, INSTR_DISPLAY_LAST_SEGMENTS)) {
        file->show_last_segments = rb->atoi(value);
        DEBUGF("Set show last segms to %d\n", file->show_last_segments);
    } else {
        /* Unknown instruction -> ignore */
        DEBUGF("Unknown processing instruction: '%s'\n", name);
    }
    
    return true;
}


bool starts_with(char *string, char *prefix)
{
    unsigned int pfx_len = rb->strlen(prefix);
    if (rb->strlen(string) < pfx_len)
        return false;
    return (rb->strncmp(string, prefix, pfx_len) == 0);
}


bool parse_name_value(char *line, char *name, int namesize,
        char *value, int valuesize)
{
    char *sep;
    int name_len, val_len;
    name[0] = value[0] = '\0';
    
    sep = rb->strcasestr(line, NAME_VALUE_SEPARATOR);
    if (sep == NULL) {
        /* No separator char -> weird instruction */
        return false;
    }
    name_len = sep - line;
    if (name_len >= namesize) {
        /* Too long name */
        return false;
    }
    rb->strncpy(name, line, name_len);
    name[name_len] = '\0';
    
    val_len = rb->strlen(line) - name_len - NAME_VALUE_SEPARATOR_LEN;
    if (val_len >= valuesize) {
        /* Too long value */
        return false;
    }
    rb->strncpy(value, sep+NAME_VALUE_SEPARATOR_LEN, val_len+1);
    return true;
}


bool dump_sc_file(sc_file_t *file, char *filename)
{
    DEBUGF("Dumping shortcuts to the file '%s'\n", filename);
    int fd;

    /* ideally, we should just write a new
    * entry to the file, but I'm going to
    * be lazy, and just re-write the whole
    * thing. */
    fd = rb->open(filename, O_WRONLY|O_TRUNC);
    if (fd < 0) {
        rb->splashf(HZ*2, "Could not open shortcuts file %s for writing",
                filename);
        return false;
    }
    
    /*
     * Write instructions
     */
    /* Always dump the 'display last segms' settings (even it it's
     * not active) so that it can be easily changed without having
     * to remember the setting name */
    write_int_instruction_to_file(fd,
            INSTR_DISPLAY_LAST_SEGMENTS, file->show_last_segments);
    
    int i;
    sc_entry_t *entry;
    for (i=0, entry=file->entries; i<file->entry_cnt; i++,entry++) {
        write_entry_to_file(fd, entry);
    }

    rb->close(fd);
    DEBUGF("Dumped %d entries to the file '%s'\n", file->entry_cnt, filename);

    return true;
}


void write_int_instruction_to_file(int fd, char *instr, int value)
{
    rb->fdprintf(fd, "%s%s%s%d\n", CONTROL_PREFIX, instr,
                 NAME_VALUE_SEPARATOR, value);
}


void write_entry_to_file(int fd, sc_entry_t *entry)
{
    rb->fdprintf(fd, "%s", entry->path);
    if (entry->explicit_disp) {
        rb->fdprintf(fd, "%s%s", PATH_DISP_SEPARATOR, entry->disp);
    }
    rb->fdprintf(fd, "\n");
}