{-
    Copyright 2012-2020 Vidar Holen

    This file is part of ShellCheck.
    https://www.shellcheck.net

    ShellCheck is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ShellCheck is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
module ShellCheck.Checks.ShellSupport (checker , ShellCheck.Checks.ShellSupport.runTests) where

import ShellCheck.AST
import ShellCheck.ASTLib
import ShellCheck.AnalyzerLib
import ShellCheck.Interface
import ShellCheck.Prelude
import ShellCheck.Regex

import Control.Monad
import Control.Monad.RWS
import Data.Char
import Data.Functor.Identity
import Data.List
import Data.Maybe
import qualified Data.Map as Map
import qualified Data.Set as Set
import Test.QuickCheck.All (forAllProperties)
import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)

data ForShell = ForShell [Shell] (Token -> Analysis)

getChecker :: Parameters -> [ForShell] -> Checker
getChecker Parameters
params [ForShell]
list = Checker {
        perScript :: Root -> Analysis
perScript = Root -> Analysis
forall {b}. b -> Analysis
nullCheck,
        perToken :: Token -> Analysis
perToken = ((Token -> Analysis) -> (Token -> Analysis) -> Token -> Analysis)
-> (Token -> Analysis) -> [Token -> Analysis] -> Token -> Analysis
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (Token -> Analysis) -> (Token -> Analysis) -> Token -> Analysis
forall a. (a -> Analysis) -> (a -> Analysis) -> a -> Analysis
composeAnalyzers Token -> Analysis
forall {b}. b -> Analysis
nullCheck ([Token -> Analysis] -> Token -> Analysis)
-> [Token -> Analysis] -> Token -> Analysis
forall a b. (a -> b) -> a -> b
$ (ForShell -> Maybe (Token -> Analysis))
-> [ForShell] -> [Token -> Analysis]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ForShell -> Maybe (Token -> Analysis)
forall {m :: * -> *}.
(Monad m, Alternative m) =>
ForShell -> m (Token -> Analysis)
include [ForShell]
list
    }
  where
    shell :: Shell
shell = Parameters -> Shell
shellType Parameters
params
    include :: ForShell -> m (Token -> Analysis)
include (ForShell [Shell]
list Token -> Analysis
a) = do
        Bool -> m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> m ()) -> Bool -> m ()
forall a b. (a -> b) -> a -> b
$ Shell
shell Shell -> [Shell] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Shell]
list
        (Token -> Analysis) -> m (Token -> Analysis)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Token -> Analysis
a

checker :: Parameters -> Checker
checker Parameters
params = Parameters -> [ForShell] -> Checker
getChecker Parameters
params [ForShell]
checks

checks :: [ForShell]
checks = [
    ForShell
checkForDecimals
    ,ForShell
checkBashisms
    ,ForShell
checkEchoSed
    ,ForShell
checkBraceExpansionVars
    ,ForShell
checkMultiDimensionalArrays
    ,ForShell
checkPS1Assignments
    ,ForShell
checkMultipleBangs
    ,ForShell
checkBangAfterPipe
    ,ForShell
checkNegatedUnaryOps
    ]

testChecker :: ForShell -> Checker
testChecker (ForShell [Shell]
_ Token -> Analysis
t) =
    Checker {
        perScript :: Root -> Analysis
perScript = Root -> Analysis
forall {b}. b -> Analysis
nullCheck,
        perToken :: Token -> Analysis
perToken = Token -> Analysis
t
    }
verify :: ForShell -> [Char] -> Bool
verify ForShell
c [Char]
s = Checker -> [Char] -> Maybe Bool
producesComments (ForShell -> Checker
testChecker ForShell
c) [Char]
s Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
verifyNot :: ForShell -> [Char] -> Bool
verifyNot ForShell
c [Char]
s = Checker -> [Char] -> Maybe Bool
producesComments (ForShell -> Checker
testChecker ForShell
c) [Char]
s Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False

prop_checkForDecimals1 :: Bool
prop_checkForDecimals1 = ForShell -> [Char] -> Bool
verify ForShell
checkForDecimals [Char]
"((3.14*c))"
prop_checkForDecimals2 :: Bool
prop_checkForDecimals2 = ForShell -> [Char] -> Bool
verify ForShell
checkForDecimals [Char]
"foo[1.2]=bar"
prop_checkForDecimals3 :: Bool
prop_checkForDecimals3 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkForDecimals [Char]
"declare -A foo; foo[1.2]=bar"
checkForDecimals :: ForShell
checkForDecimals = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Sh, Shell
Dash, Shell
BusyboxSh, Shell
Bash] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> m ()
f t :: Token
t@(TA_Expansion Id
id [Token]
_) = Maybe (m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ (Maybe (m ()) -> m ()) -> Maybe (m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ do
        first:rest <- Token -> Maybe [Char]
getLiteralString Token
t
        guard $ isDigit first && '.' `elem` rest
        return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
    f Token
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


prop_checkBashisms :: Bool
prop_checkBashisms = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"while read a; do :; done < <(a)"
prop_checkBashisms2 :: Bool
prop_checkBashisms2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"[ foo -nt bar ]"
prop_checkBashisms3 :: Bool
prop_checkBashisms3 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo $((i++))"
prop_checkBashisms4 :: Bool
prop_checkBashisms4 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"rm !(*.hs)"
prop_checkBashisms5 :: Bool
prop_checkBashisms5 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"source file"
prop_checkBashisms6 :: Bool
prop_checkBashisms6 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"[ \"$a\" == 42 ]"
prop_checkBashisms6b :: Bool
prop_checkBashisms6b = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"test \"$a\" == 42"
prop_checkBashisms6c :: Bool
prop_checkBashisms6c = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"[ foo =~ bar ]"
prop_checkBashisms6d :: Bool
prop_checkBashisms6d = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"test foo =~ bar"
prop_checkBashisms7 :: Bool
prop_checkBashisms7 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${var[1]}"
prop_checkBashisms8 :: Bool
prop_checkBashisms8 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${!var[@]}"
prop_checkBashisms9 :: Bool
prop_checkBashisms9 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${!var*}"
prop_checkBashisms10 :: Bool
prop_checkBashisms10 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${var:4:12}"
prop_checkBashisms11 :: Bool
prop_checkBashisms11 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"echo ${var:-4}"
prop_checkBashisms12 :: Bool
prop_checkBashisms12 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${var//foo/bar}"
prop_checkBashisms13 :: Bool
prop_checkBashisms13 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"exec -c env"
prop_checkBashisms14 :: Bool
prop_checkBashisms14 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo -n \"Foo: \""
prop_checkBashisms15 :: Bool
prop_checkBashisms15 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"let n++"
prop_checkBashisms16 :: Bool
prop_checkBashisms16 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo $RANDOM"
prop_checkBashisms17 :: Bool
prop_checkBashisms17 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo $((RANDOM%6+1))"
prop_checkBashisms18 :: Bool
prop_checkBashisms18 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"foo &> /dev/null"
prop_checkBashisms19 :: Bool
prop_checkBashisms19 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"foo > file*.txt"
prop_checkBashisms20 :: Bool
prop_checkBashisms20 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"read -ra foo"
prop_checkBashisms21 :: Bool
prop_checkBashisms21 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"[ -a foo ]"
prop_checkBashisms21b :: Bool
prop_checkBashisms21b = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"test -a foo"
prop_checkBashisms22 :: Bool
prop_checkBashisms22 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"[ foo -a bar ]"
prop_checkBashisms23 :: Bool
prop_checkBashisms23 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"trap mything ERR INT"
prop_checkBashisms24 :: Bool
prop_checkBashisms24 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"trap mything INT TERM"
prop_checkBashisms25 :: Bool
prop_checkBashisms25 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"cat < /dev/tcp/host/123"
prop_checkBashisms26 :: Bool
prop_checkBashisms26 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"trap mything ERR SIGTERM"
prop_checkBashisms27 :: Bool
prop_checkBashisms27 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo *[^0-9]*"
prop_checkBashisms28 :: Bool
prop_checkBashisms28 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"exec {n}>&2"
prop_checkBashisms29 :: Bool
prop_checkBashisms29 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo ${!var}"
prop_checkBashisms30 :: Bool
prop_checkBashisms30 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"printf -v '%s' \"$1\""
prop_checkBashisms31 :: Bool
prop_checkBashisms31 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"printf '%q' \"$1\""
prop_checkBashisms32 :: Bool
prop_checkBashisms32 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\n[ foo -nt bar ]"
prop_checkBashisms33 :: Bool
prop_checkBashisms33 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho -n foo"
prop_checkBashisms34 :: Bool
prop_checkBashisms34 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\necho -n foo"
prop_checkBashisms35 :: Bool
prop_checkBashisms35 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\nlocal foo"
prop_checkBashisms36 :: Bool
prop_checkBashisms36 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\nread -p foo -r bar"
prop_checkBashisms37 :: Bool
prop_checkBashisms37 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"HOSTNAME=foo; echo $HOSTNAME"
prop_checkBashisms38 :: Bool
prop_checkBashisms38 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"RANDOM=9; echo $RANDOM"
prop_checkBashisms39 :: Bool
prop_checkBashisms39 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"foo-bar() { true; }"
prop_checkBashisms40 :: Bool
prop_checkBashisms40 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo $(<file)"
prop_checkBashisms41 :: Bool
prop_checkBashisms41 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"echo `<file`"
prop_checkBashisms42 :: Bool
prop_checkBashisms42 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"trap foo int"
prop_checkBashisms43 :: Bool
prop_checkBashisms43 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"trap foo sigint"
prop_checkBashisms44 :: Bool
prop_checkBashisms44 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\ntrap foo int"
prop_checkBashisms45 :: Bool
prop_checkBashisms45 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\ntrap foo INT"
prop_checkBashisms46 :: Bool
prop_checkBashisms46 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\ntrap foo SIGINT"
prop_checkBashisms47 :: Bool
prop_checkBashisms47 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\necho foo 42>/dev/null"
prop_checkBashisms48 :: Bool
prop_checkBashisms48 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\necho $LINENO"
prop_checkBashisms49 :: Bool
prop_checkBashisms49 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\necho $MACHTYPE"
prop_checkBashisms50 :: Bool
prop_checkBashisms50 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\ncmd >& file"
prop_checkBashisms51 :: Bool
prop_checkBashisms51 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\ncmd 2>&1"
prop_checkBashisms52 :: Bool
prop_checkBashisms52 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\ncmd >&2"
prop_checkBashisms52b :: Bool
prop_checkBashisms52b = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\ncmd >& $var"
prop_checkBashisms52c :: Bool
prop_checkBashisms52c = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\ncmd >& $dir/$var"
prop_checkBashisms53 :: Bool
prop_checkBashisms53 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nprintf -- -f\n"
prop_checkBashisms54 :: Bool
prop_checkBashisms54 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nfoo+=bar"
prop_checkBashisms55 :: Bool
prop_checkBashisms55 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho ${@%foo}"
prop_checkBashisms56 :: Bool
prop_checkBashisms56 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\necho ${##}"
prop_checkBashisms57 :: Bool
prop_checkBashisms57 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\nulimit -c 0"
prop_checkBashisms58 :: Bool
prop_checkBashisms58 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nulimit -c 0"
prop_checkBashisms59 :: Bool
prop_checkBashisms59 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\njobs -s"
prop_checkBashisms60 :: Bool
prop_checkBashisms60 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\njobs -p"
prop_checkBashisms61 :: Bool
prop_checkBashisms61 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\njobs -lp"
prop_checkBashisms62 :: Bool
prop_checkBashisms62 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nexport -f foo"
prop_checkBashisms63 :: Bool
prop_checkBashisms63 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nexport -p"
prop_checkBashisms64 :: Bool
prop_checkBashisms64 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nreadonly -a"
prop_checkBashisms65 :: Bool
prop_checkBashisms65 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nreadonly -p"
prop_checkBashisms66 :: Bool
prop_checkBashisms66 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\ncd -P ."
prop_checkBashisms67 :: Bool
prop_checkBashisms67 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\ncd -P -e ."
prop_checkBashisms68 :: Bool
prop_checkBashisms68 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\numask -p"
prop_checkBashisms69 :: Bool
prop_checkBashisms69 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\numask -S"
prop_checkBashisms70 :: Bool
prop_checkBashisms70 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\ntrap -l"
prop_checkBashisms71 :: Bool
prop_checkBashisms71 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\ntype -a ls"
prop_checkBashisms72 :: Bool
prop_checkBashisms72 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\ntype ls"
prop_checkBashisms73 :: Bool
prop_checkBashisms73 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nunset -n namevar"
prop_checkBashisms74 :: Bool
prop_checkBashisms74 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nunset -f namevar"
prop_checkBashisms75 :: Bool
prop_checkBashisms75 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\necho \"-n foo\""
prop_checkBashisms76 :: Bool
prop_checkBashisms76 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\necho \"-ne foo\""
prop_checkBashisms77 :: Bool
prop_checkBashisms77 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\necho -Q foo"
prop_checkBashisms78 :: Bool
prop_checkBashisms78 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho -ne foo"
prop_checkBashisms79 :: Bool
prop_checkBashisms79 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nhash -l"
prop_checkBashisms80 :: Bool
prop_checkBashisms80 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nhash -r"
prop_checkBashisms81 :: Bool
prop_checkBashisms81 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\nhash -v"
prop_checkBashisms82 :: Bool
prop_checkBashisms82 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nset -v +o allexport -o errexit -C"
prop_checkBashisms83 :: Bool
prop_checkBashisms83 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nset --"
prop_checkBashisms84 :: Bool
prop_checkBashisms84 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nset -o pipefail"
prop_checkBashisms85 :: Bool
prop_checkBashisms85 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nset -B"
prop_checkBashisms86 :: Bool
prop_checkBashisms86 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\nset -o emacs"
prop_checkBashisms87 :: Bool
prop_checkBashisms87 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nset -o emacs"
prop_checkBashisms88 :: Bool
prop_checkBashisms88 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nset -- wget -o foo 'https://some.url'"
prop_checkBashisms89 :: Bool
prop_checkBashisms89 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nopts=$-\nset -\"$opts\""
prop_checkBashisms90 :: Bool
prop_checkBashisms90 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\nset -o \"$opt\""
prop_checkBashisms91 :: Bool
prop_checkBashisms91 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\nwait -n"
prop_checkBashisms92 :: Bool
prop_checkBashisms92 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho $((16#FF))"
prop_checkBashisms93 :: Bool
prop_checkBashisms93 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho $(( 10#$(date +%m) ))"
prop_checkBashisms94 :: Bool
prop_checkBashisms94 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\n[ -v var ]"
prop_checkBashisms95 :: Bool
prop_checkBashisms95 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho $_"
prop_checkBashisms96 :: Bool
prop_checkBashisms96 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\necho $_"
prop_checkBashisms97 :: Bool
prop_checkBashisms97 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho ${var,}"
prop_checkBashisms98 :: Bool
prop_checkBashisms98 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\necho ${var^^}"
prop_checkBashisms99 :: Bool
prop_checkBashisms99 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\necho [^f]oo"
prop_checkBashisms100 :: Bool
prop_checkBashisms100 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"read -r"
prop_checkBashisms101 :: Bool
prop_checkBashisms101 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"read"
prop_checkBashisms102 :: Bool
prop_checkBashisms102 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"read -r foo"
prop_checkBashisms103 :: Bool
prop_checkBashisms103 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"read foo"
prop_checkBashisms104 :: Bool
prop_checkBashisms104 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"read ''"
prop_checkBashisms105 :: Bool
prop_checkBashisms105 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nset -o pipefail"
prop_checkBashisms106 :: Bool
prop_checkBashisms106 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx=x\n[[ \"$x\" = \"$x\" ]]"
prop_checkBashisms107 :: Bool
prop_checkBashisms107 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx=x\n[ \"$x\" == \"$x\" ]"
prop_checkBashisms108 :: Bool
prop_checkBashisms108 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\necho magic &> /dev/null"
prop_checkBashisms109 :: Bool
prop_checkBashisms109 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\ntrap stop EXIT SIGTERM"
prop_checkBashisms110 :: Bool
prop_checkBashisms110 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nsource /dev/null"
prop_checkBashisms111 :: Bool
prop_checkBashisms111 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\nx='test'\n${x:0:3}" -- SC3057
prop_checkBashisms112 :: Bool
prop_checkBashisms112 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${x:0:3}" -- SC3057
prop_checkBashisms113 :: Bool
prop_checkBashisms113 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\nx='test'\n${x/st/xt}" -- SC3060
prop_checkBashisms114 :: Bool
prop_checkBashisms114 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${x/st/xt}" -- SC3060
prop_checkBashisms115 :: Bool
prop_checkBashisms115 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${!x}" -- SC3053
prop_checkBashisms116 :: Bool
prop_checkBashisms116 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${x[1]}" -- SC3054
prop_checkBashisms117 :: Bool
prop_checkBashisms117 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${!x[@]}" -- SC3055
prop_checkBashisms118 :: Bool
prop_checkBashisms118 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nxyz=1\n${!x*}" -- SC3056
prop_checkBashisms119 :: Bool
prop_checkBashisms119 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nx='test'\n${x^^[t]}" -- SC3059
prop_checkBashisms120 :: Bool
prop_checkBashisms120 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\n[ x == y ]"
prop_checkBashisms121 :: Bool
prop_checkBashisms121 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/sh\n# shellcheck shell=busybox\n[ x == y ]"
prop_checkBashisms122 :: Bool
prop_checkBashisms122 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\n$'a'"
prop_checkBashisms123 :: Bool
prop_checkBashisms123 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\n$'a'"
prop_checkBashisms124 :: Bool
prop_checkBashisms124 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\ntype -p test"
prop_checkBashisms125 :: Bool
prop_checkBashisms125 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\ntype -p test"
prop_checkBashisms126 :: Bool
prop_checkBashisms126 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\nread -p foo -r bar"
prop_checkBashisms127 :: Bool
prop_checkBashisms127 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/busybox sh\necho -ne foo"
prop_checkBashisms128 :: Bool
prop_checkBashisms128 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/dash\ntype -p test"
prop_checkBashisms129 :: Bool
prop_checkBashisms129 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\n[ -k /tmp ]"
prop_checkBashisms130 :: Bool
prop_checkBashisms130 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBashisms [Char]
"#!/bin/dash\ntest -k /tmp"
prop_checkBashisms131 :: Bool
prop_checkBashisms131 = ForShell -> [Char] -> Bool
verify ForShell
checkBashisms [Char]
"#!/bin/sh\n[ -o errexit ]"
checkBashisms :: ForShell
checkBashisms = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Sh, Shell
Dash, Shell
BusyboxSh] ((Token -> Analysis) -> ForShell)
-> (Token -> Analysis) -> ForShell
forall a b. (a -> b) -> a -> b
$ \Token
t -> do
    params <- RWST Parameters [TokenComment] Cache Identity Parameters
forall r (m :: * -> *). MonadReader r m => m r
ask
    kludge params t
 where
  -- This code was copy-pasted from Analytics where params was a variable
  kludge :: Parameters -> Token -> m ()
kludge Parameters
params = Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
bashism
   where
    isBusyboxSh :: Bool
isBusyboxSh = Parameters -> Shell
shellType Parameters
params Shell -> Shell -> Bool
forall a. Eq a => a -> a -> Bool
== Shell
BusyboxSh
    isDash :: Bool
isDash = Parameters -> Shell
shellType Parameters
params Shell -> Shell -> Bool
forall a. Eq a => a -> a -> Bool
== Shell
Dash Bool -> Bool -> Bool
|| Bool
isBusyboxSh
    warnMsg :: Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
code [Char]
s =
        if Bool
isDash
        then Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
err  Id
id Integer
code ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"In dash, " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" not supported."
        else Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warn Id
id Integer
code ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"In POSIX sh, " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" undefined."
    asStr :: Token -> Maybe [Char]
asStr = Token -> Maybe [Char]
getLiteralString

    bashism :: Token -> m ()
bashism (T_ProcSub Id
id [Char]
_ [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3001 [Char]
"process substitution is"
    bashism (T_Extglob Id
id [Char]
_ [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3002 [Char]
"extglob is"
    bashism (T_DollarSingleQuoted Id
id [Char]
_) =
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBusyboxSh (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3003 [Char]
"$'..' is"
    bashism (T_DollarDoubleQuoted Id
id [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3004 [Char]
"$\"..\" is"
    bashism (T_ForArithmetic Id
id Token
_ Token
_ Token
_ [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3005 [Char]
"arithmetic for loops are"
    bashism (T_Arithmetic Id
id Token
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3006 [Char]
"standalone ((..)) is"
    bashism (T_DollarBracket Id
id Token
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3007 [Char]
"$[..] in place of $((..)) is"
    bashism (T_SelectIn Id
id [Char]
_ [Token]
_ [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3008 [Char]
"select loops are"
    bashism (T_BraceExpansion Id
id [Token]
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3009 [Char]
"brace expansion is"
    bashism (T_Condition Id
id ConditionType
DoubleBracket Token
_) =
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBusyboxSh (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3010 [Char]
"[[ ]] is"
    bashism (T_HereString Id
id Token
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3011 [Char]
"here-strings are"

    bashism (TC_Binary Id
id ConditionType
_ [Char]
op Token
_ Token
_) =
        Map [Char] (Integer, [Shell], [Char] -> [Char])
-> [Char] -> Id -> m ()
forall {m :: * -> *} {t} {t :: * -> *}.
(Ord t, Foldable t, MonadWriter [TokenComment] m) =>
Map t (Integer, t Shell, t -> [Char]) -> t -> Id -> m ()
checkTestOp Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismBinaryTestFlags [Char]
op Id
id
    bashism (T_SimpleCommand Id
id [Token]
_ [Token -> Maybe [Char]
asStr -> Just [Char]
"test", Token
lhs, Token -> Maybe [Char]
asStr -> Just [Char]
op, Token
rhs]) =
        Map [Char] (Integer, [Shell], [Char] -> [Char])
-> [Char] -> Id -> m ()
forall {m :: * -> *} {t} {t :: * -> *}.
(Ord t, Foldable t, MonadWriter [TokenComment] m) =>
Map t (Integer, t Shell, t -> [Char]) -> t -> Id -> m ()
checkTestOp Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismBinaryTestFlags [Char]
op Id
id
    bashism (TC_Unary Id
id ConditionType
_ [Char]
op Token
_) =
        Map [Char] (Integer, [Shell], [Char] -> [Char])
-> [Char] -> Id -> m ()
forall {m :: * -> *} {t} {t :: * -> *}.
(Ord t, Foldable t, MonadWriter [TokenComment] m) =>
Map t (Integer, t Shell, t -> [Char]) -> t -> Id -> m ()
checkTestOp Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismUnaryTestFlags [Char]
op Id
id
    bashism (T_SimpleCommand Id
id [Token]
_ [Token -> Maybe [Char]
asStr -> Just [Char]
"test", Token -> Maybe [Char]
asStr -> Just [Char]
op, Token
_]) =
        Map [Char] (Integer, [Shell], [Char] -> [Char])
-> [Char] -> Id -> m ()
forall {m :: * -> *} {t} {t :: * -> *}.
(Ord t, Foldable t, MonadWriter [TokenComment] m) =>
Map t (Integer, t Shell, t -> [Char]) -> t -> Id -> m ()
checkTestOp Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismUnaryTestFlags [Char]
op Id
id

    bashism (TA_Unary Id
id [Char]
op Token
_)
        | [Char]
op [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ [Char]
"|++", [Char]
"|--", [Char]
"++|", [Char]
"--|"] =
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3018 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'|') [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is"
    bashism (TA_Binary Id
id [Char]
"**" Token
_ Token
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3019 [Char]
"exponentials are"
    bashism (T_FdRedirect Id
id [Char]
"&" (T_IoFile Id
_ (T_Greater Id
_) Token
_)) =
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBusyboxSh (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3020 [Char]
"&> is"
    bashism (T_FdRedirect Id
id [Char]
"" (T_IoFile Id
_ (T_GREATAND Id
_) Token
file)) =
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit ([Char] -> Bool) -> [Char] -> Bool
forall a b. (a -> b) -> a -> b
$ Token -> [Char]
onlyLiteralString Token
file) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3021 [Char]
">& filename (as opposed to >& fd) is"
    bashism (T_FdRedirect Id
id (Char
'{':[Char]
_) Token
_) = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3022 [Char]
"named file descriptors are"
    bashism (T_FdRedirect Id
id [Char]
num Token
_)
        | (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit [Char]
num Bool -> Bool -> Bool
&& [Char] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
num Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3023 [Char]
"FDs outside 0-9 are"
    bashism (T_Assignment Id
id AssignmentMode
Append [Char]
_ [Token]
_ Token
_) =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3024 [Char]
"+= is"
    bashism (T_IoFile Id
id Token
_ Token
word) | Bool
isNetworked =
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3025 [Char]
"/dev/{tcp,udp} is"
        where
            file :: [Char]
file = Token -> [Char]
onlyLiteralString Token
word
            isNetworked :: Bool
isNetworked = ([Char] -> Bool) -> [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [Char]
file) [[Char]
"/dev/tcp", [Char]
"/dev/udp"]
    bashism (T_Glob Id
id [Char]
str) | [Char]
"[^" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` [Char]
str =
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3026 [Char]
"^ in place of ! in glob bracket expressions is"

    bashism t :: Token
t@(TA_Variable Id
id [Char]
str [Token]
_) | [Char] -> Bool
isBashVariable [Char]
str =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3028 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
str [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is"

    bashism t :: Token
t@(T_DollarBraced Id
id Bool
_ Token
token) = do
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBusyboxSh (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ ((Regex, Integer, [Char]) -> m ())
-> [(Regex, Integer, [Char])] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Regex, Integer, [Char]) -> m ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
(Regex, Integer, [Char]) -> f ()
check [(Regex, Integer, [Char])]
simpleExpansions
        ((Regex, Integer, [Char]) -> m ())
-> [(Regex, Integer, [Char])] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Regex, Integer, [Char]) -> m ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
(Regex, Integer, [Char]) -> f ()
check [(Regex, Integer, [Char])]
advancedExpansions
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char] -> Bool
isBashVariable [Char]
var) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3028 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
var [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is"
      where
        str :: [Char]
str = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ Token -> [[Char]]
oversimplify Token
token
        var :: [Char]
var = [Char] -> [Char]
getBracedReference [Char]
str
        check :: (Regex, Integer, [Char]) -> f ()
check (Regex
regex, Integer
code, [Char]
feature) =
            Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [[Char]] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [[Char]] -> Bool) -> Maybe [[Char]] -> Bool
forall a b. (a -> b) -> a -> b
$ Regex -> [Char] -> Maybe [[Char]]
matchRegex Regex
regex [Char]
str) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ Id -> Integer -> [Char] -> f ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
code [Char]
feature

    bashism t :: Token
t@(T_Pipe Id
id [Char]
"|&") =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3029 [Char]
"|& in place of 2>&1 | is"
    bashism (T_Array Id
id [Token]
_) =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3030 [Char]
"arrays are"
    bashism (T_IoFile Id
id Token
_ Token
t) | Token -> Bool
isGlob Token
t =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3031 [Char]
"redirecting to/from globs is"
    bashism (T_CoProc Id
id Maybe Token
_ Token
_) =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3032 [Char]
"coproc is"

    bashism (T_Function Id
id FunctionKeyword
_ FunctionParentheses
_ [Char]
str Token
_) | Bool -> Bool
not ([Char] -> Bool
isVariableName [Char]
str) =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3033 [Char]
"naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is"

    bashism (T_DollarExpansion Id
id [Token
x]) | Token -> Bool
isOnlyRedirection Token
x =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3034 [Char]
"$(<file) to read files is"
    bashism (T_Backticked Id
id [Token
x]) | Token -> Bool
isOnlyRedirection Token
x =
        Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3035 [Char]
"`<file` to read files is"

    bashism t :: Token
t@(T_SimpleCommand Id
_ [Token]
_ (Token
cmd:Token
arg:[Token]
_))
        | Token
t Token -> [Char] -> Bool
`isCommand` [Char]
"echo" Bool -> Bool -> Bool
&& [Char]
argString [Char] -> Regex -> Bool
`matches` Regex
flagRegex =
            if Bool
isBusyboxSh
            then
                Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Char]
argString [Char] -> Regex -> Bool
`matches` Regex
busyboxFlagRegex) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                    Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg (Token -> Id
getId Token
arg) Integer
3036 [Char]
"echo flags besides -n and -e"
            else if Bool
isDash
            then
                Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
argString [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char]
"-n") (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                    Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg (Token -> Id
getId Token
arg) Integer
3036 [Char]
"echo flags besides -n"
            else
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg (Token -> Id
getId Token
arg) Integer
3037 [Char]
"echo flags are"
      where
          argString :: [Char]
argString = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ Token -> [[Char]]
oversimplify Token
arg
          flagRegex :: Regex
flagRegex = [Char] -> Regex
mkRegex [Char]
"^-[eEsn]+$"
          busyboxFlagRegex :: Regex
busyboxFlagRegex = [Char] -> Regex
mkRegex [Char]
"^-[en]+$"

    bashism t :: Token
t@(T_SimpleCommand Id
_ [Token]
_ (Token
cmd:Token
arg:[Token]
_))
        | Token -> Maybe [Char]
getLiteralString Token
cmd Maybe [Char] -> Maybe [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"exec" Bool -> Bool -> Bool
&& [Char]
"-" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Token -> [[Char]]
oversimplify Token
arg) =
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg (Token -> Id
getId Token
arg) Integer
3038 [Char]
"exec flags are"
    bashism t :: Token
t@(T_SimpleCommand Id
id [Token]
_ [Token]
_)
        | Token
t Token -> [Char] -> Bool
`isCommand` [Char]
"let" = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3039 [Char]
"'let' is"
    bashism t :: Token
t@(T_SimpleCommand Id
_ [Token]
_ (Token
cmd:[Token]
args))
        | Token
t Token -> [Char] -> Bool
`isCommand` [Char]
"set" = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isDash (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
            [(Id, [Char])] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
[(Id, [Char])] -> m ()
checkOptions ([(Id, [Char])] -> m ()) -> [(Id, [Char])] -> m ()
forall a b. (a -> b) -> a -> b
$ [Token] -> [(Id, [Char])]
getLiteralArgs [Token]
args
      where
        -- Get the literal options from a list of arguments,
        -- up until the first non-literal one
        getLiteralArgs :: [Token] -> [(Id, String)]
        getLiteralArgs :: [Token] -> [(Id, [Char])]
getLiteralArgs = (Token -> [(Id, [Char])] -> [(Id, [Char])])
-> [(Id, [Char])] -> [Token] -> [(Id, [Char])]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Token -> [(Id, [Char])] -> [(Id, [Char])]
go []
          where
            go :: Token -> [(Id, [Char])] -> [(Id, [Char])]
go Token
first [(Id, [Char])]
rest = case Token -> Maybe [Char]
getLiteralString Token
first of
                Just [Char]
str -> (Token -> Id
getId Token
first, [Char]
str) (Id, [Char]) -> [(Id, [Char])] -> [(Id, [Char])]
forall a. a -> [a] -> [a]
: [(Id, [Char])]
rest
                Maybe [Char]
Nothing -> []

        -- Check a flag-option pair (such as -o errexit)
        checkOptions :: [(Id, [Char])] -> m ()
checkOptions (flag :: (Id, [Char])
flag@(Id
fid,[Char]
flag') : opt :: (Id, [Char])
opt@(Id
oid,[Char]
opt') : [(Id, [Char])]
rest)
            | [Char]
flag' [Char] -> Regex -> Bool
`matches` Regex
oFlagRegex = do
                Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
opt' [Char] -> Set [Char] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` Set [Char]
longOptions) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                  Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
oid Integer
3040 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"set option " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
opt' [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" is"
                [(Id, [Char])] -> m ()
checkFlags ((Id, [Char])
flag(Id, [Char]) -> [(Id, [Char])] -> [(Id, [Char])]
forall a. a -> [a] -> [a]
:[(Id, [Char])]
rest)
            | Bool
otherwise = [(Id, [Char])] -> m ()
checkFlags ((Id, [Char])
flag(Id, [Char]) -> [(Id, [Char])] -> [(Id, [Char])]
forall a. a -> [a] -> [a]
:(Id, [Char])
opt(Id, [Char]) -> [(Id, [Char])] -> [(Id, [Char])]
forall a. a -> [a] -> [a]
:[(Id, [Char])]
rest)
        checkOptions ((Id, [Char])
flag:[(Id, [Char])]
rest) = [(Id, [Char])] -> m ()
checkFlags ((Id, [Char])
flag(Id, [Char]) -> [(Id, [Char])] -> [(Id, [Char])]
forall a. a -> [a] -> [a]
:[(Id, [Char])]
rest)
        checkOptions [(Id, [Char])]
_           = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

        -- Check that each option in a sequence of flags
        -- (such as -aveo) is valid
        checkFlags :: [(Id, [Char])] -> m ()
checkFlags (flag :: (Id, [Char])
flag@(Id
fid, [Char]
flag'):[(Id, [Char])]
rest)
            | [Char] -> Bool
startsOption [Char]
flag' = do
                Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Char]
flag' [Char] -> Regex -> Bool
`matches` Regex
validFlagsRegex) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                  [Char] -> (Char -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Char] -> [Char]
forall a. HasCallStack => [a] -> [a]
tail [Char]
flag') ((Char -> m ()) -> m ()) -> (Char -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Char
letter ->
                    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Char
letter Char -> Set Char -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` Set Char
optionsSet) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                      Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
fid Integer
3041 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"set flag " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> (Char
'-'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:Char
letterChar -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
" is")
                [(Id, [Char])] -> m ()
checkOptions [(Id, [Char])]
rest
            | [Char] -> Bool
beginsWithDoubleDash [Char]
flag' = do
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
fid Integer
3042 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"set flag " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
flag' [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" is"
                [(Id, [Char])] -> m ()
checkOptions [(Id, [Char])]
rest
            -- Either a word that doesn't start with a dash, or simply '--',
            -- so stop checking.
            | Bool
otherwise = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        checkFlags [] = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

        options :: [Char]
options              = [Char]
"abCefhmnuvxo"
        optionsSet :: Set Char
optionsSet           = [Char] -> Set Char
forall a. Ord a => [a] -> Set a
Set.fromList [Char]
options
        startsOption :: [Char] -> Bool
startsOption         = ([Char] -> Regex -> Bool
`matches` [Char] -> Regex
mkRegex [Char]
"^(\\+|-[^-])")
        oFlagRegex :: Regex
oFlagRegex           = [Char] -> Regex
mkRegex ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[-+][" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
options [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"]*o$"
        validFlagsRegex :: Regex
validFlagsRegex      = [Char] -> Regex
mkRegex ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[-+]([" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
options [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"]+o?|o)$"
        beginsWithDoubleDash :: [Char] -> Bool
beginsWithDoubleDash = ([Char] -> Regex -> Bool
`matches` [Char] -> Regex
mkRegex [Char]
"^--.+$")
        longOptions :: Set [Char]
longOptions          = [[Char]] -> Set [Char]
forall a. Ord a => [a] -> Set a
Set.fromList
            [ [Char]
"allexport", [Char]
"errexit", [Char]
"ignoreeof", [Char]
"monitor", [Char]
"noclobber"
            , [Char]
"noexec", [Char]
"noglob", [Char]
"nolog", [Char]
"notify" , [Char]
"nounset", [Char]
"verbose"
            , [Char]
"vi", [Char]
"xtrace" ]

    bashism t :: Token
t@(T_SimpleCommand Id
id [Token]
_ (Token
cmd:[Token]
rest)) =
        let name :: [Char]
name = [Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"" (Maybe [Char] -> [Char]) -> Maybe [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ Token -> Maybe [Char]
getCommandName Token
t
            flags :: [(Token, [Char])]
flags = Token -> [(Token, [Char])]
getLeadingFlags Token
t
        in do
            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"local" Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isDash) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                -- This is so commonly accepted that we'll make it a special case
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3043 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"'local' is"
            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
unsupportedCommands) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3044 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"'" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
name [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"' is"
            Maybe (m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ (Maybe (m ()) -> m ()) -> Maybe (m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ do
                allowed' <- [Char] -> Map [Char] (Maybe [[Char]]) -> Maybe (Maybe [[Char]])
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup [Char]
name Map [Char] (Maybe [[Char]])
allowedFlags
                allowed <- allowed'
                (word, flag) <- find
                    (\(Token, [Char])
x -> (Bool -> Bool
not (Bool -> Bool)
-> ((Token, [Char]) -> Bool) -> (Token, [Char]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Char] -> Bool)
-> ((Token, [Char]) -> [Char]) -> (Token, [Char]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Token, [Char]) -> [Char]
forall a b. (a, b) -> b
snd ((Token, [Char]) -> Bool) -> (Token, [Char]) -> Bool
forall a b. (a -> b) -> a -> b
$ (Token, [Char])
x) Bool -> Bool -> Bool
&& (Token, [Char]) -> [Char]
forall a b. (a, b) -> b
snd (Token, [Char])
x [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [[Char]]
allowed) flags
                return . warnMsg (getId word) 3045 $ name ++ " -" ++ flag ++ " is"

            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"source" Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isBusyboxSh) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3046 [Char]
"'source' in place of '.' is"
            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"trap") (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                let
                    check :: Token -> m ()
check Token
token = Maybe (m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ (Maybe (m ()) -> m ()) -> Maybe (m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ do
                        str <- Token -> Maybe [Char]
getLiteralString Token
token
                        let upper = (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper [Char]
str
                        return $ do
                            when (upper `elem` ["ERR", "DEBUG", "RETURN"]) $
                                warnMsg (getId token) 3047 $ "trapping " ++ str ++ " is"
                            when (not isBusyboxSh && "SIG" `isPrefixOf` upper) $
                                warnMsg (getId token) 3048
                                    "prefixing signal names with 'SIG' is"
                            when (not isDash && upper /= str) $
                                warnMsg (getId token) 3049
                                    "using lower/mixed case for signal names is"
                in
                    (Token -> m ()) -> [Token] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
check (Int -> [Token] -> [Token]
forall a. Int -> [a] -> [a]
drop Int
1 [Token]
rest)

            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"printf") (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe (m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ (Maybe (m ()) -> m ()) -> Maybe (m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ do
                format <- [Token]
rest [Token] -> Int -> Maybe Token
forall {a}. [a] -> Int -> Maybe a
!!! Int
0  -- flags are covered by allowedFlags
                let literal = Token -> [Char]
onlyLiteralString Token
format
                guard $ "%q" `isInfixOf` literal
                return $ warnMsg (getId format) 3050 "printf %q is"

            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"read" Bool -> Bool -> Bool
&& (Token -> Bool) -> [Token] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Token -> Bool
isFlag [Token]
rest) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
                Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg (Token -> Id
getId Token
cmd) Integer
3061 [Char]
"read without a variable is"
      where
        unsupportedCommands :: [[Char]]
unsupportedCommands = [
            [Char]
"let", [Char]
"caller", [Char]
"builtin", [Char]
"complete", [Char]
"compgen", [Char]
"declare", [Char]
"dirs", [Char]
"disown",
            [Char]
"enable", [Char]
"mapfile", [Char]
"readarray", [Char]
"pushd", [Char]
"popd", [Char]
"shopt", [Char]
"suspend",
            [Char]
"typeset"
            ]
        allowedFlags :: Map [Char] (Maybe [[Char]])
allowedFlags = [([Char], Maybe [[Char]])] -> Map [Char] (Maybe [[Char]])
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [
            ([Char]
"cd", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"L", [Char]
"P"]),
            ([Char]
"exec", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just []),
            ([Char]
"export", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"p"]),
            ([Char]
"hash", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just ([[Char]] -> Maybe [[Char]]) -> [[Char]] -> Maybe [[Char]]
forall a b. (a -> b) -> a -> b
$ if Bool
isDash then [[Char]
"r", [Char]
"v"] else [[Char]
"r"]),
            ([Char]
"jobs", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"l", [Char]
"p"]),
            ([Char]
"printf", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just []),
            ([Char]
"read", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just ([[Char]] -> Maybe [[Char]]) -> [[Char]] -> Maybe [[Char]]
forall a b. (a -> b) -> a -> b
$ if Bool
isDash Bool -> Bool -> Bool
|| Bool
isBusyboxSh then [[Char]
"r", [Char]
"p"] else [[Char]
"r"]),
            ([Char]
"readonly", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"p"]),
            ([Char]
"trap", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just []),
            ([Char]
"type", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just ([[Char]] -> Maybe [[Char]]) -> [[Char]] -> Maybe [[Char]]
forall a b. (a -> b) -> a -> b
$ if Bool
isBusyboxSh then [[Char]
"p"] else []),
            ([Char]
"ulimit", if Bool
isDash then Maybe [[Char]]
forall a. Maybe a
Nothing else [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"f"]),
            ([Char]
"umask", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"S"]),
            ([Char]
"unset", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [[Char]
"f", [Char]
"v"]),
            ([Char]
"wait", [[Char]] -> Maybe [[Char]]
forall a. a -> Maybe a
Just [])
            ]
    bashism t :: Token
t@(T_SourceCommand Id
id Token
src Token
_)
      | Token -> Maybe [Char]
getCommandName Token
src Maybe [Char] -> Maybe [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"source" =
          Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBusyboxSh (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3051 [Char]
"'source' in place of '.' is"
    bashism (TA_Expansion Id
_ (T_Literal Id
id [Char]
str : [Token]
_))
        | [Char]
str [Char] -> Regex -> Bool
`matches` Regex
radix = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warnMsg Id
id Integer
3052 [Char]
"arithmetic base conversion is"
      where
        radix :: Regex
radix = [Char] -> Regex
mkRegex [Char]
"^[0-9]+#"
    bashism Token
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    varChars :: [Char]
varChars=[Char]
"_0-9a-zA-Z"
    advancedExpansions :: [(Regex, Integer, [Char])]
advancedExpansions = let re :: [Char] -> Regex
re = [Char] -> Regex
mkRegex in [
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^![" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"]", Integer
3053, [Char]
"indirect expansion is"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"]+\\[.*\\]$", Integer
3054, [Char]
"array references are"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^![" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"]+\\[[*@]]$", Integer
3055, [Char]
"array key expansion is"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^![" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"]+[*@]$", Integer
3056, [Char]
"name matching prefixes are"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"*@]+(\\[.*\\])?[,^]", Integer
3059, [Char]
"case modification is")
        ]
    simpleExpansions :: [(Regex, Integer, [Char])]
simpleExpansions = let re :: [Char] -> Regex
re = [Char] -> Regex
mkRegex in [
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"*@]+:[^-=?+]", Integer
3057, [Char]
"string indexing is"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^([*@][%#]|#[@*])", Integer
3058, [Char]
"string operations on $@/$* are"),
        ([Char] -> Regex
re ([Char] -> Regex) -> [Char] -> Regex
forall a b. (a -> b) -> a -> b
$ [Char]
"^[" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
varChars [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"*@]+(\\[.*\\])?/", Integer
3060, [Char]
"string replacement is")
        ]
    bashVars :: [[Char]]
bashVars = [
        -- This list deliberately excludes $BASH_VERSION as it's often used
        -- for shell identification.
        [Char]
"OSTYPE", [Char]
"MACHTYPE", [Char]
"HOSTTYPE", [Char]
"HOSTNAME",
        [Char]
"DIRSTACK", [Char]
"EUID", [Char]
"UID", [Char]
"SHLVL", [Char]
"PIPESTATUS", [Char]
"SHELLOPTS",
        [Char]
"_", [Char]
"BASHOPTS", [Char]
"BASHPID", [Char]
"BASH_ALIASES", [Char]
"BASH_ARGC",
        [Char]
"BASH_ARGV", [Char]
"BASH_ARGV0", [Char]
"BASH_CMDS", [Char]
"BASH_COMMAND",
        [Char]
"BASH_EXECUTION_STRING", [Char]
"BASH_LINENO", [Char]
"BASH_REMATCH", [Char]
"BASH_SOURCE",
        [Char]
"BASH_SUBSHELL", [Char]
"BASH_VERSINFO", [Char]
"EPOCHREALTIME", [Char]
"EPOCHSECONDS",
        [Char]
"FUNCNAME", [Char]
"GROUPS", [Char]
"MACHTYPE", [Char]
"MAPFILE"
        ]
    bashDynamicVars :: [[Char]]
bashDynamicVars = [ [Char]
"RANDOM", [Char]
"SECONDS" ]
    dashVars :: [[Char]]
dashVars = [ [Char]
"_" ]
    isBashVariable :: [Char] -> Bool
isBashVariable [Char]
var =
        ([Char]
var [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
bashDynamicVars
            Bool -> Bool -> Bool
|| [Char]
var [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
bashVars Bool -> Bool -> Bool
&& Bool -> Bool
not ([Char] -> Bool
isAssigned [Char]
var))
        Bool -> Bool -> Bool
&& Bool -> Bool
not (Bool
isDash Bool -> Bool -> Bool
&& [Char]
var [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
dashVars)
    isAssigned :: [Char] -> Bool
isAssigned [Char]
var = (StackData -> Bool) -> [StackData] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StackData -> Bool
f (Parameters -> [StackData]
variableFlow Parameters
params)
      where
        f :: StackData -> Bool
f StackData
x = case StackData
x of
                Assignment (Token
_, Token
_, [Char]
name, DataType
_) -> [Char]
name [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
var
                StackData
_ -> Bool
False

    checkTestOp :: Map t (Integer, t Shell, t -> [Char]) -> t -> Id -> m ()
checkTestOp Map t (Integer, t Shell, t -> [Char])
table t
op Id
id = Maybe (m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ (Maybe (m ()) -> m ()) -> Maybe (m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ do
        (code, shells, msg) <- t
-> Map t (Integer, t Shell, t -> [Char])
-> Maybe (Integer, t Shell, t -> [Char])
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup t
op Map t (Integer, t Shell, t -> [Char])
table
        guard . not $ shellType params `elem` shells
        return $ warnMsg id code (msg op)


buildTestFlagMap :: t ([k], a) -> Map k a
buildTestFlagMap t ([k], a)
list = [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(k, a)] -> Map k a) -> [(k, a)] -> Map k a
forall a b. (a -> b) -> a -> b
$ (([k], a) -> [(k, a)]) -> t ([k], a) -> [(k, a)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\([k]
x,a
y) -> (k -> (k, a)) -> [k] -> [(k, a)]
forall a b. (a -> b) -> [a] -> [b]
map (\k
c -> (k
c,a
y)) [k]
x) t ([k], a)
list
bashismBinaryTestFlags :: Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismBinaryTestFlags = [([[Char]], (Integer, [Shell], [Char] -> [Char]))]
-> Map [Char] (Integer, [Shell], [Char] -> [Char])
forall {k} {t :: * -> *} {a}.
(Ord k, Foldable t) =>
t ([k], a) -> Map k a
buildTestFlagMap [
    -- ([list of applicable flags],
    --     (error code, exempt shells, message builder :: String -> String)),
    --
    -- Distinct error codes allow the wiki to give more helpful, targeted
    -- information.
    ([[Char]
"<", [Char]
">", [Char]
"\\<", [Char]
"\\>", [Char]
"<=", [Char]
">=", [Char]
"\\<=", [Char]
"\\>="],
        (Integer
3012, [Shell
Dash, Shell
BusyboxSh], \[Char]
op -> [Char]
"lexicographical " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is")),
    ([[Char]
"=="],
        (Integer
3014, [Shell
BusyboxSh], \[Char]
op -> [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" in place of = is")),
    ([[Char]
"=~"],
        (Integer
3015, [], \[Char]
op -> [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" regex matching is")),

    ([], (Integer
0,[],[Char] -> [Char] -> [Char]
forall a b. a -> b -> a
const [Char]
""))
  ]
bashismUnaryTestFlags :: Map [Char] (Integer, [Shell], [Char] -> [Char])
bashismUnaryTestFlags = [([[Char]], (Integer, [Shell], [Char] -> [Char]))]
-> Map [Char] (Integer, [Shell], [Char] -> [Char])
forall {k} {t :: * -> *} {a}.
(Ord k, Foldable t) =>
t ([k], a) -> Map k a
buildTestFlagMap [
    ([[Char]
"-v"],
        (Integer
3016, [], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" (in place of [ -n \"${var+x}\" ]) is")),
    ([[Char]
"-a"],
        (Integer
3017, [], \[Char]
op -> [Char]
"unary " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" in place of -e is")),
    ([[Char]
"-o"],
        (Integer
3062, [], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" to check options is")),
    ([[Char]
"-R"],
        (Integer
3063, [], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" and namerefs in general are")),
    ([[Char]
"-N"],
        (Integer
3064, [], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is")),
    ([[Char]
"-k"],
        (Integer
3065, [Shell
Dash, Shell
BusyboxSh], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is")),
    ([[Char]
"-G"],
        (Integer
3066, [Shell
Dash, Shell
BusyboxSh], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is")),
    ([[Char]
"-O"],
        (Integer
3067, [Shell
Dash, Shell
BusyboxSh], \[Char]
op -> [Char]
"test " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
op [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is")),

    ([], (Integer
0,[],[Char] -> [Char] -> [Char]
forall a b. a -> b -> a
const [Char]
""))
  ]


prop_checkEchoSed1 :: Bool
prop_checkEchoSed1 = ForShell -> [Char] -> Bool
verify ForShell
checkEchoSed [Char]
"FOO=$(echo \"$cow\" | sed 's/foo/bar/g')"
prop_checkEchoSed1b :: Bool
prop_checkEchoSed1b = ForShell -> [Char] -> Bool
verify ForShell
checkEchoSed [Char]
"FOO=$(sed 's/foo/bar/g' <<< \"$cow\")"
prop_checkEchoSed2 :: Bool
prop_checkEchoSed2 = ForShell -> [Char] -> Bool
verify ForShell
checkEchoSed [Char]
"rm $(echo $cow | sed -e 's,foo,bar,')"
prop_checkEchoSed2b :: Bool
prop_checkEchoSed2b = ForShell -> [Char] -> Bool
verify ForShell
checkEchoSed [Char]
"rm $(sed -e 's,foo,bar,' <<< $cow)"
checkEchoSed :: ForShell
checkEchoSed = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Bash, Shell
Ksh] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> f ()
f (T_Redirecting Id
id [Token]
lefts Token
r) =
        Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Token -> Bool) -> [Token] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Token -> Bool
redirectHereString [Token]
lefts) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
            Id -> [[Char]] -> f ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
Id -> [[Char]] -> f ()
checkSed Id
id [[Char]]
rcmd
      where
        redirectHereString :: Token -> Bool
        redirectHereString :: Token -> Bool
redirectHereString Token
t = case Token
t of
            (T_FdRedirect Id
_ [Char]
_ T_HereString{}) -> Bool
True
            Token
_                                 -> Bool
False
        rcmd :: [[Char]]
rcmd = Token -> [[Char]]
oversimplify Token
r

    f (T_Pipeline Id
id [Token]
_ [Token
a, Token
b]) =
        Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([[Char]]
acmd [[Char]] -> [[Char]] -> Bool
forall a. Eq a => a -> a -> Bool
== [[Char]
"echo", [Char]
"${VAR}"]) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
            Id -> [[Char]] -> f ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
Id -> [[Char]] -> f ()
checkSed Id
id [[Char]]
bcmd
      where
        acmd :: [[Char]]
acmd = Token -> [[Char]]
oversimplify Token
a
        bcmd :: [[Char]]
bcmd = Token -> [[Char]]
oversimplify Token
b

    f Token
_ = () -> f ()
forall a. a -> f a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    checkSed :: Id -> [[Char]] -> f ()
checkSed Id
id [[Char]
"sed", [Char]
v]       = Id -> [Char] -> f ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
Id -> [Char] -> f ()
checkIn Id
id [Char]
v
    checkSed Id
id [[Char]
"sed", [Char]
"-e", [Char]
v] = Id -> [Char] -> f ()
forall {f :: * -> *}.
MonadWriter [TokenComment] f =>
Id -> [Char] -> f ()
checkIn Id
id [Char]
v
    checkSed Id
_ [[Char]]
_                 = () -> f ()
forall a. a -> f a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    -- This should have used backreferences, but TDFA doesn't support them
    sedRe :: Regex
sedRe = [Char] -> Regex
mkRegex [Char]
"^s(.)([^\n]*)g?$"
    isSimpleSed :: [Char] -> Bool
isSimpleSed [Char]
s = Maybe () -> Bool
forall a. Maybe a -> Bool
isJust (Maybe () -> Bool) -> Maybe () -> Bool
forall a b. (a -> b) -> a -> b
$ do
        [h:_,rest] <- Regex -> [Char] -> Maybe [[Char]]
matchRegex Regex
sedRe [Char]
s
        let delimiters = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
h) [Char]
rest
        guard $ length delimiters == 2
    checkIn :: Id -> [Char] -> f ()
checkIn Id
id [Char]
s =
        Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char] -> Bool
isSimpleSed [Char]
s) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
            Id -> Integer -> [Char] -> f ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
style Id
id Integer
2001 [Char]
"See if you can use ${variable//search/replace} instead."


prop_checkBraceExpansionVars1 :: Bool
prop_checkBraceExpansionVars1 = ForShell -> [Char] -> Bool
verify ForShell
checkBraceExpansionVars [Char]
"echo {1..$n}"
prop_checkBraceExpansionVars2 :: Bool
prop_checkBraceExpansionVars2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBraceExpansionVars [Char]
"echo {1,3,$n}"
prop_checkBraceExpansionVars3 :: Bool
prop_checkBraceExpansionVars3 = ForShell -> [Char] -> Bool
verify ForShell
checkBraceExpansionVars [Char]
"eval echo DSC{0001..$n}.jpg"
prop_checkBraceExpansionVars4 :: Bool
prop_checkBraceExpansionVars4 = ForShell -> [Char] -> Bool
verify ForShell
checkBraceExpansionVars [Char]
"echo {$i..100}"
checkBraceExpansionVars :: ForShell
checkBraceExpansionVars = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Bash] Token -> Analysis
forall {m :: * -> *}.
(MonadReader Parameters m, MonadWriter [TokenComment] m) =>
Token -> m ()
f
  where
    f :: Token -> m ()
f t :: Token
t@(T_BraceExpansion Id
id [Token]
list) = (Token -> m ()) -> [Token] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Token -> m ()
forall {m :: * -> *}.
(MonadReader Parameters m, MonadWriter [TokenComment] m) =>
Token -> m ()
check [Token]
list
      where
        check :: Token -> f ()
check Token
element =
            Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (([Char] -> Bool) -> [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` Token -> [Char]
toString Token
element) [[Char]
"$..", [Char]
"..$"]) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ do
                c <- Token -> f Bool
forall {m :: * -> *}. MonadReader Parameters m => Token -> m Bool
isEvaled Token
element
                if c
                    then style id 2175 "Quote this invalid brace expansion since it should be passed literally to eval."
                    else warn id 2051 "Bash doesn't support variables in brace range expansions."
    f Token
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    literalExt :: Token -> m [Char]
literalExt Token
t =
        case Token
t of
            T_DollarBraced {} -> [Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"$"
            T_DollarExpansion {} -> [Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"$"
            T_DollarArithmetic {} -> [Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"$"
            Token
_ -> [Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
"-"
    toString :: Token -> [Char]
toString Token
t = Identity [Char] -> [Char]
forall a. Identity a -> a
runIdentity (Identity [Char] -> [Char]) -> Identity [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ (Token -> Identity [Char]) -> Token -> Identity [Char]
forall (m :: * -> *).
Monad m =>
(Token -> m [Char]) -> Token -> m [Char]
getLiteralStringExt Token -> Identity [Char]
forall {m :: * -> *}. Monad m => Token -> m [Char]
literalExt Token
t
    isEvaled :: Token -> m Bool
isEvaled Token
t = do
        cmd <- Token -> m (Maybe Token)
forall {m :: * -> *}.
MonadReader Parameters m =>
Token -> m (Maybe Token)
getClosestCommandM Token
t
        return $ maybe False (`isUnqualifiedCommand` "eval") cmd


prop_checkMultiDimensionalArrays1 :: Bool
prop_checkMultiDimensionalArrays1 = ForShell -> [Char] -> Bool
verify ForShell
checkMultiDimensionalArrays [Char]
"foo[a][b]=3"
prop_checkMultiDimensionalArrays2 :: Bool
prop_checkMultiDimensionalArrays2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkMultiDimensionalArrays [Char]
"foo[a]=3"
prop_checkMultiDimensionalArrays3 :: Bool
prop_checkMultiDimensionalArrays3 = ForShell -> [Char] -> Bool
verify ForShell
checkMultiDimensionalArrays [Char]
"foo=( [a][b]=c )"
prop_checkMultiDimensionalArrays4 :: Bool
prop_checkMultiDimensionalArrays4 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkMultiDimensionalArrays [Char]
"foo=( [a]=c )"
prop_checkMultiDimensionalArrays5 :: Bool
prop_checkMultiDimensionalArrays5 = ForShell -> [Char] -> Bool
verify ForShell
checkMultiDimensionalArrays [Char]
"echo ${foo[bar][baz]}"
prop_checkMultiDimensionalArrays6 :: Bool
prop_checkMultiDimensionalArrays6 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkMultiDimensionalArrays [Char]
"echo ${foo[bar]}"
checkMultiDimensionalArrays :: ForShell
checkMultiDimensionalArrays = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Bash] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> m ()
f Token
token =
        case Token
token of
            T_Assignment Id
_ AssignmentMode
_ [Char]
name (Token
first:Token
second:[Token]
_) Token
_ -> Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
about Token
second
            T_IndexedElement Id
_ (Token
first:Token
second:[Token]
_) Token
_ -> Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
about Token
second
            T_DollarBraced Id
_ Bool
_ Token
l ->
                Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Token -> Bool
isMultiDim Token
l) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
about Token
token
            Token
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    about :: Token -> m ()
about Token
t = Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
warn (Token -> Id
getId Token
t) Integer
2180 [Char]
"Bash does not support multidimensional arrays. Use 1D or associative arrays."

    re :: Regex
re = [Char] -> Regex
mkRegex [Char]
"^\\[.*\\]\\[.*\\]"  -- Fixme, this matches ${foo:- [][]} and such as well
    isMultiDim :: Token -> Bool
isMultiDim Token
l = [Char] -> [Char]
getBracedModifier ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ Token -> [[Char]]
oversimplify Token
l) [Char] -> Regex -> Bool
`matches` Regex
re

prop_checkPS11 :: Bool
prop_checkPS11 = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"PS1='\\033[1;35m\\$ '"
prop_checkPS11a :: Bool
prop_checkPS11a = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"export PS1='\\033[1;35m\\$ '"
prop_checkPSf2 :: Bool
prop_checkPSf2 = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"PS1='\\h \\e[0m\\$ '"
prop_checkPS13 :: Bool
prop_checkPS13 = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"PS1=$'\\x1b[c '"
prop_checkPS14 :: Bool
prop_checkPS14 = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"PS1=$'\\e[3m; '"
prop_checkPS14a :: Bool
prop_checkPS14a = ForShell -> [Char] -> Bool
verify ForShell
checkPS1Assignments [Char]
"export PS1=$'\\e[3m; '"
prop_checkPS15 :: Bool
prop_checkPS15 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkPS1Assignments [Char]
"PS1='\\[\\033[1;35m\\]\\$ '"
prop_checkPS16 :: Bool
prop_checkPS16 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkPS1Assignments [Char]
"PS1='\\[\\e1m\\e[1m\\]\\$ '"
prop_checkPS17 :: Bool
prop_checkPS17 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkPS1Assignments [Char]
"PS1='e033x1B'"
prop_checkPS18 :: Bool
prop_checkPS18 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkPS1Assignments [Char]
"PS1='\\[\\e\\]'"
checkPS1Assignments :: ForShell
checkPS1Assignments = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Bash] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> f ()
f Token
token = case Token
token of
        (T_Assignment Id
_ AssignmentMode
_ [Char]
"PS1" [Token]
_ Token
word) -> Token -> f ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
warnFor Token
word
        Token
_ -> () -> f ()
forall a. a -> f a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    warnFor :: Token -> f ()
warnFor Token
word =
        let contents :: [Char]
contents = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ Token -> [[Char]]
oversimplify Token
word in
            Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char] -> Bool
containsUnescaped [Char]
contents) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
                Id -> Integer -> [Char] -> f ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
info (Token -> Id
getId Token
word) Integer
2025 [Char]
"Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues"
    containsUnescaped :: [Char] -> Bool
containsUnescaped [Char]
s =
        let unenclosed :: [Char]
unenclosed = Regex -> [Char] -> [Char] -> [Char]
subRegex Regex
enclosedRegex [Char]
s [Char]
"" in
           Maybe [[Char]] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [[Char]] -> Bool) -> Maybe [[Char]] -> Bool
forall a b. (a -> b) -> a -> b
$ Regex -> [Char] -> Maybe [[Char]]
matchRegex Regex
escapeRegex [Char]
unenclosed
    enclosedRegex :: Regex
enclosedRegex = [Char] -> Regex
mkRegex [Char]
"\\\\\\[.*\\\\\\]" -- FIXME: shouldn't be eager
    escapeRegex :: Regex
escapeRegex = [Char] -> Regex
mkRegex [Char]
"\\\\x1[Bb]|\\\\e|\x1B|\\\\033"


prop_checkMultipleBangs1 :: Bool
prop_checkMultipleBangs1 = ForShell -> [Char] -> Bool
verify ForShell
checkMultipleBangs [Char]
"! ! true"
prop_checkMultipleBangs2 :: Bool
prop_checkMultipleBangs2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkMultipleBangs [Char]
"! true"
checkMultipleBangs :: ForShell
checkMultipleBangs = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Dash, Shell
BusyboxSh, Shell
Sh] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> m ()
f Token
token = case Token
token of
        T_Banged Id
id (T_Banged Id
_ Token
_) ->
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
err Id
id Integer
2325 [Char]
"Multiple ! in front of pipelines are a bash/ksh extension. Use only 0 or 1."
        Token
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


prop_checkBangAfterPipe1 :: Bool
prop_checkBangAfterPipe1 = ForShell -> [Char] -> Bool
verify ForShell
checkBangAfterPipe [Char]
"true | ! true"
prop_checkBangAfterPipe2 :: Bool
prop_checkBangAfterPipe2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBangAfterPipe [Char]
"true | ( ! true )"
prop_checkBangAfterPipe3 :: Bool
prop_checkBangAfterPipe3 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkBangAfterPipe [Char]
"! ! true | true"
checkBangAfterPipe :: ForShell
checkBangAfterPipe = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Dash, Shell
BusyboxSh, Shell
Sh, Shell
Bash] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> m ()
f Token
token = case Token
token of
        T_Pipeline Id
_ [Token]
_ [Token]
cmds -> (Token -> m ()) -> [Token] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Token -> m ()
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
check [Token]
cmds
        Token
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    check :: Token -> m ()
check Token
token = case Token
token of
        T_Banged Id
id Token
_ ->
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
err Id
id Integer
2326 [Char]
"! is not allowed in the middle of pipelines. Use command group as in cmd | { ! cmd; } if necessary."
        Token
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


prop_checkNegatedUnaryOps1 :: Bool
prop_checkNegatedUnaryOps1 = ForShell -> [Char] -> Bool
verify ForShell
checkNegatedUnaryOps [Char]
"[ ! -o braceexpand ]"
prop_checkNegatedUnaryOps2 :: Bool
prop_checkNegatedUnaryOps2 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkNegatedUnaryOps [Char]
"[ -o braceexpand ]"
prop_checkNegatedUnaryOps3 :: Bool
prop_checkNegatedUnaryOps3 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkNegatedUnaryOps [Char]
"[[ ! -o braceexpand ]]"
prop_checkNegatedUnaryOps4 :: Bool
prop_checkNegatedUnaryOps4 = ForShell -> [Char] -> Bool
verifyNot ForShell
checkNegatedUnaryOps [Char]
"! [ -o braceexpand ]"
prop_checkNegatedUnaryOps5 :: Bool
prop_checkNegatedUnaryOps5 = ForShell -> [Char] -> Bool
verify ForShell
checkNegatedUnaryOps [Char]
"[ ! -a file ]"
checkNegatedUnaryOps :: ForShell
checkNegatedUnaryOps = [Shell] -> (Token -> Analysis) -> ForShell
ForShell [Shell
Bash] Token -> Analysis
forall {m :: * -> *}. MonadWriter [TokenComment] m => Token -> m ()
f
  where
    f :: Token -> m ()
f Token
token = case Token
token of
        TC_Unary Id
id ConditionType
SingleBracket [Char]
"!" (TC_Unary Id
_ ConditionType
_ [Char]
op Token
_) | [Char]
op [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]
"-a", [Char]
"-o"] ->
            Id -> Integer -> [Char] -> m ()
forall {m :: * -> *}.
MonadWriter [TokenComment] m =>
Id -> Integer -> [Char] -> m ()
err Id
id Integer
2332 ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
msg [Char]
op
        Token
_ -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

    msg :: [Char] -> [Char]
msg [Char]
"-o" = [Char]
"[ ! -o opt ] is always true because -o becomes logical OR. Use [[ ]] or ! [ -o opt ]."
    msg [Char]
"-a" = [Char]
"[ ! -a file ] is always true because -a becomes logical AND. Use -e instead."
    msg [Char]
_ = [Char] -> [Char]
pleaseReport [Char]
"unhandled negated unary message"

return []
runTests :: IO Bool
runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])