1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
|
/*
* tclWinReg.c --
*
* This file contains the implementation of the "registry" Tcl
* built-in command. This command is built as a dynamically
* loadable extension in a separate DLL.
*
* Copyright (c) 1997 by Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclWinReg.c 1.8 97/08/01 11:17:49
*/
#include <tcl.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
/*
* VC++ has an alternate entry point called DllMain, so we need to rename
* our entry point.
*/
#ifndef STATIC_BUILD
#if defined(_MSC_VER)
# define EXPORT(a,b) __declspec(dllexport) a b
# define DllEntryPoint DllMain
#else
# if defined(__BORLANDC__)
# define EXPORT(a,b) a _export b
# else
# define EXPORT(a,b) a b
# endif
#endif
#endif
/*
* The following macros convert between different endian ints.
*/
#define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
#define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
/*
* The following flag is used in OpenKeys to indicate that the specified
* key should be created if it doesn't currently exist.
*/
#define REG_CREATE 1
/*
* The following tables contain the mapping from registry root names
* to the system predefined keys.
*/
static char *rootKeyNames[] = {
"HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
"HKEY_CURRENT_USER", "HKEY_CURRENT_CONFIG", NULL
};
static HKEY rootKeys[] = {
HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
HKEY_CURRENT_CONFIG, HKEY_PERFORMANCE_DATA, HKEY_DYN_DATA
};
/*
* The following table maps from registry types to strings. Note that
* the indices for this array are the same as the constants for the
* known registry types so we don't need a separate table to hold the
* mapping.
*/
static char *typeNames[] = {
"none", "sz", "expand_sz", "binary", "dword",
"dword_big_endian", "link", "multi_sz", "resource_list", NULL
};
static DWORD lastType = REG_RESOURCE_REQUIREMENTS_LIST;
/*
* Declarations for functions defined in this file.
*/
static void AppendSystemError(Tcl_Interp *interp, DWORD error);
static DWORD ConvertDWORD(DWORD type, DWORD value);
static int DeleteKey(Tcl_Interp *interp, Tcl_Obj *keyNameObj);
static int DeleteValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *valueNameObj);
static int GetKeyNames(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *patternObj);
static int GetType(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *valueNameObj);
static int GetValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *valueNameObj);
static int GetValueNames(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *patternObj);
static int OpenKey(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
REGSAM mode, int flags, HKEY *keyPtr);
static DWORD OpenSubKey(char *hostName, HKEY rootKey,
char *keyName, REGSAM mode, int flags,
HKEY *keyPtr);
static int ParseKeyName(Tcl_Interp *interp, char *name,
char **hostNamePtr, HKEY *rootKeyPtr,
char **keyNamePtr);
static DWORD RecursiveDeleteKey(HKEY hStartKey, LPTSTR pKeyName);
static int RegistryObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]);
static int SetValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
Tcl_Obj *valueNameObj, Tcl_Obj *dataObj,
Tcl_Obj *typeObj);
EXTERN EXPORT(int,Registry_Init)(Tcl_Interp *interp);
/*
*----------------------------------------------------------------------
*
* DllEntryPoint --
*
* This wrapper function is used by Windows to invoke the
* initialization code for the DLL. If we are compiling
* with Visual C++, this routine will be renamed to DllMain.
* routine.
*
* Results:
* Returns TRUE;
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#ifdef __WIN32__
#ifndef STATIC_BUILD
BOOL APIENTRY
DllEntryPoint(
HINSTANCE hInst, /* Library instance handle. */
DWORD reason, /* Reason this function is being called. */
LPVOID reserved) /* Not used. */
{
return TRUE;
}
#endif
#endif
/*
*----------------------------------------------------------------------
*
* Registry_Init --
*
* This procedure initializes the registry command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
EXPORT(int,Registry_Init)(
Tcl_Interp *interp)
{
Tcl_CreateObjCommand(interp, "registry", RegistryObjCmd, NULL, NULL);
return Tcl_PkgProvide(interp, "registry", "1.0");
}
/*
*----------------------------------------------------------------------
*
* RegistryObjCmd --
*
* This function implements the Tcl "registry" command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
RegistryObjCmd(
ClientData clientData, /* Not used. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj * CONST objv[]) /* Argument values. */
{
int index;
char *errString;
static char *subcommands[] = { "delete", "get", "keys", "set", "type",
"values", (char *) NULL };
enum SubCmdIdx { DeleteIdx, GetIdx, KeysIdx, SetIdx, TypeIdx, ValuesIdx };
if (objc < 2) {
Tcl_WrongNumArgs(interp, objc, objv, "option ?arg arg ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], subcommands, "option", 0, &index)
!= TCL_OK) {
return TCL_ERROR;
}
switch (index) {
case DeleteIdx: /* delete */
if (objc == 3) {
return DeleteKey(interp, objv[2]);
} else if (objc == 4) {
return DeleteValue(interp, objv[2], objv[3]);
}
errString = "keyName ?valueName?";
break;
case GetIdx: /* get */
if (objc == 4) {
return GetValue(interp, objv[2], objv[3]);
}
errString = "keyName valueName";
break;
case KeysIdx: /* keys */
if (objc == 3) {
return GetKeyNames(interp, objv[2], NULL);
} else if (objc == 4) {
return GetKeyNames(interp, objv[2], objv[3]);
}
errString = "keyName ?pattern?";
break;
case SetIdx: /* set */
if (objc == 3) {
HKEY key;
/*
* Create the key and then close it immediately.
*/
if (OpenKey(interp, objv[2], KEY_ALL_ACCESS, 1, &key)
!= TCL_OK) {
return TCL_ERROR;
}
RegCloseKey(key);
return TCL_OK;
} else if (objc == 5 || objc == 6) {
Tcl_Obj *typeObj = (objc == 5) ? NULL : objv[5];
return SetValue(interp, objv[2], objv[3], objv[4], typeObj);
}
errString = "keyName ?valueName data ?type??";
break;
case TypeIdx: /* type */
if (objc == 4) {
return GetType(interp, objv[2], objv[3]);
}
errString = "keyName valueName";
break;
case ValuesIdx: /* values */
if (objc == 3) {
return GetValueNames(interp, objv[2], NULL);
} else if (objc == 4) {
return GetValueNames(interp, objv[2], objv[3]);
}
errString = "keyName ?pattern?";
break;
}
Tcl_WrongNumArgs(interp, 2, objv, errString);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DeleteKey --
*
* This function deletes a registry key.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
DeleteKey(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj) /* Name of key to delete. */
{
char *tail, *buffer, *hostName, *keyName;
HKEY rootKey, subkey;
DWORD result;
int length;
Tcl_Obj *resultPtr;
/*
* Find the parent of the key being deleted and open it.
*/
keyName = Tcl_GetStringFromObj(keyNameObj, &length);
buffer = ckalloc(length + 1);
strcpy(buffer, keyName);
if (ParseKeyName(interp, buffer, &hostName, &rootKey, &keyName)
!= TCL_OK) {
ckfree(buffer);
return TCL_ERROR;
}
resultPtr = Tcl_GetObjResult(interp);
if (*keyName == '\0') {
Tcl_AppendToObj(resultPtr, "bad key: cannot delete root keys", -1);
ckfree(buffer);
return TCL_ERROR;
}
tail = strrchr(keyName, '\\');
if (tail) {
*tail++ = '\0';
} else {
tail = keyName;
keyName = NULL;
}
result = OpenSubKey(hostName, rootKey, keyName,
KEY_ENUMERATE_SUB_KEYS | DELETE, 0, &subkey);
if (result != ERROR_SUCCESS) {
ckfree(buffer);
if (result == ERROR_FILE_NOT_FOUND) {
return TCL_OK;
} else {
Tcl_AppendToObj(resultPtr, "unable to delete key: ", -1);
AppendSystemError(interp, result);
return TCL_ERROR;
}
}
/*
* Now we recursively delete the key and everything below it.
*/
result = RecursiveDeleteKey(subkey, tail);
if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
Tcl_AppendToObj(resultPtr, "unable to delete key: ", -1);
AppendSystemError(interp, result);
result = TCL_ERROR;
} else {
result = TCL_OK;
}
RegCloseKey(subkey);
ckfree(buffer);
return result;
}
/*
*----------------------------------------------------------------------
*
* DeleteValue --
*
* This function deletes a value from a registry key.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
DeleteValue(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Name of key. */
Tcl_Obj *valueNameObj) /* Name of value to delete. */
{
HKEY key;
char *valueName;
int length;
DWORD result;
Tcl_Obj *resultPtr;
/*
* Attempt to open the key for deletion.
*/
if (OpenKey(interp, keyNameObj, KEY_SET_VALUE, 0, &key)
!= TCL_OK) {
return TCL_ERROR;
}
resultPtr = Tcl_GetObjResult(interp);
valueName = Tcl_GetStringFromObj(valueNameObj, &length);
result = RegDeleteValue(key, valueName);
if (result != ERROR_SUCCESS) {
Tcl_AppendStringsToObj(resultPtr, "unable to delete value \"",
Tcl_GetStringFromObj(valueNameObj, NULL), "\" from key \"",
Tcl_GetStringFromObj(keyNameObj, NULL), "\": ", NULL);
AppendSystemError(interp, result);
result = TCL_ERROR;
} else {
result = TCL_OK;
}
RegCloseKey(key);
return result;
}
/*
*----------------------------------------------------------------------
*
* GetKeyNames --
*
* This function enumerates the subkeys of a given key. If the
* optional pattern is supplied, then only keys that match the
* pattern will be returned.
*
* Results:
* Returns the list of subkeys in the result object of the
* interpreter, or an error message on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
GetKeyNames(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Key to enumerate. */
Tcl_Obj *patternObj) /* Optional match pattern. */
{
HKEY key;
DWORD index;
char buffer[MAX_PATH+1], *pattern;
Tcl_Obj *resultPtr;
int result = TCL_OK;
/*
* Attempt to open the key for enumeration.
*/
if (OpenKey(interp, keyNameObj, KEY_ENUMERATE_SUB_KEYS, 0, &key)
!= TCL_OK) {
return TCL_ERROR;
}
if (patternObj) {
pattern = Tcl_GetStringFromObj(patternObj, NULL);
} else {
pattern = NULL;
}
/*
* Enumerate over the subkeys until we get an error, indicating the
* end of the list.
*/
resultPtr = Tcl_GetObjResult(interp);
for (index = 0; RegEnumKey(key, index, buffer, MAX_PATH+1)
== ERROR_SUCCESS; index++) {
if (pattern && !Tcl_StringMatch(buffer, pattern)) {
continue;
}
result = Tcl_ListObjAppendElement(interp, resultPtr,
Tcl_NewStringObj(buffer, -1));
if (result != TCL_OK) {
break;
}
}
RegCloseKey(key);
return result;
}
/*
*----------------------------------------------------------------------
*
* GetType --
*
* This function gets the type of a given registry value and
* places it in the interpreter result.
*
* Results:
* Returns a normal Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
GetType(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Name of key. */
Tcl_Obj *valueNameObj) /* Name of value to get. */
{
HKEY key;
Tcl_Obj *resultPtr;
DWORD result;
DWORD type;
/*
* Attempt to open the key for reading.
*/
if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* Get the type of the value.
*/
resultPtr = Tcl_GetObjResult(interp);
result = RegQueryValueEx(key, Tcl_GetStringFromObj(valueNameObj, NULL),
NULL, &type, NULL, NULL);
RegCloseKey(key);
if (result != ERROR_SUCCESS) {
Tcl_AppendStringsToObj(resultPtr, "unable to get type of value \"",
Tcl_GetStringFromObj(valueNameObj, NULL), "\" from key \"",
Tcl_GetStringFromObj(keyNameObj, NULL), "\": ", NULL);
AppendSystemError(interp, result);
return TCL_ERROR;
}
/*
* Set the type into the result. Watch out for unknown types.
* If we don't know about the type, just use the numeric value.
*/
if (type > lastType) {
Tcl_SetIntObj(resultPtr, type);
} else {
Tcl_SetStringObj(resultPtr, typeNames[type], -1);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GetValue --
*
* This function gets the contents of a registry value and places
* a list containing the data and the type in the interpreter
* result.
*
* Results:
* Returns a normal Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
GetValue(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Name of key. */
Tcl_Obj *valueNameObj) /* Name of value to get. */
{
HKEY key;
char *valueName;
DWORD result, length, type;
Tcl_Obj *resultPtr;
Tcl_DString data;
/*
* Attempt to open the key for reading.
*/
if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* Get the value once to determine the length then again to store
* the data in the buffer.
*/
Tcl_DStringInit(&data);
resultPtr = Tcl_GetObjResult(interp);
valueName = Tcl_GetStringFromObj(valueNameObj, (int*) &length);
result = RegQueryValueEx(key, valueName, NULL, &type, NULL, &length);
if (result == ERROR_SUCCESS) {
Tcl_DStringSetLength(&data, length);
result = RegQueryValueEx(key, valueName, NULL, &type,
(LPBYTE) Tcl_DStringValue(&data), &length);
}
RegCloseKey(key);
if (result != ERROR_SUCCESS) {
Tcl_AppendStringsToObj(resultPtr, "unable to get value \"",
Tcl_GetStringFromObj(valueNameObj, NULL), "\" from key \"",
Tcl_GetStringFromObj(keyNameObj, NULL), "\": ", NULL);
AppendSystemError(interp, result);
Tcl_DStringFree(&data);
return TCL_ERROR;
}
/*
* If the data is a 32-bit quantity, store it as an integer object. If it
* is a multi-string, store it as a list of strings. For null-terminated
* strings, append up the to first null. Otherwise, store it as a binary
* string.
*/
if (type == REG_DWORD || type == REG_DWORD_BIG_ENDIAN) {
Tcl_SetIntObj(resultPtr, ConvertDWORD(type,
*((DWORD*) Tcl_DStringValue(&data))));
} else if (type == REG_MULTI_SZ) {
char *p = Tcl_DStringValue(&data);
char *lastChar = Tcl_DStringValue(&data) + Tcl_DStringLength(&data);
/*
* Multistrings are stored as an array of null-terminated strings,
* terminated by two null characters. Also do a bounds check in
* case we get bogus data.
*/
while (p < lastChar && *p != '\0') {
Tcl_ListObjAppendElement(interp, resultPtr,
Tcl_NewStringObj(p, -1));
while (*p++ != '\0') {}
}
} else if ((type == REG_SZ) || (type == REG_EXPAND_SZ)) {
Tcl_SetStringObj(resultPtr, Tcl_DStringValue(&data), -1);
} else {
Tcl_SetStringObj(resultPtr, Tcl_DStringValue(&data), length);
}
Tcl_DStringFree(&data);
return result;
}
/*
*----------------------------------------------------------------------
*
* GetValueNames --
*
* This function enumerates the values of the a given key. If
* the optional pattern is supplied, then only value names that
* match the pattern will be returned.
*
* Results:
* Returns the list of value names in the result object of the
* interpreter, or an error message on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
GetValueNames(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Key to enumerate. */
Tcl_Obj *patternObj) /* Optional match pattern. */
{
HKEY key;
Tcl_Obj *resultPtr;
DWORD index, size, result;
Tcl_DString buffer;
char *pattern;
/*
* Attempt to open the key for enumeration.
*/
if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
!= TCL_OK) {
return TCL_ERROR;
}
resultPtr = Tcl_GetObjResult(interp);
/*
* Query the key to determine the appropriate buffer size to hold the
* largest value name plus the terminating null.
*/
result = RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, &index,
&size, NULL, NULL, NULL);
if (result != ERROR_SUCCESS) {
Tcl_AppendStringsToObj(resultPtr, "unable to query key \"",
Tcl_GetStringFromObj(keyNameObj, NULL), "\": ", NULL);
AppendSystemError(interp, result);
RegCloseKey(key);
result = TCL_ERROR;
goto done;
}
size++;
Tcl_DStringInit(&buffer);
Tcl_DStringSetLength(&buffer, size);
index = 0;
result = TCL_OK;
if (patternObj) {
pattern = Tcl_GetStringFromObj(patternObj, NULL);
} else {
pattern = NULL;
}
/*
* Enumerate the values under the given subkey until we get an error,
* indicating the end of the list. Note that we need to reset size
* after each iteration because RegEnumValue smashes the old value.
*/
while (RegEnumValue(key, index, Tcl_DStringValue(&buffer), &size, NULL,
NULL, NULL, NULL) == ERROR_SUCCESS) {
if (!pattern || Tcl_StringMatch(Tcl_DStringValue(&buffer), pattern)) {
result = Tcl_ListObjAppendElement(interp, resultPtr,
Tcl_NewStringObj(Tcl_DStringValue(&buffer), size));
if (result != TCL_OK) {
break;
}
}
index++;
size = Tcl_DStringLength(&buffer);
}
Tcl_DStringFree(&buffer);
done:
RegCloseKey(key);
return result;
}
/*
*----------------------------------------------------------------------
*
* OpenKey --
*
* This function opens the specified key. This function is a
* simple wrapper around ParseKeyName and OpenSubKey.
*
* Results:
* Returns the opened key in the keyPtr argument and a Tcl
* result code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
OpenKey(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Key to open. */
REGSAM mode, /* Access mode. */
int flags, /* 0 or REG_CREATE. */
HKEY *keyPtr) /* Returned HKEY. */
{
char *keyName, *buffer, *hostName;
int length;
HKEY rootKey;
DWORD result;
keyName = Tcl_GetStringFromObj(keyNameObj, &length);
buffer = ckalloc(length + 1);
strcpy(buffer, keyName);
result = ParseKeyName(interp, buffer, &hostName, &rootKey, &keyName);
if (result == TCL_OK) {
result = OpenSubKey(hostName, rootKey, keyName, mode, flags, keyPtr);
if (result != ERROR_SUCCESS) {
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
Tcl_AppendToObj(resultPtr, "unable to open key: ", -1);
AppendSystemError(interp, result);
result = TCL_ERROR;
} else {
result = TCL_OK;
}
}
ckfree(buffer);
return result;
}
/*
*----------------------------------------------------------------------
*
* OpenSubKey --
*
* This function opens a given subkey of a root key on the
* specified host.
*
* Results:
* Returns the opened key in the keyPtr and a Windows error code
* as the return value.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static DWORD
OpenSubKey(
char *hostName, /* Host to access, or NULL for local. */
HKEY rootKey, /* Root registry key. */
char *keyName, /* Subkey name. */
REGSAM mode, /* Access mode. */
int flags, /* 0 or REG_CREATE. */
HKEY *keyPtr) /* Returned HKEY. */
{
DWORD result;
/*
* Attempt to open the root key on a remote host if necessary.
*/
if (hostName) {
result = RegConnectRegistry(hostName, rootKey, &rootKey);
if (result != ERROR_SUCCESS) {
return result;
}
}
/*
* Now open the specified key with the requested permissions. Note
* that this key must be closed by the caller.
*/
if (flags & REG_CREATE) {
DWORD create;
result = RegCreateKeyEx(rootKey, keyName, 0, "",
REG_OPTION_NON_VOLATILE, mode, NULL, keyPtr, &create);
} else {
result = RegOpenKeyEx(rootKey, keyName, 0, mode, keyPtr);
}
/*
* Be sure to close the root key since we are done with it now.
*/
if (hostName) {
RegCloseKey(rootKey);
}
return result;
}
/*
*----------------------------------------------------------------------
*
* ParseKeyName --
*
* This function parses a key name into the host, root, and subkey
* parts.
*
* Results:
* The pointers to the start of the host and subkey names are
* returned in the hostNamePtr and keyNamePtr variables. The
* specified root HKEY is returned in rootKeyPtr. Returns
* a standard Tcl result.
*
*
* Side effects:
* Modifies the name string by inserting nulls.
*
*----------------------------------------------------------------------
*/
static int
ParseKeyName(
Tcl_Interp *interp, /* Current interpreter. */
char *name,
char **hostNamePtr,
HKEY *rootKeyPtr,
char **keyNamePtr)
{
char *rootName;
int result, index;
Tcl_Obj *rootObj, *resultPtr = Tcl_GetObjResult(interp);
/*
* Split the key into host and root portions.
*/
*hostNamePtr = *keyNamePtr = rootName = NULL;
if (name[0] == '\\') {
if (name[1] == '\\') {
*hostNamePtr = name;
for (rootName = name+2; *rootName != '\0'; rootName++) {
if (*rootName == '\\') {
*rootName++ = '\0';
break;
}
}
}
} else {
rootName = name;
}
if (!rootName) {
Tcl_AppendStringsToObj(resultPtr, "bad key \"", name,
"\": must start with a valid root", NULL);
return TCL_ERROR;
}
/*
* Split the root into root and subkey portions.
*/
for (*keyNamePtr = rootName; **keyNamePtr != '\0'; (*keyNamePtr)++) {
if (**keyNamePtr == '\\') {
**keyNamePtr = '\0';
(*keyNamePtr)++;
break;
}
}
/*
* Look for a matching root name.
*/
rootObj = Tcl_NewStringObj(rootName, -1);
result = Tcl_GetIndexFromObj(interp, rootObj, rootKeyNames, "root name",
TCL_EXACT, &index);
Tcl_DecrRefCount(rootObj);
if (result != TCL_OK) {
return TCL_ERROR;
}
*rootKeyPtr = rootKeys[index];
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* RecursiveDeleteKey --
*
* This function recursively deletes all the keys below a starting
* key. Although Windows 95 does this automatically, we still need
* to do this for Windows NT.
*
* Results:
* Returns a Windows error code.
*
* Side effects:
* Deletes all of the keys and values below the given key.
*
*----------------------------------------------------------------------
*/
static DWORD
RecursiveDeleteKey(
HKEY startKey, /* Parent of key to be deleted. */
char *keyName) /* Name of key to be deleted. */
{
DWORD result, subKeyLength;
Tcl_DString subkey;
HKEY hKey;
/*
* Do not allow NULL or empty key name.
*/
if (!keyName || lstrlen(keyName) == '\0') {
return ERROR_BADKEY;
}
result = RegOpenKeyEx(startKey, keyName, 0,
KEY_ENUMERATE_SUB_KEYS | DELETE | KEY_QUERY_VALUE, &hKey);
if (result != ERROR_SUCCESS) {
return result;
}
result = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &subKeyLength,
NULL, NULL, NULL, NULL, NULL, NULL);
subKeyLength++;
if (result != ERROR_SUCCESS) {
return result;
}
Tcl_DStringInit(&subkey);
Tcl_DStringSetLength(&subkey, subKeyLength);
while (result == ERROR_SUCCESS) {
/*
* Always get index 0 because key deletion changes ordering.
*/
subKeyLength = Tcl_DStringLength(&subkey);
result=RegEnumKeyEx(hKey, 0, Tcl_DStringValue(&subkey), &subKeyLength,
NULL, NULL, NULL, NULL);
if (result == ERROR_NO_MORE_ITEMS) {
result = RegDeleteKey(startKey, keyName);
break;
} else if (result == ERROR_SUCCESS) {
result = RecursiveDeleteKey(hKey, Tcl_DStringValue(&subkey));
}
}
Tcl_DStringFree(&subkey);
RegCloseKey(hKey);
return result;
}
/*
*----------------------------------------------------------------------
*
* SetValue --
*
* This function sets the contents of a registry value. If
* the key or value does not exist, it will be created. If it
* does exist, then the data and type will be replaced.
*
* Results:
* Returns a normal Tcl result.
*
* Side effects:
* May create new keys or values.
*
*----------------------------------------------------------------------
*/
static int
SetValue(
Tcl_Interp *interp, /* Current interpreter. */
Tcl_Obj *keyNameObj, /* Name of key. */
Tcl_Obj *valueNameObj, /* Name of value to set. */
Tcl_Obj *dataObj, /* Data to be written. */
Tcl_Obj *typeObj) /* Type of data to be written. */
{
DWORD type, result;
HKEY key;
int length;
char *valueName;
Tcl_Obj *resultPtr;
if (typeObj == NULL) {
type = REG_SZ;
} else if (Tcl_GetIndexFromObj(interp, typeObj, typeNames, "type",
0, (int *) &type) != TCL_OK) {
if (Tcl_GetIntFromObj(NULL, typeObj, (int*) &type) != TCL_OK) {
return TCL_ERROR;
}
Tcl_ResetResult(interp);
}
if (OpenKey(interp, keyNameObj, KEY_ALL_ACCESS, 1, &key) != TCL_OK) {
return TCL_ERROR;
}
valueName = Tcl_GetStringFromObj(valueNameObj, &length);
resultPtr = Tcl_GetObjResult(interp);
if (type == REG_DWORD || type == REG_DWORD_BIG_ENDIAN) {
DWORD value;
if (Tcl_GetIntFromObj(interp, dataObj, (int*) &value) != TCL_OK) {
RegCloseKey(key);
return TCL_ERROR;
}
value = ConvertDWORD(type, value);
result = RegSetValueEx(key, valueName, 0, type, (BYTE*) &value,
sizeof(DWORD));
} else if (type == REG_MULTI_SZ) {
Tcl_DString data;
int objc, i;
Tcl_Obj **objv;
char *element;
if (Tcl_ListObjGetElements(interp, dataObj, &objc, &objv) != TCL_OK) {
RegCloseKey(key);
return TCL_ERROR;
}
/*
* Append the elements as null terminated strings. Note that
* we must not assume the length of the string in case there are
* embedded nulls, which aren't allowed in REG_MULTI_SZ values.
*/
Tcl_DStringInit(&data);
for (i = 0; i < objc; i++) {
element = Tcl_GetStringFromObj(objv[i], NULL);
Tcl_DStringAppend(&data, element, -1);
Tcl_DStringSetLength(&data, Tcl_DStringLength(&data)+1);
}
result = RegSetValueEx(key, valueName, 0, type,
(LPBYTE) Tcl_DStringValue(&data),
(DWORD) (Tcl_DStringLength(&data)+1));
Tcl_DStringFree(&data);
} else {
char *data = Tcl_GetStringFromObj(dataObj, &length);
/*
* Include the null in the length if we are storing a null terminated
* string. Note that we also need to call strlen to find the first
* null so we don't pass bad data to the registry.
*/
if (type == REG_SZ || type == REG_EXPAND_SZ) {
length = strlen(data) + 1;
}
result = RegSetValueEx(key, valueName, 0, type, (LPBYTE)data, length);
}
RegCloseKey(key);
if (result != ERROR_SUCCESS) {
Tcl_AppendToObj(resultPtr, "unable to set value: ", -1);
AppendSystemError(interp, result);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* AppendSystemError --
*
* This routine formats a Windows system error message and places
* it into the interpreter result.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
AppendSystemError(
Tcl_Interp *interp, /* Current interpreter. */
DWORD error) /* Result code from error. */
{
int length;
char *msgbuf, id[10];
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
sprintf(id, "%d", error);
length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msgbuf,
0, NULL);
if (length == 0) {
if (error == ERROR_CALL_NOT_IMPLEMENTED) {
msgbuf = "function not supported under Win32s";
} else {
msgbuf = id;
}
} else {
/*
* Trim the trailing CR/LF from the system message.
*/
if (msgbuf[length-1] == '\n') {
msgbuf[--length] = 0;
}
if (msgbuf[length-1] == '\r') {
msgbuf[--length] = 0;
}
}
Tcl_SetErrorCode(interp, "WINDOWS", id, msgbuf, (char *) NULL);
Tcl_AppendToObj(resultPtr, msgbuf, -1);
if (length != 0) {
LocalFree(msgbuf);
}
}
/*
*----------------------------------------------------------------------
*
* ConvertDWORD --
*
* This function determines whether a DWORD needs to be byte
* swapped, and returns the appropriately swapped value.
*
* Results:
* Returns a converted DWORD.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static DWORD
ConvertDWORD(
DWORD type, /* Either REG_DWORD or REG_DWORD_BIG_ENDIAN */
DWORD value) /* The value to be converted. */
{
DWORD order = 1;
DWORD localType;
/*
* Check to see if the low bit is in the first byte.
*/
localType = (*((char*)(&order)) == 1) ? REG_DWORD : REG_DWORD_BIG_ENDIAN;
return (type != localType) ? SWAPLONG(value) : value;
}
|