/* Generated by re2c */
#line 1 "strip_002.re"
// re2c $INPUT -o $OUTPUT -s
/* re2c lesson 002_strip_comments, strip_002, (c) M. Boerger 2006 - 2007 */
#line 32 "strip_002.re"


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#line 13 "strip_002.c"
#define YYMAXFILL 2
#line 38 "strip_002.re"

#define	BSIZE	128

#if BSIZE < YYMAXFILL
# error BSIZE must be greater YYMAXFILL
#endif

#define	YYCTYPE		unsigned char
#define	YYCURSOR	s.cur
#define	YYLIMIT		s.lim
#define YYMARKER	s.mrk
#define	YYFILL(n)	{ if ((res = fill(&s, n)) >= 0) break; }

typedef struct Scanner
{
	FILE			*fp;
	unsigned char	*cur, *tok, *lim, *eof, *mrk;
	unsigned char 	buffer[BSIZE];
} Scanner;

int fill(Scanner *s, int len)
{
	if (!len)
	{
		s->cur = s->tok = s->lim = s->mrk = s->buffer;
		s->eof = 0;
	}
	if (!s->eof)
	{
		int got, cnt = s->tok - s->buffer;

		if (cnt > 0)
		{
			memcpy(s->buffer, s->tok, s->lim - s->tok);
			s->tok -= cnt;
			s->cur -= cnt;
			s->lim -= cnt;
			s->mrk -= cnt;
		}
		cnt = BSIZE - cnt;
		if ((got = fread(s->lim, 1, cnt, s->fp)) != cnt)
		{
			s->eof = &s->lim[got];
		}
		s->lim += got;
	}
	else if (s->cur + len > s->eof)
	{
		return 0; /* not enough input data */
	}
	return -1;
}

void echo(Scanner *s)
{
	fwrite(s->tok, 1, s->cur - s->tok, stdout);
}

int scan(FILE *fp)
{
	int  res = 0;
    Scanner s;

	if (!fp)
	{
		return 1; /* no file was opened */
	}

    s.fp = fp;
	
	fill(&s, 0);

	for(;;)
	{
		s.tok = s.cur;

#line 92 "strip_002.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych == '/') goto yy2;
			++YYCURSOR;
yy1:
#line 122 "strip_002.re"
			{ fputc(*s.tok, stdout); continue; }
#line 102 "strip_002.c"
yy2:
			yych = *++YYCURSOR;
			if (yych == '*') goto yy3;
			if (yych == '/') goto yy4;
			goto yy1;
yy3:
			++YYCURSOR;
#line 121 "strip_002.re"
			{ goto comment; }
#line 112 "strip_002.c"
yy4:
			++YYCURSOR;
#line 120 "strip_002.re"
			{ goto cppcomment; }
#line 117 "strip_002.c"
		}
#line 123 "strip_002.re"

comment:
		s.tok = s.cur;

#line 124 "strip_002.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych == '*') goto yy7;
			++YYCURSOR;
yy6:
#line 128 "strip_002.re"
			{ goto comment; }
#line 134 "strip_002.c"
yy7:
			yych = *++YYCURSOR;
			if (yych != '/') goto yy6;
			++YYCURSOR;
#line 127 "strip_002.re"
			{ goto commentws; }
#line 141 "strip_002.c"
		}
#line 129 "strip_002.re"

commentws:
		s.tok = s.cur;

#line 148 "strip_002.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych <= '\f') {
				if (yych <= 0x08) goto yy9;
				if (yych <= '\t') goto yy10;
				if (yych <= '\n') goto yy12;
			} else {
				if (yych <= '\r') goto yy13;
				if (yych == ' ') goto yy10;
			}
yy9:
			++YYCURSOR;
#line 135 "strip_002.re"
			{ echo(&s); continue; }
#line 165 "strip_002.c"
yy10:
			++YYCURSOR;
yy11:
#line 134 "strip_002.re"
			{ goto commentws; }
#line 171 "strip_002.c"
yy12:
			++YYCURSOR;
#line 133 "strip_002.re"
			{ echo(&s); continue; }
#line 176 "strip_002.c"
yy13:
			yych = *++YYCURSOR;
			if (yych == '\n') goto yy12;
			goto yy11;
		}
#line 136 "strip_002.re"

cppcomment:
		s.tok = s.cur;

#line 187 "strip_002.c"
		{
			YYCTYPE yych;
			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
			yych = *YYCURSOR;
			if (yych == '\n') goto yy16;
			if (yych == '\r') goto yy17;
			++YYCURSOR;
yy15:
#line 141 "strip_002.re"
			{ goto cppcomment; }
#line 198 "strip_002.c"
yy16:
			++YYCURSOR;
#line 140 "strip_002.re"
			{ echo(&s); continue; }
#line 203 "strip_002.c"
yy17:
			yych = *++YYCURSOR;
			if (yych == '\n') goto yy16;
			goto yy15;
		}
#line 142 "strip_002.re"

	}

	if (fp != stdin)
	{
		fclose(fp); /* close only if not stdin */
	}
	return res; /* return result */
}

int main(int argc, char **argv)
{
	if (argc > 1)
	{
		return scan(!strcmp(argv[1], "-") ? stdin : fopen(argv[1], "r"));
	}
	else
	{
		fprintf(stderr, "%s <expr>\n", argv[0]);
		return 1;
	}
}
