#include <err.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include "indent_globs.h"
#include "indent_codes.h"
#include "indent.h"
struct templ {
const char *rwd;
int rwcode;
};
struct templ specials[] =
{
{"_Bool", 4},
{"_Complex", 4},
{"_Imaginary", 4},
{"auto", 10},
{"bool", 4},
{"break", 9},
{"case", 8},
{"char", 4},
{"complex", 4},
{"const", 4},
{"continue", 12},
{"default", 8},
{"do", 6},
{"double", 4},
{"else", 6},
{"enum", 3},
{"extern", 10},
{"float", 4},
{"for", 5},
{"global", 4},
{"goto", 9},
{"if", 5},
{"imaginary", 4},
{"inline", 12},
{"int", 4},
{"long", 4},
{"offsetof", 1},
{"register", 10},
{"restrict", 12},
{"return", 9},
{"short", 4},
{"signed", 4},
{"sizeof", 2},
{"static", 10},
{"struct", 3},
{"switch", 7},
{"typedef", 11},
{"union", 3},
{"unsigned", 4},
{"void", 4},
{"volatile", 4},
{"while", 5}
};
const char **typenames;
int typename_count;
int typename_top = -1;
static char const *table[] = {
['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
['7'] = "DEIDEHHHIJ U VUVVZZZ",
['9'] = "DEJDEHHHJJ U VUVVZZZ",
['a'] = " U VUVV ",
['b'] = " K U VUVV ",
['e'] = " FFF FF U VUVV ",
['f'] = " f f U VUVV f",
['u'] = " MM M i iiM M ",
['x'] = " N ",
['p'] = " FFX ",
['L'] = " LLf fL PR Li L f",
['l'] = " OOf fO S P O i O f",
['+'] = " G Y ",
['.'] = "B EE EE T W ",
[0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
};
static int
strcmp_type(const void *e1, const void *e2)
{
return (strcmp(e1, *(const char * const *)e2));
}
int
lexi(struct parser_state *state)
{
int unary_delim;
int code;
char qchar;
e_token = s_token;
unary_delim = false;
state->col_1 = state->last_nl;
state->last_nl = false;
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
state->col_1 = false;
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (isalnum((unsigned char)*buf_ptr) ||
*buf_ptr == '_' || *buf_ptr == '$' ||
(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
struct templ *p;
if (isdigit((unsigned char)*buf_ptr) ||
(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
char s;
unsigned char i;
for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
i = (unsigned char)*buf_ptr;
if (i >= nitems(table) || table[i] == NULL ||
table[i][s - 'A'] == ' ') {
s = table[0][s - 'A'];
break;
}
s = table[i][s - 'A'];
CHECK_SIZE_TOKEN(1);
*e_token++ = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
}
else
while (isalnum((unsigned char)*buf_ptr) ||
*buf_ptr == BACKSLASH ||
*buf_ptr == '_' || *buf_ptr == '$') {
if (*buf_ptr == BACKSLASH) {
if (*(buf_ptr + 1) == '\n') {
buf_ptr += 2;
if (buf_ptr >= buf_end)
fill_buffer();
} else
break;
}
CHECK_SIZE_TOKEN(1);
*e_token++ = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
*e_token = '\0';
if (s_token[0] == 'L' && s_token[1] == '\0' &&
(*buf_ptr == '"' || *buf_ptr == '\''))
return (strpfx);
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
if (++buf_ptr >= buf_end)
fill_buffer();
}
state->keyword = 0;
if (state->last_token == structure && !state->p_l_follow) {
state->last_u_d = true;
return (decl);
}
state->last_u_d = (state->last_token == structure);
p = bsearch(s_token,
specials,
sizeof(specials) / sizeof(specials[0]),
sizeof(specials[0]),
strcmp_type);
if (p == NULL) {
char *u;
if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
strcmp(u, "_t") == 0) || (typename_top >= 0 &&
bsearch(s_token, typenames, typename_top + 1,
sizeof(typenames[0]), strcmp_type))) {
state->keyword = 4;
state->last_u_d = true;
goto found_typename;
}
} else {
state->keyword = p->rwcode;
state->last_u_d = true;
switch (p->rwcode) {
case 7:
return (swstmt);
case 8:
return (casestmt);
case 3:
case 4:
found_typename:
if (state->p_l_follow) {
state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
}
if (state->last_token == period || state->last_token == unary_op) {
state->keyword = 0;
break;
}
if (p != NULL && p->rwcode == 3)
return (structure);
if (state->p_l_follow)
break;
return (decl);
case 5:
return (sp_paren);
case 6:
return (sp_nparen);
case 10:
return (storage);
case 11:
return (type_def);
default:
return (ident);
}
}
if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
state->in_parameter_declaration == 0 && state->block_init == 0) {
char *tp = buf_ptr;
while (tp < buf_end)
if (*tp++ == ')' && (*tp == ';' || *tp == ','))
goto not_proc;
strncpy(state->procname, token, sizeof state->procname - 1);
if (state->in_decl)
state->in_parameter_declaration = 1;
return (funcname);
not_proc:;
}
else if (!state->p_l_follow && !state->block_init &&
!state->in_stmt &&
((*buf_ptr == '*' && buf_ptr[1] != '=') ||
isalpha((unsigned char)*buf_ptr)) &&
(state->last_token == semicolon || state->last_token == lbrace ||
state->last_token == rbrace)) {
state->keyword = 4;
state->last_u_d = true;
return decl;
}
if (state->last_token == decl)
state->last_u_d = true;
return (ident);
}
CHECK_SIZE_TOKEN(3);
*e_token++ = *buf_ptr;
*e_token = '\0';
if (++buf_ptr >= buf_end)
fill_buffer();
switch (*token) {
case '\n':
unary_delim = state->last_u_d;
state->last_nl = true;
code = (had_eof ? 0 : newline);
break;
case '\'':
case '"':
qchar = *token;
do {
while (1) {
if (*buf_ptr == '\n') {
diag2(1, "Unterminated literal");
goto stop_lit;
}
CHECK_SIZE_TOKEN(2);
*e_token = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
if (*e_token == BACKSLASH) {
if (*buf_ptr == '\n')
++line_no;
*++e_token = *buf_ptr++;
++e_token;
if (buf_ptr >= buf_end)
fill_buffer();
}
else
break;
}
} while (*e_token++ != qchar);
stop_lit:
code = ident;
break;
case ('('):
case ('['):
unary_delim = true;
code = lparen;
break;
case (')'):
case (']'):
code = rparen;
break;
case '#':
unary_delim = state->last_u_d;
code = preesc;
break;
case '?':
unary_delim = true;
code = question;
break;
case (':'):
code = colon;
unary_delim = true;
break;
case (';'):
unary_delim = true;
code = semicolon;
break;
case ('{'):
unary_delim = true;
code = lbrace;
break;
case ('}'):
unary_delim = true;
code = rbrace;
break;
case 014:
unary_delim = state->last_u_d;
state->last_nl = true;
code = form_feed;
break;
case (','):
unary_delim = true;
code = comma;
break;
case '.':
unary_delim = false;
code = period;
break;
case '-':
case '+':
code = (state->last_u_d ? unary_op : binary_op);
unary_delim = true;
if (*buf_ptr == token[0]) {
*e_token++ = *buf_ptr++;
if (state->last_token == ident || state->last_token == rparen) {
code = (state->last_u_d ? unary_op : postop);
unary_delim = false;
}
}
else if (*buf_ptr == '=')
*e_token++ = *buf_ptr++;
else if (*buf_ptr == '>') {
*e_token++ = *buf_ptr++;
if (!opt.pointer_as_binop) {
unary_delim = false;
code = unary_op;
state->want_blank = false;
}
}
break;
case '=':
if (state->in_or_st)
state->block_init = 1;
if (*buf_ptr == '=') {
*e_token++ = '=';
buf_ptr++;
*e_token = 0;
}
code = binary_op;
unary_delim = true;
break;
case '>':
case '<':
case '!':
if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
*e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (*buf_ptr == '=')
*e_token++ = *buf_ptr++;
code = (state->last_u_d ? unary_op : binary_op);
unary_delim = true;
break;
case '*':
unary_delim = true;
if (!state->last_u_d) {
if (*buf_ptr == '=')
*e_token++ = *buf_ptr++;
code = binary_op;
break;
}
while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
if (*buf_ptr == '*') {
CHECK_SIZE_TOKEN(1);
*e_token++ = *buf_ptr;
}
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (ps.in_decl) {
char *tp = buf_ptr;
while (isalpha((unsigned char)*tp) ||
isspace((unsigned char)*tp)) {
if (++tp >= buf_end)
fill_buffer();
}
if (*tp == '(')
ps.procname[0] = ' ';
}
code = unary_op;
break;
default:
if (token[0] == '/' && *buf_ptr == '*') {
*e_token++ = '*';
if (++buf_ptr >= buf_end)
fill_buffer();
code = comment;
unary_delim = state->last_u_d;
break;
}
while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
CHECK_SIZE_TOKEN(1);
*e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();
}
code = (state->last_u_d ? unary_op : binary_op);
unary_delim = true;
}
if (buf_ptr >= buf_end)
fill_buffer();
state->last_u_d = unary_delim;
CHECK_SIZE_TOKEN(1);
*e_token = '\0';
return (code);
}
void
init_constant_tt(void)
{
table['-'] = table['+'];
table['8'] = table['9'];
table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
table['B'] = table['b'];
table['E'] = table['e'];
table['U'] = table['u'];
table['X'] = table['x'];
table['P'] = table['p'];
table['F'] = table['f'];
}
void
alloc_typenames(void)
{
typenames = (const char **)malloc(sizeof(typenames[0]) *
(typename_count = 16));
if (typenames == NULL)
err(1, NULL);
}
void
add_typename(const char *key)
{
int comparison;
const char *copy;
if (typename_top + 1 >= typename_count) {
typenames = realloc((void *)typenames,
sizeof(typenames[0]) * (typename_count *= 2));
if (typenames == NULL)
err(1, NULL);
}
if (typename_top == -1)
typenames[++typename_top] = copy = strdup(key);
else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
if (comparison == 0)
return;
typenames[++typename_top] = copy = strdup(key);
}
else {
int p;
for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
;
if (comparison == 0)
return;
memmove(&typenames[p + 1], &typenames[p],
sizeof(typenames[0]) * (++typename_top - p));
typenames[p] = copy = strdup(key);
}
if (copy == NULL)
err(1, NULL);
}