[PATCH] reg: Parse the registry operation using an enum

Hugh McMaster hugh.mcmaster at outlook.com
Wed Jun 1 06:32:43 CDT 2016


Signed-off-by: Hugh McMaster <hugh.mcmaster at outlook.com>
---
 programs/reg/reg.c | 84 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 71 insertions(+), 13 deletions(-)

diff --git a/programs/reg/reg.c b/programs/reg/reg.c
index e3c2fdc..02b6156 100644
--- a/programs/reg/reg.c
+++ b/programs/reg/reg.c
@@ -19,6 +19,7 @@
 #include <windows.h>
 #include <wine/unicode.h>
 #include <wine/debug.h>
+#include <stdlib.h>
 #include <errno.h>
 #include "reg.h"
 
@@ -119,18 +120,24 @@ static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
     LocalFree(str);
 }
 
-static void __cdecl output_message(unsigned int id, ...)
+static void output_loadstring(unsigned int msgid, __ms_va_list va_args)
 {
     WCHAR fmt[1024];
-    __ms_va_list va_args;
 
-    if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
+    if (!LoadStringW(GetModuleHandleW(NULL), msgid, fmt, ARRAY_SIZE(fmt)))
     {
         WINE_FIXME("LoadString failed with %d\n", GetLastError());
         return;
     }
-    __ms_va_start(va_args, id);
     output_formatstring(fmt, va_args);
+}
+
+static void __cdecl output_message(unsigned int id, ...)
+{
+    __ms_va_list va_args;
+
+    __ms_va_start(va_args, id);
+    output_loadstring(id, va_args);
     __ms_va_end(va_args);
 }
 
@@ -143,6 +150,17 @@ static void __cdecl output_string(const WCHAR *fmt, ...)
     __ms_va_end(va_args);
 }
 
+static void __cdecl error_exit(unsigned int msgid, ...)
+{
+    __ms_va_list va_args;
+
+    __ms_va_start(va_args, msgid);
+    output_loadstring(msgid, va_args);
+    __ms_va_end(va_args);
+
+    exit(1);
+}
+
 /* ask_confirm() adapted from programs/cmd/builtins.c */
 static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
 {
@@ -877,13 +895,51 @@ static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, BOOL
     return ret;
 }
 
-int wmain(int argc, WCHAR *argvW[])
+static BOOL is_help_switch(const WCHAR *str)
 {
-    int i;
+    if (strlenW(str) > 2)
+        return FALSE;
+
+    if ((str[0] == '/' || str[0] == '-') && (str[1] == 'h' || str[1] == '?'))
+        return TRUE;
+
+    return FALSE;
+}
+
+enum operations {
+    REG_INVALID,
+    REG_HELP,
+    REG_ADD,
+    REG_DELETE,
+    REG_QUERY
+};
+
+static const WCHAR addW[] = {'a','d','d',0};
+static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
+static const WCHAR queryW[] = {'q','u','e','r','y',0};
+
+static enum operations get_operation(const WCHAR *str)
+{
+    if (!lstrcmpiW(str, addW))
+        return REG_ADD;
+
+    if (!lstrcmpiW(str, deleteW))
+        return REG_DELETE;
+
+    if (!lstrcmpiW(str, queryW))
+        return REG_QUERY;
 
-    static const WCHAR addW[] = {'a','d','d',0};
-    static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
-    static const WCHAR queryW[] = {'q','u','e','r','y',0};
+    if (is_help_switch(str))
+        return REG_HELP;
+
+    output_message(STRING_INVALID_OPTION, str);
+    error_exit(STRING_REG_HELP);
+    return REG_INVALID;
+}
+
+int wmain(int argc, WCHAR *argvW[])
+{
+    int i, op;
     static const WCHAR slashDW[] = {'/','d',0};
     static const WCHAR slashFW[] = {'/','f',0};
     static const WCHAR slashHW[] = {'/','h',0};
@@ -901,13 +957,15 @@ int wmain(int argc, WCHAR *argvW[])
         return 1;
     }
 
-    if (!lstrcmpW(argvW[1], slashHelpW) || !lstrcmpiW(argvW[1], slashHW))
+    op = get_operation(argvW[1]);
+
+    if (op == REG_HELP)
     {
         output_message(STRING_USAGE);
         return 0;
     }
 
-    if (!lstrcmpiW(argvW[1], addW))
+    if (op == REG_ADD)
     {
         WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
         BOOL value_empty = FALSE, force = FALSE;
@@ -965,7 +1023,7 @@ int wmain(int argc, WCHAR *argvW[])
         return reg_add(key_name, value_name, value_empty, type, separator,
             data, force);
     }
-    else if (!lstrcmpiW(argvW[1], deleteW))
+    else if (op == REG_DELETE)
     {
         WCHAR *key_name, *value_name = NULL;
         BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
@@ -1003,7 +1061,7 @@ int wmain(int argc, WCHAR *argvW[])
         }
         return reg_delete(key_name, value_name, value_empty, value_all, force);
     }
-    else if (!lstrcmpiW(argvW[1], queryW))
+    else if (op == REG_QUERY)
     {
         WCHAR *key_name, *value_name = NULL;
         BOOL value_empty = FALSE, recurse = FALSE;
-- 
1.9.1




More information about the wine-patches mailing list