1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <ctype.h> |
---|
5 | |
---|
6 | #include "cmt_script.h" |
---|
7 | #include "cmt_use.h" |
---|
8 | |
---|
9 | /*----------------------------------------------------------*/ |
---|
10 | /* */ |
---|
11 | /* Operations on Scripts */ |
---|
12 | /* */ |
---|
13 | /*----------------------------------------------------------*/ |
---|
14 | |
---|
15 | /*----------------------------------------------------------*/ |
---|
16 | void Script::action (const CmtSystem::cmt_string_vector& words, |
---|
17 | ScriptType type, |
---|
18 | Use* use) |
---|
19 | { |
---|
20 | cmt_string name; |
---|
21 | Script* script; |
---|
22 | |
---|
23 | if (use == 0) use = &(Use::current()); |
---|
24 | |
---|
25 | name = words[1]; |
---|
26 | if (name == "") return; |
---|
27 | |
---|
28 | script = add (name, type, use); |
---|
29 | } |
---|
30 | |
---|
31 | /*----------------------------------------------------------*/ |
---|
32 | Script* Script::find (const cmt_string& name, Use* use) |
---|
33 | { |
---|
34 | int script_index; |
---|
35 | |
---|
36 | if (use == 0) use = &(Use::current()); |
---|
37 | |
---|
38 | if (use->scripts.size () == 0) return (0); |
---|
39 | |
---|
40 | for (script_index = 0; |
---|
41 | script_index < use->scripts.size (); |
---|
42 | script_index++) |
---|
43 | { |
---|
44 | Script& script = use->scripts[script_index]; |
---|
45 | |
---|
46 | if (script.name == name) |
---|
47 | { |
---|
48 | return (&script); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | return (0); |
---|
53 | } |
---|
54 | |
---|
55 | /*----------------------------------------------------------*/ |
---|
56 | Script* Script::add (const cmt_string& name, ScriptType type, Use* use) |
---|
57 | { |
---|
58 | cmt_string short_name = name; |
---|
59 | |
---|
60 | if (use == 0) use = &(Use::current()); |
---|
61 | |
---|
62 | int pos; |
---|
63 | |
---|
64 | pos = short_name.find (".csh"); |
---|
65 | if (pos != cmt_string::npos) |
---|
66 | { |
---|
67 | short_name.erase (pos); |
---|
68 | } |
---|
69 | |
---|
70 | pos = short_name.find (".sh"); |
---|
71 | if (pos != cmt_string::npos) |
---|
72 | { |
---|
73 | short_name.erase (pos); |
---|
74 | } |
---|
75 | |
---|
76 | { |
---|
77 | Script* script; |
---|
78 | |
---|
79 | if (short_name == "") return (0); |
---|
80 | script = find (short_name, use); |
---|
81 | if (script != 0) |
---|
82 | { |
---|
83 | script->type = type; |
---|
84 | return (script); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | Script& script = use->scripts.add (); |
---|
89 | |
---|
90 | script.name = short_name; |
---|
91 | script.type = type; |
---|
92 | |
---|
93 | return (&script); |
---|
94 | } |
---|
95 | |
---|
96 | /*----------------------------------------------------------*/ |
---|
97 | void Script::all_print_clean (PrintMode mode) |
---|
98 | { |
---|
99 | int number; |
---|
100 | int script_number; |
---|
101 | Use::UsePtrVector& uses = Use::get_ordered_uses (); |
---|
102 | |
---|
103 | if (uses.size () > 0) |
---|
104 | { |
---|
105 | for (number = 0; number < uses.size (); number++) |
---|
106 | { |
---|
107 | const Use* use = uses[number]; |
---|
108 | |
---|
109 | Package* p = use->get_package (); |
---|
110 | if (p->is_cmt ()) continue; |
---|
111 | |
---|
112 | for (script_number = 0; |
---|
113 | script_number < use->scripts.size (); |
---|
114 | script_number++) |
---|
115 | { |
---|
116 | Script& script = use->scripts[script_number]; |
---|
117 | |
---|
118 | if (script.type == CleanupScript) |
---|
119 | { |
---|
120 | if (script.print (mode, use)) cout << "; "; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /*----------------------------------------------------------*/ |
---|
128 | void Script::all_print (PrintMode mode) |
---|
129 | { |
---|
130 | int number; |
---|
131 | int script_number; |
---|
132 | Use::UsePtrVector& uses = Use::get_ordered_uses (); |
---|
133 | |
---|
134 | if (uses.size () > 0) |
---|
135 | { |
---|
136 | for (number = 0; number < uses.size (); number++) |
---|
137 | { |
---|
138 | const Use* use = uses[number]; |
---|
139 | |
---|
140 | Package* p = use->get_package (); |
---|
141 | if (p->is_cmt ()) continue; |
---|
142 | |
---|
143 | for (script_number = 0; |
---|
144 | script_number < use->scripts.size (); |
---|
145 | script_number++) |
---|
146 | { |
---|
147 | Script& script = use->scripts[script_number]; |
---|
148 | |
---|
149 | if (script.type == SetupScript) |
---|
150 | { |
---|
151 | if (script.print (mode, use)) cout << "; "; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | { |
---|
158 | const Use& use = Use::current (); |
---|
159 | |
---|
160 | for (script_number = 0; |
---|
161 | script_number < use.scripts.size (); |
---|
162 | script_number++) |
---|
163 | { |
---|
164 | Script& script = use.scripts[script_number]; |
---|
165 | |
---|
166 | if (script.type == SetupScript) |
---|
167 | { |
---|
168 | if (script.print (mode, &use)) cout << "; "; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /*----------------------------------------------------------*/ |
---|
175 | void Script::clear_all () |
---|
176 | { |
---|
177 | } |
---|
178 | |
---|
179 | /*----------------------------------------------------------*/ |
---|
180 | Script::Script () |
---|
181 | { |
---|
182 | } |
---|
183 | |
---|
184 | /*----------------------------------------------------------*/ |
---|
185 | Script::~Script () |
---|
186 | { |
---|
187 | } |
---|
188 | |
---|
189 | /*----------------------------------------------------------*/ |
---|
190 | int Script::print (PrintMode mode, const Use* use) |
---|
191 | { |
---|
192 | int result = 1; |
---|
193 | cmt_string suffix; |
---|
194 | |
---|
195 | if (name == "") return (0); |
---|
196 | |
---|
197 | switch (mode) |
---|
198 | { |
---|
199 | case Csh : |
---|
200 | suffix = ".csh"; |
---|
201 | cout << "source "; |
---|
202 | break; |
---|
203 | case Sh : |
---|
204 | suffix = ".sh"; |
---|
205 | cout << ". "; |
---|
206 | break; |
---|
207 | } |
---|
208 | |
---|
209 | if (name.find (SLASH) == cmt_string::npos) |
---|
210 | { |
---|
211 | cout << "${" << use->prefix << "ROOT}/"; |
---|
212 | if (use->style == cmt_style) cout << "cmt/"; |
---|
213 | else cout << "mgr/"; |
---|
214 | } |
---|
215 | |
---|
216 | cout << name << suffix; |
---|
217 | |
---|
218 | return (result); |
---|
219 | } |
---|