Move code on shape lines from parser.y to parsecode.c

This commit is contained in:
Thomas Jensen 2023-04-30 14:14:21 +02:00
parent 286ec99c6d
commit 771f78874e
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
3 changed files with 123 additions and 81 deletions

View File

@ -1013,4 +1013,98 @@ int action_add_regex_rule(pass_to_bison *bison_args, char *type, reprule_t **rul
}
int action_first_shape_line(pass_to_bison *bison_args, bxstr_t *line, sentry_t *shape)
{
sentry_t rval = SENTRY_INITIALIZER;
#ifdef PARSER_DEBUG
fprintf(stderr, "Initializing a shape entry with first line\n");
#endif
size_t error_pos = 0;
if (!bxs_valid_in_shape(line, &error_pos)) {
yyerror(bison_args, "invalid character in shape line at position %d", (int) error_pos);
return RC_ERROR;
}
rval.width = line->num_columns;
rval.height = 1;
rval.chars = (char **) malloc(sizeof(char *));
if (rval.chars == NULL) {
perror(PROJECT ": shape_lines21");
return RC_ABORT;
}
rval.chars[0] = (char *) strdup(line->ascii);
if (rval.chars[0] == NULL) {
perror(PROJECT ": shape_lines22");
return RC_ABORT;
}
rval.mbcs = (bxstr_t **) malloc(sizeof(bxstr_t *));
if (rval.mbcs == NULL) {
perror(PROJECT ": shape_lines23");
return RC_ABORT;
}
rval.mbcs[0] = bxs_strdup(line);
if (rval.mbcs[0] == NULL) {
perror(PROJECT ": shape_lines24");
return RC_ABORT;
}
memcpy(shape, &rval, sizeof(sentry_t));
return RC_SUCCESS;
}
int action_add_shape_line(pass_to_bison *bison_args, sentry_t *shape, bxstr_t *line)
{
#ifdef PARSER_DEBUG
fprintf(stderr, "Extending a shape entry\n");
#endif
size_t slen = line->num_columns;
if (slen != shape->width) {
yyerror(bison_args, "all elements of a shape spec must be of equal length");
return RC_ERROR;
}
size_t error_pos = 0;
if (!bxs_valid_in_shape(line, &error_pos)) {
yyerror(bison_args, "invalid character in shape line at position %d", (int) error_pos);
return RC_ERROR;
}
shape->height++;
char **tmp = (char **) realloc(shape->chars, shape->height * sizeof(char *));
if (tmp == NULL) {
perror(PROJECT ": shape_lines11");
return RC_ABORT;
}
shape->chars = tmp;
shape->chars[shape->height - 1] = (char *) strdup(line->ascii);
if (shape->chars[shape->height - 1] == NULL) {
perror(PROJECT ": shape_lines12");
return RC_ABORT;
}
bxstr_t **mtmp = (bxstr_t **) realloc(shape->mbcs, shape->height * sizeof(bxstr_t *));
if (mtmp == NULL) {
perror(PROJECT ": shape_lines13");
return RC_ABORT;
}
shape->mbcs = mtmp;
shape->mbcs[shape->height - 1] = bxs_strdup(line);
if (shape->mbcs[shape->height - 1] == NULL) {
perror(PROJECT ": shape_lines14");
return RC_ABORT;
}
return RC_SUCCESS;
}
/* vim: set cindent sw=4: */

View File

@ -163,12 +163,36 @@ int tag_record(pass_to_bison *bison_args, bxstr_t *tag);
* @param bison_args the parser state
* @param sample the sample block content (non-empty when this is invoked)
* @return 0: success;
* 1: YYERROR must be invoked
* 1: YYERROR must be invoked;
* 2: YYABORT must be invoked
*/
int action_sample_block(pass_to_bison *bison_args, bxstr_t *sample);
/**
* Start a new shape by adding its first line.
* @param bison_args the parser state
* @param line the first line of the new shape
* @param shape the address where the new shape shall be stored
* @return 0: success;
* 1: YYERROR must be invoked;
* 2: YYABORT must be invoked
*/
int action_first_shape_line(pass_to_bison *bison_args, bxstr_t *line, sentry_t *shape);
/**
* Extend a shape by adding another line.
* @param bison_args the parser state
* @param shape the address of the shape to be extended
* @param line the new line
* @return 0: success;
* 1: YYERROR must be invoked;
* 2: YYABORT must be invoked
*/
int action_add_shape_line(pass_to_bison *bison_args, sentry_t *shape, bxstr_t *line);
#endif
/*EOF*/ /* vim: set cindent sw=4: */

View File

@ -390,91 +390,15 @@ shape_def: '(' shape_lines ')'
shape_lines: shape_lines ',' STRING
{
sentry_t rval = $1; // TODO move this to parsecode.c
size_t slen = $3->num_columns;
#ifdef PARSER_DEBUG
fprintf (stderr, "Extending a shape entry\n");
#endif
if (slen != rval.width) {
yyerror(bison_args, "all elements of a shape spec must be of equal length");
YYERROR;
}
size_t error_pos = 0;
if (!bxs_valid_in_shape($3, &error_pos)) {
yyerror(bison_args, "invalid character in shape line at position %d", (int) error_pos);
YYERROR;
}
rval.height++;
char **tmp = (char **) realloc(rval.chars, rval.height * sizeof(char *));
if (tmp == NULL) {
perror (PROJECT": shape_lines11");
YYABORT;
}
rval.chars = tmp;
rval.chars[rval.height - 1] = (char *) strdup ($3->ascii);
if (rval.chars[rval.height-1] == NULL) {
perror (PROJECT": shape_lines12");
YYABORT;
}
bxstr_t **mtmp = (bxstr_t **) realloc(rval.mbcs, rval.height * sizeof(bxstr_t *));
if (mtmp == NULL) {
perror (PROJECT": shape_lines13");
YYABORT;
}
rval.mbcs = mtmp;
rval.mbcs[rval.height - 1] = bxs_strdup($3);
if (rval.mbcs[rval.height - 1] == NULL) {
perror (PROJECT": shape_lines14");
YYABORT;
}
sentry_t rval = $1;
invoke_action(action_add_shape_line(bison_args, &rval, $3));
$$ = rval;
}
| STRING
{
sentry_t rval = SENTRY_INITIALIZER;
#ifdef PARSER_DEBUG
fprintf (stderr, "Initializing a shape entry with first line\n");
#endif
size_t error_pos = 0;
if (!bxs_valid_in_shape($1, &error_pos)) {
yyerror(bison_args, "invalid character in shape line at position %d", (int) error_pos);
YYERROR;
}
rval.width = $1->num_columns;
rval.height = 1;
rval.chars = (char **) malloc (sizeof(char*));
if (rval.chars == NULL) {
perror (PROJECT": shape_lines21");
YYABORT;
}
rval.chars[0] = (char *) strdup ($1->ascii);
if (rval.chars[0] == NULL) {
perror (PROJECT": shape_lines22");
YYABORT;
}
rval.mbcs = (bxstr_t **) malloc(sizeof(bxstr_t *));
if (rval.mbcs == NULL) {
perror (PROJECT": shape_lines23");
YYABORT;
}
rval.mbcs[0] = bxs_strdup($1);
if (rval.mbcs[0] == NULL) {
perror (PROJECT": shape_lines24");
YYABORT;
}
sentry_t rval;
invoke_action(action_first_shape_line(bison_args, $1, &rval));
$$ = rval;
}
;