Home | History | Annotate | Line # | Download | only in rpcgen
rpc_scan.c revision 1.14
      1  1.14  dholland /*	$NetBSD: rpc_scan.c,v 1.14 2015/05/09 23:12:57 dholland Exp $	*/
      2   1.1     glass /*
      3   1.1     glass  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      4   1.1     glass  * unrestricted use provided that this legend is included on all tape
      5   1.1     glass  * media and as a part of the software program in whole or part.  Users
      6   1.1     glass  * may copy or modify Sun RPC without charge, but are not authorized
      7   1.1     glass  * to license or distribute it to anyone else except as part of a product or
      8   1.4        pk  * program developed by the user or with the express written consent of
      9   1.4        pk  * Sun Microsystems, Inc.
     10   1.4        pk  *
     11   1.1     glass  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12   1.1     glass  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13   1.1     glass  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14   1.4        pk  *
     15   1.1     glass  * Sun RPC is provided with no support and without any obligation on the
     16   1.1     glass  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17   1.1     glass  * modification or enhancement.
     18   1.4        pk  *
     19   1.1     glass  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20   1.1     glass  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21   1.1     glass  * OR ANY PART THEREOF.
     22   1.4        pk  *
     23   1.1     glass  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24   1.1     glass  * or profits or other special, indirect and consequential damages, even if
     25   1.1     glass  * Sun has been advised of the possibility of such damages.
     26   1.4        pk  *
     27   1.1     glass  * Sun Microsystems, Inc.
     28   1.1     glass  * 2550 Garcia Avenue
     29   1.1     glass  * Mountain View, California  94043
     30   1.1     glass  */
     31   1.4        pk 
     32   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
     33   1.9       jmc #include "nbtool_config.h"
     34   1.9       jmc #endif
     35   1.9       jmc 
     36   1.5  christos #include <sys/cdefs.h>
     37   1.8        tv #if defined(__RCSID) && !defined(lint)
     38   1.5  christos #if 0
     39   1.4        pk static char sccsid[] = "@(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI";
     40   1.5  christos #else
     41  1.14  dholland __RCSID("$NetBSD: rpc_scan.c,v 1.14 2015/05/09 23:12:57 dholland Exp $");
     42   1.5  christos #endif
     43   1.1     glass #endif
     44   1.1     glass 
     45   1.1     glass /*
     46   1.6     lukem  * rpc_scan.c, Scanner for the RPC protocol compiler
     47   1.6     lukem  * Copyright (C) 1987, Sun Microsystems, Inc.
     48   1.1     glass  */
     49   1.4        pk #include <stdlib.h>
     50   1.1     glass #include <stdio.h>
     51   1.1     glass #include <ctype.h>
     52   1.4        pk #include <string.h>
     53   1.1     glass #include "rpc_scan.h"
     54   1.4        pk #include "rpc_parse.h"
     55   1.1     glass #include "rpc_util.h"
     56   1.1     glass 
     57   1.1     glass #define startcomment(where) (where[0] == '/' && where[1] == '*')
     58   1.1     glass #define endcomment(where) (where[-1] == '*' && where[0] == '/')
     59   1.1     glass 
     60  1.13  christos static void unget_token(token *);
     61  1.13  christos static void findstrconst(char **, const char **);
     62  1.13  christos static void findchrconst(char **, const char **);
     63  1.13  christos static void findconst(char **, const char **);
     64  1.13  christos static void findkind(char **, token *);
     65  1.13  christos static int cppline(const char *);
     66  1.13  christos static int directive(const char *);
     67  1.13  christos static void printdirective(const char *);
     68  1.13  christos static void docppline(char *, int *, const char **);
     69   1.5  christos 
     70   1.6     lukem static int pushed = 0;		/* is a token pushed */
     71   1.6     lukem static token lasttok;		/* last token, if pushed */
     72   1.1     glass 
     73   1.1     glass /*
     74   1.6     lukem  * scan expecting 1 given token
     75   1.1     glass  */
     76   1.1     glass void
     77  1.12  dholland scan(tok_kind expect, token *tokp)
     78   1.1     glass {
     79   1.1     glass 	get_token(tokp);
     80   1.1     glass 	if (tokp->kind != expect) {
     81   1.1     glass 		expected1(expect);
     82   1.1     glass 	}
     83   1.1     glass }
     84   1.1     glass /*
     85   1.6     lukem  * scan expecting any of the 2 given tokens
     86   1.1     glass  */
     87   1.1     glass void
     88  1.12  dholland scan2(tok_kind expect1, tok_kind expect2, token *tokp)
     89   1.1     glass {
     90   1.1     glass 	get_token(tokp);
     91   1.1     glass 	if (tokp->kind != expect1 && tokp->kind != expect2) {
     92   1.1     glass 		expected2(expect1, expect2);
     93   1.1     glass 	}
     94   1.1     glass }
     95   1.1     glass /*
     96   1.6     lukem  * scan expecting any of the 3 given token
     97   1.1     glass  */
     98   1.1     glass void
     99  1.12  dholland scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
    100   1.1     glass {
    101   1.1     glass 	get_token(tokp);
    102   1.1     glass 	if (tokp->kind != expect1 && tokp->kind != expect2
    103   1.1     glass 	    && tokp->kind != expect3) {
    104   1.1     glass 		expected3(expect1, expect2, expect3);
    105   1.1     glass 	}
    106   1.1     glass }
    107   1.1     glass /*
    108   1.6     lukem  * scan expecting a constant, possibly symbolic
    109   1.1     glass  */
    110   1.1     glass void
    111  1.12  dholland scan_num(token *tokp)
    112   1.1     glass {
    113   1.1     glass 	get_token(tokp);
    114   1.1     glass 	switch (tokp->kind) {
    115   1.1     glass 	case TOK_IDENT:
    116   1.1     glass 		break;
    117   1.1     glass 	default:
    118  1.14  dholland 		error("Expected constant or identifier");
    119   1.1     glass 	}
    120   1.1     glass }
    121   1.1     glass /*
    122   1.6     lukem  * Peek at the next token
    123   1.1     glass  */
    124   1.1     glass void
    125  1.12  dholland peek(token *tokp)
    126   1.1     glass {
    127   1.1     glass 	get_token(tokp);
    128   1.1     glass 	unget_token(tokp);
    129   1.1     glass }
    130   1.1     glass /*
    131   1.6     lukem  * Peek at the next token and scan it if it matches what you expect
    132   1.1     glass  */
    133   1.1     glass int
    134  1.12  dholland peekscan(tok_kind expect, token *tokp)
    135   1.1     glass {
    136   1.1     glass 	peek(tokp);
    137   1.1     glass 	if (tokp->kind == expect) {
    138   1.1     glass 		get_token(tokp);
    139   1.1     glass 		return (1);
    140   1.1     glass 	}
    141   1.1     glass 	return (0);
    142   1.1     glass }
    143   1.1     glass /*
    144   1.6     lukem  * Get the next token, printing out any directive that are encountered.
    145   1.1     glass  */
    146   1.1     glass void
    147  1.12  dholland get_token(token *tokp)
    148   1.1     glass {
    149   1.6     lukem 	int     commenting;
    150   1.1     glass 
    151   1.1     glass 	if (pushed) {
    152   1.1     glass 		pushed = 0;
    153   1.1     glass 		*tokp = lasttok;
    154   1.1     glass 		return;
    155   1.1     glass 	}
    156   1.1     glass 	commenting = 0;
    157   1.1     glass 	for (;;) {
    158   1.1     glass 		if (*where == 0) {
    159   1.1     glass 			for (;;) {
    160   1.1     glass 				if (!fgets(curline, MAXLINESIZE, fin)) {
    161   1.1     glass 					tokp->kind = TOK_EOF;
    162   1.1     glass 					*where = 0;
    163   1.1     glass 					return;
    164   1.1     glass 				}
    165   1.1     glass 				linenum++;
    166   1.1     glass 				if (commenting) {
    167   1.1     glass 					break;
    168   1.6     lukem 				} else
    169   1.6     lukem 					if (cppline(curline)) {
    170   1.6     lukem 						docppline(curline, &linenum,
    171   1.6     lukem 						    &infilename);
    172   1.6     lukem 					} else
    173   1.6     lukem 						if (directive(curline)) {
    174   1.6     lukem 							printdirective(curline);
    175   1.6     lukem 						} else {
    176   1.6     lukem 							break;
    177   1.6     lukem 						}
    178   1.1     glass 			}
    179   1.1     glass 			where = curline;
    180   1.6     lukem 		} else
    181   1.7  christos 			if (isspace((unsigned char)*where)) {
    182   1.7  christos 				while (isspace((unsigned char)*where)) {
    183   1.6     lukem 					where++;	/* eat */
    184   1.4        pk 				}
    185   1.6     lukem 			} else
    186   1.6     lukem 				if (commenting) {
    187   1.6     lukem 					for (where++; *where; where++) {
    188   1.6     lukem 						if (endcomment(where)) {
    189   1.6     lukem 							where++;
    190   1.6     lukem 							commenting--;
    191   1.6     lukem 							break;
    192   1.6     lukem 						}
    193   1.6     lukem 					}
    194   1.6     lukem 				} else
    195   1.6     lukem 					if (startcomment(where)) {
    196   1.6     lukem 						where += 2;
    197   1.6     lukem 						commenting++;
    198   1.6     lukem 					} else {
    199   1.6     lukem 						break;
    200   1.6     lukem 					}
    201   1.1     glass 	}
    202   1.1     glass 
    203   1.1     glass 	/*
    204   1.6     lukem 	 * 'where' is not whitespace, comment or directive Must be a token!
    205   1.1     glass 	 */
    206   1.1     glass 	switch (*where) {
    207   1.1     glass 	case ':':
    208   1.1     glass 		tokp->kind = TOK_COLON;
    209   1.1     glass 		where++;
    210   1.1     glass 		break;
    211   1.1     glass 	case ';':
    212   1.1     glass 		tokp->kind = TOK_SEMICOLON;
    213   1.1     glass 		where++;
    214   1.1     glass 		break;
    215   1.1     glass 	case ',':
    216   1.1     glass 		tokp->kind = TOK_COMMA;
    217   1.1     glass 		where++;
    218   1.1     glass 		break;
    219   1.1     glass 	case '=':
    220   1.1     glass 		tokp->kind = TOK_EQUAL;
    221   1.1     glass 		where++;
    222   1.1     glass 		break;
    223   1.1     glass 	case '*':
    224   1.1     glass 		tokp->kind = TOK_STAR;
    225   1.1     glass 		where++;
    226   1.1     glass 		break;
    227   1.1     glass 	case '[':
    228   1.1     glass 		tokp->kind = TOK_LBRACKET;
    229   1.1     glass 		where++;
    230   1.1     glass 		break;
    231   1.1     glass 	case ']':
    232   1.1     glass 		tokp->kind = TOK_RBRACKET;
    233   1.1     glass 		where++;
    234   1.1     glass 		break;
    235   1.1     glass 	case '{':
    236   1.1     glass 		tokp->kind = TOK_LBRACE;
    237   1.1     glass 		where++;
    238   1.1     glass 		break;
    239   1.1     glass 	case '}':
    240   1.1     glass 		tokp->kind = TOK_RBRACE;
    241   1.1     glass 		where++;
    242   1.1     glass 		break;
    243   1.1     glass 	case '(':
    244   1.1     glass 		tokp->kind = TOK_LPAREN;
    245   1.1     glass 		where++;
    246   1.1     glass 		break;
    247   1.1     glass 	case ')':
    248   1.1     glass 		tokp->kind = TOK_RPAREN;
    249   1.1     glass 		where++;
    250   1.1     glass 		break;
    251   1.1     glass 	case '<':
    252   1.1     glass 		tokp->kind = TOK_LANGLE;
    253   1.1     glass 		where++;
    254   1.1     glass 		break;
    255   1.1     glass 	case '>':
    256   1.1     glass 		tokp->kind = TOK_RANGLE;
    257   1.1     glass 		where++;
    258   1.1     glass 		break;
    259   1.1     glass 
    260   1.1     glass 	case '"':
    261   1.1     glass 		tokp->kind = TOK_STRCONST;
    262   1.1     glass 		findstrconst(&where, &tokp->str);
    263   1.1     glass 		break;
    264   1.4        pk 	case '\'':
    265   1.4        pk 		tokp->kind = TOK_CHARCONST;
    266   1.4        pk 		findchrconst(&where, &tokp->str);
    267   1.4        pk 		break;
    268   1.1     glass 
    269   1.1     glass 	case '-':
    270   1.1     glass 	case '0':
    271   1.1     glass 	case '1':
    272   1.1     glass 	case '2':
    273   1.1     glass 	case '3':
    274   1.1     glass 	case '4':
    275   1.1     glass 	case '5':
    276   1.1     glass 	case '6':
    277   1.1     glass 	case '7':
    278   1.1     glass 	case '8':
    279   1.1     glass 	case '9':
    280   1.1     glass 		tokp->kind = TOK_IDENT;
    281   1.1     glass 		findconst(&where, &tokp->str);
    282   1.1     glass 		break;
    283   1.1     glass 
    284   1.1     glass 	default:
    285   1.7  christos 		if (!(isalpha((unsigned char)*where) || *where == '_')) {
    286   1.6     lukem 			char    buf[100];
    287   1.6     lukem 			char   *p;
    288   1.1     glass 
    289  1.14  dholland 			s_print(buf, "Illegal character in file: ");
    290   1.1     glass 			p = buf + strlen(buf);
    291   1.7  christos 			if (isprint((unsigned char)*where)) {
    292   1.1     glass 				s_print(p, "%c", *where);
    293   1.1     glass 			} else {
    294   1.1     glass 				s_print(p, "%d", *where);
    295   1.1     glass 			}
    296   1.1     glass 			error(buf);
    297   1.1     glass 		}
    298   1.1     glass 		findkind(&where, tokp);
    299   1.1     glass 		break;
    300   1.1     glass 	}
    301   1.1     glass }
    302   1.1     glass 
    303   1.5  christos static void
    304  1.12  dholland unget_token(token *tokp)
    305   1.1     glass {
    306   1.1     glass 	lasttok = *tokp;
    307   1.1     glass 	pushed = 1;
    308   1.1     glass }
    309   1.1     glass 
    310   1.5  christos static void
    311  1.12  dholland findstrconst(char **str, const char **val)
    312   1.1     glass {
    313  1.12  dholland 	char *p;
    314   1.6     lukem 	int     size;
    315  1.12  dholland 	char *tmp;
    316   1.1     glass 
    317   1.1     glass 	p = *str;
    318   1.1     glass 	do {
    319   1.5  christos 		p++;
    320   1.1     glass 	} while (*p && *p != '"');
    321   1.1     glass 	if (*p == 0) {
    322  1.14  dholland 		error("Unterminated string constant");
    323   1.1     glass 	}
    324   1.1     glass 	p++;
    325   1.1     glass 	size = p - *str;
    326  1.12  dholland 	tmp = alloc(size + 1);
    327  1.12  dholland 	(void) strncpy(tmp, *str, size);
    328  1.12  dholland 	tmp[size] = 0;
    329  1.12  dholland 	*val = tmp;
    330   1.1     glass 	*str = p;
    331   1.1     glass }
    332   1.1     glass 
    333   1.5  christos static void
    334  1.12  dholland findchrconst(char **str, const char **val)
    335   1.4        pk {
    336  1.12  dholland 	char *p;
    337   1.6     lukem 	int     size;
    338  1.12  dholland 	char *tmp;
    339   1.4        pk 
    340   1.4        pk 	p = *str;
    341   1.4        pk 	do {
    342   1.5  christos 		p++;
    343   1.4        pk 	} while (*p && *p != '\'');
    344   1.4        pk 	if (*p == 0) {
    345  1.14  dholland 		error("Unterminated string constant");
    346   1.4        pk 	}
    347   1.4        pk 	p++;
    348   1.4        pk 	size = p - *str;
    349   1.4        pk 	if (size != 3) {
    350  1.14  dholland 		error("Empty character");
    351   1.4        pk 	}
    352  1.12  dholland 	tmp = alloc(size + 1);
    353  1.12  dholland 	(void) strncpy(tmp, *str, size);
    354  1.12  dholland 	tmp[size] = 0;
    355  1.12  dholland 	*val = tmp;
    356   1.4        pk 	*str = p;
    357   1.4        pk }
    358   1.4        pk 
    359   1.5  christos static void
    360  1.12  dholland findconst(char **str, const char **val)
    361   1.1     glass {
    362  1.12  dholland 	char *p;
    363   1.6     lukem 	int     size;
    364  1.12  dholland 	char *tmp;
    365   1.1     glass 
    366   1.1     glass 	p = *str;
    367   1.1     glass 	if (*p == '0' && *(p + 1) == 'x') {
    368   1.1     glass 		p++;
    369   1.1     glass 		do {
    370   1.1     glass 			p++;
    371   1.7  christos 		} while (isxdigit((unsigned char)*p));
    372   1.1     glass 	} else {
    373   1.1     glass 		do {
    374   1.1     glass 			p++;
    375   1.7  christos 		} while (isdigit((unsigned char)*p));
    376   1.1     glass 	}
    377   1.1     glass 	size = p - *str;
    378  1.12  dholland 	tmp = alloc(size + 1);
    379  1.12  dholland 	(void) strncpy(tmp, *str, size);
    380  1.12  dholland 	tmp[size] = 0;
    381  1.12  dholland 	*val = tmp;
    382   1.1     glass 	*str = p;
    383   1.1     glass }
    384   1.1     glass 
    385  1.12  dholland static const token symbols[] = {
    386   1.6     lukem 	{TOK_CONST, "const"},
    387   1.6     lukem 	{TOK_UNION, "union"},
    388   1.6     lukem 	{TOK_SWITCH, "switch"},
    389   1.6     lukem 	{TOK_CASE, "case"},
    390   1.6     lukem 	{TOK_DEFAULT, "default"},
    391   1.6     lukem 	{TOK_STRUCT, "struct"},
    392   1.6     lukem 	{TOK_TYPEDEF, "typedef"},
    393   1.6     lukem 	{TOK_ENUM, "enum"},
    394   1.6     lukem 	{TOK_OPAQUE, "opaque"},
    395   1.6     lukem 	{TOK_BOOL, "bool"},
    396   1.6     lukem 	{TOK_VOID, "void"},
    397   1.6     lukem 	{TOK_CHAR, "char"},
    398   1.6     lukem 	{TOK_INT, "int"},
    399   1.6     lukem 	{TOK_UNSIGNED, "unsigned"},
    400   1.6     lukem 	{TOK_SHORT, "short"},
    401   1.6     lukem 	{TOK_LONG, "long"},
    402  1.11  ginsbach 	{TOK_HYPER, "hyper"},
    403   1.6     lukem 	{TOK_FLOAT, "float"},
    404   1.6     lukem 	{TOK_DOUBLE, "double"},
    405  1.11  ginsbach 	{TOK_QUAD, "quadruple"},
    406   1.6     lukem 	{TOK_STRING, "string"},
    407   1.6     lukem 	{TOK_PROGRAM, "program"},
    408   1.6     lukem 	{TOK_VERSION, "version"},
    409   1.6     lukem 	{TOK_EOF, "??????"},
    410   1.1     glass };
    411   1.1     glass 
    412   1.5  christos static void
    413  1.12  dholland findkind(char **mark, token *tokp)
    414   1.1     glass {
    415   1.6     lukem 	int     len;
    416  1.12  dholland 	const token *s;
    417  1.12  dholland 	char *str;
    418  1.12  dholland 	char *tmp;
    419   1.1     glass 
    420   1.1     glass 	str = *mark;
    421   1.1     glass 	for (s = symbols; s->kind != TOK_EOF; s++) {
    422   1.1     glass 		len = strlen(s->str);
    423   1.1     glass 		if (strncmp(str, s->str, len) == 0) {
    424   1.7  christos 			if (!isalnum((unsigned char)str[len]) &&
    425   1.7  christos 			    str[len] != '_') {
    426   1.1     glass 				tokp->kind = s->kind;
    427   1.1     glass 				tokp->str = s->str;
    428   1.1     glass 				*mark = str + len;
    429   1.1     glass 				return;
    430   1.1     glass 			}
    431   1.1     glass 		}
    432   1.1     glass 	}
    433   1.1     glass 	tokp->kind = TOK_IDENT;
    434   1.7  christos 	for (len = 0; isalnum((unsigned char)str[len]) ||
    435   1.7  christos 	    str[len] == '_'; len++);
    436  1.12  dholland 	tmp = alloc(len + 1);
    437  1.12  dholland 	(void) strncpy(tmp, str, len);
    438  1.12  dholland 	tmp[len] = 0;
    439  1.12  dholland 	tokp->str = tmp;
    440   1.1     glass 	*mark = str + len;
    441   1.1     glass }
    442   1.1     glass 
    443   1.5  christos static int
    444  1.12  dholland cppline(const char *line)
    445   1.1     glass {
    446   1.1     glass 	return (line == curline && *line == '#');
    447   1.1     glass }
    448   1.1     glass 
    449   1.5  christos static int
    450  1.12  dholland directive(const char *line)
    451   1.1     glass {
    452   1.1     glass 	return (line == curline && *line == '%');
    453   1.1     glass }
    454   1.1     glass 
    455   1.5  christos static void
    456  1.12  dholland printdirective(const char *line)
    457   1.1     glass {
    458   1.1     glass 	f_print(fout, "%s", line + 1);
    459   1.1     glass }
    460   1.1     glass 
    461   1.5  christos static void
    462  1.12  dholland docppline(char *line, int *lineno, const char **fname)
    463   1.6     lukem {
    464   1.6     lukem 	char   *file;
    465   1.6     lukem 	int     num;
    466   1.6     lukem 	char   *p;
    467   1.1     glass 
    468   1.1     glass 	line++;
    469   1.7  christos 	while (isspace((unsigned char)*line)) {
    470   1.1     glass 		line++;
    471   1.1     glass 	}
    472   1.1     glass 	num = atoi(line);
    473   1.7  christos 	while (isdigit((unsigned char)*line)) {
    474   1.1     glass 		line++;
    475   1.1     glass 	}
    476   1.7  christos 	while (isspace((unsigned char)*line)) {
    477   1.1     glass 		line++;
    478   1.1     glass 	}
    479   1.1     glass 	if (*line != '"') {
    480  1.14  dholland 		error("Preprocessor error");
    481   1.1     glass 	}
    482   1.1     glass 	line++;
    483   1.1     glass 	p = file = alloc(strlen(line) + 1);
    484   1.1     glass 	while (*line && *line != '"') {
    485   1.1     glass 		*p++ = *line++;
    486   1.1     glass 	}
    487   1.1     glass 	if (*line == 0) {
    488  1.14  dholland 		error("Preprocessor error");
    489   1.1     glass 	}
    490   1.1     glass 	*p = 0;
    491   1.1     glass 	if (*file == 0) {
    492   1.1     glass 		*fname = NULL;
    493  1.10      elad 		free(file);
    494   1.1     glass 	} else {
    495   1.1     glass 		*fname = file;
    496   1.1     glass 	}
    497   1.1     glass 	*lineno = num - 1;
    498   1.1     glass }
    499