michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2009-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * michael@0: * FILE NAME : icuplug.c michael@0: * michael@0: * Date Name Description michael@0: * 10/29/2009 sl New. michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/icuplug.h" michael@0: #include "icuplugimp.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: #include "putilimp.h" michael@0: #include "ucln.h" michael@0: #include michael@0: #ifdef __MVS__ /* defined by z/OS compiler */ michael@0: #define _POSIX_SOURCE michael@0: #include /* 12 Nov 2011 JAM iscics() function */ michael@0: #endif michael@0: michael@0: #ifndef UPLUG_TRACE michael@0: #define UPLUG_TRACE 0 michael@0: #endif michael@0: michael@0: #if UPLUG_TRACE michael@0: #include michael@0: #define DBG(x) fprintf(stderr, "%s:%d: ",__FILE__,__LINE__); fprintf x michael@0: #endif michael@0: michael@0: /** michael@0: * Internal structure of an ICU plugin. michael@0: */ michael@0: michael@0: struct UPlugData { michael@0: UPlugEntrypoint *entrypoint; /**< plugin entrypoint */ michael@0: uint32_t structSize; /**< initialized to the size of this structure */ michael@0: uint32_t token; /**< must be U_PLUG_TOKEN */ michael@0: void *lib; /**< plugin library, or NULL */ michael@0: char libName[UPLUG_NAME_MAX]; /**< library name */ michael@0: char sym[UPLUG_NAME_MAX]; /**< plugin symbol, or NULL */ michael@0: char config[UPLUG_NAME_MAX]; /**< configuration data */ michael@0: void *context; /**< user context data */ michael@0: char name[UPLUG_NAME_MAX]; /**< name of plugin */ michael@0: UPlugLevel level; /**< level of plugin */ michael@0: UBool awaitingLoad; /**< TRUE if the plugin is awaiting a load call */ michael@0: UBool dontUnload; /**< TRUE if plugin must stay resident (leak plugin and lib) */ michael@0: UErrorCode pluginStatus; /**< status code of plugin */ michael@0: }; michael@0: michael@0: michael@0: michael@0: #define UPLUG_LIBRARY_INITIAL_COUNT 8 michael@0: #define UPLUG_PLUGIN_INITIAL_COUNT 12 michael@0: michael@0: /** michael@0: * Remove an item michael@0: * @param list the full list michael@0: * @param listSize the number of entries in the list michael@0: * @param memberSize the size of one member michael@0: * @param itemToRemove the item number of the member michael@0: * @return the new listsize michael@0: */ michael@0: static int32_t uplug_removeEntryAt(void *list, int32_t listSize, int32_t memberSize, int32_t itemToRemove) { michael@0: uint8_t *bytePtr = (uint8_t *)list; michael@0: michael@0: /* get rid of some bad cases first */ michael@0: if(listSize<1) { michael@0: return listSize; michael@0: } michael@0: michael@0: /* is there anything to move? */ michael@0: if(listSize > itemToRemove+1) { michael@0: memmove(bytePtr+(itemToRemove*memberSize), bytePtr+((itemToRemove+1)*memberSize), memberSize); michael@0: } michael@0: michael@0: return listSize-1; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: #if U_ENABLE_DYLOAD michael@0: /** michael@0: * Library management. Internal. michael@0: * @internal michael@0: */ michael@0: struct UPlugLibrary; michael@0: michael@0: /** michael@0: * Library management. Internal. michael@0: * @internal michael@0: */ michael@0: typedef struct UPlugLibrary { michael@0: void *lib; /**< library ptr */ michael@0: char name[UPLUG_NAME_MAX]; /**< library name */ michael@0: uint32_t ref; /**< reference count */ michael@0: } UPlugLibrary; michael@0: michael@0: static UPlugLibrary staticLibraryList[UPLUG_LIBRARY_INITIAL_COUNT]; michael@0: static UPlugLibrary * libraryList = staticLibraryList; michael@0: static int32_t libraryCount = 0; michael@0: static int32_t libraryMax = UPLUG_LIBRARY_INITIAL_COUNT; michael@0: michael@0: /** michael@0: * Search for a library. Doesn't lock michael@0: * @param libName libname to search for michael@0: * @return the library's struct michael@0: */ michael@0: static int32_t searchForLibraryName(const char *libName) { michael@0: int32_t i; michael@0: michael@0: for(i=0;i= libraryMax) { michael@0: /* Ran out of library slots. Statically allocated because we can't depend on allocating memory.. */ michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "uplug_openLibrary() - out of library slots (max %d)\n", libraryMax)); michael@0: #endif michael@0: return NULL; michael@0: } michael@0: /* Some operating systems don't want michael@0: DL operations from multiple threads. */ michael@0: libraryList[libEntry].lib = uprv_dl_open(libName, status); michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); michael@0: #endif michael@0: michael@0: if(libraryList[libEntry].lib == NULL || U_FAILURE(*status)) { michael@0: /* cleanup. */ michael@0: libraryList[libEntry].lib = NULL; /* failure with open */ michael@0: libraryList[libEntry].name[0] = 0; michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); michael@0: #endif michael@0: /* no need to free - just won't increase the count. */ michael@0: libraryCount--; michael@0: } else { /* is it still there? */ michael@0: /* link it in */ michael@0: uprv_strncpy(libraryList[libEntry].name,libName,UPLUG_NAME_MAX); michael@0: libraryList[libEntry].ref=1; michael@0: lib = libraryList[libEntry].lib; michael@0: } michael@0: michael@0: } else { michael@0: lib = libraryList[libEntry].lib; michael@0: libraryList[libEntry].ref++; michael@0: } michael@0: return lib; michael@0: } michael@0: michael@0: U_INTERNAL void U_EXPORT2 michael@0: uplug_closeLibrary(void *lib, UErrorCode *status) { michael@0: int32_t i; michael@0: michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "uplug_closeLibrary(%p,%s) list %p\n", lib, u_errorName(*status), (void*)libraryList)); michael@0: #endif michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: for(i=0;i=pastPlug) { michael@0: return pluginCount; michael@0: } else { michael@0: return (d-pluginList)/sizeof(pluginList[0]); michael@0: } michael@0: } michael@0: michael@0: michael@0: U_CAPI UPlugData * U_EXPORT2 michael@0: uplug_nextPlug(UPlugData *prior) { michael@0: if(prior==NULL) { michael@0: return pluginList; michael@0: } else { michael@0: UPlugData *nextPlug = &prior[1]; michael@0: UPlugData *pastPlug = &pluginList[pluginCount]; michael@0: michael@0: if(nextPlug>=pastPlug) { michael@0: return NULL; michael@0: } else { michael@0: return nextPlug; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: /** michael@0: * Call the plugin with some params michael@0: */ michael@0: static void uplug_callPlug(UPlugData *plug, UPlugReason reason, UErrorCode *status) { michael@0: UPlugTokenReturn token; michael@0: if(plug==NULL||U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: token = (*(plug->entrypoint))(plug, reason, status); michael@0: if(token!=UPLUG_TOKEN) { michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void uplug_unloadPlug(UPlugData *plug, UErrorCode *status) { michael@0: if(plug->awaitingLoad) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: if(U_SUCCESS(plug->pluginStatus)) { michael@0: /* Don't unload a plug which has a failing load status - means it didn't actually load. */ michael@0: uplug_callPlug(plug, UPLUG_REASON_UNLOAD, status); michael@0: } michael@0: } michael@0: michael@0: static void uplug_queryPlug(UPlugData *plug, UErrorCode *status) { michael@0: if(!plug->awaitingLoad || !(plug->level == UPLUG_LEVEL_UNKNOWN) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: plug->level = UPLUG_LEVEL_INVALID; michael@0: uplug_callPlug(plug, UPLUG_REASON_QUERY, status); michael@0: if(U_SUCCESS(*status)) { michael@0: if(plug->level == UPLUG_LEVEL_INVALID) { michael@0: plug->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; michael@0: plug->awaitingLoad = FALSE; michael@0: } michael@0: } else { michael@0: plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; michael@0: plug->awaitingLoad = FALSE; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void uplug_loadPlug(UPlugData *plug, UErrorCode *status) { michael@0: if(!plug->awaitingLoad || (plug->level < UPLUG_LEVEL_LOW) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: uplug_callPlug(plug, UPLUG_REASON_LOAD, status); michael@0: plug->awaitingLoad = FALSE; michael@0: if(!U_SUCCESS(*status)) { michael@0: plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: } michael@0: michael@0: static UPlugData *uplug_allocateEmptyPlug(UErrorCode *status) michael@0: { michael@0: UPlugData *plug = NULL; michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(pluginCount == UPLUG_PLUGIN_INITIAL_COUNT) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: plug = &pluginList[pluginCount++]; michael@0: michael@0: plug->token = UPLUG_TOKEN; michael@0: plug->structSize = sizeof(UPlugData); michael@0: plug->name[0]=0; michael@0: plug->level = UPLUG_LEVEL_UNKNOWN; /* initialize to null state */ michael@0: plug->awaitingLoad = TRUE; michael@0: plug->dontUnload = FALSE; michael@0: plug->pluginStatus = U_ZERO_ERROR; michael@0: plug->libName[0] = 0; michael@0: plug->config[0]=0; michael@0: plug->sym[0]=0; michael@0: plug->lib=NULL; michael@0: plug->entrypoint=NULL; michael@0: michael@0: michael@0: return plug; michael@0: } michael@0: michael@0: static UPlugData *uplug_allocatePlug(UPlugEntrypoint *entrypoint, const char *config, void *lib, const char *symName, michael@0: UErrorCode *status) { michael@0: UPlugData *plug; michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: plug = uplug_allocateEmptyPlug(status); michael@0: if(config!=NULL) { michael@0: uprv_strncpy(plug->config, config, UPLUG_NAME_MAX); michael@0: } else { michael@0: plug->config[0] = 0; michael@0: } michael@0: michael@0: if(symName!=NULL) { michael@0: uprv_strncpy(plug->sym, symName, UPLUG_NAME_MAX); michael@0: } else { michael@0: plug->sym[0] = 0; michael@0: } michael@0: michael@0: plug->entrypoint = entrypoint; michael@0: plug->lib = lib; michael@0: uplug_queryPlug(plug, status); michael@0: michael@0: return plug; michael@0: } michael@0: michael@0: static void uplug_deallocatePlug(UPlugData *plug, UErrorCode *status) { michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: if(!plug->dontUnload) { michael@0: #if U_ENABLE_DYLOAD michael@0: uplug_closeLibrary(plug->lib, &subStatus); michael@0: #endif michael@0: } michael@0: plug->lib = NULL; michael@0: if(U_SUCCESS(*status) && U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: } michael@0: /* shift plugins up and decrement count. */ michael@0: if(U_SUCCESS(*status)) { michael@0: /* all ok- remove. */ michael@0: pluginCount = uplug_removeEntryAt(pluginList, pluginCount, sizeof(plug[0]), uplug_pluginNumber(plug)); michael@0: } else { michael@0: /* not ok- leave as a message. */ michael@0: plug->awaitingLoad=FALSE; michael@0: plug->entrypoint=0; michael@0: plug->dontUnload=TRUE; michael@0: } michael@0: } michael@0: michael@0: static void uplug_doUnloadPlug(UPlugData *plugToRemove, UErrorCode *status) { michael@0: if(plugToRemove != NULL) { michael@0: uplug_unloadPlug(plugToRemove, status); michael@0: uplug_deallocatePlug(plugToRemove, status); michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_removePlug(UPlugData *plug, UErrorCode *status) { michael@0: UPlugData *cursor = NULL; michael@0: UPlugData *plugToRemove = NULL; michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: for(cursor=pluginList;cursor!=NULL;) { michael@0: if(cursor==plug) { michael@0: plugToRemove = plug; michael@0: cursor=NULL; michael@0: } else { michael@0: cursor = uplug_nextPlug(cursor); michael@0: } michael@0: } michael@0: michael@0: uplug_doUnloadPlug(plugToRemove, status); michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_setPlugNoUnload(UPlugData *data, UBool dontUnload) michael@0: { michael@0: data->dontUnload = dontUnload; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_setPlugLevel(UPlugData *data, UPlugLevel level) { michael@0: data->level = level; michael@0: } michael@0: michael@0: michael@0: U_CAPI UPlugLevel U_EXPORT2 michael@0: uplug_getPlugLevel(UPlugData *data) { michael@0: return data->level; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_setPlugName(UPlugData *data, const char *name) { michael@0: uprv_strncpy(data->name, name, UPLUG_NAME_MAX); michael@0: } michael@0: michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: uplug_getPlugName(UPlugData *data) { michael@0: return data->name; michael@0: } michael@0: michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: uplug_getSymbolName(UPlugData *data) { michael@0: return data->sym; michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: uplug_getLibraryName(UPlugData *data, UErrorCode *status) { michael@0: if(data->libName[0]) { michael@0: return data->libName; michael@0: } else { michael@0: #if U_ENABLE_DYLOAD michael@0: return uplug_findLibrary(data->lib, status); michael@0: #else michael@0: return NULL; michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: U_CAPI void * U_EXPORT2 michael@0: uplug_getLibrary(UPlugData *data) { michael@0: return data->lib; michael@0: } michael@0: michael@0: U_CAPI void * U_EXPORT2 michael@0: uplug_getContext(UPlugData *data) { michael@0: return data->context; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_setContext(UPlugData *data, void *context) { michael@0: data->context = context; michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: uplug_getConfiguration(UPlugData *data) { michael@0: return data->config; michael@0: } michael@0: michael@0: U_INTERNAL UPlugData* U_EXPORT2 michael@0: uplug_getPlugInternal(int32_t n) { michael@0: if(n <0 || n >= pluginCount) { michael@0: return NULL; michael@0: } else { michael@0: return &(pluginList[n]); michael@0: } michael@0: } michael@0: michael@0: michael@0: U_CAPI UErrorCode U_EXPORT2 michael@0: uplug_getPlugLoadStatus(UPlugData *plug) { michael@0: return plug->pluginStatus; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: /** michael@0: * Initialize a plugin fron an entrypoint and library - but don't load it. michael@0: */ michael@0: static UPlugData* uplug_initPlugFromEntrypointAndLibrary(UPlugEntrypoint *entrypoint, const char *config, void *lib, const char *sym, michael@0: UErrorCode *status) { michael@0: UPlugData *plug = NULL; michael@0: michael@0: plug = uplug_allocatePlug(entrypoint, config, lib, sym, status); michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: return plug; michael@0: } else { michael@0: uplug_deallocatePlug(plug, status); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: U_CAPI UPlugData* U_EXPORT2 michael@0: uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status) { michael@0: UPlugData* plug = uplug_initPlugFromEntrypointAndLibrary(entrypoint, config, NULL, NULL, status); michael@0: uplug_loadPlug(plug, status); michael@0: return plug; michael@0: } michael@0: michael@0: #if U_ENABLE_DYLOAD michael@0: michael@0: static UPlugData* michael@0: uplug_initErrorPlug(const char *libName, const char *sym, const char *config, const char *nameOrError, UErrorCode loadStatus, UErrorCode *status) michael@0: { michael@0: UPlugData *plug = uplug_allocateEmptyPlug(status); michael@0: if(U_FAILURE(*status)) return NULL; michael@0: michael@0: plug->pluginStatus = loadStatus; michael@0: plug->awaitingLoad = FALSE; /* Won't load. */ michael@0: plug->dontUnload = TRUE; /* cannot unload. */ michael@0: michael@0: if(sym!=NULL) { michael@0: uprv_strncpy(plug->sym, sym, UPLUG_NAME_MAX); michael@0: } michael@0: michael@0: if(libName!=NULL) { michael@0: uprv_strncpy(plug->libName, libName, UPLUG_NAME_MAX); michael@0: } michael@0: michael@0: if(nameOrError!=NULL) { michael@0: uprv_strncpy(plug->name, nameOrError, UPLUG_NAME_MAX); michael@0: } michael@0: michael@0: if(config!=NULL) { michael@0: uprv_strncpy(plug->config, config, UPLUG_NAME_MAX); michael@0: } michael@0: michael@0: return plug; michael@0: } michael@0: michael@0: /** michael@0: * Fetch a plugin from DLL, and then initialize it from a library- but don't load it. michael@0: */ michael@0: static UPlugData* michael@0: uplug_initPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status) { michael@0: void *lib = NULL; michael@0: UPlugData *plug = NULL; michael@0: if(U_FAILURE(*status)) { return NULL; } michael@0: lib = uplug_openLibrary(libName, status); michael@0: if(lib!=NULL && U_SUCCESS(*status)) { michael@0: UPlugEntrypoint *entrypoint = NULL; michael@0: entrypoint = (UPlugEntrypoint*)uprv_dlsym_func(lib, sym, status); michael@0: michael@0: if(entrypoint!=NULL&&U_SUCCESS(*status)) { michael@0: plug = uplug_initPlugFromEntrypointAndLibrary(entrypoint, config, lib, sym, status); michael@0: if(plug!=NULL&&U_SUCCESS(*status)) { michael@0: plug->lib = lib; /* plug takes ownership of library */ michael@0: lib = NULL; /* library is now owned by plugin. */ michael@0: } michael@0: } else { michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: plug = uplug_initErrorPlug(libName,sym,config,"ERROR: Could not load entrypoint",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); michael@0: } michael@0: if(lib!=NULL) { /* still need to close the lib */ michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: uplug_closeLibrary(lib, &subStatus); /* don't care here */ michael@0: } michael@0: } else { michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: plug = uplug_initErrorPlug(libName,sym,config,"ERROR: could not load library",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); michael@0: } michael@0: return plug; michael@0: } michael@0: michael@0: U_CAPI UPlugData* U_EXPORT2 michael@0: uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status) { michael@0: UPlugData *plug = NULL; michael@0: if(U_FAILURE(*status)) { return NULL; } michael@0: plug = uplug_initPlugFromLibrary(libName, sym, config, status); michael@0: uplug_loadPlug(plug, status); michael@0: michael@0: return plug; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: U_CAPI UPlugLevel U_EXPORT2 uplug_getCurrentLevel() { michael@0: if(cmemory_inUse()) { michael@0: return UPLUG_LEVEL_HIGH; michael@0: } else { michael@0: return UPLUG_LEVEL_LOW; michael@0: } michael@0: } michael@0: michael@0: static UBool U_CALLCONV uplug_cleanup(void) michael@0: { michael@0: int32_t i; michael@0: michael@0: UPlugData *pluginToRemove; michael@0: /* cleanup plugs */ michael@0: for(i=0;iawaitingLoad) { michael@0: if(pluginToLoad->level == UPLUG_LEVEL_LOW) { michael@0: if(currentLevel > UPLUG_LEVEL_LOW) { michael@0: pluginToLoad->pluginStatus = U_PLUGIN_TOO_HIGH; michael@0: } else { michael@0: UPlugLevel newLevel; michael@0: uplug_loadPlug(pluginToLoad, &subStatus); michael@0: newLevel = uplug_getCurrentLevel(); michael@0: if(newLevel > currentLevel) { michael@0: pluginToLoad->pluginStatus = U_PLUGIN_CHANGED_LEVEL_WARNING; michael@0: currentLevel = newLevel; michael@0: } michael@0: } michael@0: pluginToLoad->awaitingLoad = FALSE; michael@0: } michael@0: } michael@0: } michael@0: for(i=0;iawaitingLoad) { michael@0: if(pluginToLoad->level == UPLUG_LEVEL_INVALID) { michael@0: pluginToLoad->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; michael@0: } else if(pluginToLoad->level == UPLUG_LEVEL_UNKNOWN) { michael@0: pluginToLoad->pluginStatus = U_INTERNAL_PROGRAM_ERROR; michael@0: } else { michael@0: uplug_loadPlug(pluginToLoad, &subStatus); michael@0: } michael@0: pluginToLoad->awaitingLoad = FALSE; michael@0: } michael@0: } michael@0: michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, " Done Loading Plugs. Level: %d\n", (int32_t)uplug_getCurrentLevel())); michael@0: #endif michael@0: } michael@0: michael@0: /* Name of the plugin config file */ michael@0: static char plugin_file[2048] = ""; michael@0: #endif michael@0: michael@0: U_INTERNAL const char* U_EXPORT2 michael@0: uplug_getPluginFile() { michael@0: #if U_ENABLE_DYLOAD michael@0: return plugin_file; michael@0: #else michael@0: return NULL; michael@0: #endif michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uplug_init(UErrorCode *status) { michael@0: #if !U_ENABLE_DYLOAD michael@0: (void)status; /* unused */ michael@0: #else michael@0: const char *plugin_dir; michael@0: michael@0: if(U_FAILURE(*status)) return; michael@0: plugin_dir = getenv("ICU_PLUGINS"); michael@0: michael@0: #if defined(DEFAULT_ICU_PLUGINS) michael@0: if(plugin_dir == NULL || !*plugin_dir) { michael@0: plugin_dir = DEFAULT_ICU_PLUGINS; michael@0: } michael@0: #endif michael@0: michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "ICU_PLUGINS=%s\n", plugin_dir)); michael@0: #endif michael@0: michael@0: if(plugin_dir != NULL && *plugin_dir) { michael@0: FILE *f; michael@0: michael@0: michael@0: #ifdef OS390BATCH michael@0: /* There are potentially a lot of ways to implement a plugin directory on OS390/zOS */ michael@0: /* Keeping in mind that unauthorized file access is logged, monitored, and enforced */ michael@0: /* I've chosen to open a DDNAME if BATCH and leave it alone for (presumably) UNIX */ michael@0: /* System Services. Alternative techniques might be allocating a member in */ michael@0: /* SYS1.PARMLIB or setting an environment variable "ICU_PLUGIN_PATH" (?). The */ michael@0: /* DDNAME can be connected to a file in the HFS if need be. */ michael@0: michael@0: uprv_strncpy(plugin_file,"//DD:ICUPLUG", 2047); /* JAM 20 Oct 2011 */ michael@0: #else michael@0: uprv_strncpy(plugin_file, plugin_dir, 2047); michael@0: uprv_strncat(plugin_file, U_FILE_SEP_STRING,2047); michael@0: uprv_strncat(plugin_file, "icuplugins",2047); michael@0: uprv_strncat(plugin_file, U_ICU_VERSION_SHORT ,2047); michael@0: uprv_strncat(plugin_file, ".txt" ,2047); michael@0: #endif michael@0: michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "pluginfile= %s\n", plugin_file)); michael@0: #endif michael@0: michael@0: #ifdef __MVS__ michael@0: if (iscics()) /* 12 Nov 2011 JAM */ michael@0: { michael@0: f = NULL; michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: f = fopen(plugin_file, "r"); michael@0: } michael@0: michael@0: if(f != NULL) { michael@0: char linebuf[1024]; michael@0: char *p, *libName=NULL, *symName=NULL, *config=NULL; michael@0: int32_t line = 0; michael@0: michael@0: michael@0: while(fgets(linebuf,1023,f)) { michael@0: line++; michael@0: michael@0: if(!*linebuf || *linebuf=='#') { michael@0: continue; michael@0: } else { michael@0: p = linebuf; michael@0: while(*p&&isspace((int)*p)) michael@0: p++; michael@0: if(!*p || *p=='#') continue; michael@0: libName = p; michael@0: while(*p&&!isspace((int)*p)) { michael@0: p++; michael@0: } michael@0: if(!*p || *p=='#') continue; /* no tab after libname */ michael@0: *p=0; /* end of libname */ michael@0: p++; michael@0: while(*p&&isspace((int)*p)) { michael@0: p++; michael@0: } michael@0: if(!*p||*p=='#') continue; /* no symname after libname +tab */ michael@0: symName = p; michael@0: while(*p&&!isspace((int)*p)) { michael@0: p++; michael@0: } michael@0: michael@0: if(*p) { /* has config */ michael@0: *p=0; michael@0: ++p; michael@0: while(*p&&isspace((int)*p)) { michael@0: p++; michael@0: } michael@0: if(*p) { michael@0: config = p; michael@0: } michael@0: } michael@0: michael@0: /* chop whitespace at the end of the config */ michael@0: if(config!=NULL&&*config!=0) { michael@0: p = config+strlen(config); michael@0: while(p>config&&isspace((int)*(--p))) { michael@0: *p=0; michael@0: } michael@0: } michael@0: michael@0: /* OK, we're good. */ michael@0: { michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: UPlugData *plug = uplug_initPlugFromLibrary(libName, symName, config, &subStatus); michael@0: if(U_FAILURE(subStatus) && U_SUCCESS(*status)) { michael@0: *status = subStatus; michael@0: } michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "PLUGIN libName=[%s], sym=[%s], config=[%s]\n", libName, symName, config)); michael@0: DBG((stderr, " -> %p, %s\n", (void*)plug, u_errorName(subStatus))); michael@0: #else michael@0: (void)plug; /* unused */ michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: fclose(f); michael@0: } else { michael@0: #if UPLUG_TRACE michael@0: DBG((stderr, "Can't open plugin file %s\n", plugin_file)); michael@0: #endif michael@0: } michael@0: } michael@0: uplug_loadWaitingPlugs(status); michael@0: #endif /* U_ENABLE_DYLOAD */ michael@0: ucln_registerCleanup(UCLN_UPLUG, uplug_cleanup); michael@0: }