Guest User

Untitled

a guest
Jul 17th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1.  
  2. // (c) 2002 Julien Jalon <jalon@julien-jalon.org>
  3. // See license info at the end of this file.
  4.  
  5. #import <Foundation/Foundation.h>
  6.  
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <SystemConfiguration/SystemConfiguration.h>
  9.  
  10. #define PRINT(X) printf("%s\n", [[NSString stringWithFormat:@"%@", (X)] ISOLatin1EncodedCString])
  11.  
  12. // Encoding String additions
  13.  
  14. @interface NSString (EncodedCStringAdditions)
  15.  
  16. + (NSString *)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encoding;
  17. - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding;
  18.  
  19. + (NSString *)stringWithISOLatin1EncodedCString:(const char *)cString;
  20. - (const char *)ISOLatin1EncodedCString;
  21.  
  22. @end
  23.  
  24. @implementation NSString (EncodedCStringAdditions)
  25.  
  26. + (NSString *)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encoding {
  27. return [[[NSString alloc] initWithData:[NSData dataWithBytes:cString length:strlen(cString)] encoding:encoding] autorelease];
  28. }
  29.  
  30. - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding {
  31. char *returnValue;
  32. NSData *encodedData=[self dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
  33. unsigned dataLength=[encodedData length];
  34.  
  35. returnValue = [[NSMutableData dataWithLength:dataLength+1] mutableBytes];
  36.  
  37. memcpy(returnValue,(char *)[encodedData bytes],dataLength);
  38.  
  39. returnValue[dataLength]='\0';
  40.  
  41. return returnValue;
  42. }
  43.  
  44. + (NSString *)stringWithISOLatin1EncodedCString:(const char *)cString {
  45. return [NSString stringWithCString:cString encoding:NSISOLatin1StringEncoding];
  46. }
  47.  
  48. - (const char *)ISOLatin1EncodedCString {
  49. return [self cStringUsingEncoding:NSISOLatin1StringEncoding];
  50. }
  51.  
  52. @end
  53.  
  54. SCPreferencesRef prefRef;
  55.  
  56. void usage() {
  57. NSString *commandpath = [[NSProcessInfo processInfo] processName];
  58. NSString *licence = @"This program is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License version 2 as published\nby the Free Software Foundation.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program; if not, write to the Free Software\nFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA\n02111-1307, USA.";
  59. NSString *usageString = [NSString stringWithFormat:
  60. @"%@ view (sets|current|<setid>)\n%@ viewservice <serviceid> [<property>]\n%@ change <serviceid> <property> <key> (string|array) <value1> [<value2> ...]\n\n%@", commandpath, commandpath, commandpath, licence];
  61. PRINT(usageString);
  62. exit(0);
  63. }
  64.  
  65. int main (int argc, const char * argv[]) {
  66. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  67. NSString *output;
  68. NSArray *arguments;
  69. NSString *command, *objId;
  70.  
  71. arguments = [[NSProcessInfo processInfo] arguments];
  72. if([arguments count]<2) {
  73. usage();
  74. }
  75.  
  76. command = [arguments objectAtIndex:1];
  77. objId = [arguments objectAtIndex:2];
  78. if(!(command && objId)) {
  79. usage();
  80. }
  81.  
  82. prefRef = SCPreferencesCreate(kCFAllocatorSystemDefault, (CFStringRef)@"XXX", NULL);
  83. if([command isEqualToString:@"view"]) {
  84. NSString *set = objId;
  85. if([set isEqualToString:@"sets"]) {
  86. NSDictionary *allSets = (NSDictionary *)SCPreferencesGetValue(prefRef, kSCPrefSets);
  87. NSEnumerator *setEnumerator = [allSets keyEnumerator];
  88. NSString *setId;
  89.  
  90. while(setId = [setEnumerator nextObject]) {
  91. NSString *setStatus = [NSString stringWithFormat:@"%@ - %@", setId,
  92. [[allSets objectForKey:setId] objectForKey:(NSString *)kSCPropUserDefinedName]];
  93. PRINT(setStatus);
  94. }
  95. } else {
  96. NSString *setPath;
  97. NSDictionary *setDict;
  98. NSString *setName;
  99. NSArray *setServices;
  100.  
  101. if([set isEqualToString:@"current"]) {
  102. setPath = (NSString *)SCPreferencesGetValue(prefRef, kSCPrefCurrentSet);
  103. } else {
  104. setPath = [NSString stringWithFormat:@"/%@/%@", kSCPrefSets, set];
  105. }
  106.  
  107. setDict = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)setPath);
  108. if(!setDict) {
  109. PRINT(@"Set not found");
  110. exit(1);
  111. }
  112. setName = [setDict objectForKey:(NSString *)kSCPropUserDefinedName];
  113. setServices = [[[setDict objectForKey:(NSString *)kSCCompNetwork] objectForKey:(NSString *)kSCCompService] allKeys];
  114.  
  115. {
  116. NSEnumerator *serviceEnumerator = [setServices objectEnumerator];
  117. NSString *serviceId;
  118.  
  119. output = [NSString stringWithFormat:@"Set %@:", setName];
  120. PRINT(output);
  121. while(serviceId = [serviceEnumerator nextObject]) {
  122. NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
  123. NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
  124. NSString *serviceName = [service objectForKey:(NSString *)kSCPropUserDefinedName];
  125. BOOL isActive = ![service objectForKey:(NSString *)kSCResvInactive];
  126. NSString *output = [NSString stringWithFormat:@"%@ - %@ (%@)", serviceId, serviceName, (isActive?@"Active":@"Inactive")];
  127. PRINT(output);
  128. }
  129. }
  130. }
  131. } else if([command isEqualToString:@"viewservice"]) {
  132. NSString *serviceId = objId;
  133. NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
  134. NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
  135. NSString *serviceName;
  136. BOOL isActive;
  137. NSString *output;
  138. NSEnumerator *servicePropsEnumerator;
  139. NSString *serviceProp;
  140. id serviceProperty;
  141.  
  142. if(!service) {
  143. PRINT(@"Service not found");
  144. exit(1);
  145. }
  146.  
  147. serviceName = [service objectForKey:(NSString *)kSCPropUserDefinedName];
  148. isActive = ![service objectForKey:(NSString *)kSCResvInactive];
  149. output = [NSString stringWithFormat:@"Service %@ (%@):", serviceName, (isActive?@"Active":@"Inactive")];
  150.  
  151. PRINT(output);
  152.  
  153. if([arguments count] == 4) {
  154. serviceProp = [arguments objectAtIndex:3];
  155. serviceProperty = [service objectForKey:serviceProp];
  156. if(!serviceProperty) {
  157. PRINT(@"Property not found");
  158. exit(1);
  159. } else {
  160. output = [NSString stringWithFormat:@"%@ = %@", serviceProp, serviceProperty];
  161. PRINT(output);
  162. }
  163. } else {
  164. servicePropsEnumerator = [service keyEnumerator];
  165. while(serviceProp = [servicePropsEnumerator nextObject]) {
  166. serviceProperty = [service objectForKey:serviceProp];
  167. if([serviceProperty respondsToSelector:@selector(objectForKey:)]) {
  168. output = [NSString stringWithFormat:@"%@ = %@", serviceProp, serviceProperty];
  169. PRINT(output);
  170. }
  171. }
  172. }
  173. } else if([command isEqualToString:@"change"]) {
  174. int argcount = [arguments count];
  175. if(argcount<7) {
  176. usage();
  177. }
  178. {
  179. NSString *serviceId = [arguments objectAtIndex:2];
  180. NSString *propId = [arguments objectAtIndex:3];
  181. NSString *key = [arguments objectAtIndex:4];
  182. NSString *type = [arguments objectAtIndex:5];
  183. NSString *servicePath = [NSString stringWithFormat:@"/%@/%@", (NSString *)kSCPrefNetworkServices, serviceId];
  184. NSDictionary *service = (NSDictionary *)SCPreferencesPathGetValue(prefRef, (CFStringRef)servicePath);
  185. NSDictionary *propDict;
  186. id newValue = nil;
  187.  
  188. if(!service) {
  189. PRINT(@"Service not found");
  190. exit(1);
  191. }
  192. propDict = [service objectForKey:propId];
  193. if(!propDict) {
  194. PRINT(@"Key not found in service properties");
  195. exit(1);
  196. }
  197.  
  198. if([type isEqualToString:@"string"]) {
  199. if(argcount!=7) {
  200. usage();
  201. }
  202. newValue = [arguments objectAtIndex:6];
  203. } else if([type isEqualToString:@"array"]) {
  204. newValue = [arguments subarrayWithRange:NSMakeRange(6, argcount-6)];
  205. } else {
  206. usage();
  207. }
  208. {
  209. NSString *propDictPath = [NSString stringWithFormat:@"%@/%@", servicePath, propId];
  210. NSMutableDictionary *newPropDict = [propDict mutableCopy];
  211. BOOL ok;
  212. [newPropDict setObject: newValue forKey: key];
  213. ok = SCPreferencesPathSetValue(prefRef, (CFStringRef)propDictPath, (CFDictionaryRef)newPropDict);
  214. if(ok) {
  215. PRINT(@"Change succeeded");
  216. } else {
  217. PRINT(@"Change failed!");
  218. exit(1);
  219. }
  220. ok = SCPreferencesCommitChanges(prefRef);
  221. if(ok) {
  222. PRINT(@"Commit succeeded");
  223. } else {
  224. PRINT(@"Commit failed!");
  225. exit(1);
  226. }
  227. ok = SCPreferencesApplyChanges(prefRef);
  228. if(ok) {
  229. PRINT(@"Apply succeeded");
  230. } else {
  231. PRINT(@"Apply failed!");
  232. exit(1);
  233. }
  234. }
  235. }
  236. } else {
  237. usage();
  238. }
  239.  
  240. [pool release];
  241.  
  242. return 0;
  243. }
  244.  
  245. // This program is free software; you can redistribute it and/or modify
  246. // it under the terms of the GNU General Public License version 2 as published
  247. // by the Free Software Foundation.
  248. //
  249. // This program is distributed in the hope that it will be useful,
  250. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  251. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  252. // GNU General Public License for more details.
  253. //
  254. // You should have received a copy of the GNU General Public License
  255. // along with this program; if not, write to the Free Software
  256. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  257. // 02111-1307, USA.
  258. Choose download locationDownload
Add Comment
Please, Sign In to add comment