144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross/*
244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * Copyright (C) 2010 The Android Open Source Project
344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *
444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * Licensed under the Apache License, Version 2.0 (the "License");
544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * you may not use this file except in compliance with the License.
644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * You may obtain a copy of the License at
744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *
844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *      http://www.apache.org/licenses/LICENSE-2.0
944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *
1044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * Unless required by applicable law or agreed to in writing, software
1144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * distributed under the License is distributed on an "AS IS" BASIS,
1244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * See the License for the specific language governing permissions and
1444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * limitations under the License.
1544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross */
1644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
1744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <stdio.h>
1844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <unistd.h>
1944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <stdarg.h>
2044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <string.h>
2144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
2244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include "ueventd_parser.h"
2344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include "parser.h"
2444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include "log.h"
2544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include "util.h"
2644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
2744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic void parse_line_device(struct parse_state *state, int nargs, char **args);
2844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
2944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic void parse_config(const char *fn, char *s)
3044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{
3144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct parse_state state;
3244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    char *args[UEVENTD_PARSER_MAXARGS];
3344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    int nargs;
3444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    nargs = 0;
3544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    state.filename = fn;
3644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    state.line = 1;
3744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    state.ptr = s;
3844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    state.nexttoken = 0;
3944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    state.parse_line = parse_line_device;
4044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    for (;;) {
4144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        int token = next_token(&state);
4244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        switch (token) {
4344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        case T_EOF:
4444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            state.parse_line(&state, 0, 0);
4544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            return;
4644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        case T_NEWLINE:
4744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (nargs) {
4844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                state.parse_line(&state, nargs, args);
4944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                nargs = 0;
5044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            }
5144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            break;
5244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        case T_TEXT:
5344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (nargs < UEVENTD_PARSER_MAXARGS) {
5444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                args[nargs++] = state.text;
5544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            }
5644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            break;
5744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        }
5844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    }
5944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross}
6044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
6144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossint ueventd_parse_config_file(const char *fn)
6244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{
6344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    char *data;
6444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    data = read_file(fn, 0);
6544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    if (!data) return -1;
6644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
6744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    parse_config(fn, data);
6844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    DUMP();
6944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0;
7044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross}
7144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
7244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic void parse_line_device(struct parse_state* state, int nargs, char **args)
7344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{
7444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    set_device_permission(nargs, args);
7544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross}
76