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