Skip to content

[PATCH] Loses a few pipes, passes shellcheck #5

@kseistrup

Description

@kseistrup

The attached patch …

  • adds SPDX tag SPDX-FileCopyrightText,
  • explicitly disables irrelevant shellcheck checks,
  • loses a few pipes,
  • loses seemingly unnecessay call to sed in favour of ${variable//search/replace}, and
  • double-quotes a few command substitutions, just to be on the safe side.

📎 shellcheck.diff.txt

It passes shellcheck and seems to work just fine here, but somebody should probably check it on their side.


Overview:

diff --git a/man.filter.dpi b/man.filter.dpi
index 5b9adbc..bc0fb3d 100755
--- a/man.filter.dpi
+++ b/man.filter.dpi
@@ -1,13 +1,14 @@
 #!/bin/bash
-# Copyright (c) 2023 Rodrigo Arias Mallo
+# SPDX-FileCopyrightText: Copyright (c) 2023 Rodrigo Arias Mallo
 # SPDX-License-Identifier: GPL-3.0-or-later
 
-IFS= read -d '>' auth # Ignore auth
-IFS= read -d '>' cmd
+# shellcheck disable=SC2034
+IFS= read -rd '>' auth # Ignore auth
+IFS= read -rd '>' cmd
 
 case "$cmd" in
   "<cmd='open_url' url='"*);;
-  *) echo $cmd; exit;;
+  *) echo "$cmd"; exit;;
 esac
 
 url=${cmd#"<cmd='open_url' url='"}
@@ -17,20 +18,20 @@ serve_404() {
   printf "<cmd='start_send_page' url='' '>\n"
   printf "Content-type: text/plain\r\n\r\n"
   manpage="$1"
-  apropos="$2"
-  if [ -z "$1" ]; then
+  test -n "$manpage" || {
     echo "Not found"
     exit 0
-  fi
-  echo "Manual page not found: $1"
-  if [ -n "$apropos" ]; then
+  }
+  apropos="$2"
+  echo "Manual page not found: $manpage"
+  test -n "$apropos" && {
     printf "\nRelated pages:\n"
     man -k "$apropos"
-  fi
+  }
 }
 
 inject_css() {
-  css=$(cat "$(dirname $(readlink -f $0))/style.css" | tr '\n' ' ')
+  css=$(tr '\n' ' ' < "$(dirname "$(readlink -f "$0")")/style.css")
   sed "s_</style>_$css\n</style>_"
 }
 
@@ -54,19 +55,23 @@ serve_manpage() {
   url="$1"
   ref="${url#"man:"}"
   # Reverse open(3) -> 3 open if given
-  manpage=$(echo "$ref" | sed 's/\(.*\)(\(.*\))/\2 \1/')
+  # shellcheck disable=SC2001
+  manpage=$(sed 's/\(.*\)(\(.*\))/\2 \1/' <<< "$ref")
   # If page not found, return 404
-  if [ -z "$manpage" ]; then
+  test -z "$manpage" && {
     serve_404
     exit 0
-  fi
+  }
+  # $manpage may contain both man section and man page
+  # shellcheck disable=SC2086
   if ! man -w $manpage 2>/dev/null; then
-    apropos=$(echo "$ref" | sed 's/(.*)//')
+    apropos="${ref%%(*}"
     serve_404 "$manpage" "$apropos"
   else
     printf "<cmd='start_send_page' url='' '>\n"
     printf "Content-type: text/html\r\n\r\n"
     unset MANROFFOPT
+    # shellcheck disable=SC2086
     man -Thtml $manpage | fix_br | inject_css | link_xrefs 2>&1
   fi
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions