Debian Codegraph @

libdebugger/command/source.c

raw symbols SCIP
1 /*
2 Copyright (C) 2011, 2020 R. Bernstein <[email protected]>
3 This file is part of GNU Make (remake variant).
4
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 /**
21 * \brief Debugger `source` command.
22 *
23 * Read and run debugger commands from a file.
24 **/
25
26 #include "../../src/trace.h"
27 #include "../cmd.h"
28 #include "../fns.h"
29 #include "../msg.h"
30
31 #define DEPENDS_COMMANDS " depends commands"
32
33 #include <glob.h>
34 #include "help/source.h"
35
36 extern debug_return_t
37 dbg_cmd_source(char *psz_filename)
38 {
39 if (psz_filename && *psz_filename) {
40 FILE *p_source_file;
41 char *psz_expanded_file;
42 glob_t p;
43
44 glob(psz_filename, 0, NULL, &p);
45 if (0 == p.gl_pathc) {
46 struct stat stat_buf;
47 int ret = stat(psz_filename, &stat_buf);
48 if (ret != 0) {
49 dbg_errmsg("Can't find file %s:\n\t%s", psz_filename, strerror(errno));
50 return debug_cmd_error;
51 }
52 psz_expanded_file = psz_filename;
53 } else if (1 != p.gl_pathc) {
54 dbg_errmsg("Expansion of %s doesn't lead to a single filename. \n"
55 "Got %zu matches",
56 psz_filename, (size_t)p.gl_pathv);
57 return debug_cmd_error;
58
59 } else {
60 psz_expanded_file = p.gl_pathv[0];
61 }
62
63 p_source_file = fopen(psz_expanded_file, "r");
64
65 if (p_source_file != NULL) {
66 debug_return_t debug_return = debug_readloop;
67 char line[2048];
68 while (!feof(p_source_file) && debug_return == debug_readloop)
69 {
70 char *s;
71 char *shut_up_gcc_warning = fgets(line, 2048, p_source_file);
72 if (feof(p_source_file)) break;
73 chomp(shut_up_gcc_warning);
74 if ( *(s=stripwhite(line)) ) debug_return=execute_line(s);
75 }
76 fclose(p_source_file);
77 } else
78 dbg_errmsg("error reading file %s (expanded to %s):\n\t%s",
79 psz_filename, psz_expanded_file, strerror(errno));
80
81 globfree(&p);
82 } else {
83 dbg_errmsg("Expecting a file name");
84 return debug_cmd_error;
85 }
86 return debug_readloop;
87 }
88
89
90 /*
91 * Local variables:
92 * eval: (c-set-style "gnu")
93 * indent-tabs-mode: nil
94 * End:
95 */