⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
yy_size_t size;
#endif
	{
	YY_BUFFER_STATE b;

	if ( size < 2 ||
	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
	     base[size-1] != YY_END_OF_BUFFER_CHAR )
		/* They forgot to leave room for the EOB's. */
		return 0;

	b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) );
	if ( ! b )
		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );

	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
	b->yy_buf_pos = b->yy_ch_buf = base;
	b->yy_is_our_buffer = 0;
	b->yy_input_file = 0;
	b->yy_n_chars = b->yy_buf_size;
	b->yy_is_interactive = 0;
	b->yy_at_bol = 1;
	b->yy_fill_buffer = 0;
	b->yy_buffer_status = YY_BUFFER_NEW;

	yy_switch_to_buffer( b );

	return b;
	}
#endif


#ifndef YY_NO_SCAN_STRING
#ifdef YY_USE_PROTOS
YY_BUFFER_STATE yy_scan_string( yyconst char *yy_str )
#else
YY_BUFFER_STATE yy_scan_string( yy_str )
yyconst char *yy_str;
#endif
	{
	int len;
	for ( len = 0; yy_str[len]; ++len )
		;

	return yy_scan_bytes( yy_str, len );
	}
#endif


#ifndef YY_NO_SCAN_BYTES
#ifdef YY_USE_PROTOS
YY_BUFFER_STATE yy_scan_bytes( yyconst char *bytes, int len )
#else
YY_BUFFER_STATE yy_scan_bytes( bytes, len )
yyconst char *bytes;
int len;
#endif
	{
	YY_BUFFER_STATE b;
	char *buf;
	yy_size_t n;
	int i;

	/* Get memory for full buffer, including space for trailing EOB's. */
	n = len + 2;
	buf = (char *) yy_flex_alloc( n );
	if ( ! buf )
		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );

	for ( i = 0; i < len; ++i )
		buf[i] = bytes[i];

	buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR;

	b = yy_scan_buffer( buf, n );
	if ( ! b )
		YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );

	/* It's okay to grow etc. this buffer, and we should throw it
	 * away when we're done.
	 */
	b->yy_is_our_buffer = 1;

	return b;
	}
#endif


#ifndef YY_NO_PUSH_STATE
#ifdef YY_USE_PROTOS
static void yy_push_state( int new_state )
#else
static void yy_push_state( new_state )
int new_state;
#endif
	{
	if ( yy_start_stack_ptr >= yy_start_stack_depth )
		{
		yy_size_t new_size;

		yy_start_stack_depth += YY_START_STACK_INCR;
		new_size = yy_start_stack_depth * sizeof( int );

		if ( ! yy_start_stack )
			yy_start_stack = (int *) yy_flex_alloc( new_size );

		else
			yy_start_stack = (int *) yy_flex_realloc(
					(void *) yy_start_stack, new_size );

		if ( ! yy_start_stack )
			YY_FATAL_ERROR(
			"out of memory expanding start-condition stack" );
		}

	yy_start_stack[yy_start_stack_ptr++] = YY_START;

	BEGIN(new_state);
	}
#endif


#ifndef YY_NO_POP_STATE
static void yy_pop_state()
	{
	if ( --yy_start_stack_ptr < 0 )
		YY_FATAL_ERROR( "start-condition stack underflow" );

	BEGIN(yy_start_stack[yy_start_stack_ptr]);
	}
#endif


#ifndef YY_NO_TOP_STATE
static int yy_top_state()
	{
	return yy_start_stack[yy_start_stack_ptr - 1];
	}
#endif

#ifndef YY_EXIT_FAILURE
#define YY_EXIT_FAILURE 2
#endif

#ifdef YY_USE_PROTOS
static void yy_fatal_error( yyconst char msg[] )
#else
static void yy_fatal_error( msg )
char msg[];
#endif
	{
	(void) fprintf( stderr, "%s\n", msg );
	exit( YY_EXIT_FAILURE );
	}



/* Redefine yyless() so it works in section 3 code. */

#undef yyless
#define yyless(n) \
	do \
		{ \
		/* Undo effects of setting up yytext. */ \
		yytext[yyleng] = yy_hold_char; \
		yy_c_buf_p = yytext + n; \
		yy_hold_char = *yy_c_buf_p; \
		*yy_c_buf_p = '\0'; \
		yyleng = n; \
		} \
	while ( 0 )


/* Internal utility routines. */

#ifndef yytext_ptr
#ifdef YY_USE_PROTOS
static void yy_flex_strncpy( char *s1, yyconst char *s2, int n )
#else
static void yy_flex_strncpy( s1, s2, n )
char *s1;
yyconst char *s2;
int n;
#endif
	{
	register int i;
	for ( i = 0; i < n; ++i )
		s1[i] = s2[i];
	}
#endif

#ifdef YY_NEED_STRLEN
#ifdef YY_USE_PROTOS
static int yy_flex_strlen( yyconst char *s )
#else
static int yy_flex_strlen( s )
yyconst char *s;
#endif
	{
	register int n;
	for ( n = 0; s[n]; ++n )
		;

	return n;
	}
#endif


#ifdef YY_USE_PROTOS
static void *yy_flex_alloc( yy_size_t size )
#else
static void *yy_flex_alloc( size )
yy_size_t size;
#endif
	{
	return (void *) malloc( size );
	}

#ifdef YY_USE_PROTOS
static void *yy_flex_realloc( void *ptr, yy_size_t size )
#else
static void *yy_flex_realloc( ptr, size )
void *ptr;
yy_size_t size;
#endif
	{
	/* The cast to (char *) in the following accommodates both
	 * implementations that use char* generic pointers, and those
	 * that use void* generic pointers.  It works with the latter
	 * because both ANSI C and C++ allow castless assignment from
	 * any pointer type to void*, and deal with argument conversions
	 * as though doing an assignment.
	 */
	return (void *) realloc( (char *) ptr, size );
	}

#ifdef YY_USE_PROTOS
static void yy_flex_free( void *ptr )
#else
static void yy_flex_free( ptr )
void *ptr;
#endif
	{
	free( ptr );
	}

#if YY_MAIN
int main()
	{
	yylex();
	return 0;
	}
#endif
#line 244 "language.l"


/*---------------------------------------------------------------------*/

/*
 *  terminate yylex() when EOF reached
 */
int yywrap (void)
{
  return (1);
}

/*
 *  Add a character to output buffer
 */
static void AddChar (int ch)
{
  if (ptr)
  {
    DEBUG ("%c", ch);
    *ptr++ = ch;
    if (ptr >= end)
       error ("string too large");
  }
}

/*
 *  Add a decimal code to output buffer
 */
static void AddDecimal (const char *buf)
{
  if (ptr)
  {
    int dec;
    sscanf (buf, "%d", &dec);
    DEBUG ("dec <%d>", dec);
    *ptr++ = dec;
    if (dec > 255)
       Abort ("Illegal decimal", NULL);
    if (ptr >= end)
       error ("string too large");
  }
}

/*
 *  Add a hex code to output buffer
 */
static void AddHexcode (const char *buf)
{
  if (ptr)
  {
    WORD hex;
    sscanf (buf, "%hx", &hex);
    DEBUG ("hex <%X>", hex);
    *ptr++ = hex;
    if (ptr >= end)
       error ("string too large");
  }
}

/*
 *  Add an octal code to output buffer
 */
static void AddOctal (const char *buf)
{
  if (ptr)
  {
    unsigned oct;
    sscanf (buf, "%o", &oct);
    DEBUG ("oct <%o>", oct);
    *ptr++ = oct;
    if (ptr >= end)
       error ("string too large");
  }
}

/*
 *  Add an language entry for translation
 */
static void AddEntry (void)
{
  char        *str;
  struct Node *list = NULL;

  if ((str = strdup(buf)) == NULL ||
     (list = malloc(sizeof(*list))) == NULL)
  {
    outsnl ("Language file too big!");
    longjmp (bail_out, 1);
  }

  list->next   = lang.list;
  list->crc    = crc_ref;
  list->string = str;
  lang.list    = list;
  lang.entries++;
  do_ref = 1;
}

/*
 * Comparision routine needed by quick-sort and bsearch routines.
 * Don't use unsigned arithmetic
 */
static int Compare (const struct Elem *a, const struct Elem *b)
{
  if (a->crc > b->crc) return (1);
  if (a->crc < b->crc) return (-1);
  return (0);
}

/*
 * Sort the linked list in accending order of CRC value
 */
static int SortLanguageEntries (DWORD num)
{ 
  struct Node *lng, *next;
  struct Elem *array = malloc ((size_t)(num * sizeof(*array)));

  lang.array = array;
  if (!array)
     return (0);

  for (lng = lang.list; lng; lng = next, array++)
  {
    array->crc    = lng->crc;
    array->string = lng->string;
    next = lng->next;
    free (lng);
  }
  lang.list = NULL;
  qsort (lang.array, (size_t)num, (size_t)sizeof(*array), (int(*)())Compare);
  return (1);
}

/*
 *  Initialize the linked list for language.
 *
 *  The wattcp.cfg entry has format LANGUAGE = lang,language-file
 *  e.g. "LANGUAGE = de,$(ETC)\language.txt"
 *
 *  Country-code MUST be two characters (see ISO639 list above)
 *
 *  May be called multiple times to extend language entries from
 *  more than 1 file.
 */
void lang_init (const char *value)
{
  FILE *langf;
  char *s, *fname;

  if (!value)
     return;

  s = strchr (value, ',');
  if (!s || (s - value) != 2)
     Abort ("Illegal LANGUAGE syntax", NULL);

  lang.line = 1;
  lang.prefix[0] = value[0];
  lang.prefix[1] = value[1];
  lang.prefix[2] = ':';
  lang.prefix[3] = 0;
  strlwr (lang.prefix);

  if (!strnicmp(lang.prefix,"en",2))  /* english is default language */
     return;

  if (!watt_crc_init())
  {
    outsnl ("CRC init failed");
    return;
  }

  fname = ++s;
  langf = fopen (fname, "rb");
  if (!langf)
  {
    outs ("Cannot open language file ");
    outsnl (fname);
    return;
  }

  if (!setjmp(bail_out))
  {
    yyin = langf;
    yylex();
    fclose (langf);

    if (!SortLanguageEntries(lang.entries))
    {
      outsnl ("Could not sort LANGUAGE entries");
      lang.entries = 0;
    }
#if defined(TEST_PROG)
    printf ("file-name    = `%s'\n",   fname);
    printf ("lang.prefix  = `%s'\n",   lang.prefix);
    printf ("lang.entries = %lu\n",    lang.entries);
    printf ("lang.lines   = %lu\n",    lang.line);
    printf ("lang.list    = %08lX\n",  (DWORD)lang.list);
    printf ("lang.array   = %08lX\n\n",(DWORD)lang.array);
    {
      struct Elem *elem;
      int    i;
      for (i = 0, elem = lang.array; i < lang.entries; i++, elem++)
          printf ("CRC = %08lX, <%s>\n", elem->crc, elem->string);
    }
#endif
  }
}

/*
 * return foreign translation of string str
 */
const char *_LANG (const char *str)
{
  struct Elem *elem;
  DWORD  hash;
  int    len;

  if (!lang.entries || !str)
     return (str);

  len = strlen (str);
  if (len == 0)
     return (str);

  hash = watt_crc_bytes (str, len);

  elem = (struct Elem*) bsearch (&hash, lang.array, (size_t)lang.entries,
                                 (size_t)sizeof(*elem), (int(*)())Compare);

  if (!elem || !elem->string || !elem->string[0])
     return (str);

  return (const char*)elem->string;
}

/*
 * Print message and abort program
 */
static void Abort (const char *s1, const char *s2)
{
  if (lang.line > 0)
  {
#ifdef __DJGPP__
    (*_printf) ("\r\nline %lu: ", lang.line);
#else
    char buf [15];
    ltoa (lang.line, buf, 10);
    outs ("\r\nline ");
    outs (buf);
    outs (": ");
#endif
  }
  else
    outs ("\r\n");

  outs (s1);
  if (s2)
     outsnl (s2);
  outs ("\r\n");
  exit (1);
}

/*
 * Print error message for current line
 */
static void error (const char *err)
{
#ifdef __DJGPP__
  (*_printf) ("%s at line %lu\r\n", err, lang.line);
#else
  char buf [15];
  ltoa (lang.line, buf, 10);
  outs (err);
  outs (" at line ");
  outs (buf);
  outs ("\r\n");
#endif
}

#if defined(TEST_PROG)
int main (void)
{
  puts ("You must define a non-english language to parse language-file.\n"
        "Use e.g: \"language = no,$(ETC)\\watlang.txt\" in WATTCP.CFG\n"
        "Press any key to initialise Watt-32.");
  getch();
  sock_init();
  return (0);
}
#endif  /* TEST_PROG    */
#endif  /* USE_LANGUAGE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -