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

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

1.5     ! takayama    1: /* $OpenXM: OpenXM/src/util/oxgreph.c,v 1.4 2009/02/22 17:30:03 ohara 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.5     ! takayama   25: int main(int argc, char *argv[]) {
1.1       takayama   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;
1.5     ! takayama   33:   int r;
1.1       takayama   34:   m = 256;
                     35:   pid = getpid();
1.3       takayama   36:   if (argc < 2) {
                     37:        fprintf(stderr,"oxgreph [--leaveTag] args_to_grep \n");
                     38:        fprintf(stderr,"Note: LC_ALL should be C on some systems or use -f to give the pattern.\n");
                     39:        fprintf(stderr,"Example 1:  oxgreph \"`nkf -w s.txt`\" $OpenXM_HOME/doc/cfep/html-ja_JP.utf8/*.html >t.html\n");
                     40:        fprintf(stderr,"Example 2:  oxgreph -f s.txt $OpenXM_HOME/doc/cfep/html-ja_JP.utf8/*.html >t.html\n");
                     41:        fprintf(stderr,"  The keyword is stored in s.txt.\n");
                     42:        exit(0);
                     43:   }
1.1       takayama   44:   for (i=1; i<argc; i++) {
1.2       takayama   45:     m += strlen(argv[i])+1;
1.1       takayama   46:   }
                     47:   com = (char *)mymalloc(m);
                     48:   sprintf(com,"grep ");
                     49:   for (i=1; i<argc; i++) {
1.2       takayama   50:     if (strcmp(argv[i],"--sjis")==0) {
                     51:       Sjis = 1;
                     52:     }else if (strcmp(argv[i],"--leaveTag")==0) {
                     53:       LeaveTag = 1;
                     54:     }else {
                     55:       sprintf(&(com[strlen(com)]),"%s ",argv[i]);
                     56:     }
1.1       takayama   57:   }
                     58:   sprintf(workf,"/tmp/tmp-oxgreph-%d.txt",pid);
                     59:   sprintf(&(com[strlen(com)]),">%s",workf);
1.5     ! takayama   60:   r=system(com);
1.1       takayama   61:
                     62:   printf("<ol>\n");
                     63:   fp = fopen(workf,"r");
                     64:   if (fp == NULL) {
1.2       takayama   65:     fprintf(stderr,"Open error of the workfile.\n"); exit(1);
1.1       takayama   66:   }
                     67:   while (fgets(s,SSIZE-1,fp)) {
1.2       takayama   68:     cpos = -1; n = strlen(s);
                     69:     for (i=0; i< n; i++) {
                     70:       if (s[i] == ':') {
                     71:         cpos = i; break;
                     72:       }
                     73:     }
                     74:     if (cpos >= 0) {
                     75:       s[cpos] = 0; start = 0;
                     76:       for (i=cpos+1; i<n; i++) {
                     77:         if (Sjis && (((unsigned char)s[i]) >= 0x80)) {
                     78:           i++;
                     79:           continue; /* skip the next byte. */
                     80:         }
                     81:         if (s[i] == 0x1b) { /* skip escape sequence */
                     82:           i++;
                     83:           while (s[i] != 0x1b) i++;
                     84:         }
                     85:         if (LeaveTag) ;
                     86:         else {
                     87:           if (s[i] == '<') { s[i]='{'; }
                     88:           else if (s[i] == '>') { s[i]='}'; }
1.1       takayama   89:         }
1.2       takayama   90:         if (s[i] == '\n') s[i] = ' ';
                     91:       }
                     92:       printf("<li><a href=\"%s\">%s</a> <pre>%s </pre>\n",s,s,&(s[cpos+1]));
                     93:     }
1.1       takayama   94:   }
                     95:   printf("</ol>\n");
                     96:   sprintf(com,"rm -f %s",workf);
1.5     ! takayama   97:   r=system(com);
        !            98:   return 0;
1.1       takayama   99: }

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