home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / CLISP-1.LHA / CLISP960530-sr.lha / utils / comment5.d < prev    next >
Encoding:
Text File  |  1996-04-15  |  4.9 KB  |  139 lines

  1. # COMMENT.D
  2. # dient zum Umwandeln von Kommentaren:
  3. # Inputzeile:
  4. #      text # comment
  5. # Outputzeile:
  6. #      text / * comment * /
  7. # Bruno Haible 9.7.1990, 6.8.1990, 15.8.1990, 22.9.1990, 17.1.1991, 31.8.1991
  8.  
  9. # Aufrufmöglichkeiten, um program.d in program.c umzuwandeln:
  10. #   comment program
  11. #   comment           und dann von Hand 'program' eingeben
  12. #   comment program.d program.c
  13. #   comment program.d > program.c
  14.  
  15. # Methode:
  16. # Das Inputfile wird Zeile für Zeile umgewandelt. Ein '#', gefolgt von ' ',
  17. # leitet einen Kommentar ein, der bis Zeilenende geht.
  18. # '#'' ' wird durch '/''*'' ' ersetzt, und vors Zeilenende kommt ' ''*''/'.
  19. # Ein '\' unmittelbar am Ende einer Zeile wird dabei zum Zeilenende gerechnet.
  20. # Als erste Zeile wird  '#line 1 "sourcefile"' eingefügt.
  21.  
  22. # Syntax eines Inputfile:                                               #
  23. # -------> Char ----> '#' -> ' ' ----> Char ----> '\\' ---> '\n' -----> #
  24. #    / /         \                 /         \ \       /           \    #
  25. #   |  \         /                 \         /  --->---             |   #
  26. #    \  --<------                   --<------                      /    #
  27. #     -------------------<-----------------------------------------     #
  28.  
  29. #include <stdio.h>
  30. #if 1 # defined(unix) || defined(__unix) || defined(__EMX__) || defined(__WATCOMC__)
  31.   #define fopen_read_ascii  "r"
  32.   #define fopen_write_ascii  "w"
  33.   #define fputc  putc
  34.   #define fgetc  getc
  35. #endif
  36.  
  37. typedef unsigned char  Char;
  38.  
  39. #ifdef __STDC__
  40. int main (int argc, char** argv)
  41. #else
  42. int main(argc,argv)
  43.  int    argc;
  44.  char** argv;
  45. #endif
  46. { char infilenamebuffer[1000];
  47.   char outfilenamebuffer[1000];
  48.   FILE * infile;
  49.   FILE * outfile;
  50.   if (argc==3)
  51.     { # Aufruf der Form 'comment source destination'
  52.       { char* p1 = argv[1]; char* p2 = infilenamebuffer; while (*p2++ = *p1++); }
  53.       { char* p1 = argv[2]; char* p2 = outfilenamebuffer; while (*p2++ = *p1++); }
  54.     }
  55.   else
  56.     { char filenamebuffer[1000];
  57.       char* filename;
  58.       if (argc==2)
  59.         { filename=argv[1]; }
  60.         else
  61.         { printf("Filename: "); filename=gets(filenamebuffer); }
  62.       # infilename basteln: filename+".d"
  63.       { char* p = infilenamebuffer;
  64.         { char* p2 = filename; char c; while (c = *p2++) { *p++ = c; } }
  65.         # Endet filename bereits mit ".d" ?
  66.         if ((&p[-2] >= infilenamebuffer) && (p[-2]=='.') && (p[-1]=='d'))
  67.           { *p++ = '\0'; goto to_stdout; } # ja -> Output nach stdout
  68.         *p++ = '.'; *p++ = 'd';
  69.         *p++ = '\0';
  70.       }
  71.       # outfilename basteln: filename+".c"
  72.       { char* p = outfilenamebuffer;
  73.         { char* p2 = filename; char c; while (c = *p2++) { *p++ = c; } }
  74.         *p++ = '.'; *p++ = 'c';
  75.         *p++ = '\0';
  76.       }
  77.     }
  78.   # infile öffnen:
  79.   if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
  80.   # outfile öffnen:
  81.   if ((outfile = fopen(outfilenamebuffer,fopen_write_ascii))==NULL)
  82.     { fclose(infile); exit(1); }
  83.   if (0)
  84.     { to_stdout:
  85.       # infile öffnen:
  86.       if ((infile = fopen(infilenamebuffer,fopen_read_ascii))==NULL) { exit(1); }
  87.       outfile = stdout; # outfile = Standard-Output
  88.     }
  89.   #ifdef ATARI
  90.   # infile und outfile einen größeren Buffer zuweisen:
  91.   { long free = Malloc(-1); # Anzahl der freien Bytes
  92.     long free2 = free/2; # halbe Anzahl
  93.     long freep1 = Malloc(free2); # 1. Pointer auf freien Speicher
  94.     long freep2 = Malloc(free2); # 2. Pointer auf freien Speicher
  95.     if ((freep1<0) || (freep2<0)) { exit(1); } # evtl. Fehler
  96.     setvbuf(infile,(char*)freep1,_IOFBF,free2);
  97.     setvbuf(outfile,(char*)freep2,_IOFBF,free2);
  98.   }
  99.   #endif
  100.   # Header in outfile schreiben:
  101.   { fputs("#line 1 \"",outfile);
  102.     fputs(infilenamebuffer,outfile);
  103.     fputs("\"\n",outfile);
  104.   }
  105.   # infile in outfile kopieren:
  106.   #define fput_startcomment(outfile)  \
  107.     { fputc('/',outfile); fputc('*',outfile); fputc(' ',outfile); }
  108.   #define fput_endcomment(outfile)  \
  109.     { fputc(' ',outfile); fputc('*',outfile); fputc('/',outfile); }
  110.   { register int c;
  111.     L1:  # innerhalb einer Zeile, vor Kommentar
  112.          c = fgetc(infile) ;
  113.     L1a: if (c==EOF){ goto L3; }
  114.          if (!(c=='#')) { fputc(c,outfile); goto L1; }
  115.          # innerhalb einer Zeile, nach '#', vor ' '
  116.          c = fgetc(infile) ;
  117.          if (!(c==' ')) { fputc('#',outfile); goto L1a; }
  118.          fput_startcomment(outfile);
  119.     L2:  # innerhalb eines Kommentars
  120.          c = fgetc(infile) ;
  121.     L2a: if (c==EOF) { fput_endcomment(outfile); goto L3; }
  122.          if (c=='\n') { fput_endcomment(outfile); fputc(c,outfile); goto L1; }
  123.          if (!(c=='\\')) { fputc(c,outfile); goto L2; }
  124.          # innerhalb eines Kommentars, nach '\\'
  125.          c = fgetc(infile) ;
  126.          if (!(c=='\n')) { fputc('\\',outfile); goto L2a; }
  127.          fput_endcomment(outfile); fputc('\\',outfile); fputc(c,outfile);
  128.          goto L1;
  129.     L3:  ; # am File-Ende
  130.   }
  131.   # Files schließen:
  132.   if (ferror(infile) || ferror(outfile))
  133.     { fclose(infile); fclose(outfile); exit(1); }
  134.   fclose(infile);
  135.   fclose(outfile);
  136.   exit(0); # alles OK
  137. }
  138.  
  139.