From e82a02945365ef4165d660c79804a7445242083b Mon Sep 17 00:00:00 2001
From: mark <mark@augsburg>
Date: Fri, 10 Feb 2017 14:10:00 +0100
Subject: [PATCH] ENH: add help/usage for dirToString, wmkdep. Add dirToString
 -strip option.

- The dirToString -strip option is simple, but reduces effort for the caller.
---
 wmake/scripts/makeFiles   | 28 +++++++++-------------
 wmake/scripts/makeOptions |  2 +-
 wmake/src/dirToString.c   | 50 ++++++++++++++++++++++++++++++++++-----
 wmake/src/wmkdep.l        | 37 ++++++++++++++++++++++-------
 4 files changed, 84 insertions(+), 33 deletions(-)

diff --git a/wmake/scripts/makeFiles b/wmake/scripts/makeFiles
index 8d5a2c76f3f..78b7fffed3b 100755
--- a/wmake/scripts/makeFiles
+++ b/wmake/scripts/makeFiles
@@ -34,7 +34,7 @@
 
 if [ -r Make/files ]
 then
-    echo "Error: Make/files already exists - exiting"
+    echo "Error: Make/files already exists - exiting" 1>&2
     exit 1
 fi
 
@@ -43,38 +43,32 @@ dirToString=$WM_DIR/platforms/$WM_ARCH$WM_COMPILER/dirToString
 [ -d Make ] || mkdir Make
 rm -f Make/files
 
-for dir in `find . -type d -print`
+for dir in $(find . -mindepth 1 -type d -print)
 do
     case "$dir" in
     . | ./Make | ./lnInclude )
         # Skip special directories
         ;;
     *)
-        baseDir=`echo $dir | sed 's%^\./%%'`
-        baseDirName=`echo $baseDir | $dirToString`
-
-        echo $baseDirName " = " $baseDir >> Make/files
+        echo "$(echo $dir | $dirToString -strip)  =  ${dir#./}"
         ;;
     esac
-done
-
-echo >> Make/files
+done >> Make/files
+[ -s Make/files ] && echo >> Make/files
 
-for file in `find . -name "*.[cCylLfF]" -type f -print`
+for file in $(find . -type f -name "*.[cCylLfF]" -print)
 do
-    fileName=${file##*/}
-    pathName=`echo ${file%/*} | sed -e 's%^\.%%' -e 's%^/%%' | $dirToString`
+    pathName=$(echo ${file%/*} | $dirToString -strip)
 
     if [ -n "$pathName" ]
     then
-        echo '$('$pathName')/'$fileName >> Make/files
+        echo '$('$pathName')/'"${file##*/}"
     else
-        echo $fileName >> Make/files
+        echo "${file##*/}"
     fi
-done
+done >> Make/files
 
 echo >> Make/files
-
-echo 'EXE = $(FOAM_APPBIN)/'${PWD##*/} >> Make/files
+echo 'EXE = $(FOAM_APPBIN)/'"${PWD##*/}" >> Make/files
 
 #------------------------------------------------------------------------------
diff --git a/wmake/scripts/makeOptions b/wmake/scripts/makeOptions
index 56ba6009ce4..62f7e88e5ef 100755
--- a/wmake/scripts/makeOptions
+++ b/wmake/scripts/makeOptions
@@ -34,7 +34,7 @@
 
 if [ -r Make/options ]
 then
-    echo "Error: Make/options already exists - exiting"
+    echo "Error: Make/options already exists - exiting" 1>&2
     exit 1
 fi
 
diff --git a/wmake/src/dirToString.c b/wmake/src/dirToString.c
index 4200fd89d85..1ce3e9455f7 100644
--- a/wmake/src/dirToString.c
+++ b/wmake/src/dirToString.c
@@ -3,7 +3,7 @@
  \\      /   F ield          | OpenFOAM: The Open Source CFD Toolbox
   \\    /    O peration      |
    \\  /     A nd            | Copyright (C) 2011 OpenFOAM Foundation
-    \\/      M anipulation   |
+    \\/      M anipulation   | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -33,22 +33,61 @@ Usage
 
     e.g.
         using sh
-        baseDirName=`echo $dir | sed 's%^\./%%' | $bin/dirToString`
+        baseDirName=$(echo $dir | $bin/dirToString -strip)
 
         using csh
-        set baseDirName=`echo $dir | sed 's%^\./%%' | $bin/dirToString`
+        set baseDirName=`echo $dir | $bin/dirToString -strip`
 
 \*----------------------------------------------------------------------------*/
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 
-int main()
+int main(int argc, char* argv[])
 {
     int c;
     int nextupper = 0;
 
+    if (argc > 1)
+    {
+        if (!strncmp(argv[1], "-h", 2))  /* -h, -help */
+        {
+            fprintf
+            (
+                stderr,
+                "\nUsage: %s [-strip]\n\n",
+                "dirToString"
+            );
+
+            fprintf
+            (
+                stderr,
+                "  -strip    ignore leading [./] characters.\n\n"
+                "Transform dir1/dir2 to camel-case dir1Dir2\n\n"
+            );
+
+            return 0;
+        }
+
+        if (!strcmp(argv[1], "-s") || !strcmp(argv[1], "-strip"))  /* -s, -strip */
+        {
+            while ((c=getchar()) != EOF && (c == '.' || c == '/'))
+            {
+                /* nop */
+            }
+
+            if (c == EOF)
+            {
+                return 0;
+            }
+
+            putchar(c);
+        }
+    }
+
+
     while ((c=getchar()) != EOF)
     {
         if (c == '/')
@@ -60,13 +99,12 @@ int main()
             if (nextupper)
             {
                 putchar(toupper(c));
+                nextupper = 0;
             }
             else
             {
                 putchar(c);
             }
-
-            nextupper = 0;
         }
     }
 
diff --git a/wmake/src/wmkdep.l b/wmake/src/wmkdep.l
index b0b48d1055d..4b0aeaa85a6 100644
--- a/wmake/src/wmkdep.l
+++ b/wmake/src/wmkdep.l
@@ -4,7 +4,7 @@
  \\      /   F ield          | OpenFOAM: The Open Source CFD Toolbox
   \\    /    O peration      |
    \\  /     A nd            | Copyright (C) 2011-2016 OpenFOAM Foundation
-    \\/      M anipulation   |
+    \\/      M anipulation   | Copyright (C) 2017 OpenCFD Ltd.
 ------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -147,10 +147,29 @@ int main(int argc, char* argv[])
     char *basePos, *dotPos;
     int i, silent;
 
-    if (argc == 1)
+    if (argc < 2)
     {
         fprintf(stderr, "input file not supplied\n");
-        exit(1);
+        return 1;
+    }
+    else if (!strncmp(argv[1], "-h", 2)) /* -h, -help */
+    {
+        fprintf
+        (
+            stderr,
+            "\nUsage: %s [-Idir ... -Idir] [-iheader .. -iheader] filename\n\n",
+            "wmkdep"
+        );
+
+        fprintf
+        (
+            stderr,
+            "  -Idir     Directories to be searched for headers.\n"
+            "  -iheader  Headers to be ignored.\n\n"
+            "Dependency list generator, similar to 'cpp -M'\n\n"
+        );
+
+        return 0;
     }
 
     sourceFile = strdup(argv[argc-1]);
@@ -161,7 +180,7 @@ int main(int argc, char* argv[])
     }
     else
     {
-        basePos++;
+        ++basePos;
     }
 
     if
@@ -176,18 +195,18 @@ int main(int argc, char* argv[])
             "cannot find extension in source file name %s\n",
             sourceFile
         );
-        exit(1);
+        return 1;
     }
 
     /* count number of -I directories */
     nDirectories = 0;
-    for (i = 1; i < argc; i++)
+    for (i = 1; i < argc; ++i)
     {
         if (strncmp(argv[i], "-I", 2) == 0)
         {
             if (strlen(argv[i]) > 2)
             {
-                nDirectories++;
+                ++nDirectories;
             }
         }
     }
@@ -196,7 +215,7 @@ int main(int argc, char* argv[])
 
     /* build list of -I directories and add -i ignores */
     nDirectories = 0;
-    for (i = 1; i < argc; i++)
+    for (i = 1; i < argc; ++i)
     {
         if (strncmp(argv[i], "-I", 2) == 0)
         {
@@ -240,7 +259,7 @@ int main(int argc, char* argv[])
 
     puts("\n");
 
-    for (i = 0; i < nDirectories; i++)
+    for (i = 0; i < nDirectories; ++i)
     {
         free(directories[i]);
     }
-- 
GitLab