1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
3 *
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6
7 /* This file is ALSO:
8 * Copyright 2001-2004 David Abrahams.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
11 */
12
13 /*
14 * command.c - maintain lists of commands
15 */
16
17 #include "jam.h"
18 #include "command.h"
19
20 #include "lists.h"
21 #include "rules.h"
22
23 #include <assert.h>
24
25
26 /*
27 * cmdlist_append_cmd
28 */
cmdlist_append_cmd(CMDLIST * l,CMD * cmd)29 CMDLIST * cmdlist_append_cmd( CMDLIST * l, CMD * cmd )
30 {
31 CMDLIST * result = (CMDLIST *)BJAM_MALLOC( sizeof( CMDLIST ) );
32 result->iscmd = 1;
33 result->next = l;
34 result->impl.cmd = cmd;
35 return result;
36 }
37
cmdlist_append_target(CMDLIST * l,TARGET * t)38 CMDLIST * cmdlist_append_target( CMDLIST * l, TARGET * t )
39 {
40 CMDLIST * result = (CMDLIST *)BJAM_MALLOC( sizeof( CMDLIST ) );
41 result->iscmd = 0;
42 result->next = l;
43 result->impl.t = t;
44 return result;
45 }
46
cmdlist_free(CMDLIST * l)47 void cmdlist_free( CMDLIST * l )
48 {
49 while ( l )
50 {
51 CMDLIST * tmp = l->next;
52 BJAM_FREE( l );
53 l = tmp;
54 }
55 }
56
57 /*
58 * cmd_new() - return a new CMD.
59 */
60
cmd_new(RULE * rule,LIST * targets,LIST * sources,LIST * shell)61 CMD * cmd_new( RULE * rule, LIST * targets, LIST * sources, LIST * shell )
62 {
63 CMD * cmd = (CMD *)BJAM_MALLOC( sizeof( CMD ) );
64 FRAME frame[ 1 ];
65
66 assert( cmd );
67 cmd->rule = rule;
68 cmd->shell = shell;
69 cmd->next = 0;
70 cmd->noop = 0;
71 cmd->asynccnt = 1;
72 cmd->status = 0;
73 cmd->lock = NULL;
74 cmd->unlock = NULL;
75
76 lol_init( &cmd->args );
77 lol_add( &cmd->args, targets );
78 lol_add( &cmd->args, sources );
79 string_new( cmd->buf );
80
81 frame_init( frame );
82 frame->module = rule->module;
83 lol_init( frame->args );
84 lol_add( frame->args, list_copy( targets ) );
85 lol_add( frame->args, list_copy( sources ) );
86 function_run_actions( rule->actions->command, frame, stack_global(),
87 cmd->buf );
88 frame_free( frame );
89
90 return cmd;
91 }
92
93
94 /*
95 * cmd_free() - free a CMD
96 */
97
cmd_free(CMD * cmd)98 void cmd_free( CMD * cmd )
99 {
100 cmdlist_free( cmd->next );
101 lol_free( &cmd->args );
102 list_free( cmd->shell );
103 string_free( cmd->buf );
104 freetargets( cmd->unlock );
105 BJAM_FREE( (void *)cmd );
106 }
107
108
109 /*
110 * cmd_release_targets_and_shell()
111 *
112 * Makes the CMD release its hold on its targets & shell lists and forget
113 * about them. Useful in case caller still has references to those lists and
114 * wants to reuse them after freeing the CMD object.
115 */
116
cmd_release_targets_and_shell(CMD * cmd)117 void cmd_release_targets_and_shell( CMD * cmd )
118 {
119 cmd->args.list[ 0 ] = L0; /* targets */
120 cmd->shell = L0; /* shell */
121 }
122