File indexing completed on 2024-05-05 03:54:29

0001 /* vi: ts=8 sts=4 sw=4
0002 
0003     This file is part of the KDE project, module kdesu.
0004     SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
0005 
0006     lexer.cpp: A lexer for the kdesud protocol. See kdesud.cpp for a
0007                description of the protocol.
0008 */
0009 
0010 #include "lexer.h"
0011 
0012 #include <ctype.h>
0013 
0014 Lexer::Lexer(const QByteArray &input)
0015 {
0016     m_Input = input;
0017     in = 0;
0018 }
0019 
0020 Lexer::~Lexer()
0021 {
0022     // Erase buffers
0023     m_Input.fill('x');
0024     m_Output.fill('x');
0025 }
0026 
0027 QByteArray &Lexer::lval()
0028 {
0029     return m_Output;
0030 }
0031 
0032 /*
0033  * lex() is the lexer. There is no end-of-input check here so that has to be
0034  * done by the caller.
0035  */
0036 
0037 int Lexer::lex()
0038 {
0039     char c;
0040 
0041     c = m_Input[in++];
0042     m_Output.fill('x');
0043     m_Output.resize(0);
0044 
0045     while (1) {
0046         // newline?
0047         if (c == '\n') {
0048             return '\n';
0049         }
0050 
0051         // No control characters
0052         if (iscntrl(c)) {
0053             return Tok_none;
0054         }
0055 
0056         if (isspace(c)) {
0057             while (isspace(c = m_Input[in++])) {
0058                 ;
0059             }
0060         }
0061 
0062         // number?
0063         if (isdigit(c)) {
0064             m_Output += c;
0065             while (isdigit(c = m_Input[in++])) {
0066                 m_Output += c;
0067             }
0068             in--;
0069             return Tok_num;
0070         }
0071 
0072         // quoted string?
0073         if (c == '"') {
0074             c = m_Input[in++];
0075             while ((c != '"') && !iscntrl(c)) {
0076                 // handle escaped characters
0077                 if (c == '\\') {
0078                     c = m_Input[in++];
0079                     if (iscntrl(c)) {
0080                         return Tok_none;
0081                     }
0082                     if (c == '^') {
0083                         c = m_Input[in++];
0084                         if ((c == '"') || iscntrl(c)) {
0085                             return Tok_none;
0086                         }
0087                         m_Output += c - '@';
0088                     } else {
0089                         m_Output += c;
0090                     }
0091                 } else {
0092                     m_Output += c;
0093                 }
0094                 c = m_Input[in++];
0095             }
0096             if (c == '"') {
0097                 return Tok_str;
0098             }
0099             return Tok_none;
0100         }
0101 
0102         // normal string
0103         while (!isspace(c) && !iscntrl(c)) {
0104             m_Output += c;
0105             c = m_Input[in++];
0106         }
0107         in--;
0108 
0109         // command?
0110         if (m_Output.length() <= 4) {
0111             if (m_Output == "EXEC") {
0112                 return Tok_exec;
0113             }
0114             if (m_Output == "PASS") {
0115                 return Tok_pass;
0116             }
0117             if (m_Output == "DEL") {
0118                 return Tok_delCmd;
0119             }
0120             if (m_Output == "PING") {
0121                 return Tok_ping;
0122             }
0123             if (m_Output == "EXIT") {
0124                 return Tok_exit;
0125             }
0126             if (m_Output == "STOP") {
0127                 return Tok_stop;
0128             }
0129             if (m_Output == "SET") {
0130                 return Tok_set;
0131             }
0132             if (m_Output == "GET") {
0133                 return Tok_get;
0134             }
0135             if (m_Output == "HOST") {
0136                 return Tok_host;
0137             }
0138             if (m_Output == "SCHD") {
0139                 return Tok_sched;
0140             }
0141             if (m_Output == "PRIO") {
0142                 return Tok_prio;
0143             }
0144             if (m_Output == "DELV") {
0145                 return Tok_delVar;
0146             }
0147             if (m_Output == "DELG") {
0148                 return Tok_delGroup;
0149             }
0150             if (m_Output == "DELS") {
0151                 return Tok_delSpecialKey;
0152             }
0153             if (m_Output == "GETK") {
0154                 return Tok_getKeys;
0155             }
0156             if (m_Output == "CHKG") {
0157                 return Tok_chkGroup;
0158             }
0159         }
0160 
0161         return Tok_str;
0162     }
0163 }