Back to index

radiance  4R0+20100331
mgfilt.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: mgfilt.c,v 1.8 2003/11/15 17:54:06 schorsch Exp $";
00003 #endif
00004 /*
00005  * Filter MGF stream, removing entities that won't be understood
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include "parser.h"
00011 
00012                             /* Number of entities for major versions */
00013 short  nentlist[MG_VMAJOR] = MG_NELIST;
00014 
00015 
00016 int
00017 put_entity(          /* general output routine */
00018        register int  ac,
00019        register char **av
00020 )
00021 {
00022        while (ac-- > 0) {
00023               fputs(*av++, stdout);
00024               putchar(ac ? ' ' : '\n');
00025        }
00026        return(MG_OK);
00027 }
00028 
00029 
00030 int
00031 main(  /* first argument is understood entities, comma-sep. */
00032        int    argc,
00033        char   *argv[]
00034 )
00035 {
00036        char   *cp1, *cp2;
00037        int    i, en;
00038 
00039        if (argc < 2) {
00040               fprintf(stderr,
00041                      "Usage: %s { version | entity,list } [file ..]\n",
00042                             argv[0]);
00043               exit(1);
00044        }
00045        if (isint(argv[1])) {
00046               i = atoi(argv[1]);
00047               if ((i < 1) | (i > MG_VMAJOR)) {
00048                      fprintf(stderr, "%s: bad version number: %d\n",
00049                                    argv[0], i);
00050                      exit(1);
00051               }
00052               for (en = nentlist[i-1]; en--; )
00053                      mg_ehand[en] = put_entity;
00054               mg_ehand[MG_E_INCLUDE] = NULL;            /* expand include's */
00055        } else
00056               for (cp1 = cp2 = argv[1]; *cp1; cp1 = cp2) {
00057                      while (*cp2) {
00058                             if (*cp2 == ',') {
00059                                    *cp2++ = '\0';
00060                                    break;
00061                             }
00062                             cp2++;
00063                      }
00064                      en = mg_entity(cp1);
00065                      if (en < 0) {
00066                             fprintf(stderr, "%s: %s: no such entity\n",
00067                                           argv[0], cp1);
00068                             exit(1);
00069                      }
00070                      mg_ehand[en] = put_entity;
00071               }
00072        mg_init();
00073        if (argc < 3) {
00074               if (mg_load((char *)NULL) != MG_OK)
00075                      exit(1);
00076               if (mg_nunknown)
00077                      printf("%s %s: %u unknown entities on input\n",
00078                                    mg_ename[MG_E_COMMENT],
00079                                    argv[0], mg_nunknown);
00080               exit(0);
00081        }
00082        for (i = 2; i < argc; i++) {
00083               if (mg_load(argv[i]) != MG_OK)
00084                      exit(1);
00085               if (mg_nunknown) {
00086                      printf("%s %s: %u unknown entities\n",
00087                                    mg_ename[MG_E_COMMENT],
00088                                    argv[i], mg_nunknown);
00089                      mg_nunknown = 0;
00090               }
00091        }
00092        exit(0);
00093 }