static launch_data_t to_launchd_sockets(json_t *json) { launch_data_t result, arr; const char *key; size_t idx; json_t *val, *val2; result = launch_data_alloc(LAUNCH_DATA_DICTIONARY); json_object_foreach(json, key, val) { arr = launch_data_alloc(LAUNCH_DATA_ARRAY); switch (json_typeof(val)) { case JSON_OBJECT: launch_data_array_set_index(arr, create_socket(val), 0); break; case JSON_ARRAY: json_array_foreach(val, idx, val2) { launch_data_array_set_index(arr, create_socket(val2), idx); } break; default: errx(EX_OSERR, "Invalid jlist specification"); }
bool launch_data_dict_insert(launch_data_t dict, launch_data_t what, const char *key) { size_t i; launch_data_t thekey = launch_data_alloc(LAUNCH_DATA_STRING); launch_data_set_string(thekey, key); for (i = 0; i < dict->_array_cnt; i += 2) { if (!strcmp(key, dict->_array[i]->string)) { launch_data_array_set_index(dict, thekey, i); launch_data_array_set_index(dict, what, i + 1); return true; } } launch_data_array_set_index(dict, thekey, i); launch_data_array_set_index(dict, what, i + 1); return true; }
static void launch_msg_getmsgs(launch_data_t m, void *context) { launch_data_t async_resp, *sync_resp = context; if ((LAUNCH_DATA_DICTIONARY == launch_data_get_type(m)) && (async_resp = launch_data_dict_lookup(m, "_AsyncMessage"))) { launch_data_array_set_index(_lc->async_resp, launch_data_copy(async_resp), launch_data_array_get_count(_lc->async_resp)); } else { *sync_resp = launch_data_copy(m); } }
launch_data_t GTMLaunchDataCreateFromCFType(CFTypeRef cf_type_ref, CFErrorRef *error) { launch_data_t result = NULL; CFErrorRef local_error = NULL; if (cf_type_ref == NULL) { local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("NULL CFType")); goto exit; } CFTypeID cf_type = CFGetTypeID(cf_type_ref); if (cf_type == CFStringGetTypeID()) { CFIndex length = CFStringGetLength(cf_type_ref); CFIndex max_length = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; char *buffer = calloc(max_length, sizeof(char)); size_t buffer_size = max_length * sizeof(char); if (buffer) { if (CFStringGetCString(cf_type_ref, buffer, buffer_size, kCFStringEncodingUTF8)) { result = launch_data_alloc(LAUNCH_DATA_STRING); launch_data_set_string(result, buffer); } else { local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("CFStringGetCString failed %@"), cf_type_ref); } free(buffer); } else { local_error = GTMCFLaunchCreateUnlocalizedError(ENOMEM, CFSTR("calloc of %zu failed"), buffer_size); } } else if (cf_type == CFBooleanGetTypeID()) { result = launch_data_alloc(LAUNCH_DATA_BOOL); launch_data_set_bool(result, CFBooleanGetValue(cf_type_ref)); } else if (cf_type == CFArrayGetTypeID()) { CFIndex count = CFArrayGetCount(cf_type_ref); result = launch_data_alloc(LAUNCH_DATA_ARRAY); for (CFIndex i = 0; i < count; i++) { CFTypeRef array_value = CFArrayGetValueAtIndex(cf_type_ref, i); if (array_value) { launch_data_t launch_value = GTMLaunchDataCreateFromCFType(array_value, &local_error); if (local_error) break; launch_data_array_set_index(result, launch_value, i); } } } else if (cf_type == CFDictionaryGetTypeID()) { result = launch_data_alloc(LAUNCH_DATA_DICTIONARY); GTMCFToLDictContext context = { result, &local_error }; CFDictionaryApplyFunction(cf_type_ref, GTMConvertCFDictEntryToLaunchDataDictEntry, &context); } else if (cf_type == CFDataGetTypeID()) { result = launch_data_alloc(LAUNCH_DATA_OPAQUE); launch_data_set_opaque(result, CFDataGetBytePtr(cf_type_ref), CFDataGetLength(cf_type_ref)); } else if (cf_type == CFNumberGetTypeID()) { CFNumberType cf_number_type = CFNumberGetType(cf_type_ref); switch (cf_number_type) { case kCFNumberSInt8Type: case kCFNumberSInt16Type: case kCFNumberSInt32Type: case kCFNumberSInt64Type: case kCFNumberCharType: case kCFNumberShortType: case kCFNumberIntType: case kCFNumberLongType: case kCFNumberLongLongType: case kCFNumberCFIndexType: case kCFNumberNSIntegerType:{ long long value; if (CFNumberGetValue(cf_type_ref, kCFNumberLongLongType, &value)) { result = launch_data_alloc(LAUNCH_DATA_INTEGER); launch_data_set_integer(result, value); } else { local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("Unknown to convert: %@"), cf_type_ref); } break; } case kCFNumberFloat32Type: case kCFNumberFloat64Type: case kCFNumberFloatType: case kCFNumberDoubleType: case kCFNumberCGFloatType: { double value; if (CFNumberGetValue(cf_type_ref, kCFNumberDoubleType, &value)) { result = launch_data_alloc(LAUNCH_DATA_REAL); launch_data_set_real(result, value); } else { local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("Unknown to convert: %@"), cf_type_ref); } break; } default: local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("Unknown CFNumberType %lld"), (long long)cf_number_type); break; } } else { local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL, CFSTR("Unknown CFTypeID %lu"), cf_type); } exit: if (error) { *error = local_error; } else if (local_error) { #ifdef DEBUG CFShow(local_error); #endif // DEBUG CFRelease(local_error); } return result; }