Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenregedit.c
Go to the documentation of this file.
00001 /* 00002 * Windows regedit.exe registry editor implementation. 00003 * 00004 * Copyright 2002 Andriy Palamarchuk 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include <regedit.h> 00022 00023 00024 static const char *usage = 00025 "Usage:\n" 00026 " regedit filename\n" 00027 " regedit /E filename [regpath]\n" 00028 " regedit /D regpath\n" 00029 "\n" 00030 "filename - registry file name\n" 00031 "regpath - name of the registry key\n" 00032 "\n" 00033 "When is called without any switches adds contents of the specified\n" 00034 "registry file to the registry\n" 00035 "\n" 00036 "Switches:\n" 00037 " /E - exports contents of the specified registry key to the specified\n" 00038 " file. Exports the whole registry if no key is specified.\n" 00039 " /D - deletes specified registry key\n" 00040 " /S - silent execution, can be used with any other switch.\n" 00041 " The only existing mode, exists for compatibility with Windows regedit.\n" 00042 " /V - advanced mode, can be used with any other switch.\n" 00043 " Ignored, exists for compatibility with Windows regedit.\n" 00044 " /L - location of system.dat file. Can be used with any other switch.\n" 00045 " Ignored. Exists for compatibility with Windows regedit.\n" 00046 " /R - location of user.dat file. Can be used with any other switch.\n" 00047 " Ignored. Exists for compatibility with Windows regedit.\n" 00048 " /? - print this help. Any other switches are ignored.\n" 00049 " /C - create registry from. Not implemented.\n" 00050 "\n" 00051 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n" 00052 "This program is command-line compatible with Microsoft Windows\n" 00053 "regedit.\n"; 00054 00055 typedef enum 00056 { 00057 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE 00058 } REGEDIT_ACTION; 00059 00060 00061 const CHAR *getAppName(void) 00062 { 00063 return "regedit"; 00064 } 00065 00066 /****************************************************************************** 00067 * Copies file name from command line string to the buffer. 00068 * Rewinds the command line string pointer to the next non-space character 00069 * after the file name. 00070 * Buffer contains an empty string if no filename was found; 00071 * 00072 * params: 00073 * command_line - command line current position pointer 00074 * where *s[0] is the first symbol of the file name. 00075 * file_name - buffer to write the file name to. 00076 */ 00077 void get_file_name(LPWSTR *command_line, LPWSTR file_name) 00078 { 00079 WCHAR *s = *command_line; 00080 int pos = 0; /* position of pointer "s" in *command_line */ 00081 file_name[0] = 0; 00082 00083 if (!s[0]) 00084 { 00085 return; 00086 } 00087 00088 if (s[0] == L'"') 00089 { 00090 s++; 00091 (*command_line)++; 00092 while(s[0] != L'"') 00093 { 00094 if (!s[0]) 00095 { 00096 fprintf(stderr, "%s: Unexpected end of file name!\n", getAppName()); 00097 exit(1); 00098 } 00099 s++; 00100 pos++; 00101 } 00102 } 00103 else 00104 { 00105 while(s[0] && !iswspace(s[0])) 00106 { 00107 s++; 00108 pos++; 00109 } 00110 } 00111 memcpy(file_name, *command_line, pos * sizeof((*command_line)[0])); 00112 /* remove the last backslash */ 00113 if (file_name[pos - 1] == L'\\') 00114 { 00115 file_name[pos - 1] = L'\0'; 00116 } 00117 else 00118 { 00119 file_name[pos] = L'\0'; 00120 } 00121 00122 if (s[0]) 00123 { 00124 s++; 00125 pos++; 00126 } 00127 while(s[0] && iswspace(s[0])) 00128 { 00129 s++; 00130 pos++; 00131 } 00132 (*command_line) += pos; 00133 } 00134 00135 BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s) 00136 { 00137 TCHAR szTitle[256], szText[256]; 00138 switch (action) 00139 { 00140 case ACTION_ADD: 00141 { 00142 WCHAR filename[MAX_PATH]; 00143 FILE *fp; 00144 00145 get_file_name(&s, filename); 00146 if (!filename[0]) 00147 { 00148 fprintf(stderr, "%s: No file name is specified\n", getAppName()); 00149 fprintf(stderr, usage); 00150 exit(4); 00151 } 00152 00153 while(filename[0]) 00154 { 00155 fp = _wfopen(filename, L"r"); 00156 if (fp == NULL) 00157 { 00158 LPSTR p = GetMultiByteString(filename); 00159 perror(""); 00160 fprintf(stderr, "%s: Can't open file \"%s\"\n", getAppName(), p); 00161 HeapFree(GetProcessHeap(), 0, p); 00162 exit(5); 00163 } 00164 import_registry_file(fp); 00165 get_file_name(&s, filename); 00166 LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle)); 00167 LoadString(hInst, IDS_IMPORTED_OK, szText, sizeof(szTitle)); 00168 /* show successful import */ 00169 MessageBox(NULL, szText, szTitle, MB_OK); 00170 } 00171 break; 00172 } 00173 case ACTION_DELETE: 00174 { 00175 WCHAR reg_key_name[KEY_MAX_LEN]; 00176 get_file_name(&s, reg_key_name); 00177 if (!reg_key_name[0]) 00178 { 00179 fprintf(stderr, "%s: No registry key is specified for removal\n", getAppName()); 00180 fprintf(stderr, usage); 00181 exit(6); 00182 } 00183 delete_registry_key(reg_key_name); 00184 break; 00185 } 00186 case ACTION_EXPORT: 00187 { 00188 WCHAR filename[MAX_PATH]; 00189 00190 filename[0] = _T('\0'); 00191 get_file_name(&s, filename); 00192 if (!filename[0]) 00193 { 00194 fprintf(stderr, "%s: No file name is specified\n", getAppName()); 00195 fprintf(stderr, usage); 00196 exit(7); 00197 } 00198 00199 if (s[0]) 00200 { 00201 WCHAR reg_key_name[KEY_MAX_LEN]; 00202 get_file_name(&s, reg_key_name); 00203 export_registry_key(filename, reg_key_name, REG_FORMAT_4); 00204 } 00205 else 00206 { 00207 export_registry_key(filename, NULL, REG_FORMAT_4); 00208 } 00209 break; 00210 } 00211 default: 00212 fprintf(stderr, "%s: Unhandled action!\n", getAppName()); 00213 exit(8); 00214 break; 00215 } 00216 return TRUE; 00217 } 00218 00226 static void error_unknown_switch(WCHAR chu, LPWSTR s) 00227 { 00228 if (iswalpha(chu)) 00229 { 00230 fprintf(stderr, "%s: Undefined switch /%c!\n", getAppName(), chu); 00231 } 00232 else 00233 { 00234 fprintf(stderr, "%s: Alphabetic character is expected after '%c' " 00235 "in swit ch specification\n", getAppName(), *(s - 1)); 00236 } 00237 exit(1); 00238 } 00239 00240 BOOL ProcessCmdLine(LPWSTR lpCmdLine) 00241 { 00242 REGEDIT_ACTION action = ACTION_UNDEF; 00243 LPWSTR s = lpCmdLine; /* command line pointer */ 00244 WCHAR ch = *s; /* current character */ 00245 00246 while (ch && ((ch == L'-') || (ch == L'/'))) 00247 { 00248 WCHAR chu; 00249 WCHAR ch2; 00250 00251 s++; 00252 ch = *s; 00253 ch2 = *(s + 1); 00254 chu = (WCHAR)towupper(ch); 00255 if (!ch2 || iswspace(ch2)) 00256 { 00257 if (chu == L'S' || chu == L'V') 00258 { 00259 /* ignore these switches */ 00260 } 00261 else 00262 { 00263 switch (chu) 00264 { 00265 case L'D': 00266 action = ACTION_DELETE; 00267 break; 00268 case L'E': 00269 action = ACTION_EXPORT; 00270 break; 00271 case L'?': 00272 fprintf(stderr, usage); 00273 exit(3); 00274 break; 00275 default: 00276 error_unknown_switch(chu, s); 00277 break; 00278 } 00279 } 00280 s++; 00281 } 00282 else 00283 { 00284 if (ch2 == L':') 00285 { 00286 switch (chu) 00287 { 00288 case L'L': 00289 /* fall through */ 00290 case L'R': 00291 s += 2; 00292 while (*s && !iswspace(*s)) 00293 { 00294 s++; 00295 } 00296 break; 00297 default: 00298 error_unknown_switch(chu, s); 00299 break; 00300 } 00301 } 00302 else 00303 { 00304 /* this is a file name, starting from '/' */ 00305 s--; 00306 break; 00307 } 00308 } 00309 /* skip spaces to the next parameter */ 00310 ch = *s; 00311 while (ch && iswspace(ch)) 00312 { 00313 s++; 00314 ch = *s; 00315 } 00316 } 00317 00318 if (*s && action == ACTION_UNDEF) 00319 { 00320 TCHAR szTitle[256], szText[256]; 00321 LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle)); 00322 LoadString(hInst, IDS_IMPORT_PROMPT, szText, sizeof(szTitle)); 00323 /* request import confirmation */ 00324 if (MessageBox(NULL, szText, szTitle, MB_YESNO) == IDYES) 00325 { 00326 action = ACTION_ADD; 00327 } 00328 else return TRUE; 00329 } 00330 if (action == ACTION_UNDEF) 00331 return FALSE; 00332 00333 return PerformRegAction(action, s); 00334 } Generated on Sun May 27 2012 04:17:41 for ReactOS by
1.7.6.1
|