File: fix-multiline-comments.sh

package info (click to toggle)
qemu 1%3A10.0.3%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 413,680 kB
  • sloc: ansic: 4,733,433; pascal: 114,769; python: 105,506; asm: 68,431; sh: 52,881; makefile: 27,469; perl: 18,778; cpp: 11,435; xml: 3,404; objc: 2,877; yacc: 2,505; php: 1,299; tcl: 1,296; lex: 1,110; sql: 71; awk: 43; sed: 35; javascript: 7
file content (62 lines) | stat: -rwxr-xr-x 1,714 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /bin/sh
#
# Fix multiline comments to match docs/devel/style.rst
#
# Copyright (C) 2018 Red Hat, Inc.
#
# Author: Paolo Bonzini
#
# Usage: scripts/fix-multiline-comments.sh [-i] FILE...
#
# -i edits the file in place (requires gawk 4.1.0).
#
# Set the AWK environment variable to choose the awk interpreter to use
# (default 'awk')

if test "$1" = -i; then
  # gawk extension
  inplace="-i inplace"
  shift
fi
${AWK-awk} $inplace 'BEGIN { indent = -1 }
{
    line = $0
    # apply a star to the indent on lines after the first
    if (indent != -1) {
        if (line == "") {
            line = sp " *"
        } else if (substr(line, 1, indent + 2) == sp "  ") {
            line = sp " *" substr(line, indent + 3)
        }
    }

    is_lead = (line ~ /^[ \t]*\/\*/)
    is_trail = (line ~ /\*\//)
    if (is_lead && !is_trail) {
        # grab the indent at the start of a comment, but not for
        # single-line comments
        match(line, /^[ \t]*\/\*/)
        indent = RLENGTH - 2
        sp = substr(line, 1, indent)
    }

    # the regular expression filters out lone /*, /**, or */
    if (indent != -1 && !(line ~ /^[ \t]*(\/\*+|\*\/)[ \t]*$/)) {
        if (is_lead) {
            # split the leading /* or /** on a separate line
            match(line, /^[ \t]*\/\*+/)
            lead = substr(line, 1, RLENGTH)
            match(line, /^[ \t]*\/\*+[ \t]*/)
            line = lead "\n" sp " *" substr(line, RLENGTH)
        }
        if (is_trail) {
            # split the trailing */ on a separate line
            match(line, /[ \t]*\*\//)
            line = substr(line, 1, RSTART - 1) "\n" sp " */"
        }
    }
    if (is_trail) {
        indent = -1
    }
    print line
}' "$@"