Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kdesu / kdesud / lexer.cpp
blob2a18c8b9ae8edfb17f3cb0fd1fd225d72ac7a13c
1 /* vi: ts=8 sts=4 sw=4
3 * This file is part of the KDE project, module kdesu.
4 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
6 * lexer.cpp: A lexer for the kdesud protocol. See kdesud.cpp for a
7 * description of the protocol.
8 */
10 #include "lexer.h"
12 #include <ctype.h>
15 Lexer::Lexer(const QByteArray &input)
17 m_Input = input;
18 in = 0;
21 Lexer::~Lexer()
23 // Erase buffers
24 m_Input.fill('x');
25 m_Output.fill('x');
28 QByteArray &Lexer::lval()
30 return m_Output;
34 * lex() is the lexer. There is no end-of-input check here so that has to be
35 * done by the caller.
38 int Lexer::lex()
40 char c;
42 c = m_Input[in++];
43 m_Output.fill('x');
44 m_Output.resize(0);
46 while (1)
48 // newline?
49 if (c == '\n')
50 return '\n';
52 // No control characters
53 if (iscntrl(c))
54 return Tok_none;
56 if (isspace(c))
57 while (isspace(c = m_Input[in++]))
60 // number?
61 if (isdigit(c))
63 m_Output += c;
64 while (isdigit(c = m_Input[in++]))
65 m_Output += c;
66 in--;
67 return Tok_num;
70 // quoted string?
71 if (c == '"')
73 c = m_Input[in++];
74 while ((c != '"') && !iscntrl(c)) {
75 // handle escaped characters
76 if (c == '\\')
77 m_Output += m_Input[in++];
78 else
79 m_Output += c;
80 c = m_Input[in++];
82 if (c == '"')
83 return Tok_str;
84 return Tok_none;
87 // normal string
88 while (!isspace(c) && !iscntrl(c))
90 m_Output += c;
91 c = m_Input[in++];
93 in--;
95 // command?
96 if (m_Output.length() <= 4)
98 if (m_Output == "EXEC")
99 return Tok_exec;
100 if (m_Output == "PASS")
101 return Tok_pass;
102 if (m_Output == "DEL")
103 return Tok_delCmd;
104 if (m_Output == "PING")
105 return Tok_ping;
106 if (m_Output == "EXIT")
107 return Tok_exit;
108 if (m_Output == "STOP")
109 return Tok_stop;
110 if (m_Output == "SET")
111 return Tok_set;
112 if (m_Output == "GET")
113 return Tok_get;
114 if (m_Output == "HOST")
115 return Tok_host;
116 if (m_Output == "SCHD")
117 return Tok_sched;
118 if (m_Output == "PRIO")
119 return Tok_prio;
120 if (m_Output == "DELV")
121 return Tok_delVar;
122 if (m_Output == "DELG")
123 return Tok_delGroup;
124 if (m_Output == "DELS")
125 return Tok_delSpecialKey;
126 if (m_Output == "GETK")
127 return Tok_getKeys;
128 if (m_Output == "CHKG")
129 return Tok_chkGroup;
132 return Tok_str;