File: traddecl.d

package info (click to toggle)
clisp 1997-12-06-1
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 20,744 kB
  • ctags: 8,390
  • sloc: ansic: 37,808; lisp: 37,255; asm: 6,393; sh: 3,077; objc: 2,481; makefile: 1,174; sed: 96; perl: 14
file content (553 lines) | stat: -rw-r--r-- 20,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# Programm zum Umwandeln von Funktionsdeklarationen von der ANSI-Syntax in
# den traditionellen Stil bei nicht allzu bel geformten C-Programmen
# Bruno Haible 16.2.1993

# Ziel:
# 1. Tokens ## umwandeln in / * * /
# 2. Zahl-Token-Endung 'UL' oder 'LU' in 'L' umwandeln.
# 3. Token &! streichen.
# 4. Deklarationen
#        storspec ... tspec (arg1,...,argk);
#    umwandeln in
#        storspec ... tspec ();
#    wobei storspec eines der Wrter 'typedef', 'extern', 'local', 'global',
#    'extern_C'.

# Methode:
# Mit Kenntnis der Begriffe "Prprozessor-Kommando", "Kommentar", "Token".
# 1. Zwei aufeinanderfolgende '#' werden durch '/ * * /' ersetzt.
# 2. Aus 'LU' oder 'UL' am Zahl-Token-Ende das 'U' streichen.
# 3. Aufeinanderfolgende '&' und '!' werden gestrichen.
# 4. Es wird nach typedef-Tokens gesucht. Kommt vor dem nchsten Strichpunkt
# ';' des uersten Schachtelungslevels eine Klammer zu ')', so bis zur
# vorigen Klammer auf '(' des uersten Schachtelungslevels alles durch
# Leerstellen ersetzt.


#define MAXARGCOUNT  50  # maximale Anzahl Argumente einer Funktionsdefinition
#define MAXHEADERLEN  5000  # maximale Lnge eines Funktionsdefinitions-Kopfes

#define local static
#define global
#define var
#define loop  while (1)
#define until(exp)  while (!(exp))
#define elif  else if
typedef unsigned char  uintB;
typedef unsigned short  uintW;
typedef unsigned long  uintL;
typedef int  boolean;
#define FALSE 0
#define TRUE 1

#if defined(__TURBOC__) || defined(__GO32__) || defined(__WATCOMC__)
#define STDC_HEADERS 1
#else
#if defined(unix) || defined(__unix) || defined(_AIX) || defined(sinix) || defined(__POSIX__)
#include "unixconf.h"
#endif
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif

#include <stdio.h>

#ifndef NULL
#define NULL ((void*)0)
#endif

local FILE* infile;
local FILE* outfile;

# Input
# =====

local uintL input_line;

local int in_char ()
  { var int c = getc(infile);
    if (c=='\n') { input_line++; }
    return c;
  }

local int peek_char ()
  { var int c = getc(infile);
    if (!(c==EOF)) { ungetc(c,infile); }
    return c;
  }

local uintL last_good_input_line;

# Output
# ======

# Output kann immer ein wenig gepuffert werden:
local struct { enum { direct, buffered } mode; # Output-Modus
               uintB buffer[MAXHEADERLEN]; # Buffer
               uintL buffindex; # Index in den Buffer
             }
      out;

#define char_out(char)  putc(char,outfile)

# Output-Bufferung ausschalten:
local void outbuffer_off ()
  { if (out.mode==buffered)
      { var uintL index = 0;
        while (index < out.buffindex)
          { char_out(out.buffer[index]); index++; }
        out.mode = direct;
  }   }

# Output-Bufferung einschalten:
local void outbuffer_on ()
  { if (out.mode==direct)
      { out.buffindex = 0;
        out.mode = buffered;
  }   }

# Character ausgeben:
local void out_char (c)
  var char c;
  { if (out.mode==buffered)
      { if (out.buffindex < MAXHEADERLEN)
          { out.buffer[out.buffindex++] = c; }
          else
          # Buffer voll -> Buffer abschalten
          { outbuffer_off(); char_out(c); }
      }
      else
      { char_out(c); }
  }

# lexikalische Analyse
# ====================

# Holt das nchste Character:
local int next_char ()
  { var int c = in_char();
    if (!(c==EOF))
      { out_char(c); } # c auch ausgeben
    return c;
  }

# Fr unsere Zwecke brauchen ++ -> != usw. nicht als eigene Token betrachtet
# zu werden, wir kennen also nur:
#   EOF
#   Identifier
#   Zahl-Konstanten
#   Character-Konstanten
#   String-Konstanten
#   Operator/Separator
# Verallgemeinerte Tokens: Expressions (mit balancierten Klammern)
#define MAXIDENTLEN  7
typedef struct { enum { eof, eol, ident, number, charconst, stringconst, sep, expr } type;
                 # falls Bufferung aktiv:
                 uintL startindex; # Startindex im Buffer
                 uintL endindex; # Endindex im Buffer
                 # bei sep (Operator/Separator):
                 uintB ch;
                 # bei ident (Identifier):
                 uintL len; # = min(Lnge,MAXIDENTLEN+1)
                 uintB name[MAXIDENTLEN+1];
               }
        token_;
typedef token_* Token;

# globale Token-Tabelle (Inhalt nur whrend einer Bufferungsperiode gltig):
#define MAXTOKENS  20000
local struct { uintL index;
               token_ data[MAXTOKENS];
             }
      tokens;

# Holt das nchste Token:
# (Innerhalb von Prprozessor-Direktiven zhlt Zeilenende als eigenes Token,
# und '#' leitet keine verschachtelte Prprozessor-Direktive ein.)
local Token nexttoken (within_prep_directive)
  var boolean within_prep_directive;
  { if (tokens.index == MAXTOKENS)
      # kein Platz mehr in der Token-Tabelle -> nicht mehr buffern
      { outbuffer_off(); tokens.index = 0;
        fprintf(stderr,"Token-Tabelle bergelaufen in Zeile %lu.\nFehler irgendwo nach Zeile %lu.\n",input_line,last_good_input_line);
        exit(1);
      }
    # Nun ist tokens.index < MAXTOKENS .
   {var Token token = &tokens.data[tokens.index]; # Platz frs nchste Token
    tokens.index++;
    restart:
    token->startindex = out.buffindex;
#ifndef FOR_ANSI_PREPROCESSOR
    if (peek_char() == '#')
      { in_char(); # erstes '#' berlesen
        if (peek_char() == '#')
          # '##'
          { in_char(); # zweites '#' berlesen
            # stattdessen '/ * * /' durchreichen:
            out_char('/'); out_char('*'); out_char('*'); out_char('/');
            token->type = sep; token->ch = '#'; goto fertig;
          }
          else
          # auch ausgeben, normal weiter
          { out_char('#'); goto prep_directive; }
      }
#endif
    if (peek_char() == '&')
      { in_char(); # '&' berlesen
        if (peek_char() == '!')
          # '&!'
          { in_char(); goto restart; } # '!' berlesen
          else
          { out_char('&'); token->type = sep; token->ch = '&'; goto fertig; }
      }
    { var int c = next_char();
      switch (c)
        { case EOF:
            # EOF
            token->type = eof; goto fertig;
          case ' ': case '\v': case '\t':
            # Whitespace. berlesen
            goto restart;
          case '\n':
            # Zeilenende
            if (within_prep_directive)
              { token->type = eol; goto fertig; } # als Token zurck
              else
              { goto restart; } # berlesen
          case '\\':
            if (peek_char()=='\n')
              # Zeilenende nach '\'. berlesen
              { next_char(); goto restart; }
              else
              goto separator;
          case '/':
            if (peek_char() == '*')
              # Kommentar
              { next_char();
                loop { c = next_char();
                       if (c==EOF) { fprintf(stderr,"Unbeendeter Kommentar\n"); break; }
                       if ((c=='*') && (peek_char()=='/')) { next_char(); break; }
                     }
                goto restart;
              }
              else
              goto separator;
          case '*':
            if (peek_char() == '/')
              # illegales Kommentar-Ende
              { fprintf(stderr,"Kommentar-Ende auerhalb Kommentar in Zeile %lu\n",input_line); }
            goto separator;
          case '#': prep_directive:
            if (within_prep_directive)
              { c = '#'; goto separator; }
              else
              { # Prprozessor-Anweisung.
                # Bis Zeilenende oder EOF lesen.
                loop
                  { var Token subtoken = nexttoken(TRUE);
                    if ((subtoken->type == eof) || (subtoken->type == eol))
                      break;
                  }
                goto restart; # und berlesen
              }
          case '.':
            c = peek_char();
            if (!(((c>='0') && (c<='9')) || (c=='.'))) goto separator;
          case '0': case '1': case '2': case '3': case '4':
          case '5': case '6': case '7': case '8': case '9':
            # Zahl. Weiterlesen, solange alphanumerisches Zeichen oder '.':
            loop
              { c = peek_char();
                if (((c>='0') && (c<='9'))
                    || ((c>='A') && (c<='Z')) || ((c>='a') && (c<='z'))
                    || (c=='.')
                   )
                  { if ((c=='U') || (c=='u')
#ifdef CUT_U_AND_L
                        || (c=='L') || (c=='l')
#endif
                       )
                      in_char(); # 'U' innerhalb einer Zahl streichen
                      else
                      next_char();
                  }
                  else
                  break;
              }
            token->type = number; goto fertig;
          case '\'':
            # Character-Konstante
            loop
              { c = next_char();
                if (c==EOF) { fprintf(stderr,"Unbeendete Character-Konstante\n"); break; }
                if (c=='\'') break;
                if (c=='\\') { c = next_char(); }
              }
            token->type = charconst; goto fertig;
          case '\"':
            # String-Konstante
            loop
              {
#ifndef QUOTE_QUOTES
                c = next_char();
                if (c==EOF) { fprintf(stderr,"Unbeendete String-Konstante\n"); break; }
#else # mu Single-Quotes in Strings quotieren:
                c = in_char();
                if (c==EOF) { fprintf(stderr,"Unbeendete String-Konstante\n"); break; }
                if (c=='\'')
                  { # statt "'" ein "\047" ausgeben:
                    out_char('\\');
                    out_char('0'+((((unsigned char)'\'')/64)%8));
                    out_char('0'+((((unsigned char)'\'')/8)%8));
                    out_char('0'+(((unsigned char)'\'')%8));
                    continue;
                  }
                out_char(c);
#endif
                if (c=='\"') break;
                if (c=='\\') { c = next_char(); }
              }
            token->type = stringconst; goto fertig;
          case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
          case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
          case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
          case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
          case 'Y': case 'Z':
          case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
          case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
          case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
          case 's': case 't': case 'u': case 'v': case 'w': case 'x':
          case 'y': case 'z':
          case '_':
            # Identifier. alles alphanumerische berlesen.
            { var uintB* ptr = &token->name[0];
              var uintL len = 0;
              loop
                { if (len <= MAXIDENTLEN) { *ptr++ = c; len++; }
                  c = peek_char();
                  if (   ((c>='0') && (c<='9'))
                      || ((c>='A') && (c<='Z')) || ((c>='a') && (c<='z'))
                      || (c=='_')
                     )
                    { next_char(); }
                    else
                    break;
                }
              token->type = ident; token->len = len; goto fertig;
            }
          default:
          separator:
            token->type = sep; token->ch = c; goto fertig;
    }   }
    fertig:
    token->endindex = out.buffindex;
    return token;
  }}
#define next_token() nexttoken(FALSE)

# Klammern mitzhlen:
#define MAXBRACES 1000 # maximale Verschachtelungstiefe von Klammern
local struct { uintL count;
               struct { uintB brace_type; uintL input_line; } opening[MAXBRACES];
             }
      open_braces;

# Mitzhlen einer ffnenden Klammer:
local void handle_opening_token (token)
  var Token token;
  { if (open_braces.count < MAXBRACES)
      { open_braces.opening[open_braces.count].brace_type = token->ch;
        open_braces.opening[open_braces.count].input_line = input_line;
      }
    open_braces.count++;
  }

# Mitzhlen einer schlieenden Klammer (ohne berprfung der Verschachtelung):
# local void handle_closing_token (Token token)
#   { open_braces.count--; }
# oder als Macro
#define handle_closing_token(token)  { open_braces.count--; }

# nchste Expression mit balancierten Klammern '()', '{}', '[]' lesen:
# (Dabei ist auf das Niveau open_braces.count=0 zu kommen,
#  evtl. ist jetzt schon open_braces.count>0.)
local Token next_balanced_token (start_token)
  var Token start_token;
  { var uintL open_braces_start = 0; # Ziel-Niveau: 0
    var Token token = (start_token==NULL ? next_token() : start_token);
    var uintL startindex = token->startindex;
    loop
      { # Hier stets  open_braces.count >= open_braces_start .
        switch (token->type)
          { case eof:
              if (open_braces.count > open_braces_start)
                { if (open_braces.count <= MAXBRACES)
                    fprintf(stderr,"Nicht geschlossene '%c' in Zeile %lu\n",
                                   open_braces.opening[open_braces.count-1].brace_type,
                                   open_braces.opening[open_braces.count-1].input_line
                           );
                    else
                    fprintf(stderr,"Nicht geschlossene '(' oder '{' oder '['\n");
                }
              return token; # EOF-Token als Ergebnis
            case sep:
              switch (token->ch)
                { case '(': case '{': case '[':
                    handle_opening_token(token);
                    break;
                  case ')': case '}': case ']':
                    if (open_braces.count > open_braces_start)
                      { open_braces.count--;
                        if (open_braces.count < MAXBRACES)
                          { var uintB opening_ch = open_braces.opening[open_braces.count].brace_type;
                            var uintB closing_ch = token->ch;
                            if (!(   ((opening_ch == '(') && (closing_ch == ')'))
                                  || ((opening_ch == '{') && (closing_ch == '}'))
                                  || ((opening_ch == '[') && (closing_ch == ']'))
                               ) )
                              { fprintf(stderr,"ffnende Klammer '%c' in Zeile %lu\n und schlieende Klammer '%c'\n in Zeile %lu passen nicht zusammen.\n",
                                        opening_ch,open_braces.opening[open_braces.count].input_line,
                                        closing_ch,input_line
                                       );
                          }   }
                      }
                      else
                      { fprintf(stderr,"Nicht geffnete '%c' in Zeile %lu\n",
                                token->ch,input_line
                               );
                        goto fertig;
                      }
                    break;
                  default:
                    break;
                }
            default: ;
              # alles andere ist ausbalanciert
          }
        if (open_braces.count == open_braces_start) break; # fertig ausbalanciert?
        token = next_token(); # nein -> nchstes Token lesen
      }
    fertig:
    token->startindex = startindex;
    return token;
  }

# Umwandlung der Funktionsdefinitions-Deklarationen auf dem ganzen File
# vornehmen:
local void convert ()
  { input_line = 1; last_good_input_line = 1;
    out.mode = direct;
    open_braces.count = 0;
    restart1: # Hier out.mode=direct, neues Token lesen:
    tokens.index = 0; # Token-Tabelle leeren
   {var Token token = next_token(); # nchstes Token lesen
    restart2: # Hier out.mode=direct, neues Token gelesen.
    if ((token->type == ident)
        && (   ((token->len == 7) # 'typedef' ?
                && (token->name[0] == 't')
                && (token->name[1] == 'y')
                && (token->name[2] == 'p')
                && (token->name[3] == 'e')
                && (token->name[4] == 'd')
                && (token->name[5] == 'e')
                && (token->name[6] == 'f')
               )
            || ((token->len == 6) # 'extern' ?
                && (token->name[0] == 'e')
                && (token->name[1] == 'x')
                && (token->name[2] == 't')
                && (token->name[3] == 'e')
                && (token->name[4] == 'r')
                && (token->name[5] == 'n')
               )
            || ((token->len == 5) # 'local' ?
                && (token->name[0] == 'l')
                && (token->name[1] == 'o')
                && (token->name[2] == 'c')
                && (token->name[3] == 'a')
                && (token->name[4] == 'l')
               )
            || ((token->len == 6) # 'global' ?
                && (token->name[0] == 'g')
                && (token->name[1] == 'l')
                && (token->name[2] == 'o')
                && (token->name[3] == 'b')
                && (token->name[4] == 'a')
                && (token->name[5] == 'l')
               )
            || ((token->len == 8) # 'extern_C' ?
                && (token->name[0] == 'e')
                && (token->name[1] == 'x')
                && (token->name[2] == 't')
                && (token->name[3] == 'e')
                && (token->name[4] == 'r')
                && (token->name[5] == 'n')
                && (token->name[6] == '_')
                && (token->name[7] == 'C')
               )
       )   )
      # ja -> aufpassen:
      { outbuffer_on(); # Buffer einschalten
        # Ein Attribut nach dem anderen lesen, bis zum Strichpunkt.
        # Dabei speichern, ob das letzte von Klammern begrenzt war.
        { var boolean last_parenthesized; # ob letztes Attribut geklammert war
          var uintL last_startindex; # Falls ja: Index nach der Klammer auf
          var uintL last_endindex; # Falls ja: Index vor der Klammer zu
          last_parenthesized = FALSE;
          token = next_token();
          loop
            { if (token->type == eof) goto nix; # EOF?
              elif ((token->type == sep) && (token->ch == '(')) # Klammer auf?
                { last_parenthesized = TRUE;
                  last_startindex = out.buffindex;
                  token = next_balanced_token(token); # Attribut berlesen
                  if ((out.mode==buffered) && (out.buffer[out.buffindex-1] == ')')) # sollte mit ')' enden
                    { last_endindex = out.buffindex-1; }
                    else
                    { last_parenthesized = FALSE; } # sonst war's wohl nix
                  # Wenn's jetzt mit '{' weitergeht, ist das wohl eine
                  # Funktionsdefinition und keine Funktionsdeklaration:
                  token = next_token();
                  if ((token->type == sep) && (token->ch == '{')) goto nix;
                }
              elif ((token->type == sep) && (token->ch == ';')) # Strichpunkt?
                break;
              elif ((token->type == sep) && (token->ch == '='))
                # '=' bedeutet initialisierte Variable, keine Funktionsdeklaration
                goto nix;
              else
                { token = next_balanced_token(token); # Attribut berlesen
                  last_parenthesized = FALSE;
                  token = next_token();
                }
            }
          # Am Strichpunkt angelangt
          if (last_parenthesized)
            # Teilstck des Buffers durch Leerstellen berschreiben:
            { var uintL index = last_startindex;
              until (index==last_endindex) { out.buffer[index++] = ' '; }
              last_good_input_line = input_line;
            }
        }
        outbuffer_off(); # Buffer wieder ausschalten
      }
    else
      { nix: # keine umwandelbare Funktionsdeklaration
        outbuffer_off();
        token = next_balanced_token(token);
      }
    end:
    # Hier ist wieder out.mode=direct.
    if (!(token->type==eof)) goto restart1;
    # Bei EOF am Input: fertig (da outfile ungebuffert).
  }}

int main ()
  { infile = stdin;
    outfile = stdout;
    convert();
    if (ferror(stdin) || ferror(stdout)) { exit(1); }
    exit(0);
  }