xref: /openbsd/gnu/usr.bin/gcc/gcc/f/ansify.c (revision c87b03e5)
1 /* ansify.c
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    Contributed by James Craig Burley.
4 
5 This file is part of GNU Fortran.
6 
7 GNU Fortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11 
12 GNU Fortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GNU Fortran; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21 
22 #include "hconfig.h"
23 #include "system.h"
24 
25 #define die_unless(c) \
26   do if (!(c)) \
27     { \
28       fprintf (stderr, "%s:%lu: %s\n", argv[1], lineno, #c); \
29       die (); \
30     } \
31   while(0)
32 
33 static void ATTRIBUTE_NORETURN
die(void)34 die (void)
35 {
36   exit (1);
37 }
38 
39 int
main(int argc,char ** argv)40 main(int argc, char **argv)
41 {
42   int c;
43   static unsigned long lineno = 1;
44 
45   die_unless (argc == 2);
46 
47   printf ("\
48 /* This file is automatically generated from `%s',\n\
49    which you should modify instead.  */\n\
50 #line 1 \"%s\"\n\
51 ",
52 	  argv[1], argv[1]);
53 
54   while ((c = getchar ()) != EOF)
55     {
56       switch (c)
57 	{
58 	default:
59 	  putchar (c);
60 	  break;
61 
62 	case '\n':
63 	  ++lineno;
64 	  putchar (c);
65 	  break;
66 
67 	case '"':
68 	  putchar (c);
69 	  for (;;)
70 	    {
71 	      c = getchar ();
72 	      die_unless (c != EOF);
73 	      switch (c)
74 		{
75 		case '"':
76 		  putchar (c);
77 		  goto next_char;
78 
79 		case '\n':
80 		  putchar ('\\');
81 		  putchar ('n');
82 		  putchar ('\\');
83 		  putchar ('\n');
84 		  ++lineno;
85 		  break;
86 
87 		case '\\':
88 		  putchar (c);
89 		  c = getchar ();
90 		  die_unless (c != EOF);
91 		  putchar (c);
92 		  if (c == '\n')
93 		    ++lineno;
94 		  break;
95 
96 		default:
97 		  putchar (c);
98 		  break;
99 		}
100 	    }
101 	  break;
102 
103 	case '\'':
104 	  putchar (c);
105 	  for (;;)
106 	    {
107 	      c = getchar ();
108 	      die_unless (c != EOF);
109 	      switch (c)
110 		{
111 		case '\'':
112 		  putchar (c);
113 		  goto next_char;
114 
115 		case '\n':
116 		  putchar ('\\');
117 		  putchar ('n');
118 		  putchar ('\\');
119 		  putchar ('\n');
120 		  ++lineno;
121 		  break;
122 
123 		case '\\':
124 		  putchar (c);
125 		  c = getchar ();
126 		  die_unless (c != EOF);
127 		  putchar (c);
128 		  if (c == '\n')
129 		    ++lineno;
130 		  break;
131 
132 		default:
133 		  putchar (c);
134 		  break;
135 		}
136 	    }
137 	  break;
138 
139 	case '/':
140 	  putchar (c);
141 	  c = getchar ();
142 	  putchar (c);
143 	  if (c != '*')
144 	    break;
145 	  for (;;)
146 	    {
147 	      c = getchar ();
148 	      die_unless (c != EOF);
149 
150 	      switch (c)
151 		{
152 		case '\n':
153 		  ++lineno;
154 		  putchar (c);
155 		  break;
156 
157 		case '*':
158 		  c = getchar ();
159 		  die_unless (c != EOF);
160 		  if (c == '/')
161 		    {
162 		      putchar ('*');
163 		      putchar ('/');
164 		      goto next_char;
165 		    }
166 		  if (c == '\n')
167 		    {
168 		      ++lineno;
169 		      putchar (c);
170 		    }
171 		  break;
172 
173 		default:
174 		  /* Don't bother outputting content of comments.  */
175 		  break;
176 		}
177 	    }
178 	  break;
179 	}
180 
181     next_char:
182       ;
183     }
184 
185   die_unless (c == EOF);
186 
187   return 0;
188 }
189