Annotation of researchv9/jtools/src/cmd/cleave.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sys/types.h>
        !             3: #include <sys/stat.h>
        !             4: FILE *popen();
        !             5: 
        !             6: int Argc; char **Argv;
        !             7: char Doth[128],Dotu[128],Dotf[128],Dotut[128],Dotft[128],Dotui[128],Dotfi[128];
        !             8: 
        !             9: char Cfront[128] = "cfront";
        !            10: char Cpp[128]   = "/lib/cpp";
        !            11: char *Base = 0;
        !            12: 
        !            13: main( argc, argv )
        !            14: char **argv;
        !            15: {
        !            16:        int i;
        !            17: 
        !            18:        setbuf( stderr, malloc(BUFSIZ) );
        !            19:        Argc = argc; Argv= argv;
        !            20:        if( argc<2 ){
        !            21: Argerr:
        !            22:                fprintf( stderr, "use: cleave [-I] [+VE] base\n");
        !            23:                exit(1);
        !            24:        }
        !            25:        for( i = 1; argv[i]; ++i ) switch( argv[i][0] ){
        !            26:                case '+':
        !            27:                        strcat( Cfront, " " );
        !            28:                        strcat( Cfront, argv[i] );
        !            29:                        break;
        !            30:                case '-':
        !            31:                        strcat( Cpp, " " );
        !            32:                        strcat( Cpp, argv[i] );
        !            33:                        break;
        !            34:                default:
        !            35:                        if( Base ) goto Argerr;
        !            36:                        Base = argv[i];
        !            37:        }
        !            38:        if( !Base ) goto Argerr;
        !            39:        sprintf( Doth,  "%s.h",   Base );
        !            40:        sprintf( Dotu,  "%s.pub",   Base );
        !            41:        sprintf( Dotf,  "%s.pri",   Base );
        !            42:        sprintf( Dotut, "%s.pub.t", Base );
        !            43:        sprintf( Dotft, "%s.pri.t", Base );
        !            44:        sprintf( Dotui, "%s.pub.i", Base );
        !            45:        sprintf( Dotfi, "%s.pri.i", Base );
        !            46:        UpToDate();
        !            47:        Split(0);
        !            48:        Compare();
        !            49:        Split(0);
        !            50:        Install(Dotut,Dotu);
        !            51:        Install(Dotft,Dotf);
        !            52:        exit(0);
        !            53: }
        !            54: 
        !            55: char *GetLine(s, n, f)
        !            56: char *s;
        !            57: int n;
        !            58: FILE *f;
        !            59: {
        !            60:        char *e, l[256];
        !            61:        int d;
        !            62: 
        !            63:        do e = fgets(s, n, f);
        !            64:        while( e && sscanf(s, "# %s %d", l, &d)==2 && !strcmp(l, "line") );
        !            65:        return e;
        !            66: }
        !            67: 
        !            68: Same(a,b)
        !            69: char *a, *b;
        !            70: {
        !            71:        struct stat unused;
        !            72:        char al[1024], bl[1024], same = 1;
        !            73:        FILE *ap, *bp;
        !            74: 
        !            75:        if( !(bp = fopen(b,"r")) ) return 0;
        !            76:        if( !(ap = fopen(a,"r")) ) return 0;
        !            77:        while( same && GetLine(al, 1024, ap) )
        !            78:                same = GetLine(bl, 1024, bp) && !strcmp(al,bl);
        !            79:        same = same && !GetLine(bl, 1024, bp);
        !            80:        fclose(ap); fclose(bp);
        !            81:        return same;
        !            82: }
        !            83: 
        !            84: Install(from,to)
        !            85: char *from, *to;
        !            86: {
        !            87:        if( Same(from,to) ){
        !            88:                unlink(from);
        !            89:                fprintf( stderr, "%s unchanged\n", to );
        !            90:                return;
        !            91:        }
        !            92:        unlink(to);
        !            93:        if( link(from,to) )
        !            94:                Exit(1, "cannot link %s %s", from, to );
        !            95:        if( unlink(from) )
        !            96:                Exit(1, "cannot unlink %s", from );
        !            97:        fprintf( stderr, "%s modified\n", to ); return;
        !            98: }
        !            99: 
        !           100: Compile( from, to )
        !           101: char *from, *to;
        !           102: {
        !           103:        char cpp[256], cfront[256], *cpped = "huf.tmp";
        !           104: 
        !           105:        sprintf( cpp, "%s <%s >%s", Cpp, from, cpped );
        !           106:        if( system( cpp ) ) Exit( 1, "cpp failed: %s", from );
        !           107:        sprintf( cfront, "%s +f%s <%s >%s", Cfront, from, cpped, to );
        !           108:        if( system( cfront ) ) Exit( 1, "cfront failed: %s", from );
        !           109:        unlink( cpped );
        !           110: }
        !           111: 
        !           112: struct Context {
        !           113:        FILE    *fp;
        !           114:        char    line[1024];
        !           115:        int     n;
        !           116:        char    path[256];
        !           117: };
        !           118: 
        !           119: int LineNo(c)
        !           120: struct Context *c;
        !           121: {
        !           122:        if( c->line[0] == '#' ){
        !           123:                if( 2 != sscanf(c->line, "# %d %[^\n]", &c->n, c->path) )
        !           124:                        Exit( 1, ".i # syntax error: %s", c->line );
        !           125:                --c->n;
        !           126:        } else ++c->n;
        !           127:        return c->line[0] == '#';
        !           128: }
        !           129: 
        !           130: ShowContext(c)
        !           131: struct Context *c;
        !           132: {
        !           133:        fprintf( stderr, "%s ~%d >C++> %s", c->path, c->n, c->line );
        !           134: }
        !           135: 
        !           136: struct Context ZContext;
        !           137: 
        !           138: Compare()
        !           139: {
        !           140:        char sed[256], i;
        !           141:        struct Context u, f, save;
        !           142: 
        !           143:        u = f = save = ZContext;
        !           144:        Compile( Dotut, Dotui );
        !           145:        Compile( Dotft, Dotfi );
        !           146:        sprintf( sed, "sed /pub_filler/d <%s", Dotui );
        !           147:        u = f = save = ZContext;
        !           148:        if( !(u.fp = popen(sed, "r")) )
        !           149:                Exit( 1, "cannot popen %s", sed );
        !           150:        if( !(f.fp = fopen(Dotfi, "r")) )
        !           151:                Exit( 1, "cannot open %s", Dotfi );
        !           152:        strcpy( u.path, Dotut );
        !           153:        strcpy( f.path, Dotft );
        !           154:        for( i = 1; i <= 5; ++i )
        !           155:                fgets( u.line, sizeof u.line, u.fp );
        !           156:        while( fgets( u.line, sizeof u.line, u.fp ) ){
        !           157:                if( LineNo(&u) ) continue;
        !           158:                while( fgets( f.line, sizeof f.line, f.fp ) ){
        !           159:                        if( LineNo(&f) ) continue;
        !           160:                        save = f;
        !           161:                        break;
        !           162:                }
        !           163: /*Bogusfgets:  if( strlen(u.line) > 510 ) continue;    */
        !           164:                while( strcmp( u.line, f.line ) ){
        !           165:                        if( !fgets( f.line, sizeof f.line, f.fp ) ){
        !           166:                                fprintf( stderr, "%s vs %s:\n", Dotui, Dotfi );
        !           167:                                ShowContext( &u );
        !           168:                                ShowContext( &save );
        !           169:                                exit(1);
        !           170:                        }
        !           171:                        LineNo(&f);
        !           172:                }
        !           173:        }
        !           174:        pclose(u.fp); fclose(f.fp); unlink(Dotui); unlink(Dotfi);
        !           175: }
        !           176: 
        !           177: Split(v)
        !           178: {
        !           179:        FILE *h, *ut, *ft;
        !           180:        char text[256], u = 1, f = 1, cmd[256], direct[256];
        !           181:        int n = 0, sync = 0;
        !           182: 
        !           183:        if( !(h = fopen(Doth, "r")) )
        !           184:                Exit( 1, "cannot open %s", Doth );
        !           185:        if( !(ut = fopen(Dotut, "w")) )
        !           186:                Exit( 1, "cannot open %s", Dotut );
        !           187:        if( !(ft = fopen(Dotft, "w")) )
        !           188:                Exit( 1, "cannot open %s", Dotft );
        !           189:        while( ++n, fgets( text, 256, h ) ){
        !           190:                if( text[0] == '>' ){
        !           191:                        strcpy( direct, "" );
        !           192:                        sscanf( text, ">%s", direct );
        !           193:                        if(!strcmp(direct,"pub") || !strcmp(direct,"u")){
        !           194:                                u = 1;
        !           195:                                f = 0;
        !           196:                        } else if(!strcmp(direct,"pri") || !strcmp(direct,"f") ){
        !           197:                                u = 0;
        !           198:                                f = 1;
        !           199:                        } else {
        !           200:                                if(v) fprintf(ft, "static %s_%d;\n", Base, ++sync );
        !           201:                                if(v) fprintf(ut, "static %s_%d;\n", Base, sync );
        !           202:                                u = 1; f = 1;
        !           203:                        }
        !           204:                        if(f&&v) fprintf(ft, "# line %d \"%s(.pri)\"\n", n+1, Doth);
        !           205:                        if(u&&v) fprintf(ut, "# line %d \"%s(.pub)\"\n", n+1, Doth);
        !           206:                        continue;
        !           207:                }
        !           208:                if( f ) fprintf( ft, "%s", text );
        !           209:                if( u ) fprintf( ut, "%s", text );
        !           210:        }
        !           211:        fclose(h); fclose(ut); fclose(ft);
        !           212:        sprintf( cmd, "cmp -s %s %s", Dotut, Dotft );
        !           213:        if( v && !system(cmd) ){
        !           214:                fprintf( stderr, "warning: identical .pri and .pub\n" );
        !           215:                fflush(stderr);
        !           216:        }
        !           217: }
        !           218: 
        !           219: UpToDate()
        !           220: {
        !           221:        struct stat h, f;
        !           222: 
        !           223:        if( stat(Doth, &h) )
        !           224:                Exit( 1, "cannot stat %s", Doth );
        !           225:        if( stat(Dotf, &f) )
        !           226:                return;
        !           227:        if( h.st_mtime<f.st_mtime ){
        !           228:                fprintf( stderr, "%s is up to date\n", Dotf );
        !           229:                exit(0);
        !           230:        }
        !           231: }
        !           232: 
        !           233: Exit( code, f, a1, a2, a3, a4, a5, a6 )
        !           234: char *f;
        !           235: {
        !           236:        char msg[256];
        !           237: 
        !           238:        sprintf( msg, f, a1, a2, a3, a4, a5, a6 );
        !           239:        perror( msg );
        !           240:        exit(code);
        !           241: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.