[BACK]Return to extract_func.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-doc

Annotation of OpenXM/src/asir-doc/extract_func.c, Revision 1.5

1.5     ! ohara       1: /* $OpenXM: OpenXM/src/asir-doc/extract_func.c,v 1.4 2006/02/12 08:55:26 noro Exp $ */
1.1       noro        2:
                      3: #include <stdio.h>
                      4: #include <string.h>
1.5     ! ohara       5: #include <stdlib.h>
        !             6: #include <unistd.h>
        !             7: #include <ctype.h>
        !             8: #include <sys/stat.h>
1.1       noro        9:
1.5     ! ohara      10: void create_dir(char *);
        !            11: int fname(char *,char **);
        !            12:
        !            13: int
1.1       noro       14: main(argc,argv)
                     15: int argc;
                     16: char **argv;
                     17: {
                     18:        int c;
                     19:        FILE *in,*fp;
                     20:        char buf[BUFSIZ*100];
                     21:        char buf1[BUFSIZ*100];
                     22:        char *name[BUFSIZ];
                     23:        char cmd[BUFSIZ];
                     24:        char fn[BUFSIZ];
1.2       noro       25:        char *file;
                     26:        int jis;
1.1       noro       27:        int i,j;
                     28:
1.2       noro       29:        if ( (argc != 2) && (argc != 3) ) {
                     30:                fprintf(stderr,"usage : extract_func [-j] infofile\n");
1.1       noro       31:                exit(0);
                     32:        }
1.2       noro       33:        if ( argc == 3 ) {
                     34:                jis = 1;
                     35:                file = argv[2];
                     36:        } else {
                     37:                jis = 0;
                     38:                file = argv[1];
                     39:        }
                     40:        in = fopen(file,"rb");
1.1       noro       41:        if ( !in ) {
1.2       noro       42:                fprintf(stderr,"%s : not found",file);
1.1       noro       43:                exit(0);
                     44:        }
                     45:        fp = 0;
                     46:        while ( 1 ) {
                     47:                c = getc(in);
                     48:                if ( c == EOF )
                     49:                        exit(0);
                     50:                else if ( !fp && c == '\n' )
                     51:                        continue;
                     52:                ungetc(c,in);
                     53:                fgets(buf,BUFSIZ,in);
                     54:                if ( fname(buf,name) ) {
                     55:                        fgets(buf1,BUFSIZ,in);
                     56:                        for ( i = 0; buf1[i] && buf1[i] == '-'; i++ );
                     57:                        if ( i >= 3 && buf1[i] == '\n' ) {
                     58:                                fputs(buf,stderr);
                     59:                                strcpy(fn,name[0]);
1.3       noro       60:                                create_dir(fn);
1.1       noro       61:                                fp = fopen(fn,"w");
                     62:                                for ( j = 1; name[j]; j++ )
                     63:                                        symlink(fn,name[j]);
                     64:                                fputs(buf,fp);
                     65:                                fputs(buf1,fp);
                     66:                        } else if ( fp ) {
                     67:                                fputs(buf,fp);
                     68:                                if ( buf1[0] == 0x1f ) {
                     69:                                        fclose(fp); fp = 0;
1.2       noro       70:                                        if ( jis ) {
                     71:                                                sprintf(cmd,"nkf %s > %s.tmp; rm -f %s; mv %s.tmp %s",
                     72:                                                        name[0],name[0],name[0]);
                     73:                                                system(cmd);
                     74:                                        }
1.1       noro       75:                                } else
                     76:                                        fputs(buf1,fp);
                     77:                        }
1.5     ! ohara      78:                } else if ( fp ) {
1.1       noro       79:                        if ( buf[0] == 0x1f ) {
                     80:                                fclose(fp); fp = 0;
1.2       noro       81:                                if ( jis ) {
                     82:                                        sprintf(cmd,"nkf %s > %s.tmp; rm -f %s; mv %s.tmp %s",
                     83:                                                fn,fn,fn,fn,fn);
                     84:                                        system(cmd);
                     85:                                }
1.5     ! ohara      86:                        } else {
1.1       noro       87:                                fputs(buf,fp);
1.5     ! ohara      88:                        }
        !            89:                }
1.1       noro       90:        }
1.3       noro       91: }
                     92:
1.5     ! ohara      93: void create_dir(char *fname)
1.3       noro       94: {
                     95:        char *p;
                     96:
                     97:        p = fname;
                     98:        while ( *p && (p = strchr(p,'/')) ) {
                     99:                *p = 0;
                    100:                mkdir(fname,0755);
                    101:                *p = '/';
                    102:                p++;
                    103:        }
                    104:
1.1       noro      105: }
                    106:
                    107: int fname(buf,name)
                    108: char *buf;
                    109: char **name;
                    110: {
                    111:        int i,len;
1.4       noro      112:        char *quote,*bquote,*comma,*space,*p;
1.1       noro      113:
1.4       noro      114:        if ( *buf != '`' ) {
                    115:                /* skip X.X.X if exists */
                    116:                space = index(buf,' ');
                    117:                if ( !space ) return 0;
                    118:                for ( p = buf; p < space; p++ )
                    119:                        if ( !isdigit(*p) && *p != '.' ) return 0;
                    120:                buf = space+1;
                    121:        }
1.1       noro      122:        i = 0;
                    123:        while ( 1 ) {
                    124:                /* search a back quote */
                    125:                bquote = index(buf,'`' );
                    126:                if ( !bquote )
                    127:                        return 0;
                    128:                buf = bquote+1;
                    129:
                    130:                /* buf points to a function; search a quote */
                    131:                quote = index(buf,'\'');
                    132:                if ( !quote )
                    133:                        return 0;
                    134:                len = quote-buf;
                    135:                name[i] = (char *)malloc(len+1);
                    136:                strncpy(name[i],buf,len);
                    137:                name[i][len] = 0;
                    138:                i++;
                    139:                buf = quote+1;
                    140:                /* buf points to ',' or a space char; search a comma */
                    141:                comma = index(buf,',');
                    142:                if ( !comma ) {
                    143:                        /* if the rest chars include a non-space char, then return 0 */
                    144:                        while ( *buf && isspace(*buf) ) buf++;
                    145:                        if ( *buf )
                    146:                                return 0;
                    147:                        else {
                    148:                                name[i] = 0;
                    149:                                return 1;
                    150:                        }
                    151:                }
                    152:        }
                    153: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>