[BACK]Return to oxgreph.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / util

Annotation of OpenXM/src/util/oxgreph.c, Revision 1.4

1.4     ! ohara       1: /* $OpenXM: OpenXM/src/util/oxgreph.c,v 1.3 2006/03/05 04:01:17 takayama Exp $ */
1.1       takayama    2: #include <stdio.h>
                      3: #include <stdlib.h>
                      4: #include <string.h>
                      5: #include <sys/types.h>
                      6: #include <unistd.h>
                      7:
                      8: #define mymalloc(n)  malloc(n)
                      9:
                     10: /*
                     11:    It greps files and outputs an html file to browse the files.
                     12:    Keyword should be given in euc or utf-8 depending on the coding system of the files.
                     13:    It does not add meta section  of html file.
1.3       takayama   14:    Locale must be "C" to use this program or use -f option to read keyword.
1.2       takayama   15:
                     16:    Example:
1.3       takayama   17:    export LC_ALL="C"
1.2       takayama   18:    s.txt contains "insubunkai"
1.4     ! ohara      19:    oxgreph  "`nkf -w s.txt`" $OpenXM_HOME/doc/cfep/html-ja_JP.utf8/html-ja/*.html >t.html
        !            20:    oxgreph --sjis "`nkf -s s.txt`" $OpenXM_HOME/doc/cfep/html-ja_JP.sjis/html-ja/*.html >t.html
1.1       takayama   21: */
1.2       takayama   22:
                     23: int Sjis = 0;
                     24: int LeaveTag = 0;
1.1       takayama   25: main(int argc, char *argv[]) {
                     26:   int i,m,pid,n, cpos,start;
                     27:   char *com;
                     28:   char workf[256];
                     29: #define SSIZE 1024
                     30:   char s[SSIZE];
                     31:   char ss[SSIZE*2];
                     32:   FILE *fp;
                     33:   m = 256;
                     34:   pid = getpid();
1.3       takayama   35:   if (argc < 2) {
                     36:        fprintf(stderr,"oxgreph [--leaveTag] args_to_grep \n");
                     37:        fprintf(stderr,"Note: LC_ALL should be C on some systems or use -f to give the pattern.\n");
                     38:        fprintf(stderr,"Example 1:  oxgreph \"`nkf -w s.txt`\" $OpenXM_HOME/doc/cfep/html-ja_JP.utf8/*.html >t.html\n");
                     39:        fprintf(stderr,"Example 2:  oxgreph -f s.txt $OpenXM_HOME/doc/cfep/html-ja_JP.utf8/*.html >t.html\n");
                     40:        fprintf(stderr,"  The keyword is stored in s.txt.\n");
                     41:        exit(0);
                     42:   }
1.1       takayama   43:   for (i=1; i<argc; i++) {
1.2       takayama   44:     m += strlen(argv[i])+1;
1.1       takayama   45:   }
                     46:   com = (char *)mymalloc(m);
                     47:   sprintf(com,"grep ");
                     48:   for (i=1; i<argc; i++) {
1.2       takayama   49:     if (strcmp(argv[i],"--sjis")==0) {
                     50:       Sjis = 1;
                     51:     }else if (strcmp(argv[i],"--leaveTag")==0) {
                     52:       LeaveTag = 1;
                     53:     }else {
                     54:       sprintf(&(com[strlen(com)]),"%s ",argv[i]);
                     55:     }
1.1       takayama   56:   }
                     57:   sprintf(workf,"/tmp/tmp-oxgreph-%d.txt",pid);
                     58:   sprintf(&(com[strlen(com)]),">%s",workf);
                     59:   system(com);
                     60:
                     61:   printf("<ol>\n");
                     62:   fp = fopen(workf,"r");
                     63:   if (fp == NULL) {
1.2       takayama   64:     fprintf(stderr,"Open error of the workfile.\n"); exit(1);
1.1       takayama   65:   }
                     66:   while (fgets(s,SSIZE-1,fp)) {
1.2       takayama   67:     cpos = -1; n = strlen(s);
                     68:     for (i=0; i< n; i++) {
                     69:       if (s[i] == ':') {
                     70:         cpos = i; break;
                     71:       }
                     72:     }
                     73:     if (cpos >= 0) {
                     74:       s[cpos] = 0; start = 0;
                     75:       for (i=cpos+1; i<n; i++) {
                     76:         if (Sjis && (((unsigned char)s[i]) >= 0x80)) {
                     77:           i++;
                     78:           continue; /* skip the next byte. */
                     79:         }
                     80:         if (s[i] == 0x1b) { /* skip escape sequence */
                     81:           i++;
                     82:           while (s[i] != 0x1b) i++;
                     83:         }
                     84:         if (LeaveTag) ;
                     85:         else {
                     86:           if (s[i] == '<') { s[i]='{'; }
                     87:           else if (s[i] == '>') { s[i]='}'; }
1.1       takayama   88:         }
1.2       takayama   89:         if (s[i] == '\n') s[i] = ' ';
                     90:       }
                     91:       printf("<li><a href=\"%s\">%s</a> <pre>%s </pre>\n",s,s,&(s[cpos+1]));
                     92:     }
1.1       takayama   93:   }
                     94:   printf("</ol>\n");
                     95:   sprintf(com,"rm -f %s",workf);
                     96:   system(com);
                     97: }

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