Fix typo in license headers
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / pax / nocom.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: nocom.c /main/2 1996/05/08 19:40:06 drk $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
29 *                          AT&T CORP.                          *
30 *                                                              *
31 *                Copyright (c) 1995 AT&T Corp.                 *
32 *                     All Rights Reserved                      *
33 *                                                              *
34 *           This software is licensed by AT&T Corp.            *
35 *       under the terms and conditions of the license in       *
36 *       http://www.research.att.com/orgs/ssr/book/reuse        *
37 *                                                              *
38 *               This software was created by the               *
39 *           Software Engineering Research Department           *
40 *                    AT&T Bell Laboratories                    *
41 *                                                              *
42 *               For further information contact                *
43 *                     gsf@research.att.com                     *
44 *                                                              *
45 ***************************************************************/
46
47 /* : : generated by proto : : */
48
49 #if !defined(__PROTO__)
50 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
51 #if defined(__cplusplus)
52 #define __MANGLE__      "C"
53 #else
54 #define __MANGLE__
55 #endif
56 #define __STDARG__
57 #define __PROTO__(x)    x
58 #define __OTORP__(x)
59 #define __PARAM__(n,o)  n
60 #if !defined(__STDC__) && !defined(__cplusplus)
61 #if !defined(c_plusplus)
62 #define const
63 #endif
64 #define signed
65 #define void            int
66 #define volatile
67 #define __V_            char
68 #else
69 #define __V_            void
70 #endif
71 #else
72 #define __PROTO__(x)    ()
73 #define __OTORP__(x)    x
74 #define __PARAM__(n,o)  o
75 #define __MANGLE__
76 #define __V_            char
77 #define const
78 #define signed
79 #define void            int
80 #define volatile
81 #endif
82 #if defined(__cplusplus) || defined(c_plusplus)
83 #define __VARARG__      ...
84 #else
85 #define __VARARG__
86 #endif
87 #if defined(__STDARG__)
88 #define __VA_START__(p,a)       va_start(p,a)
89 #else
90 #define __VA_START__(p,a)       va_start(p)
91 #endif
92 #endif
93 static const char id[] = "\n@(#)nocom (AT&T Bell Laboratories) 08/11/94\0\n";
94
95 #include <ast.h>
96 #include <ctype.h>
97 #include <error.h>
98
99 #define DATA(c) do                                                      \
100                 {                                                       \
101                         data = 1;                                       \
102                         if (line > prev)                                \
103                         {                                               \
104                                 if (sfsprintf(buf, sizeof(buf), "#%s %d\n", sync <= 0 ? "line" : "", line) <= line - prev)      \
105                                 {                                       \
106                                         sfputr(sfstdout, buf, -1);      \
107                                         prev = line;                    \
108                                 }                                       \
109                                 else while (prev++ < line)              \
110                                         sfputc(sfstdout, '\n');         \
111                         }                                               \
112                         sfputc(sfstdout, c);                            \
113                 } while (0)
114
115 /*
116  * uncomment sp
117  */
118
119 static void
120 nocom __PARAM__((register Sfio_t* sp, char* file), (sp, file)) __OTORP__(register Sfio_t* sp; char* file;){
121         register int    c = 0;
122         register int    p;
123         register int    data = 0;
124         int             sync = 0;
125         unsigned long   line = 0;
126         unsigned long   prev = 0;
127         unsigned long   directive = 0;
128         int             quote;
129         char*           s;
130         char            buf[PATH_MAX];
131
132         for (;;)
133         {
134         next:
135                 p = c;
136                 c = sfgetc(sp);
137         check:
138                 switch (c)
139                 {
140                 case EOF:
141                         return;
142                 case 0:
143                         sfputc(sfstdout, c);
144                         sfmove(sp, sfstdout, SF_UNBOUND, -1);
145                         return;
146                 case '\\':
147                         DATA(c);
148                         switch (c = sfgetc(sp))
149                         {
150                         case EOF:
151                                 return;
152                         case '\n':
153                                 prev = ++line;
154                                 break;
155                         }
156                         sfputc(sfstdout, c);
157                         break;
158                 case '\f':
159                 case '\t':
160                 case '\013':
161                         c = ' ';
162                         /*FALLTHROUGH*/
163                 case ' ':
164                         if (data) switch (p)
165                         {
166                         case ' ':
167                         case '\n':
168                         case '(':
169                         case '[':
170                         case ']':
171                         case '{':
172                         case '}':
173                         case ';':
174                         case ':':
175                         case '!':
176                         case '<':
177                         case '>':
178                         case '|':
179                         case ',':
180                         case '?':
181                         case '*':
182                                 break;
183                         case ')':
184                                 if (directive == line)
185                                 {
186                                         directive = 0;
187                                         sfputc(sfstdout, ' ');
188                                 }
189                                 break;
190                         default:
191                                 switch (c = sfgetc(sp))
192                                 {
193                                 case '\n':
194                                 case ')':
195                                 case '[':
196                                 case ']':
197                                 case '{':
198                                 case '}':
199                                 case ';':
200                                 case ':':
201                                 case '=':
202                                 case '!':
203                                 case '<':
204                                 case '>':
205                                 case '|':
206                                 case ',':
207                                 case '?':
208                                         break;
209                                 case '(':
210                                         if (directive == line)
211                                         {
212                                                 directive = 0;
213                                                 sfputc(sfstdout, ' ');
214                                         }
215                                         break;
216                                 case '*':
217                                         if (p == '/' || p == '=')
218                                                 sfputc(sfstdout, ' ');
219                                         break;
220                                 case '+':
221                                 case '-':
222                                 case '&':
223                                         if (p == c || p == '=')
224                                                 sfputc(sfstdout, ' ');
225                                         break;
226                                 default:
227                                         if (p != '=')
228                                                 sfputc(sfstdout, ' ');
229                                         break;
230                                 }
231                                 p = ' ';
232                                 goto check;
233                         }
234                         break;
235                 case '\n':
236                         line++;
237                         if (data)
238                         {
239                                 data = 0;
240                                 sfputc(sfstdout, '\n');
241                                 prev = line;
242                         }
243                         break;
244                 case '#':
245                         if (sync >= 0 && !data)
246                         {
247                                 if (s = sfgetr(sp, '\n', 1))
248                                 {
249                                         while (*s == ' ' && *s == '\t')
250                                                 s++;
251                                         if (p = isdigit(*s))
252                                         {
253                                                 sync = 1;
254                                                 line = strtol(s, NiL, 10);
255                                         }
256                                         else if (!sync)
257                                         {
258                                                 if (!line && strneq(s, "pragma prototyped", 17))
259                                                 {
260                                                         sync = -1;
261                                                         s = "proto -ns";
262                                                         if (sp != sfstdin)
263                                                         {
264                                                                 sfsprintf(buf, sizeof(buf), "proto -ns %s", file);
265                                                                 s = buf;
266                                                         }
267                                                         else if (sfseek(sp, 0L, SEEK_SET))
268                                                         {
269                                                                 error(2, "%s: must be seekable to run proto", file);
270                                                                 return;
271                                                         }
272                                                         if (!(sp = sfpopen(sp, s, "r")))
273                                                         {
274                                                                 error(2, "%s: cannot run proto", file);
275                                                                 return;
276                                                         }
277                                                         c = 0;
278                                                         goto next;
279                                                 }
280                                                 if (!strneq(s, "ident", 5) && !strneq(s, "line", 4) && !strneq(s, "pragma", 6))
281                                                         sync = -1;
282                                         }
283                                         if (!p || strchr(s, '"'))
284                                         {
285                                                 if (p) sfputc(sfstdout, c);
286                                                 else DATA(c);
287                                                 sfputr(sfstdout, s, '\n');
288                                                 prev = line;
289                                         }
290                                         c = '\n';
291                                         break;
292                                 }
293                                 sync = -1;
294                         }
295                         directive = line;
296                         DATA(c);
297                         break;
298                 case '/':
299                         switch (c = sfgetc(sp))
300                         {
301                         case EOF:
302                                 return;
303                         case '/':
304                                 for (;;) switch (sfgetc(sp))
305                                 {
306                                 case EOF:
307                                         return;
308                                 case '\n':
309                                         p = ' ';
310                                         c = '\n';
311                                         goto check;
312                                 }
313                                 break;
314                         case '*':
315                                 for (;;) switch (sfgetc(sp))
316                                 {
317                                 case EOF:
318                                         return;
319                                 case '\n':
320                                         line++;
321                                         if (data)
322                                         {
323                                                 data = 0;
324                                                 sfputc(sfstdout, '\n');
325                                                 prev = line;
326                                         }
327                                         break;
328                                 case '*':
329                                         for (;;)
330                                         {
331                                                 switch (sfgetc(sp))
332                                                 {
333                                                 case EOF:
334                                                         return;
335                                                 case '\n':
336                                                         line++;
337                                                         if (data)
338                                                         {
339                                                                 data = 0;
340                                                                 sfputc(sfstdout, '\n');
341                                                                 prev = line;
342                                                         }
343                                                         break;
344                                                 case '/':
345                                                         p = c = ' ';
346                                                         goto check;
347                                                 case '*':
348                                                         continue;
349                                                 }
350                                                 break;
351                                         }
352                                         break;
353                                 }
354                                 break;
355                         default:
356                                 p = '/';
357                                 DATA(p);
358                                 goto check;
359                         }
360                         break;
361                 case '"':
362                 case '\'':
363                         DATA(c);
364                         quote = c;
365                         for (;;)
366                         {
367                                 switch (c = sfgetc(sp))
368                                 {
369                                 case EOF:
370                                         return;
371                                 case '\\':
372                                         sfputc(sfstdout, c);
373                                         switch (c = sfgetc(sp))
374                                         {
375                                         case EOF:
376                                                 return;
377                                         case '\n':
378                                                 prev = ++line;
379                                                 break;
380                                         }
381                                         break;
382                                 case '"':
383                                 case '\'':
384                                         if (c == quote)
385                                         {
386                                                 sfputc(sfstdout, c);
387                                                 goto next;
388                                         }
389                                         break;
390                                 case '\n':
391                                         p = ' ';
392                                         goto check;
393                                 }
394                                 sfputc(sfstdout, c);
395                         }
396                         break;
397                 default:
398                         DATA(c);
399                         break;
400                 }
401         }
402         /*NOTREACHED*/
403 }
404
405 main __PARAM__((int argc, char** argv), (argc, argv)) __OTORP__(int argc; char** argv;){
406         register int            c;
407         register char*          s;
408         register Sfio_t*        sp;
409
410         NoP(argc);
411         error_info.id = "nocom";
412         while (c = optget(argv, " [file ...]")) switch (c)
413         {
414         case '?':
415                 error(ERROR_USAGE|4, opt_info.arg);
416                 break;
417         case ':':
418                 error(2, opt_info.arg);
419                 break;
420         }
421         argv += opt_info.index;
422         if (error_info.errors)
423                 error(ERROR_USAGE|4, optusage(NiL));
424         if (!*argv) nocom(sfstdin, "/dev/stdin");
425         else while (s = *argv++)
426         {
427                 if (!(sp = sfopen(NiL, s, "r")))
428                         error(ERROR_SYSTEM|2, "%s: cannot read", s);
429                 else
430                 {
431                         nocom(sp, s);
432                         sfclose(sp);
433                 }
434         }
435         exit(error_info.errors != 0);
436 }