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