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