// $Id: astyle_main.cpp,v 1.9 2004/02/06 09:37:36 devsolar Exp $ // -------------------------------------------------------------------------- // // Copyright (c) 1998,1999,2000,2001,2002 Tal Davidson. All rights reserved. // // compiler_defines.h // by Tal Davidson (davidsont@bigfoot.com) // // This file is a part of "Artistic Style" - an indentater and reformatter // of C, C++, C# and Java source files. // // -------------------------------------------------------------------------- // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // -------------------------------------------------------------------------- #include "compiler_defines.h" #include "astyle_main.h" #include "astyle.h" #include #include #include #include #include #include #ifdef WIN32 #include #endif #define IS_PARAM_OPTION(arg,op) ((arg).length() < strlen(op) ? 0 : (arg).COMPARE(0, strlen(op), string(op))==0) #define IS_PARAM_OPTIONS(arg,a,b) (IS_PARAM_OPTION((arg),(a)) || IS_PARAM_OPTION((arg),(b))) #define GET_PARAM(arg,op) ((arg).substr(strlen(op))) #define GET_PARAMS(arg,a,b) (IS_PARAM_OPTION((arg),(a)) ? GET_PARAM((arg),(a)) : GET_PARAM((arg),(b))) #ifdef USES_NAMESPACE using namespace std; using namespace istyle; #endif #ifndef ASTYLE_LIB // default options: ostream *_err = &cerr; string _suffix = ".orig"; const string _version = "1.21"; bool shouldBackupFile = true; // -------------------------------------------------------------------------- // Helper Functions // -------------------------------------------------------------------------- void SetColor(unsigned short ForeColor=3,unsigned short BackGroundColor=0) { #ifdef WIN32 HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,ForeColor|(BackGroundColor*16)); #endif } void error(const char *why, const char* what) { SetColor(12,0); (*_err) << why << ' ' << what <<'\n'; SetColor(7,0); exit(1); } #endif // ASTYLE_LIB bool parseOption(ASFormatter &formatter, const string &arg, const string &/*errorInfo*/) { #ifndef ASTYLE_LIB if ( ( arg == "n" ) || ( arg == "suffix=none" ) ) { shouldBackupFile = false; } else if ( IS_PARAM_OPTION(arg, "suffix=") ) { string suffixParam = GET_PARAM(arg, "suffix="); if (suffixParam.length() > 0) _suffix = suffixParam; } else #endif // ASTYLE_LIB if ( arg == "style=ansi" ) { formatter.setBracketIndent(false); formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(BREAK_MODE); formatter.setSwitchIndent(false); } else if ( arg == "style=gnu" ) { formatter.setBlockIndent(true); formatter.setSpaceIndentation(2); formatter.setBracketFormatMode(BREAK_MODE); formatter.setSwitchIndent(false); } else if ( arg == "style=kr" ) { formatter.setBracketIndent(false); formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(ATTACH_MODE); formatter.setSwitchIndent(false); } else if (IS_PARAM_OPTION(arg, "A")) { int style = 0; string styleParam = GET_PARAM(arg, "A"); if (styleParam.length() > 0) style = atoi(styleParam.c_str()); if (style == 1)//Ansi { formatter.setBracketIndent(false); formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(BREAK_MODE); formatter.setSwitchIndent(false); } else if (style == 2)//KR { formatter.setBracketIndent(false); formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(ATTACH_MODE); formatter.setSwitchIndent(false); } else if (style == 3) { formatter.setBlockIndent(true); formatter.setSpaceIndentation(2); formatter.setBracketFormatMode(BREAK_MODE); formatter.setSwitchIndent(false); } } else if ( IS_PARAM_OPTIONS(arg, "t", "indent=tab=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "t", "indent=tab="); if (spaceNumParam.length() > 0) { spaceNum = atoi(spaceNumParam.c_str()); if(spaceNum==0) { //(*_err) << errorInfo << arg << endl; return false; // unknown option } } formatter.setTabIndentation(spaceNum, false); } else if ( IS_PARAM_OPTIONS(arg, "T", "force-indent=tab=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "T", "force-indent=tab="); if (spaceNumParam.length() > 0) { spaceNum = atoi(spaceNumParam.c_str()); if(spaceNum==0) { //(*_err) << errorInfo << arg << endl; return false; // unknown option } } formatter.setTabIndentation(spaceNum, true); } else if ( IS_PARAM_OPTION(arg, "indent=tab") ) { formatter.setTabIndentation(4); } else if ( IS_PARAM_OPTIONS(arg, "s", "indent=spaces=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "s", "indent=spaces="); if (spaceNumParam.length() > 0) { spaceNum = atoi(spaceNumParam.c_str()); if(spaceNum==0) { //(*_err) << errorInfo << arg << endl; return false; // unknown option } } formatter.setSpaceIndentation(spaceNum); } else if ( IS_PARAM_OPTION(arg, "indent=spaces") ) { formatter.setSpaceIndentation(4); } else if ( IS_PARAM_OPTIONS(arg, "M", "max-instatement-indent=") ) { int maxIndent = 40; string maxIndentParam = GET_PARAMS(arg, "M", "max-instatement-indent="); if (maxIndentParam.length() > 0) maxIndent = atoi(maxIndentParam.c_str()); if (maxIndent < 40) { return false; // unknown option } if (maxIndent > 120) { return false; // unknown option } formatter.setMaxInStatementIndentLength(maxIndent); } else if ( IS_PARAM_OPTIONS(arg, "m", "min-conditional-indent=") ) { int minIndent = 0; string minIndentParam = GET_PARAMS(arg, "m", "min-conditional-indent="); if (minIndentParam.length() > 0) { minIndent = atoi(minIndentParam.c_str()); if(minIndent > 20) { return false; // unknown option } } formatter.setMinConditionalIndentLength(minIndent); } else if (IS_PARAM_OPTIONS(arg, "B", "indent-brackets")) { formatter.setBracketIndent(true); } else if (IS_PARAM_OPTIONS(arg, "G", "indent-blocks")) { formatter.setBlockIndent(true); } else if (IS_PARAM_OPTIONS(arg, "b", "brackets=break")) { formatter.setBracketFormatMode(BREAK_MODE); } else if (IS_PARAM_OPTIONS(arg, "a", "brackets=attach")) { formatter.setBracketFormatMode(ATTACH_MODE); } else if (IS_PARAM_OPTIONS(arg, "O", "one-line=keep-blocks")) { formatter.setBreakOneLineBlocksMode(false); } else if (IS_PARAM_OPTIONS(arg, "o", "one-line=keep-statements")) { formatter.setSingleStatementsMode(false); } else if (IS_PARAM_OPTIONS(arg, "L", "pad=paren")) { formatter.setParenthesisPaddingMode(true); } else if (IS_PARAM_OPTIONS(arg, "l", "pad=block")) { formatter.setBlockPaddingMode(true); } else if (IS_PARAM_OPTIONS(arg, "P", "pad=all")) { formatter.setOperatorPaddingMode(true); formatter.setParenthesisPaddingMode(true); formatter.setBlockPaddingMode(true); } else if (IS_PARAM_OPTIONS(arg, "p", "pad=oper")) { formatter.setOperatorPaddingMode(true); } else if (IS_PARAM_OPTIONS(arg, "E", "fill-empty-lines")) { formatter.setEmptyLineFill(true); } else if (IS_PARAM_OPTIONS(arg, "w", "indent-preprocessor")) { formatter.setPreprocessorIndent(true); } else if (IS_PARAM_OPTIONS(arg, "c", "convert-tabs")) { formatter.setTabSpaceConversionMode(true); } else if (IS_PARAM_OPTIONS(arg, "F", "break-blocks=all")) { formatter.setBreakBlocksMode(true); formatter.setBreakClosingHeaderBlocksMode(true); } else if (IS_PARAM_OPTIONS(arg, "f", "break-blocks")) { formatter.setBreakBlocksMode(true); } else if (IS_PARAM_OPTIONS(arg, "e", "break-elseifs")) { formatter.setBreakElseIfsMode(true); } else if ( (arg == "X") || (arg == "errors-to-standard-output") ) { //_err = &cout; } else if ( (arg == "v") || (arg == "version") ) { //(*_err) << "iStyle " << _version << endl; } else { //(*_err) << errorInfo << arg << endl; return false; // unknown option } return true; //o.k. } void importOptions(istream &in, vector &optionsVector) { char ch; string currentToken; while (in) { currentToken = ""; do { in.get(ch); if (in.eof()) break; // treat '#' as line comments if (ch == '#') while (in) { in.get(ch); if (ch == '\n' || ch == '\r') break; } // break options on spaces, tabs or new-lines if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') break; else currentToken.append(1, ch); } while (in); if (currentToken.length() != 0) optionsVector.push_back(currentToken); } } template bool parseOptions(ASFormatter &formatter, const ITER &optionsBegin, const ITER &optionsEnd, const string &errorInfo) { ITER option; bool ok = true; string arg, subArg; for (option = optionsBegin; option != optionsEnd; ++option) { arg = *option; //string(*option); if (arg.COMPARE(0, 2, string("--")) == 0) ok &= parseOption(formatter, arg.substr(2), errorInfo); else if (arg[0] == '-') { int i; for (i=1; i < (int)arg.length(); ++i) { if (isalpha(arg[i]) && i > 1) { ok &= parseOption(formatter, subArg, errorInfo); subArg = ""; } subArg.append(1, arg[i]); } ok &= parseOption(formatter, subArg, errorInfo); subArg = ""; } else { ok &= parseOption(formatter, arg, errorInfo); subArg = ""; } } return ok; } #ifndef ASTYLE_LIB void printHelpTitle() { cout << endl; SetColor(10,0); cout << "iStyle " << _version; SetColor(2,0); cout << " (Fast and Free Automatic Formatter for Verilog Source Code)\n"; cout << " (Created by haimag, Report Bugs: haimag@gmail.com)\n"; cout << " (Thanks to Tal Davidson & Astyle)\n"; cout << endl; SetColor(7,0); //~ cout << endl; } void printHelpSimple(int isGetchar=0) { SetColor(14,0); cout << endl; cout << "Usage : iStyle [options] Foo.v B*r.v [...]\n"; cout << endl; SetColor(7,0); cout << "For help on options, type 'iStyle -h'" ; if(isGetchar ==1 ) { cout << ", Press ENTER to exit." << endl; getchar(); } else { cout<<"."< optionsVector; istringstream opt(pOptions); importOptions(opt, optionsVector); bool ok = parseOptions(formatter, optionsVector.begin(), optionsVector.end(), string("Unknown command line option: ")); if (!ok) strErr = "Unknown command line option: "; istringstream in(pSourceIn); ASStreamIterator streamIterator(&in); ostringstream out; formatter.init(&streamIterator); while (formatter.hasMoreLines()) { out << formatter.nextLine(); if (formatter.hasMoreLines()) out << "\n"; } strOut = out.str(); return true; } #elif !defined(ASTYLECON_LIB) int main(int argc, char *argv[]) { ASFormatter formatter; vector fileNameVector; vector optionsVector; string optionsFileName = ""; string arg; bool ok = true; bool shouldPrintHelp = false; bool shouldParseOptionsFile = true; _err = &cerr; _suffix = ".orig"; printHelpTitle(); // manage flags for (int i=1; i fileOptionsVector; // reading (whitespace seperated) strings from file into string vector importOptions(optionsIn, fileOptionsVector); ok = parseOptions(formatter, fileOptionsVector.begin(), fileOptionsVector.end(), string("Unknown option in default options file: ")); } optionsIn.close(); if (!ok) { printHelpSimple(); } } } // parse options from command line ok = parseOptions(formatter, optionsVector.begin(), optionsVector.end(), string("Unknown command line option: ")); if (!ok) { printHelpSimple(); exit(1); } if (shouldPrintHelp) { printHelpFull(); exit(1); } // if no files have been given, use cin for input and cout for output if (fileNameVector.empty() ) { printHelpSimple(1); } else { // indent the given files for (int i=0; i