File: comment5.c

package info (click to toggle)
clisp 1:2.41-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 49,804 kB
  • ctags: 16,291
  • sloc: lisp: 75,912; ansic: 49,247; xml: 24,289; asm: 21,993; sh: 11,234; fortran: 6,692; cpp: 2,660; objc: 2,481; makefile: 2,355; perl: 164; sed: 55
file content (148 lines) | stat: -rw-r--r-- 5,345 bytes parent folder | download | duplicates (3)
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
/* dient zum Umwandeln von Kommentaren:
 Input:
      text # comment
 Output:
      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 <stdlib.h>

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

int main (int argc, char* argv[]) {
  char infilenamebuffer[1000];
  char outfilenamebuffer[1000] = "stdout";
  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(__LINE__); }
    }
    { /* 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';
    }
  }
  if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL)
    { perror(infilenamebuffer); exit(__LINE__); }
  if ((outfile = fopen(outfilenamebuffer,fopen_write_ascii))==NULL)
    { fclose(infile); perror(outfilenamebuffer); exit(__LINE__); }
  if (0) {
   to_stdout:
    if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL)
      { perror(infilenamebuffer); exit(__LINE__); }
    outfile = stdout; /* outfile = Standard-Output */
  }
  { /* write header in outfile: */
    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=='#')) goto L4a;
         /* innerhalb einer Zeile, nach '#', vor ' ' */
         c = fgetc(infile) ;
         if (!(c==' ')) { fputc('#',outfile); goto L1a; }
         fput_startcomment(outfile);
    L2:  /* innerhalb eines Kommentars */
         /* inside '#' comment */
         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;
    L4a: /* possibly start of '/*' comment */
         /* EOF impossible */
         fputc(c,outfile);
         if (!(c=='/')) {  goto L1; } 
         c = fgetc(infile) ;
         if (c==EOF){ goto L3; }
         fputc(c,outfile);
         if (!(c=='*')) goto L1;
    L5:  /* Inside '/*' comment */
         c = fgetc(infile) ;
    L5a: if (c==EOF){ goto L3; } /* do not fix programmer errors */
         fputc(c,outfile);
         if (c=='*') goto L6;
         goto L5;
    L6:  /* after asterisk in '/*' comment */
         c = fgetc(infile) ;
    L6a: if (c==EOF){ goto L3; }
         fputc(c,outfile);
         if (c=='/') goto L1;
         goto L5;
    L3:  ; /* am File-Ende */
  }
  /* error checking should work after file closing, but it does not */
  if (ferror(infile) || fclose(infile))
    { perror(infilenamebuffer); exit(__LINE__); }
  if (ferror(outfile) || fclose(outfile))
    { perror(outfilenamebuffer); exit(__LINE__); }
  exit(0); /* OK */
}