diff options
Diffstat (limited to 'data')
-rw-r--r-- | data/canonicalize_filename.sh | 48 | ||||
-rwxr-xr-x | data/check-code-style | 60 | ||||
-rw-r--r-- | data/install-git-hook.sh | 11 | ||||
-rwxr-xr-x | data/lineup-parameters | 210 | ||||
-rw-r--r-- | data/pre-commit-hook | 17 | ||||
-rw-r--r-- | data/uncrustify.cfg | 111 |
6 files changed, 457 insertions, 0 deletions
diff --git a/data/canonicalize_filename.sh b/data/canonicalize_filename.sh new file mode 100644 index 0000000..49c4dec --- /dev/null +++ b/data/canonicalize_filename.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Provide the canonicalize filename (physical filename with out any symlinks) +# like the GNU version readlink with the -f option regardless of the version of +# readlink (GNU or BSD). + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/ddddavidmartin/Pre-commit-hooks +# Contact: David Martin, ddddavidmartin@fastmail.com + +########################################################### +# There should be no need to change anything below this line. + +# Canonicalize by recursively following every symlink in every component of the +# specified filename. This should reproduce the results of the GNU version of +# readlink with the -f option. +# +# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac +canonicalize_filename () { + local target_file="$1" + local physical_directory="" + local result="" + + # Need to restore the working directory after work. + local working_dir="`pwd`" + + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + + # Iterate down a (possible) chain of symlinks + while [ -L "$target_file" ] + do + target_file="$(readlink -- "$target_file")" + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + done + + # Compute the canonicalized name by finding the physical path + # for the directory we're in and appending the target file. + physical_directory="`pwd -P`" + result="$physical_directory/$target_file" + + # restore the working directory after work. + cd -- "$working_dir" + + echo "$result" +} diff --git a/data/check-code-style b/data/check-code-style new file mode 100755 index 0000000..3b9ca19 --- /dev/null +++ b/data/check-code-style @@ -0,0 +1,60 @@ +#!/bin/bash + +SOURCE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" + +UNCRUSTIFY=$(command -v uncrustify) +if [ -z "$UNCRUSTIFY" ]; +then + echo "Uncrustify is not installed on your system." + exit 1 +fi + +LINEUP_PARAMETERS="$SOURCE_ROOT/data/lineup-parameters" +if [ ! -x "$LINEUP_PARAMETERS" ]; +then + echo "lineup-parameters script is missing" + exit 1 +fi + +# create a filename to store our generated patch +prefix="libproxy-ccs" +suffix="$(date +%C%y-%m-%d_%Hh%Mm%Ss)" +patch="/tmp/$prefix-$suffix.patch" + +# clean up old patches +rm -f /tmp/$prefix*.patch + +pushd $SOURCE_ROOT +for DIR in src +do + # Aligning prototypes is not working yet, so avoid headers + for FILE in $(find "$DIR" -name "*.c" ! -path "*/contrib/*") + do + "$UNCRUSTIFY" -q -c "data/uncrustify.cfg" -f "$FILE" -o "$FILE.uncrustify" + "$LINEUP_PARAMETERS" "$FILE.uncrustify" > "$FILE.temp" && mv "$FILE.temp" "$FILE.uncrustify" + diff -urN "$FILE" "$FILE.uncrustify" | \ + sed -e "1s|--- |--- a/|" -e "2s|+++ |+++ b/|" >> "$patch" + rm "$FILE.uncrustify" + done +done +popd + +if [ ! -s "$patch" ] ; then + printf "** Commit is valid. \\o/\n" + rm -f "$patch" + exit 0 +fi + +printf "** Commit does not comply to the correct style :(\n\n" + +if hash colordiff 2> /dev/null; then + colordiff < $patch +else + cat "$patch" +fi + +printf "\n" +printf "You can apply these changed with: git apply $patch\n" +printf "\n" + +exit 1 diff --git a/data/install-git-hook.sh b/data/install-git-hook.sh new file mode 100644 index 0000000..355867b --- /dev/null +++ b/data/install-git-hook.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +cd "$MESON_SOURCE_ROOT" + +[ -d .git ] || exit 2 # not a git repo +[ ! -f .git/hooks/pre-commit ] || exit 2 # already installed + +cp data/pre-commit-hook .git/hooks/pre-commit +cp data/canonicalize_filename.sh .git/hooks/canonicalize_filename.sh +chmod +x .git/hooks/pre-commit +echo "Activated pre-commit hook"
\ No newline at end of file diff --git a/data/lineup-parameters b/data/lineup-parameters new file mode 100755 index 0000000..aae22de --- /dev/null +++ b/data/lineup-parameters @@ -0,0 +1,210 @@ +#!/usr/bin/env python3 +# +# Copyright © 2019 Michael Catanzaro <mcatanzaro@gnome.org> +# +# 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 3 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, see <http://www.gnu.org/licenses/>. + +# Based on original C lineup-parameters by Sébastien Wilmet <swilmet@gnome.org> +# Rewritten in Python to allow simple execution from source directory. +# +# Usage: lineup-parameters [file] +# If the file is not given, stdin is read. +# The result is printed to stdout. +# +# The restrictions: +# - The function name must be at column 0, followed by a space and an opening +# parenthesis; +# - One parameter per line; +# - A parameter must follow certain rules (see the regex in the code), but it +# doesn't accept all possibilities of the C language. +# - The opening curly brace ("{") of the function must also be at column 0. +# +# If one restriction is missing, the function declaration is not modified. +# +# Example: +# +# gboolean +# frobnitz (Frobnitz *frobnitz, +# gint magic_number, +# GError **error) +# { +# ... +# } +# +# Becomes: +# +# gboolean +# frobnitz (Frobnitz *frobnitz, +# gint magic_number, +# GError **error) +# { +# ... +# } +# +# TODO: support "..." vararg parameter + +import argparse +import re +import sys + +from typing import NamedTuple + +# https://regexr.com/ is your friend. +functionNameRegex = re.compile(r'^(\w+) ?\(') +parameterRegex = re.compile( + r'^\s*(?P<type>(const\s+)?\w+)' + r'\s+(?P<stars>\**)' + r'\s*(?P<name>\w+)' + r'\s*(?P<end>,|\))' + r'\s*$') +openingCurlyBraceRegex = re.compile(r'^{\s*$') + + +def matchFunctionName(line): + match = functionNameRegex.match(line) + if match: + functionName = match.group(1) + firstParamPosition = match.end(0) + return (functionName, firstParamPosition) + return (None, 0) + + +class ParameterInfo(NamedTuple): + paramType: str + name: str + numStars: int + isLastParameter: bool + + +def matchParameter(line): + _, firstParamPosition = matchFunctionName(line) + match = parameterRegex.match(line[firstParamPosition:]) + if match is None: + return None + paramType = match.group('type') + assert(paramType is not None) + name = match.group('name') + assert(name is not None) + stars = match.group('stars') + numStars = len(stars) if stars is not None else 0 + end = match.group('end') + isLastParameter = True if end is not None and end == ')' else False + return ParameterInfo(paramType, name, numStars, isLastParameter) + + +def matchOpeningCurlyBrace(line): + return True if openingCurlyBraceRegex.match(line) is not None else False + + +# Length returned is number of lines the declaration takes up +def getFunctionDeclarationLength(remainingLines): + for i in range(len(remainingLines)): + currentLine = remainingLines[i] + parameterInfo = matchParameter(currentLine) + if parameterInfo is None: + return 0 + if parameterInfo.isLastParameter: + if i + 1 == len(remainingLines): + return 0 + nextLine = remainingLines[i + 1] + if not matchOpeningCurlyBrace(nextLine): + return 0 + return i + 1 + return 0 + + +def getParameterInfos(remainingLines, length): + parameterInfos = [] + for i in range(length): + parameterInfos.append(matchParameter(remainingLines[i])) + return parameterInfos + + +def computeSpacing(parameterInfos): + maxTypeLength = 0 + maxStarsLength = 0 + for parameterInfo in parameterInfos: + maxTypeLength = max(maxTypeLength, len(parameterInfo.paramType)) + maxStarsLength = max(maxStarsLength, parameterInfo.numStars) + return (maxTypeLength, maxStarsLength) + + +def printParameter(parameterInfo, maxTypeLength, maxStarsLength, outfile): + outfile.write(f'{parameterInfo.paramType:<{maxTypeLength + 1}}') + paramNamePaddedWithStars = f'{parameterInfo.name:*>{parameterInfo.numStars + len(parameterInfo.name)}}' + outfile.write(f'{paramNamePaddedWithStars:>{maxStarsLength + len(parameterInfo.name)}}') + + +def printFunctionDeclaration(remainingLines, length, useTabs, outfile): + functionName, _ = matchFunctionName(remainingLines[0]) + assert(functionName is not None) + outfile.write(f'{functionName} (') + numSpacesToParenthesis = len(functionName) + 2 + whitespace = '' + if useTabs: + tabs = ''.ljust(numSpacesToParenthesis // 8, '\t') + spaces = ''.ljust(numSpacesToParenthesis % 8) + whitespace = tabs + spaces + else: + whitespace = ''.ljust(numSpacesToParenthesis) + parameterInfos = getParameterInfos(remainingLines, length) + maxTypeLength, maxStarsLength = computeSpacing(parameterInfos) + numParameters = len(parameterInfos) + for i in range(numParameters): + parameterInfo = parameterInfos[i] + if i != 0: + outfile.write(whitespace) + printParameter(parameterInfo, maxTypeLength, maxStarsLength, outfile) + if i + 1 != numParameters: + outfile.write(',\n') + outfile.write(')\n') + + +def parseContents(infile, useTabs, outfile): + lines = infile.readlines() + i = 0 + while i < len(lines): + line = lines[i] + functionName, _ = matchFunctionName(line) + if functionName is None: + outfile.write(line) + i += 1 + continue + remainingLines = lines[i:] + length = getFunctionDeclarationLength(remainingLines) + if length == 0: + outfile.write(line) + i += 1 + continue + printFunctionDeclaration(remainingLines, length, useTabs, outfile) + i += length + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description='Line up parameters of C functions') + parser.add_argument('infile', nargs='?', + type=argparse.FileType('r'), + default=sys.stdin, + help='input C source file') + parser.add_argument('-o', metavar='outfile', nargs='?', + type=argparse.FileType('w'), + default=sys.stdout, + help='where to output modified file') + parser.add_argument('--tabs', action='store_true', + help='whether use tab characters in the output') + args = parser.parse_args() + parseContents(args.infile, args.tabs, args.o) + args.infile.close() + args.o.close() diff --git a/data/pre-commit-hook b/data/pre-commit-hook new file mode 100644 index 0000000..24b2ff2 --- /dev/null +++ b/data/pre-commit-hook @@ -0,0 +1,17 @@ +#!/bin/bash + +. "$(dirname -- "$0")/canonicalize_filename.sh" + + +# exit on error +set -e + +# Absolute path to this script +SCRIPT="$(canonicalize_filename "$0")" +# Absolute path this script is in, e.g. /home/user/bin/ +SCRIPTPATH="$(dirname -- "$SCRIPT")" + +echo $SCRIPTPATH +cd $SCRIPTPATH/../../ +data/check-code-style +cd -
\ No newline at end of file diff --git a/data/uncrustify.cfg b/data/uncrustify.cfg new file mode 100644 index 0000000..211c438 --- /dev/null +++ b/data/uncrustify.cfg @@ -0,0 +1,111 @@ +# indent using tabs +output_tab_size = 2 +indent_columns = output_tab_size +indent_with_tabs = 0 + +# indent case +indent_switch_case = indent_columns +indent_case_brace = 0 + +indent_ternary_operator = 2 + +# newlines +newlines = lf +nl_after_semicolon = true +nl_start_of_file = remove +nl_end_of_file = force +nl_end_of_file_min = 1 + +# spaces +sp_return_paren = force # "return (1);" vs "return(1);" +sp_sizeof_paren = force # "sizeof (int)" vs "sizeof(int)" +sp_assign = force +sp_arith = force +sp_bool = force +sp_compare = force +sp_after_comma = force +sp_case_label = force +sp_else_brace = force +sp_brace_else = force +sp_func_call_paren = force # "foo (" vs "foo(" +sp_func_proto_paren = force # "int foo ();" vs "int foo();" +sp_func_def_paren = force +sp_before_ptr_star = force +sp_after_ptr_star_qualifier = force # "const char * const" vs. "const char *const" +sp_after_ptr_star = remove +sp_between_ptr_star = remove # "**var" vs "* *var" +sp_inside_paren = remove # "( 1 )" vs "(1)" +sp_inside_fparen = remove # "( 1 )" vs "(1)" - functions +sp_inside_sparen = remove # "( 1 )" vs "(1)" - if/for/etc +sp_after_cast = remove # "(int) a" vs "(int)a" +sp_func_call_user_paren = remove # For gettext, "_()" vs. "_ ()" +set func_call_user _ N_ C_ # Needed for sp_after_cast +sp_before_semi = remove +sp_paren_paren = remove # Space between (( and )) + +eat_blanks_before_close_brace = true +eat_blanks_after_open_brace = true + +# K&R style for curly braces +nl_assign_brace = remove +nl_enum_brace = remove # "enum {" vs "enum \n {" +nl_union_brace = remove # "union {" vs "union \n {" +nl_struct_brace = remove # "struct {" vs "struct \n {" +nl_do_brace = remove # "do {" vs "do \n {" +nl_if_brace = remove # "if () {" vs "if () \n {" +nl_for_brace = remove # "for () {" vs "for () \n {" +nl_else_brace = remove # "else {" vs "else \n {" +nl_elseif_brace = remove # "else if {" vs "else if \n {" +nl_while_brace = remove # "while () {" vs "while () \n {" +nl_switch_brace = remove # "switch () {" vs "switch () \n {" +nl_before_case = false +nl_fcall_brace = add # "foo() {" vs "foo()\n{" +nl_fdef_brace = add # "int foo() {" vs "int foo()\n{" +nl_brace_while = remove +nl_brace_else = remove +nl_squeeze_ifdef = true +nl_case_colon_brace = remove +nl_after_brace_open = true + +# Function calls and parameters +nl_func_paren = remove +nl_func_def_paren = remove +nl_func_decl_start = remove +nl_func_def_start = remove +nl_func_decl_args = force +nl_func_def_args = force +nl_func_decl_end = remove +nl_func_def_end = remove +nl_func_type_name = force + +# Code modifying options (non-whitespace) +mod_remove_extra_semicolon = true + +# Align +align_func_params = true +align_single_line_func = true +align_var_def_star_style = 2 + +# one liners +nl_func_leave_one_liners = true +nl_enum_leave_one_liners = true +nl_assign_leave_one_liners = true + +# Comments +cmt_cpp_to_c = true # "/* */" vs. "//" +cmt_convert_tab_to_spaces = true +cmt_star_cont = true # Whether to put a star on subsequent comment lines +cmt_sp_after_star_cont = 1 # The number of spaces to insert after the star on subsequent comment lines +cmt_reflow_mode = 2 +cmt_c_nl_start = false # false/true +cmt_c_nl_end = true # false/true +# For multi-line comments with a '*' lead, remove leading spaces if the first and last lines of +# the comment are the same length. Default=True +cmt_multi_check_last = true +# Disable touching multi-line comments. +cmt_indent_multi = false + +# Encoding +utf8_bom = remove +utf8_force = true + |