8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / loadkeys / loadkeys.y
blob2795f1f7bfd43c2b3414c8b8cdb93fade7c113f0
1 %{
2 /*
3 * CDDL HEADER START
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License, Version 1.0 only
7 * (the "License"). You may not use this file except in compliance
8 * with the License.
10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing permissions
13 * and limitations under the License.
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * CDDL HEADER END
24 #ifndef lint
25 #pragma ident "%Z%%M% %I% %E% SMI"
26 #endif
29 * Copyright (c) 1999 by Sun Microsystems, Inc.
30 * All rights reserved.
33 #include <sys/param.h>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <search.h>
37 #include <string.h>
38 #include <malloc.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <sys/kbd.h>
44 #include <sys/kbio.h>
46 #define ALL -1 /* special symbol for all tables */
49 * SunOS 4.x and Solaris 2.[1234] put Type 4 key tables into
50 * the keytables directory with no type qualification.
51 * If we're a SPARC, we might be using an NFS server that
52 * doesn't have the new type-qualified directories.
53 * (loadkeys wasn't used on non-SPARCs in 2.[1234].)
55 #ifdef sparc
56 #define COMPATIBILITY_DIR
57 #endif
59 static char keytable_dir[] = "/usr/share/lib/keytables/type_%d/";
60 #ifdef COMPATIBILITY_DIR
61 static char keytable_dir2[] = "/usr/share/lib/keytables/";
62 #endif
63 static char layout_prefix[] = "layout_";
65 struct keyentry {
66 struct keyentry *ke_next;
67 struct kiockeymap ke_entry;
70 typedef struct keyentry keyentry;
72 static keyentry *firstentry;
73 static keyentry *lastentry;
75 struct dupentry {
76 struct dupentry *de_next;
77 int de_station;
78 int de_otherstation;
81 typedef struct dupentry dupentry;
83 static dupentry *firstduplicate;
84 static dupentry *lastduplicate;
86 static dupentry *firstswap;
87 static dupentry *lastswap;
89 static char *infilename;
90 static FILE *infile;
91 static int lineno;
92 static int begline;
94 static char *strings[16] = {
95 "\033[H", /* HOMEARROW */
96 "\033[A", /* UPARROW */
97 "\033[B", /* DOWNARROW */
98 "\033[D", /* LEFTARROW */
99 "\033[C", /* RIGHTARROW */
102 static int nstrings = 5; /* start out with 5 strings */
104 typedef enum {
105 SM_INVALID, /* this shift mask is invalid for this keyboard */
106 SM_NORMAL, /* "normal", valid shift mask */
107 SM_NUMLOCK, /* "Num Lock" shift mask */
108 SM_UP /* "Up" shift mask */
109 } smtype_t;
111 typedef struct {
112 int sm_mask;
113 smtype_t sm_type;
114 } smentry_t;
116 static smentry_t shiftmasks[] = {
117 { 0, SM_NORMAL },
118 { SHIFTMASK, SM_NORMAL },
119 { CAPSMASK, SM_NORMAL },
120 { CTRLMASK, SM_NORMAL },
121 { ALTGRAPHMASK, SM_NORMAL },
122 { NUMLOCKMASK, SM_NUMLOCK },
123 { UPMASK, SM_UP },
127 #define NSHIFTS (sizeof (shiftmasks) / sizeof (shiftmasks[0]))
129 static void enter_mapentry(int station, keyentry *entrylistp);
130 static keyentry *makeentry(int tablemask, int entry);
131 static int loadkey(int kbdfd, keyentry *kep);
132 static int dupkey(int kbdfd, dupentry *dep, int shiftmask);
133 static int swapkey(int kbdfd, dupentry *dep, int shiftmask);
134 static int yylex();
135 static int readesc(FILE *stream, int delim, int single_char);
136 static int wordcmp(const void *w1, const void *w2);
137 static int yyerror(char *msg);
138 static void usage(void);
139 static void set_layout(char *arg);
140 static FILE *open_mapping_file(char *pathbuf, char *name,
141 boolean_t explicit_name, int type);
144 main(argc, argv)
145 int argc;
146 char **argv;
148 register int kbdfd;
149 int type;
150 int layout;
151 /* maxint is 8 hex digits. */
152 char layout_filename[sizeof(layout_prefix)+8];
153 char pathbuf[MAXPATHLEN];
154 register int shift;
155 struct kiockeymap mapentry;
156 register keyentry *kep;
157 register dupentry *dep;
158 boolean_t explicit_name;
160 while(++argv, --argc) {
161 if(argv[0][0] != '-') break;
162 switch(argv[0][1]) {
163 case 'e':
164 /* -e obsolete, silently ignore */
165 break;
166 case 's':
167 if (argc != 2) {
168 usage();
169 /* NOTREACHED */
171 set_layout(argv[1]);
172 exit(0);
173 default:
174 usage();
175 /* NOTREACHED */
179 if (argc > 1) usage();
181 if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
182 /* perror("loadkeys: /dev/kbd"); */
183 return (1);
186 if (ioctl(kbdfd, KIOCTYPE, &type) < 0) {
188 * There may not be a keyboard connected,
189 * return silently
191 return (1);
194 if (argc == 0) {
195 /* If no keyboard detected, exit silently. */
196 if (type == -1)
197 return (0);
199 if (ioctl(kbdfd, KIOCLAYOUT, &layout) < 0) {
200 perror("loadkeys: ioctl(KIOCLAYOUT)");
201 return (1);
204 (void) sprintf(layout_filename,
205 "%s%.2x", layout_prefix, layout);
206 infilename = layout_filename;
207 explicit_name = B_FALSE;
208 } else {
209 infilename = argv[0];
210 explicit_name = B_TRUE;
213 infile = open_mapping_file(pathbuf, infilename, explicit_name, type);
214 if (infile == NULL) return (1);
216 infilename = pathbuf;
218 lineno = 0;
219 begline = 1;
220 yyparse();
221 fclose(infile);
224 * See which shift masks are valid for this keyboard.
225 * We do that by trying to get the entry for keystation 0 and that
226 * shift mask; if the "ioctl" fails, we assume it's because the shift
227 * mask is invalid.
229 for (shift = 0; shift < NSHIFTS; shift++) {
230 mapentry.kio_tablemask =
231 shiftmasks[shift].sm_mask;
232 mapentry.kio_station = 0;
233 if (ioctl(kbdfd, KIOCGKEY, &mapentry) < 0)
234 shiftmasks[shift].sm_type = SM_INVALID;
237 for (kep = firstentry; kep != NULL; kep = kep->ke_next) {
238 if (kep->ke_entry.kio_tablemask == ALL) {
239 for (shift = 0; shift < NSHIFTS; shift++) {
240 switch (shiftmasks[shift].sm_type) {
242 case SM_INVALID:
243 continue;
245 case SM_NUMLOCK:
247 * Defaults to NONL, not to a copy of
248 * the base entry.
250 if (kep->ke_entry.kio_entry != HOLE)
251 kep->ke_entry.kio_entry = NONL;
252 break;
254 case SM_UP:
256 * Defaults to NOP, not to a copy of
257 * the base entry.
259 if (kep->ke_entry.kio_entry != HOLE)
260 kep->ke_entry.kio_entry = NOP;
261 break;
263 kep->ke_entry.kio_tablemask =
264 shiftmasks[shift].sm_mask;
265 if (!loadkey(kbdfd, kep))
266 return (1);
268 } else {
269 if (!loadkey(kbdfd, kep))
270 return (1);
274 for (dep = firstswap; dep != NULL; dep = dep->de_next) {
275 for (shift = 0; shift < NSHIFTS; shift++) {
276 if (shiftmasks[shift].sm_type != SM_INVALID) {
277 if (!swapkey(kbdfd, dep,
278 shiftmasks[shift].sm_mask))
279 return (0);
284 for (dep = firstduplicate; dep != NULL; dep = dep->de_next) {
285 for (shift = 0; shift < NSHIFTS; shift++) {
286 if (shiftmasks[shift].sm_type != SM_INVALID) {
287 if (!dupkey(kbdfd, dep,
288 shiftmasks[shift].sm_mask))
289 return (0);
294 close(kbdfd);
295 return (0);
298 static void
299 usage()
301 (void) fprintf(stderr, "usage: loadkeys [ file ]\n");
302 exit(1);
305 static void
306 set_layout(char *arg)
308 int layout;
309 int ret;
310 int kbdfd;
312 layout = (int) strtol(arg, &arg, 0);
313 if (*arg != '\0') {
314 fprintf(stderr, "usage: loadkeys -s layoutnumber\n");
315 exit(1);
318 if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
319 perror("/dev/kbd");
320 exit(1);
323 ret = ioctl(kbdfd, KIOCSLAYOUT, layout);
324 if (ret == -1) {
325 perror("KIOCSLAYOUT");
328 close(kbdfd);
332 * Attempt to find the specified mapping file. Return a FILE * if found,
333 * else print a message on stderr and return NULL.
335 FILE *
336 open_mapping_file(
337 char *pathbuf,
338 char *name,
339 boolean_t explicit_name,
340 int type
342 /* If the user specified the name, try it "raw". */
343 if (explicit_name) {
344 strcpy(pathbuf, name);
345 infile = fopen(pathbuf, "r");
346 if (infile) return (infile);
347 if (errno != ENOENT) goto fopen_fail;
350 /* Everything after this point applies only to relative names. */
351 if (*name == '/') goto fopen_fail;
353 /* Try the type-qualified directory name. */
354 sprintf(pathbuf, keytable_dir, type);
355 if ((int)(strlen(pathbuf) + strlen(name) + 1) >= MAXPATHLEN) {
356 (void) fprintf(stderr, "loadkeys: Name %s is too long\n",
357 name);
358 return (NULL);
360 (void) strcat(pathbuf, name);
361 infile = fopen(pathbuf, "r");
362 if (infile) return (infile);
363 if (errno != ENOENT) goto fopen_fail;
365 #ifdef COMPATIBILITY_DIR
366 /* If not, and either the name was specified explicitly */
367 /* or this is a type 4... */
368 if (explicit_name || type == KB_SUN4) {
369 /* Try the compatibility name. */
370 /* No need to check len here, it's shorter. */
371 (void) strcpy(pathbuf, keytable_dir2);
372 (void) strcat(pathbuf, infilename);
373 infile = fopen(pathbuf, "r");
374 if (infile) return (infile);
375 if (errno != ENOENT) goto fopen_fail;
377 #endif
379 fopen_fail:
380 (void) fprintf(stderr, "loadkeys: ");
381 perror(name);
382 return (NULL);
386 * We have a list of entries for a given keystation, and the keystation number
387 * for that keystation; put that keystation number into all the entries in that
388 * list, and chain that list to the end of the main list of entries.
390 static void
391 enter_mapentry(station, entrylistp)
392 int station;
393 keyentry *entrylistp;
395 register keyentry *kep;
397 if (lastentry == NULL)
398 firstentry = entrylistp;
399 else
400 lastentry->ke_next = entrylistp;
401 kep = entrylistp;
402 for (;;) {
403 kep->ke_entry.kio_station = (u_char)station;
404 if (kep->ke_next == NULL) {
405 lastentry = kep;
406 break;
408 kep = kep->ke_next;
413 * Allocate and fill in a new entry.
415 static keyentry *
416 makeentry(tablemask, entry)
417 int tablemask;
418 int entry;
420 register keyentry *kep;
421 register int index;
423 if ((kep = (keyentry *) malloc((unsigned)sizeof (keyentry))) == NULL)
424 yyerror("out of memory for entries");
425 kep->ke_next = NULL;
426 kep->ke_entry.kio_tablemask = tablemask;
427 kep->ke_entry.kio_station = 0;
428 kep->ke_entry.kio_entry = (u_short)entry;
429 index = entry - STRING;
430 if (index >= 0 && index <= 15)
431 (void) strncpy(kep->ke_entry.kio_string, strings[index],
432 KTAB_STRLEN);
433 return (kep);
437 * Make a set of entries for a keystation that indicate that that keystation's
438 * settings should be copied from another keystation's settings.
440 static void
441 duplicate_mapentry(station, otherstation)
442 int station;
443 int otherstation;
445 register dupentry *dep;
447 if ((dep = (dupentry *) malloc((unsigned)sizeof (dupentry))) == NULL)
448 yyerror("out of memory for entries");
450 if (lastduplicate == NULL)
451 firstduplicate = dep;
452 else
453 lastduplicate->de_next = dep;
454 lastduplicate = dep;
455 dep->de_next = NULL;
456 dep->de_station = station;
457 dep->de_otherstation = otherstation;
461 * Make a set of entries for a keystation that indicate that that keystation's
462 * settings should be swapped with another keystation's settings.
464 static void
465 swap_mapentry(station, otherstation)
466 int station;
467 int otherstation;
469 register dupentry *dep;
471 if ((dep = (dupentry *) malloc((unsigned)sizeof (dupentry))) == NULL)
472 yyerror("out of memory for entries");
474 if (lastswap == NULL)
475 firstswap = dep;
476 else
477 lastswap->de_next = dep;
478 lastswap = dep;
479 dep->de_next = NULL;
480 dep->de_station = station;
481 dep->de_otherstation = otherstation;
484 static int
485 loadkey(kbdfd, kep)
486 int kbdfd;
487 register keyentry *kep;
489 if (ioctl(kbdfd, KIOCSKEY, &kep->ke_entry) < 0) {
490 perror("loadkeys: ioctl(KIOCSKEY)");
491 return (0);
493 return (1);
496 static int
497 dupkey(kbdfd, dep, shiftmask)
498 int kbdfd;
499 register dupentry *dep;
500 int shiftmask;
502 struct kiockeymap entry;
504 entry.kio_tablemask = shiftmask;
505 entry.kio_station = dep->de_otherstation;
506 if (ioctl(kbdfd, KIOCGKEY, &entry) < 0) {
507 perror("loadkeys: ioctl(KIOCGKEY)");
508 return (0);
510 entry.kio_station = dep->de_station;
511 if (ioctl(kbdfd, KIOCSKEY, &entry) < 0) {
512 perror("loadkeys: ioctl(KIOCSKEY)");
513 return (0);
515 return (1);
520 static int
521 swapkey(kbdfd, dep, shiftmask)
522 int kbdfd;
523 register dupentry *dep;
524 int shiftmask;
526 struct kiockeymap entry1, entry2;
528 entry1.kio_tablemask = shiftmask;
529 entry1.kio_station = dep->de_station;
530 if (ioctl(kbdfd, KIOCGKEY, &entry1) < 0) {
531 perror("loadkeys: ioctl(KIOCGKEY)");
532 return (0);
534 entry2.kio_tablemask = shiftmask;
535 entry2.kio_station = dep->de_otherstation;
536 if (ioctl(kbdfd, KIOCGKEY, &entry2) < 0) {
537 perror("loadkeys: ioctl(KIOCGKEY)");
538 return (0);
540 entry1.kio_station = dep->de_otherstation;
541 if (ioctl(kbdfd, KIOCSKEY, &entry1) < 0) {
542 perror("loadkeys: ioctl(KIOCSKEY)");
543 return (0);
545 entry2.kio_station = dep->de_station;
546 if (ioctl(kbdfd, KIOCSKEY, &entry2) < 0) {
547 perror("loadkeys: ioctl(KIOCSKEY)");
548 return (0);
550 return (1);
554 %term TABLENAME INT CHAR CHARSTRING CONSTANT FKEY KEY SAME AS SWAP WITH
556 %union {
557 keyentry *keyentry;
558 int number;
561 %type <keyentry> entrylist entry
562 %type <number> CHARSTRING CHAR INT CONSTANT FKEY TABLENAME
563 %type <number> code expr term number
567 table:
568 table line
569 | /* null */
572 line:
573 KEY number entrylist '\n'
575 enter_mapentry($2, $3);
577 | KEY number SAME AS number '\n'
579 duplicate_mapentry($2, $5);
581 | SWAP number WITH number '\n'
583 swap_mapentry($2, $4);
585 | '\n'
588 entrylist:
589 entrylist entry
592 * Append this entry to the end of the entry list.
594 register keyentry *kep;
595 kep = $1;
596 for (;;) {
597 if (kep->ke_next == NULL) {
598 kep->ke_next = $2;
599 break;
601 kep = kep->ke_next;
603 $$ = $1;
605 | entry
607 $$ = $1;
611 entry:
612 TABLENAME code
614 $$ = makeentry($1, $2);
618 code:
619 CHARSTRING
621 $$ = $1;
623 | CHAR
625 $$ = $1;
627 | '('
629 $$ = '(';
631 | ')'
633 $$ = ')';
635 | '+'
637 $$ = '+';
639 | expr
641 $$ = $1;
645 expr:
646 term
648 $$ = $1;
650 | expr '+' term
652 $$ = $1 + $3;
656 term:
657 CONSTANT
659 $$ = $1;
661 | FKEY '(' number ')'
663 if ($3 < 1 || $3 > 16)
664 yyerror("invalid function key number");
665 $$ = $1 + $3 - 1;
669 number:
672 $$ = $1;
674 | CHAR
676 if (isdigit($1))
677 $$ = $1 - '0';
678 else
679 yyerror("syntax error");
685 typedef struct {
686 char *w_string;
687 int w_type; /* token type */
688 int w_lval; /* yylval for this token */
689 } word_t;
692 * Table must be in alphabetical order.
694 word_t wordtab[] = {
695 { "all", TABLENAME, ALL },
696 { "alt", CONSTANT, ALT },
697 { "altg", TABLENAME, ALTGRAPHMASK },
698 { "altgraph", CONSTANT, ALTGRAPH },
699 { "as", AS, 0 },
700 { "base", TABLENAME, 0 },
701 { "bf", FKEY, BOTTOMFUNC },
702 { "buckybits", CONSTANT, BUCKYBITS },
703 { "caps", TABLENAME, CAPSMASK },
704 { "capslock", CONSTANT, CAPSLOCK },
705 { "compose", CONSTANT, COMPOSE },
706 { "ctrl", TABLENAME, CTRLMASK },
707 { "downarrow", CONSTANT, DOWNARROW },
708 { "error", CONSTANT, ERROR },
709 { "fa_acute", CONSTANT, FA_ACUTE },
710 { "fa_cedilla", CONSTANT, FA_CEDILLA },
711 { "fa_cflex", CONSTANT, FA_CFLEX },
712 { "fa_grave", CONSTANT, FA_GRAVE },
713 { "fa_tilde", CONSTANT, FA_TILDE },
714 { "fa_umlaut", CONSTANT, FA_UMLAUT },
715 { "hole", CONSTANT, HOLE },
716 { "homearrow", CONSTANT, HOMEARROW },
717 { "idle", CONSTANT, IDLE },
718 { "key", KEY, 0 },
719 { "leftarrow", CONSTANT, LEFTARROW },
720 { "leftctrl", CONSTANT, LEFTCTRL },
721 { "leftshift", CONSTANT, LEFTSHIFT },
722 { "lf", FKEY, LEFTFUNC },
723 { "metabit", CONSTANT, METABIT },
724 { "nonl", CONSTANT, NONL },
725 { "nop", CONSTANT, NOP },
726 { "numl", TABLENAME, NUMLOCKMASK },
727 { "numlock", CONSTANT, NUMLOCK },
728 { "oops", CONSTANT, OOPS },
729 { "pad0", CONSTANT, PAD0 },
730 { "pad1", CONSTANT, PAD1 },
731 { "pad2", CONSTANT, PAD2 },
732 { "pad3", CONSTANT, PAD3 },
733 { "pad4", CONSTANT, PAD4 },
734 { "pad5", CONSTANT, PAD5 },
735 { "pad6", CONSTANT, PAD6 },
736 { "pad7", CONSTANT, PAD7 },
737 { "pad8", CONSTANT, PAD8 },
738 { "pad9", CONSTANT, PAD9 },
739 { "paddot", CONSTANT, PADDOT },
740 { "padenter", CONSTANT, PADENTER },
741 { "padequal", CONSTANT, PADEQUAL },
742 { "padminus", CONSTANT, PADMINUS },
743 { "padplus", CONSTANT, PADPLUS },
744 { "padsep", CONSTANT, PADSEP },
745 { "padslash", CONSTANT, PADSLASH },
746 { "padstar", CONSTANT, PADSTAR },
747 { "reset", CONSTANT, RESET },
748 { "rf", FKEY, RIGHTFUNC },
749 { "rightarrow", CONSTANT, RIGHTARROW },
750 { "rightctrl", CONSTANT, RIGHTCTRL },
751 { "rightshift", CONSTANT, RIGHTSHIFT },
752 { "same", SAME, 0 },
753 { "shift", TABLENAME, SHIFTMASK },
754 { "shiftkeys", CONSTANT, SHIFTKEYS },
755 { "shiftlock", CONSTANT, SHIFTLOCK },
756 { "string", CONSTANT, STRING },
757 { "swap", SWAP, 0 },
758 { "systembit", CONSTANT, SYSTEMBIT },
759 { "tf", FKEY, TOPFUNC },
760 { "up", TABLENAME, UPMASK },
761 { "uparrow", CONSTANT, UPARROW },
762 { "with", WITH, 0 },
765 #define NWORDS (sizeof (wordtab) / sizeof (wordtab[0]))
767 static int
768 yylex()
770 register int c;
771 char tokbuf[256+1];
772 register char *cp;
773 register int tokentype;
775 while ((c = getc(infile)) == ' ' || c == '\t')
777 if (begline) {
778 lineno++;
779 begline = 0;
780 if (c == '#') {
781 while ((c = getc(infile)) != EOF && c != '\n')
785 if (c == EOF)
786 return (0); /* end marker */
787 if (c == '\n') {
788 begline = 1;
789 return (c);
792 switch (c) {
794 case '\'':
795 tokentype = CHAR;
796 if ((c = getc(infile)) == EOF)
797 yyerror("unterminated character constant");
798 if (c == '\n') {
799 (void) ungetc(c, infile);
800 yylval.number = '\'';
801 } else {
802 switch (c) {
804 case '\'':
805 yyerror("null character constant");
806 break;
808 case '\\':
809 yylval.number = readesc(infile, '\'', 1);
810 break;
812 default:
813 yylval.number = c;
814 break;
816 if ((c = getc(infile)) == EOF || c == '\n')
817 yyerror("unterminated character constant");
818 else if (c != '\'')
819 yyerror("only one character allowed in character constant");
821 break;
823 case '"':
824 if ((c = getc(infile)) == EOF)
825 yyerror("unterminated string constant");
826 if (c == '\n') {
827 (void) ungetc(c, infile);
828 tokentype = CHAR;
829 yylval.number = '"';
830 } else {
831 tokentype = CHARSTRING;
832 cp = &tokbuf[0];
833 do {
834 if (cp > &tokbuf[256])
835 yyerror("line too long");
836 if (c == '\\')
837 c = readesc(infile, '"', 0);
838 *cp++ = (char)c;
839 } while ((c = getc(infile)) != EOF && c != '\n' &&
840 c != '"');
841 if (c != '"')
842 yyerror("unterminated string constant");
843 *cp = '\0';
844 if (nstrings == 16)
845 yyerror("too many strings");
846 if ((int) strlen(tokbuf) > KTAB_STRLEN)
847 yyerror("string too long");
848 strings[nstrings] = strdup(tokbuf);
849 yylval.number = STRING+nstrings;
850 nstrings++;
852 break;
854 case '(':
855 case ')':
856 case '+':
857 tokentype = c;
858 break;
860 case '^':
861 if ((c = getc(infile)) == EOF)
862 yyerror("missing newline at end of line");
863 tokentype = CHAR;
864 if (c == ' ' || c == '\t' || c == '\n') {
866 * '^' by itself.
868 yylval.number = '^';
869 } else {
870 yylval.number = c & 037;
871 if ((c = getc(infile)) == EOF)
872 yyerror("missing newline at end of line");
873 if (c != ' ' && c != '\t' && c != '\n')
874 yyerror("invalid control character");
876 (void) ungetc(c, infile);
877 break;
879 default:
880 cp = &tokbuf[0];
881 do {
882 if (cp > &tokbuf[256])
883 yyerror("line too long");
884 *cp++ = (char)c;
885 } while ((c = getc(infile)) != EOF && (isalnum(c) || c == '_'));
886 if (c == EOF)
887 yyerror("newline missing");
888 (void) ungetc(c, infile);
889 *cp = '\0';
890 if (strlen(tokbuf) == 1) {
891 tokentype = CHAR;
892 yylval.number = (unsigned char)tokbuf[0];
893 } else if (strlen(tokbuf) == 2 && tokbuf[0] == '^') {
894 tokentype = CHAR;
895 yylval.number = (unsigned char)(tokbuf[1] & 037);
896 } else {
897 word_t word;
898 register word_t *wptr;
899 char *ptr;
901 for (cp = &tokbuf[0]; (c = *cp) != '\0'; cp++) {
902 if (isupper(c))
903 *cp = tolower(c);
905 word.w_string = tokbuf;
906 wptr = (word_t *)bsearch((char *)&word,
907 (char *)wordtab, NWORDS, sizeof (word_t),
908 wordcmp);
909 if (wptr != NULL) {
910 yylval.number = wptr->w_lval;
911 tokentype = wptr->w_type;
912 } else {
913 yylval.number = strtol(tokbuf, &ptr, 10);
914 if (ptr == tokbuf)
915 yyerror("syntax error");
916 else
917 tokentype = INT;
919 break;
923 return (tokentype);
926 static int
927 readesc(stream, delim, single_char)
928 FILE *stream;
929 int delim;
930 int single_char;
932 register int c;
933 register int val;
934 register int i;
936 if ((c = getc(stream)) == EOF || c == '\n')
937 yyerror("unterminated character constant");
939 if (c >= '0' && c <= '7') {
940 val = 0;
941 i = 1;
942 for (;;) {
943 val = val*8 + c - '0';
944 if ((c = getc(stream)) == EOF || c == '\n')
945 yyerror("unterminated character constant");
946 if (c == delim)
947 break;
948 i++;
949 if (i > 3) {
950 if (single_char)
951 yyerror("escape sequence too long");
952 else
953 break;
955 if (c < '0' || c > '7') {
956 if (single_char)
957 yyerror("illegal character in escape sequence");
958 else
959 break;
962 (void) ungetc(c, stream);
963 } else {
964 switch (c) {
966 case 'n':
967 val = '\n';
968 break;
970 case 't':
971 val = '\t';
972 break;
974 case 'b':
975 val = '\b';
976 break;
978 case 'r':
979 val = '\r';
980 break;
982 case 'v':
983 val = '\v';
984 break;
986 case '\\':
987 val = '\\';
988 break;
990 default:
991 if (c == delim)
992 val = delim;
993 else
994 yyerror("illegal character in escape sequence");
997 return (val);
1000 static int
1001 wordcmp(const void *w1, const void *w2)
1003 return (strcmp(
1004 ((const word_t *)w1)->w_string,
1005 ((const word_t *)w2)->w_string));
1008 static int
1009 yyerror(msg)
1010 char *msg;
1012 (void) fprintf(stderr, "%s, line %d: %s\n", infilename, lineno, msg);
1013 exit(1);