| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 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 |
|
| 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++; |
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 118 |
|
| 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 |
|
| 129 |
|
| 130 |
fprintf (outfd, |
| 131 |
"## %s:%s", p_target->name, |
| 132 |
p_target->double_colon ? ":" : ""); |
| 133 |
|
| 134 |
|
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|