Home | History | Annotate | Line # | Download | only in indent
lexi.c revision 1.26
      1 /*	$NetBSD: lexi.c,v 1.26 2021/03/08 20:20:11 rillig Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-4-Clause
      5  *
      6  * Copyright (c) 1985 Sun Microsystems, Inc.
      7  * Copyright (c) 1980, 1993
      8  *	The Regents of the University of California.  All rights reserved.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #if 0
     41 #ifndef lint
     42 static char sccsid[] = "@(#)lexi.c	8.1 (Berkeley) 6/6/93";
     43 #endif /* not lint */
     44 #endif
     45 
     46 #include <sys/cdefs.h>
     47 #ifndef lint
     48 #if defined(__NetBSD__)
     49 __RCSID("$NetBSD: lexi.c,v 1.26 2021/03/08 20:20:11 rillig Exp $");
     50 #elif defined(__FreeBSD__)
     51 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
     52 #endif
     53 #endif
     54 
     55 /*
     56  * Here we have the token scanner for indent.  It scans off one token and puts
     57  * it in the global variable "token".  It returns a code, indicating the type
     58  * of token scanned.
     59  */
     60 
     61 #include <assert.h>
     62 #include <err.h>
     63 #include <stdio.h>
     64 #include <ctype.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <sys/param.h>
     68 
     69 #include "indent.h"
     70 
     71 struct templ {
     72     const char *rwd;
     73     enum rwcode rwcode;
     74 };
     75 
     76 /*
     77  * This table has to be sorted alphabetically, because it'll be used in binary
     78  * search. For the same reason, string must be the first thing in struct templ.
     79  */
     80 struct templ specials[] =
     81 {
     82     {"_Bool", rw_type},
     83     {"_Complex", rw_type},
     84     {"_Imaginary", rw_type},
     85     {"auto", rw_storage_class},
     86     {"bool", rw_type},
     87     {"break", rw_jump},
     88     {"case", rw_case_or_default},
     89     {"char", rw_type},
     90     {"complex", rw_type},
     91     {"const", rw_type},
     92     {"continue", rw_jump},
     93     {"default", rw_case_or_default},
     94     {"do", rw_do_or_else},
     95     {"double", rw_type},
     96     {"else", rw_do_or_else},
     97     {"enum", rw_struct_or_union_or_enum},
     98     {"extern", rw_storage_class},
     99     {"float", rw_type},
    100     {"for", rw_for_or_if_or_while},
    101     {"global", rw_type},
    102     {"goto", rw_jump},
    103     {"if", rw_for_or_if_or_while},
    104     {"imaginary", rw_type},
    105     {"inline", rw_inline_or_restrict},
    106     {"int", rw_type},
    107     {"long", rw_type},
    108     {"offsetof", rw_offsetof},
    109     {"register", rw_storage_class},
    110     {"restrict", rw_inline_or_restrict},
    111     {"return", rw_jump},
    112     {"short", rw_type},
    113     {"signed", rw_type},
    114     {"sizeof", rw_sizeof},
    115     {"static", rw_storage_class},
    116     {"struct", rw_struct_or_union_or_enum},
    117     {"switch", rw_switch},
    118     {"typedef", rw_typedef},
    119     {"union", rw_struct_or_union_or_enum},
    120     {"unsigned", rw_type},
    121     {"void", rw_type},
    122     {"volatile", rw_type},
    123     {"while", rw_for_or_if_or_while}
    124 };
    125 
    126 const char **typenames;
    127 int         typename_count;
    128 int         typename_top = -1;
    129 
    130 /*
    131  * The transition table below was rewritten by hand from lx's output, given
    132  * the following definitions. lx is Katherine Flavel's lexer generator.
    133  *
    134  * O  = /[0-7]/;        D  = /[0-9]/;          NZ = /[1-9]/;
    135  * H  = /[a-f0-9]/i;    B  = /[0-1]/;          HP = /0x/i;
    136  * BP = /0b/i;          E  = /e[+\-]?/i D+;    P  = /p[+\-]?/i D+;
    137  * FS = /[fl]/i;        IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
    138  *
    139  * D+           E  FS? -> $float;
    140  * D*    "." D+ E? FS? -> $float;
    141  * D+    "."    E? FS? -> $float;    HP H+           IS? -> $int;
    142  * HP H+        P  FS? -> $float;    NZ D*           IS? -> $int;
    143  * HP H* "." H+ P  FS? -> $float;    "0" O*          IS? -> $int;
    144  * HP H+ "."    P  FS  -> $float;    BP B+           IS? -> $int;
    145  */
    146 static char const *table[] = {
    147     /*                examples:
    148                                      00
    149              s                      0xx
    150              t                    00xaa
    151              a     11       101100xxa..
    152              r   11ee0001101lbuuxx.a.pp
    153              t.01.e+008bLuxll0Ll.aa.p+0
    154     states:  ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    155     ['0'] = "CEIDEHHHIJQ  U  Q  VUVVZZZ",
    156     ['1'] = "DEIDEHHHIJQ  U  Q  VUVVZZZ",
    157     ['7'] = "DEIDEHHHIJ   U     VUVVZZZ",
    158     ['9'] = "DEJDEHHHJJ   U     VUVVZZZ",
    159     ['a'] = "             U     VUVV   ",
    160     ['b'] = "  K          U     VUVV   ",
    161     ['e'] = "  FFF   FF   U     VUVV   ",
    162     ['f'] = "    f  f     U     VUVV  f",
    163     ['u'] = "  MM    M  i  iiM   M     ",
    164     ['x'] = "  N                       ",
    165     ['p'] = "                    FFX   ",
    166     ['L'] = "  LLf  fL  PR   Li  L    f",
    167     ['l'] = "  OOf  fO   S P O i O    f",
    168     ['+'] = "     G                 Y  ",
    169     ['.'] = "B EE    EE   T      W     ",
    170     /*       ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    171     [0]   = "uuiifuufiuuiiuiiiiiuiuuuuu",
    172 };
    173 
    174 static void
    175 check_size_token(size_t desired_size)
    176 {
    177     if (e_token + (desired_size) >= l_token) {
    178 	int nsize = l_token - s_token + 400 + desired_size;
    179 	int token_len = e_token - s_token;
    180 	tokenbuf = (char *)realloc(tokenbuf, nsize);
    181 	if (tokenbuf == NULL)
    182 	    err(1, NULL);
    183 	e_token = tokenbuf + token_len + 1;
    184 	l_token = tokenbuf + nsize - 5;
    185 	s_token = tokenbuf + 1;
    186     }
    187 }
    188 
    189 static int
    190 strcmp_type(const void *e1, const void *e2)
    191 {
    192     return strcmp(e1, *(const char *const *)e2);
    193 }
    194 
    195 #ifdef debug
    196 const char *
    197 token_type_name(token_type tk)
    198 {
    199     static const char *const name[] = {
    200 	"end_of_file", "newline", "lparen", "rparen", "unary_op",
    201 	"binary_op", "postop", "question", "casestmt", "colon",
    202 	"semicolon", "lbrace", "rbrace", "ident", "comma",
    203 	"comment", "swstmt", "preesc", "form_feed", "decl",
    204 	"sp_paren", "sp_nparen", "ifstmt", "whilestmt", "forstmt",
    205 	"stmt", "stmtl", "elselit", "dolit", "dohead",
    206 	"ifhead", "elsehead", "period", "strpfx", "storage",
    207 	"funcname", "type_def", "structure"
    208     };
    209 
    210     assert(0 <= tk && tk < sizeof name / sizeof name[0]);
    211 
    212     return name[tk];
    213 }
    214 
    215 static void
    216 print_buf(const char *name, const char *s, const char *e)
    217 {
    218     if (s == e)
    219 	return;
    220 
    221     printf(" %s \"", name);
    222     for (const char *p = s; p < e; p++) {
    223 	if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
    224 	    printf("%c", *p);
    225 	else if (*p == '\n')
    226 	    printf("\\n");
    227 	else if (*p == '\t')
    228 	    printf("\\t");
    229 	else
    230 	    printf("\\x%02x", *p);
    231     }
    232     printf("\"");
    233 }
    234 
    235 static token_type
    236 lexi_end(token_type code)
    237 {
    238     printf("in line %d, lexi returns '%s'", line_no, token_type_name(code));
    239     print_buf("token", s_token, e_token);
    240     print_buf("label", s_lab, e_lab);
    241     print_buf("code", s_code, e_code);
    242     print_buf("comment", s_com, e_com);
    243     printf("\n");
    244 
    245     return code;
    246 }
    247 #else
    248 #  define lexi_end(tk) (tk)
    249 #endif
    250 
    251 token_type
    252 lexi(struct parser_state *state)
    253 {
    254     int         unary_delim;	/* this is set to 1 if the current token
    255 				 * forces a following operator to be unary */
    256     token_type  code;		/* internal code to be returned */
    257     char        qchar;		/* the delimiter character for a string */
    258 
    259     e_token = s_token;		/* point to start of place to save token */
    260     unary_delim = false;
    261     state->col_1 = state->last_nl;	/* tell world that this token started
    262 					 * in column 1 iff the last thing
    263 					 * scanned was a newline */
    264     state->last_nl = false;
    265 
    266     while (*buf_ptr == ' ' || *buf_ptr == '\t') {	/* get rid of blanks */
    267 	state->col_1 = false;	/* leading blanks imply token is not in column
    268 				 * 1 */
    269 	if (++buf_ptr >= buf_end)
    270 	    fill_buffer();
    271     }
    272 
    273     /* Scan an alphanumeric token */
    274     if (isalnum((unsigned char)*buf_ptr) ||
    275 	*buf_ptr == '_' || *buf_ptr == '$' ||
    276 	(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
    277 	/*
    278 	 * we have a character or number
    279 	 */
    280 	struct templ *p;
    281 
    282 	if (isdigit((unsigned char)*buf_ptr) ||
    283 	    (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
    284 	    char s;
    285 	    unsigned char i;
    286 
    287 	    for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
    288 		i = (unsigned char)*buf_ptr;
    289 		if (i >= nitems(table) || table[i] == NULL ||
    290 		    table[i][s - 'A'] == ' ') {
    291 		    s = table[0][s - 'A'];
    292 		    break;
    293 		}
    294 		s = table[i][s - 'A'];
    295 		check_size_token(1);
    296 		*e_token++ = *buf_ptr++;
    297 		if (buf_ptr >= buf_end)
    298 		    fill_buffer();
    299 	    }
    300 	    /* s now indicates the type: f(loating), i(integer), u(nknown) */
    301 	}
    302 	else
    303 	    while (isalnum((unsigned char)*buf_ptr) ||
    304 	        *buf_ptr == '\\' ||
    305 		*buf_ptr == '_' || *buf_ptr == '$') {
    306 		/* fill_buffer() terminates buffer with newline */
    307 		if (*buf_ptr == '\\') {
    308 		    if (*(buf_ptr + 1) == '\n') {
    309 			buf_ptr += 2;
    310 			if (buf_ptr >= buf_end)
    311 			    fill_buffer();
    312 			} else
    313 			    break;
    314 		}
    315 		check_size_token(1);
    316 		/* copy it over */
    317 		*e_token++ = *buf_ptr++;
    318 		if (buf_ptr >= buf_end)
    319 		    fill_buffer();
    320 	    }
    321 	*e_token = '\0';
    322 
    323 	if (s_token[0] == 'L' && s_token[1] == '\0' &&
    324 	      (*buf_ptr == '"' || *buf_ptr == '\''))
    325 	    return lexi_end(strpfx);
    326 
    327 	while (*buf_ptr == ' ' || *buf_ptr == '\t') {	/* get rid of blanks */
    328 	    if (++buf_ptr >= buf_end)
    329 		fill_buffer();
    330 	}
    331 	state->keyword = rw_0;
    332 	if (state->last_token == structure && !state->p_l_follow) {
    333 				/* if last token was 'struct' and we're not
    334 				 * in parentheses, then this token
    335 				 * should be treated as a declaration */
    336 	    state->last_u_d = true;
    337 	    return lexi_end(decl);
    338 	}
    339 	/*
    340 	 * Operator after identifier is binary unless last token was 'struct'
    341 	 */
    342 	state->last_u_d = (state->last_token == structure);
    343 
    344 	p = bsearch(s_token,
    345 	    specials,
    346 	    sizeof(specials) / sizeof(specials[0]),
    347 	    sizeof(specials[0]),
    348 	    strcmp_type);
    349 	if (p == NULL) {	/* not a special keyword... */
    350 	    char *u;
    351 
    352 	    /* ... so maybe a type_t or a typedef */
    353 	    if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
    354 	        strcmp(u, "_t") == 0) || (typename_top >= 0 &&
    355 		  bsearch(s_token, typenames, typename_top + 1,
    356 		    sizeof(typenames[0]), strcmp_type))) {
    357 		state->keyword = rw_type;
    358 		state->last_u_d = true;
    359 	        goto found_typename;
    360 	    }
    361 	} else {			/* we have a keyword */
    362 	    state->keyword = p->rwcode;
    363 	    state->last_u_d = true;
    364 	    switch (p->rwcode) {
    365 	    case rw_switch:
    366 		return lexi_end(swstmt);
    367 	    case rw_case_or_default:
    368 		return lexi_end(casestmt);
    369 	    case rw_struct_or_union_or_enum:
    370 	    case rw_type:
    371 	    found_typename:
    372 		if (state->p_l_follow) {
    373 		    /* inside parens: cast, param list, offsetof or sizeof */
    374 		    state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
    375 		}
    376 		if (state->last_token == period || state->last_token == unary_op) {
    377 		    state->keyword = rw_0;
    378 		    break;
    379 		}
    380 		if (p != NULL && p->rwcode == rw_struct_or_union_or_enum)
    381 		    return lexi_end(structure);
    382 		if (state->p_l_follow)
    383 		    break;
    384 		return lexi_end(decl);
    385 
    386 	    case rw_for_or_if_or_while:
    387 		return lexi_end(sp_paren);
    388 
    389 	    case rw_do_or_else:
    390 		return lexi_end(sp_nparen);
    391 
    392 	    case rw_storage_class:
    393 		return lexi_end(storage);
    394 
    395 	    case rw_typedef:
    396 		return lexi_end(type_def);
    397 
    398 	    default:		/* all others are treated like any other
    399 				 * identifier */
    400 		return lexi_end(ident);
    401 	    }			/* end of switch */
    402 	}			/* end of if (found_it) */
    403 	if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
    404 	    state->in_parameter_declaration == 0 && state->block_init == 0) {
    405 	    char *tp = buf_ptr;
    406 	    while (tp < buf_end)
    407 		if (*tp++ == ')' && (*tp == ';' || *tp == ','))
    408 		    goto not_proc;
    409 	    strncpy(state->procname, token, sizeof state->procname - 1);
    410 	    if (state->in_decl)
    411 		state->in_parameter_declaration = 1;
    412 	    return lexi_end(funcname);
    413     not_proc:;
    414 	}
    415 	/*
    416 	 * The following hack attempts to guess whether or not the current
    417 	 * token is in fact a declaration keyword -- one that has been
    418 	 * typedefd
    419 	 */
    420 	else if (!state->p_l_follow && !state->block_init &&
    421 	    !state->in_stmt &&
    422 	    ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
    423 		isalpha((unsigned char)*buf_ptr)) &&
    424 	    (state->last_token == semicolon || state->last_token == lbrace ||
    425 		state->last_token == rbrace)) {
    426 	    state->keyword = rw_type;
    427 	    state->last_u_d = true;
    428 	    return lexi_end(decl);
    429 	}
    430 	if (state->last_token == decl)	/* if this is a declared variable,
    431 					 * then following sign is unary */
    432 	    state->last_u_d = true;	/* will make "int a -1" work */
    433 	return lexi_end(ident);		/* the ident is not in the list */
    434     }				/* end of procesing for alpanum character */
    435 
    436     /* Scan a non-alphanumeric token */
    437 
    438     check_size_token(3);		/* things like "<<=" */
    439     *e_token++ = *buf_ptr;		/* if it is only a one-character token, it is
    440 				 * moved here */
    441     *e_token = '\0';
    442     if (++buf_ptr >= buf_end)
    443 	fill_buffer();
    444 
    445     switch (*token) {
    446     case '\n':
    447 	unary_delim = state->last_u_d;
    448 	state->last_nl = true;	/* remember that we just had a newline */
    449 	code = (had_eof ? end_of_file : newline);
    450 
    451 	/*
    452 	 * if data has been exhausted, the newline is a dummy, and we should
    453 	 * return code to stop
    454 	 */
    455 	break;
    456 
    457     case '\'':			/* start of quoted character */
    458     case '"':			/* start of string */
    459 	qchar = *token;
    460 	do {			/* copy the string */
    461 	    while (1) {		/* move one character or [/<char>]<char> */
    462 		if (*buf_ptr == '\n') {
    463 		    diag(1, "Unterminated literal");
    464 		    goto stop_lit;
    465 		}
    466 		check_size_token(2);
    467 		*e_token = *buf_ptr++;
    468 		if (buf_ptr >= buf_end)
    469 		    fill_buffer();
    470 		if (*e_token == '\\') {		/* if escape, copy extra char */
    471 		    if (*buf_ptr == '\n')	/* check for escaped newline */
    472 			++line_no;
    473 		    *++e_token = *buf_ptr++;
    474 		    ++e_token;	/* we must increment this again because we
    475 				 * copied two chars */
    476 		    if (buf_ptr >= buf_end)
    477 			fill_buffer();
    478 		}
    479 		else
    480 		    break;	/* we copied one character */
    481 	    }			/* end of while (1) */
    482 	} while (*e_token++ != qchar);
    483 stop_lit:
    484 	code = ident;
    485 	break;
    486 
    487     case ('('):
    488     case ('['):
    489 	unary_delim = true;
    490 	code = lparen;
    491 	break;
    492 
    493     case (')'):
    494     case (']'):
    495 	code = rparen;
    496 	break;
    497 
    498     case '#':
    499 	unary_delim = state->last_u_d;
    500 	code = preesc;
    501 	break;
    502 
    503     case '?':
    504 	unary_delim = true;
    505 	code = question;
    506 	break;
    507 
    508     case (':'):
    509 	code = colon;
    510 	unary_delim = true;
    511 	break;
    512 
    513     case (';'):
    514 	unary_delim = true;
    515 	code = semicolon;
    516 	break;
    517 
    518     case ('{'):
    519 	unary_delim = true;
    520 
    521 	/*
    522 	 * if (state->in_or_st) state->block_init = 1;
    523 	 */
    524 	/* ?	code = state->block_init ? lparen : lbrace; */
    525 	code = lbrace;
    526 	break;
    527 
    528     case ('}'):
    529 	unary_delim = true;
    530 	/* ?	code = state->block_init ? rparen : rbrace; */
    531 	code = rbrace;
    532 	break;
    533 
    534     case 014:			/* a form feed */
    535 	unary_delim = state->last_u_d;
    536 	state->last_nl = true;	/* remember this so we can set 'state->col_1'
    537 				 * right */
    538 	code = form_feed;
    539 	break;
    540 
    541     case (','):
    542 	unary_delim = true;
    543 	code = comma;
    544 	break;
    545 
    546     case '.':
    547 	unary_delim = false;
    548 	code = period;
    549 	break;
    550 
    551     case '-':
    552     case '+':			/* check for -, +, --, ++ */
    553 	code = (state->last_u_d ? unary_op : binary_op);
    554 	unary_delim = true;
    555 
    556 	if (*buf_ptr == token[0]) {
    557 	    /* check for doubled character */
    558 	    *e_token++ = *buf_ptr++;
    559 	    /* buffer overflow will be checked at end of loop */
    560 	    if (state->last_token == ident || state->last_token == rparen) {
    561 		code = (state->last_u_d ? unary_op : postop);
    562 		/* check for following ++ or -- */
    563 		unary_delim = false;
    564 	    }
    565 	}
    566 	else if (*buf_ptr == '=')
    567 	    /* check for operator += */
    568 	    *e_token++ = *buf_ptr++;
    569 	else if (*buf_ptr == '>') {
    570 	    /* check for operator -> */
    571 	    *e_token++ = *buf_ptr++;
    572 	    unary_delim = false;
    573 	    code = unary_op;
    574 	    state->want_blank = false;
    575 	}
    576 	break;			/* buffer overflow will be checked at end of
    577 				 * switch */
    578 
    579     case '=':
    580 	if (state->in_or_st)
    581 	    state->block_init = 1;
    582 	if (*buf_ptr == '=') {/* == */
    583 	    *e_token++ = '=';	/* Flip =+ to += */
    584 	    buf_ptr++;
    585 	    *e_token = 0;
    586 	}
    587 	code = binary_op;
    588 	unary_delim = true;
    589 	break;
    590 	/* can drop thru!!! */
    591 
    592     case '>':
    593     case '<':
    594     case '!':			/* ops like <, <<, <=, !=, etc */
    595 	if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
    596 	    *e_token++ = *buf_ptr;
    597 	    if (++buf_ptr >= buf_end)
    598 		fill_buffer();
    599 	}
    600 	if (*buf_ptr == '=')
    601 	    *e_token++ = *buf_ptr++;
    602 	code = (state->last_u_d ? unary_op : binary_op);
    603 	unary_delim = true;
    604 	break;
    605 
    606     case '*':
    607 	unary_delim = true;
    608 	if (!state->last_u_d) {
    609 	    if (*buf_ptr == '=')
    610 		*e_token++ = *buf_ptr++;
    611 	    code = binary_op;
    612 	    break;
    613 	}
    614 	while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
    615 	    if (*buf_ptr == '*') {
    616 		check_size_token(1);
    617 		*e_token++ = *buf_ptr;
    618 	    }
    619 	    if (++buf_ptr >= buf_end)
    620 		fill_buffer();
    621 	}
    622 	if (ps.in_decl) {
    623 	    char *tp = buf_ptr;
    624 
    625 	    while (isalpha((unsigned char)*tp) ||
    626 		   isspace((unsigned char)*tp)) {
    627 		if (++tp >= buf_end)
    628 		    fill_buffer();
    629 	    }
    630 	    if (*tp == '(')
    631 		ps.procname[0] = ' ';
    632 	}
    633 	code = unary_op;
    634 	break;
    635 
    636     default:
    637 	if (token[0] == '/' && (*buf_ptr == '*' || *buf_ptr == '/')) {
    638 	    /* it is start of comment */
    639 	    *e_token++ = *buf_ptr;
    640 
    641 	    if (++buf_ptr >= buf_end)
    642 		fill_buffer();
    643 
    644 	    code = comment;
    645 	    unary_delim = state->last_u_d;
    646 	    break;
    647 	}
    648 	while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
    649 	    /*
    650 	     * handle ||, &&, etc, and also things as in int *****i
    651 	     */
    652 	    check_size_token(1);
    653 	    *e_token++ = *buf_ptr;
    654 	    if (++buf_ptr >= buf_end)
    655 		fill_buffer();
    656 	}
    657 	code = (state->last_u_d ? unary_op : binary_op);
    658 	unary_delim = true;
    659 
    660 
    661     }				/* end of switch */
    662     if (buf_ptr >= buf_end)	/* check for input buffer empty */
    663 	fill_buffer();
    664     state->last_u_d = unary_delim;
    665     check_size_token(1);
    666     *e_token = '\0';		/* null terminate the token */
    667     return lexi_end(code);
    668 }
    669 
    670 /* Initialize constant transition table */
    671 void
    672 init_constant_tt(void)
    673 {
    674     table['-'] = table['+'];
    675     table['8'] = table['9'];
    676     table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
    677     table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
    678     table['B'] = table['b'];
    679     table['E'] = table['e'];
    680     table['U'] = table['u'];
    681     table['X'] = table['x'];
    682     table['P'] = table['p'];
    683     table['F'] = table['f'];
    684 }
    685 
    686 void
    687 alloc_typenames(void)
    688 {
    689 
    690     typenames = (const char **)malloc(sizeof(typenames[0]) *
    691         (typename_count = 16));
    692     if (typenames == NULL)
    693 	err(1, NULL);
    694 }
    695 
    696 void
    697 add_typename(const char *key)
    698 {
    699     int comparison;
    700     const char *copy;
    701 
    702     if (typename_top + 1 >= typename_count) {
    703 	typenames = realloc((void *)typenames,
    704 	    sizeof(typenames[0]) * (typename_count *= 2));
    705 	if (typenames == NULL)
    706 	    err(1, NULL);
    707     }
    708     if (typename_top == -1)
    709 	typenames[++typename_top] = copy = strdup(key);
    710     else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
    711 	/* take advantage of sorted input */
    712 	if (comparison == 0)	/* remove duplicates */
    713 	    return;
    714 	typenames[++typename_top] = copy = strdup(key);
    715     }
    716     else {
    717 	int p;
    718 
    719 	for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
    720 	    /* find place for the new key */;
    721 	if (comparison == 0)	/* remove duplicates */
    722 	    return;
    723 	memmove(&typenames[p + 1], &typenames[p],
    724 	    sizeof(typenames[0]) * (++typename_top - p));
    725 	typenames[p] = copy = strdup(key);
    726     }
    727 
    728     if (copy == NULL)
    729 	err(1, NULL);
    730 }
    731