From 4b147cf86bfd9bf735b551e1ec8174921f677a88 Mon Sep 17 00:00:00 2001 From: Diomidis Spinellis Date: Sat, 21 Dec 2024 12:25:57 +0200 Subject: [PATCH] Add -b (from beginning) option --- man/cscut.1 | 6 +++++- src/cscut.sh | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/man/cscut.1 b/man/cscut.1 index 7731844c..769c8e4b 100644 --- a/man/cscut.1 +++ b/man/cscut.1 @@ -1,4 +1,4 @@ -.TH CSCUT 1 "10 December 2024" +.TH CSCUT 1 "21 December 2024" .\" .\" (C) Copyright 2024 Diomidis Spinellis .\" @@ -21,6 +21,7 @@ cscut \- Extract portions of a CScout processing script .SH SYNOPSIS \fBcscut\fP +[\fB-b\fP] [\fB-e\fP] [\fB-F\fP \fIfile-list\fP | \fB-f\fP \fIfile\fP | \fB-p\fP \fIproject\fP] \fIcscout-file\fP @@ -32,6 +33,9 @@ script based on files or projects. .PP The following options are available: .TP +\fB-b\fP +Output from the file's beginning until the specified point. +.TP \fB-e\fP Output from the specified point to the end of the file. .TP diff --git a/src/cscut.sh b/src/cscut.sh index a0fbf0e3..c41d0054 100755 --- a/src/cscut.sh +++ b/src/cscut.sh @@ -28,8 +28,9 @@ set -eu usage() { cat <&2 -Usage: $(basename $0) [-e] (-F file-list|-f file|-p project) cscout-file +Usage: $(basename $0) [-be] (-F file-list|-f file|-p project) cscout-file + -b Output from the beginning to the specified point -e Output from specified point to the end -F file-list Process only the specified files contained in file-list file -f file Process only the specified file @@ -47,13 +48,22 @@ file_list='' # Non-empty if a project is being cut project='' +# Non-empty if files from the beginning are to be cut +from_beginning='' + # Non-empty if files until the end are to be cut to_end='' # Process command-line arguments -while getopts "eF:f:p:" opt; do +while getopts "beF:f:p:" opt; do case $opt in + b) + from_beginning=1 + ;; + e) + to_end=1 + ;; f) search="$OPTARG" file=1 @@ -61,9 +71,6 @@ while getopts "eF:f:p:" opt; do F) file_list="$OPTARG" ;; - e) - to_end=1 - ;; p) search="project $OPTARG" project=1 @@ -95,7 +102,11 @@ extract() local pattern=$(echo "$search" | sed 's/\([].\\*$^\/[]\)/\\\1/g') - local begin="/#pragma echo \"Processing $pattern\\\\n\"/" + if [ -n "$from_beginning" ]; then + local begin=1 + else + local begin="/#pragma echo \"Processing $pattern\\\\n\"/" + fi if [ -n "$to_end" ]; then local end=\$ @@ -106,7 +117,7 @@ extract() sed -n "${begin},${end}p" "$csfile" } -if [ -z "$project" ] ; then +if [ -z "$project" -a -z "$from_beginning" ]] ; then cat <<\EOF #pragma project "cscut" #pragma block_enter