harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r350181 [156/198] - in /incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core: ./ depends/ depends/files/ depends/jars/ depends/libs/ depends/libs/linux.IA32/ depends/libs/win.IA32/ depends/oss/ depends/oss/linux.IA32/ depends/oss/win....
Date Thu, 01 Dec 2005 06:04:00 GMT
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfiletext.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfiletext.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfiletext.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfiletext.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,397 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CDEV_CURRENT_FUNCTION _comment_
+/**
+ * @file
+ * @ingroup Port
+ * @brief file
+ */
+
+#undef CDEV_CURRENT_FUNCTION
+
+/* __STDC_ISO_10646__ indicates that the platform wchar_t encoding is Unicode */
+/* but older versions of libc fail to set the flag, even though they are Unicode */
+#if defined(__STDC_ISO_10646__) || defined(LINUX)
+#define HYVM_USE_WCTOMB
+#else
+#define HYVM_USE_ICONV
+#endif
+
+#if defined(HYVM_USE_ICONV)
+#include <iconv.h>
+#endif
+
+#include <nl_types.h>
+#include <langinfo.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "hyport.h"
+
+/* Some older platforms (Netwinder) don't declare CODESET */
+#if !defined(CODESET)
+#define CODESET _NL_CTYPE_CODESET_NAME
+#endif
+
+/* a2e overrides nl_langinfo to return ASCII strings. We need the native EBCDIC string */
+
+#define CDEV_CURRENT_FUNCTION _prototypes_private
+#if (defined(HYVM_USE_ICONV))
+static IDATA file_write_using_iconv (struct HyPortLibrary *portLibrary,
+                                     IDATA fd, const char *buf, IDATA nbytes);
+#endif /* HYVM_USE_ICONV (autogen) */
+
+#if (defined(HYVM_USE_WCTOMB))
+static IDATA walkUTF8String (const U_8 * buf, IDATA nbytes);
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#if (defined(HYVM_USE_WCTOMB))
+static void translateUTF8String (const U_8 * in, U_8 * out, IDATA nbytes);
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#if (defined(HYVM_USE_WCTOMB))
+static IDATA file_write_using_wctomb (struct HyPortLibrary *portLibrary,
+                                      IDATA fd, const char *buf,
+                                      IDATA nbytes);
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyfile_write_text
+/**
+ * Output the buffer onto the stream as text. The buffer is a UTF8-encoded array of chars.
+ * It is converted to the appropriate platform encoding.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] fd the file descriptor.
+ * @param[in] buf buffer of text to be output.
+ * @param[in] nbytes size of buffer of text to be output.
+ *
+ * @return 0 on success, negative error code on failure.
+ */
+IDATA VMCALL
+hyfile_write_text (struct HyPortLibrary *portLibrary, IDATA fd,
+                   const char *buf, IDATA nbytes)
+{
+  IDATA result, i;
+  int requiresTranslation = 0;
+
+  const char *utf8Encoding = "UTF-8";
+
+  /* we can short circuit if the string is all ASCII */
+  for (i = 0; i < nbytes; i++)
+    {
+      if ((U_8) buf[i] >= 0x80)
+        {
+          requiresTranslation = 1;
+          break;
+        }
+    }
+
+  if (!requiresTranslation
+      || strcmp (nl_langinfo (CODESET), utf8Encoding) == 0)
+    {
+      /* We're in luck! No transformations are necessary */
+      result =
+        portLibrary->file_write (portLibrary, fd, (void *) buf, nbytes);
+      return (result == nbytes) ? 0 : result;
+    }
+
+#if defined(HYVM_USE_WCTOMB)
+  return file_write_using_wctomb (portLibrary, fd, buf, nbytes);
+#else
+  return file_write_using_iconv (portLibrary, fd, buf, nbytes);
+#endif
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION walkUTF8String
+#if (defined(HYVM_USE_WCTOMB))
+/** 
+ * @internal walk the given UTF8 string, looking for non-ASCII characters.
+ * @return  0 if none were found, or, if non-ASCII strings were found,
+ * answer the length of the buffer if it were converted to platform
+ * encoding
+ *
+ * @note this relies on the assumption that wide chars are Unicode.
+ * If not, the platform will need different support for this
+ */
+static IDATA
+walkUTF8String (const U_8 * buf, IDATA nbytes)
+{
+  const U_8 *end = buf + nbytes;
+  const U_8 *cursor = buf;
+  IDATA newLength = 0;
+  int hasHighChars = 0;
+  /* reset the shift state */
+  wctomb (NULL, 0);
+  while (cursor < end)
+    {
+      if ((*cursor & 0x80) == 0x80)
+        {
+          char temp[MB_CUR_MAX];
+          int wcresult;
+          U_16 unicode;
+          U_32 numberU8Consumed =
+            decodeUTF8CharN (cursor, &unicode, end - cursor);
+          if (numberU8Consumed == 0)
+            {
+              /* an illegal encoding was encountered! Don't try to decode the string */
+              return 0;
+            }
+          cursor += numberU8Consumed;
+          /* calculate the encoded length of this character */
+          wcresult = wctomb (temp, (wchar_t) unicode);
+          if (wcresult == -1)
+            {
+              /* an un-encodable char was encountered */
+              newLength += 1;
+            }
+          else
+            {
+              newLength += wcresult;
+            }
+          hasHighChars = 1;
+        }
+      else
+        {
+          newLength += 1;
+          cursor += 1;
+        }
+    }
+  return hasHighChars ? newLength : 0;
+}
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION translateUTF8String
+#if (defined(HYVM_USE_WCTOMB))
+/**
+ * @internal assumes that the input has already been validated by walkUTF8String
+ */
+static void
+translateUTF8String (const U_8 * in, U_8 * out, IDATA nbytes)
+{
+  const U_8 *end = in + nbytes;
+  const U_8 *cursor = in;
+  /* walk the string again, translating it */
+  while (cursor < end)
+    {
+      U_32 numberU8Consumed;
+      if ((*cursor & 0x80) == 0x80)
+        {
+          U_16 unicode;
+          int wcresult;
+          numberU8Consumed = decodeUTF8Char (cursor, &unicode);
+          cursor += numberU8Consumed;
+          wcresult = wctomb (out, (wchar_t) unicode);
+          if (wcresult == -1)
+            {
+              *out++ = '?';
+            }
+          else
+            {
+              out += wcresult;
+            }
+        }
+      else
+        {
+          *out++ = *cursor++;
+        }
+    }
+}
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION file_write_using_wctomb
+#if (defined(HYVM_USE_WCTOMB))
+static IDATA
+file_write_using_wctomb (struct HyPortLibrary *portLibrary, IDATA fd,
+                         const char *buf, IDATA nbytes)
+{
+  IDATA result;
+  IDATA newLength = 0;
+  char stackBuf[512];
+  char *newBuf = stackBuf;
+  newLength = walkUTF8String ((U_8 *) buf, nbytes);
+  if (newLength)
+    {
+      if (newLength > sizeof (stackBuf))
+        {
+          newBuf = portLibrary->mem_allocate_memory (portLibrary, newLength);
+        }
+      if (newBuf)
+        {
+          translateUTF8String (buf, newBuf, nbytes);
+          buf = newBuf;
+          nbytes = newLength;
+        }
+    }
+  result = portLibrary->file_write (portLibrary, fd, (void *) buf, nbytes);
+  if (newBuf != stackBuf && newBuf != NULL)
+    {
+      portLibrary->mem_free_memory (portLibrary, newBuf);
+    }
+  return (result == nbytes) ? 0 : result;
+}
+#endif /* HYVM_USE_WCTOMB (autogen) */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION file_write_using_iconv
+#if (defined(HYVM_USE_ICONV))
+static IDATA
+file_write_using_iconv (struct HyPortLibrary *portLibrary, IDATA fd,
+                        const char *buf, IDATA nbytes)
+{
+  IDATA result, i;
+  IDATA newLength = 0;
+  char stackBuf[512];
+  char *bufStart;
+  UDATA outBufLen = sizeof (stackBuf);
+  iconv_t converter;
+  size_t inbytesleft, outbytesleft;
+  char *inbuf, *outbuf;
+/* iconv_open is not an a2e function, so we need to pass it honest-to-goodness EBCDIC strings */
+  converter = iconv_open (nl_langinfo (CODESET), "UTF-8");
+  if (converter == (iconv_t) - 1)
+    {
+      /* no converter available for this code set. Just dump the UTF-8 chars */
+      result =
+        portLibrary->file_write (portLibrary, fd, (void *) buf, nbytes);
+      return (result == nbytes) ? 0 : result;
+    }
+  inbuf = (char *) buf;         /* for some reason this argument isn't const */
+  outbuf = bufStart = stackBuf;
+  inbytesleft = nbytes;
+  outbytesleft = sizeof (stackBuf);
+  while ((size_t) - 1 ==
+         iconv (converter, &inbuf, &inbytesleft, &outbuf, &outbytesleft))
+    {
+      if (errno == E2BIG)
+        {
+          /* grow the buffer by 512 more bytes */
+          char *newBuf =
+            portLibrary->mem_allocate_memory (portLibrary, outBufLen += 512);
+          if (newBuf == NULL)
+            {
+              break;            /* just output what we've got so far */
+            }
+          /* copy over the work we've already done */
+          memcpy (newBuf, bufStart, outbuf - bufStart);
+          /* set up the new buffer, and free the old one */
+          outbytesleft = outBufLen - (outbuf - bufStart);
+          outbuf = newBuf + (outbuf - bufStart);
+          if (bufStart != stackBuf)
+            {
+              portLibrary->mem_free_memory (portLibrary, bufStart);
+            }
+          bufStart = newBuf;
+        }
+      else
+        {
+          /* failure -- just output the unconverted data */
+          iconv_close (converter);
+          result =
+            portLibrary->file_write (portLibrary, fd, (void *) buf, nbytes);
+          return (result == nbytes) ? 0 : result;
+        }
+    }
+  iconv_close (converter);
+  result =
+    portLibrary->file_write (portLibrary, fd, (void *) bufStart,
+                             outbuf - bufStart);
+  if (bufStart != stackBuf)
+    {
+      portLibrary->mem_free_memory (portLibrary, bufStart);
+    }
+  return (result == nbytes) ? 0 : result;
+}
+#endif /* HYVM_USE_ICONV (autogen) */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyfile_read_text
+/**
+ * Read a line of text from the file into buf.  Text is converted from the platform file encoding to UTF8.
+ * This is mostly equivalent to fgets in standard C.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] fd File descriptor.
+ * @param[in,out] buf Buffer for read in text.
+ * @param[in] nbytes Size of buffer.
+ *
+ * @return buf on success, NULL on failure.
+ */
+char *VMCALL
+hyfile_read_text (struct HyPortLibrary *portLibrary, IDATA fd, char *buf,
+                  IDATA nbytes)
+{
+  char temp[64];
+  IDATA count, i, result;
+  char *cursor = buf;
+
+  if (nbytes <= 0)
+    {
+      return 0;
+    }
+
+  /* discount 1 for the trailing NUL */
+  nbytes -= 1;
+
+  while (nbytes)
+    {
+      count = sizeof (temp) > nbytes ? nbytes : sizeof (temp);
+      count = portLibrary->file_read (portLibrary, fd, temp, count);
+
+      /* ignore translation for now */
+      if (count < 0)
+        {
+          if (cursor == buf)
+            {
+              return NULL;
+            }
+          else
+            {
+              break;
+            }
+        }
+
+      for (i = 0; i < count; i++)
+        {
+          char c = temp[i];
+          *cursor++ = c;
+
+          if (c == '\n')
+            {                   /* EOL */
+              portLibrary->file_seek (portLibrary, fd, i - count + 1,
+                                      HySeekCur);
+              *cursor = '\0';
+              return buf;
+            }
+        }
+      nbytes -= count;
+    }
+
+  *cursor = '\0';
+  return buf;
+}
+
+#undef CDEV_CURRENT_FUNCTION

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfpu.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfpu.h?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfpu.h (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyfpu.h Wed Nov 30 21:29:27 2005
@@ -0,0 +1,28 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file 
+ * @ingroup Port
+ * @brief Floating Point Support
+ */
+
+#if !defined(hyfpu_h)
+#define hyfpu_h
+/**
+ * Initialize floating point support
+ */
+#define 	HYPORT_SETUP_FPU_STATE()
+#endif /* hyfpu_h */

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,158 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @ingroup Port
+ * @file
+ * Provides platform-neutral signal handling functions.  
+ * The @ref hygp_register_handler function is partially dependent on internal VM structures, and must be called with NULL as a third (userData) parameter.
+ * <br><br>
+ */
+
+#include "hyport.h"
+#include "hycomp.h"
+#include "gp.h"
+
+/**
+ * Kicks off the new thread by calling the function provided in protected_fn fn.
+ *
+ * All threads spawned by the vm start here and all OS signals that will be handled
+ * by fn must be registered to the OS here. 
+ * Upon receiving a signal from the OS, fn is responsible for calling the function 
+ * specified in @ref hygp_register_handler if it is determined that a shutdown is required.
+ * 
+ * @param[in] portLibrary The port library
+ * @param[in] fn the function that will be used to kick off the thread
+ * @param[in] arg arguments to protected_fn fn
+ *
+ * @return the return value of the function provided in fn
+ * 
+ * @note it is a good idea to save the portLibrary in case a registered exception handler 
+ * cannot be found
+ */
+UDATA VMCALL
+hygp_protect (struct HyPortLibrary * portLibrary, protected_fn fn, void *arg)
+{
+  /* sorry!  No way to protect it generically.  Just run it. */
+  return fn (arg);
+}
+
+/**
+ * Sets the function that is responsible for preserving/outputting the state of the vm and
+ * initiating a graceful shutdown resulting from a gp.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] fn function responsible for preserving/outputting the state of the vm and 
+ * initiating a graceful shutdown resulting from a gp.
+ * @param[in] aUserData The HyJavaVM or NULL for non-HY consumers.
+ *
+ * @note vmGPHandler in gphandle.c is currently the only function used for handling gps. 
+ * @note  fn is not called by the OS but by the gp handler function specified in the call
+ * to @ref hygp_protect gp module
+ * @note above occurs after the OS has passed a signal along to us and it is determined 
+ * that a shutdown is required.
+ */
+void VMCALL
+hygp_register_handler (struct HyPortLibrary *portLibrary, handler_fn fn,
+                       void *aUserData)
+{
+}
+
+/**
+ * Provides the name and value, specified by category/index of the gp information in info. 
+ * Returns the kind of information found at category/index specified, or undefined 
+ * if the category/index are invalid.  The number of items in the category specified must 
+ * equal the count @ref hygp_info_count returns for that category. 
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] info struct containing all available signal information. Normally includes 
+ * register values, name of module where crash occured and its base address.
+ * @param[in] category the category of signal information that you are querying. 
+ * @param[in] index the index of the item in the specified category. The number of items 
+ * for each category are defined in the sourceTemplate.
+ * @param[out] name the name of the item at the specified index.  
+ * @param[out] value the value of the item at the specified index
+ *
+ * @return the kind of info at the specified index. For example, this allows the caller to 
+ * determine whether the item placed in **value corresponds to a 32/64-bit integer or a pointer to a string. 
+ *
+ * @note The program counter and module name also have negative indexes as defined by 
+ * HYGP_CONTROL_PC and HYGP_MODULE_NAME respectively.
+ * @note Above allows the handler function registered in @ref hygp_register_handler to 
+ * distinguish (and use) them from the other gp items.
+ * @note The caller is responsible for allocating and freeing any buffers used by **name, **value.
+ */
+U_32 VMCALL
+hygp_info (struct HyPortLibrary *portLibrary, void *info, U_32 category,
+           I_32 index, const char **name, void **value)
+{
+  /* default implementation can't do anything */
+  return 0;
+}
+
+/**
+ * Returns the number of items that exist in the category specified, or zero if the category is undefined.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] info struct containing all available signal information. Normally includes register values, 
+ * name of module where crash occured and its base address. 
+ * @param[in] category the category in which we want to find the number of items that exist.
+ *
+ * @note Return value must agree with the number of items that @ref hygp_info makes available for the 
+ * category specified. 
+*/
+U_32 VMCALL
+hygp_info_count (struct HyPortLibrary * portLibrary, void *info,
+                 U_32 category)
+{
+  /* default implementation can't do anything */
+  return 0;
+}
+
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  
+ * Any resources that were created by @ref hygp_startup
+ * should be destroyed here.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @note Most implementations will be empty.
+ */
+void VMCALL
+hygp_shutdown (struct HyPortLibrary *portLibrary)
+{
+}
+
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the shared library operation may be created here.  All resources created here should be destroyed
+ * in @ref hygp_shutdown.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_GP
+ *
+ * @note Most implementations will simply return success.
+ */
+I_32 VMCALL
+hygp_startup (struct HyPortLibrary *portLibrary)
+{
+  return 0;
+}

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.h?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.h (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hygp.h Wed Nov 30 21:29:27 2005
@@ -0,0 +1,29 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(hygp_h)
+#define hygp_h
+void VMCALL hygp_register_handler (struct HyPortLibrary *portLibrary,
+                                   handler_fn fn, void *aUserData);
+I_32 VMCALL hygp_startup (struct HyPortLibrary *portLibrary);
+void VMCALL hygp_shutdown (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hygp_protect (struct HyPortLibrary *portLibrary, protected_fn fn,
+                           void *arg);
+U_32 VMCALL hygp_info_count (struct HyPortLibrary *portLibrary, void *info,
+                             U_32 category);
+U_32 VMCALL hygp_info (struct HyPortLibrary *portLibrary, void *info,
+                       U_32 category, I_32 index, const char **name,
+                       void **value);
+#endif /* hygp_h */

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyipcmutex.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyipcmutex.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyipcmutex.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hyipcmutex.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,284 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CDEV_CURRENT_FUNCTION _comment_
+/**
+ * @file
+ * @ingroup Port
+ * @brief Shared Resource Mutex
+ *
+ * The HyIPCMutex is used to protect a shared resource from 
+ * simultaneous access by processes or threads executing in 
+ * the same or different VMs.
+ * Each process/thread must request and wait for the ownership 
+ * of the shared resource before it can use that resource. 
+ * It must also release the ownership
+ * of the resource as soon as it has finished using it so that 
+ * other processes competing for the same resource are not delayed.
+ */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include <string.h>
+#include <errno.h>
+#include "hyport.h"
+
+/* arg for semctl semaphore system calls. */
+union semun
+{
+  int val;
+  struct semid_ds *buf;
+  U_16 *array;
+};
+
+#define CDEV_CURRENT_FUNCTION _prototypes_private
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyipcmutex_acquire
+/**
+ * Acquires a named mutex for the calling process.
+ *
+ * If a Mutex with the same Name already exists, the function opens the existing 
+ * Mutex and tries to lock it.
+ * If another process already has the Mutex locked, the function will block indefinetely. 
+ * If there is no Mutex with the same Name, the function will create it and lock
+ * it for the calling process of this function.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] name Mutex to be acquired
+ *
+ * @return 0 on success, -1 on error.
+ *
+ * @note The Mutex must be explicitly released by calling the @ref hyipcmutex_release function as 
+ * soon as the lock is no longer required.
+ */
+I_32 VMCALL
+hyipcmutex_acquire (struct HyPortLibrary *portLibrary, const char *name)
+{
+  int sid;
+  int nsops;                    /* number of operations to perform on semaphore */
+  int nsems = 1;                /* number of semaphores */
+  key_t sKey;                   /* semaphore identifier key */
+  int nameLen;                  /* length of semaphore name */
+  char *sPath;                  /* semaphore path (used in ftok) */
+  int sPathLen;                 /* length of semaphore path */
+  union semun arg;              /* initialization options structure */
+  struct sembuf sLock;          /* operations buffer */
+  I_32 mutexFD;                 /* mutex file descriptor */
+
+  nameLen = strlen (name);
+  /* check if length of semaphore name is empty */
+  if (nameLen == 0)
+    {
+      return -1;
+    }
+
+  /* get size required for semaphore path and name */
+  sPathLen = nameLen + sizeof ("/tmp/");
+
+  sPath = portLibrary->mem_allocate_memory (portLibrary, sPathLen);
+  if (!sPath)
+    {
+      return -1;
+    }
+
+  /* initialize semaphore path */
+  strcpy (sPath, "/tmp/");
+  strcat (sPath, name);
+
+  /* create file to be used by semaphore */
+  mutexFD =
+    portLibrary->file_open (portLibrary, sPath,
+                            HyOpenCreate | HyOpenRead | HyOpenWrite, 0666);
+  if (mutexFD == -1)
+    {
+      return -1;
+    }
+
+  /* close handle */
+  portLibrary->file_close (portLibrary, mutexFD);
+
+  /* build unique semaphore key */
+  sKey = ftok (sPath, 's');
+
+  /* free allocated memory no longer needed */
+  portLibrary->mem_free_memory (portLibrary, sPath);
+
+  if (sKey == ((key_t) - 1))
+    {
+      return -1;
+    }
+
+  /* check if semaphore already exists */
+  sid = semget (sKey, 0, 0666);
+  if (sid == -1)
+    {
+      /* semaphore doesn't exist, create it */
+      sid = semget (sKey, nsems, IPC_CREAT | 0666);
+      if (sid == -1)
+        {
+          return -1;
+        }
+
+      /* sempahore created, set initial value */
+      arg.val = 1;
+      if (semctl (sid, 0, SETVAL, arg) == -1)
+        {
+          semctl (sid, 0, IPC_RMID, arg);       /* cleanup semaphore from system */
+          return -1;
+        }
+    }
+
+  /* initialize operation structure to lock mutex */
+  sLock.sem_num = 0;
+  sLock.sem_op = -1;
+  sLock.sem_flg = 0;
+
+  /* set operation to acquire semaphore */
+  nsops = 1;                    /* one operation to be performed (acquire) */
+
+  /* if semaphore was acquired successfully, return 0 */
+  /* otherwise, return -1 */
+  return (I_32) semop (sid, &sLock, nsops);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyipcmutex_release
+/**
+ * Releases a named Mutex from the calling process.
+ *
+ * If a Mutex with the same Name already exists, the function opens the existing Mutex and tries to unlock it.
+ * This function will fail if a Mutex with the given Name is not found or if the Mutex cannot be unlocked.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] name Mutex to be released.
+ *
+ * @return 0 on success, -1 on error.
+ *
+ * @note Callers of this function must have called the function @ref hyipcmutex_acquire prior to calling this function.
+ */
+I_32 VMCALL
+hyipcmutex_release (struct HyPortLibrary * portLibrary, const char *name)
+{
+  int sid;
+  int nsops;                    /* number of operations to perform on semaphore */
+  int nsems = 1;                /* number of semaphores */
+  key_t sKey = 439;             /* semaphore identifier key */
+  int nameLen;                  /* length of semaphore name */
+  char *sPath;                  /* semaphore path (used in ftok) */
+  int sPathLen;                 /* length of semaphore path */
+  struct sembuf sUnlock;        /* operations buffer */
+
+  nameLen = strlen (name);
+
+  /* get size required for semaphore path and name */
+  sPathLen = nameLen + sizeof ("/tmp/");
+
+  /* check if length of semaphore name is empty or if it exeeds max size for path */
+  if (nameLen == 0)
+    {
+      return -1;
+    }
+
+  sPath = portLibrary->mem_allocate_memory (portLibrary, sPathLen);
+  if (!sPath)
+    {
+      return -1;
+    }
+
+  /* build unique semaphore key */
+  strcpy (sPath, "/tmp/");
+  strcat (sPath, name);
+  sKey = ftok (sPath, 's');
+
+  /* free allocated memory no longer needed */
+  portLibrary->mem_free_memory (portLibrary, sPath);
+
+  if (sKey == (key_t) - 1)
+    {
+      return -1;
+    }
+
+  /* open existing semaphore */
+  sid = semget (sKey, 0, 0666);
+  if (sid == -1)
+    {
+      return -1;
+    }
+
+  /* initialize operation structure to unlock mutex */
+  sUnlock.sem_num = 0;
+  sUnlock.sem_op = 1;
+  sUnlock.sem_flg = 0;
+
+  /* set operations to wait and acquire semaphore */
+  nsops = 1;                    /* one operation to be performed (release) */
+
+  /* if semaphore was unlocked successfully, return 0 */
+  /* otherwise, return -1 */
+  return (I_32) semop (sid, &sUnlock, nsops);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyipcmutex_shutdown
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by @ref hyipcmutex_startup
+ * should be destroyed here.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @note Most implementations will be empty.
+ */
+void VMCALL
+hyipcmutex_shutdown (struct HyPortLibrary *portLibrary)
+{
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hyipcmutex_startup
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the IPC mutex operations may be created here.  All resources created here should be destroyed
+ * in @ref hyipcmutex_shutdown.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_IPCMUTEX
+ *
+ * @note Most implementations will simply return success.
+ */
+I_32 VMCALL
+hyipcmutex_startup (struct HyPortLibrary *portLibrary)
+{
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION
+
+#undef CDEV_CURRENT_FUNCTION

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymem.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymem.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymem.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymem.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,265 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CDEV_CURRENT_FUNCTION _comment_
+/**
+ * @file
+ * @ingroup Port
+ * @brief Memory Utilities
+ */
+
+#undef CDEV_CURRENT_FUNCTION
+
+/* 
+ * This file contains code for the portability library memory management.
+ */
+#include <stdlib.h>
+#include <string.h>
+#include "hyport.h"
+#include "portpriv.h"
+#include "hyportpg.h"
+#include "ut_hyprt.h"
+
+#if defined(DEBUG_MALLOC_FREE_LEAK)
+#include <stdio.h>
+static UDATA DEBUG_TOTAL_ALLOCATED_MEMORY = 0;
+#endif
+
+
+#define CDEV_CURRENT_FUNCTION hymem_allocate_memory
+/**
+ * Allocate memory.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] byteAmount Number of bytes to allocate.
+ *
+ * @return pointer to memory on success, NULL on error.
+ * @return Memory is not guaranteed to be zeroed as part of this call
+ *
+ * @internal @warning Do not call error handling code @ref hyerror upon error as 
+ * the error handling code uses per thread buffers to store the last error.  If memory
+ * can not be allocated the result would be an infinite loop.
+ */
+void *VMCALL
+hymem_allocate_memory (struct HyPortLibrary *portLibrary, UDATA byteAmount)
+{
+  void *pointer = NULL;
+  void *mem;
+
+  Trc_PRT_mem_hymem_allocate_memory_Entry (byteAmount);
+#if defined(DEBUG_MALLOC_FREE_LEAK)
+  if (byteAmount == 0)
+    {                           /* prevent malloc from failing causing allocate to return null */
+      byteAmount = 1;
+    }
+  DEBUG_TOTAL_ALLOCATED_MEMORY += byteAmount;
+  portLibrary->tty_printf (portLibrary,
+                           "\nallocate of %u bytes (new total is %u bytes)\n",
+                           byteAmount, DEBUG_TOTAL_ALLOCATED_MEMORY);
+  mem = (void *) malloc (byteAmount + sizeof (UDATA));
+  *((UDATA *) mem) = byteAmount;
+  pointer = ((UDATA) mem + sizeof (UDATA));
+#else
+  if (byteAmount == 0)
+    {                           /* prevent malloc from failing causing allocate to return null */
+      byteAmount = 1;
+    }
+  pointer = malloc (byteAmount);
+#endif
+
+
+  Trc_PRT_mem_hymem_allocate_memory_Exit (pointer);
+  return pointer;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_free_memory
+/**
+ * Deallocate memory.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] memoryPointer Base address of memory to be deallocated.
+ */
+void VMCALL
+hymem_free_memory (struct HyPortLibrary *portLibrary, void *memoryPointer)
+{
+  Trc_PRT_mem_hymem_free_memory_Entry (memoryPointer);
+#if defined(DEBUG_MALLOC_FREE_LEAK)
+  DEBUG_TOTAL_ALLOCATED_MEMORY -=
+    *((UDATA *) ((UDATA) memoryPointer - sizeof (UDATA)));
+  portLibrary->tty_printf (portLibrary,
+                           "\nfree of %u bytes (new total is %u bytes)\n",
+                           *((UDATA *) ((UDATA) memoryPointer -
+                                        sizeof (UDATA))),
+                           DEBUG_TOTAL_ALLOCATED_MEMORY);
+  free ((void *) ((UDATA) memoryPointer - sizeof (UDATA)));
+#else
+  free (memoryPointer);
+#endif
+
+
+  Trc_PRT_mem_hymem_free_memory_Exit ();
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_reallocate_memory
+/**
+ * Re-allocate memory.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] memoryPointer Base address of memory to be re-allocated.
+ * @param[in] byteAmount Number of bytes to re-allocated.
+ *
+ * @return pointer to memory on success, NULL on error.
+ *
+ * @internal @warning Do not call error handling code @ref hyerror upon error as 
+ * the error handling code uses per thread buffers to store the last error.  If memory
+ * can not be allocated the result would be an infinite loop.
+ */
+void *VMCALL
+hymem_reallocate_memory (struct HyPortLibrary *portLibrary,
+                         void *memoryPointer, UDATA byteAmount)
+{
+  void *ptr = NULL;
+
+  Trc_PRT_mem_hymem_reallocate_memory_Entry (memoryPointer, byteAmount);
+
+  ptr = realloc (memoryPointer, byteAmount);
+
+  Trc_PRT_mem_hymem_reallocate_memory_Exit (ptr);
+  return ptr;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_shutdown
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by @ref hymem_startup
+ * should be destroyed here.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @note Must deallocate portGlobals.
+ * @note Most implementations will just deallocate portGlobals.
+ */
+void VMCALL
+hymem_shutdown (struct HyPortLibrary *portLibrary)
+{
+  portLibrary->mem_free_memory (portLibrary, portLibrary->portGlobals);
+  portLibrary->portGlobals = NULL;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_startup
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the memory operations may be created here.  All resources created here should be destroyed
+ * in @ref hymem_shutdown.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] portGlobalSize Size of the global data structure to allocate
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_MEM
+ *.
+ * @note Must allocate portGlobals.
+ * @note Most implementations will just allocate portGlobals.
+ *
+ * @internal @note portLibrary->portGlobals must point to an aligned structure
+ */
+I_32 VMCALL
+hymem_startup (struct HyPortLibrary *portLibrary, UDATA portGlobalSize)
+{
+  portLibrary->portGlobals =
+    portLibrary->mem_allocate_memory (portLibrary, portGlobalSize);
+  if (!portLibrary->portGlobals)
+    {
+      return HYPORT_ERROR_STARTUP_MEM;
+    }
+  memset (portLibrary->portGlobals, 0, portGlobalSize);
+
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_allocate_memory_callSite
+/**
+ * Allocate memory.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] byteAmount Number of bytes to allocate.
+ * @param[in] callSite String describing callsite, usually file and line number.
+ *
+ * @return pointer to memory on success, NULL on error.
+ *
+ * @internal @warning Do not call error handling code @ref hyerror upon error as 
+ * the error handling code uses per thread buffers to store the last error.  If memory
+ * can not be allocated the result would be an infinite loop.
+ */
+void *VMCALL
+hymem_allocate_memory_callSite (struct HyPortLibrary *portLibrary,
+                                UDATA byteAmount, char *callSite)
+{
+  void *ptr = NULL;
+
+  Trc_PRT_mem_hymem_allocate_memory_callSite_Entry (byteAmount, callSite);
+  ptr = portLibrary->mem_allocate_memory (portLibrary, byteAmount);
+  Trc_PRT_mem_hymem_allocate_memory_callSite_Exit (ptr);
+  return ptr;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_allocate_portLibrary
+/**
+ * @internal Allocate memory for a portLibrary.
+ *
+ * @param[in] byteAmount Number of bytes to allocate.
+ *
+ * @return pointer to memory on success, NULL on error.
+ * @note This function is called prior to the portLibrary being initialized
+ * @note Must be implemented for all platforms.
+ */
+void *VMCALL
+hymem_allocate_portLibrary (UDATA byteAmount)
+{
+  return (void *) malloc (byteAmount);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymem_deallocate_portLibrary
+/**
+ * @internal Free memory for a portLibrary.
+ *
+ * @param[in] memoryPointer Base address to be deallocated.
+ *
+ * @note Must be implemented for all platforms.
+ */
+void VMCALL
+hymem_deallocate_portLibrary (void *memoryPointer)
+{
+  free (memoryPointer);
+}
+
+#undef CDEV_CURRENT_FUNCTION

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,173 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CDEV_CURRENT_FUNCTION _comment_
+/**
+ * @file
+ * @ingroup Port
+ * @brief Memory map
+ *
+ * This module provides memory mapping facilities that allow a user to map files
+ * into the virtual address space of the process.  There are various options that
+ * can be used when mapping a file into memory, such as copy on write.  Not all 
+ * of these options are available on all platforms, @ref hymmap_capabilities 
+ * provides the list of supported options.  Note also that on some platforms 
+ * memory mapping facilites do not exist at all. On these platforms the API will
+ * still be available, but will simply read the file into allocated memory.
+ */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "hyport.h"
+
+typedef struct HyLinuxMMAPHandle
+{
+  void *pointer;
+  size_t length;
+} HyLinuxMMAPHandle;
+
+#define CDEV_CURRENT_FUNCTION _prototypes_private
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymmap_map_file
+/**
+ * Map a file into memory.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] path - the path of the file to mapped into memory.
+ * @param[out] handle - updates *handle with the memory map handle, this handle is later passed to unmap.
+ *
+ * @return pointer to newly mapped memory on success, NULL on error.
+ */
+void *VMCALL
+hymmap_map_file (struct HyPortLibrary *portLibrary, const char *path,
+                 void **handle)
+{
+  HyLinuxMMAPHandle *lhandle;
+  int fd;
+  struct stat buf;
+
+  lhandle =
+    portLibrary->mem_allocate_memory (portLibrary,
+                                      sizeof (HyLinuxMMAPHandle));
+  if (lhandle == NULL)
+    {
+      return NULL;
+    }
+
+  fd = open (path, O_RDONLY);
+  if (!fstat (fd, &buf))
+    {
+      lhandle->length = buf.st_size;
+      lhandle->pointer =
+        mmap (0, lhandle->length, (PROT_READ | PROT_WRITE), MAP_PRIVATE, fd,
+              0);
+      close (fd);
+      if (lhandle->pointer != MAP_FAILED)
+        {
+          *handle = lhandle;
+          return lhandle->pointer;
+        }
+    }
+
+  return NULL;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymmap_unmap_file
+/**
+ * UnMap previously mapped memory.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @param[in] handle - the handle from the mmap_map_file.
+ */
+void VMCALL
+hymmap_unmap_file (struct HyPortLibrary *portLibrary, void *handle)
+{
+  HyLinuxMMAPHandle *lhandle = (HyLinuxMMAPHandle *) handle;
+  munmap (lhandle->pointer, lhandle->length);
+  portLibrary->mem_free_memory (portLibrary, handle);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymmap_shutdown
+/**
+ * PortLibrary shutdown.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by @ref hymmap_startup
+ * should be destroyed here.
+ *
+ */
+void VMCALL
+hymmap_shutdown (struct HyPortLibrary *portLibrary)
+{
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymmap_startup
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the memory mapping operations may be created here.  All resources created here should be destroyed
+ * in @ref hymmap_shutdown.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_MMAP
+ *
+ * @note Most implementations will simply return success.
+ */
+I_32 VMCALL
+hymmap_startup (struct HyPortLibrary *portLibrary)
+{
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hymmap_capabilities
+/**
+ * Check the capabilities available for HYMMAP at runtime for the current platform.
+ *
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return a bit map containing the capabilites supported by the hymmap sub component of the port library.
+ * Possible bit values:
+ *   HYPORT_MMAP_CAPABILITY_COPYONWRITE - if not present, platform is not capable of "copy on write" memory mapping.
+ *
+ */
+I_32 VMCALL
+hymmap_capabilities (struct HyPortLibrary * portLibrary)
+{
+  return HYPORT_MMAP_CAPABILITY_COPYONWRITE;
+}
+
+#undef CDEV_CURRENT_FUNCTION

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.h?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.h (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hymmap.h Wed Nov 30 21:29:27 2005
@@ -0,0 +1,26 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(hymmap_h)
+#define hymmap_h
+void VMCALL hymmap_unmap_file (struct HyPortLibrary *portLibrary,
+                               void *handle);
+void *VMCALL hymmap_map_file (struct HyPortLibrary *portLibrary,
+                              const char *path, void **handle);
+I_32 VMCALL hymmap_startup (struct HyPortLibrary *portLibrary);
+I_32 VMCALL hymmap_capabilities (struct HyPortLibrary *portLibrary);
+void VMCALL hymmap_shutdown (struct HyPortLibrary *portLibrary);
+#endif /* hymmap_h */
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hynls.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hynls.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hynls.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hynls.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,1234 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @ingroup Port
+ * @brief Native language support
+ */
+#define CDEV_CURRENT_FUNCTION include_header
+
+#undef CDEV_CURRENT_FUNCTION
+
+#include "hycomp.h"
+#include "hyport.h"
+#include "portpriv.h"
+#include "hythread.h"
+#include "hynlshelpers.h"
+#include "portnls.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#define CDEV_CURRENT_FUNCTION _prototypes_private
+static const char *nlsh_lookup (struct HyPortLibrary *portLibrary,
+                                U_32 module_name, U_32 message_num);
+
+static char *build_catalog_name (struct HyPortLibrary *portLibrary,
+                                 I_32 usePath, I_32 useDepth);
+
+static void free_catalog (struct HyPortLibrary *portLibrary);
+
+char *read_from_catalog (struct HyPortLibrary *portLibrary, IDATA fd,
+                         char *buf, IDATA bufsize);
+
+static HyNLSHashEntry *nls_allocateHashEntry (struct HyPortLibrary
+                                              *portLibrary, U_32 module_name,
+                                              U_32 message_num,
+                                              const char *message,
+                                              U_32 sizeOfMessage);
+static const char *parse_catalog (struct HyPortLibrary *portLibrary,
+                                  UDATA flags, U_32 module_name,
+                                  U_32 message_num,
+                                  const char *default_string);
+static void nlsh_insert (struct HyPortLibrary *portLibrary,
+                         HyNLSHashEntry * entry);
+
+static void open_catalog (struct HyPortLibrary *portLibrary);
+
+void convertModuleName (U_32 module_name, U_8 * module_str);
+static U_32 nlsh_hash (U_32 module_name, U_32 message_num);
+
+#undef CDEV_CURRENT_FUNCTION
+
+/* a sample key */
+#define HYNLS_EXEMPLAR "XXXX000"
+
+#define BUF_SIZE 1024
+
+#define CDEV_CURRENT_FUNCTION hynls_set_locale
+/**
+ * Set the language, region, and variant of the locale.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] lang - the language of the locale (e.g., "en"), 2 characters or less
+ * @param[in] region - the region of the locale (e.g., "US"), 2 characters or less
+ * @param[in] variant - the variant of the locale (e.g., "boont"), 31 characters or less
+ */
+void VMCALL
+hynls_set_locale (struct HyPortLibrary *portLibrary, const char *lang,
+                  const char *region, const char *variant)
+{
+
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_set_locale\n");
+#endif
+
+
+  hythread_monitor_enter (nls->monitor);
+
+  if (lang && strlen (lang) <= 2)
+    strcpy (nls->language, lang);
+  if (region && strlen (region) <= 2)
+    strcpy (nls->region, region);
+  if (variant && strlen (variant) <= 31)
+    strcpy (nls->variant, variant);
+
+  hythread_monitor_exit (nls->monitor);
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_get_language
+/**
+ * Return the string representing the currently set language.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return language string
+ */
+const char *VMCALL
+hynls_get_language (struct HyPortLibrary *portLibrary)
+{
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_get_language\n");
+#endif
+
+
+  return portLibrary->portGlobals->nls_data.language;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_get_region
+/**
+ * Return the string representing the currently set region.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return region string
+ */
+const char *VMCALL
+hynls_get_region (struct HyPortLibrary *portLibrary)
+{
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_get_region\n");
+#endif
+
+
+  return portLibrary->portGlobals->nls_data.region;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_get_variant
+/**
+ * Return the string representing the currently set variant.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return variant string
+ */
+const char *VMCALL
+hynls_get_variant (struct HyPortLibrary *portLibrary)
+{
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_get_variant\n");
+#endif
+
+
+  return portLibrary->portGlobals->nls_data.variant;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_printf
+/**
+ * Print a formatted NLS message. 
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] flags - to indicate what type of message (e.g., ERROR) and whether a newline is required
+ * @param[in] module_name - the module identifier of the NLS message
+ * @param[in] message_num - the NLS message number within the module
+ * @param[in] ... - arguments used in the NLS message format
+ *
+ */
+void VMCALL
+hynls_printf (struct HyPortLibrary *portLibrary, UDATA flags,
+              U_32 module_name, U_32 message_num, ...)
+{
+  va_list args;
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_printf\n");
+#endif
+
+
+  va_start (args, message_num);
+  portLibrary->nls_vprintf (portLibrary, flags, module_name, message_num,
+                            args);
+  va_end (args);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_vprintf
+/**
+ * Print a formatted NLS message. 
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] flags - to indicate what type of message (e.g., ERROR) and whether a newline is required
+ * @param[in] module_name - the module identifier of the NLS message
+ * @param[in] message_num - the NLS message number within the module
+ * @param[in] args - arguments used in the NLS message format
+ *
+ */
+void VMCALL
+hynls_vprintf (struct HyPortLibrary *portLibrary, UDATA flags,
+               U_32 module_name, U_32 message_num, va_list args)
+{
+  const char *message;
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_vprintf\n");
+#endif
+
+
+  message =
+    portLibrary->nls_lookup_message (portLibrary, flags, module_name,
+                                     message_num, NULL);
+
+#if defined(NLS_DEBUG)
+  portLibrary->tty_printf (portLibrary, "NLS - vprintf - message: %s\n",
+                           message);
+#endif
+
+
+  if (flags & HYNLS_STDOUT)
+    {
+      portLibrary->file_vprintf (portLibrary, HYPORT_TTY_OUT, message, args);
+    }
+  else
+    {
+      portLibrary->file_vprintf (portLibrary, HYPORT_TTY_ERR, message, args);
+    }
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_lookup_message
+/**
+ * Return the NLS string for the module name and message number.  If no string is found,
+ * or a failure occurs, return the default_string.
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] flags - to indicate what type of message (e.g., ERROR) and whether a newline is required
+ * @param[in] module_name - the module identifier of the NLS message
+ * @param[in] message_num - the NLS message number within the module
+ * @param[in] default_string - a default message, in case no NLS message is found
+ *
+ * @return NLS String
+ */
+const char *VMCALL
+hynls_lookup_message (struct HyPortLibrary *portLibrary, UDATA flags,
+                      U_32 module_name, U_32 message_num,
+                      const char *default_string)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  const char *message;
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_lookup_message\n");
+#endif
+
+
+  hythread_monitor_enter (nls->monitor);
+
+  if (!nls->catalog)
+    open_catalog (portLibrary);
+
+  message = nlsh_lookup (portLibrary, module_name, message_num);
+  if (!message)
+    {
+      message =
+        parse_catalog (portLibrary, flags, module_name, message_num,
+                       default_string);
+      if (!message)
+        message =
+          HYNLS_ERROR_MESSAGE (HYNLS_PORT_NLS_FAILURE, "NLS Failure\n");
+    }
+
+  hythread_monitor_exit (nls->monitor);
+  return message;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_release_message
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_set_catalog
+/**
+ * Setup the path to the NLS catalog. 
+ *
+ * @param[in] portLibrary The port library
+ * @param[in] paths - an array of directory paths where the NLS catalog may be found
+ * @param[in] nPaths - the number of entries in the @ref paths array
+ * @param[in] baseName - the lead name of the catalog file name (i.e., the "java" in java_en_US.properties)
+ * @param[in] extension - the extension of the catalog file name (i.e., the "properties in java_en_US.properties)
+ */
+void VMCALL
+hynls_set_catalog (struct HyPortLibrary *portLibrary, const char **paths,
+                   const int nPaths, const char *baseName,
+                   const char *extension)
+{
+
+  int i;
+  char *p;
+
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - hynls_set_catalog\n");
+#endif
+
+
+  hythread_monitor_enter (nls->monitor);
+
+  if (!baseName || !extension)
+    goto clean_exit;
+  for (i = 0; i < nPaths; i++)
+    {
+      if (nls->baseCatalogPaths[i])
+        portLibrary->mem_free_memory (portLibrary, nls->baseCatalogPaths[i]);
+      nls->baseCatalogPaths[i] = NULL;
+    }
+  nls->nPaths = 0;
+  if (nls->baseCatalogName)
+    {
+      portLibrary->mem_free_memory (portLibrary, nls->baseCatalogName);
+      nls->baseCatalogName = NULL;
+    }
+  if (nls->baseCatalogExtension)
+    {
+      portLibrary->mem_free_memory (portLibrary, nls->baseCatalogExtension);
+      nls->baseCatalogExtension = NULL;
+    }
+
+  for (i = 0; i < nPaths; i++)
+    {
+      nls->baseCatalogPaths[i] =
+        portLibrary->mem_allocate_memory (portLibrary, strlen (paths[i]) + 1);
+      if (nls->baseCatalogPaths[i])
+        {
+          strcpy (nls->baseCatalogPaths[i], paths[i]);
+          p = strrchr (nls->baseCatalogPaths[i], DIR_SEPARATOR);
+          if (p)
+            p[1] = '\0';
+          nls->nPaths++;
+        }
+    }
+
+  nls->baseCatalogName =
+    portLibrary->mem_allocate_memory (portLibrary, strlen (baseName) + 1);
+  if (nls->baseCatalogName)
+    strcpy (nls->baseCatalogName, baseName);
+
+  nls->baseCatalogExtension =
+    portLibrary->mem_allocate_memory (portLibrary, strlen (extension) + 1);
+  if (nls->baseCatalogExtension)
+    strcpy (nls->baseCatalogExtension, extension);
+
+  if (nls->language[0] == 0 && nls->region[0] == 0 && nls->variant[0] == 0)
+    {
+      nls_determine_locale (portLibrary);
+    }
+
+clean_exit:
+  hythread_monitor_exit (nls->monitor);
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION build_catalog_name
+
+static char *
+build_catalog_name (struct HyPortLibrary *portLibrary, I_32 usePath,
+                    I_32 useDepth)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  I_32 len = 1;
+  char *catalog = NULL;
+  char *defaultCatalog = NULL;
+  char *endPathPtr = NULL;
+
+  hysysinfo_get_executable_name (portLibrary, NULL, &defaultCatalog);
+  endPathPtr = strrchr (defaultCatalog, DIR_SEPARATOR);
+  endPathPtr[1] = '\0';
+  // defaultCatalog now holds the name of the launcher's home directory
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - build_catalog_name\n");
+#endif
+
+
+  if (!nls->nPaths)
+    {
+      portLibrary->nls_set_catalog (portLibrary,
+                                    (const char **) &defaultCatalog, 1,
+                                    "harmony", "properties");  
+      if (!nls->baseCatalogName)
+        goto _done;
+      if (nls->language[0] == 0 && nls->region[0] == 0
+          && nls->variant[0] == 0)
+        {
+          nls_determine_locale (portLibrary);
+        }
+    }
+
+  if (useDepth > 0)
+    {
+      if (nls->language[0] == 0)
+        goto _done;
+      if (useDepth > 1)
+        {
+          if (nls->region[0] == 0)
+            goto _done;
+          if (useDepth > 2)
+            {
+              if (nls->variant[0] == 0)
+                goto _done;
+            }
+        }
+    }
+
+  len += strlen (nls->baseCatalogPaths[usePath]);
+  len += strlen (nls->baseCatalogName);
+  len += strlen (nls->baseCatalogExtension);
+  len += 1;                     /* the . before the extension */
+  len += strlen (nls->language) + 1;    /* '_en' */
+  len += strlen (nls->region) + 1;
+  len += strlen (nls->variant) + 1;
+  len += 1;                     /* null terminator */
+
+  catalog = portLibrary->mem_allocate_memory (portLibrary, len);
+  if (!catalog)
+    goto _done;
+  strcpy (catalog, nls->baseCatalogPaths[usePath]);
+  strcat (catalog, nls->baseCatalogName);
+  if (useDepth > 0)
+    {
+      strcat (catalog, "_");
+      strcat (catalog, nls->language);
+      if (useDepth > 1)
+        {
+          strcat (catalog, "_");
+          strcat (catalog, nls->region);
+          if (useDepth > 2)
+            {
+              strcat (catalog, "_");
+              strcat (catalog, nls->variant);
+            }
+        }
+    }
+  strcat (catalog, ".");
+  strcat (catalog, nls->baseCatalogExtension);
+
+_done:
+  // Free memory for defaultCatalog -- necessary ??
+  if (defaultCatalog)
+    {
+      portLibrary->mem_free_memory (portLibrary, defaultCatalog);
+    }
+
+  return catalog;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION open_catalog
+
+static void
+open_catalog (struct HyPortLibrary *portLibrary)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  char *catalog = NULL;
+  I_32 fd = -1, d, p;
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - open_catalog\n");
+  portLibrary->tty_printf (portLibrary, "nPaths = %d\n", (I_32) nls->nPaths);
+#endif
+
+
+  /* try the following way to look for the catalog:
+   * The base name will typically be "<path>/java.properties". Append as many locale descriptors to the file name as possible to find a file. 
+   * e.g. 
+   *      java_en_US_Texas.properties
+   *      java_en_US.properties
+   *      java_en.properties
+   *      java.properties
+   */
+
+  for (p = 0; p < (I_32) nls->nPaths; p++)
+    {
+      for (d = 3; d >= 0; d--)
+        {
+          if (catalog)
+            portLibrary->mem_free_memory (portLibrary, catalog);
+          catalog = build_catalog_name (portLibrary, p, d);
+          if (!catalog)
+            continue;
+#if defined(NLS_DEBUG)
+          portLibrary->tty_printf (portLibrary,
+                                   "NLS - attempting to open: %s\n", catalog);
+#endif
+
+
+          fd = portLibrary->file_open (portLibrary, catalog, HyOpenRead, 0);
+          if (fd != -1)
+            break;
+        }
+      if (fd != -1)
+        break;
+    }
+
+  if (fd == -1)
+    {
+#if defined(NLS_DEBUG)
+      portLibrary->tty_printf (portLibrary,
+                               "NLS - failed to open the nls catalog\n");
+#endif
+
+
+      return;
+    }
+
+  portLibrary->portGlobals->nls_data.catalog = catalog;
+
+  portLibrary->file_close (portLibrary, fd);
+
+#if defined(NLS_DEBUG)
+  portLibrary->tty_printf (portLibrary, "NLS - succesfully opened %s\n",
+                           catalog);
+#endif
+
+
+  free_catalog (portLibrary);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION parse_catalog
+static const char *
+parse_catalog (struct HyPortLibrary *portLibrary, UDATA flags,
+               U_32 module_name, U_32 message_num, const char *default_string)
+{
+
+#define MSG_NONE 0
+#define MSG_SLASH 1
+#define MSG_UNICODE 2
+#define MSG_CONTINUE 3
+#define MSG_DONE 4
+#define MSG_IGNORE 5
+
+  U_8 dataBuf[BUF_SIZE];
+  U_8 *charPointer = NULL;
+  U_8 *endPointer = NULL;
+  int mode = MSG_NONE, count, digit;
+  U_32 unicode = 0;
+  char nextChar;
+  U_32 offset = 0, bufSize = BUF_SIZE, maxOffset = 0;
+  I_32 keyLength = -1;
+  char *buf, *newBuf;
+  BOOLEAN firstChar = TRUE;
+  IDATA fd = -1;
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  HyNLSHashEntry *entry = NULL;
+  char *message = NULL;
+  char convertedModuleEnum[5];
+  /* calculate a size which is larger than we could possibly need by putting together all of the prefixes and suffixes */
+  char
+    prefix[sizeof
+           (HYNLS_ERROR_PREFIX "" HYNLS_INFO_PREFIX "" HYNLS_WARNING_PREFIX ""
+            HYNLS_COMMON_PREFIX "" HYNLS_EXEMPLAR "" HYNLS_ERROR_SUFFIX ""
+            HYNLS_INFO_SUFFIX "" HYNLS_WARNING_SUFFIX " \n")];
+  char *searchKey;
+  BOOLEAN newline = !(flags & HYNLS_DO_NOT_APPEND_NEWLINE);
+  const char *format;
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - parse_catalog\n");
+#endif
+
+
+  convertModuleName (module_name, (U_8 *) convertedModuleEnum);
+
+  format = "" HYNLS_COMMON_PREFIX "%s%03u %s";
+
+  if (0 == (flags & HYNLS_DO_NOT_PRINT_MESSAGE_TAG))
+    {
+      if (flags & HYNLS_ERROR)
+        {
+          format =
+            "" HYNLS_ERROR_PREFIX "" HYNLS_COMMON_PREFIX "%s%03u"
+            HYNLS_ERROR_SUFFIX " %s";
+        }
+      else if (flags & HYNLS_WARNING)
+        {
+          format =
+            "" HYNLS_WARNING_PREFIX "" HYNLS_COMMON_PREFIX "%s%03u"
+            HYNLS_WARNING_SUFFIX " %s";
+        }
+      else if (flags & HYNLS_INFO)
+        {
+          format =
+            "" HYNLS_INFO_PREFIX "" HYNLS_COMMON_PREFIX "%s%03u"
+            HYNLS_INFO_SUFFIX " %s";
+        }
+    }
+  portLibrary->str_printf (portLibrary, prefix, sizeof (prefix), format,
+                           convertedModuleEnum, message_num,
+                           newline ? "\n" : "");
+
+  /* make sure the searchKey string starts at HYVM001 instead of (E)HYVM001 */
+  searchKey = prefix + (strchr (format, '%') - format);
+
+#if defined(NLS_DEBUG)
+  portLibrary->tty_printf (portLibrary,
+                           "NLS - parse_catalog - searchKey: %s\n",
+                           searchKey);
+#endif
+
+
+  /* we do a lazy caching, populate the cache as we look up messages */
+
+  if (nls->catalog)
+    {
+      fd =
+        portLibrary->file_open (portLibrary, (char *) nls->catalog,
+                                HyOpenRead, 0);
+    }
+  if (fd == -1)
+    {
+      /* couldn't open the file, store the searchKey instead */
+      char *tmpStr = prefix;
+      if (default_string)
+        {
+          tmpStr = (char *) default_string;
+        }
+      entry =
+        nls_allocateHashEntry (portLibrary, module_name, message_num, tmpStr,
+                               strlen (tmpStr));
+      if (!entry)
+        {
+          return default_string;
+        }
+      nlsh_insert (portLibrary, entry);
+      return entry->message;
+    }
+
+  if (!(buf = portLibrary->mem_allocate_memory (portLibrary, bufSize)))
+    {
+      goto finished;
+    }
+
+  while (read_from_catalog (portLibrary, fd, (char *) dataBuf, BUF_SIZE) !=
+         NULL)
+    {
+      charPointer = dataBuf;
+      endPointer = charPointer + strlen ((char *) dataBuf);
+
+      while (charPointer < endPointer)
+        {
+          nextChar = *charPointer++;
+
+          if (offset + 2 >= bufSize)
+            {
+              bufSize <<= 1;
+              if (!
+                  (newBuf =
+                   portLibrary->mem_allocate_memory (portLibrary, bufSize)))
+                goto finished;
+              memcpy (newBuf, buf, offset);
+              portLibrary->mem_free_memory (portLibrary, buf);
+              buf = newBuf;
+            }
+
+          if (mode == MSG_UNICODE)
+            {
+              if (nextChar >= '0' && nextChar <= '9')
+                digit = nextChar - '0';
+              else if (nextChar >= 'a' && nextChar <= 'f')
+                digit = (nextChar - 'a') + 10;
+              else if (nextChar >= 'A' && nextChar <= 'F')
+                digit = (nextChar - 'A') + 10;
+              else
+                digit = -1;
+              if (digit >= 0)
+                {
+                  unicode = (unicode << 4) + digit;
+                  if (++count < 4)
+                    continue;
+                }
+              mode = MSG_NONE;
+              if (unicode >= 0x01 && unicode <= 0x7f)
+                {
+                  buf[offset++] = unicode;
+                }
+              else if (unicode == 0 || (unicode >= 0x80 && unicode <= 0x7ff))
+                {
+                  buf[offset++] = ((unicode >> 6) & 0x1f) | 0xc0;
+                  buf[offset++] = (unicode & 0x3f) | 0x80;
+                }
+              else if (unicode >= 0x800 && unicode <= 0xffff)
+                {
+                  buf[offset++] = ((unicode >> 12) & 0x0f) | 0xe0;
+                  buf[offset++] = ((unicode >> 6) & 0x3f) | 0x80;
+                  buf[offset++] = (unicode & 0x3f) | 0x80;
+                }
+              if (nextChar != '\n')
+                continue;
+            }
+
+          if (mode == MSG_SLASH)
+            {
+              mode = MSG_NONE;
+              switch (nextChar)
+                {
+                case '\r':
+                  mode = MSG_CONTINUE;  /* Look for a following 'n */
+                  continue;
+                case '\n':
+                  mode = MSG_IGNORE;    /* Ignore whitespace on the next line */
+                  continue;
+                case 'b':
+                  nextChar = '\b';
+                  break;
+                case 'f':
+                  nextChar = '\f';
+                  break;
+                case 'n':
+                  nextChar = '\n';
+                  break;
+                case 'r':
+                  nextChar = '\r';
+                  break;
+                case 't':
+                  nextChar = '\t';
+                  break;
+                case 'u':
+                  mode = MSG_UNICODE;
+                  unicode = count = 0;
+                  continue;
+                }
+            }
+          else
+            {
+              switch (nextChar)
+                {
+                case '#':
+                case '!':
+                  if (firstChar)
+                    {
+
+                      while (1)
+                        {
+                          if (charPointer >= endPointer)
+                            {
+                              if (read_from_catalog
+                                  (portLibrary, fd, (char *) dataBuf,
+                                   BUF_SIZE) != NULL)
+                                {
+                                  charPointer = dataBuf;
+                                  endPointer =
+                                    charPointer + strlen (charPointer);
+                                }
+                            }
+                          if (charPointer >= endPointer)
+                            break;
+                          nextChar = *charPointer++;
+                          if (nextChar == '\r' || nextChar == '\n')
+                            break;
+                        }
+                      continue;
+                    }
+                  break;
+                case '\n':
+                  if (mode == MSG_CONTINUE)
+                    {           /* Part of a \r\n sequence */
+                      mode = MSG_IGNORE;
+                      continue;
+                    }
+                  /* fall into next case */
+                case '\r':
+                  mode = MSG_NONE;
+                  firstChar = TRUE;
+                makeStrings:
+                  if (keyLength >= 0)
+                    {
+#if defined(NLS_DEBUG)
+                      portLibrary->tty_printf (portLibrary,
+                                               "NLS - parse_catalog - keyLength: %d -- buf: %20.20s\n",
+                                               keyLength, buf);
+#endif
+
+
+                      if (strncmp
+                          (searchKey, buf, sizeof (HYNLS_EXEMPLAR) - 1) == 0)
+                        {
+#if defined(NLS_DEBUG)
+                          portLibrary->tty_printf (portLibrary,
+                                                   "NLS - parse_catalog - key match\n");
+#endif
+
+
+                          /* we have the exact message */
+                          if (flags & HYNLS_DO_NOT_PRINT_MESSAGE_TAG)
+                            {
+                              entry =
+                                nls_allocateHashEntry (portLibrary,
+                                                       module_name,
+                                                       message_num,
+                                                       buf + keyLength,
+                                                       offset - keyLength +
+                                                       1);
+                              if (entry)
+                                {
+                                  entry->message[offset - keyLength] = '\0';
+                                  if (newline)
+                                    strcat (entry->message, "\n");
+                                }
+                            }
+                          else
+                            {
+                              entry =
+                                nls_allocateHashEntry (portLibrary,
+                                                       module_name,
+                                                       message_num, prefix,
+                                                       offset - keyLength +
+                                                       strlen (prefix));
+                              if (entry)
+                                {
+                                  /* null terminate and trim the \n if required */
+                                  entry->message[strlen (prefix) -
+                                                 (newline ? 1 : 0)] = '\0';
+                                  strncat (entry->message, buf + keyLength,
+                                           offset - keyLength);
+                                  if (newline)
+                                    strcat (entry->message, "\n");
+                                }
+                            }
+                          goto finished;
+                        }
+                      keyLength = -1;
+                    }
+                  if (charPointer >= endPointer)
+                    {
+                      if (read_from_catalog
+                          (portLibrary, fd, (char *) dataBuf,
+                           BUF_SIZE) != NULL)
+                        {
+                          charPointer = dataBuf;
+                          endPointer = charPointer + strlen (charPointer);
+                        }
+                    }
+                  if (charPointer >= endPointer)
+                    {
+                    finished:
+                      if (buf)
+                        portLibrary->mem_free_memory (portLibrary, buf);
+#if defined(NLS_DEBUG)
+                      portLibrary->tty_printf (portLibrary,
+                                               "NLS - parse_catalog - inserting message\n");
+#endif
+
+
+                      if (!entry)
+                        {
+                          char *tmpStr = prefix;
+                          if (default_string)
+                            tmpStr = (char *) default_string;
+                          entry =
+                            nls_allocateHashEntry (portLibrary, module_name,
+                                                   message_num, tmpStr,
+                                                   strlen (tmpStr));
+                          if (!entry)
+                            {
+                              portLibrary->file_close (portLibrary, fd);
+                              return default_string;
+                            }
+                        }
+                      nlsh_insert (portLibrary, entry);
+                      portLibrary->file_close (portLibrary, fd);
+                      return entry->message;
+                    }
+                  if (offset > maxOffset)
+                    maxOffset = offset;
+                  offset = 0;
+                  continue;
+                case '\\':
+                  mode = MSG_SLASH;
+                  continue;
+                case ':':
+                case '=':
+                  if (keyLength == -1)
+                    {           /* if parsing the key */
+                      keyLength = offset;
+                      continue;
+                    }
+                  break;
+                }
+              if ((nextChar >= 0x1c && nextChar <= ' ')
+                  || (nextChar >= 9 && nextChar <= 0xd))
+                {
+                  if (mode == MSG_CONTINUE)
+                    mode = MSG_IGNORE;
+                  /* if key length == 0 or value length == 0 */
+                  if (offset == 0 || offset == (U_32) keyLength
+                      || mode == MSG_IGNORE)
+                    continue;
+                  if (keyLength == -1)
+                    {           /* if parsing the key */
+                      mode = MSG_DONE;
+                      continue;
+                    }
+                }
+              if (mode == MSG_IGNORE || mode == MSG_CONTINUE)
+                mode = MSG_NONE;
+            }
+          firstChar = FALSE;
+          if (mode == MSG_DONE)
+            {
+              keyLength = offset;
+              mode = MSG_NONE;
+            }
+          buf[offset++] = nextChar;
+        }
+    }
+  goto makeStrings;
+
+#undef MSG_NONE
+#undef MSG_SLASH
+#undef MSG_UNICODE
+#undef MSG_CONTINUE
+#undef MSG_DONE
+#undef MSG_IGNORE
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION free_catalog
+
+static void
+free_catalog (struct HyPortLibrary *portLibrary)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  HyNLSHashEntry *old_hashEntries = nls->old_hashEntries;
+  U_32 i;
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - free_catalog\n");
+#endif
+
+
+  for (i = 0; i < HYNLS_NUM_HASH_BUCKETS; i++)
+    {
+      HyNLSHashEntry *entry = nls->hash_buckets[i];
+      if (entry)
+        {
+          while (entry->next)
+            {
+              entry = entry->next;
+            }
+          entry->next = nls->old_hashEntries;
+          nls->old_hashEntries = nls->hash_buckets[i];
+          nls->hash_buckets[i] = NULL;
+        }
+    }
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION nlsh_hash
+static U_32
+nlsh_hash (U_32 module_name, U_32 message_num)
+{
+  return (module_name ^ message_num);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION nlsh_lookup
+static const char *
+nlsh_lookup (struct HyPortLibrary *portLibrary, U_32 module_name,
+             U_32 message_num)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  U_32 hashKey = nlsh_hash (module_name, message_num);
+  U_32 index;
+  HyNLSHashEntry *entry;
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - nlsh_lookup\n");
+#endif
+
+
+  index = hashKey % HYNLS_NUM_HASH_BUCKETS;
+  entry = nls->hash_buckets[index];
+
+  while (entry)
+    {
+      if (entry->module_name == module_name
+          && entry->message_num == message_num)
+        {
+          return entry->message;
+        }
+      entry = entry->next;
+    }
+
+  return NULL;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION nlsh_insert
+static void
+nlsh_insert (struct HyPortLibrary *portLibrary, HyNLSHashEntry * entry)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  U_32 hashKey = nlsh_hash (entry->module_name, entry->message_num);
+  U_32 index;
+
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - nlsh_insert\n");
+#endif
+
+
+  index = hashKey % HYNLS_NUM_HASH_BUCKETS;
+  entry->next = nls->hash_buckets[index];
+  nls->hash_buckets[index] = entry;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_startup
+/**
+ * PortLibrary startup.
+ * 
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the NLS library operations may be created here.  All resources created here should be destroyed
+ * in @ref hynls_shutdown.
+ *
+ * @param[in] portLibrary The port library
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_NLS
+ */
+I_32 VMCALL
+hynls_startup (struct HyPortLibrary *portLibrary)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+
+  if (0 !=
+      hythread_monitor_init_with_name (&nls->monitor, 0, "NLS hash table"))
+    {
+      return (I_32) HYPORT_ERROR_STARTUP_NLS;
+    }
+
+  nls_determine_locale (portLibrary);
+
+  return (I_32) 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hynls_shutdown
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by 
+ * @ref hynls_startup should be destroyed here.
+ *
+ * @param[in] portLibrary The port library
+ *
+ */
+void VMCALL
+hynls_shutdown (struct HyPortLibrary *portLibrary)
+{
+  HyNLSDataCache *nls = &portLibrary->portGlobals->nls_data;
+  HyNLSHashEntry *entry;
+  U_32 i;
+
+  for (i = 0; i < HYNLS_NUM_HASH_BUCKETS; i++)
+    {
+      entry = nls->hash_buckets[i];
+      while (entry)
+        {
+          HyNLSHashEntry *next = entry->next;
+          portLibrary->mem_free_memory (portLibrary, entry);
+          entry = next;
+        }
+    }
+
+  /* Free the baseCatalogPaths allocated in hynls_set_catalog */
+  for (i = 0; i < nls->nPaths; i++)
+    {
+      if (nls->baseCatalogPaths[i])
+        {
+          portLibrary->mem_free_memory (portLibrary,
+                                        nls->baseCatalogPaths[i]);
+          nls->baseCatalogPaths[i] = NULL;
+        }
+    }
+
+  if (nls->baseCatalogExtension)
+    {
+      portLibrary->mem_free_memory (portLibrary, nls->baseCatalogExtension);
+      nls->baseCatalogExtension = NULL;
+    }
+
+/* catalog is never free'd without this flag */
+  entry = nls->old_hashEntries;
+  while (entry)
+    {
+      HyNLSHashEntry *next = entry->next;
+      portLibrary->mem_free_memory (portLibrary, entry);
+      entry = next;
+    }
+
+  if (nls->baseCatalogName)
+    portLibrary->mem_free_memory (portLibrary, nls->baseCatalogName);
+  if (nls->catalog)
+    portLibrary->mem_free_memory (portLibrary, nls->catalog);
+
+  hythread_monitor_destroy (nls->monitor);
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION nls_allocateHashEntry
+static HyNLSHashEntry *
+nls_allocateHashEntry (struct HyPortLibrary *portLibrary, U_32 module_name,
+                       U_32 message_num, const char *message,
+                       U_32 sizeOfMessage)
+{
+  HyNLSHashEntry *entry = portLibrary->mem_allocate_memory (portLibrary,
+                                                            sizeof
+                                                            (HyNLSHashEntry) +
+                                                            sizeOfMessage +
+                                                            1 -
+                                                            sizeof (entry->
+                                                                    message));
+#if defined(NLS_DEBUG_TRACE)
+  portLibrary->tty_printf (portLibrary, "NLS - nls_allocateHashEntry\n");
+#endif
+
+
+  if (!entry)
+    return NULL;
+  entry->module_name = module_name;
+  entry->message_num = message_num;
+  entry->next = NULL;
+  memcpy (entry->message, message, sizeOfMessage);
+  entry->message[sizeOfMessage] = '\0';
+#if defined(NLS_DEBUG)
+  portLibrary->tty_printf (portLibrary,
+                           "NLS - nls_allocateHashEntry - message: %s - sizeOfMessage: %d\n",
+                           entry->message, sizeOfMessage);
+#endif
+
+
+  return entry;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION convertModuleName
+void
+convertModuleName (U_32 module_name, U_8 * module_str)
+{
+  module_str[0] = (U_8) ((module_name >> 24) & 0xff);
+  module_str[1] = (U_8) ((module_name >> 16) & 0xff);
+  module_str[2] = (U_8) ((module_name >> 8) & 0xff);
+  module_str[3] = (U_8) ((module_name) & 0xff);
+  module_str[4] = 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION read_from_catalog
+
+char *
+read_from_catalog (struct HyPortLibrary *portLibrary, IDATA fd, char *buf,
+                   IDATA bufsize)
+{
+  char temp[BUF_SIZE];
+  IDATA count, nbytes = bufsize;
+  char *cursor = buf;
+
+  if (nbytes <= 0)
+    return 0;
+
+  /* discount 1 for the trailing NUL */
+  nbytes -= 1;
+
+  while (nbytes)
+    {
+      count = BUF_SIZE > nbytes ? nbytes : BUF_SIZE;
+      count = portLibrary->file_read (portLibrary, fd, temp, count);
+
+      if (count < 0)
+        {
+
+          /* if we've made it through a successful read, return the buf. */
+          if (nbytes + 1 != bufsize)
+            return buf;
+          return NULL;
+        }
+
+      memcpy (cursor, temp, count);
+      cursor += count;
+      nbytes -= count;
+
+    }
+
+  *cursor = '\0';
+
+  return buf;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION
+
+#undef CDEV_CURRENT_FUNCTION



Mime
View raw message