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