Home | History | Annotate | Download | only in init
      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 "util.h"
     26 
     27 static void parse_line_device(struct parse_state *state, int nargs, char **args);
     28 
     29 static void parse_config(const char *fn, char *s)
     30 {
     31     struct parse_state state;
     32     char *args[UEVENTD_PARSER_MAXARGS];
     33     int nargs;
     34     nargs = 0;
     35     state.filename = fn;
     36     state.line = 1;
     37     state.ptr = s;
     38     state.nexttoken = 0;
     39     state.parse_line = parse_line_device;
     40     for (;;) {
     41         int token = next_token(&state);
     42         switch (token) {
     43         case T_EOF:
     44             state.parse_line(&state, 0, 0);
     45             return;
     46         case T_NEWLINE:
     47             if (nargs) {
     48                 state.parse_line(&state, nargs, args);
     49                 nargs = 0;
     50             }
     51             break;
     52         case T_TEXT:
     53             if (nargs < UEVENTD_PARSER_MAXARGS) {
     54                 args[nargs++] = state.text;
     55             }
     56             break;
     57         }
     58     }
     59 }
     60 
     61 int ueventd_parse_config_file(const char *fn)
     62 {
     63     char *data;
     64     data = read_file(fn, 0);
     65     if (!data) return -1;
     66 
     67     parse_config(fn, data);
     68     DUMP();
     69     return 0;
     70 }
     71 
     72 static void parse_line_device(struct parse_state* state, int nargs, char **args)
     73 {
     74     set_device_permission(nargs, args);
     75 }
     76