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

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

1.2     ! takayama    1: /* $OpenXM: OpenXM/src/util/oxgreph.c,v 1.1 2006/03/04 07:48:12 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.2     ! takayama   14:
        !            15:    Example:
        !            16:    s.txt contains "insubunkai"
        !            17:    oxgreph --sjis `cat s.txt` $OpenXM_HOME/doc/cfep/html-ja_JP.sjis/html-jp/*.html >t.html
1.1       takayama   18: */
1.2     ! takayama   19:
        !            20: int Sjis = 0;
        !            21: int LeaveTag = 0;
1.1       takayama   22: main(int argc, char *argv[]) {
                     23:   int i,m,pid,n, cpos,start;
                     24:   char *com;
                     25:   char workf[256];
                     26: #define SSIZE 1024
                     27:   char s[SSIZE];
                     28:   char ss[SSIZE*2];
                     29:   FILE *fp;
                     30:   m = 256;
                     31:   pid = getpid();
                     32:   for (i=1; i<argc; i++) {
1.2     ! takayama   33:     m += strlen(argv[i])+1;
1.1       takayama   34:   }
                     35:   com = (char *)mymalloc(m);
                     36:   sprintf(com,"grep ");
                     37:   for (i=1; i<argc; i++) {
1.2     ! takayama   38:     if (strcmp(argv[i],"--sjis")==0) {
        !            39:       Sjis = 1;
        !            40:     }else if (strcmp(argv[i],"--leaveTag")==0) {
        !            41:       LeaveTag = 1;
        !            42:     }else {
        !            43:       sprintf(&(com[strlen(com)]),"%s ",argv[i]);
        !            44:     }
1.1       takayama   45:   }
                     46:   sprintf(workf,"/tmp/tmp-oxgreph-%d.txt",pid);
                     47:   sprintf(&(com[strlen(com)]),">%s",workf);
                     48:   system(com);
                     49:
                     50:   printf("<ol>\n");
                     51:   fp = fopen(workf,"r");
                     52:   if (fp == NULL) {
1.2     ! takayama   53:     fprintf(stderr,"Open error of the workfile.\n"); exit(1);
1.1       takayama   54:   }
                     55:   while (fgets(s,SSIZE-1,fp)) {
1.2     ! takayama   56:     cpos = -1; n = strlen(s);
        !            57:     for (i=0; i< n; i++) {
        !            58:       if (s[i] == ':') {
        !            59:         cpos = i; break;
        !            60:       }
        !            61:     }
        !            62:     if (cpos >= 0) {
        !            63:       s[cpos] = 0; start = 0;
        !            64:       for (i=cpos+1; i<n; i++) {
        !            65:         if (Sjis && (((unsigned char)s[i]) >= 0x80)) {
        !            66:           i++;
        !            67:           continue; /* skip the next byte. */
        !            68:         }
        !            69:         if (s[i] == 0x1b) { /* skip escape sequence */
        !            70:           i++;
        !            71:           while (s[i] != 0x1b) i++;
        !            72:         }
        !            73:         if (LeaveTag) ;
        !            74:         else {
        !            75:           if (s[i] == '<') { s[i]='{'; }
        !            76:           else if (s[i] == '>') { s[i]='}'; }
1.1       takayama   77:         }
1.2     ! takayama   78:         if (s[i] == '\n') s[i] = ' ';
        !            79:       }
        !            80:       printf("<li><a href=\"%s\">%s</a> <pre>%s </pre>\n",s,s,&(s[cpos+1]));
        !            81:     }
1.1       takayama   82:   }
                     83:   printf("</ol>\n");
                     84:   sprintf(com,"rm -f %s",workf);
                     85:   system(com);
                     86: }

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