~m-grant-prg/wutconv/jammy-trunk

« back to all changes in this revision

Viewing changes to source/progs/bash/wutconv.sh.in

  • Committer: Mark Grant
  • Date: 2018-02-10 16:07:49 UTC
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: m.grant.prg@gmail.com-20180210160749-8vfrsb94g2i8ns5s
Tags: upstream-1.0.9-4-gc3ad37f
ImportĀ upstreamĀ versionĀ 1.0.9-4-gc3ad37f

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env bash
2
 
#########################################################################
3
 
#                                                                       #
4
 
#       wutconv is automatically generated,                             #
5
 
#               please do not modify!                                   #
6
 
#                                                                       #
7
 
#########################################################################
8
 
 
9
 
#########################################################################
10
 
#                                                                       #
11
 
# Script ID: wutconv                                                    #
12
 
# Author: Copyright (C) 2014-2017  Mark Grant                           #
13
 
#                                                                       #
14
 
# Released under the GPLv3 only.                                        #
15
 
# SPDX-License-Identifier: GPL-3.0                                      #
16
 
#                                                                       #
17
 
# Purpose:                                                              #
18
 
# Converts one or more text files to windows or unix line endings, or,  #
19
 
# if no text files are specified on the command line, then stdin is     #
20
 
# converted to stdout.                                                  #
21
 
#                                                                       #
22
 
# Syntax:      wutconv [OPTIONS] [TextFileName] [TextFileName1] ...     #
23
 
#                       [OPTIONS] are:-                                 #
24
 
#       -h, --help Displays usage information.                          #
25
 
#       -u, --unix Convert to Unix line endings.                        #
26
 
#       -V, --version Displays version information.                     #
27
 
#       -w, --windows Convert to Windows line endings.                  #
28
 
#                                                                       #
29
 
# Exit Codes:   0 - success                                             #
30
 
#               1 - failure                                             #
31
 
#                                                                       #
32
 
#########################################################################
33
 
 
34
 
#########################################################################
35
 
#                                                                       #
36
 
# Changelog                                                             #
37
 
#                                                                       #
38
 
# Date          Author  Version Description                             #
39
 
#                                                                       #
40
 
# 04/09/2014    MG      1.0.1   First release.                          #
41
 
# 29/11/2014    MG      1.0.2   Switch to use GNU getopt to enable long #
42
 
#                               options.                                #
43
 
# 29/11/2014    MG      1.0.3   Add overall package version to -V.      #
44
 
# 01/12/2014    MG      1.0.4   Implement conversion of stdin to stdout #
45
 
#                               if no text files are specified on the   #
46
 
#                               command line.                           #
47
 
# 01/12/2014    MG      1.0.5   Implement verbose option. Default is    #
48
 
#                               now quiet - no status output.           #
49
 
# 01/12/2014    MG      1.0.6   Check that both -u and -w have not been #
50
 
#                               entered.                                #
51
 
# 26/06/2015    MG      1.0.7   Remove BSD support.                     #
52
 
# 23/10/2015    MG      1.0.8   Remove redundant osname look-up.        #
53
 
# 01/07/2017    MG      1.0.9   Enforce 80 column rule.                 #
54
 
# 02/12/2017    MG      1.0.10  Add SPDX license tags to source files.  #
55
 
#                               Adopt normal exit code policy; 0 on     #
56
 
#                               success, 1 on failure.                  #
57
 
#                                                                       #
58
 
#########################################################################
59
 
 
60
 
 
61
 
##################
62
 
# Init variables #
63
 
##################
64
 
script_exit_code=0
65
 
version="1.0.10"                        # set version variable
66
 
outputprefix="wutconv: "
67
 
packageversion=@pkgversion@             # Version of the complete package
68
 
 
69
 
unix=FALSE
70
 
windows=FALSE
71
 
verbosity=""
72
 
 
73
 
#############
74
 
# Functions #
75
 
#############
76
 
 
77
 
# Output $1 to stdout or stderr depending on $2
78
 
output()
79
 
{
80
 
        if [ $2 = 0 ]
81
 
        then
82
 
                if [ "$verbosity" = "-v" ]
83
 
                then
84
 
                        echo "$outputprefix$1"
85
 
                fi
86
 
        else
87
 
                echo "$outputprefix$1" 1>&2
88
 
        fi
89
 
}
90
 
 
91
 
# Standard function to test command error ($1 is $?) and exit if non-zero
92
 
std_cmd_err_handler()
93
 
{
94
 
        if [ $1 != 0 ]
95
 
        then
96
 
                script_exit_code=$1
97
 
                script_exit
98
 
        fi
99
 
}
100
 
 
101
 
# Standard function to tidy up and return exit code
102
 
script_exit()
103
 
{
104
 
        exit $script_exit_code
105
 
}
106
 
 
107
 
# Standard trap exit function
108
 
trap_exit()
109
 
{
110
 
script_exit_code=1
111
 
output "Script terminating due to trap received. Code: "$script_exit_code 1
112
 
script_exit
113
 
}
114
 
 
115
 
# Setup trap
116
 
trap trap_exit SIGHUP SIGINT SIGTERM
117
 
 
118
 
########
119
 
# Main #
120
 
########
121
 
# Process command line arguments with GNU getopt.
122
 
 
123
 
# Options u and w are mutually exclusive.
124
 
 
125
 
GETOPTTEMP=`getopt -o huvVw --long help,unix,verbose,version,windows \
126
 
                -n "$0" -- "$@"`
127
 
std_cmd_err_handler $?
128
 
 
129
 
eval set -- "$GETOPTTEMP"
130
 
std_cmd_err_handler $?
131
 
 
132
 
while true
133
 
do
134
 
        case "$1" in
135
 
        -h|--help)
136
 
                echo "Usage is $0 [OPTIONS]" TextFileName TextFileName1 ...
137
 
                echo "  [OPTIONS] are:-"
138
 
                echo "  '-h or --help' Displays usage information."
139
 
                echo "  '-u or --unix' Convert to Unix line endings."
140
 
                echo "  '-v or --verbose' verbose output"
141
 
                echo "  '-V or --version' Displays version information."
142
 
                echo "  '-w or --windows' Convert to Windows line endings."
143
 
                shift
144
 
                script_exit_code=0
145
 
                script_exit
146
 
                ;;
147
 
        -u|--unix)
148
 
                if [ $windows = TRUE ]
149
 
                then
150
 
                        script_exit_code=1
151
 
                        output "Cannot specify options u AND w." 1
152
 
                        script_exit
153
 
                fi
154
 
                unix=TRUE
155
 
                shift
156
 
                ;;
157
 
        -v|--verbose)   verbosity="-v"
158
 
                shift
159
 
                ;;
160
 
        -V|--version)
161
 
                echo "$0 Script version "$version
162
 
                echo "$0 Package version "$packageversion
163
 
                shift
164
 
                script_exit_code=0
165
 
                script_exit
166
 
                ;;
167
 
        -w|--windows)
168
 
                if [ $unix = TRUE ]
169
 
                then
170
 
                        script_exit_code=1
171
 
                        output "Cannot specify options w AND u." 1
172
 
                        script_exit
173
 
                fi
174
 
                windows=TRUE
175
 
                shift
176
 
                ;;
177
 
        --)     shift
178
 
                break
179
 
                ;;
180
 
        *)      script_exit_code=1
181
 
                output "Internal error." 1
182
 
                script_exit
183
 
                ;;
184
 
        esac
185
 
done
186
 
 
187
 
# Either u or w flag must be set.
188
 
if [ $windows = FALSE -a $unix = FALSE ]
189
 
then
190
 
        script_exit_code=1
191
 
        output "Either -u or -w must be set." 1
192
 
        script_exit
193
 
fi
194
 
 
195
 
# u and w flag cannot both be set.
196
 
if [ $windows = TRUE -a $unix = TRUE ]
197
 
then
198
 
        script_exit_code=1
199
 
        output "-u and -w cannot both be set." 1
200
 
        script_exit
201
 
fi
202
 
 
203
 
# If no cmd line filenames then use stdin and stdout.
204
 
if [ $# -lt 1 ]
205
 
then
206
 
        if [ $unix = TRUE ]
207
 
        then    # Convert to Unix line endings.
208
 
                awk '{if (substr($0,length,1)=="\r")
209
 
                        sub(/\r$/, "");
210
 
                        print;}'
211
 
        else    # Convert to Windows line endings.
212
 
                awk '{if (substr($0,length,1)!="\r")
213
 
                        sub(/$/, "\r");
214
 
                        print;}'
215
 
        fi
216
 
        # And exit
217
 
        script_exit_code=0
218
 
        output "Script complete with exit code: "$script_exit_code \
219
 
                $script_exit_code
220
 
        script_exit
221
 
fi
222
 
 
223
 
# Otherwise process cmd line filenames in sequence.
224
 
# The for line == for arg in $@
225
 
for arg
226
 
do
227
 
        if [ -f $arg ]  # Is it a regular file.
228
 
        then
229
 
                if [ -r $arg -a -w $arg ]       # Is it readable and writable.
230
 
                then
231
 
                        if [ $unix = TRUE ]
232
 
                        then    # Convert to Unix line endings.
233
 
                                awk '{if (substr($0,length,1)=="\r")
234
 
                                        sub(/\r$/, "");
235
 
                                        print;}' $arg > $arg.tmp \
236
 
                                        && mv -f $arg.tmp $arg
237
 
                                status=$?
238
 
                                msg="Converted file "$arg" to Unix format "
239
 
                                msg+="complete with status "$status
240
 
                                output "$msg" $status
241
 
                                ((script_exit_code=$script_exit_code + $status))
242
 
                        else    # Convert to Windows line endings.
243
 
                                awk '{if (substr($0,length,1)!="\r")
244
 
                                        sub(/$/, "\r");
245
 
                                        print;}' $arg > $arg.tmp \
246
 
                                        && mv -f $arg.tmp $arg
247
 
                                status=$?
248
 
                                msg="Converted file "$arg" to Windows format "
249
 
                                msg+="complete with status "$status
250
 
                                output "$msg" $status
251
 
                                ((script_exit_code=$script_exit_code + $status))
252
 
                        fi
253
 
                else
254
 
                        msg="File "$arg" does not have the correct permissions."
255
 
                        output "$msg" 1
256
 
                        ((script_exit_code=$script_exit_code + 1))
257
 
                fi
258
 
        else
259
 
                output "File "$arg" does not exist or is not a regular file." 1
260
 
                ((script_exit_code=$script_exit_code + 1))
261
 
        fi
262
 
done
263
 
 
264
 
 
265
 
# And exit
266
 
output "Script complete with exit code: "$script_exit_code $script_exit_code
267
 
script_exit