ueventd_parser.c revision 44b65d047cc39baf30e21bfd8dd438f6bc1f77f5
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <unistd.h>
19#include <stdarg.h>
20#include <string.h>
21
22#include "ueventd_parser.h"
23#include "parser.h"
24#include "log.h"
25#include "list.h"
26#include "util.h"
27
28static void parse_line_device(struct parse_state *state, int nargs, char **args);
29
30static void parse_config(const char *fn, char *s)
31{
32    struct parse_state state;
33    char *args[UEVENTD_PARSER_MAXARGS];
34    int nargs;
35    nargs = 0;
36    state.filename = fn;
37    state.line = 1;
38    state.ptr = s;
39    state.nexttoken = 0;
40    state.parse_line = parse_line_device;
41    for (;;) {
42        int token = next_token(&state);
43        switch (token) {
44        case T_EOF:
45            state.parse_line(&state, 0, 0);
46            return;
47        case T_NEWLINE:
48            if (nargs) {
49                state.parse_line(&state, nargs, args);
50                nargs = 0;
51            }
52            break;
53        case T_TEXT:
54            if (nargs < UEVENTD_PARSER_MAXARGS) {
55                args[nargs++] = state.text;
56            }
57            break;
58        }
59    }
60}
61
62int ueventd_parse_config_file(const char *fn)
63{
64    char *data;
65    data = read_file(fn, 0);
66    if (!data) return -1;
67
68    parse_config(fn, data);
69    DUMP();
70    return 0;
71}
72
73static void parse_line_device(struct parse_state* state, int nargs, char **args)
74{
75    set_device_permission(nargs, args);
76}
77