1# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
2#
3# Copyright (C) 2006 Red Hat
4# see file 'COPYING' for use and warranty information
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; version 2 only
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19import sys
20
21tokens = ('DEFINE',
22          'NAME',
23          'TICK',
24          'SQUOTE',
25          'OBRACE',
26          'CBRACE',
27          'SEMI',
28          'OPAREN',
29          'CPAREN',
30          'COMMA')
31
32reserved = {
33    'define' : 'DEFINE' }
34
35t_TICK      = r'\`'
36t_SQUOTE    = r'\''
37t_OBRACE    = r'\{'
38t_CBRACE    = r'\}'
39t_SEMI      = r'\;'
40t_OPAREN    = r'\('
41t_CPAREN    = r'\)'
42t_COMMA     = r'\,'
43
44t_ignore    = " \t\n"
45
46def t_NAME(t):
47    r'[a-zA-Z_][a-zA-Z0-9_]*'
48    t.type = reserved.get(t.value,'NAME')
49    return t
50
51def t_error(t):
52    print("Illegal character '%s'" % t.value[0])
53    t.skip(1)
54
55from . import lex
56lex.lex()
57
58def p_statements(p):
59    '''statements : define_stmt
60                  | define_stmt statements
61    '''
62    if len(p) == 2:
63        p[0] = [p[1]]
64    else:
65        p[0] = [p[1]] + [p[2]]
66
67def p_define_stmt(p):
68    # This sucks - corresponds to 'define(`foo',`{ read write }')
69    '''define_stmt : DEFINE OPAREN TICK NAME SQUOTE COMMA TICK list SQUOTE CPAREN
70    '''
71
72    p[0] = [p[4], p[8]]
73
74def p_list(p):
75    '''list : NAME
76            | OBRACE names CBRACE
77    '''
78    if p[1] == "{":
79        p[0] = p[2]
80    else:
81        p[0] = [p[1]]
82
83def p_names(p):
84    '''names : NAME
85             | NAME names
86    '''
87    if len(p) == 2:
88        p[0] = [p[1]]
89    else:
90        p[0] = [p[1]] + p[2]
91
92def p_error(p):
93    print("Syntax error on line %d %s [type=%s]" % (p.lineno, p.value, p.type))
94
95from . import yacc
96yacc.yacc()
97
98
99f = open("all_perms.spt")
100txt = f.read()
101f.close()
102
103#lex.input(txt)
104#while 1:
105#    tok = lex.token()
106#    if not tok:
107#        break
108#    print tok
109
110test = "define(`foo',`{ read write append }')"
111test2 = """define(`all_filesystem_perms',`{ mount remount unmount getattr relabelfrom relabelto transition associate quotamod quotaget }')
112define(`all_security_perms',`{ compute_av compute_create compute_member check_context load_policy compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot }')
113"""
114result = yacc.parse(txt)
115print(result)
116
117