[PATCH 2/2] winepath: add option to escape the output

Lei Zhang thestig at google.com
Sun Jul 15 05:36:05 CDT 2007


Hi,

It's also nice to be able to get the escaped path, so I added the
--escaped option to winepath.
-------------- next part --------------
From 984c5733697aad75b1cc0dd0a1e509ad3d30f941 Mon Sep 17 00:00:00 2001
From: Lei Zhang <thestig at google.com>
Date: Sun, 15 Jul 2007 03:03:08 -0700
Subject: [PATCH 2/2] winepath: add option to escape the output
---
 programs/winepath/winepath.c |   56 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/programs/winepath/winepath.c b/programs/winepath/winepath.c
index d7605a8..b60d293 100644
--- a/programs/winepath/winepath.c
+++ b/programs/winepath/winepath.c
@@ -36,6 +36,7 @@ enum {
     UNIXFORMAT    = 4,
     WINDOWSFORMAT = 8,
     ANSIFORMAT    = 16,
+    ESCFORMAT     = 32,
 };
 
 static const char progname[] = "winepath";
@@ -56,6 +57,7 @@ static int option(int shortopt, const WC
     "                directory to the short format\n"
     "  -a, --ansi    output a Windows path in the current Windows ANSI code\n"
     "                page\n"
+    "  -e, --escaped escape the output\n"
     "  -h, --help    output this help message and exit\n"
     "  -v, --version output version information and exit\n"
     "\n"
@@ -81,6 +83,8 @@ static int option(int shortopt, const WC
             return WINDOWSFORMAT;
         case 'a':
             return ANSIFORMAT;
+        case 'e':
+            return ESCFORMAT;
     }
 
     fprintf(stderr, "%s: invalid option ", progname);
@@ -102,10 +106,11 @@ static int parse_options(const WCHAR *ar
     static const WCHAR unixW[] = { 'u','n','i','x',0 };
     static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
     static const WCHAR ansiW[] = { 'a','n','s','i',0 };
+    static const WCHAR escapedW[] = { 'e','s','c','a','p','e','d',0 };
     static const WCHAR helpW[] = { 'h','e','l','p',0 };
     static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
     static const WCHAR nullW[] = { 0 };
-    static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, ansiW, helpW, versionW, nullW };
+    static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, ansiW, escapedW, helpW, versionW, nullW };
     int outputformats = 0;
     int done = 0;
     int i, j;
@@ -144,10 +149,45 @@ static int parse_options(const WCHAR *ar
 }
 
 /*
+ * Output path
+ */
+static void print_output(const char * path, BOOL escaped, BOOL endline)
+{
+    if (escaped)
+    {
+        int i;
+        for (i = 0; i < strlen(path); i++)
+        {
+            if (path[i] >= ' ' && path[i] <= '~')
+            {
+                switch (path[i])
+                {
+                    case ' ':
+                    case '\\':
+                    case '\'':
+                    case '\"':
+                        printf("\\%c", path[i]);
+                        break;
+                    default:
+                        printf("%c", path[i]);
+                }
+            }
+            else
+                printf("\\x%02x", (unsigned char) path[i]);
+        }
+    }
+    else
+        printf("%s", path);
+    if (endline)
+        printf("\n");
+}
+
+/*
  * Main function
  */
 int wmain(int argc, const WCHAR *argv[])
 {
+    BOOL escaped = FALSE;
     LPSTR (*wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
     LPWSTR (*wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
     UINT code_page = CP_UNIXCP;
@@ -184,6 +224,8 @@ int wmain(int argc, const WCHAR *argv[])
 
     if (outputformats & ANSIFORMAT)
         code_page = CP_ACP;
+    if (outputformats & ESCFORMAT)
+        escaped = TRUE;
 
     for (i = 1; argv[i]; i++)
     {
@@ -191,12 +233,12 @@ int wmain(int argc, const WCHAR *argv[])
         if (outputformats & LONGFORMAT) {
             if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
                 WideCharToMultiByte(code_page, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
-            printf("%s\n", path);
+            print_output(path, escaped, TRUE);
         }
         if (outputformats & SHORTFORMAT) {
             if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
                 WideCharToMultiByte(code_page, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
-            printf("%s\n", path);
+            print_output(path, escaped, TRUE);
         }
         if (outputformats & UNIXFORMAT) {
             WCHAR *ntpath, *tail;
@@ -215,11 +257,13 @@ int wmain(int argc, const WCHAR *argv[])
                     if (tail)
                     {
                         WideCharToMultiByte(CP_UNIXCP, 0, tail+1, -1, path, MAX_PATH, NULL, NULL);
-                        printf("%s/%s\n", unix_name, path);
+                        print_output(unix_name, escaped, FALSE);
+                        print_output("/", escaped, FALSE);
+                        print_output(path, escaped, TRUE);
                     }
                     else
                     {
-                        printf("%s\n", unix_name);
+                        print_output(path, escaped, TRUE);
                     }
                     HeapFree( GetProcessHeap(), 0, unix_name );
                     break;
@@ -267,7 +311,7 @@ int wmain(int argc, const WCHAR *argv[])
             if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
             {
                 WideCharToMultiByte(code_page, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
-                printf("%s\n", path);
+                print_output(path, escaped, TRUE);
                 HeapFree( GetProcessHeap(), 0, windows_name );
             }
             else printf( "\n" );
-- 
1.4.1


More information about the wine-patches mailing list