home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / CLISP-1.LHA / CLISP960530-sr.lha / utils / comment5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  5.1 KB  |  140 lines

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