4 * Copyright (C) 2002, 2003 ETC s.r.o.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * Written by Marcel Telka <marcel@telka.sk>, 2002, 2003.
22 * Modified by Ajith Kumar P.C <ajithpc@kila.com>, 20/09/2006.
23 * Modified by Ville Voipio <vv@iki.fi>, 7-May-2008
35 #include <urjtag/log.h>
36 #include <urjtag/error.h>
37 #include <urjtag/chain.h>
38 #include <urjtag/parse.h>
39 #include <urjtag/cmd.h>
40 #include <urjtag/jtag.h>
41 #include <urjtag/bsdl.h>
43 #define MAXINPUTLINE 300 /* Maximum input line length */
48 urj_parse_line (urj_chain_t *chain, char *line)
57 urj_error_set (URJ_ERROR_INVALID, "NULL line");
58 return URJ_STATUS_FAIL;
64 /* allocate as many chars as in the input line; this will be enough in all cases */
65 sline = malloc (l + 1);
68 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails",
70 return URJ_STATUS_FAIL;
73 /* count and copy the tokens */
79 /* eat up leading spaces */
83 /* if the command ends here (either by NUL or comment) */
84 if (*c == '\0' || *c == '#')
87 /* copy the meat (non-space, non-NUL) */
88 while (!isspace (*c) && *c != '\0')
92 /* mark the end to the destination string */
100 return URJ_STATUS_OK;
103 /* allocate the token pointer table */
104 a = malloc ((tcnt + 1) * sizeof (char *));
107 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails",
108 (size_t) ((tcnt + 1) * sizeof (char *)));
109 return URJ_STATUS_FAIL;
112 /* find the starting points for the tokens */
114 for (i = 0; i < tcnt; i++)
122 r = urj_cmd_run (chain, a);
123 urj_log (URJ_LOG_LEVEL_DEBUG, "Return in urj_parse_line r=%d\n", r);
132 urj_parse_stream (urj_log_level_t ll, urj_chain_t *chain, FILE *f)
134 char inputline[MAXINPUTLINE + 1];
135 int go = 1, i, c, lnr, clip, found_comment;
137 /* read the stream line-by-line until EOF or "quit" */
145 /* read stream until '\n' or EOF, copy at most MAXINPUTLINE-1 chars */
149 if (c == EOF || c == '\n')
153 if (i < sizeof (inputline) - 1)
160 if (clip && !found_comment)
161 urj_warning ("line %d exceeds %zd characters, clipped\n", lnr,
162 sizeof (inputline) - 1);
163 go = urj_parse_line (chain, inputline);
164 if (go == URJ_STATUS_FAIL)
166 urj_log (ll, "Error: %s; command '%s'\n", urj_error_describe(), inputline);
169 urj_tap_chain_flush (chain);
171 while (go != URJ_STATUS_MUST_QUIT && c != EOF);
177 urj_parse_file (urj_log_level_t ll, urj_chain_t *chain, const char *filename)
182 f = fopen (filename, "r");
185 urj_error_IO_set ("Cannot open file '%s' to parse", filename);
186 return URJ_STATUS_FAIL;
189 go = urj_parse_stream (ll, chain, f);
192 urj_log (URJ_LOG_LEVEL_DEBUG, "File Closed go=%d\n", go);
198 urj_parse_include (urj_chain_t *chain, const char *filename, int ignore_path)
201 int r = URJ_STATUS_OK;
205 /* If "filename" begins with a slash, or dots followed by a slash,
206 * assume that user wants to ignore the search path after all */
207 const char *slashdots = filename;
210 if (isalpha (*slashdots) && slashdots[1] == ':')
213 while (*slashdots == '.')
215 ignore_path = (*slashdots == '/' || *slashdots == '\\');
220 const char *jtag_data_dir = urj_get_data_dir ();
223 path = malloc (len = strlen (jtag_data_dir) + strlen (filename) + 2);
226 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", len);
227 return URJ_STATUS_FAIL;
229 snprintf (path, len, "%s/%s", jtag_data_dir, filename);
235 /* perform a test read to check for BSDL syntax */
236 if (urj_bsdl_read_file (chain, filename, URJ_BSDL_MODE_INCLUDE1, NULL) >= 0)
238 // @@@@ RFHH ToDo: let urj_bsdl_read_file also return URJ_STATUS_...
239 /* it seems to be a proper BSDL file, so re-read and execute */
240 if (urj_bsdl_read_file (chain, filename, URJ_BSDL_MODE_INCLUDE2,
248 r = urj_parse_file (URJ_LOG_LEVEL_NORMAL, chain, filename);