чување 97999bc88b436de47c1f6ad67b38e00ee72c5934
родитељ 3f8287b2b3cc618371a39727ef109e053c2040cc
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Fri, 2 Jul 2021 22:11:26 +0200
Added detection of more cases of wrong syntax, moved error reporting to poe.c
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | po.c | | | 50 | +++++++++++++++++++++++--------------------------- |
| M | po.h | | | 7 | ++++--- |
| M | poe.c | | | 11 | ++++++----- |
измењених датотека: 3, додавања: 33(+), брисања: 35(-)
diff --git a/po.c b/po.c
@@ -4,8 +4,6 @@
#include "po.h"
#include "util.h"
-extern int print_error(const int code, const char* msg, ...);
-
char*
format_flags(char* buffer, const size_t max, const struct PoEntry* entry)
{
@@ -163,12 +161,12 @@ u32_add_comment(struct PoEntry* entry, const uint32_t* comment)
int
load_file(struct PoEntry** entries, size_t* entries_size,
- size_t* entries_count, const char* filename)
+ size_t* entries_count, const char* filename, long* lineno,
+ long* col)
{
struct PoEntry* newchunk = calloc(ALLOC_DELTA, sizeof(struct PoEntry));
struct PoEntry* entry = NULL;
ParseState state = PS_NONE;
- long lineno = 0;
*entries_size = 0;
if (!newchunk)
@@ -196,10 +194,9 @@ load_file(struct PoEntry** entries, size_t* entries_size,
int result;
if ((result = parse_po_line(input_line, entries, entries_size,
- entries_count, &entry, &state)
- == PARSE_ERR_NONE))
+ entries_count, &entry, &state,
+ col)) == PARSE_ERR_NONE)
{
- poe_log("result == ERR_NONE, %d\n", result);
if (state & PS_MSGID)
msgid_counter = -1;
else if (msgid_counter != -1)
@@ -210,14 +207,11 @@ load_file(struct PoEntry** entries, size_t* entries_size,
}
else
{
- poe_log("result != ERR_NONE, %d\n", result);
- print_error(result, "%s:%ld: Parse error", filename,
- lineno);
if (*entries)
free(*entries);
return LOAD_PARSE_ERR(result);
}
- lineno++;
+ (*lineno)++;
}
fclose(input);
@@ -237,10 +231,10 @@ save_file(const struct PoEntry* entries, const char* filename)
int
parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
size_t* entries_count, struct PoEntry** current_entry,
- ParseState* state)
+ ParseState* state, long* col)
{
size_t i = 0;
- long col = 0;
+ *col = 1;
const char* pline = line;
char token[MAXBUFLINE];
*token = 0;
@@ -254,7 +248,7 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
size_t len = strlen("msgid ");
*state |= PS_MSGID;
pline += len;
- col += len;
+ *col += len;
}
else if (starts_with(line, "msgid_plural "))
{
@@ -262,7 +256,7 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
*state &= ~PS_MSGID;
*state |= PS_MSGID_PLURAL;
pline += len;
- col += len;
+ *col += len;
}
else if (starts_with(line, "msgstr "))
{
@@ -271,7 +265,7 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
*state &= ~PS_MSGID_PLURAL;
*state |= PS_MSGSTR;
pline += len;
- col += len;
+ *col += len;
}
else if (starts_with(line, "msgstr["))
{
@@ -280,12 +274,12 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
*state &= ~PS_MSGID_PLURAL;
*state |= PS_MSGSTR_ARRAY;
pline += len;
- col += len;
+ *col += len;
while (*pline >= '0' && *pline <= '9')
{
pline++;
- col++;
+ (*col)++;
}
if (starts_with(pline, "] "))
@@ -303,36 +297,38 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
*state &= ~PS_MSGSTR;
*state &= ~PS_MSGSTR_ARRAY;
}
+ else if (*line != ' ' && *line != '\t' && *line != '"' && *line != '#')
+ return PARSE_ERR_SYNTAX;
while (*pline && i < strlen(line))
{
switch (*pline)
{
case '#':
- if (col == 0)
+ if (*col == 1)
{
*state |= PS_COMMENT;
pline++;
- col++;
+ (*col)++;
}
else
{
*ptoken++ = *pline++;
- col++;
+ (*col)++;
}
break;
case '.':
- if (col == 1 && *state & PS_COMMENT)
+ if (*col == 2 && *state & PS_COMMENT)
{
*state &= ~PS_COMMENT;
*state |= PS_COMMENT_DESC;
pline++;
- col++;
+ (*col)++;
}
else
{
*ptoken++ = *pline++;
- col++;
+ (*col)++;
}
break;
case '"':
@@ -340,17 +336,17 @@ parse_po_line(const char* line, struct PoEntry** entries, size_t* entries_size,
if (ptoken == token || *(pline+1) == 0)
{
pline++;
- col++;
+ (*col)++;
}
else
{
*ptoken++ = *pline++;
- col++;
+ (*col)++;
}
break;
default:
*ptoken++ = *pline++;
- col++;
+ (*col)++;
}
}
if (*state == PS_NONE)
diff --git a/po.h b/po.h
@@ -8,7 +8,7 @@
#define ALLOC_DELTA 10
#define PO_DETECTION_LINES 15
-#define LOAD_PARSE_ERR(e) (LOAD_ERR_PARSE_PREFIX + e)
+#define LOAD_PARSE_ERR(e) (LOAD_ERR_PARSE_PREFIX + (e))
enum {
PARSE_ERR_NONE = 0,
@@ -73,9 +73,10 @@ struct PoEntry* u32_set_msgstr(struct PoEntry* entry, const uint32_t* msgstr,
int msgstr_index, int append);
struct PoEntry* u32_add_comment(struct PoEntry* entry, const uint32_t* comment);
int load_file(struct PoEntry** entries, size_t* entries_size,
- size_t* entries_count, const char* filename);
+ size_t* entries_count, const char* filename, long* lineno,
+ long* col);
int save_file(const struct PoEntry* entries, const char* filename);
int parse_po_line(const char* line, struct PoEntry** entries,
size_t* entries_size, size_t* entries_count,
- struct PoEntry** current_entry, ParseState* state);
+ struct PoEntry** current_entry, ParseState* state, long* col);
diff --git a/poe.c b/poe.c
@@ -777,11 +777,14 @@ main(int argc, char** argv)
if (!(input = fopen(filename, "rt")))
return print_error(ENOENT, "No such file `%s'", filename);
+ long lineno = 1;
+ long col = 1;
int result = load_file(&state.entries, &state.msgid_size,
- &state.msgid_count, filename);
+ &state.msgid_count, filename, &lineno, &col);
switch (result)
{
case LOAD_ERR_CANT_ALLOC:
+ case LOAD_ERR_P_ALLOC:
return print_error(result,
"Allocation failed (out of memory?)");
case LOAD_ERR_CANT_OPEN_FILE:
@@ -792,11 +795,9 @@ main(int argc, char** argv)
filename);
case LOAD_ERR_P_INVAL:
return print_error(result, "Invalid value");
- case LOAD_ERR_P_ALLOC:
- return print_error(result,
- "Allocation failed during parse");
case LOAD_ERR_P_SYNTAX:
- return print_error(result, "Syntax error");
+ return print_error(result, "%s:%ld: Syntax error",
+ filename, lineno);
case LOAD_ERR_NONE:
break;
default: