/* * MAKECONV.CMD - V1.1 C.Langanke 2007 * * Syntax: makeconv.cmd [sourcefile] * * cfgconv.cmd takes simple makefiles containing symbol definitons * and either * - converts them to other formats (like e.g. C-header files) * - applies the values to other files * * The sourcefile may include only lines like varname=varvalue * and may use nmake macros. Condition expessions like !ifdef * are ignored. * * The action being taken for a definition line in the inut file * like 'NAME=VALUE' is determined by the filename * * *.h - C header: convert to: #define NAME "VALUE" * *.cmd - CMD file: convert to: SET NAME=VALUE * *.inc - HyperText/2 file: convert to: .SET NAME=VALUE * *.diz - DIZ file: apply first version value 'Vx.xx' * and apply macros */ /* The first comment is used as online help text */ /* ***** BEGIN LICENSE BLOCK ***** * Version: CDDL 1.0 * * The contents of this file are subject to the COMMON DEVELOPMENT AND * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of * the License at http://www.sun.com/cddl/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is * "netlabs.org Open Source Archive Administrator/Client" * * The Original Distribution Package is named * "netlabs.org Open Source Archive Administrator/Client" * and maintained and distributed by the Initial Developer * and/or netlabs.org only. * * In addition to the CDDL the following applies: * If you modify the Original Code, you may distribute it only * as a part of a distribution package where * - the name of the package, created directories or * OS2.INI entries do not contain any of the terms * - "netlabs.org Open Source Archive" * - "NOSA" * - "NOSAC" * - "NOSAADM" * - neither netlabs.org nor the Initial Developer is stated as * the vendor or originator of the resulting Distribution Package, * which contains the Modified Code. * * The Initial Developer of the Original Code is * netlabs.org: Christian Langanke . * Portions created by the Initial Developer are Copyright (C) 1999-2008 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * ***** END LICENSE BLOCK ***** */ SIGNAL ON HALT TitleLine = STRIP(SUBSTR(SourceLine(2), 3)); PARSE VAR TitleLine CmdName'.CMD 'Info Title = CmdName Info env = 'OS2ENVIRONMENT'; TRUE = (1 = 1); FALSE = (0 = 1); CrLf = '0d0a'x; Redirection = '> NUL 2>&1'; '@ECHO OFF' /* OS/2 Error codes */ ERROR.NO_ERROR = 0; ERROR.INVALID_FUNCTION = 1; ERROR.FILE_NOT_FOUND = 2; ERROR.PATH_NOT_FOUND = 3; ERROR.ACCESS_DENIED = 5; ERROR.NOT_ENOUGH_MEMORY = 8; ERROR.INVALID_FORMAT = 11; ERROR.INVALID_DATA = 13; ERROR.NO_MORE_FILES = 18; ERROR.WRITE_FAULT = 29; ERROR.READ_FAULT = 30; ERROR.GEN_FAILURE = 31; ERROR.INVALID_PARAMETER = 87; ERROR.OPEN_FAILED = 110; ERROR.ENVVAR_NOT_FOUND = 203; GlobalVars = 'Title CmdName CrLf env TRUE FALSE Redirection ERROR.'; /* eventually show help */ ARG Parm . IF ((Parm = '') | (POS('/?', Parm) > 0)) THEN DO rc = ShowHelp(); EXIT( ERROR.INVALID_PARAMETER); END; /* dafault values */ GlobalVars = GlobalVars 'Var.'; rc = ERROR.NO_ERROR; ValidExtensions = '.H .CMD .INC .DIZ'; ConfigFile = ''; SourceFile = ''; OutFile = ''; Var. = ''; DO 1 /* get parms */ PARSE ARG ConfigFile SourceFile OutFile; IF (OutFile = '') THEN DO SourceFile = ''; PARSE ARG ConfigFile OutFile; END; IF (OutFile = '') THEN DO SAY CmdName': error: not enough parameters'; rc = ERROR.INVALID_PARAMETER; LEAVE; END; /* determine the type of outfile */ FileExt = ''; BaseName = FILESPEC( 'N', OutFile); FileExtPos = LASTPOS( '.', BaseName); IF (FileExtPos > 0) THEN FileExt = TRANSLATE( SUBSTR( BaseName, FileExtPos)); IF (WORDPOS( FileExt, ValidExtensions) = 0) THEN DO SAY CmdName': error: outputfile has no supported format:' OutFile; rc = ERROR.INVALID_PARAMETER; LEAVE; END; /* open files */ IF (STREAM( ConfigFile, 'C', 'OPEN READ') \= 'READY:') THEN DO SAY CmdName': error: cannot open' ConfigFile 'for reading'; rc = ERROR.OPEN_FAILED; LEAVE; END; IF (SourceFile \= '') THEN DO IF (STREAM( SourceFile, 'C', 'OPEN READ') \= 'READY:') THEN DO SAY CmdName': error: cannot open' SourceFile 'for reading'; rc = ERROR.OPEN_FAILED; LEAVE; END; END; rcx = SysFileDelete( OutFile); IF (STREAM( OutFile, 'C', 'OPEN WRITE') \= 'READY:') THEN DO SAY CmdName': error: cannot open' OutFile 'for writing.'; rc = ERROR.OPEN_FAILED; LEAVE; END; /* read input file, and convert, if required */ VersionValue = ''; DO WHILE (LINES( ConfigFile) > 0) NewLine = ''; ThisLine = STRIP( LINEIN( ConfigFile)); IF (ThisLine = '') THEN ITERATE; IF (LEFT( ThisLine, 1) = '!') THEN ITERATE; IF (LEFT( ThisLine, 1) = '#') THEN ITERATE; /* read consecutive lines */ DO WHILE (RIGHT( ThisLine, 1) = '\') NextLine = LINEIN( ConfigFile); IF (LEFT( NextLine, 1) = '#') THEN NextLine = LINEIN( ConfigFile); ThisLine = STRIP( LEFT( ThisLine, LENGTH( ThisLine) - 1)) STRIP( NextLine); END; IF (POS( '=', ThisLine) = 0) THEN ITERATE; PARSE VAR ThisLine VarName'='VarValue; VarName = STRIP( VarName); VarValue = STRIP( VarValue); VarValue = ReplaceMacros( VarValue); SELECT WHEN (FileExt = '.H') THEN DO CValue = VarValue; SELECT WHEN (DATATYPE( CValue) = 'NUM') THEN NOP; WHEN (TRANSLATE( LEFT( CValue, 2)) = '0X') THEN NOP; OTHERWISE CValue = '"'CValue'"'; END; NewLine = '#define' VarName CValue; END WHEN (FileExt = '.INC') THEN DO IF (VarValue = '') THEN NewLine = ''; ELSE NewLine = '.SET' VarName'='VarValue; END; WHEN (FileExt = '.CMD') THEN DO NewLine = 'SET' VarName'='EscapeCmdChars( VarValue); END; WHEN ((FileExt = '.DIZ') & (VersionValue = '')) THEN DO IF (LEFT( TRANSLATE( VarValue), 1) = 'V') THEN DO PARSE VAR VarValue WITH . +1 VersionMajor'.'VersionMinor; IF ((VersionMinor \= '') &, (DATATYPE( VersionMajor) = 'NUM')) THEN VersionValue = VersionMajor'.'Versionminor; END; END; OTHERWISE NOP; END; /* add variable to our var pool */ Var.VarName = VarValue; /* writeout new line */ IF (NewLine \='') THEN rcx = LINEOUT( OutFile, NewLine); END; /* if a patch is required, do that here */ IF (FileExt = '.DIZ') THEN DO FirstLine = LINEIN( SourceFile); PARSE VAR FirstLine '('DizVersion')'DizHeader; IF (DizHeader = '') THEN DO SAY CmdName': error: sourcefile has no version information:' SourceFile; rc = ERROR.INVALID_PARAMETER; LEAVE; END; /* take chare for a 'v' char */ DisVerChar = ''; IF (DATATYPE( LEFT( DizVersion, 1)) \= 'NUM') THEN PARSE VAR DizVersion DisVerChar +1 DizVersion; /* assemble new version information */ DizVersion = DisVerChar''VersionValue; NewLine = '('DizVersion')'DizHeader; rcx = LINEOUT( OutFile, NewLine); /* append rest of file */ DO WHILE (LINES( SourceFile) > 0) ThisLine = LINEIN( SourceFile); NewLine = ReplaceMacros( ThisLine); rcx = LINEOUT( OutFile, NewLine); END; END; END; /* cleanup */ IF (ConfigFile \= '') THEN rcx = STREAM( ConfigFile, 'C', 'CLOSE'); IF (SourceFile \= '') THEN rcx = STREAM( SourceFile, 'C', 'CLOSE'); IF (OutFile \= '') THEN rcx = STREAM( OutFile, 'C', 'CLOSE'); EXIT( rc); /* ------------------------------------------------------------------------- */ HALT: SAY 'Interrupted by user.'; EXIT( ERROR.GEN_FAILURE); /* ------------------------------------------------------------------------- */ ShowHelp: PROCEDURE EXPOSE (GlobalVars) /* show title */ SAY; SAY Title; SAY; PARSE SOURCE . . ThisFile /* skip header */ DO i = 1 TO 3 rc = LINEIN(ThisFile); END; /* show help text */ ThisLine = LINEIN(Thisfile); DO WHILE (ThisLine \= ' */') SAY SUBSTR(ThisLine, 3); ThisLine = LINEIN(Thisfile); END; /* close file */ rc = LINEOUT(Thisfile); RETURN(''); /* ========================================================================= */ EscapeCmdChars: PROCEDURE PARSE ARG VarValue; InvalidChars = '&|<>'; NewValue = ''; DO WHILE (VarValue \= '') PARSE VAR VarValue ThisChar +1 VarValue; IF (POS( ThisChar, InvalidChars) > 0) THEN ThisChar = '"'ThisChar'"' NewValue = NewValue''ThisChar; END; RETURN( NewValue); /* ========================================================================= */ ReplaceMacros: PROCEDURE EXPOSE (GlobalVars) PARSE ARG Line NewLine = ''; /* detect marcos */ mPos = POS( '$(', Line); DO WHILE (mPos > 0) mEnd = POS( ')', Line, mPos); mValue = ''; IF (mEnd > 0) THEN DO /* determine name of macro and its replacement */ mName = SUBSTR( Line, mPos + 2, mEnd - mPos - 2) IF (mName \= '') THEN mValue = Var.mName; END; ELSE /* make sure that invalid macro gets deleted */ mEnd = LENGTH( Line); /* copy over line up to macro and macro */ NewLine = NewLine''LEFT( Line, mPos - 1)''mValue; /* remove up to end of current macro */ Line = SUBSTR( Line, mEnd + 1); /* next macro */ mPos = POS( '$(', Line); END; /* append rest of remaining line */ NewLine = NewLine''Line; RETURN( NewLine)