#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "message.h"
#include "xgettext.h"
#include "x-ycp.h"
#include "error.h"
#include "xalloc.h"
#include "exit.h"
#include "gettext.h"
#define _(s) gettext(s)
void
init_flag_table_ycp ()
{
xgettext_record_flag ("sformat:1:ycp-format");
xgettext_record_flag ("y2debug:1:ycp-format");
xgettext_record_flag ("y2milestone:1:ycp-format");
xgettext_record_flag ("y2warning:1:ycp-format");
xgettext_record_flag ("y2error:1:ycp-format");
xgettext_record_flag ("y2security:1:ycp-format");
xgettext_record_flag ("y2internal:1:ycp-format");
}
static const char *real_file_name;
static char *logical_file_name;
static int line_number;
static int char_in_line;
static FILE *fp;
static int last_comment_line;
static int last_non_comment_line;
static int
phase1_getc ()
{
int c = getc (fp);
if (c == EOF)
{
if (ferror (fp))
error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
real_file_name);
return EOF;
}
if (c == '\n')
{
line_number++;
char_in_line = 0;
}
else
char_in_line++;
return c;
}
static void
phase1_ungetc (int c)
{
if (c != EOF)
{
if (c == '\n')
{
--line_number;
char_in_line = INT_MAX;
}
else
--char_in_line;
ungetc (c, fp);
}
}
static unsigned char phase2_pushback[1];
static int phase2_pushback_length;
static int
phase2_getc ()
{
static char *buffer;
static size_t bufmax;
size_t buflen;
int lineno;
int c;
bool last_was_star;
if (phase2_pushback_length)
return phase2_pushback[--phase2_pushback_length];
if (char_in_line == 0)
{
do
c = phase1_getc ();
while (c == '\t' || c == ' ');
if (c == '#')
{
buflen = 0;
lineno = line_number;
for (;;)
{
c = phase1_getc ();
if (c == '\n' || c == EOF)
break;
if (!(buflen == 0 && (c == ' ' || c == '\t')))
{
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen++] = c;
}
}
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
last_comment_line = lineno;
return '\n';
}
}
else
c = phase1_getc ();
if (c == '/')
{
c = phase1_getc ();
switch (c)
{
default:
phase1_ungetc (c);
return '/';
case '*':
buflen = 0;
lineno = line_number;
last_was_star = false;
for (;;)
{
c = phase1_getc ();
if (c == EOF)
break;
if (buflen == 0 && (c == ' ' || c == '\t'))
continue;
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen++] = c;
switch (c)
{
case '\n':
--buflen;
while (buflen >= 1
&& (buffer[buflen - 1] == ' '
|| buffer[buflen - 1] == '\t'))
--buflen;
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
buflen = 0;
lineno = line_number;
last_was_star = false;
continue;
case '*':
last_was_star = true;
continue;
case '/':
if (last_was_star)
{
buflen -= 2;
while (buflen >= 1
&& (buffer[buflen - 1] == ' '
|| buffer[buflen - 1] == '\t'))
--buflen;
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
break;
}
default:
last_was_star = false;
continue;
}
break;
}
last_comment_line = lineno;
return ' ';
case '/':
buflen = 0;
lineno = line_number;
for (;;)
{
c = phase1_getc ();
if (c == '\n' || c == EOF)
break;
if (!(buflen == 0 && (c == ' ' || c == '\t')))
{
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen++] = c;
}
}
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
last_comment_line = lineno;
return '\n';
}
}
else
return c;
}
static void
phase2_ungetc (int c)
{
if (c != EOF)
phase2_pushback[phase2_pushback_length++] = c;
}
enum token_type_ty
{
token_type_eof,
token_type_lparen,
token_type_rparen,
token_type_comma,
token_type_i18n,
token_type_string_literal,
token_type_symbol,
token_type_other
};
typedef enum token_type_ty token_type_ty;
typedef struct token_ty token_ty;
struct token_ty
{
token_type_ty type;
char *string;
int line_number;
};
#define P7_QUOTES (1000 + '"')
static int
phase7_getc ()
{
int c;
for (;;)
{
c = phase1_getc ();
if (c == '"')
return P7_QUOTES;
if (c != '\\')
return c;
c = phase1_getc ();
if (c != '\n')
switch (c)
{
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
#if 0
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
{
int n, j;
n = 0;
for (j = 0; j < 3; ++j)
{
n = n * 8 + c - '0';
c = phase1_getc ();
switch (c)
{
default:
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
continue;
}
break;
}
phase1_ungetc (c);
return n;
}
#endif
default:
return c;
}
}
}
static void
x_ycp_lex (token_ty *tp)
{
static char *buffer;
static int bufmax;
int bufpos;
int c;
for (;;)
{
tp->line_number = line_number;
c = phase2_getc ();
switch (c)
{
case EOF:
tp->type = token_type_eof;
return;
case '\n':
if (last_non_comment_line > last_comment_line)
xgettext_comment_reset ();
case '\r':
case '\t':
case ' ':
continue;
}
last_non_comment_line = tp->line_number;
switch (c)
{
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
case '_':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
bufpos = 0;
for (;;)
{
if (bufpos >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[bufpos++] = c;
c = phase2_getc ();
switch (c)
{
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
case '_':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
continue;
default:
if (bufpos == 1 && buffer[0] == '_' && c == '(')
{
tp->type = token_type_i18n;
return;
}
phase2_ungetc (c);
break;
}
break;
}
if (bufpos >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[bufpos] = '\0';
tp->string = xstrdup (buffer);
tp->type = token_type_symbol;
return;
case '"':
bufpos = 0;
for (;;)
{
c = phase7_getc ();
if (c == EOF || c == P7_QUOTES)
break;
if (bufpos >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[bufpos++] = c;
}
if (bufpos >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[bufpos] = '\0';
tp->string = xstrdup (buffer);
tp->type = token_type_string_literal;
return;
case '(':
tp->type = token_type_lparen;
return;
case ')':
tp->type = token_type_rparen;
return;
case ',':
tp->type = token_type_comma;
return;
default:
tp->type = token_type_other;
return;
}
}
}
static flag_context_list_table_ty *flag_context_list_table;
static bool
extract_parenthesized (message_list_ty *mlp,
flag_context_ty outer_context,
flag_context_list_iterator_ty context_iter,
bool in_i18n)
{
int state;
message_ty *plural_mp = NULL;
flag_context_list_iterator_ty next_context_iter =
passthrough_context_list_iterator;
flag_context_ty inner_context =
inherited_context (outer_context,
flag_context_list_iterator_advance (&context_iter));
state = (in_i18n ? 1 : 0);
for (;;)
{
token_ty token;
x_ycp_lex (&token);
switch (token.type)
{
case token_type_i18n:
if (extract_parenthesized (mlp, inner_context, next_context_iter,
true))
return true;
next_context_iter = null_context_list_iterator;
state = 0;
continue;
case token_type_string_literal:
if (state == 1)
{
lex_pos_ty pos;
pos.file_name = logical_file_name;
pos.line_number = token.line_number;
if (plural_mp == NULL)
{
plural_mp = remember_a_message (mlp, token.string,
inner_context, &pos);
state = 2;
}
else
{
remember_a_message_plural (plural_mp, token.string,
inner_context, &pos);
state = 0;
}
}
else
{
free (token.string);
state = 0;
}
next_context_iter = null_context_list_iterator;
continue;
case token_type_symbol:
next_context_iter =
flag_context_list_iterator (
flag_context_list_table_lookup (
flag_context_list_table,
token.string, strlen (token.string)));
free (token.string);
state = 0;
continue;
case token_type_lparen:
if (extract_parenthesized (mlp, inner_context, next_context_iter,
false))
return true;
next_context_iter = null_context_list_iterator;
state = 0;
continue;
case token_type_rparen:
return false;
case token_type_comma:
if (state == 2)
state = 1;
else
state = 0;
inner_context =
inherited_context (outer_context,
flag_context_list_iterator_advance (
&context_iter));
next_context_iter = passthrough_context_list_iterator;
continue;
case token_type_other:
next_context_iter = null_context_list_iterator;
state = 0;
continue;
case token_type_eof:
return true;
default:
abort ();
}
}
}
void
extract_ycp (FILE *f,
const char *real_filename, const char *logical_filename,
flag_context_list_table_ty *flag_table,
msgdomain_list_ty *mdlp)
{
message_list_ty *mlp = mdlp->item[0]->messages;
fp = f;
real_file_name = real_filename;
logical_file_name = xstrdup (logical_filename);
line_number = 1;
char_in_line = 0;
last_comment_line = -1;
last_non_comment_line = -1;
flag_context_list_table = flag_table;
while (!extract_parenthesized (mlp, null_context, null_context_list_iterator,
false))
;
fp = NULL;
real_file_name = NULL;
logical_file_name = NULL;
line_number = 0;
char_in_line = 0;
}