Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 609ccec7871768842f506a63d115dc25d763327f (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.cmdline;

import org.eclipse.tcf.util.TCFTask;

public class TCFCommandLine {

    private static final class CommandInfo {
        final String name;
        final Class<?> cls;

        CommandInfo(String name, Class<?> cls) {
            this.name = name;
            this.cls = cls;
        }
    }

    private static final CommandInfo[] command_list = {
        new CommandInfo("step", CommandStep.class),
    };

    private class CommandStep extends TCFTask<String> {

        public void run() {
            done("OK");
        }
    }

    public String command(String cmd) {
        try {
            int i = 0;
            int l = cmd.length();
            StringBuffer bf = new StringBuffer();
            while (i < l) {
                char ch = cmd.charAt(i);
                if (ch == ' ' || ch == '#') break;
                bf.append(ch);
                i++;
            }
            if (bf.length() > 0) {
                CommandInfo cmd_info = null;
                String name = bf.toString();
                for (CommandInfo c : command_list) {
                    if (c.name.startsWith(name)) {
                        if (cmd_info != null) return "Ambiguous command";
                        cmd_info = c;
                    }
                }
                if (cmd_info == null) return "Unknown command";
                @SuppressWarnings("unchecked")
                TCFTask<String> task = (TCFTask<String>)cmd_info.cls.getConstructors()[0].newInstance(this);
                return task.get();
            }
        }
        catch (Throwable x) {
            return x.getMessage();
        }
        return null;
    }
}

Back to the top