| 1 | /* |
| 2 | * File: ConfigFile.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "ConfigFile.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "ConfigurationEditor.h" |
| 28 | #include "SystemConfigLine.h" |
| 29 | #include "DeviceConfigLine.h" |
| 30 | |
| 31 | #include <QTextBlock> |
| 32 | #include <cstdio> |
| 33 | #include <iostream> |
| 34 | #include <sstream> |
| 35 | #include <iomanip> |
| 36 | |
| 37 | using namespace std; |
| 38 | |
| 39 | #define synonymEntry(keyword1, keyword2) {#keyword1, #keyword2}, {#keyword2, #keyword1}, |
| 40 | |
| 41 | struct SynonymTableEntry ConfigFile::mSynonyms[] = { |
| 42 | synonymEntry(ASN_AND_LX_REUSE,ALRF) |
| 43 | {"", ""} |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | ConfigFile::ConfigFile(const string& fileName) : |
| 48 | mEditor(NULL), mLastSys(-1), mChanged(false), mNewConfig(false) |
| 49 | { |
| 50 | mFileName = fileName; |
| 51 | initialize(); |
| 52 | } |
| 53 | |
| 54 | void ConfigFile::initialize() |
| 55 | { |
| 56 | mFile = fopen(mFileName.c_str(), "r"); |
| 57 | if (mFile==NULL) |
| 58 | { |
| 59 | hOutDebug(4, "file not found:" << mFileName << std::endl); |
| 60 | openTemplate(); |
| 61 | } |
| 62 | buildConfig(ConfigFile::File); |
| 63 | if (mFile != NULL) |
| 64 | { |
| 65 | fclose(mFile); |
| 66 | mFile = NULL; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ConfigFile::updateEditor(QPlainTextEdit& editor) |
| 71 | { |
| 72 | mEditor = &editor; |
| 73 | mEditorIterator = mEditor->document()->begin(); |
| 74 | buildConfig(ConfigFile::TextEdit); |
| 75 | // TODO: build yourself by doing a merge: |
| 76 | //1. Search the line in mFileArray |
| 77 | //2. if not found - add. |
| 78 | //3. compare if the same text - if not - flag with update. |
| 79 | char buff[MAX_CONFIG_LINE]; |
| 80 | while ( getNextLine(buff, MAX_CONFIG_LINE, ConfigFile::TextEdit)) |
| 81 | { |
| 82 | std::string s(buff); |
| 83 | for (int i=0; i<size(); i++) |
| 84 | { |
| 85 | ConfigLine& line = mFileArray.at(i); |
| 86 | if (line.isDeleted()) continue; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | void ConfigFile::buildConfig(BuildType type) |
| 93 | { |
| 94 | mLastSys = -1; |
| 95 | mFileArray.clear(); |
| 96 | |
| 97 | char buff[MAX_CONFIG_LINE]; |
| 98 | bool devices = false; |
| 99 | while ( getNextLine(buff, MAX_CONFIG_LINE, type) != NULL ) |
| 100 | { |
| 101 | std::string s(buff); |
| 102 | hOutDebug(5,"LINE:" << buff); |
| 103 | size_t nb = s.find_first_not_of(" "); |
| 104 | if ((s.length() == 0) || (nb != std::string::npos && |
| 105 | (s.substr(nb,1).find_first_not_of("0123456789") != std::string::npos)) ) // not device |
| 106 | { |
| 107 | hOutDebug(4, mFileArray.size() << " " << buff << std::endl); |
| 108 | SystemConfigLine l(s.c_str()); |
| 109 | mFileArray.push_back(l); |
| 110 | |
| 111 | if (!l.isRemark() && !devices) |
| 112 | mLastSys = mFileArray.size()-1; |
| 113 | } |
| 114 | else // device |
| 115 | { |
| 116 | devices=true; |
| 117 | DeviceConfigLine l(s.c_str()); |
| 118 | int devno = ConfigurationEditor::parseNum(l.getToken(0),16); |
| 119 | std::stringstream newLine; |
| 120 | newLine << std::hex << std::setw(4) << std::setfill('0') << std::uppercase |
| 121 | << devno << " " << l.getMultiToken(1,0) << "\n"; |
| 122 | DeviceConfigLine n(newLine.str().c_str()); |
| 123 | mFileArray.push_back(n); |
| 124 | if (l.getMultiCount() > 1) |
| 125 | { |
| 126 | int additions = l.getMultiCount(); |
| 127 | while (additions>1) |
| 128 | { |
| 129 | devno++; |
| 130 | newLine.str(""); |
| 131 | newLine << std::hex << std::setw(4) << std::setfill('0') << std::uppercase |
| 132 | << devno << " " << l.getMultiToken(1,0) << "\n"; |
| 133 | DeviceConfigLine n(newLine.str().c_str()); |
| 134 | mFileArray.push_back(n); |
| 135 | additions--; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | hOutDebug(3, "### last sys=" << mLastSys << " size=" << mFileArray.size() << std::endl); |
| 141 | hOutDebug(3, mFileArray[mLastSys].getLine() << std::endl) |
| 142 | |
| 143 | } |
| 144 | |
| 145 | ConfigFile::~ConfigFile() |
| 146 | { |
| 147 | mFileArray.clear(); |
| 148 | } |
| 149 | |
| 150 | void ConfigFile::addNonDev(SystemConfigLine * configLine) |
| 151 | { |
| 152 | FileArray::iterator it = mFileArray.begin() + mLastSys+1; |
| 153 | mFileArray.insert(it, *configLine); |
| 154 | ++mLastSys; |
| 155 | mChanged = true; |
| 156 | } |
| 157 | |
| 158 | void ConfigFile::addDev(DeviceConfigLine * configLine) |
| 159 | { |
| 160 | hOutDebug(2, "add dev after " << mFileArray.size() << " line:'" << configLine->getLine() << std::endl); |
| 161 | configLine->parseLine(); |
| 162 | mFileArray.push_back(*configLine); |
| 163 | mChanged = true; |
| 164 | } |
| 165 | |
| 166 | const std::string& ConfigFile::getFileName() const |
| 167 | { |
| 168 | return mFileName; |
| 169 | } |
| 170 | |
| 171 | DeviceConfigLine * ConfigFile::getDevice(int index) const |
| 172 | { |
| 173 | if (index <= mLastSys) |
| 174 | { |
| 175 | hOutDebug(0, "ConfigFile[" << index << "] requested where lastsys=" << mLastSys); |
| 176 | abort(); |
| 177 | } |
| 178 | if (static_cast<unsigned int>(index) >= mFileArray.size()) |
| 179 | { |
| 180 | hOutDebug(0,"ConfigFile[" << index << "] requested where size=" << mFileArray.size()); |
| 181 | abort(); |
| 182 | } |
| 183 | return static_cast<DeviceConfigLine *> (const_cast<ConfigLine *>(&mFileArray.at(index))); |
| 184 | } |
| 185 | |
| 186 | int ConfigFile::numberOfDevices() |
| 187 | { |
| 188 | int num=0; |
| 189 | for (size_t i=mLastSys+1 ; i<mFileArray.size(); i++) |
| 190 | { |
| 191 | if (!getDevice(i)->isRemark() && |
| 192 | !getDevice(i)->isDeleted() ) |
| 193 | num++; |
| 194 | } |
| 195 | return num; |
| 196 | } |
| 197 | |
| 198 | DeviceConfigLine * ConfigFile::getDevice(const std::string& devno) const |
| 199 | { |
| 200 | for (size_t i=mLastSys+1; i<mFileArray.size(); i++) |
| 201 | { |
| 202 | DeviceConfigLine * l = getDevice(i); |
| 203 | if (l->getToken(0) == devno) return l; |
| 204 | } |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | DeviceConfigLine * ConfigFile::findFirstDeviceByType(const char * type) const |
| 209 | { |
| 210 | DeviceConfigLine * ret; |
| 211 | for (unsigned int i=mLastSys+1; i < mFileArray.size() ; i++) |
| 212 | { |
| 213 | ret = getDevice(i); |
| 214 | if (ret->getToken(1) == type) |
| 215 | return ret; |
| 216 | } |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | SystemConfigLine * ConfigFile::operator[] (int index) const |
| 221 | { |
| 222 | if (index > mLastSys) |
| 223 | { |
| 224 | hOutDebug(5, "ConfigFile[" << index << "] requested where lastsys=" << mLastSys << std::endl); |
| 225 | abort(); |
| 226 | } |
| 227 | return static_cast<SystemConfigLine *> (const_cast<ConfigLine *>(&mFileArray.at(index))); |
| 228 | } |
| 229 | |
| 230 | int ConfigFile::size() const |
| 231 | { |
| 232 | return mFileArray.size(); |
| 233 | } |
| 234 | |
| 235 | bool ConfigFile::write(const string& fileName) |
| 236 | { |
| 237 | FILE * f = fopen(fileName.c_str(), "w"); |
| 238 | if (f==NULL) return false; |
| 239 | for (int i=0; i<size(); i++) |
| 240 | { |
| 241 | ConfigLine& configLine = mFileArray[i]; |
| 242 | if (!configLine.isNew() || configLine.isUpdated()) |
| 243 | fputs(mFileArray[i].getLine().c_str(),f); |
| 244 | configLine.setUpdated(false); |
| 245 | } |
| 246 | fclose(f); |
| 247 | mFileName = fileName; |
| 248 | mChanged = false; |
| 249 | mNewConfig = false; |
| 250 | mNewLines.clear(); |
| 251 | initialize(); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | bool ConfigFile::write() |
| 256 | { |
| 257 | return write(mFileName); |
| 258 | } |
| 259 | |
| 260 | void ConfigFile::deleteLine(int index) |
| 261 | { |
| 262 | mFileArray.erase(mFileArray.begin()+index); |
| 263 | mChanged = true; |
| 264 | } |
| 265 | |
| 266 | bool ConfigFile::changed() |
| 267 | { |
| 268 | if (mChanged) return true; |
| 269 | |
| 270 | for (int i=0; i<size(); i++) |
| 271 | { |
| 272 | ConfigLine configLine = mFileArray[i]; |
| 273 | if (configLine.isUpdated()) |
| 274 | return true; |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | const ConfigLine * ConfigFile::locateLine(const std::string& keyword, bool create, bool synonyms) |
| 280 | { |
| 281 | hOutDebug(3,"localLine seraching for '" << keyword << "'" << std::endl); |
| 282 | for (int i=0; i<=getLastSys(); i++) |
| 283 | { |
| 284 | hOutDebug(5, "examining " << (*this)[i]->getLine() << "=='" << (*this)[i]->getToken(0) << "'" <<std::endl); |
| 285 | if ( !(*this)[i]->isRemark() && ((*this)[i]->getUppercaseToken(0) == keyword)) |
| 286 | { |
| 287 | hOutDebug(5,"token(0):'" << (*this)[i]->getLine() << "'" << std::endl); |
| 288 | return (*this)[i]; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // look in new lines |
| 293 | for (unsigned int i=0; i<mNewLines.size(); i++) |
| 294 | { |
| 295 | ConfigLine * cl = &mNewLines[i]; |
| 296 | hOutDebug(4,"new line test:" << cl->getLine() << std::endl); |
| 297 | if (cl->getToken(0) == keyword) |
| 298 | { |
| 299 | hOutDebug(4,"found" << std::endl); |
| 300 | return cl; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (synonyms) |
| 305 | { |
| 306 | hOutDebug(4, "locating in synonyms " << keyword << " in synonyms " << std::endl); |
| 307 | for (int i=0; mSynonyms[i].keyword1.length() > 0; i++) |
| 308 | { |
| 309 | if (mSynonyms[i].keyword1.compare(keyword) == 0) |
| 310 | { |
| 311 | const ConfigLine * tmp = locateLine(mSynonyms[i].keyword2, false, false); |
| 312 | if (tmp != NULL) |
| 313 | { |
| 314 | hOutDebug(4, "found in synonyms" << std::endl); |
| 315 | return tmp; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | SystemConfigLine * ret = NULL; |
| 322 | if (create) |
| 323 | { |
| 324 | std::string line(keyword + "\n"); |
| 325 | ret = new SystemConfigLine(line.c_str()); |
| 326 | ret->setNew(true); |
| 327 | hOutDebug(3, "new line 1:" << ret->getToken(0) << " 2:" << ret->getToken(1) << std::endl); |
| 328 | mNewLines.push_back(*ret); |
| 329 | } |
| 330 | return ret; |
| 331 | |
| 332 | } |
| 333 | |
| 334 | void ConfigFile::appendNewLines() |
| 335 | { |
| 336 | hOutDebug(4, "append new lines:" << mNewLines.size() << std::endl); |
| 337 | // append new system lines |
| 338 | for (unsigned int i=0; i<mNewLines.size(); i++) |
| 339 | { |
| 340 | hOutDebug(3, mNewLines[i].getLine() << std::endl); |
| 341 | SystemConfigLine * cl = &mNewLines[i]; |
| 342 | addNonDev(cl); |
| 343 | } |
| 344 | // append new device lines |
| 345 | for (unsigned int i=0; i<mNewDeviceLines.size(); i++) |
| 346 | { |
| 347 | hOutDebug(3, mNewDeviceLines[i].getLine() << std::endl); |
| 348 | DeviceConfigLine * cl = &mNewDeviceLines[i]; |
| 349 | addDev(cl); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void ConfigFile::setNew(bool isNew) |
| 354 | { |
| 355 | mNewConfig = isNew; |
| 356 | if (isNew) mChanged = true; |
| 357 | } |
| 358 | |
| 359 | void ConfigFile::openTemplate() |
| 360 | { |
| 361 | templateCounter = -1; |
| 362 | } |
| 363 | |
| 364 | char *ConfigFile::getNextLine(char * buff, int len, BuildType type) |
| 365 | { |
| 366 | if (type == File) |
| 367 | { |
| 368 | if (mFile != NULL) |
| 369 | return fgets(buff, len, mFile); |
| 370 | |
| 371 | const char * from = configTemplate[++templateCounter]; |
| 372 | if (strcmp("EOF",from) == 0) |
| 373 | return NULL; |
| 374 | |
| 375 | strncpy(buff,from, len); |
| 376 | return buff; |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | if (mEditorIterator != mEditor->document()->end()) |
| 381 | { |
| 382 | len = mEditorIterator.length(); |
| 383 | if (len > 0) |
| 384 | { |
| 385 | strncpy(buff,mEditorIterator.text().toUtf8().data(),len-1); |
| 386 | buff[len-1] = '\0'; |
| 387 | } |
| 388 | else |
| 389 | buff[0] = '\0'; |
| 390 | mEditorIterator = mEditorIterator.next(); |
| 391 | return buff; |
| 392 | } |
| 393 | else |
| 394 | return NULL; |
| 395 | |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | const char * ConfigFile::configTemplate[52] = { |
| 400 | "#------------------------------------------------------------------------------\n", |
| 401 | "# CPU Configuration\n", |
| 402 | "#------------------------------------------------------------------------------\n", |
| 403 | "\n", |
| 404 | " CPUSERIAL 002623 # CPU serial number\n", |
| 405 | " CPUMODEL 3090 # CPU model number\n", |
| 406 | " MODEL EMULATOR # STSI returned model\n", |
| 407 | " PLANT ZZ # STSI returned plant\n", |
| 408 | " MANUFACTURER HRC # STSI returned manufacturer\n", |
| 409 | " LPARNAME HERCULES # DIAG 204 returned lparname\n", |
| 410 | " CPUVERID FD # CPU Version Identification\n", |
| 411 | " MAINSIZE 128 # Main storage size in megabytes\n", |
| 412 | " NUMCPU 1 # Number of CPUs" |
| 413 | "\n", |
| 414 | "#------------------------------------------------------------------------------\n", |
| 415 | "# OS Tailoring\n", |
| 416 | "#------------------------------------------------------------------------------\n", |
| 417 | "\n", |
| 418 | " LOADPARM 0120.... # IPL parameter\n", |
| 419 | "\n", |
| 420 | "#------------------------------------------------------------------------------\n", |
| 421 | "# Hercules Service Processor and Hercules Application Window\n", |
| 422 | "#------------------------------------------------------------------------------\n", |
| 423 | "\n", |
| 424 | " DIAG8CMD disable echo # OS may not issue commands via DIAG 8\n", |
| 425 | "\n", |
| 426 | "# HTTPPORT 8081 noauth userid password # HTTP server port\n", |
| 427 | "\n", |
| 428 | " PANRATE FAST # Panel refresh rate\n", |
| 429 | "\n", |
| 430 | "\n", |
| 431 | "#-------------------------------------------------------------------------------\n", |
| 432 | "# Advanced Hercules Performance Tailoring\n", |
| 433 | "#\n", |
| 434 | "# +----------------------------------------------------------------+\n", |
| 435 | "# | Caution: Modification of these parameters may adversely |\n", |
| 436 | "# | affect the performance of the host system |\n", |
| 437 | "# | and/or Hercules. |\n", |
| 438 | "# +----------------------------------------------------------------+\n", |
| 439 | "#-------------------------------------------------------------------------------\n", |
| 440 | "\n", |
| 441 | " HERCPRIO 0 # Hercules process runs at Normal priority\n", |
| 442 | " CPUPRIO 15 # CPU thread(s) run at Low priority\n", |
| 443 | " DEVPRIO 8 # Device thread(s) run at Below Normal priority\n", |
| 444 | " TODPRIO -20 # TOD Clock and timer thread are Time Critical\n", |
| 445 | "\n", |
| 446 | "#------------------------------------------------------------------------------\n", |
| 447 | "# I/O Devices\n", |
| 448 | "#------------------------------------------------------------------------------\n", |
| 449 | " 0009 3270\n", |
| 450 | "\n", |
| 451 | "EOF"}; |