* @package ChromiumTools * @version $Id$ */ if (!isset ($_SERVER ['argc'])) die (); $chromeSwitches = file ('http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/chrome_switches.cc'); $commentBuffer = array (); $currentCondition = ''; $switches = array (); function clearifyCondition ($condition) { if (substr ($condition, 0, 7) == 'defined' && strpos ($condition, '||') !== false) { preg_match_all ('/defined\(([^\)]+)\)/s', $condition, $matches); return 'One of these constants must be defined: "' . implode ('", "', $matches [1]) . '".'; } if (substr ($condition, 0, 7) == 'defined') { return 'The constant "' . substr ($condition, 8, -1) . '" must be defined.'; } if (substr ($condition, 0, 8) == '!defined') { return 'The constant "' . substr ($condition, 9, -1) . '" must not be defined.'; } return $condition; } foreach ($chromeSwitches as $idx => $line) { $line = trim ($line); switch (substr ($line, 0, 1)) { case '/': /* comment line */ { $commentBuffer [] = ltrim (substr ($line, 2)); break; } case 'c': /* const char */ { if (substr ($line, 0, 10) != 'const char') continue; $switchName = substr ($line, strpos ($line, '"') + 1, -2); if (substr ($switchName, 0, 5) == 'onst ') { /* Switch is on the next line, 80-character lines are so.. 1990. This line is over 140 characters in length! */ $switchName = substr (trim ($chromeSwitches [$idx + 1]), 1, -2); } $switches [$switchName] = array ( 'comment' => implode (' ', $commentBuffer), 'condition' => $currentCondition ); $commentBuffer = array (); break; } case '#': /* condition */ { $directive = substr ($line, 1, strpos ($line, ' ') ?: 8); switch (trim ($directive)) { case 'if': /* if-condition */ { $currentCondition = clearifyCondition (substr ($line, 4)); break; } case 'ifndef': /* if not defined */ { $currentCondition = 'The constant "' . substr ($line, 8) . '" must not be defined.'; break; } case 'else': /* reverse the condition */ { if (strpos ('must not be', $currentCondition) !== false) { $currentCondition = str_replace ('must not be', 'must be', $currentCondition); } else { $currentCondition = str_replace ('must be', 'must not be', $currentCondition); } break; } default: /* reset the condition state */ { $currentCondition = ''; } } break; } default: /* reset comment state */ { $commentBuffer = array (); break; } } } /** * Now sort the switches and generate a list of conditions, so we can refer to * them by number instead of having to include the entire condition every time. */ $conditionList = array (); $switchList = array (); ksort ($switches); foreach ($switches as $name => $information) { if (strlen ($information ['condition'])) { if (!isset ($conditionList [$information ['condition']])) { $conditionList [$information ['condition']] = count ($conditionList) + 1; } $information ['condition'] = $conditionList [$information ['condition']]; } $switchList [] = array ( 'name' => $name, 'comment' => $information ['comment'], 'condition' => $information ['condition'] ); } /** * Done. Now create a JSON file based on the gathered information. */ file_put_contents (__DIR__ . '/chromium-command-line-switches.json', json_encode (array ( 'switches' => $switchList, 'conditions' => array_flip ($conditionList) )));