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