File: comment5.d

package info (click to toggle)
clisp 1:2.27-0.5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 49,860 kB
  • ctags: 20,752
  • sloc: ansic: 123,781; lisp: 67,533; asm: 19,633; xml: 11,766; sh: 9,788; fortran: 8,307; makefile: 3,570; objc: 2,481; perl: 1,744; java: 341; yacc: 318; sed: 117
file content (132 lines) | stat: -rw-r--r-- 4,642 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
# COMMENT.D
# dient zum Umwandeln von Kommentaren:
# Inputzeile:
#      text # comment
# Outputzeile:
#      text / * comment * /
# Bruno Haible 9.7.1990, 6.8.1990, 15.8.1990, 22.9.1990, 17.1.1991, 31.8.1991

# Aufrufmöglichkeiten, um program.d in program.c umzuwandeln:
#   comment program
#   comment           und dann von Hand 'program' eingeben
#   comment program.d program.c
#   comment program.d > program.c

# Methode:
# Das Inputfile wird Zeile für Zeile umgewandelt. Ein '#', gefolgt von ' ',
# leitet einen Kommentar ein, der bis Zeilenende geht.
# '#'' ' wird durch '/''*'' ' ersetzt, und vors Zeilenende kommt ' ''*''/'.
# Ein '\' unmittelbar am Ende einer Zeile wird dabei zum Zeilenende gerechnet.
# Als erste Zeile wird  '#line 1 "sourcefile"' eingefügt.

# Syntax eines Inputfile:                                               #
# -------> Char ----> '#' -> ' ' ----> Char ----> '\\' ---> '\n' -----> #
#    / /         \                 /         \ \       /           \    #
#   |  \         /                 \         /  --->---             |   #
#    \  --<------                   --<------                      /    #
#     -------------------<-----------------------------------------     #

#include <stdio.h>
#define fopen_read_ascii  "r"
#define fopen_write_ascii  "w"
#define fputc  putc
#define fgetc  getc

#ifdef __cplusplus
extern "C" void exit(int);
#endif

int main (int argc, char* argv[])
{ char infilenamebuffer[1000];
  char outfilenamebuffer[1000];
  FILE * infile;
  FILE * outfile;
  if (argc==3)
    { # Aufruf der Form 'comment source destination'
      { char* p1 = argv[1]; char* p2 = infilenamebuffer;
        while ((*p2++ = *p1++) != '\0');
      }
      { char* p1 = argv[2]; char* p2 = outfilenamebuffer;
        while ((*p2++ = *p1++) != '\0');
      }
    }
  else
    { char filenamebuffer[1000];
      char* filename;
      if (argc==2)
        { filename = argv[1]; }
        else
        { printf("Filename: ");
          filename = fgets(filenamebuffer,sizeof(filenamebuffer),stdin);
          if (filename==NULL) { exit(1); }
        }
      # infilename basteln: filename+".d"
      { char* p = infilenamebuffer;
        { char* p2 = filename;
          char c; while ((c = *p2++) != '\0') { *p++ = c; }
        }
        # Endet filename bereits mit ".d" ?
        if ((&p[-2] >= infilenamebuffer) && (p[-2]=='.') && (p[-1]=='d'))
          { *p++ = '\0'; goto to_stdout; } # ja -> Output nach stdout
        *p++ = '.'; *p++ = 'd';
        *p++ = '\0';
      }
      # outfilename basteln: filename+".c"
      { char* p = outfilenamebuffer;
        { char* p2 = filename;
          char c; while ((c = *p2++) != '\0') { *p++ = c; }
        }
        *p++ = '.'; *p++ = 'c';
        *p++ = '\0';
      }
    }
  # infile öffnen:
  if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
  # outfile öffnen:
  if ((outfile = fopen(outfilenamebuffer,fopen_write_ascii))==NULL)
    { fclose(infile); exit(1); }
  if (0)
    { to_stdout:
      # infile öffnen:
      if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
      outfile = stdout; # outfile = Standard-Output
    }
  # Header in outfile schreiben:
  { fputs("#line 1 \"",outfile);
    fputs(infilenamebuffer,outfile);
    fputs("\"\n",outfile);
  }
  # infile in outfile kopieren:
  #define fput_startcomment(outfile)  \
    { fputc('/',outfile); fputc('*',outfile); fputc(' ',outfile); }
  #define fput_endcomment(outfile)  \
    { fputc(' ',outfile); fputc('*',outfile); fputc('/',outfile); }
  { register int c;
    L1:  # innerhalb einer Zeile, vor Kommentar
         c = fgetc(infile) ;
    L1a: if (c==EOF){ goto L3; }
         if (!(c=='#')) { fputc(c,outfile); goto L1; }
         # innerhalb einer Zeile, nach '#', vor ' '
         c = fgetc(infile) ;
         if (!(c==' ')) { fputc('#',outfile); goto L1a; }
         fput_startcomment(outfile);
    L2:  # innerhalb eines Kommentars
         c = fgetc(infile) ;
    L2a: if (c==EOF) { fput_endcomment(outfile); goto L3; }
         if (c=='\n') { fput_endcomment(outfile); fputc(c,outfile); goto L1; }
         if (!(c=='\\')) { fputc(c,outfile); goto L2; }
         # innerhalb eines Kommentars, nach '\\'
         c = fgetc(infile) ;
         if (!(c=='\n')) { fputc('\\',outfile); goto L2a; }
         fput_endcomment(outfile); fputc('\\',outfile); fputc(c,outfile);
         goto L1;
    L3:  ; # am File-Ende
  }
  # Files schließen:
  if (ferror(infile) || fflush(outfile) || ferror(outfile))
    { fclose(infile); fclose(outfile); exit(1); }
  fclose(infile);
  fclose(outfile);
  exit(0); # alles OK
}