ao-tools: Fix warnings in ao-tools
[fw/altos] / ao-tools / ao-dbg / ao-dbg-parse.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao-dbg.h"
20
21 static struct command_function functions[] = {
22         { "help",   "?",  command_help, "help",         "Print this list\n" },
23         { "quit",   "q",  command_quit, "[q]uit",       "Quit\n" },
24         { "di",     "di", command_di,   "di <start> <end>",
25                 "Dump imem\n" },
26         { "ds",     "ds", command_ds,   "ds <start> <end>",
27                 "Dump sprs\n" },
28         { "dx",     "dx", command_dx,   "dx <start> <end>",
29                 "Dump xaddr\n" },
30         { "set",    "t",  command_set,  "se[t] mem <prefix> <address> <data> ...",
31                 "Set mem {xram|rom|iram|sfr}\n"
32                 "set bit <addr>\n" },
33         { "dump",   "d",  command_dump, "[d]ump <prefix> <start> <end>",
34                 "Dump {xram|rom|iram|sfr} <start> <end>\n" },
35         { "file", "file", command_file, "file <filename>",
36                 "Pretend to load executable from <filename>\n" },
37         { "pc",     "p",  command_pc, "[p]c [addr]",
38                 "Get or set pc value\n" },
39         { "break",  "b",  command_break,"[b]reak <addr>",
40                 "Set break point\n" },
41         { "clear",  "c",  command_clear,"[c]lear <addr>",
42                 "Clear break point\n" },
43         { "run",    "r",  command_run, "[r]un [start] [stop]",
44                 "Run with optional start and temp breakpoint addresses\n" },
45         { "go",     "g",  command_run, "[g]o [start] [stop]",
46                 "Run with optional start and temp breakpoint addresses\n" },
47         { "next",   "n",  command_next, "[n]ext",
48                 "Step over one instruction, past any call\n" },
49         { "step",   "s",  command_step, "[s]tep",
50                 "Single step\n" },
51         { "load",   "l",  command_load, "[l]oad <file>",
52                 "Load a hex file into memory or flash" },
53         { "halt",   "h",  command_halt, "[h]alt",
54                 "Halt the processor\n" },
55         { "reset","res",command_reset,  "[res]et",
56                 "Reset the CPU\n" },
57         { "status","status",command_status, "status",
58                 "Display CC1111 debug status\n" },
59         { "info",   "i",  command_info, "[i]info",
60                 "Get information\n" },
61         { "stop",  "stop", command_stop, "stop",
62                 "Ignored\n" },
63         { NULL, NULL, NULL, NULL, NULL },
64 };
65
66 #ifndef FALSE
67 #define FALSE 0
68 #define TRUE 1
69 #endif
70
71 struct command_function *
72 command_string_to_function(struct command_function *functions, char *name)
73 {
74         int i;
75         for (i = 0; functions[i].name; i++)
76                 if (!strcmp(name, functions[i].name) ||
77                     !strcmp(name, functions[i].alias))
78                         return &functions[i];
79         return NULL;
80 }
81
82 enum command_result
83 command_function_help(struct command_function *functions, int argc, char **argv)
84 {
85         int i;
86         struct command_function *func;
87
88         if (argc == 1) {
89                 for (i = 0; functions[i].name; i++)
90                         s51_printf("%-10s%s\n", functions[i].name,
91                                functions[i].usage);
92         } else {
93                 for (i = 1; i < argc; i++) {
94                         func = command_string_to_function(functions, argv[i]);
95                         if (!func) {
96                                 s51_printf("%-10s unknown command\n", argv[i]);
97                                 return command_syntax;
98                         }
99                         s51_printf("%-10s %s\n%s", func->name,
100                                func->usage, func->help);
101                 }
102         }
103         return command_debug;
104 }
105
106 static int
107 command_split_into_words(char *line, char **argv)
108 {
109         char quotechar;
110         int argc;
111
112         argc = 0;
113         while (*line) {
114                 while (isspace(*line))
115                         line++;
116                 if (!*line)
117                         break;
118                 if (*line == '"') {
119                         quotechar = *line++;
120                         *argv++ = line;
121                         argc++;
122                         while (*line && *line != quotechar)
123                                 line++;
124                         if (*line)
125                                 *line++ = '\0';
126                 } else {
127                         *argv++ = line;
128                         argc++;
129                         while (*line && !isspace(*line))
130                                 line++;
131                         if (*line)
132                                 *line++ = '\0';
133                 }
134         }
135         *argv = 0;
136         return argc;
137 }
138
139 enum command_result
140 command_help(int argc, char **argv)
141 {
142         return command_function_help(functions, argc, argv);
143 }
144
145 void
146 command_syntax_error(int argc, char **argv)
147 {
148         s51_printf("Syntax error in:");
149         while (*argv)
150                 s51_printf(" %s", *argv++);
151         s51_printf("\n");
152 }
153
154 void
155 command_read (void)
156 {
157         int argc;
158         char line[1024];
159         char *argv[20];
160         enum command_result result;
161         struct command_function *func;
162
163         if (!s51_tty) {
164                 if (!s51_device)
165                         s51_device = getenv("AO_DBG_DEVICE");
166                 s51_tty = cc_usbdevs_find_by_arg(s51_device, "TeleDongle");
167         }
168         s51_dbg = ccdbg_open (s51_tty);
169         if (!s51_dbg)
170                 exit(1);
171         ccdbg_debug_mode(s51_dbg);
172         ccdbg_halt(s51_dbg);
173         s51_printf("Welcome to the non-simulated processor\n");
174         for (;;) {
175                 if (s51_read_line (line, sizeof line) == 0)
176                         break;
177                 s51_interrupted = 0;
178                 argc = command_split_into_words(line, argv);
179                 if (argc > 0) {
180                         func = command_string_to_function(functions, argv[0]);
181                         if (!func)
182                                 command_syntax_error(argc, argv);
183                         else
184                         {
185                                 result = (*func->func)(argc, argv);
186                                 if (s51_interrupted)
187                                         result = command_interrupt;
188                                 switch (result) {
189                                 case command_syntax:
190                                         command_syntax_error(argc, argv);
191                                         break;
192                                 case command_error:
193                                         s51_printf("Error\n");
194                                         break;
195                                 case command_success:
196                                         break;
197                                 case command_interrupt:
198                                         ccdbg_halt(s51_dbg);
199                                         s51_printf("Interrupted\n");
200                                         break;
201                                 default:
202                                         break;
203                                 }
204                         }
205                 }
206         }
207         ccdbg_close(s51_dbg);
208         s51_printf("...\n");
209 }