16afeb397SPaolo Bonzini#! /bin/sh
26afeb397SPaolo Bonzini#
3*93a11007SAlex Bennée# Fix multiline comments to match docs/devel/style.rst
46afeb397SPaolo Bonzini#
56afeb397SPaolo Bonzini# Copyright (C) 2018 Red Hat, Inc.
66afeb397SPaolo Bonzini#
76afeb397SPaolo Bonzini# Author: Paolo Bonzini
86afeb397SPaolo Bonzini#
96afeb397SPaolo Bonzini# Usage: scripts/fix-multiline-comments.sh [-i] FILE...
106afeb397SPaolo Bonzini#
116afeb397SPaolo Bonzini# -i edits the file in place (requires gawk 4.1.0).
126afeb397SPaolo Bonzini#
136afeb397SPaolo Bonzini# Set the AWK environment variable to choose the awk interpreter to use
146afeb397SPaolo Bonzini# (default 'awk')
156afeb397SPaolo Bonzini
166afeb397SPaolo Bonziniif test "$1" = -i; then
176afeb397SPaolo Bonzini  # gawk extension
186afeb397SPaolo Bonzini  inplace="-i inplace"
196afeb397SPaolo Bonzini  shift
206afeb397SPaolo Bonzinifi
216afeb397SPaolo Bonzini${AWK-awk} $inplace 'BEGIN { indent = -1 }
226afeb397SPaolo Bonzini{
236afeb397SPaolo Bonzini    line = $0
246afeb397SPaolo Bonzini    # apply a star to the indent on lines after the first
256afeb397SPaolo Bonzini    if (indent != -1) {
266afeb397SPaolo Bonzini        if (line == "") {
276afeb397SPaolo Bonzini            line = sp " *"
286afeb397SPaolo Bonzini        } else if (substr(line, 1, indent + 2) == sp "  ") {
296afeb397SPaolo Bonzini            line = sp " *" substr(line, indent + 3)
306afeb397SPaolo Bonzini        }
316afeb397SPaolo Bonzini    }
326afeb397SPaolo Bonzini
336afeb397SPaolo Bonzini    is_lead = (line ~ /^[ \t]*\/\*/)
346afeb397SPaolo Bonzini    is_trail = (line ~ /\*\//)
356afeb397SPaolo Bonzini    if (is_lead && !is_trail) {
366afeb397SPaolo Bonzini        # grab the indent at the start of a comment, but not for
376afeb397SPaolo Bonzini        # single-line comments
386afeb397SPaolo Bonzini        match(line, /^[ \t]*\/\*/)
396afeb397SPaolo Bonzini        indent = RLENGTH - 2
406afeb397SPaolo Bonzini        sp = substr(line, 1, indent)
416afeb397SPaolo Bonzini    }
426afeb397SPaolo Bonzini
436afeb397SPaolo Bonzini    # the regular expression filters out lone /*, /**, or */
446afeb397SPaolo Bonzini    if (indent != -1 && !(line ~ /^[ \t]*(\/\*+|\*\/)[ \t]*$/)) {
456afeb397SPaolo Bonzini        if (is_lead) {
466afeb397SPaolo Bonzini            # split the leading /* or /** on a separate line
476afeb397SPaolo Bonzini            match(line, /^[ \t]*\/\*+/)
486afeb397SPaolo Bonzini            lead = substr(line, 1, RLENGTH)
496afeb397SPaolo Bonzini            match(line, /^[ \t]*\/\*+[ \t]*/)
506afeb397SPaolo Bonzini            line = lead "\n" sp " *" substr(line, RLENGTH)
516afeb397SPaolo Bonzini        }
526afeb397SPaolo Bonzini        if (is_trail) {
536afeb397SPaolo Bonzini            # split the trailing */ on a separate line
546afeb397SPaolo Bonzini            match(line, /[ \t]*\*\//)
556afeb397SPaolo Bonzini            line = substr(line, 1, RSTART - 1) "\n" sp " */"
566afeb397SPaolo Bonzini        }
576afeb397SPaolo Bonzini    }
586afeb397SPaolo Bonzini    if (is_trail) {
596afeb397SPaolo Bonzini        indent = -1
606afeb397SPaolo Bonzini    }
616afeb397SPaolo Bonzini    print line
626afeb397SPaolo Bonzini}' "$@"
63