Debian Codegraph @

libdebugger/command/write.c

raw symbols SCIP
1 /*
2 Copyright (C) 2011, 2017, 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 #include "../../src/trace.h"
21 #include "../../src/commands.h"
22 #include "../../src/dep.h"
23 #include "../../src/variable.h"
24
25 #include "../cmd.h"
26 #include "../fns.h"
27
28 /**
29 * \brief Debugger `write` command.
30 *
31 * Write commands associated with a given target.
32 **/
33
34 #include "../../src/trace.h"
35
36 extern debug_return_t
37 dbg_cmd_write(char *psz_args)
38 {
39 file_t *p_target = NULL;
40 const char *psz_target = NULL;
41 int b_stdout = 0;
42
43 p_target = get_target(&psz_args, &psz_target);
44 if (p_target) {
45 variable_t *p_v = lookup_variable ("SHELL", strlen ("SHELL"));
46 char *psz_filename = NULL;
47 FILE *outfd;
48 char *s;
49
50 if (! p_target->cmds || ! p_target->cmds->commands) {
51 printf(_("Target \"%s\" doesn't have commands associated with it.\n"),
52 psz_target);
53 return debug_readloop;
54 }
55
56 s = p_target->cmds->commands;
57
58 /* FIXME: should get directory from a variable, e.g. TMPDIR */
59
60 if (psz_args && *psz_args) {
61 if (strcmp (psz_args, "here") == 0)
62 b_stdout = 1;
63 else
64 psz_filename = strdup(psz_args);
65 } else {
66 /* Create target from the basename of the target name. */
67 char *psz_target_basename = strrchr(psz_target, '/');
68 if (!psz_target_basename)
69 psz_target_basename = (char *) psz_target;
70 else
71 psz_target_basename++; /* Skip delimiter */
72 psz_filename = CALLOC(char, strlen(psz_target_basename) + 10);
73 snprintf(psz_filename, MAX_FILE_LENGTH, "/tmp/%s.sh",
74 psz_target_basename);
75 }
76
77 /* Skip leading space, MAKE's command prefixes:
78 echo suppression @,
79 ignore-error -,
80 and recursion +
81 */
82 while (*s != '\0')
83 {
84 switch (*s) {
85 case '@':
86 case '+':
87 case '-':
88 ++s;
89 break;
90 default:
91 if (!isblank ((unsigned char)*s))
92 goto found_begin;
93 ++s;
94 }
95 }
96
97 found_begin:
98 if ( '\0' == *s ) {
99 printf(_("Null command string parsed\n"));
100 } else {
101 dep_t *d;
102 dep_t *ood = 0;
103
104 if (b_stdout)
105 outfd = stdout;
106 else if (!(outfd = fopen (psz_filename, "w"))) {
107 perror ("write target");
108 free(psz_filename);
109 return debug_readloop;
110 }
111
112 if (p_v) {
113 fprintf(outfd, "#!%s\n", variable_expand(p_v->value));
114 }
115
116 if (!p_target->floc.filenm && p_target->cmds->fileinfo.filenm) {
117 /* Fake the location based on the commands - it's better than
118 nothing...
119 */
120 fprintf(outfd, "## %s/%s:%lu\n", starting_directory,
121 p_target->cmds->fileinfo.filenm,
122 p_target->cmds->fileinfo.lineno-1);
123 } else {
124 fprintf(outfd, "## %s/%s:%lu\n", starting_directory,
125 p_target->floc.filenm, p_target->floc.lineno);
126 }
127
128 /* FIXME: this code duplicates some code in file.c:
129 print_target_props. DRY. */
130 fprintf (outfd,
131 "## %s:%s", p_target->name,
132 p_target->double_colon ? ":" : "");
133
134 /* Print all normal dependencies; note any order-only deps. */
135 for (d = p_target->deps; d != 0; d = d->next)
136 if (! d->ignore_mtime) {
137 fprintf (outfd, " %s", dep_name (d));
138 } else if (! ood)
139 ood = d;
140
141 /* Print order-only deps, if we have any. */
142 if (ood)
143 {
144 fprintf (outfd, " | %s", dep_name (ood));
145 for (d = ood->next; d != 0; d = d->next)
146 if (d->ignore_mtime)
147 fprintf (outfd, " %s", dep_name (d));
148 }
149
150 fprintf (outfd, "\n");
151
152 {
153 char wd[300];
154 if (getcwd (wd, sizeof(wd))) {
155 fprintf(outfd, "\n#cd %s\n", wd);
156 }
157 }
158
159 initialize_file_variables (p_target, 0);
160 set_file_variables (p_target);
161
162 {
163 #if 0
164 commands_t cmds;
165 char **lines;
166 unsigned int i;
167 memcpy(&cmds, p_target->cmds, sizeof(cmds));
168 cmds.command_lines = NULL;
169 chop_commands (&cmds);
170 lines = xmalloc (cmds.ncommand_lines * sizeof (char *));
171 expand_command_lines(&cmds, lines, p_target);
172 for (i = 0; i < cmds.ncommand_lines; ++i)
173 {
174 fprintf (outfd, "%s\n", lines[i]);
175 free (lines[i]);
176 }
177 free (lines);
178 #else
179 char *line = allocated_variable_expand_for_file (s, p_target);
180 fprintf (outfd, "%s\n", line);
181 free(line);
182 #endif
183 }
184
185 if (!b_stdout) {
186 struct stat buf;
187 if (0 == fstat(fileno(outfd), &buf)) {
188 mode_t mode = buf.st_mode;
189 if (buf.st_mode & S_IRUSR) mode |= S_IXUSR;
190 if (buf.st_mode & S_IRGRP) mode |= S_IXGRP;
191 if (buf.st_mode & S_IROTH) mode |= S_IXOTH;
192 if (0 != fchmod(fileno(outfd), mode)) {
193 printf(_("Can't set execute mode on \"%s\".\n"), psz_filename);
194 }
195 }
196 fclose(outfd);
197 printf(_("File \"%s\" written.\n"), psz_filename);
198 free(psz_filename);
199 }
200 }
201 }
202 return debug_readloop;
203 }
204
205
206 /*
207 * Local variables:
208 * eval: (c-set-style "gnu")
209 * indent-tabs-mode: nil
210 * End:
211 */