All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
@ 2014-07-09 15:25 Trevor Woerner
  2014-07-11 13:28 ` Daiane Angolini
  0 siblings, 1 reply; 6+ messages in thread
From: Trevor Woerner @ 2014-07-09 15:25 UTC (permalink / raw)
  To: meta-freescale; +Cc: Otavio Salvador, patches

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'"
+       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



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
  2014-07-09 15:25 [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced Trevor Woerner
@ 2014-07-11 13:28 ` Daiane Angolini
  2014-07-11 14:44   ` Trevor Woerner
  2014-07-11 15:24   ` Otavio Salvador
  0 siblings, 2 replies; 6+ messages in thread
From: Daiane Angolini @ 2014-07-11 13:28 UTC (permalink / raw)
  To: Trevor Woerner; +Cc: meta-freescale, Otavio Salvador, patches

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
  2014-07-11 13:28 ` Daiane Angolini
@ 2014-07-11 14:44   ` Trevor Woerner
  2014-07-11 15:24   ` Otavio Salvador
  1 sibling, 0 replies; 6+ messages in thread
From: Trevor Woerner @ 2014-07-11 14:44 UTC (permalink / raw)
  To: Daiane Angolini; +Cc: meta-freescale, Otavio Salvador, patches

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 :-)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
  2014-07-11 13:28 ` Daiane Angolini
  2014-07-11 14:44   ` Trevor Woerner
@ 2014-07-11 15:24   ` Otavio Salvador
  2014-07-11 15:46     ` Daiane Angolini
  1 sibling, 1 reply; 6+ messages in thread
From: Otavio Salvador @ 2014-07-11 15:24 UTC (permalink / raw)
  To: Daiane Angolini; +Cc: meta-freescale, patches

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.

-- 
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
  2014-07-11 15:24   ` Otavio Salvador
@ 2014-07-11 15:46     ` Daiane Angolini
  2014-07-11 16:43       ` Otavio Salvador
  0 siblings, 1 reply; 6+ messages in thread
From: Daiane Angolini @ 2014-07-11 15:46 UTC (permalink / raw)
  To: Otavio Salvador; +Cc: meta-freescale, patches

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced
  2014-07-11 15:46     ` Daiane Angolini
@ 2014-07-11 16:43       ` Otavio Salvador
  0 siblings, 0 replies; 6+ messages in thread
From: Otavio Salvador @ 2014-07-11 16:43 UTC (permalink / raw)
  To: Daiane Angolini; +Cc: meta-freescale, patches

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.

-- 
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-07-11 16:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-09 15:25 [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced Trevor Woerner
2014-07-11 13:28 ` Daiane Angolini
2014-07-11 14:44   ` Trevor Woerner
2014-07-11 15:24   ` Otavio Salvador
2014-07-11 15:46     ` Daiane Angolini
2014-07-11 16:43       ` Otavio Salvador

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.