1 | /* vi: ts=8 sts=4 sw=4 |
---|---|
2 | |
3 | This file is part of the KDE project, module kdesu. |
4 | SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org> |
5 | |
6 | lexer.cpp: A lexer for the kdesud protocol. See kdesud.cpp for a |
7 | description of the protocol. |
8 | */ |
9 | |
10 | #include "lexer.h" |
11 | |
12 | #include <ctype.h> |
13 | |
14 | Lexer::Lexer(const QByteArray &input) |
15 | { |
16 | m_Input = input; |
17 | in = 0; |
18 | } |
19 | |
20 | Lexer::~Lexer() |
21 | { |
22 | // Erase buffers |
23 | m_Input.fill(c: 'x'); |
24 | m_Output.fill(c: 'x'); |
25 | } |
26 | |
27 | QByteArray &Lexer::lval() |
28 | { |
29 | return m_Output; |
30 | } |
31 | |
32 | /* |
33 | * lex() is the lexer. There is no end-of-input check here so that has to be |
34 | * done by the caller. |
35 | */ |
36 | |
37 | int Lexer::lex() |
38 | { |
39 | char c; |
40 | |
41 | c = m_Input[in++]; |
42 | m_Output.fill(c: 'x'); |
43 | m_Output.resize(size: 0); |
44 | |
45 | while (1) { |
46 | // newline? |
47 | if (c == '\n') { |
48 | return '\n'; |
49 | } |
50 | |
51 | // No control characters |
52 | if (iscntrl(c)) { |
53 | return Tok_none; |
54 | } |
55 | |
56 | if (isspace(c)) { |
57 | while (isspace(c = m_Input[in++])) { |
58 | ; |
59 | } |
60 | } |
61 | |
62 | // number? |
63 | if (isdigit(c)) { |
64 | m_Output += c; |
65 | while (isdigit(c = m_Input[in++])) { |
66 | m_Output += c; |
67 | } |
68 | in--; |
69 | return Tok_num; |
70 | } |
71 | |
72 | // quoted string? |
73 | if (c == '"') { |
74 | c = m_Input[in++]; |
75 | while ((c != '"') && !iscntrl(c)) { |
76 | // handle escaped characters |
77 | if (c == '\\') { |
78 | c = m_Input[in++]; |
79 | if (iscntrl(c)) { |
80 | return Tok_none; |
81 | } |
82 | if (c == '^') { |
83 | c = m_Input[in++]; |
84 | if ((c == '"') || iscntrl(c)) { |
85 | return Tok_none; |
86 | } |
87 | m_Output += c - '@'; |
88 | } else { |
89 | m_Output += c; |
90 | } |
91 | } else { |
92 | m_Output += c; |
93 | } |
94 | c = m_Input[in++]; |
95 | } |
96 | if (c == '"') { |
97 | return Tok_str; |
98 | } |
99 | return Tok_none; |
100 | } |
101 | |
102 | // normal string |
103 | while (!isspace(c) && !iscntrl(c)) { |
104 | m_Output += c; |
105 | c = m_Input[in++]; |
106 | } |
107 | in--; |
108 | |
109 | // command? |
110 | if (m_Output.length() <= 4) { |
111 | if (m_Output == "EXEC") { |
112 | return Tok_exec; |
113 | } |
114 | if (m_Output == "PASS") { |
115 | return Tok_pass; |
116 | } |
117 | if (m_Output == "DEL") { |
118 | return Tok_delCmd; |
119 | } |
120 | if (m_Output == "PING") { |
121 | return Tok_ping; |
122 | } |
123 | if (m_Output == "EXIT") { |
124 | return Tok_exit; |
125 | } |
126 | if (m_Output == "STOP") { |
127 | return Tok_stop; |
128 | } |
129 | if (m_Output == "SET") { |
130 | return Tok_set; |
131 | } |
132 | if (m_Output == "GET") { |
133 | return Tok_get; |
134 | } |
135 | if (m_Output == "HOST") { |
136 | return Tok_host; |
137 | } |
138 | if (m_Output == "SCHD") { |
139 | return Tok_sched; |
140 | } |
141 | if (m_Output == "PRIO") { |
142 | return Tok_prio; |
143 | } |
144 | if (m_Output == "DELV") { |
145 | return Tok_delVar; |
146 | } |
147 | if (m_Output == "DELG") { |
148 | return Tok_delGroup; |
149 | } |
150 | if (m_Output == "DELS") { |
151 | return Tok_delSpecialKey; |
152 | } |
153 | if (m_Output == "GETK") { |
154 | return Tok_getKeys; |
155 | } |
156 | if (m_Output == "CHKG") { |
157 | return Tok_chkGroup; |
158 | } |
159 | } |
160 | |
161 | return Tok_str; |
162 | } |
163 | } |
164 |