/* * Parse the /D option. */ static int parse_D( OPT_STRING **p ) /**********************************/ { char * str; char * eq; p = p; CmdScanWhitespace(); str = CmdScanString(); if( str == NULL ) { FatalError( "/D requires an argument" ); return( 0 ); } for( ;; ) { /* convert all '#' chars to '=' chars */ eq = strchr( str, '#' ); if( eq == NULL ) break; *eq = '='; } if( DefineMacro( str ) ) { return( 1 ); } else { Warning( "Ignoring invalid macro definition '%s'", str ); return( 0 ); } }
void testFilePolicies(void) { const char* currentConfigFile = "MyConfigFile"; const char* currentConfigFileBak = "MyConfigFile.bak"; const char* noAccessFile = "NoAccessFile"; /* Define a macro used in the static policy table */ DefineMacro("currentConfigFilePath", currentConfigFile); PEGASUS_TEST_ASSERT(CheckOpenFilePolicy(currentConfigFile, 'w', NULL) == 0); PEGASUS_TEST_ASSERT(CheckOpenFilePolicy(noAccessFile, 'w', NULL) != 0); PEGASUS_TEST_ASSERT(CheckRemoveFilePolicy(currentConfigFile) == 0); PEGASUS_TEST_ASSERT(CheckRemoveFilePolicy(noAccessFile) != 0); PEGASUS_TEST_ASSERT( CheckRenameFilePolicy(currentConfigFile, currentConfigFileBak) == 0); PEGASUS_TEST_ASSERT( CheckRenameFilePolicy(currentConfigFile, noAccessFile) != 0); }
/* ============== GetToken ============== */ qboolean GetToken (qboolean crossline) { char *token_p; if (tokenready) // is a token allready waiting? { tokenready = false; return true; } // printf("script_p %x (%x)\n", script->script_p, script->end_p ); fflush( stdout ); if (script->script_p >= script->end_p) { return EndOfScript (crossline); } tokenready = false; // skip space, ctrl chars skipspace: while (*script->script_p <= 32) { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } if (*(script->script_p++) == '\n') { if (!crossline) { Error ("Line %i is incomplete\n",scriptline); } scriptline = ++script->line; } } if (script->script_p >= script->end_p) { return EndOfScript (crossline); } // strip single line comments if (*script->script_p == ';' || *script->script_p == '#' || // semicolon and # is comment field (*script->script_p == '/' && *((script->script_p)+1) == '/')) // also make // a comment field { if (!crossline) Error ("Line %i is incomplete\n",scriptline); while (*script->script_p++ != '\n') { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } } scriptline = ++script->line; goto skipspace; } // strip out matching /* */ comments if (*script->script_p == '/' && *((script->script_p)+1) == '*') { script->script_p += 2; while (*script->script_p != '*' || *((script->script_p)+1) != '/') { if (*script->script_p++ != '\n') { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } scriptline = ++script->line; } } script->script_p += 2; goto skipspace; } // copy token to buffer token_p = token; if (*script->script_p == '"') { // quoted token script->script_p++; while (*script->script_p != '"') { *token_p++ = *script->script_p++; if (script->script_p == script->end_p) break; if (token_p == &token[MAXTOKEN]) Error ("Token too large on line %i\n",scriptline); } script->script_p++; } else if ( g_bCheckSingleCharTokens && !g_sSingleCharTokens.IsEmpty() && strchr( g_sSingleCharTokens.String(), *script->script_p ) != NULL ) { *token_p++ = *script->script_p++; } else // regular token while ( *script->script_p > 32 && *script->script_p != ';') { if ( !ExpandMacroToken( token_p ) ) { if ( !ExpandVariableToken( token_p ) ) { *token_p++ = *script->script_p++; if (script->script_p == script->end_p) break; if (token_p == &token[MAXTOKEN]) Error ("Token too large on line %i\n",scriptline); } } } // add null to end of token *token_p = 0; // check for other commands if ( !stricmp( token, "$include" ) ) { GetToken( false ); bool bFallbackToToken = true; CUtlVector< CUtlString > expandedPathList; if ( CmdLib_ExpandWithBasePaths( expandedPathList, token ) > 0 ) { for ( int i = 0; i < expandedPathList.Count(); ++i ) { CUtlVector< CUtlString > findFileList; FindFileAbsoluteList( findFileList, expandedPathList[i].String() ); if ( findFileList.Count() > 0 ) { bFallbackToToken = false; // Only add the first set of glob matches from the first base path for ( int j = 0; j < findFileList.Count(); ++j ) { AddScriptToStack( const_cast< char * >( findFileList[j].String() ) ); } break; } } } if ( bFallbackToToken ) { AddScriptToStack( token ); } return GetToken( crossline ); } else if (!stricmp (token, "$definemacro")) { GetToken (false); DefineMacro(token); return GetToken (crossline); } else if (!stricmp (token, "$definevariable")) { GetToken (false); DefineVariable(token); return GetToken (crossline); } else if (AddMacroToStack( token )) { return GetToken (crossline); } return true; }
void Preprocess (void) /* Preprocess a line */ { int Skip; ident Directive; /* Create the output buffer if we don't already have one */ if (MLine == 0) { MLine = NewStrBuf (); } /* Skip white space at the beginning of the line */ SkipWhitespace (0); /* Check for stuff to skip */ Skip = 0; while (CurC == '\0' || CurC == '#' || Skip) { /* Check for preprocessor lines lines */ if (CurC == '#') { NextChar (); SkipWhitespace (0); if (CurC == '\0') { /* Ignore the empty preprocessor directive */ continue; } if (!IsSym (Directive)) { PPError ("Preprocessor directive expected"); ClearLine (); } else { switch (FindPPToken (Directive)) { case PP_DEFINE: if (!Skip) { DefineMacro (); } break; case PP_ELIF: if (IfIndex >= 0) { if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) { /* Handle as #else/#if combination */ if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) { Skip = !Skip; } IfStack[IfIndex] |= IFCOND_ELSE; Skip = DoIf (Skip); /* #elif doesn't need a terminator */ IfStack[IfIndex] &= ~IFCOND_NEEDTERM; } else { PPError ("Duplicate #else/#elif"); } } else { PPError ("Unexpected #elif"); } break; case PP_ELSE: if (IfIndex >= 0) { if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) { if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) { Skip = !Skip; } IfStack[IfIndex] |= IFCOND_ELSE; } else { PPError ("Duplicate #else"); } } else { PPError ("Unexpected `#else'"); } break; case PP_ENDIF: if (IfIndex >= 0) { /* Remove any clauses on top of stack that do not * need a terminating #endif. */ while (IfIndex >= 0 && (IfStack[IfIndex] & IFCOND_NEEDTERM) == 0) { --IfIndex; } /* Stack may not be empty here or something is wrong */ CHECK (IfIndex >= 0); /* Remove the clause that needs a terminator */ Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0; } else { PPError ("Unexpected `#endif'"); } break; case PP_ERROR: if (!Skip) { DoError (); } break; case PP_IF: Skip = DoIf (Skip); break; case PP_IFDEF: Skip = DoIfDef (Skip, 1); break; case PP_IFNDEF: Skip = DoIfDef (Skip, 0); break; case PP_INCLUDE: if (!Skip) { DoInclude (); } break; case PP_LINE: /* Should do something in C99 at least, but we ignore it */ if (!Skip) { ClearLine (); } break; case PP_PRAGMA: if (!Skip) { DoPragma (); goto Done; } break; case PP_UNDEF: if (!Skip) { DoUndef (); } break; case PP_WARNING: /* #warning is a non standard extension */ if (IS_Get (&Standard) > STD_C99) { if (!Skip) { DoWarning (); } } else { if (!Skip) { PPError ("Preprocessor directive expected"); } ClearLine (); } break; default: if (!Skip) { PPError ("Preprocessor directive expected"); } ClearLine (); } } } if (NextLine () == 0) { if (IfIndex >= 0) { PPError ("`#endif' expected"); } return; } SkipWhitespace (0); } PreprocessLine (); Done: if (Verbosity > 1 && SB_NotEmpty (Line)) { printf ("%s(%u): %.*s\n", GetCurrentFile (), GetCurrentLine (), (int) SB_GetLen (Line), SB_GetConstBuf (Line)); } }
/* ============== GetToken ============== */ qboolean GetToken (qboolean crossline) { char *token_p; if (tokenready) // is a token allready waiting? { tokenready = false; return true; } // printf("script_p %x (%x)\n", script->script_p, script->end_p ); fflush( stdout ); if (script->script_p >= script->end_p) { return EndOfScript (crossline); } tokenready = false; // skip space, ctrl chars skipspace: while (*script->script_p <= 32) { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } if (*(script->script_p++) == '\n') { if (!crossline) { Error ("Line %i is incomplete\n",scriptline); } scriptline = ++script->line; } } if (script->script_p >= script->end_p) { return EndOfScript (crossline); } // strip single line comments if (*script->script_p == ';' || *script->script_p == '#' || // semicolon and # is comment field (*script->script_p == '/' && *((script->script_p)+1) == '/')) // also make // a comment field { if (!crossline) Error ("Line %i is incomplete\n",scriptline); while (*script->script_p++ != '\n') { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } } scriptline = ++script->line; goto skipspace; } // strip out matching /* */ comments if (*script->script_p == '/' && *((script->script_p)+1) == '*') { script->script_p += 2; while (*script->script_p != '*' || *((script->script_p)+1) != '/') { if (*script->script_p++ != '\n') { if (script->script_p >= script->end_p) { return EndOfScript (crossline); } scriptline = ++script->line; } } script->script_p += 2; goto skipspace; } // copy token to buffer token_p = token; if (*script->script_p == '"') { // quoted token script->script_p++; while (*script->script_p != '"') { *token_p++ = *script->script_p++; if (script->script_p == script->end_p) break; if (token_p == &token[MAXTOKEN]) Error ("Token too large on line %i\n",scriptline); } script->script_p++; } else // regular token while ( *script->script_p > 32 && *script->script_p != ';') { if ( !ExpandMacroToken( token_p ) ) { if ( !ExpandVariableToken( token_p ) ) { *token_p++ = *script->script_p++; if (script->script_p == script->end_p) break; if (token_p == &token[MAXTOKEN]) Error ("Token too large on line %i\n",scriptline); } } } // add null to end of token *token_p = 0; // check for other commands if (!stricmp (token, "$include")) { GetToken (false); AddScriptToStack (token); return GetToken (crossline); } else if (!stricmp (token, "$definemacro")) { GetToken (false); DefineMacro(token); return GetToken (crossline); } else if (!stricmp (token, "$definevariable")) { GetToken (false); DefineVariable(token); return GetToken (crossline); } else if (AddMacroToStack( token )) { return GetToken (crossline); } return true; }
void testDumpPolicy(void) { FILE* dumpFile; char dumpFileBuffer[MAX_DUMP_SIZE]; size_t numBytesRead; unlink(TEST_DUMP_FILE); /* Test DumpPolicy() with expandMacros=false */ { const char* expectedDumpResult = "===== Policy:\n" "OpenFile(\"${currentConfigFilePath}\", \"w\")\n" "RenameFile(\"${currentConfigFilePath}\", " "\"${currentConfigFilePath}.bak\")\n" "RemoveFile(\"${currentConfigFilePath}\")\n" "RemoveFile(\"${currentConfigFilePath}.bak\")\n" "OpenFile(\"${plannedConfigFilePath}\", \"w\")\n" "RenameFile(\"${plannedConfigFilePath}\", " "\"${plannedConfigFilePath}.bak\")\n" "RemoveFile(\"${plannedConfigFilePath}\")\n" "RemoveFile(\"${plannedConfigFilePath}.bak\")\n" "OpenFile(\"${passwordFilePath}\", \"w\")\n" "RenameFile(\"${passwordFilePath}.bak\", \"${passwordFilePath}\")\n" "RenameFile(\"${passwordFilePath}\", \"${passwordFilePath}.bak\")\n" "RemoveFile(\"${passwordFilePath}.bak\")\n" "RemoveFile(\"${passwordFilePath}\")\n" "OpenFile(\"${sslKeyFilePath}\", \"r\")\n" "OpenFile(\"${sslTrustStore}/*\", \"w\")\n" "RemoveFile(\"${sslTrustStore}/*\")\n" "OpenFile(\"${crlStore}/*\", \"w\")\n" "RemoveFile(\"${crlStore}/*\")\n" "RemoveFile(\"${localAuthDir}/*\")\n" "\n"; dumpFile = fopen(TEST_DUMP_FILE, "a"); PEGASUS_TEST_ASSERT(dumpFile != 0); DumpPolicy(dumpFile, 0); fclose(dumpFile); dumpFile = fopen(TEST_DUMP_FILE, "rb"); PEGASUS_TEST_ASSERT(dumpFile != 0); memset(dumpFileBuffer, 0, MAX_DUMP_SIZE); numBytesRead = fread(dumpFileBuffer, sizeof(char), MAX_DUMP_SIZE - 1, dumpFile); PEGASUS_TEST_ASSERT(numBytesRead != 0); fclose(dumpFile); PEGASUS_TEST_ASSERT(strcmp(dumpFileBuffer, expectedDumpResult) == 0); unlink(TEST_DUMP_FILE); } /* Test DumpPolicyHelper() with expandMacros=false */ { const char* expectedDumpResult = "Ping()\n" "RenameFile(\"${file1}\", \"${file2}\")\n" "RenameFile(\"file1\", \"${file2}\")\n" "RenameFile(\"file1\", \"file2\")\n"; dumpFile = fopen(TEST_DUMP_FILE, "a"); PEGASUS_TEST_ASSERT(dumpFile != 0); DumpPolicyHelper(dumpFile, _testPolicyTable, _testPolicyTableSize, 0); fclose(dumpFile); dumpFile = fopen(TEST_DUMP_FILE, "rb"); PEGASUS_TEST_ASSERT(dumpFile != 0); memset(dumpFileBuffer, 0, MAX_DUMP_SIZE); numBytesRead = fread(dumpFileBuffer, sizeof(char), MAX_DUMP_SIZE - 1, dumpFile); PEGASUS_TEST_ASSERT(numBytesRead != 0); fclose(dumpFile); PEGASUS_TEST_ASSERT(strcmp(dumpFileBuffer, expectedDumpResult) == 0); unlink(TEST_DUMP_FILE); } /* Test DumpPolicyHelper() with expandMacros=true */ { const char* expectedDumpResult = "Ping()\n" "RenameFile(\"MyFile1\", \"MyFile2\")\n" "RenameFile(\"file1\", \"MyFile2\")\n" "RenameFile(\"file1\", \"file2\")\n"; DefineMacro("file1", "MyFile1"); DefineMacro("file2", "MyFile2"); dumpFile = fopen(TEST_DUMP_FILE, "a"); PEGASUS_TEST_ASSERT(dumpFile != 0); DumpPolicyHelper(dumpFile, _testPolicyTable, _testPolicyTableSize, 1); fclose(dumpFile); UndefineMacro("file1"); UndefineMacro("file2"); dumpFile = fopen(TEST_DUMP_FILE, "rb"); PEGASUS_TEST_ASSERT(dumpFile != 0); memset(dumpFileBuffer, 0, MAX_DUMP_SIZE); numBytesRead = fread(dumpFileBuffer, sizeof(char), MAX_DUMP_SIZE - 1, dumpFile); PEGASUS_TEST_ASSERT(numBytesRead != 0); fclose(dumpFile); PEGASUS_TEST_ASSERT(strcmp(dumpFileBuffer, expectedDumpResult) == 0); unlink(TEST_DUMP_FILE); } }