[SHELL32] SHChangeNotify: Use tree for CDirectoryList (#6784)
[reactos.git] / base / applications / regedit / regedit.c
index e49f0e0..0e3cc27 100644 (file)
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include <regedit.h>
-
-
-static const char *usage =
-    "Usage:\n"
-    "    regedit filename\n"
-    "    regedit /E filename [regpath]\n"
-    "    regedit /D regpath\n"
-    "\n"
-    "filename - registry file name\n"
-    "regpath - name of the registry key\n"
-    "\n"
-    "When is called without any switches adds contents of the specified\n"
-    "registry file to the registry\n"
-    "\n"
-    "Switches:\n"
-    "    /E - exports contents of the specified registry key to the specified\n"
-    "  file. Exports the whole registry if no key is specified.\n"
-    "    /D - deletes specified registry key\n"
-    "    /S - silent execution, can be used with any other switch.\n"
-    "  The only existing mode, exists for compatibility with Windows regedit.\n"
-    "    /V - advanced mode, can be used with any other switch.\n"
-    "  Ignored, exists for compatibility with Windows regedit.\n"
-    "    /L - location of system.dat file. Can be used with any other switch.\n"
-    "  Ignored. Exists for compatibility with Windows regedit.\n"
-    "    /R - location of user.dat file. Can be used with any other switch.\n"
-    "  Ignored. Exists for compatibility with Windows regedit.\n"
-    "    /? - print this help. Any other switches are ignored.\n"
-    "    /C - create registry from. Not implemented.\n"
-    "\n"
-    "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
-    "This program is command-line compatible with Microsoft Windows\n"
-    "regedit.\n";
-
-typedef enum
-{
-    ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
-} REGEDIT_ACTION;
+#ifndef __REACTOS__
+#include <stdlib.h>
+#include <windows.h>
+#include <commctrl.h>
+#include <shellapi.h>
 
+#include "wine/debug.h"
+#include "main.h"
+#else
+#include "regedit.h"
+#endif
 
-const CHAR *getAppName(void)
-{
-    return "regedit";
-}
+WINE_DEFAULT_DEBUG_CHANNEL(regedit);
 
-/******************************************************************************
- * Copies file name from command line string to the buffer.
- * Rewinds the command line string pointer to the next non-space character
- * after the file name.
- * Buffer contains an empty string if no filename was found;
- *
- * params:
- * command_line - command line current position pointer
- *      where *s[0] is the first symbol of the file name.
- * file_name - buffer to write the file name to.
- */
-void get_file_name(LPWSTR *command_line, LPWSTR file_name)
+static void output_writeconsole(const WCHAR *str, DWORD wlen)
 {
-    WCHAR *s = *command_line;
-    int pos = 0;                /* position of pointer "s" in *command_line */
-    file_name[0] = 0;
+#ifdef __REACTOS__
+    /* This is win32gui application, don't ever try writing to console.
+     * For the console version we have a separate reg.exe application. */
+    MessageBoxW(NULL, str, NULL, MB_ICONERROR);
+#else
+    DWORD count;
 
-    if (!s[0])
+    if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL))
     {
-        return;
-    }
+        DWORD len;
+        char  *msgA;
 
-    if (s[0] == L'"')
-    {
-        s++;
-        (*command_line)++;
-        while(s[0] != L'"')
-        {
-            if (!s[0])
-            {
-                fprintf(stderr, "%s: Unexpected end of file name!\n", getAppName());
-                exit(1);
-            }
-            s++;
-            pos++;
-        }
-    }
-    else
-    {
-        while(s[0] && !iswspace(s[0]))
-        {
-            s++;
-            pos++;
-        }
-    }
-    memcpy(file_name, *command_line, pos * sizeof((*command_line)[0]));
-    /* remove the last backslash */
-    if (file_name[pos - 1] == L'\\')
-    {
-        file_name[pos - 1] = L'\0';
-    }
-    else
-    {
-        file_name[pos] = L'\0';
-    }
+        /* WriteConsole() fails on Windows if its output is redirected. If this occurs,
+         * we should call WriteFile() with OEM code page.
+         */
+        len = WideCharToMultiByte(GetOEMCP(), 0, str, wlen, NULL, 0, NULL, NULL);
+        msgA = malloc(len);
+        if (!msgA) return;
 
-    if (s[0])
-    {
-        s++;
-        pos++;
-    }
-    while(s[0] && iswspace(s[0]))
-    {
-        s++;
-        pos++;
+        WideCharToMultiByte(GetOEMCP(), 0, str, wlen, msgA, len, NULL, NULL);
+        WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
+        free(msgA);
     }
-    (*command_line) += pos;
+#endif
 }
 
-BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s)
+static void output_formatstring(const WCHAR *fmt, va_list va_args)
 {
-    TCHAR szTitle[256], szText[256];
-    switch (action)
-    {
-    case ACTION_ADD:
-    {
-        WCHAR filename[MAX_PATH];
-        FILE *fp;
-
-        get_file_name(&s, filename);
-        if (!filename[0])
-        {
-            fprintf(stderr, "%s: No file name is specified\n", getAppName());
-            fprintf(stderr, usage);
-            exit(4);
-        }
+    WCHAR *str;
+    DWORD len;
 
-        while(filename[0])
-        {
-            fp = _wfopen(filename, L"r");
-            if (fp == NULL)
-            {
-                LPSTR p = GetMultiByteString(filename);
-                perror("");
-                fprintf(stderr, "%s: Can't open file \"%s\"\n", getAppName(), p);
-                HeapFree(GetProcessHeap(), 0, p);
-                exit(5);
-            }
-            import_registry_file(fp);
-            get_file_name(&s, filename);
-            LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle));
-            LoadString(hInst, IDS_IMPORTED_OK, szText, sizeof(szTitle));
-            /* show successful import */
-            MessageBox(NULL, szText, szTitle, MB_OK);
-        }
-        break;
-    }
-    case ACTION_DELETE:
+#ifdef __REACTOS__
+    SetLastError(NO_ERROR);
+#endif
+    len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
+                         fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
+#ifdef __REACTOS__
+    if (len == 0 && GetLastError() != NO_ERROR)
+#else
+    if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
+#endif
     {
-        WCHAR reg_key_name[KEY_MAX_LEN];
-        get_file_name(&s, reg_key_name);
-        if (!reg_key_name[0])
-        {
-            fprintf(stderr, "%s: No registry key is specified for removal\n", getAppName());
-            fprintf(stderr, usage);
-            exit(6);
-        }
-        delete_registry_key(reg_key_name);
-        break;
+        WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
+        return;
     }
-    case ACTION_EXPORT:
-    {
-        WCHAR filename[MAX_PATH];
+    output_writeconsole(str, len);
+    LocalFree(str);
+}
 
-        filename[0] = _T('\0');
-        get_file_name(&s, filename);
-        if (!filename[0])
-        {
-            fprintf(stderr, "%s: No file name is specified\n", getAppName());
-            fprintf(stderr, usage);
-            exit(7);
-        }
+void WINAPIV output_message(unsigned int id, ...)
+{
+    WCHAR fmt[1536];
+    va_list va_args;
 
-        if (s[0])
-        {
-            WCHAR reg_key_name[KEY_MAX_LEN];
-            get_file_name(&s, reg_key_name);
-            export_registry_key(filename, reg_key_name, REG_FORMAT_4);
-        }
-        else
-        {
-            export_registry_key(filename, NULL, REG_FORMAT_4);
-        }
-        break;
-    }
-    default:
-        fprintf(stderr, "%s: Unhandled action!\n", getAppName());
-        exit(8);
-        break;
+    if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
+    {
+        WINE_FIXME("LoadString failed with %ld\n", GetLastError());
+        return;
     }
-    return TRUE;
+    va_start(va_args, id);
+    output_formatstring(fmt, va_args);
+    va_end(va_args);
 }
 
-/**
- * Process unknown switch.
- *
- * Params:
- *   chu - the switch character in upper-case.
- *   s - the command line string where s points to the switch character.
- */
-static void error_unknown_switch(WCHAR chu, LPWSTR s)
+void WINAPIV error_exit(unsigned int id, ...)
 {
-    if (iswalpha(chu))
-    {
-        fprintf(stderr, "%s: Undefined switch /%c!\n", getAppName(), chu);
-    }
-    else
+    WCHAR fmt[1536];
+    va_list va_args;
+
+    if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
     {
-        fprintf(stderr, "%s: Alphabetic character is expected after '%c' "
-                "in swit ch specification\n", getAppName(), *(s - 1));
+#ifndef __REACTOS__
+        WINE_FIXME("LoadString failed with %lu\n", GetLastError());
+#endif
+        return;
     }
-    exit(1);
+    va_start(va_args, id);
+    output_formatstring(fmt, va_args);
+    va_end(va_args);
+
+    exit(0); /* regedit.exe always terminates with error code zero */
 }
 
-BOOL ProcessCmdLine(LPWSTR lpCmdLine)
+typedef enum {
+    ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
+} REGEDIT_ACTION;
+
+#ifdef __REACTOS__
+static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i, BOOL silent)
+#else
+static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
+#endif
 {
-    REGEDIT_ACTION action = ACTION_UNDEF;
-    LPWSTR s = lpCmdLine;       /* command line pointer */
-    WCHAR ch = *s;              /* current character */
+    switch (action) {
+    case ACTION_ADD: {
+            WCHAR *filename = argv[*i];
+            WCHAR *realname = NULL;
+            FILE *reg_file;
 
-    while (ch && ((ch == L'-') || (ch == L'/')))
-    {
-        WCHAR chu;
-        WCHAR ch2;
-
-        s++;
-        ch = *s;
-        ch2 = *(s + 1);
-        chu = (WCHAR)towupper(ch);
-        if (!ch2 || iswspace(ch2))
-        {
-            if (chu == L'S' || chu == L'V')
+#ifdef __REACTOS__
+            /* Request import confirmation */
+            if (!silent)
             {
-                /* ignore these switches */
+                WCHAR szText[512];
+                int choice;
+                UINT mbType = MB_YESNO;
+
+                LoadStringW(hInst, IDS_IMPORT_PROMPT, szText, ARRAY_SIZE(szText));
+
+                if (argv[*i + 1] != NULL)
+                {
+                    /* Enable three buttons if there's another file coming */
+                    mbType = MB_YESNOCANCEL;
+                }
+
+                choice = InfoMessageBox(NULL, mbType | MB_ICONQUESTION, szTitle, szText, filename);
+                switch (choice)
+                {
+                    case IDNO:
+                        return;
+                    case IDCANCEL:
+                        /* The cancel case is useful if the user is importing more than one registry file
+                         * at a time, and wants to back out anytime during the import process. This way, the
+                         * user doesn't have to resort to ending the regedit process abruptly just to cancel
+                         * the operation.
+                         * To achieve this, we skip all further command line arguments.
+                         */
+                        *i = INT_MAX - 1;
+                        return;
+                    default:
+                        break;
+                }
             }
+#endif
+            if (!lstrcmpW(filename, L"-"))
+                reg_file = stdin;
             else
             {
-                switch (chu)
+                int size;
+
+                size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
+                if (size > 0)
                 {
-                case L'D':
-                    action = ACTION_DELETE;
-                    break;
-                case L'E':
-                    action = ACTION_EXPORT;
-                    break;
-                case L'?':
-                    fprintf(stderr, usage);
-                    exit(3);
-                    break;
-                default:
-                    error_unknown_switch(chu, s);
-                    break;
+                    realname = malloc(size * sizeof(WCHAR));
+                    size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
                 }
-            }
-            s++;
-        }
-        else
-        {
-            if (ch2 == L':')
-            {
-                switch (chu)
+                if (size == 0)
                 {
-                case L'L':
-                    /* fall through */
-                case L'R':
-                    s += 2;
-                    while (*s && !iswspace(*s))
-                    {
-                        s++;
-                    }
-                    break;
-                default:
-                    error_unknown_switch(chu, s);
-                    break;
+                    output_message(STRING_FILE_NOT_FOUND, filename);
+                    free(realname);
+                    return;
+                }
+                reg_file = _wfopen(realname, L"rb");
+                if (reg_file == NULL)
+                {
+                    _wperror(L"regedit");
+                    output_message(STRING_CANNOT_OPEN_FILE, filename);
+                    free(realname);
+                    return;
                 }
             }
-            else
+            import_registry_file(reg_file);
+            if (realname)
             {
-                /* this is a file name, starting from '/' */
-                s--;
-                break;
+                free(realname);
+                fclose(reg_file);
             }
+            break;
+        }
+    case ACTION_DELETE:
+            delete_registry_key(argv[*i]);
+            break;
+    case ACTION_EXPORT: {
+            WCHAR *filename = argv[*i];
+            WCHAR *key_name = argv[++(*i)];
+
+            if (key_name && *key_name)
+                export_registry_key(filename, key_name, REG_FORMAT_5);
+            else
+                export_registry_key(filename, NULL, REG_FORMAT_5);
+            break;
         }
-        /* skip spaces to the next parameter */
-        ch = *s;
-        while (ch && iswspace(ch))
+    default:
+#ifdef __REACTOS__
+        output_message(STRING_UNHANDLED_ACTION);
+#else
+        error_exit(STRING_UNHANDLED_ACTION);
+#endif
+        break;
+    }
+}
+
+BOOL ProcessCmdLine(WCHAR *cmdline)
+{
+    WCHAR **argv;
+    int argc, i;
+    REGEDIT_ACTION action = ACTION_ADD;
+#ifdef __REACTOS__
+    BOOL silent = FALSE;
+#endif
+
+    argv = CommandLineToArgvW(cmdline, &argc);
+
+    if (!argv)
+        return FALSE;
+
+    if (argc == 1)
+    {
+        LocalFree(argv);
+        return FALSE;
+    }
+
+    for (i = 1; i < argc; i++)
+    {
+        if (argv[i][0] != '/' && argv[i][0] != '-')
+            break; /* No flags specified. */
+
+        if (!argv[i][1] && argv[i][0] == '-')
+            break; /* '-' is a filename. It indicates we should use stdin. */
+
+        if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
+            break; /* This is a file path beginning with '/'. */
+
+        switch (towupper(argv[i][1]))
         {
-            s++;
-            ch = *s;
+        case '?':
+            error_exit(STRING_USAGE);
+            break;
+        case 'D':
+            action = ACTION_DELETE;
+            break;
+        case 'E':
+            action = ACTION_EXPORT;
+            break;
+        case 'C':
+        case 'L':
+        case 'M':
+        case 'R':
+            /* unhandled */;
+            break;
+        case 'S':
+#ifdef __REACTOS__
+            silent = TRUE;
+            break;
+#endif
+        case 'V':
+            /* ignored */;
+            break;
+        default:
+            output_message(STRING_INVALID_SWITCH, argv[i]);
+            error_exit(STRING_HELP);
         }
     }
 
-    if (*s && action == ACTION_UNDEF)
-           {
-         TCHAR szTitle[256], szText[256];
-         LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle));
-         LoadString(hInst, IDS_IMPORT_PROMPT, szText, sizeof(szTitle));        
-         /* request import confirmation */
-            if (MessageBox(NULL, szText, szTitle, MB_YESNO) == IDYES) 
-            {
-          action = ACTION_ADD;
-         }
-                else return TRUE;
+    if (i == argc)
+    {
+        switch (action)
+        {
+        case ACTION_ADD:
+        case ACTION_EXPORT:
+            output_message(STRING_NO_FILENAME);
+            break;
+        case ACTION_DELETE:
+            output_message(STRING_NO_REG_KEY);
+            break;
         }
-       if (action == ACTION_UNDEF)
-        return FALSE;
+        error_exit(STRING_HELP);
+    }
 
-    return PerformRegAction(action, s);
+    for (; i < argc; i++)
+#ifdef __REACTOS__
+        PerformRegAction(action, argv, &i, silent);
+#else
+        PerformRegAction(action, argv, &i);
+#endif
+
+    LocalFree(argv);
+
+    return TRUE;
 }