diff mbox

[fsl-community-bsp-base] setup-environment: check script has been sourced

Message ID 1404919528-10899-1-git-send-email-trevor.woerner@linaro.org
State New
Headers show

Commit Message

Trevor Woerner July 9, 2014, 3:25 p.m. UTC
Check that this script has been invoked correctly (i.e. that it has been
"source"d and not merely run directly). If it has been run directly, don't
exit immediately in case the user specified the "help" option, allow the help
to be displayed, then exit. Adjust how this script is terminated based on
whether it has been sourced or run directly (i.e. use either "return" or
"exit" as required).

This change fixes an infinite loop that is caused if the user runs this script
directly and specifies the help option.

These changes have been tested on: bash, dash, and zsh.

Signed-off-by: Trevor Woerner <trevor.woerner@linaro.org>
---
 setup-environment | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Daiane Angolini July 11, 2014, 1:28 p.m. UTC | #1
On Wed, Jul 9, 2014 at 12:25 PM, Trevor Woerner
<trevor.woerner@linaro.org> wrote:
> Check that this script has been invoked correctly (i.e. that it has been
> "source"d and not merely run directly). If it has been run directly, don't
> exit immediately in case the user specified the "help" option, allow the help
> to be displayed, then exit. Adjust how this script is terminated based on
> whether it has been sourced or run directly (i.e. use either "return" or
> "exit" as required).
>
> This change fixes an infinite loop that is caused if the user runs this script
> directly and specifies the help option.
>
> These changes have been tested on: bash, dash, and zsh.
>
> Signed-off-by: Trevor Woerner <trevor.woerner@linaro.org>
> ---
>  setup-environment | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
>
> diff --git a/setup-environment b/setup-environment
> index 5931e2b..d3f89ce 100755
> --- a/setup-environment
> +++ b/setup-environment
> @@ -24,6 +24,23 @@ NCPU=`grep -c processor /proc/cpuinfo`
>  CWD=`pwd`
>  PROGNAME="setup-environment"
>
> +# try to determine if this script has been invoked correctly
> +# in other words, make sure it has been "source"d
> +# don't exit out immediately, allow the user to ask for -h
> +INVOKEGOOD=1
> +if [ -n "$ZSH_NAME" ]; then
> +    # check for zsh
> +    if [ -n "$BASH_SOURCE" ]; then
> +        INVOKEGOOD=0
> +    fi
> +else
> +    # check for bash, sh, and dash
> +    echo $0 | grep -q $PROGNAME
> +    if [ $? -eq 0 ]; then
> +        INVOKEGOOD=0
> +    fi
> +fi
> +
>  usage()
>  {
>      echo -e "\nUsage: source $PROGNAME <build-dir>
> @@ -55,6 +72,10 @@ ARGS=$(getopt --options $SHORTOPTS  \
>    --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
>  # Print the usage menu if invalid options are specified
>  if [ $? != 0 -o $# -lt 1 ]; then
> +   if [ $INVOKEGOOD = 0 ]; then
> +       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"

I hate using dot "." in documentations or log messages (in fact I
don´t use it any more in my command lines) because I cannot *see* it
when I look to the log/doc line.

Do you see any problem in the use of "source" instead of ". " (dot +
space) as the suggested command line?

Other than that I don´t see any technical problem with the patch.

Daiane


> +       exit 1
> +   fi
>     usage && clean_up
>     return 1
>  fi
> @@ -66,6 +87,9 @@ do
>          -h|--help)
>             usage
>             clean_up
> +           if [ $INVOKEGOOD = 0 ]; then
> +               exit 0
> +           fi
>             return 0
>             ;;
>          --)
> @@ -75,6 +99,11 @@ do
>      esac
>  done
>
> +if [ $INVOKEGOOD = 0 ]; then
> +    echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
> +    exit 1
> +fi
> +
>  if [ "$(whoami)" = "root" ]; then
>      echo "ERROR: do not use the BSP as root. Exiting..."
>  fi
> --
> 2.0.0.5.gbce14aa
>
> --
> _______________________________________________
> meta-freescale mailing list
> meta-freescale@yoctoproject.org
> https://lists.yoctoproject.org/listinfo/meta-freescale
Trevor Woerner July 11, 2014, 2:44 p.m. UTC | #2
Hi Daiane,

On 07/11/14 09:28, Daiane Angolini wrote:
>
> I hate using dot "." in documentations or log messages (in fact I
> don´t use it any more in my command lines) because I cannot *see* it
> when I look to the log/doc line.
>
> Do you see any problem in the use of "source" instead of ". " (dot +
> space) as the suggested command line?

I agree. The funny thing is, I always do "source ..." instead of the dot :-)
Otavio Salvador July 11, 2014, 3:24 p.m. UTC | #3
On Fri, Jul 11, 2014 at 10:28 AM, Daiane Angolini <daiane.list@gmail.com> wrote:
> On Wed, Jul 9, 2014 at 12:25 PM, Trevor Woerner
> <trevor.woerner@linaro.org> wrote:
...
>> @@ -55,6 +72,10 @@ ARGS=$(getopt --options $SHORTOPTS  \
>>    --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
>>  # Print the usage menu if invalid options are specified
>>  if [ $? != 0 -o $# -lt 1 ]; then
>> +   if [ $INVOKEGOOD = 0 ]; then
>> +       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
>
> I hate using dot "." in documentations or log messages (in fact I
> don´t use it any more in my command lines) because I cannot *see* it
> when I look to the log/doc line.
>
> Do you see any problem in the use of "source" instead of ". " (dot +
> space) as the suggested command line?
>
> Other than that I don´t see any technical problem with the patch.

The suggested line ought to be using "." as this is POSIX compliant.
"source" is Bash specific.
Daiane Angolini July 11, 2014, 3:46 p.m. UTC | #4
On Fri, Jul 11, 2014 at 12:24 PM, Otavio Salvador
<otavio@ossystems.com.br> wrote:
> On Fri, Jul 11, 2014 at 10:28 AM, Daiane Angolini <daiane.list@gmail.com> wrote:
>> On Wed, Jul 9, 2014 at 12:25 PM, Trevor Woerner
>> <trevor.woerner@linaro.org> wrote:
> ...
>>> @@ -55,6 +72,10 @@ ARGS=$(getopt --options $SHORTOPTS  \
>>>    --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
>>>  # Print the usage menu if invalid options are specified
>>>  if [ $? != 0 -o $# -lt 1 ]; then
>>> +   if [ $INVOKEGOOD = 0 ]; then
>>> +       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
>>
>> I hate using dot "." in documentations or log messages (in fact I
>> don´t use it any more in my command lines) because I cannot *see* it
>> when I look to the log/doc line.
>>
>> Do you see any problem in the use of "source" instead of ". " (dot +
>> space) as the suggested command line?
>>
>> Other than that I don´t see any technical problem with the patch.
>
> The suggested line ought to be using "." as this is POSIX compliant.
> "source" is Bash specific.


I cannot accept that the "POSIX compliant" is the final argument in
this discussion.

I mean, I´m not saying you are wrong. I´m saying I still cannot see
the dot even being POSIX compliant!

I would suggest to use a glitter shining intermittent red and pink
dot, instead. However, it´s too girly even to me.

How to highlight the user that *does not know* how to use the tool
(otherwise the log is useless) to see the dot?

And, this points an error in our README / Doc files:

https://github.com/Freescale/fsl-community-bsp-platform/blob/daisy/README#L22

https://github.com/Freescale/Documentation/blob/master/user-guide/source/nsteps.rst
(section 4)


However:
https://github.com/Freescale/fsl-community-bsp-base/blob/master/README#L17


Daiane
Otavio Salvador July 11, 2014, 4:43 p.m. UTC | #5
On Fri, Jul 11, 2014 at 12:46 PM, Daiane Angolini <daiane.list@gmail.com> wrote:
> On Fri, Jul 11, 2014 at 12:24 PM, Otavio Salvador
> <otavio@ossystems.com.br> wrote:
>> On Fri, Jul 11, 2014 at 10:28 AM, Daiane Angolini <daiane.list@gmail.com> wrote:
>>> On Wed, Jul 9, 2014 at 12:25 PM, Trevor Woerner
>>> <trevor.woerner@linaro.org> wrote:
>> ...
>>>> @@ -55,6 +72,10 @@ ARGS=$(getopt --options $SHORTOPTS  \
>>>>    --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
>>>>  # Print the usage menu if invalid options are specified
>>>>  if [ $? != 0 -o $# -lt 1 ]; then
>>>> +   if [ $INVOKEGOOD = 0 ]; then
>>>> +       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
>>>
>>> I hate using dot "." in documentations or log messages (in fact I
>>> don´t use it any more in my command lines) because I cannot *see* it
>>> when I look to the log/doc line.
>>>
>>> Do you see any problem in the use of "source" instead of ". " (dot +
>>> space) as the suggested command line?
>>>
>>> Other than that I don´t see any technical problem with the patch.
>>
>> The suggested line ought to be using "." as this is POSIX compliant.
>> "source" is Bash specific.
>
>
> I cannot accept that the "POSIX compliant" is the final argument in
> this discussion.
>
> I mean, I´m not saying you are wrong. I´m saying I still cannot see
> the dot even being POSIX compliant!
>
> I would suggest to use a glitter shining intermittent red and pink
> dot, instead. However, it´s too girly even to me.
>
> How to highlight the user that *does not know* how to use the tool
> (otherwise the log is useless) to see the dot?
>
> And, this points an error in our README / Doc files:
>
> https://github.com/Freescale/fsl-community-bsp-platform/blob/daisy/README#L22
>
> https://github.com/Freescale/Documentation/blob/master/user-guide/source/nsteps.rst
> (section 4)
>
>
> However:
> https://github.com/Freescale/fsl-community-bsp-base/blob/master/README#L17

We can print something as:

Error: This script needs to be sourced.

In case you are using Bash as your shell, you can use:

    source ./setup-environment ...

Otherwise, you can use:

    . ./setup-environment ...

so we cover both cases.
diff mbox

Patch

diff --git a/setup-environment b/setup-environment
index 5931e2b..d3f89ce 100755
--- a/setup-environment
+++ b/setup-environment
@@ -24,6 +24,23 @@  NCPU=`grep -c processor /proc/cpuinfo`
 CWD=`pwd`
 PROGNAME="setup-environment"
 
+# try to determine if this script has been invoked correctly
+# in other words, make sure it has been "source"d
+# don't exit out immediately, allow the user to ask for -h
+INVOKEGOOD=1
+if [ -n "$ZSH_NAME" ]; then
+    # check for zsh
+    if [ -n "$BASH_SOURCE" ]; then
+        INVOKEGOOD=0
+    fi
+else
+    # check for bash, sh, and dash
+    echo $0 | grep -q $PROGNAME
+    if [ $? -eq 0 ]; then
+        INVOKEGOOD=0
+    fi
+fi
+
 usage()
 {
     echo -e "\nUsage: source $PROGNAME <build-dir>
@@ -55,6 +72,10 @@  ARGS=$(getopt --options $SHORTOPTS  \
   --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
 # Print the usage menu if invalid options are specified
 if [ $? != 0 -o $# -lt 1 ]; then
+   if [ $INVOKEGOOD = 0 ]; then
+       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
+       exit 1
+   fi
    usage && clean_up
    return 1
 fi
@@ -66,6 +87,9 @@  do
         -h|--help)
            usage
            clean_up
+           if [ $INVOKEGOOD = 0 ]; then
+               exit 0
+           fi
            return 0
            ;;
         --)
@@ -75,6 +99,11 @@  do
     esac
 done
 
+if [ $INVOKEGOOD = 0 ]; then
+    echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
+    exit 1
+fi
+
 if [ "$(whoami)" = "root" ]; then
     echo "ERROR: do not use the BSP as root. Exiting..."
 fi