Home | History | Annotate | Line # | Download | only in indent
lexi.c revision 1.22
      1 /*	$NetBSD: lexi.c,v 1.22 2021/03/07 20:40:18 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.22 2021/03/07 20:40:18 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 int
    175 strcmp_type(const void *e1, const void *e2)
    176 {
    177     return (strcmp(e1, *(const char * const *)e2));
    178 }
    179 
    180 #ifdef debug
    181 const char *
    182 token_type_name(token_type tk)
    183 {
    184     static const char *const name[] = {
    185 	"end_of_file", "newline", "lparen", "rparen", "unary_op",
    186 	"binary_op", "postop", "question", "casestmt", "colon",
    187 	"semicolon", "lbrace", "rbrace", "ident", "comma",
    188 	"comment", "swstmt", "preesc", "form_feed", "decl",
    189 	"sp_paren", "sp_nparen", "ifstmt", "whilestmt", "forstmt",
    190 	"stmt", "stmtl", "elselit", "dolit", "dohead",
    191 	"ifhead", "elsehead", "period", "strpfx", "storage",
    192 	"funcname", "type_def", "structure"
    193     };
    194 
    195     assert(0 <= tk && tk < sizeof name / sizeof name[0]);
    196 
    197     return name[tk];
    198 }
    199 
    200 static void
    201 print_buf(const char *name, const char *s, const char *e)
    202 {
    203     if (s == e)
    204 	return;
    205 
    206     printf(" %s \"", name);
    207     for (const char *p = s; p < e; p++) {
    208 	if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
    209 	    printf("%c", *p);
    210 	else if (*p == '\n')
    211 	    printf("\\n");
    212 	else if (*p == '\t')
    213 	    printf("\\t");
    214 	else
    215 	    printf("\\x%02x", *p);
    216     }
    217     printf("\"");
    218 }
    219 
    220 static token_type
    221 lexi_end(token_type code)
    222 {
    223     printf("in line %d, lexi returns '%s'", line_no, token_type_name(code));
    224     print_buf("token", s_token, e_token);
    225     print_buf("label", s_lab, e_lab);
    226     print_buf("code", s_code, e_code);
    227     print_buf("comment", s_com, e_com);
    228     printf("\n");
    229 
    230     return code;
    231 }
    232 #else
    233 #  define lexi_end(tk) (tk)
    234 #endif
    235 
    236 token_type
    237 lexi(struct parser_state *state)
    238 {
    239     int         unary_delim;	/* this is set to 1 if the current token
    240 				 * forces a following operator to be unary */
    241     token_type  code;		/* internal code to be returned */
    242     char        qchar;		/* the delimiter character for a string */
    243 
    244     e_token = s_token;		/* point to start of place to save token */
    245     unary_delim = false;
    246     state->col_1 = state->last_nl;	/* tell world that this token started
    247 					 * in column 1 iff the last thing
    248 					 * scanned was a newline */
    249     state->last_nl = false;
    250 
    251     while (*buf_ptr == ' ' || *buf_ptr == '\t') {	/* get rid of blanks */
    252 	state->col_1 = false;	/* leading blanks imply token is not in column
    253 				 * 1 */
    254 	if (++buf_ptr >= buf_end)
    255 	    fill_buffer();
    256     }
    257 
    258     /* Scan an alphanumeric token */
    259     if (isalnum((unsigned char)*buf_ptr) ||
    260 	*buf_ptr == '_' || *buf_ptr == '$' ||
    261 	(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
    262 	/*
    263 	 * we have a character or number
    264 	 */
    265 	struct templ *p;
    266 
    267 	if (isdigit((unsigned char)*buf_ptr) ||
    268 	    (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
    269 	    char s;
    270 	    unsigned char i;
    271 
    272 	    for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
    273 		i = (unsigned char)*buf_ptr;
    274 		if (i >= nitems(table) || table[i] == NULL ||
    275 		    table[i][s - 'A'] == ' ') {
    276 		    s = table[0][s - 'A'];
    277 		    break;
    278 		}
    279 		s = table[i][s - 'A'];
    280 		CHECK_SIZE_TOKEN(1);
    281 		*e_token++ = *buf_ptr++;
    282 		if (buf_ptr >= buf_end)
    283 		    fill_buffer();
    284 	    }
    285 	    /* s now indicates the type: f(loating), i(integer), u(nknown) */
    286 	}
    287 	else
    288 	    while (isalnum((unsigned char)*buf_ptr) ||
    289 	        *buf_ptr == BACKSLASH ||
    290 		*buf_ptr == '_' || *buf_ptr == '$') {
    291 		/* fill_buffer() terminates buffer with newline */
    292 		if (*buf_ptr == BACKSLASH) {
    293 		    if (*(buf_ptr + 1) == '\n') {
    294 			buf_ptr += 2;
    295 			if (buf_ptr >= buf_end)
    296 			    fill_buffer();
    297 			} else
    298 			    break;
    299 		}
    300 		CHECK_SIZE_TOKEN(1);
    301 		/* copy it over */
    302 		*e_token++ = *buf_ptr++;
    303 		if (buf_ptr >= buf_end)
    304 		    fill_buffer();
    305 	    }
    306 	*e_token = '\0';
    307 
    308 	if (s_token[0] == 'L' && s_token[1] == '\0' &&
    309 	      (*buf_ptr == '"' || *buf_ptr == '\''))
    310 	    return lexi_end(strpfx);
    311 
    312 	while (*buf_ptr == ' ' || *buf_ptr == '\t') {	/* get rid of blanks */
    313 	    if (++buf_ptr >= buf_end)
    314 		fill_buffer();
    315 	}
    316 	state->keyword = rw_0;
    317 	if (state->last_token == structure && !state->p_l_follow) {
    318 				/* if last token was 'struct' and we're not
    319 				 * in parentheses, then this token
    320 				 * should be treated as a declaration */
    321 	    state->last_u_d = true;
    322 	    return lexi_end(decl);
    323 	}
    324 	/*
    325 	 * Operator after identifier is binary unless last token was 'struct'
    326 	 */
    327 	state->last_u_d = (state->last_token == structure);
    328 
    329 	p = bsearch(s_token,
    330 	    specials,
    331 	    sizeof(specials) / sizeof(specials[0]),
    332 	    sizeof(specials[0]),
    333 	    strcmp_type);
    334 	if (p == NULL) {	/* not a special keyword... */
    335 	    char *u;
    336 
    337 	    /* ... so maybe a type_t or a typedef */
    338 	    if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
    339 	        strcmp(u, "_t") == 0) || (typename_top >= 0 &&
    340 		  bsearch(s_token, typenames, typename_top + 1,
    341 		    sizeof(typenames[0]), strcmp_type))) {
    342 		state->keyword = rw_type;
    343 		state->last_u_d = true;
    344 	        goto found_typename;
    345 	    }
    346 	} else {			/* we have a keyword */
    347 	    state->keyword = p->rwcode;
    348 	    state->last_u_d = true;
    349 	    switch (p->rwcode) {
    350 	    case rw_switch:
    351 		return lexi_end(swstmt);
    352 	    case rw_case_or_default:
    353 		return lexi_end(casestmt);
    354 	    case rw_struct_or_union_or_enum:
    355 	    case rw_type:
    356 	    found_typename:
    357 		if (state->p_l_follow) {
    358 		    /* inside parens: cast, param list, offsetof or sizeof */
    359 		    state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
    360 		}
    361 		if (state->last_token == period || state->last_token == unary_op) {
    362 		    state->keyword = rw_0;
    363 		    break;
    364 		}
    365 		if (p != NULL && p->rwcode == rw_struct_or_union_or_enum)
    366 		    return lexi_end(structure);
    367 		if (state->p_l_follow)
    368 		    break;
    369 		return lexi_end(decl);
    370 
    371 	    case rw_for_or_if_or_while:
    372 		return lexi_end(sp_paren);
    373 
    374 	    case rw_do_or_else:
    375 		return lexi_end(sp_nparen);
    376 
    377 	    case rw_storage_class:
    378 		return lexi_end(storage);
    379 
    380 	    case rw_typedef:
    381 		return lexi_end(type_def);
    382 
    383 	    default:		/* all others are treated like any other
    384 				 * identifier */
    385 		return lexi_end(ident);
    386 	    }			/* end of switch */
    387 	}			/* end of if (found_it) */
    388 	if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
    389 	    state->in_parameter_declaration == 0 && state->block_init == 0) {
    390 	    char *tp = buf_ptr;
    391 	    while (tp < buf_end)
    392 		if (*tp++ == ')' && (*tp == ';' || *tp == ','))
    393 		    goto not_proc;
    394 	    strncpy(state->procname, token, sizeof state->procname - 1);
    395 	    if (state->in_decl)
    396 		state->in_parameter_declaration = 1;
    397 	    return lexi_end(funcname);
    398     not_proc:;
    399 	}
    400 	/*
    401 	 * The following hack attempts to guess whether or not the current
    402 	 * token is in fact a declaration keyword -- one that has been
    403 	 * typedefd
    404 	 */
    405 	else if (!state->p_l_follow && !state->block_init &&
    406 	    !state->in_stmt &&
    407 	    ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
    408 		isalpha((unsigned char)*buf_ptr)) &&
    409 	    (state->last_token == semicolon || state->last_token == lbrace ||
    410 		state->last_token == rbrace)) {
    411 	    state->keyword = rw_type;
    412 	    state->last_u_d = true;
    413 	    return lexi_end(decl);
    414 	}
    415 	if (state->last_token == decl)	/* if this is a declared variable,
    416 					 * then following sign is unary */
    417 	    state->last_u_d = true;	/* will make "int a -1" work */
    418 	return lexi_end(ident);		/* the ident is not in the list */
    419     }				/* end of procesing for alpanum character */
    420 
    421     /* Scan a non-alphanumeric token */
    422 
    423     CHECK_SIZE_TOKEN(3);		/* things like "<<=" */
    424     *e_token++ = *buf_ptr;		/* if it is only a one-character token, it is
    425 				 * moved here */
    426     *e_token = '\0';
    427     if (++buf_ptr >= buf_end)
    428 	fill_buffer();
    429 
    430     switch (*token) {
    431     case '\n':
    432 	unary_delim = state->last_u_d;
    433 	state->last_nl = true;	/* remember that we just had a newline */
    434 	code = (had_eof ? end_of_file : newline);
    435 
    436 	/*
    437 	 * if data has been exhausted, the newline is a dummy, and we should
    438 	 * return code to stop
    439 	 */
    440 	break;
    441 
    442     case '\'':			/* start of quoted character */
    443     case '"':			/* start of string */
    444 	qchar = *token;
    445 	do {			/* copy the string */
    446 	    while (1) {		/* move one character or [/<char>]<char> */
    447 		if (*buf_ptr == '\n') {
    448 		    diag(1, "Unterminated literal");
    449 		    goto stop_lit;
    450 		}
    451 		CHECK_SIZE_TOKEN(2);
    452 		*e_token = *buf_ptr++;
    453 		if (buf_ptr >= buf_end)
    454 		    fill_buffer();
    455 		if (*e_token == BACKSLASH) {	/* if escape, copy extra char */
    456 		    if (*buf_ptr == '\n')	/* check for escaped newline */
    457 			++line_no;
    458 		    *++e_token = *buf_ptr++;
    459 		    ++e_token;	/* we must increment this again because we
    460 				 * copied two chars */
    461 		    if (buf_ptr >= buf_end)
    462 			fill_buffer();
    463 		}
    464 		else
    465 		    break;	/* we copied one character */
    466 	    }			/* end of while (1) */
    467 	} while (*e_token++ != qchar);
    468 stop_lit:
    469 	code = ident;
    470 	break;
    471 
    472     case ('('):
    473     case ('['):
    474 	unary_delim = true;
    475 	code = lparen;
    476 	break;
    477 
    478     case (')'):
    479     case (']'):
    480 	code = rparen;
    481 	break;
    482 
    483     case '#':
    484 	unary_delim = state->last_u_d;
    485 	code = preesc;
    486 	break;
    487 
    488     case '?':
    489 	unary_delim = true;
    490 	code = question;
    491 	break;
    492 
    493     case (':'):
    494 	code = colon;
    495 	unary_delim = true;
    496 	break;
    497 
    498     case (';'):
    499 	unary_delim = true;
    500 	code = semicolon;
    501 	break;
    502 
    503     case ('{'):
    504 	unary_delim = true;
    505 
    506 	/*
    507 	 * if (state->in_or_st) state->block_init = 1;
    508 	 */
    509 	/* ?	code = state->block_init ? lparen : lbrace; */
    510 	code = lbrace;
    511 	break;
    512 
    513     case ('}'):
    514 	unary_delim = true;
    515 	/* ?	code = state->block_init ? rparen : rbrace; */
    516 	code = rbrace;
    517 	break;
    518 
    519     case 014:			/* a form feed */
    520 	unary_delim = state->last_u_d;
    521 	state->last_nl = true;	/* remember this so we can set 'state->col_1'
    522 				 * right */
    523 	code = form_feed;
    524 	break;
    525 
    526     case (','):
    527 	unary_delim = true;
    528 	code = comma;
    529 	break;
    530 
    531     case '.':
    532 	unary_delim = false;
    533 	code = period;
    534 	break;
    535 
    536     case '-':
    537     case '+':			/* check for -, +, --, ++ */
    538 	code = (state->last_u_d ? unary_op : binary_op);
    539 	unary_delim = true;
    540 
    541 	if (*buf_ptr == token[0]) {
    542 	    /* check for doubled character */
    543 	    *e_token++ = *buf_ptr++;
    544 	    /* buffer overflow will be checked at end of loop */
    545 	    if (state->last_token == ident || state->last_token == rparen) {
    546 		code = (state->last_u_d ? unary_op : postop);
    547 		/* check for following ++ or -- */
    548 		unary_delim = false;
    549 	    }
    550 	}
    551 	else if (*buf_ptr == '=')
    552 	    /* check for operator += */
    553 	    *e_token++ = *buf_ptr++;
    554 	else if (*buf_ptr == '>') {
    555 	    /* check for operator -> */
    556 	    *e_token++ = *buf_ptr++;
    557 	    unary_delim = false;
    558 	    code = unary_op;
    559 	    state->want_blank = false;
    560 	}
    561 	break;			/* buffer overflow will be checked at end of
    562 				 * switch */
    563 
    564     case '=':
    565 	if (state->in_or_st)
    566 	    state->block_init = 1;
    567 	if (*buf_ptr == '=') {/* == */
    568 	    *e_token++ = '=';	/* Flip =+ to += */
    569 	    buf_ptr++;
    570 	    *e_token = 0;
    571 	}
    572 	code = binary_op;
    573 	unary_delim = true;
    574 	break;
    575 	/* can drop thru!!! */
    576 
    577     case '>':
    578     case '<':
    579     case '!':			/* ops like <, <<, <=, !=, etc */
    580 	if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
    581 	    *e_token++ = *buf_ptr;
    582 	    if (++buf_ptr >= buf_end)
    583 		fill_buffer();
    584 	}
    585 	if (*buf_ptr == '=')
    586 	    *e_token++ = *buf_ptr++;
    587 	code = (state->last_u_d ? unary_op : binary_op);
    588 	unary_delim = true;
    589 	break;
    590 
    591     case '*':
    592 	unary_delim = true;
    593 	if (!state->last_u_d) {
    594 	    if (*buf_ptr == '=')
    595 		*e_token++ = *buf_ptr++;
    596 	    code = binary_op;
    597 	    break;
    598 	}
    599 	while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
    600 	    if (*buf_ptr == '*') {
    601 		CHECK_SIZE_TOKEN(1);
    602 		*e_token++ = *buf_ptr;
    603 	    }
    604 	    if (++buf_ptr >= buf_end)
    605 		fill_buffer();
    606 	}
    607 	if (ps.in_decl) {
    608 	    char *tp = buf_ptr;
    609 
    610 	    while (isalpha((unsigned char)*tp) ||
    611 		   isspace((unsigned char)*tp)) {
    612 		if (++tp >= buf_end)
    613 		    fill_buffer();
    614 	    }
    615 	    if (*tp == '(')
    616 		ps.procname[0] = ' ';
    617 	}
    618 	code = unary_op;
    619 	break;
    620 
    621     default:
    622 	if (token[0] == '/' && *buf_ptr == '*') {
    623 	    /* it is start of comment */
    624 	    *e_token++ = '*';
    625 
    626 	    if (++buf_ptr >= buf_end)
    627 		fill_buffer();
    628 
    629 	    code = comment;
    630 	    unary_delim = state->last_u_d;
    631 	    break;
    632 	}
    633 	while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
    634 	    /*
    635 	     * handle ||, &&, etc, and also things as in int *****i
    636 	     */
    637 	    CHECK_SIZE_TOKEN(1);
    638 	    *e_token++ = *buf_ptr;
    639 	    if (++buf_ptr >= buf_end)
    640 		fill_buffer();
    641 	}
    642 	code = (state->last_u_d ? unary_op : binary_op);
    643 	unary_delim = true;
    644 
    645 
    646     }				/* end of switch */
    647     if (buf_ptr >= buf_end)	/* check for input buffer empty */
    648 	fill_buffer();
    649     state->last_u_d = unary_delim;
    650     CHECK_SIZE_TOKEN(1);
    651     *e_token = '\0';		/* null terminate the token */
    652     return lexi_end(code);
    653 }
    654 
    655 /* Initialize constant transition table */
    656 void
    657 init_constant_tt(void)
    658 {
    659     table['-'] = table['+'];
    660     table['8'] = table['9'];
    661     table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
    662     table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
    663     table['B'] = table['b'];
    664     table['E'] = table['e'];
    665     table['U'] = table['u'];
    666     table['X'] = table['x'];
    667     table['P'] = table['p'];
    668     table['F'] = table['f'];
    669 }
    670 
    671 void
    672 alloc_typenames(void)
    673 {
    674 
    675     typenames = (const char **)malloc(sizeof(typenames[0]) *
    676         (typename_count = 16));
    677     if (typenames == NULL)
    678 	err(1, NULL);
    679 }
    680 
    681 void
    682 add_typename(const char *key)
    683 {
    684     int comparison;
    685     const char *copy;
    686 
    687     if (typename_top + 1 >= typename_count) {
    688 	typenames = realloc((void *)typenames,
    689 	    sizeof(typenames[0]) * (typename_count *= 2));
    690 	if (typenames == NULL)
    691 	    err(1, NULL);
    692     }
    693     if (typename_top == -1)
    694 	typenames[++typename_top] = copy = strdup(key);
    695     else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
    696 	/* take advantage of sorted input */
    697 	if (comparison == 0)	/* remove duplicates */
    698 	    return;
    699 	typenames[++typename_top] = copy = strdup(key);
    700     }
    701     else {
    702 	int p;
    703 
    704 	for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
    705 	    /* find place for the new key */;
    706 	if (comparison == 0)	/* remove duplicates */
    707 	    return;
    708 	memmove(&typenames[p + 1], &typenames[p],
    709 	    sizeof(typenames[0]) * (++typename_top - p));
    710 	typenames[p] = copy = strdup(key);
    711     }
    712 
    713     if (copy == NULL)
    714 	err(1, NULL);
    715 }
    716