summaryrefslogtreecommitdiffstats
path: root/kdesu/kdesud/lexer.cpp
blob: d9fbf463929eef5bab3811139e79da89de2c6de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* vi: ts=8 sts=4 sw=4
 *
 * This file is part of the KDE project, module kdesu.
 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
 * 
 * lexer.cpp: A lexer for the kdesud protocol. See kdesud.cpp for a
 *            description of the protocol.
 */

#include <ctype.h>
#include <tqcstring.h>
#include "lexer.h"


Lexer::Lexer(const TQCString &input)
{
    m_Input = input;
    in = 0;
}

Lexer::~Lexer()
{
    // Erase buffers
    m_Input.fill('x');
    m_Output.fill('x');
}

TQCString &Lexer::lval()
{
    return m_Output;
}

/*
 * lex() is the lexer. There is no end-of-input check here so that has to be
 * done by the caller.
 */

int Lexer::lex()
{
    char c;

    c = m_Input[in++];
    m_Output.fill('x');
    m_Output.resize(0);

    while (1) 
    {
	// newline? 
	if (c == '\n')
	    return '\n';

	// No control characters 
	if (iscntrl(c))
	    return Tok_none;

	if (isspace(c))
	    while (isspace(c = m_Input[in++]));

	// number?
	if (isdigit(c)) 
	{
	    m_Output += c;
	    while (isdigit(c = m_Input[in++]))
		m_Output += c;
	    in--;
	    return Tok_num;
	}

	// quoted string?
	if (c == '"') 
	{
	    c = m_Input[in++];
	    while ((c != '"') && !iscntrl(c)) {
		// handle escaped characters
		if (c == '\\')
		    m_Output += m_Input[in++];
		else
		    m_Output += c;
		c = m_Input[in++];
	    }
	    if (c == '"')
		return Tok_str;
	    return Tok_none;
	}

	// normal string
	while (!isspace(c) && !iscntrl(c)) 
	{
	    m_Output += c;
	    c = m_Input[in++];
	}
	in--;

	// command? 
	if (m_Output.length() <= 4) 
	{
	    if (m_Output == "EXEC")
		return Tok_exec;
	    if (m_Output == "PASS")
		return Tok_pass;
	    if (m_Output == "DEL")
		return Tok_delCmd;
	    if (m_Output == "PING")
		return Tok_ping;
	    if (m_Output == "EXIT")
		return Tok_exit;
	    if (m_Output == "STOP")
		return Tok_stop;
	    if (m_Output == "SET")
		return Tok_set;
	    if (m_Output == "GET")
		return Tok_get;
	    if (m_Output == "HOST")
		return Tok_host;
	    if (m_Output == "SCHD")
		return Tok_sched;
	    if (m_Output == "PRIO")
		return Tok_prio;
	    if (m_Output == "DELV")
		return Tok_delVar;
	    if (m_Output == "DELG")
		return Tok_delGroup;
	    if (m_Output == "DELS")
		return Tok_delSpecialKey;
        if (m_Output == "GETK")
        return Tok_getKeys;
        if (m_Output == "CHKG")
        return Tok_chkGroup;
	}

	return Tok_str;
    }
}