1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1986-2010 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * convert C prototypes to ANSI, K&R and C++ styles or K&R to ANSI
1N/A * slips into the pp block read
1N/A *
1N/A * define PROTOMAIN for standalone proto
1N/A * PROTOMAIN is coded for minimal library support
1N/A */
1N/A
1N/Astatic const char id[] = "\n@(#)$Id: proto (AT&T Research) 2008-05-11 $\0\n";
1N/A
1N/A#if PROTOMAIN
1N/A
1N/A#include "ppfsm.c"
1N/A
1N/A#include <hashkey.h>
1N/A
1N/A#if PROTO_STANDALONE
1N/A#undef O_RDONLY
1N/A#endif
1N/A
1N/A#else
1N/A
1N/A#include "pplib.h"
1N/A#include "ppfsm.h"
1N/A
1N/A#endif
1N/A
1N/A#define MAGICGEN "/* : : generated by proto : : */\n"
1N/A
1N/A#define MAGICDIR "pragma" /* proto magic directive */
1N/A#define MAGICARG "prototyped" /* proto magic directive arg */
1N/A#define MAGICOFF "noticed" /* no notice if found in pragma */
1N/A#define MAGICTOP 64 /* must be in these top lines */
1N/A#define NOTICED "Copyright" /* no notice if found in magic */
1N/A#define PUBLICDOMAIN "Public Domain" /* no notice if found in magic */
1N/A
1N/Astruct proto /* proto buffer state */
1N/A{
1N/A int brace; /* {..} level */
1N/A int call; /* call level */
1N/A int fd; /* input file descriptor */
1N/A char* file; /* input file name */
1N/A long flags; /* coupled flags */
1N/A long options; /* uncoupled flags */
1N/A char* package; /* header package */
1N/A int line; /* input line count */
1N/A int test; /* testing */
1N/A
1N/A char* tp; /* input token base */
1N/A
1N/A int iz; /* input buffer size */
1N/A char* ib; /* input buffer base */
1N/A char* ip; /* input buffer pointer */
1N/A
1N/A int oz; /* output buffer size */
1N/A char* ob; /* output buffer base */
1N/A char* op; /* output buffer pointer */
1N/A char* ox; /* output buffer externalize */
1N/A
1N/A char cc[3]; /* beg mid end comment char */
1N/A char pushback[4]; /* pushback area for caller */
1N/A
1N/A char variadic[256]; /* variadic args buffer */
1N/A
1N/A /* output buffer */
1N/A /* slide buffer */
1N/A /* input buffer */
1N/A};
1N/A
1N/A/*
1N/A * proto is separate from pp so these undef's are ok
1N/A */
1N/A
1N/A#undef CLASSIC
1N/A#define CLASSIC (1L<<0)
1N/A#undef DECLARE
1N/A#define DECLARE (1L<<1)
1N/A#undef DEFINE
1N/A#define DEFINE (1L<<2)
1N/A#undef DIRECTIVE
1N/A#define DIRECTIVE (1L<<3)
1N/A#undef ERROR
1N/A#define ERROR (1L<<4)
1N/A#undef EXTERN
1N/A#define EXTERN (1L<<5)
1N/A#undef EXTERNALIZE
1N/A#define EXTERNALIZE (1L<<6)
1N/A#undef IDID
1N/A#define IDID (1L<<7)
1N/A#undef INDIRECT
1N/A#define INDIRECT (1L<<8)
1N/A#undef INIT
1N/A#define INIT (1L<<9)
1N/A#undef INIT_DEFINE
1N/A#define INIT_DEFINE (1L<<10)
1N/A#undef INIT_INCLUDE
1N/A#define INIT_INCLUDE (1L<<11)
1N/A#undef JUNK
1N/A#define JUNK (1L<<12)
1N/A#undef LINESYNC
1N/A#define LINESYNC (1L<<13)
1N/A#undef MANGLE
1N/A#define MANGLE (1L<<14)
1N/A#undef MATCH
1N/A#define MATCH (1L<<15)
1N/A#undef MORE
1N/A#define MORE (1L<<16)
1N/A#undef OTHER
1N/A#define OTHER (1L<<17)
1N/A#undef PASS
1N/A#define PASS (1L<<18)
1N/A#undef PLUSONLY
1N/A#define PLUSONLY (1L<<19)
1N/A#undef PLUSPLUS
1N/A#define PLUSPLUS (1L<<20)
1N/A#undef RECURSIVE
1N/A#define RECURSIVE (1L<<21)
1N/A#undef SHARP
1N/A#define SHARP (1L<<22)
1N/A#undef SKIP
1N/A#define SKIP (1L<<23)
1N/A#undef SLIDE
1N/A#define SLIDE (1L<<24)
1N/A#undef TOKENS
1N/A#define TOKENS (1L<<25)
1N/A#undef TYPEDEF
1N/A#define TYPEDEF (1L<<26)
1N/A#undef VARIADIC
1N/A#define VARIADIC (1L<<27)
1N/A#undef VARIADIC2
1N/A#define VARIADIC2 (1L<<28)
1N/A#undef YACC
1N/A#define YACC (1L<<29)
1N/A#undef YACCSPLIT
1N/A#define YACCSPLIT (1L<<30)
1N/A#undef YACC2
1N/A#define YACC2 (1L<<31)
1N/A
1N/A#undef GLOBAL
1N/A#define GLOBAL (MORE)
1N/A
1N/A#undef REGULAR
1N/A#define REGULAR (1L<<0)
1N/A
1N/A#ifndef CHUNK
1N/A#define CHUNK 1024
1N/A#endif
1N/A#define BLOCK (8*CHUNK)
1N/A
1N/A#define T_VA_START (N_TOKEN+1)
1N/A
1N/A#define RESERVED(b,e,n) ((((long)(b))<<16)|(((long)(e))<<8)|((long)(n)))
1N/A
1N/A/*
1N/A * generate integer
1N/A * pointer to end returned
1N/A */
1N/A
1N/Astatic char*
1N/Anumber(register char* p, register long n)
1N/A{
1N/A register long d;
1N/A
1N/A for (d = 1000000; d > 1; d /= 10)
1N/A if (n >= d) *p++ = '0' + (n / d) % 10;
1N/A *p++ = '0' + n % 10;
1N/A return p;
1N/A}
1N/A
1N/A#if PROTOMAIN
1N/A
1N/Astatic int errors;
1N/A
1N/A#if PROTO_STANDALONE
1N/A
1N/A/*
1N/A * namespace pollution forces us to claim parts of libc
1N/A */
1N/A
1N/A#undef memcpy
1N/A#define memcpy(t,f,n) memcopy(t,f,n)
1N/A#undef strcpy
1N/A#define strcpy(t,f) strcopy(t,f)
1N/A#undef strlen
1N/A#define strlen(s) sstrlen(s)
1N/A#undef strncmp
1N/A#define strncmp(s,t,n) sstrncmp(s,t,n)
1N/A
1N/A/*
1N/A * environmentally safe strlen()
1N/A */
1N/A
1N/Astatic int
1N/Asstrlen(register const char* s)
1N/A{
1N/A register const char* b;
1N/A
1N/A for (b = s; *s; s++);
1N/A return s - b;
1N/A}
1N/A
1N/A/*
1N/A * environmentally safe strncmp()
1N/A */
1N/A
1N/Astatic int
1N/Asstrncmp(register const char* s, register char* t, register int n)
1N/A{
1N/A register const char* e = s + n;
1N/A
1N/A while (s < e)
1N/A {
1N/A if (*s != *t || !*s)
1N/A return *s - *t;
1N/A s++;
1N/A t++;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * strcpy() except pointer to end returned
1N/A */
1N/A
1N/Astatic char*
1N/Astrcopy(register char* s, register const char* t)
1N/A{
1N/A while (*s++ = *t++);
1N/A return s - 1;
1N/A}
1N/A
1N/A#endif
1N/A
1N/Astatic void
1N/Aproto_error(char* iob, int level, char* msg, char* arg)
1N/A{
1N/A register char* p;
1N/A char buf[1024];
1N/A
1N/A p = strcopy(buf, "proto: ");
1N/A if (iob)
1N/A {
1N/A register struct proto* proto = (struct proto*)(iob - sizeof(struct proto));
1N/A
1N/A if (proto->line)
1N/A {
1N/A if (proto->file)
1N/A {
1N/A *p++ = '"';
1N/A p = strcopy(p, proto->file);
1N/A *p++ = '"';
1N/A *p++ = ',';
1N/A *p++ = ' ';
1N/A }
1N/A p = strcopy(p, "line ");
1N/A p = number(p, proto->line);
1N/A }
1N/A else if (proto->file)
1N/A p = strcopy(p, proto->file);
1N/A }
1N/A else
1N/A {
1N/A p = strcopy(p, msg);
1N/A msg = arg;
1N/A arg = 0;
1N/A }
1N/A if (*(p - 1) != ' ')
1N/A {
1N/A *p++ = ':';
1N/A *p++ = ' ';
1N/A }
1N/A if (level == 1)
1N/A p = strcopy(p, "warning: ");
1N/A p = strcopy(p, msg);
1N/A if (arg)
1N/A {
1N/A *p++ = ' ';
1N/A p = strcopy(p, arg);
1N/A }
1N/A *p++ = '\n';
1N/A write(2, buf, p - buf);
1N/A if (level >= 3)
1N/A exit(level - 2);
1N/A if (level >= 2)
1N/A errors++;
1N/A}
1N/A
1N/A/*
1N/A * memcpy() but pointer to end returned
1N/A */
1N/A
1N/Astatic char*
1N/Amemcopy(register char* s, register char* t, int n)
1N/A{
1N/A register char* e = t + n;
1N/A
1N/A while (t < e) *s++ = *t++;
1N/A return s;
1N/A}
1N/A
1N/A#include "../libast/port/astlicense.c"
1N/A
1N/A#else
1N/A
1N/A#define memcopy(s,t,n) (((char*)memcpy(s,t,n))+(n))
1N/A
1N/A#endif
1N/A
1N/A/*
1N/A * generate line sync
1N/A * pointer to end returned
1N/A */
1N/A
1N/Astatic char*
1N/Alinesync(register struct proto* proto, register char* p, register long n)
1N/A{
1N/A#if PROTOMAIN
1N/A if (proto->flags & LINESYNC)
1N/A#endif
1N/A {
1N/A#if PROTOMAIN
1N/A p = strcopy(p, "\n#line ");
1N/A#else
1N/A p = strcopy(p, "\n# ");
1N/A#endif
1N/A p = number(p, n);
1N/A *p++ = '\n';
1N/A }
1N/A return p;
1N/A}
1N/A
1N/A/*
1N/A * output init header
1N/A * pointer to end returned
1N/A */
1N/A
1N/Astatic char*
1N/Ainit(struct proto* proto, char* op, int flags)
1N/A{
1N/A register char* s;
1N/A
1N/A if (flags & INIT_DEFINE)
1N/A {
1N/A op = strcopy(op, "\
1N/A\n\
1N/A#if !defined(__PROTO__)\n\
1N/A# if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)\n\
1N/A# if defined(__cplusplus)\n\
1N/A# define __LINKAGE__ \"C\"\n\
1N/A# else\n\
1N/A# define __LINKAGE__\n\
1N/A# endif\n\
1N/A# define __STDARG__\n\
1N/A# define __PROTO__(x) x\n\
1N/A# define __OTORP__(x)\n\
1N/A# define __PARAM__(n,o) n\n\
1N/A# if !defined(__STDC__) && !defined(__cplusplus)\n\
1N/A# if !defined(c_plusplus)\n\
1N/A# define const\n\
1N/A# endif\n\
1N/A# define signed\n\
1N/A# define void int\n\
1N/A# define volatile\n\
1N/A# define __V_ char\n\
1N/A# else\n\
1N/A# define __V_ void\n\
1N/A# endif\n\
1N/A# else\n\
1N/A# define __PROTO__(x) ()\n\
1N/A# define __OTORP__(x) x\n\
1N/A# define __PARAM__(n,o) o\n\
1N/A# define __LINKAGE__\n\
1N/A# define __V_ char\n\
1N/A# define const\n\
1N/A# define signed\n\
1N/A# define void int\n\
1N/A# define volatile\n\
1N/A# endif\n\
1N/A# define __MANGLE__ __LINKAGE__\n\
1N/A# if defined(__cplusplus) || defined(c_plusplus)\n\
1N/A# define __VARARG__ ...\n\
1N/A# else\n\
1N/A# define __VARARG__\n\
1N/A# endif\n\
1N/A# if defined(__STDARG__)\n\
1N/A# define __VA_START__(p,a) va_start(p,a)\n\
1N/A# else\n\
1N/A# define __VA_START__(p,a) va_start(p)\n\
1N/A# endif\n\
1N/A# if !defined(__INLINE__)\n\
1N/A# if defined(__cplusplus)\n\
1N/A# define __INLINE__ extern __MANGLE__ inline\n\
1N/A# else\n\
1N/A# if defined(_WIN32) && !defined(__GNUC__)\n\
1N/A# define __INLINE__ __inline\n\
1N/A# endif\n\
1N/A# endif\n\
1N/A# endif\n\
1N/A#endif\n\
1N/A#if !defined(__LINKAGE__)\n\
1N/A#define __LINKAGE__ /* 2004-08-11 transition */\n\
1N/A#endif\n\
1N/A");
1N/A }
1N/A else
1N/A op = strcopy(op, "\
1N/A\n\
1N/A#if !defined(__PROTO__)\n\
1N/A#include <prototyped.h>\n\
1N/A#endif\n\
1N/A#if !defined(__LINKAGE__)\n\
1N/A#define __LINKAGE__ /* 2004-08-11 transition */\n\
1N/A#endif\n\
1N/A");
1N/A if (proto->package)
1N/A {
1N/A s = "\
1N/A#ifndef __MANGLE_%_DATA__\n\
1N/A# ifdef _BLD_%\n\
1N/A# ifdef __EXPORT__\n\
1N/A# define __MANGLE_%_DATA__ __MANGLE__ __EXPORT__\n\
1N/A# else\n\
1N/A# define __MANGLE_%_DATA__ __MANGLE__\n\
1N/A# endif\n\
1N/A# define __MANGLE_%_FUNC__ __MANGLE__\n\
1N/A# else\n\
1N/A# ifdef __IMPORT__\n\
1N/A# define __MANGLE_%_DATA__ __MANGLE__ __IMPORT__\n\
1N/A# else\n\
1N/A# define __MANGLE_%_DATA__ __MANGLE__\n\
1N/A# endif\n\
1N/A# define __MANGLE_%_FUNC__ __MANGLE__\n\
1N/A# endif\n\
1N/A#endif\n\
1N/A";
1N/A for (;;)
1N/A {
1N/A switch (*op++ = *s++)
1N/A {
1N/A case 0:
1N/A op--;
1N/A break;
1N/A case '%':
1N/A op = strcopy(op - 1, proto->package);
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A }
1N/A return op;
1N/A}
1N/A
1N/A#define BACKOUT() (op=ko)
1N/A#define CACHE() do{CACHEIN();CACHEOUT();call=proto->call;}while(0)
1N/A#define CACHEIN() (ip=proto->ip)
1N/A#define CACHEOUT() (op=proto->op)
1N/A#define GETCHR() (*(unsigned char*)ip++)
1N/A#define KEEPOUT() (ko=op)
1N/A#define LASTOUT() (*(op-1))
1N/A#define PUTCHR(c) (*op++=(c))
1N/A#define SYNC() do{SYNCIN();SYNCOUT();proto->flags&=~(EXTERN|INIT|OTHER|VARIADIC|VARIADIC2);proto->flags|=flags&(EXTERN|INIT|OTHER|VARIADIC|VARIADIC2);proto->call=call;}while(0)
1N/A#define SYNCIN() (proto->ip=ip)
1N/A#define SYNCOUT() (proto->op=op)
1N/A#define UNGETCHR() (ip--)
1N/A#define UNPUTCHR() (op--)
1N/A
1N/A/*
1N/A * advance to the next non-space character
1N/A */
1N/A
1N/Astatic char*
1N/Anns(register char* s)
1N/A{
1N/A while (*s == ' ' || *s == '\t' || *s == '\n')
1N/A s++;
1N/A return s;
1N/A}
1N/A
1N/A#define DIR_if 01
1N/A#define DIR_el 02
1N/A#define DIR_en 03
1N/A#define DIR 03
1N/A
1N/A/*
1N/A * update directive mask
1N/A */
1N/A
1N/Astatic int
1N/Adirective(register char* s, int dir)
1N/A{
1N/A switch (*(s = nns(s)))
1N/A {
1N/A case 'e':
1N/A case 'i':
1N/A dir <<= 2;
1N/A switch (*++s)
1N/A {
1N/A case 'f':
1N/A dir |= DIR_if;
1N/A break;
1N/A case 'l':
1N/A dir |= DIR_el;
1N/A break;
1N/A case 'n':
1N/A dir |= DIR_en;
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A return dir;
1N/A}
1N/A
1N/A/*
1N/A * the tokenizer
1N/A * top level calls loop until EOB
1N/A * recursive calls just return the next token
1N/A */
1N/A
1N/Astatic int
1N/Alex(register struct proto* proto, register long flags)
1N/A{
1N/A register char* ip;
1N/A register char* op;
1N/A register int c;
1N/A register int state;
1N/A register short* rp;
1N/A char* m;
1N/A char* e;
1N/A char* t;
1N/A char* bp;
1N/A char* v;
1N/A char* im;
1N/A char* ko;
1N/A char* aom;
1N/A int n;
1N/A int line;
1N/A int quot;
1N/A int brack;
1N/A int sub;
1N/A int x;
1N/A int vc;
1N/A
1N/A char* ie = 0;
1N/A char* om = 0;
1N/A char* aim = 0;
1N/A char* aie = 0;
1N/A char* func = 0;
1N/A int call = 0;
1N/A int dir = 0;
1N/A int group = 0;
1N/A int last = 0;
1N/A int paren = 0;
1N/A#if PROTOMAIN
1N/A char* qe = 0;
1N/A int qn = 0;
1N/A int args = 0;
1N/A#endif
1N/A
1N/A CACHE();
1N/A#if PROTOMAIN
1N/A if (flags & EXTERN) KEEPOUT();
1N/A#endif
1N/A fsm_start:
1N/A proto->tp = ip;
1N/A state = PROTO;
1N/A bp = ip;
1N/A do
1N/A {
1N/A rp = fsm[state];
1N/A fsm_get:
1N/A while (!(state = rp[c = GETCHR()]));
1N/A fsm_next:
1N/A ;
1N/A } while (state > 0);
1N/A if ((n = ip - bp - 1) > 0)
1N/A {
1N/A ip = bp;
1N/A MEMCPY(op, ip, n);
1N/A ip++;
1N/A }
1N/A state = ~state;
1N/A fsm_terminal:
1N/A switch (TERM(state))
1N/A {
1N/A case S_CHR:
1N/A if (op > proto->ob && *(op - 1) == '=' && (op == proto->ob + 1 || *(op - 2) != '=')) switch (c)
1N/A {
1N/A case '+':
1N/A case '-':
1N/A case '*':
1N/A case '&':
1N/A PUTCHR(' ');
1N/A break;
1N/A }
1N/A PUTCHR(c);
1N/A break;
1N/A
1N/A case S_CHRB:
1N/A UNGETCHR();
1N/A c = LASTOUT();
1N/A break;
1N/A
1N/A case S_COMMENT:
1N/A switch (c)
1N/A {
1N/A case '\n':
1N/A if (INCOMMENTXX(rp)) goto fsm_newline;
1N/A PUTCHR(c);
1N/A proto->line++;
1N/A rp = fsm[COM2];
1N/A break;
1N/A case '/':
1N/A#if PROTOMAIN
1N/A if ((flags & (EXTERN|MATCH)) == EXTERN) BACKOUT();
1N/A else
1N/A#endif
1N/A PUTCHR(c);
1N/A if (INCOMMENTXX(rp))
1N/A {
1N/A rp = fsm[COM5];
1N/A break;
1N/A }
1N/A goto fsm_start;
1N/A case EOF:
1N/A break;
1N/A default:
1N/A#if PROTOMAIN
1N/A if ((flags & (EXTERN|MATCH)) == EXTERN) BACKOUT();
1N/A else
1N/A#endif
1N/A PUTCHR(c);
1N/A rp = fsm[INCOMMENTXX(rp) ? COM5 : COM3];
1N/A break;
1N/A }
1N/A bp = ip;
1N/A goto fsm_get;
1N/A
1N/A case S_EOB:
1N/A if (c)
1N/A {
1N/A if (state = fsm[TERMINAL][INDEX(rp)+1])
1N/A goto fsm_terminal;
1N/A SYNC();
1N/A return 0;
1N/A }
1N/A UNGETCHR();
1N/A fsm_eob:
1N/A if ((flags & (DECLARE|GLOBAL|RECURSIVE)) == GLOBAL && (proto->flags & MORE))
1N/A {
1N/A#if PROTOMAIN
1N/A if (!(flags & EXTERN)) /* XXX */
1N/A#endif
1N/A flags |= SLIDE;
1N/A c = ip - proto->ib;
1N/A if (!(flags & MATCH))
1N/A im = proto->tp;
1N/A if (ip > proto->ib)
1N/A {
1N/A n = ip - im;
1N/A if (ip - n < proto->ib)
1N/A proto->flags |= ERROR;
1N/A memcopy(proto->ib - n, ip - n, n);
1N/A ip = proto->ib;
1N/A }
1N/A proto->tp -= c;
1N/A if (flags & MATCH)
1N/A {
1N/A im -= c;
1N/A ie -= c;
1N/A }
1N/A if (aim)
1N/A aim -= c;
1N/A if (aie)
1N/A aie -= c;
1N/A if ((n = read(proto->fd, ip, proto->iz)) > 0)
1N/A {
1N/A if ((proto->options & REGULAR) && n < proto->iz)
1N/A {
1N/A proto->flags &= ~MORE;
1N/A close(proto->fd);
1N/A }
1N/A *(ip + n) = 0;
1N/A if (state & SPLICE)
1N/A goto fsm_splice;
1N/A bp = ip;
1N/A goto fsm_get;
1N/A }
1N/A *ip = 0;
1N/A proto->flags &= ~MORE;
1N/A close(proto->fd);
1N/A }
1N/A if (state & SPLICE)
1N/A goto fsm_splice;
1N/A /* NOTE: RECURSIVE lex() should really SLIDE too */
1N/A if (!(flags & RECURSIVE) && (state = rp[c = EOF]))
1N/A {
1N/A bp = ip;
1N/A goto fsm_next;
1N/A }
1N/A SYNC();
1N/A return 0;
1N/A
1N/A case S_LITBEG:
1N/A quot = c;
1N/A#if PROTOMAIN
1N/A if (c == '"' && qe)
1N/A {
1N/A for (n = 0, t = qe + 1; t < op && (*t == ' ' || *t == '\t' || *t == '\n' && ++n || *t >= 'A' && *t <= 'Z' || *t == '_'); t++);
1N/A if (t == op)
1N/A {
1N/A op = qe;
1N/A qe = 0;
1N/A qn = n;
1N/A }
1N/A else PUTCHR(c);
1N/A }
1N/A else
1N/A#endif
1N/A PUTCHR(c);
1N/A rp = fsm[LIT1];
1N/A bp = ip;
1N/A goto fsm_get;
1N/A
1N/A case S_LITEND:
1N/A if (c == quot)
1N/A {
1N/A#if PROTOMAIN
1N/A if (!(flags & DIRECTIVE))
1N/A qe = (c == '"') ? op : (char*)0;
1N/A#endif
1N/A PUTCHR(c);
1N/A#if PROTOMAIN
1N/A while (qn > 0)
1N/A {
1N/A qn--;
1N/A PUTCHR('\n');
1N/A }
1N/A#endif
1N/A }
1N/A else if (c != '\n' && c != EOF)
1N/A {
1N/A PUTCHR(c);
1N/A bp = ip;
1N/A goto fsm_get;
1N/A }
1N/A else
1N/A {
1N/A#if PROTOMAIN
1N/A while (qn > 0)
1N/A {
1N/A qn--;
1N/A PUTCHR('\n');
1N/A }
1N/A#endif
1N/A UNGETCHR();
1N/A }
1N/A c = T_INVALID;
1N/A break;
1N/A
1N/A case S_LITESC:
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC) PUTCHR(c);
1N/A else
1N/A#endif
1N/A switch (c)
1N/A {
1N/A case 'a':
1N/A n = CC_bel;
1N/A goto fsm_oct;
1N/A case 'E':
1N/A n = CC_esc;
1N/A goto fsm_oct;
1N/A case 'v':
1N/A n = CC_vt;
1N/A goto fsm_oct;
1N/A case 'x':
1N/A SYNC();
1N/A lex(proto, (flags & GLOBAL) | RECURSIVE);
1N/A for (n = x = 0; (c = GETCHR()), x < 3; x++) switch (c)
1N/A {
1N/A case '0': case '1': case '2': case '3':
1N/A case '4': case '5': case '6': case '7':
1N/A case '8': case '9':
1N/A n = (n << 4) + c - '0';
1N/A break;
1N/A case 'a': case 'b': case 'c': case 'd':
1N/A case 'e': case 'f':
1N/A n = (n << 4) + c - 'a' + 10;
1N/A break;
1N/A case 'A': case 'B': case 'C': case 'D':
1N/A case 'E': case 'F':
1N/A n = (n << 4) + c - 'A' + 10;
1N/A break;
1N/A default:
1N/A goto fsm_hex;
1N/A }
1N/A fsm_hex:
1N/A UNGETCHR();
1N/A fsm_oct:
1N/A PUTCHR(((n >> 6) & 07) + '0');
1N/A PUTCHR(((n >> 3) & 07) + '0');
1N/A PUTCHR((n & 07) + '0');
1N/A break;
1N/A default:
1N/A PUTCHR(c);
1N/A break;
1N/A }
1N/A rp = fsm[LIT1];
1N/A bp = ip;
1N/A goto fsm_get;
1N/A
1N/A case S_MACRO:
1N/A UNGETCHR();
1N/A#if PROTOMAIN
1N/A if ((flags & EXTERN) && *proto->tp == 's' && !strncmp(proto->tp, "static", 6))
1N/A {
1N/A c = T_EXTERN;
1N/A break;
1N/A }
1N/A#endif
1N/A if (*proto->tp == '_' && !strncmp(proto->tp, "__STDPP__directive", 6)) c = '#';
1N/A else c = T_ID;
1N/A
1N/A break;
1N/A
1N/A case S_NL:
1N/A fsm_newline:
1N/A proto->line++;
1N/A#if PROTOMAIN
1N/A if (flags & EXTERN)
1N/A {
1N/A if (op != proto->ob && LASTOUT() != ' ' && LASTOUT() != '\n')
1N/A PUTCHR(' ');
1N/A }
1N/A else
1N/A#endif
1N/A PUTCHR(c);
1N/A if (flags & DIRECTIVE)
1N/A {
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC)
1N/A {
1N/A if (flags & EXTERN) BACKOUT();
1N/A if (flags & JUNK)
1N/A {
1N/A *(ip - 1) = 0;
1N/A op = strcopy(om, "/* ");
1N/A op = strcopy(op, im);
1N/A op = strcopy(op, " */\n");
1N/A }
1N/A flags &= ~(DEFINE|DIRECTIVE|IDID|INDIRECT|JUNK|MATCH|SHARP|TYPEDEF);
1N/A }
1N/A else
1N/A#endif
1N/A {
1N/A if ((flags & (DEFINE|SHARP)) == (DEFINE|SHARP))
1N/A {
1N/A *(ip - 1) = 0;
1N/A op = strcopy(om, "#if defined(__STDC__) || defined(__STDPP__)\n");
1N/A op = strcopy(op, im);
1N/A op = strcopy(op, "\n#else\n");
1N/A bp = ip;
1N/A ip = im;
1N/A *op++ = *ip++;
1N/A while (*op = *ip++)
1N/A if (*op++ == '#' && *ip != '(')
1N/A {
1N/A op--;
1N/A while (*--op == ' ' || *op == '\t');
1N/A if (*ip == '#')
1N/A {
1N/A op = strcopy(op + 1, "/**/");
1N/A while (*++ip == ' ' || *ip == '\t');
1N/A }
1N/A else
1N/A {
1N/A if (*op != '"') *++op = '"';
1N/A op++;
1N/A while (*ip == ' ' || *ip == '\t') ip++;
1N/A while ((c = *ip) >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_') *op++ = *ip++;
1N/A while (*ip == ' ' || *ip == '\t') ip++;
1N/A if (*ip == '"') ip++;
1N/A else *op++ = '"';
1N/A }
1N/A }
1N/A ip = bp;
1N/A op = strcopy(op, "\n#endif\n");
1N/A op = linesync(proto, op, proto->line);
1N/A }
1N/A flags &= ~(DEFINE|DIRECTIVE|IDID|INDIRECT|MATCH|OTHER|SHARP|SKIP|TOKENS|TYPEDEF);
1N/A }
1N/A call = 0;
1N/A group = 0;
1N/A paren = 0;
1N/A last = '\n';
1N/A }
1N/A if (paren == 0 && (flags & (MATCH|RECURSIVE|SKIP|SLIDE)) == SLIDE)
1N/A {
1N/A#if PROTOMAIN
1N/A if (flags & EXTERN) BACKOUT();
1N/A#endif
1N/A SYNC();
1N/A return 0;
1N/A }
1N/A goto fsm_start;
1N/A
1N/A case S_QUAL:
1N/A PUTCHR(c);
1N/A rp = fsm[NEXT(state)];
1N/A bp = ip;
1N/A goto fsm_get;
1N/A
1N/A case S_TOK:
1N/A PUTCHR(c);
1N/A c = TYPE(state);
1N/A break;
1N/A
1N/A case S_TOKB:
1N/A UNGETCHR();
1N/A c = TYPE(state);
1N/A break;
1N/A
1N/A case S_RESERVED:
1N/A UNGETCHR();
1N/A c = T_ID;
1N/A if (!(flags & DECLARE)) switch (RESERVED(*proto->tp, *(ip - 1), ip - proto->tp))
1N/A {
1N/A case RESERVED('N', 'N', 3):
1N/A if (proto->tp[1] == 'o')
1N/A c = T_DO;
1N/A break;
1N/A case RESERVED('d', 'o', 2):
1N/A c = T_DO;
1N/A break;
1N/A case RESERVED('e', 'e', 4):
1N/A if (!(flags & RECURSIVE) && (flags & (DIRECTIVE|TOKENS)) != DIRECTIVE && !strncmp(proto->tp, "else", 4))
1N/A {
1N/A c = T_ELSE;
1N/A goto fsm_id;
1N/A }
1N/A break;
1N/A case RESERVED('e', 'n', 6):
1N/A if (!strncmp(proto->tp, "extern", 6))
1N/A c = T_EXTERN;
1N/A break;
1N/A case RESERVED('f', 'r', 3):
1N/A if (!(flags & RECURSIVE) && !strncmp(proto->tp, "for", 3))
1N/A {
1N/A c = T_FOR;
1N/A goto fsm_id;
1N/A }
1N/A break;
1N/A case RESERVED('i', 'f', 2):
1N/A c = T_IF;
1N/A break;
1N/A case RESERVED('i', 'e', 6):
1N/A if (!strncmp(proto->tp, "inline", 6) && !(flags & (MATCH|SKIP|TOKENS|TYPEDEF)) && proto->brace == 0 && paren == 0 && group == 0 && (last == ';' || last == '}' || last == '\n' || last == 0))
1N/A {
1N/A flags |= SKIP;
1N/A SYNC();
1N/A line = proto->line;
1N/A op = strcopy(op - 6, "__INLINE__");
1N/A SYNC();
1N/A }
1N/A break;
1N/A case RESERVED('r', 'n', 6):
1N/A if (!(flags & RECURSIVE) && !strncmp(proto->tp, "return", 6))
1N/A {
1N/A c = T_RETURN;
1N/A goto fsm_id;
1N/A }
1N/A break;
1N/A case RESERVED('s', 'c', 6):
1N/A if ((proto->options & EXTERNALIZE) && !strncmp(proto->tp, "static", 6))
1N/A {
1N/A proto->ox = op - 6;
1N/A flags |= EXTERNALIZE;
1N/A }
1N/A break;
1N/A case RESERVED('t', 'f', 7):
1N/A if (!(flags & RECURSIVE) && !strncmp(proto->tp, "typedef", 7))
1N/A {
1N/A flags |= TYPEDEF;
1N/A c = T_EXTERN;
1N/A }
1N/A break;
1N/A case RESERVED('v', 't', 8):
1N/A if (*ip == '(' && !strncmp(proto->tp, "va_start", 8)) c = T_VA_START;
1N/A break;
1N/A case RESERVED('v', 'd', 4):
1N/A if (!strncmp(proto->tp, "void", 4))
1N/A {
1N/A if (flags & (CLASSIC|PLUSONLY|INIT_DEFINE|INIT_INCLUDE)) c = T_VOID;
1N/A else
1N/A {
1N/A SYNC();
1N/A line = proto->line;
1N/A if (lex(proto, (flags & GLOBAL) | RECURSIVE) == '*')
1N/A {
1N/A memcopy(op - 4, "__V_", 4);
1N/A memcopy(ip - 4, "__V_", 4);
1N/A }
1N/A else c = T_VOID;
1N/A proto->line = line;
1N/A SYNC();
1N/A bp = ip;
1N/A }
1N/A }
1N/A break;
1N/A case RESERVED('w', 'e', 5):
1N/A if (!(flags & RECURSIVE) && !strncmp(proto->tp, "while", 5))
1N/A {
1N/A c = T_WHILE;
1N/A goto fsm_id;
1N/A }
1N/A break;
1N/A }
1N/A#if PROTOMAIN
1N/A if ((flags & CLASSIC) && c != T_EXTERN)
1N/A c = T_ID;
1N/A#endif
1N/A break;
1N/A
1N/A case S_VS:
1N/A goto fsm_start;
1N/A
1N/A case S_WS:
1N/A UNGETCHR();
1N/A#if PROTOMAIN
1N/A if ((flags & (EXTERN|MATCH)) == EXTERN)
1N/A {
1N/A while (op > proto->ob && (*(op - 1) == ' ' || *(op - 1) == '\t'))
1N/A op--;
1N/A if (op > proto->ob && *(op - 1) != '\n') *op++ = ' ';
1N/A }
1N/A#endif
1N/A goto fsm_start;
1N/A
1N/A default:
1N/A if (state & SPLICE)
1N/A {
1N/A if (c == '\\')
1N/A {
1N/A if (!(n = GETCHR()))
1N/A {
1N/A goto fsm_eob;
1N/A fsm_splice:
1N/A c = '\\';
1N/A n = GETCHR();
1N/A }
1N/A if (n == '\n')
1N/A {
1N/A proto->line++;
1N/A PUTCHR('\\');
1N/A PUTCHR('\n');
1N/A bp = ip;
1N/A goto fsm_get;
1N/A }
1N/A UNGETCHR();
1N/A }
1N/A state &= ~SPLICE;
1N/A if (state >= TERMINAL)
1N/A goto fsm_terminal;
1N/A rp = fsm[state];
1N/A }
1N/A PUTCHR(c);
1N/A bp = ip;
1N/A goto fsm_get;
1N/A }
1N/A if (!(flags & (INIT_DEFINE|INIT_INCLUDE|RECURSIVE)))
1N/A {
1N/A if (!(flags & DIRECTIVE)) switch (c)
1N/A {
1N/A case '(':
1N/A#if PROTOMAIN
1N/A if (!(flags & CLASSIC) || proto->brace == 0)
1N/A#endif
1N/A {
1N/A if (paren++ == 0)
1N/A {
1N/A#if PROTOMAIN
1N/A if (!(flags & CLASSIC) || group <= 1)
1N/A#endif
1N/A {
1N/A#if PROTOMAIN
1N/A args = 0;
1N/A#endif
1N/A if (group++ == 0) group++;
1N/A else if (flags & INDIRECT) call++;
1N/A flags |= MATCH;
1N/A im = ip - 1;
1N/A om = op - 1;
1N/A }
1N/A sub = 0;
1N/A }
1N/A else if (paren == 2 && !aim)
1N/A {
1N/A sub++;
1N/A if (last == '(')
1N/A {
1N/A flags &= ~MATCH;
1N/A om = 0;
1N/A }
1N/A else if (flags & INDIRECT)
1N/A {
1N/A aim = ip - 1;
1N/A aom = op - 1;
1N/A }
1N/A else if ((flags & (MATCH|TOKENS)) == MATCH)
1N/A {
1N/A for (m = ip - 2; m > im && (*m == ' ' || *m == '\t'); m--);
1N/A if (m != im && sub == 1)
1N/A {
1N/A m = im + (*nns(ip) == '*');
1N/A }
1N/A if (m == im)
1N/A {
1N/A flags &= ~MATCH;
1N/A om = 0;
1N/A }
1N/A }
1N/A else if ((flags & MATCH) && sub == 1 && *nns(ip) != '*')
1N/A {
1N/A flags &= ~MATCH;
1N/A om = 0;
1N/A }
1N/A }
1N/A flags &= ~TOKENS;
1N/A }
1N/A break;
1N/A case ')':
1N/A#if PROTOMAIN
1N/A if (!(flags & CLASSIC) || proto->brace == 0)
1N/A#endif
1N/A if (--paren == 0)
1N/A {
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC)
1N/A {
1N/A if (group != 2)
1N/A {
1N/A c = T_ID;
1N/A break;
1N/A }
1N/A group++;
1N/A }
1N/A#endif
1N/A ie = ip;
1N/A }
1N/A else if (paren == 1 && (flags & INDIRECT) && !aie)
1N/A aie = ip;
1N/A break;
1N/A case '*':
1N/A if (last == '(' && group == 2)
1N/A {
1N/A group--;
1N/A if (paren == 1)
1N/A {
1N/A flags |= INDIRECT;
1N/A aim = aie = 0;
1N/A }
1N/A }
1N/A break;
1N/A case '#':
1N/A dir = directive(ip, dir);
1N/A if (proto->brace == 0 && paren == 0 && last != '=' && (flags & (CLASSIC|DECLARE|DIRECTIVE|MATCH|PLUSONLY|SKIP|TOKENS)) == (MATCH|TOKENS) && ((dir & DIR) != DIR_en || ((dir>>2) & DIR) != DIR_if))
1N/A flags |= DIRECTIVE;
1N/A else if (!(flags & (DECLARE|DIRECTIVE)))
1N/A {
1N/A flags |= DIRECTIVE;
1N/A if (!(flags & PLUSONLY))
1N/A {
1N/A bp = ip;
1N/A while (*ip == ' ' || *ip == '\t') ip++;
1N/A if (*ip == 'l' && *++ip == 'i' && *++ip == 'n' && *++ip == 'e')
1N/A {
1N/A if (*++ip == ' ' || *ip == '\t')
1N/A {
1N/A proto->line = 0;
1N/A while (*++ip >= '0' && *ip <= '9')
1N/A proto->line = proto->line * 10 + *ip - '0';
1N/A proto->line--;
1N/A }
1N/A }
1N/A#if PROTOMAIN
1N/A else if ((flags & (CLASSIC|EXTERN)) == CLASSIC)
1N/A {
1N/A n = 0;
1N/A t = ip + 6;
1N/A while (ip < t && *ip >= 'a' && *ip <= 'z')
1N/A n = HASHKEYPART(n, *ip++);
1N/A switch (n)
1N/A {
1N/A case HASHKEY4('e','l','s','e'):
1N/A case HASHKEY5('e','n','d','i','f'):
1N/A while (*ip == ' ' || *ip == '\t') ip++;
1N/A if (*ip != '\n' && *ip != '/' && *(ip + 1) != '*')
1N/A {
1N/A flags |= JUNK|MATCH;
1N/A im = ip;
1N/A om = op + (ip - bp);
1N/A }
1N/A break;
1N/A case HASHKEY4('e','l','i','f'):
1N/A case HASHKEY5('e','r','r','o','r'):
1N/A case HASHKEY2('i','f'):
1N/A case HASHKEY5('i','f','d','e','f'):
1N/A case HASHKEY6('i','f','n','d','e','f'):
1N/A case HASHKEY5('u','n','d','e','f'):
1N/A break;
1N/A case HASHKEY6('i','n','c','l','u','d'):
1N/A if (*ip == 'e') ip++;
1N/A /*FALLTHROUGH*/
1N/A case HASHKEY6('d','e','f','i','n','e'):
1N/A case HASHKEY6('p','r','a','g','m','a'):
1N/A if (*ip < 'a' || *ip > 'z') break;
1N/A /*FALLTHROUGH*/
1N/A default:
1N/A flags |= JUNK|MATCH;
1N/A im = bp - 1;
1N/A om = op - 1;
1N/A break;
1N/A }
1N/A }
1N/A else
1N/A#endif
1N/A {
1N/A if (*ip == 'i' && *++ip == 'n' && *++ip == 'c' && *++ip == 'l' && *++ip == 'u' && *++ip == 'd' && *++ip == 'e')
1N/A {
1N/A while (*++ip == ' ' || *ip == '\t');
1N/A if (*ip++ == '<' && *ip++ == 's' && *ip++ == 't' && *ip++ == 'd' && *ip++ == 'a' && *ip++ == 'r' && *ip++ == 'g' && *ip++ == '.' && *ip++ == 'h' && *ip++ == '>')
1N/A {
1N/A op = strcopy(op, "\
1N/Aif !defined(va_start)\n\
1N/A#if defined(__STDARG__)\n\
1N/A#include <stdarg.h>\n\
1N/A#else\n\
1N/A#include <varargs.h>\n\
1N/A#endif\n\
1N/A#endif\n\
1N/A");
1N/A op = linesync(proto, op, proto->line);
1N/A break;
1N/A }
1N/A }
1N/A else if (*ip == 'd' && *++ip == 'e' && *++ ip == 'f' && *++ip == 'i' && *++ip == 'n' && *++ip == 'e' && (*++ip == ' ' || *ip == '\t'))
1N/A {
1N/A while (*++ip == ' ' || *ip == '\t');
1N/A if (*ip == 'e' && *++ip == 'x' && *++ ip == 't' && *++ip == 'e' && *++ip == 'r' && *++ip == 'n' && (*++ip == ' ' || *ip == '\t'))
1N/A {
1N/A t = ip;
1N/A while (*++t == ' ' || *t == '\t');
1N/A if (*t == 'e' && *++t == 'x' && *++ t == 't' && *++t == 'e' && *++t == 'r' && *++t == 'n' && (*++t == ' ' || *t == '\t' || *t == '\n' || *t == '\r'))
1N/A ip = t;
1N/A t = ip;
1N/A while (*++t == ' ' || *t == '\t');
1N/A if (*t == '_' && *(t + 1) == '_')
1N/A {
1N/A op = strcopy(op, "undef __MANGLE__\n");
1N/A op = linesync(proto, op, proto->line);
1N/A op = strcopy(op, "#define __MANGLE__ __LINKAGE__");
1N/A break;
1N/A }
1N/A }
1N/A flags |= DEFINE|MATCH;
1N/A im = bp - 1;
1N/A om = op - 1;
1N/A }
1N/A else if (*ip == 'u' && *++ip == 'n' && *++ ip == 'd' && *++ip == 'e' && *++ip == 'f' && (*++ip == ' ' || *ip == '\t'))
1N/A {
1N/A while (*++ip == ' ' || *ip == '\t');
1N/A if (*ip == 'e' && *++ip == 'x' && *++ ip == 't' && *++ip == 'e' && *++ip == 'r' && *++ip == 'n' && (*++ip == ' ' || *ip == '\t' || *ip == '\n' || *ip == '\r'))
1N/A {
1N/A op = strcopy(op, "undef __MANGLE__\n");
1N/A op = linesync(proto, op, proto->line);
1N/A op = strcopy(op, "#define __MANGLE__ __LINKAGE__");
1N/A break;
1N/A }
1N/A flags |= DEFINE|MATCH;
1N/A im = bp - 1;
1N/A om = op - 1;
1N/A }
1N/A }
1N/A ip = bp;
1N/A }
1N/A break;
1N/A }
1N/A else
1N/A break;
1N/A /*FALLTHROUGH*/
1N/A case '{':
1N/A if (proto->brace++ == 0 && paren == 0)
1N/A {
1N/A if (last == '=') flags |= INIT;
1N/A#if PROTOMAIN
1N/A else if (flags & CLASSIC)
1N/A {
1N/A if ((flags & (MATCH|OTHER|SKIP)) == MATCH)
1N/A {
1N/A if (args)
1N/A {
1N/A v = number(op, args < 0 ? -args : args);
1N/A v = strcopy(v, " argument actual/formal mismatch");
1N/A *v++ = ' ';
1N/A v = memcopy(v, im, ie - im);
1N/A *v = 0;
1N/A proto_error((char*)proto + sizeof(struct proto), 2, op, NiL);
1N/A }
1N/A ip--;
1N/A /*UNDENT...*/
1N/A v = ie;
1N/A while (ie < ip)
1N/A if (*ie++ == '/' && *ie == '*')
1N/A {
1N/A e = ie - 1;
1N/A while (++ie < ip)
1N/A {
1N/A if (*ie == '*')
1N/A {
1N/A while (ie < ip && *ie == '*') ie++;
1N/A if (ie < ip && *ie == '/')
1N/A {
1N/A while (++ie < ip && (*ie == ' ' || *ie == '\t'));
1N/A while (e > v && (*(e - 1) == ' ' || *(e - 1) == '\t')) e--;
1N/A if (e > v && *e != '\n') *e++ = ' ';
1N/A t = ie;
1N/A while (--e >= v)
1N/A *--t = *e;
1N/A v = t;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A ie = v;
1N/A /*...INDENT*/
1N/A op = om++;
1N/A if (flags & EXTERN)
1N/A {
1N/A v = op;
1N/A while (v > ko && *--v != ' ');
1N/A if (*v != ' ')
1N/A {
1N/A om = (v = (op += 4)) + 1;
1N/A while (v >= ko + 4)
1N/A {
1N/A *v = *(v - 4);
1N/A v--;
1N/A }
1N/A memcopy(ko, "int ", 4);
1N/A }
1N/A if (*v == ' ')
1N/A {
1N/A while (*(v + 1) == '*')
1N/A *v++ = '*';
1N/A *v = '\t';
1N/A if ((v - ko) <= 8)
1N/A {
1N/A om = (e = ++op) + 1;
1N/A while (e > v)
1N/A {
1N/A *e = *(e - 1);
1N/A e--;
1N/A }
1N/A }
1N/A }
1N/A om = (v = (op += 7)) + 1;
1N/A while (v >= ko + 7)
1N/A {
1N/A *v = *(v - 7);
1N/A v--;
1N/A }
1N/A memcopy(ko, "extern ", 7);
1N/A }
1N/A PUTCHR('(');
1N/A t = op;
1N/A e = 0;
1N/A /*UNDENT...*/
1N/A while (ie < ip)
1N/A {
1N/A if ((c = *ie) == ' ' || c == '\t' || c == '\n')
1N/A {
1N/A while ((c = *++ie) == ' ' || c == '\t' || c == '\n');
1N/A if (ie >= ip) break;
1N/A if (c != '*' && op > om) PUTCHR(' ');
1N/A }
1N/A if ((n = ((c = *ie) == ',')) || c == ';')
1N/A {
1N/A if (flags & EXTERN)
1N/A {
1N/A m = op;
1N/A while (op > om && ((c = *(op - 1)) == '(' || c == ')' || c == '[' || c == ']'))
1N/A op--;
1N/A v = op;
1N/A while (op > om && (c = *(op - 1)) != ' ' && c != '*')
1N/A op--;
1N/A while (*(op - 1) == ' ')
1N/A op--;
1N/A if (!e)
1N/A {
1N/A e = op;
1N/A while (e > om && *(e - 1) == '*')
1N/A e--;
1N/A }
1N/A#if _s5r4_386_compiler_bug_fixed_
1N/A if (op <= om || *(op - 1) == ',' && (*op++ = ' '))
1N/A op = strcopy(op, "int");
1N/A#else
1N/A if (op <= om)
1N/A op = strcopy(op, "int");
1N/A else if (*(op - 1) == ',')
1N/A op = strcopy(op, " int");
1N/A#endif
1N/A while (v < m)
1N/A PUTCHR(*v++);
1N/A }
1N/A PUTCHR(',');
1N/A if (n)
1N/A {
1N/A if (x = !e) e = op - 1;
1N/A PUTCHR(' ');
1N/A m = t;
1N/A while (m < e)
1N/A PUTCHR(*m++);
1N/A if (x)
1N/A {
1N/A m = e;
1N/A while (*--e != ' ');
1N/A while (*(e - 1) == '*') e--;
1N/A op -= m - e;
1N/A }
1N/A }
1N/A while ((c = *++ie) == ' ' || c == '\t' || c == '\n');
1N/A if (ie >= ip) UNPUTCHR();
1N/A else PUTCHR(' ');
1N/A if (!n)
1N/A {
1N/A t = op;
1N/A e = 0;
1N/A }
1N/A }
1N/A else if (*ie == '*')
1N/A {
1N/A if (op > om && (c = *(op - 1)) == ' ') op--;
1N/A while (*ie == '*') PUTCHR(*ie++);
1N/A while (*ie == ' ' || *ie == '\t' || *ie == '\n') ie++;
1N/A if (c != '(') PUTCHR(' ');
1N/A }
1N/A else if (*ie == '(')
1N/A {
1N/A if (op > om && *(op - 1) == ' ') op--;
1N/A PUTCHR(*ie++);
1N/A while (*ie == ' ' || *ie == '\t' || *ie == '\n') ie++;
1N/A }
1N/A else if (*ie == ')')
1N/A {
1N/A if (op > om && *(op - 1) == '(')
1N/A proto_error((char*)proto + sizeof(struct proto), 1, "function pointer argument prototype omitted", NiL);
1N/A PUTCHR(*ie++);
1N/A while (*ie == ' ' || *ie == '\t' || *ie == '\n') ie++;
1N/A }
1N/A else if ((flags & EXTERN) && (op == om || *(op - 1) == ' ') && *ie == 'r' && !strncmp(ie, "register", 8) && (*(ie + 8) == ' ' || *(ie + 8) == '\t' || *(ie + 8) == '\n'))
1N/A {
1N/A ie += 8;
1N/A if (op > om) UNPUTCHR();
1N/A }
1N/A else PUTCHR(*ie++);
1N/A }
1N/A /*...INDENT*/
1N/A if (op <= om) op = strcopy(op, "void");
1N/A PUTCHR(')');
1N/A if (flags & EXTERN)
1N/A {
1N/A PUTCHR(';');
1N/A PUTCHR('\n');
1N/A SYNCOUT();
1N/A KEEPOUT();
1N/A }
1N/A else
1N/A {
1N/A PUTCHR('\n');
1N/A PUTCHR(*ip);
1N/A }
1N/A ip++;
1N/A flags &= ~(MATCH|SKIP);
1N/A }
1N/A }
1N/A#endif
1N/A else if ((flags & (MATCH|PLUSONLY|SKIP|TOKENS)) == (MATCH|TOKENS))
1N/A {
1N/A line = proto->line;
1N/A op = strcopy(om, " __PARAM__(");
1N/A op = memcopy(op, im, ie - im);
1N/A PUTCHR(',');
1N/A PUTCHR(' ');
1N/A PUTCHR('(');
1N/A flags &= ~(MATCH|SKIP);
1N/A if (flags & VARIADIC)
1N/A {
1N/A if ((vc = ie - im + 1) > sizeof(proto->variadic)) vc = sizeof(proto->variadic);
1N/A memcopy(proto->variadic, im, vc);
1N/A op = strcopy(op, "va_alist)) __OTORP__(va_dcl)\n{");
1N/A }
1N/A else
1N/A {
1N/A flags |= SKIP;
1N/A proto->ip = im;
1N/A proto->op = op;
1N/A group = 0;
1N/A brack = 0;
1N/A for (;;)
1N/A {
1N/A switch (lex(proto, (flags & GLOBAL) | RECURSIVE))
1N/A {
1N/A case '[':
1N/A brack++;
1N/A continue;
1N/A case ']':
1N/A brack--;
1N/A continue;
1N/A case '(':
1N/A if (paren++) group++;
1N/A continue;
1N/A case ')':
1N/A if (--paren == 0)
1N/A {
1N/A group = 0;
1N/A if (flags & MATCH)
1N/A {
1N/A flags &= ~(MATCH|SKIP);
1N/A op = memcopy(op, m, e - m);
1N/A }
1N/A break;
1N/A }
1N/A continue;
1N/A case ',':
1N/A if (paren == 1)
1N/A {
1N/A group = 0;
1N/A if (flags & MATCH)
1N/A {
1N/A flags &= ~(MATCH|SKIP);
1N/A op = memcopy(op, m, e - m);
1N/A }
1N/A PUTCHR(',');
1N/A PUTCHR(' ');
1N/A proto->op = op;
1N/A }
1N/A continue;
1N/A case T_ID:
1N/A if (group <= 1 && !brack)
1N/A {
1N/A flags |= MATCH;
1N/A m = proto->tp;
1N/A e = proto->ip;
1N/A }
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A PUTCHR(')');
1N/A PUTCHR(')');
1N/A }
1N/A if (!(flags & SKIP))
1N/A {
1N/A flags |= SKIP;
1N/A proto->op = strcopy(op, " __OTORP__(");
1N/A proto->ip = im + 1;
1N/A n = *(ie - 1);
1N/A *(ie - 1) = ';';
1N/A c = *ie;
1N/A *ie = 0;
1N/A lex(proto, (flags & GLOBAL) | DECLARE);
1N/A *(ie - 1) = n;
1N/A *ie = c;
1N/A proto->ip = ie;
1N/A op = proto->op;
1N/A PUTCHR(')');
1N/A }
1N/A if (flags & EXTERNALIZE) memcpy(proto->ox, "extern", 6);
1N/A op = linesync(proto, op, proto->line = line);
1N/A if (flags & DIRECTIVE)
1N/A {
1N/A proto->brace = 0;
1N/A PUTCHR('\n');
1N/A PUTCHR('#');
1N/A }
1N/A else if (!(flags & VARIADIC)) PUTCHR('{');
1N/A }
1N/A }
1N/A flags &= ~(IDID|INDIRECT|MATCH|OTHER|SKIP);
1N/A call = 0;
1N/A group = 0;
1N/A break;
1N/A case '}':
1N/A flags &= ~(IDID|INDIRECT|MATCH|OTHER|SKIP|TOKENS);
1N/A if (--proto->brace == 0)
1N/A {
1N/A flags &= ~(INIT|VARIADIC|VARIADIC2);
1N/A#if PROTOMAIN
1N/A if (flags & EXTERN) BACKOUT();
1N/A#endif
1N/A }
1N/A call = 0;
1N/A group = 0;
1N/A paren = 0;
1N/A break;
1N/A case '=':
1N/A if (last == '?') flags |= DIRECTIVE;
1N/A else if (paren == 0 && (flags & (INIT|MATCH|SKIP)) == MATCH)
1N/A {
1N/A if (last == ')' && proto->brace && (group != 2 || call != 2)) flags |= SKIP;
1N/A else goto fsm_statement;
1N/A }
1N/A goto fsm_other;
1N/A case ',':
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC)
1N/A {
1N/A if (paren == 1) args++;
1N/A else
1N/A {
1N/A args--;
1N/A flags &= ~MATCH;
1N/A }
1N/A break;
1N/A }
1N/A#endif
1N/A if (paren == 0 && (flags & DECLARE)) *(op - 1) = c = ';';
1N/A /*FALLTHROUGH*/
1N/A case ';':
1N/A fsm_statement:
1N/A if (flags & INIT) /* ignore */;
1N/A#if PROTOMAIN
1N/A else if (flags & CLASSIC)
1N/A {
1N/A if (paren == 0)
1N/A {
1N/A if ((flags & MATCH) && last == ')')
1N/A flags &= ~MATCH;
1N/A if (!(flags & MATCH))
1N/A {
1N/A call = 0;
1N/A group = 0;
1N/A flags &= ~SKIP;
1N/A if (flags & EXTERN) BACKOUT();
1N/A if (flags & SLIDE)
1N/A {
1N/A SYNC();
1N/A return 0;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A args--;
1N/A if ((flags & (EXTERN|SKIP)) == (EXTERN|SKIP))
1N/A BACKOUT();
1N/A }
1N/A }
1N/A }
1N/A#endif
1N/A else if (paren == 0)
1N/A {
1N/A if ((flags & (MATCH|OTHER|SKIP)) == MATCH && call > 1)
1N/A {
1N/A if ((flags & MANGLE) && func)
1N/A {
1N/A func[0] = 'F';
1N/A func[1] = 'U';
1N/A func[2] = 'N';
1N/A func[3] = 'C';
1N/A func = 0;
1N/A }
1N/A if ((flags & (DECLARE|INDIRECT)) == INDIRECT && aim && aie < im)
1N/A {
1N/A while (aie < ip && (*aie == ' ' || *aie == '\t' || *aie == '\n')) aie++;
1N/A v = aim;
1N/A while (v < aie)
1N/A if (*v++ == ')') break;
1N/A while (v < aie && (*v == ' ' || *v == '\t' || *v == '\n')) v++;
1N/A if (v == aie || !(flags & PLUSPLUS))
1N/A {
1N/A if (flags & PLUSPLUS) n = 3;
1N/A else if (v == aie && *v == '(') n = 10;
1N/A else n = 11;
1N/A ko = op;
1N/A om += n;
1N/A v = op += n;
1N/A while (v >= ko + n)
1N/A {
1N/A *v = *(v - n);
1N/A v--;
1N/A }
1N/A if (flags & PLUSPLUS) memcopy(aom, "(...))", 6);
1N/A else if (n == 10) memcopy(aom, "(__VARARG__))", 13);
1N/A else
1N/A {
1N/A ko = strcopy(aom, " __PROTO__(");
1N/A ko = memcopy(ko, aim, aie - aim);
1N/A *ko = ')';
1N/A if (++ko >= om)
1N/A {
1N/A *ko++ = ')';
1N/A om = ko;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A else if (flags & TYPEDEF)
1N/A {
1N/A op = om;
1N/A while (*--op == ' ' || *op == '\t' || *op == '\n');
1N/A if (*op != ')')
1N/A {
1N/A op = om += 14;
1N/A *--op = ')';
1N/A while ((x = *(op - 14)) >= 'A' && x <= 'Z' || x >= 'a' && x <= 'z' || x >= '0' && x <= '9' || x == '_')
1N/A *--op = x;
1N/A memcopy(op - 13, "(__OTORP__(*)", 13);
1N/A }
1N/A }
1N/A if (flags & OTHER)
1N/A ;
1N/A else if (flags & PLUSPLUS)
1N/A {
1N/A op = om;
1N/A if (!(flags & TOKENS)) op = strcopy(op, "(...)");
1N/A else op = memcopy(op, im, ie - im);
1N/A PUTCHR(c);
1N/A }
1N/A else
1N/A {
1N/A if (flags & DECLARE) op = strcopy(om, "()");
1N/A else if (!(flags & TOKENS)) op = strcopy(om, "(__VARARG__)");
1N/A else
1N/A {
1N/A op = strcopy(om, " __PROTO__(");
1N/A op = memcopy(op, im, ie - im);
1N/A PUTCHR(')');
1N/A }
1N/A if (flags & EXTERNALIZE) memcpy(proto->ox, "extern", 6);
1N/A PUTCHR(c);
1N/A }
1N/A flags &= ~(MATCH|VARIADIC|VARIADIC2);
1N/A if (c == ',' && !(flags & INDIRECT))
1N/A {
1N/A call = 1;
1N/A group = 0;
1N/A break;
1N/A }
1N/A }
1N/A else if (flags & (OTHER|SKIP)) call = 0;
1N/A if (c == ';')
1N/A {
1N/A flags &= ~(EXTERNALIZE|MANGLE|TOKENS|TYPEDEF);
1N/A call = 0;
1N/A if (flags & SLIDE)
1N/A {
1N/A SYNC();
1N/A return 0;
1N/A }
1N/A }
1N/A else call = call > 1 && c == ',';
1N/A group = 0;
1N/A flags &= ~(IDID|INDIRECT|MATCH|OTHER|SKIP);
1N/A }
1N/A else if (paren == 1 && group == 1 && !(flags & (IDID|MANGLE))) flags |= TOKENS|OTHER;
1N/A break;
1N/A case T_DO:
1N/A case T_IF:
1N/A flags |= TOKENS|SKIP;
1N/A break;
1N/A case T_EXTERN:
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC)
1N/A {
1N/A if (proto->brace == 0)
1N/A flags |= SKIP;
1N/A }
1N/A else
1N/A#endif
1N/A if (paren == 0 && !(flags & TYPEDEF))
1N/A {
1N/A flags |= MANGLE;
1N/A if (!(flags & PLUSONLY) || proto->package)
1N/A {
1N/A op = strcopy(op, " __MANGLE__");
1N/A if (proto->package)
1N/A {
1N/A op = strcopy(op - 1, proto->package);
1N/A func = op + 1;
1N/A op = strcopy(op, "_DATA__");
1N/A }
1N/A }
1N/A else
1N/A func = 0;
1N/A }
1N/A break;
1N/A case T_VARIADIC:
1N/A if (paren == 0 && (flags & (DECLARE|VARIADIC)) == DECLARE)
1N/A {
1N/A op -= 3;
1N/A SYNC();
1N/A return c;
1N/A }
1N/A if (paren == 1 && !(flags & SKIP))
1N/A flags |= VARIADIC;
1N/A flags |= TOKENS;
1N/A break;
1N/A case T_VOID:
1N/A goto fsm_id;
1N/A case T_VA_START:
1N/A if ((flags & (PLUSONLY|VARIADIC)) == VARIADIC)
1N/A {
1N/A flags &= ~MATCH;
1N/A line = proto->line;
1N/A op = strcopy(op - 8, "__VA_START__");
1N/A SYNC();
1N/A for (;;)
1N/A {
1N/A switch (lex(proto, (flags & GLOBAL) | RECURSIVE))
1N/A {
1N/A case 0:
1N/A case ';':
1N/A break;
1N/A case T_ID:
1N/A if (!(flags & MATCH))
1N/A {
1N/A flags |= MATCH;
1N/A m = proto->tp;
1N/A e = proto->ip;
1N/A }
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A CACHE();
1N/A if (flags & MATCH)
1N/A {
1N/A v = m;
1N/A n = e - m;
1N/A }
1N/A else
1N/A {
1N/A v = "ap";
1N/A n = 2;
1N/A }
1N/A op = strcopy(op, " __OTORP__(");
1N/A proto->ip = proto->variadic;
1N/A proto->op = op;
1N/A flags &= ~MATCH;
1N/A group = 0;
1N/A bp = proto->ip + 1;
1N/A if (*bp == 'r' && !strncmp(bp, "register", 8) && (*(bp + 8) == ' ' || *(bp + 8) == '\t')) bp += 9;
1N/A for (;;)
1N/A {
1N/A switch (lex(proto, (flags & GLOBAL) | RECURSIVE))
1N/A {
1N/A case '(':
1N/A if (paren++) group++;
1N/A continue;
1N/A case ')':
1N/A if (--paren == 0)
1N/A {
1N/A if (flags & MATCH)
1N/A {
1N/A flags &= ~MATCH;
1N/A if (!(flags & VARIADIC2))
1N/A {
1N/A op = memcopy(op, m, e - m);
1N/A op = strcopy(op, " = ");
1N/A }
1N/A op = strcopy(op, "va_arg(");
1N/A op = memcopy(op, v, n);
1N/A PUTCHR(',');
1N/A PUTCHR(' ');
1N/A if (m > bp) op = memcopy(op, bp, m - bp);
1N/A else op = strcopy(op, "int ");
1N/A if (group > 1) op = strcopy(op, ")()");
1N/A else op = memcopy(op, e, proto->ip - e - 1);
1N/A PUTCHR(')');
1N/A PUTCHR(';');
1N/A }
1N/A group = 0;
1N/A break;
1N/A }
1N/A continue;
1N/A case ',':
1N/A if (paren == 1)
1N/A {
1N/A if (flags & MATCH)
1N/A {
1N/A flags &= ~MATCH;
1N/A if (!(flags & VARIADIC2))
1N/A {
1N/A op = memcopy(op, m, e - m);
1N/A op = strcopy(op, " = ");
1N/A }
1N/A op = strcopy(op, "va_arg(");
1N/A op = memcopy(op, v, n);
1N/A PUTCHR(',');
1N/A PUTCHR(' ');
1N/A if (m > bp) op = memcopy(op, bp, m - bp);
1N/A else op = strcopy(op, "int ");
1N/A if (group > 1) op = strcopy(op, ")()");
1N/A else op = memcopy(op, e, proto->ip - e - 1);
1N/A PUTCHR(')');
1N/A PUTCHR(';');
1N/A bp = proto->ip + 1;
1N/A if (*bp == 'r' && !strncmp(bp, "register", 8) && (*(bp + 8) == ' ' || *(bp + 8) == '\t')) bp += 9;
1N/A }
1N/A group = 0;
1N/A proto->op = op;
1N/A }
1N/A continue;
1N/A case T_ID:
1N/A if (group <= 1)
1N/A {
1N/A flags |= MATCH;
1N/A m = proto->tp;
1N/A e = proto->ip;
1N/A }
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A op = strcopy(op, ")");
1N/A flags |= VARIADIC2;
1N/A proto->line = line;
1N/A call = 0;
1N/A break;
1N/A }
1N/A /*FALLTHROUGH*/
1N/A case T_ID:
1N/A fsm_id:
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC)
1N/A {
1N/A if (!args && paren == 1) args++;
1N/A break;
1N/A }
1N/A#endif
1N/A if (paren == 0)
1N/A {
1N/A if (last == ')')
1N/A {
1N/A if (proto->brace == 0 && !(flags & DECLARE)) flags |= SKIP;
1N/A call = !call;
1N/A }
1N/A else if ((flags & SKIP) || c == T_ID || c == T_VOID) call++;
1N/A else flags |= SKIP;
1N/A if (last == T_ID) flags |= IDID;
1N/A }
1N/A c = T_ID;
1N/A flags |= TOKENS;
1N/A break;
1N/A case T_INVALID:
1N/A if (*proto->tp >= '0' && *proto->tp <= '9')
1N/A {
1N/A n = 0;
1N/A for (;; op--)
1N/A {
1N/A switch (*(op - 1))
1N/A {
1N/A case 'f':
1N/A case 'F':
1N/A t = op;
1N/A while ((c = *--t) >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
1N/A if (*t == '.')
1N/A op--;
1N/A n = 0;
1N/A break;
1N/A case 'l':
1N/A case 'L':
1N/A if (!(n & 01))
1N/A {
1N/A n |= 01;
1N/A t = op;
1N/A while ((c = *--t) >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
1N/A if (*t == '.')
1N/A {
1N/A n = 0;
1N/A op--;
1N/A break;
1N/A }
1N/A }
1N/A continue;
1N/A case 'u':
1N/A case 'U':
1N/A n |= 02;
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A if (n & 01)
1N/A *op++ = 'L';
1N/A if (n & 02)
1N/A {
1N/A m = op;
1N/A t = op = m + 10;
1N/A while ((c = *--m) >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
1N/A *--t = c;
1N/A c = *t;
1N/A strcopy(m + 1, "(unsigned)");
1N/A *t = c;
1N/A break;
1N/A }
1N/A }
1N/A goto fsm_other;
1N/A#if PROTOMAIN
1N/A case '[':
1N/A if ((flags & CLASSIC) && paren == 0 && group <= 2) flags |= SKIP;
1N/A /*FALLTHROUGH*/
1N/A#endif
1N/A default:
1N/A fsm_other:
1N/A#if PROTOMAIN
1N/A if (flags & CLASSIC) break;
1N/A#endif
1N/A flags |= TOKENS;
1N/A if (paren == 0) flags |= OTHER;
1N/A break;
1N/A }
1N/A else if (c == '#' && *ip != '(') flags |= SHARP;
1N/A last = c;
1N/A#if PROTOMAIN
1N/A if ((flags & (EXTERN|MATCH)) == (EXTERN|MATCH) && ((flags & (DIRECTIVE|SKIP)) || proto->brace || c != '(' && c != ')' && c != '*' && c != T_ID))
1N/A CACHEOUT();
1N/A else
1N/A#endif
1N/A SYNCOUT();
1N/A goto fsm_start;
1N/A }
1N/A else if (flags & (INIT_DEFINE|INIT_INCLUDE))
1N/A {
1N/A#if PROTOMAIN
1N/A if ((flags & YACC) && c == '%' && *ip == '{') t = 0;
1N/A else
1N/A#endif
1N/A {
1N/A if (c == '#') for (t = ip; *t == ' ' || *t == '\t'; t++);
1N/A else t = "";
1N/A if (*t++ == 'i' && *t++ == 'f' && *t++ == 'n' && *t++ == 'd' && *t++ == 'e' && *t++ == 'f')
1N/A {
1N/A#if !PROTOMAIN
1N/A while (*t == ' ' || *t == '\t') t++;
1N/A if (*t != '_')
1N/A#endif
1N/A t = 0;
1N/A }
1N/A }
1N/A if (t)
1N/A {
1N/A ip = bp;
1N/A op = proto->op;
1N/A }
1N/A else while (*ip != '\n') *op++ = *ip++;
1N/A op = init(proto, op, flags);
1N/A op = linesync(proto, op, proto->line);
1N/A flags &= ~(INIT_DEFINE|INIT_INCLUDE);
1N/A proto->flags &= ~(INIT_DEFINE|INIT_INCLUDE);
1N/A goto fsm_start;
1N/A }
1N/A SYNC();
1N/A return c;
1N/A}
1N/A
1N/A/*
1N/A * close a proto buffer stream
1N/A */
1N/A
1N/Avoid
1N/Apppclose(char* iob)
1N/A{
1N/A register struct proto* proto = (struct proto*)(iob - sizeof(struct proto));
1N/A
1N/A if (proto->flags & MORE) close(proto->fd);
1N/A free((char*)proto); /* some ANSI cc's botch the free() prototype */
1N/A}
1N/A
1N/A/*
1N/A * open a new proto buffer stream
1N/A * read buffer pointer returned
1N/A * 0 returned on error or if no magic
1N/A *
1N/A * file !=0 file path to open, otherwise use fd
1N/A * fd open file fd if file==0
1N/A * notice !=0 copyright notice info commented at the top
1N/A * options !=0 additional notice name=value pairs, space or ; separated
1N/A * package !=0 generate header for this package
1N/A */
1N/A
1N/Achar*
1N/Apppopen(char* file, int fd, char* notice, char* options, char* package, char* comment, int flags)
1N/A{
1N/A register struct proto* proto;
1N/A register char* iob;
1N/A register long n;
1N/A register char* s;
1N/A int pragma;
1N/A char* b;
1N/A#if PROTOMAIN
1N/A int comlen;
1N/A char com[80];
1N/A#endif
1N/A int m = 0;
1N/A
1N/A static int retain;
1N/A
1N/A /*
1N/A * initialize proto
1N/A */
1N/A
1N/A#if PROTOMAIN
1N/A if (flags & PROTO_CLASSIC) flags &= ~PROTO_INCLUDE;
1N/A#endif
1N/A if (flags & PROTO_RETAIN) flags &= ~retain;
1N/A else retain &= PROTO_INITIALIZED;
1N/A if (file && (fd = open(file, O_RDONLY)) < 0) return 0;
1N/A#if !PROTOMAIN
1N/A if ((n = lseek(fd, 0L, 2)) > 0)
1N/A {
1N/A if (lseek(fd, 0L, 0)) return 0;
1N/A if (n < CHUNK) n = CHUNK;
1N/A else if (n > 2 * BLOCK) n = 0;
1N/A m = 1;
1N/A }
1N/A if (n > 0)
1N/A {
1N/A /*
1N/A * file read in one chunk
1N/A */
1N/A
1N/A if (!(proto = newof(0, struct proto, 1, 4 * n + 2)))
1N/A return 0;
1N/A proto->iz = n;
1N/A proto->oz = 3 * n;
1N/A n = 0;
1N/A }
1N/A else
1N/A#endif
1N/A {
1N/A /*
1N/A * file read in BLOCK chunks
1N/A */
1N/A
1N/A n = BLOCK;
1N/A if (!(proto = newof(0, struct proto, 1, 5 * n + 2)))
1N/A return 0;
1N/A proto->iz = n;
1N/A proto->oz = 3 * n;
1N/A proto->flags |= MORE;
1N/A }
1N/A proto->fd = fd;
1N/A proto->package = package;
1N/A iob = (char*)proto + sizeof(struct proto);
1N/A proto->op = proto->ob = iob;
1N/A proto->ip = proto->ib = iob + proto->oz + n;
1N/A if (m) proto->options |= REGULAR;
1N/A if (!comment)
1N/A comment = "/*";
1N/A if (!(proto->cc[0] = comment[0]))
1N/A notice = options = 0;
1N/A else if (comment[1])
1N/A {
1N/A proto->cc[1] = comment[1];
1N/A proto->cc[2] = comment[2] ? comment[2] : comment[0];
1N/A }
1N/A else
1N/A proto->cc[1] = proto->cc[2] = comment[0];
1N/A
1N/A /*
1N/A * read the first chunk
1N/A */
1N/A
1N/A n = read(fd, proto->ip, proto->iz);
1N/A if (!(proto->flags & MORE))
1N/A close(fd);
1N/A if (n < 0)
1N/A {
1N/A pppclose(iob);
1N/A return 0;
1N/A }
1N/A *(proto->ip + n) = 0;
1N/A
1N/A /*
1N/A * check for proto pragma in first block of lines
1N/A * pragma blanked out if found
1N/A *
1N/A * -1 no pragma
1N/A * 0 #pragma noprototyped
1N/A * 1 #pragma prototyped
1N/A *
1N/A * NOTE: matches may occur inside comments and quotes
1N/A */
1N/A
1N/A#if PROTOMAIN
1N/A if (!notice && !options || (comlen = astlicense(com, sizeof(com), NiL, "type=check", proto->cc[0], proto->cc[1], proto->cc[2])) <= 0)
1N/A *com = 0;
1N/A#endif
1N/A pragma = -1;
1N/A s = proto->ip;
1N/A m = MAGICTOP;
1N/A while (m-- > 0 && *s)
1N/A {
1N/A while (*s == ' ' || *s == '\t') s++;
1N/A if (*s == '#')
1N/A {
1N/A b = s++;
1N/A while (*s == ' ' || *s == '\t') s++;
1N/A if (!strncmp(s, MAGICDIR, sizeof(MAGICDIR) - 1) && (*(s += sizeof(MAGICDIR) - 1) == ' ' || *s == '\t'))
1N/A {
1N/A while (*s == ' ' || *s == '\t') s++;
1N/A if (*s == 'n' && *(s + 1) == 'o')
1N/A {
1N/A s += 2;
1N/A pragma = -2;
1N/A }
1N/A if (!strncmp(s, MAGICARG, sizeof(MAGICARG) - 1) && (*(s += sizeof(MAGICARG) - 1) == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
1N/A while (*s)
1N/A {
1N/A if ((*(s - 1) == ' ' || *(s - 1) == '\t') && *s == *MAGICOFF && !strncmp(s, MAGICOFF, sizeof(MAGICOFF) - 1))
1N/A notice = options = 0;
1N/A if (*s++ == '\n')
1N/A {
1N/A pragma += 2;
1N/A#if PROTOMAIN
1N/A if (!(flags & PROTO_DISABLE) || (flags & PROTO_NOPRAGMA))
1N/A#endif
1N/A for (s--; b < s; *b++ = ' ');
1N/A goto magic;
1N/A }
1N/A }
1N/A pragma = -1;
1N/A }
1N/A }
1N/A else if (*s == '/' && !strncmp(s, MAGICGEN, sizeof(MAGICGEN) - 1))
1N/A {
1N/A pragma = 0;
1N/A break;
1N/A }
1N/A#if PROTOMAIN
1N/A else if (*s == '%' && *(s + 1) == '{')
1N/A proto->flags |= YACC;
1N/A if (notice || options)
1N/A {
1N/A if (*s == *com && !strncmp(s, com, comlen))
1N/A notice = options = 0;
1N/A else
1N/A while (*s)
1N/A {
1N/A if (*s == *NOTICED && !strncmp(s, NOTICED, sizeof(NOTICED) - 1))
1N/A {
1N/A s += sizeof(NOTICED) - 1;
1N/A while (*s == ' ' || *s == '\t')
1N/A s++;
1N/A if (*s == '(' && (*(s + 1) == 'c' || *(s + 1) == 'C') && *(s + 2) == ')' || *s >= '0' && *s <= '9' && *(s + 1) >= '0' && *(s + 1) <= '9')
1N/A {
1N/A notice = options = 0;
1N/A break;
1N/A }
1N/A }
1N/A if (*s == *PUBLICDOMAIN && !strncmp(s, PUBLICDOMAIN, sizeof(PUBLICDOMAIN) - 1))
1N/A {
1N/A notice = options = 0;
1N/A break;
1N/A }
1N/A else if (*s++ == '\n')
1N/A {
1N/A s--;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A#endif
1N/A while (*s && *s++ != '\n');
1N/A }
1N/A magic:
1N/A if (flags & PROTO_PLUSPLUS) proto->flags |= PLUSPLUS;
1N/A if (flags & PROTO_TEST) proto->test = 1;
1N/A if (flags & PROTO_EXTERNALIZE) proto->options |= EXTERNALIZE;
1N/A#if PROTOMAIN
1N/A if (flags & PROTO_CLASSIC) pragma = -pragma;
1N/A if (flags & PROTO_DISABLE) pragma = 0;
1N/A if (flags & PROTO_LINESYNC) proto->flags |= LINESYNC;
1N/A if (!(proto->flags & YACC) && file && (m = strlen(file)) > 2 && file[--m] == 'y' && file[--m] == '.')
1N/A proto->flags |= YACC;
1N/A#endif
1N/A if (pragma <= 0)
1N/A {
1N/A if (flags & PROTO_PLUSPLUS)
1N/A {
1N/A flags &= ~(PROTO_HEADER|PROTO_INCLUDE);
1N/A proto->flags |= PLUSONLY;
1N/A }
1N/A else if (!(flags & (PROTO_FORCE|PROTO_PASS)))
1N/A {
1N/A pppclose(iob);
1N/A return 0;
1N/A }
1N/A else if ((flags & (PROTO_FORCE|PROTO_PASS)) == PROTO_PASS || !pragma)
1N/A {
1N/A proto->flags |= PASS;
1N/A if (proto->flags & MORE)
1N/A proto->oz += proto->iz;
1N/A proto->iz = n;
1N/A if (notice || options)
1N/A {
1N/A if (proto->cc[0] == '#' && proto->ip[0] == '#' && proto->ip[1] == '!')
1N/A {
1N/A s = proto->ip;
1N/A while (*s && *s++ != '\n');
1N/A m = s - proto->ip;
1N/A proto->op = memcopy(proto->op, proto->ip, m);
1N/A proto->ip = s;
1N/A proto->iz = n -= m;
1N/A }
1N/A#if PROTOMAIN
1N/A if (proto->cc[0])
1N/A {
1N/A if ((comlen = astlicense(proto->op, proto->oz, notice, options, proto->cc[0], proto->cc[1], proto->cc[2])) < 0)
1N/A proto_error((char*)proto + sizeof(struct proto), 1, proto->op, NiL);
1N/A else
1N/A proto->op += comlen;
1N/A }
1N/A if (!(flags & PROTO_CLASSIC) && !(proto->flags & YACC))
1N/A#endif
1N/A proto->op = linesync(proto, proto->op, 1);
1N/A proto->iz += proto->op - proto->ob;
1N/A }
1N/A memcopy(proto->op, proto->ip, n);
1N/A return iob;
1N/A }
1N/A }
1N/A#if PROTOMAIN
1N/A if (!(retain & PROTO_INITIALIZED))
1N/A {
1N/A retain |= PROTO_INITIALIZED;
1N/A ppfsm(FSM_INIT, NiL);
1N/A }
1N/A#endif
1N/A proto->line = 1;
1N/A#if CHUNK >= 512
1N/A if (notice || options || (flags & (PROTO_HEADER|PROTO_INCLUDE)))
1N/A {
1N/A#if PROTOMAIN
1N/A if (notice || options)
1N/A {
1N/A if ((comlen = astlicense(proto->op, proto->oz, notice, options, proto->cc[0], proto->cc[1], proto->cc[2])) < 0)
1N/A proto_error((char*)proto + sizeof(struct proto), 1, proto->op, NiL);
1N/A else
1N/A proto->op += comlen;
1N/A }
1N/A#endif
1N/A if (flags & PROTO_INCLUDE)
1N/A {
1N/A proto->flags |= INIT_INCLUDE;
1N/A if (flags & PROTO_RETAIN)
1N/A retain |= PROTO_INCLUDE;
1N/A }
1N/A else if (flags & PROTO_HEADER)
1N/A {
1N/A if (flags & PROTO_RETAIN) retain |= PROTO_HEADER;
1N/A#if PROTOMAIN
1N/A if (flags & PROTO_CLASSIC)
1N/A {
1N/A *proto->op++ = '#';
1N/A proto->op = strcopy(proto->op, MAGICDIR);
1N/A *proto->op++ = ' ';
1N/A proto->op = strcopy(proto->op, MAGICARG);
1N/A *proto->op++ = '\n';
1N/A }
1N/A else
1N/A#endif
1N/A proto->flags |= INIT_DEFINE;
1N/A }
1N/A#if PROTOMAIN
1N/A if (!(flags & PROTO_CLASSIC))
1N/A {
1N/A if (proto->flags & YACC)
1N/A {
1N/A proto->op = strcopy(proto->op, "\n%{\n" + !notice);
1N/A proto->op = strcopy(proto->op, MAGICGEN);
1N/A proto->op = strcopy(proto->op, "%}\n");
1N/A }
1N/A else
1N/A {
1N/A if (n || notice || options)
1N/A *proto->op++ = '\n';
1N/A proto->op = strcopy(proto->op, MAGICGEN);
1N/A if (n)
1N/A proto->op = linesync(proto, proto->op, proto->line);
1N/A else if (proto->flags & (INIT_DEFINE|INIT_INCLUDE))
1N/A proto->op = init(proto, proto->op, proto->flags);
1N/A }
1N/A }
1N/A#endif
1N/A }
1N/A#endif
1N/A#if PROTOMAIN
1N/A proto->file = file;
1N/A if (flags & PROTO_CLASSIC)
1N/A {
1N/A proto->flags |= CLASSIC;
1N/A if (!(flags & PROTO_HEADER)) proto->flags |= EXTERN;
1N/A }
1N/A#endif
1N/A return iob;
1N/A}
1N/A
1N/A/*
1N/A * read next proto'd chunk into iob
1N/A * the chunk is 0 terminated and its size is returned
1N/A */
1N/A
1N/Aint
1N/Apppread(char* iob)
1N/A{
1N/A register struct proto* proto = (struct proto*)(iob - sizeof(struct proto));
1N/A register int n;
1N/A
1N/A if (proto->flags & PASS)
1N/A {
1N/A if (proto->iz)
1N/A {
1N/A n = proto->iz;
1N/A proto->iz = 0;
1N/A }
1N/A else if (!(proto->flags & MORE)) n = 0;
1N/A else if ((n = read(proto->fd, proto->ob, proto->oz)) <= 0 || (proto->options & REGULAR) && n < proto->oz)
1N/A {
1N/A proto->flags &= ~MORE;
1N/A close(proto->fd);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (proto->op == proto->ob)
1N/A {
1N/A if (proto->flags & ERROR) return -1;
1N/A#if PROTOMAIN
1N/A if (proto->flags & YACC)
1N/A {
1N/A register char* ip = proto->ip;
1N/A register char* op = proto->ob;
1N/A register char* ep = proto->ob + proto->oz - 2;
1N/A
1N/A if (!*ip)
1N/A {
1N/A ip = proto->ip = proto->ib;
1N/A if (!(proto->flags & MORE)) n = 0;
1N/A else if ((n = read(proto->fd, ip, proto->iz)) <= 0 || (proto->options & REGULAR) && n < proto->iz)
1N/A {
1N/A if (n < 0) n = 0;
1N/A proto->flags &= ~MORE;
1N/A close(proto->fd);
1N/A }
1N/A ip[n] = 0;
1N/A }
1N/A if (proto->flags & YACCSPLIT)
1N/A {
1N/A proto->flags &= ~YACCSPLIT;
1N/A if (*ip == '%')
1N/A {
1N/A *op++ = *ip++;
1N/A if (proto->flags & YACC2) proto->flags &= ~YACC;
1N/A else proto->flags |= YACC2;
1N/A }
1N/A }
1N/A if (proto->flags & YACC)
1N/A while (op < ep && (n = *op++ = *ip))
1N/A {
1N/A ip++;
1N/A if (n == '%')
1N/A {
1N/A if (*ip == '%' && (ip == proto->ip + 1 || *(ip - 2) == '\n'))
1N/A {
1N/A *op++ = *ip++;
1N/A if (proto->flags & YACC2) proto->flags &= ~YACC;
1N/A else proto->flags |= YACC2;
1N/A break;
1N/A }
1N/A if (!*ip)
1N/A {
1N/A *op++ = '%';
1N/A proto->flags |= YACCSPLIT;
1N/A break;
1N/A }
1N/A }
1N/A else if (n == '\n') proto->line++;
1N/A }
1N/A proto->op = memcopy(proto->ob, proto->ip, ip - proto->ip);
1N/A proto->ip = ip;
1N/A }
1N/A else
1N/A#endif
1N/A lex(proto, proto->flags);
1N/A if ((proto->flags & (ERROR|MORE)) == ERROR)
1N/A proto->op = strcopy(proto->op, "/* NOTE: some constructs may not have been converted */\n");
1N/A }
1N/A n = proto->op - proto->ob;
1N/A proto->op = proto->ob;
1N/A }
1N/A return n;
1N/A}
1N/A
1N/A#if !PROTOMAIN
1N/A
1N/A/*
1N/A * drop control of iob after first pppread()
1N/A * return value is input fd
1N/A * if fd<0 then all data in iob
1N/A */
1N/A
1N/Aint
1N/Apppdrop(char* iob)
1N/A{
1N/A register struct proto* proto = (struct proto*)(iob - sizeof(struct proto));
1N/A
1N/A if (proto->flags & MORE)
1N/A {
1N/A proto->flags &= ~MORE;
1N/A return proto->fd;
1N/A }
1N/A return -1;
1N/A}
1N/A
1N/A#endif