This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 266049bd11b612f4fce57a9d8c956a2581042007
Author: wangjianyu3 <[email protected]>
AuthorDate: Mon Sep 30 15:04:04 2024 +0800

    tools/mksymtab.sh: Using getopts to parse parameters
    
    Use the "-a" option to specify additional lists
    
    Examples
      - The basic.txt
        $ cat basic.txt
        basic_func0
        basic_func1
        basic_func2
    
      - The additional.txt
        $ cat additional.txt
        additional_func0
        additional_func1
        additional_func2
    
      1. Get symbols from directory "EMPTY_DIR" and additional list basic.txt
        ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt
        #if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)
        const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] =
        #elif defined(CONFIG_NSH_SYMTAB)
        const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] =
        #else
        const struct symtab_s dummy_symtab[] =
        #endif
        {
          {"basic_func0", &basic_func0},
          {"basic_func1", &basic_func1},
          {"basic_func2", &basic_func2},
        };
    
      2. Get symbols from directory "EMPTY_DIR" and two additional lists 
basic.txt, additional.txt
        ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt -a additional.txt
        #if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)
        const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] =
        #elif defined(CONFIG_NSH_SYMTAB)
        const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] =
        #else
        const struct symtab_s dummy_symtab[] =
        #endif
        {
          {"additional_func0", &additional_func0},
          {"additional_func1", &additional_func1},
          {"additional_func2", &additional_func2},
          {"basic_func0", &basic_func0},
          {"basic_func1", &basic_func1},
          {"basic_func2", &basic_func2},
        };
    
      3. Set prefix and get symbols from directory "EMPTY_DIR" and two 
additional lists basic.txt, additional.txt
        ./tools/mksymtab.sh ./EMPTY_DIR PREFIX_TEST  -a basic.txt -a 
additional.txt
        const struct symtab_s PREFIX_TEST_exports[] =
        {
          {"additional_func0", &additional_func0},
          {"additional_func1", &additional_func1},
          {"additional_func2", &additional_func2},
          {"basic_func0", &basic_func0},
          {"basic_func1", &basic_func1},
          {"basic_func2", &basic_func2},
        };
    
      4. Error: Missing <imagedirpath>
        $ ./tools/mksymtab.sh
        ERROR: Missing <imagedirpath>
    
        Usage: ./tools/mksymtab.sh <imagedirpath> [symtabprefix] [-a 
additionalsymbolspath]
    
    UNSUPPORTED usage examples
      # `getopt` supports these, but the usage in GNU and macOS is incompatible.
      - ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt PREFIX_TEST -a 
additional.txt
      - ./tools/mksymtab.sh -a basic.txt ./EMPTY_DIR PREFIX_TEST -a 
additional.txt
    
    References
      BASH(1)   -- getopts
      GETOPT(1) -- getopt
    
    Signed-off-by: wangjianyu3 <[email protected]>
---
 tools/mksymtab.sh | 60 ++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 20 deletions(-)

diff --git a/tools/mksymtab.sh b/tools/mksymtab.sh
index c53aab6a4..08d02373c 100755
--- a/tools/mksymtab.sh
+++ b/tools/mksymtab.sh
@@ -21,21 +21,45 @@
 
 export LC_ALL=C
 
-usage="Usage: $0 <imagedirpath> [symtabprefix [additionalsymbolspath]]"
+usage() {
+  if [ $# -ne 0 ]; then
+    echo "ERROR: $@"
+  fi
+  echo -e "\nUsage: $0 <imagedirpath> [symtabprefix] [-a 
additionalsymbolspath]"
+  exit 1
+}
 
 # Check for the required directory path
 
 dir=$1
 if [ -z "$dir" ]; then
-  echo "ERROR: Missing <imagedirpath>"
-  echo ""
-  echo $usage
-  exit 1
+  usage "Missing <imagedirpath>"
 fi
 
 # Get the symbol table prefix
 
-prefix=$2
+if [ "x${2:0:1}" != "x-" ]; then
+  prefix=$2
+  OPTIND=3
+else
+  OPTIND=2
+fi
+
+# Parse remaining arguments
+
+while getopts a: opt; do
+  case $opt in
+    a)
+      addlist="${addlist[@]} $OPTARG"
+      ;;
+    \?)
+      usage
+  esac
+done
+
+if [ $OPTIND != $(($# + 1)) ]; then
+  usage "Arguments remaining: \"${@:$OPTIND}\""
+fi
 
 # Extract all of the undefined symbols from the ELF files and create a
 # list of sorted, unique undefined variable names.
@@ -59,20 +83,16 @@ if [ -z "$varlist" ]; then
   fi
 fi
 
-if [ $# -gt 2 ]; then
-  shift 2
-  for add_sym in $@; do
-    if [ -f $add_sym ]; then
-      varlist="${varlist}\n$(cat $add_sym | grep -v "^,.*")"
-    elif [ -d $add_sym ]; then
-      varlist="${varlist}\n$(find $add_sym -type f | xargs cat | grep -v 
"^,.*")"
-    else
-      echo $usage
-      exit 1
-    fi
-    varlist=$(echo -e "${varlist}" | sort -u)
-  done
-fi
+for addsym in ${addlist[@]}; do
+  if [ -f $addsym ]; then
+    varlist="${varlist}\n$(cat $addsym | grep -v "^,.*")"
+  elif [ -d $addsym ]; then
+    varlist="${varlist}\n$(find $addsym -type f | xargs cat | grep -v "^,.*")"
+  else
+    usage
+  fi
+  varlist=$(echo -e "${varlist}" | sort -u)
+done
 
 # Now output the symbol table as a structure in a C source file.  All
 # undefined symbols are declared as void* types.  If the toolchain does

Reply via email to