filter.c
author viric@llimona
Sun, 02 Sep 2007 15:59:31 +0200
changeset 26 700a68421116
parent 4 b2dfe3374454
child 31 2bcc08d0f1d8
permissions -rw-r--r--
Adding .hgignore and the ia5test.
viric@0
     1
#include <stdio.h>
viric@0
     2
#include <stdlib.h>
viric@0
     3
#include <sys/select.h>
viric@0
     4
#include <sys/types.h>
viric@0
     5
#include <sys/wait.h>
viric@0
     6
#include <unistd.h>
viric@2
     7
#include <fcntl.h>
viric@0
     8
viric@0
     9
#include "dictre.h"
viric@0
    10
viric@1
    11
enum
viric@1
    12
{
viric@3
    13
    SIZESTEP=1000
viric@1
    14
};
viric@1
    15
viric@0
    16
extern struct Def defs[];
viric@0
    17
extern int ndefs;
viric@0
    18
extern int dont_touch[];
viric@0
    19
extern int ndont_touch;
viric@0
    20
viric@0
    21
static void more_memory(void **ptr, int size)
viric@0
    22
{
viric@0
    23
    void *new;
viric@0
    24
viric@0
    25
    new = realloc(*ptr, size);
viric@0
    26
    *ptr = new;
viric@0
    27
}
viric@0
    28
viric@0
    29
static
viric@0
    30
char * manage_filter(const char *def, int deflen, int writeto, int readfrom,
viric@0
    31
        int *outlen)
viric@0
    32
{
viric@0
    33
    int maxfd;
viric@0
    34
    int defptr;
viric@0
    35
    int outptr;
viric@0
    36
    char *out;
viric@0
    37
    int outsize;
viric@0
    38
    int outrest;
viric@2
    39
    int res;
viric@0
    40
viric@0
    41
    out = 0;
viric@1
    42
    outsize = SIZESTEP;
viric@0
    43
    outptr = 0;
viric@0
    44
    more_memory((void **) &out, outsize);
viric@1
    45
    outrest = SIZESTEP;
viric@0
    46
viric@2
    47
    /* We need unblocking write for select() */
viric@2
    48
    res = fcntl(writeto, F_SETFL, O_NONBLOCK);
viric@2
    49
    if (res == -1)
viric@2
    50
    {
viric@2
    51
        perror("Error setting nonblock to writeto");
viric@2
    52
        exit(-1);
viric@2
    53
    }
viric@2
    54
viric@0
    55
    maxfd = writeto;
viric@0
    56
    if (readfrom > maxfd)
viric@0
    57
        maxfd = readfrom;
viric@0
    58
viric@0
    59
    defptr = 0;
viric@0
    60
    do
viric@0
    61
    {
viric@0
    62
        fd_set writeset, readset;
viric@0
    63
        FD_ZERO(&writeset);
viric@0
    64
        FD_ZERO(&readset);
viric@0
    65
        if (defptr < deflen)
viric@0
    66
            FD_SET(writeto, &writeset);
viric@0
    67
        FD_SET(readfrom, &readset);
viric@0
    68
viric@0
    69
        select(maxfd+1, &readset, &writeset, 0, 0);
viric@0
    70
viric@0
    71
        if (FD_ISSET(readfrom, &readset))
viric@0
    72
        {
viric@0
    73
            int res;
viric@0
    74
            res = read(readfrom, out + outptr, outrest);
viric@0
    75
            if (res == 0)
viric@0
    76
            {
viric@0
    77
                close(readfrom);
viric@0
    78
                break;
viric@0
    79
            }
viric@0
    80
            outrest -= res;
viric@0
    81
            outptr += res;
viric@0
    82
            if (outrest == 0)
viric@0
    83
            {
viric@1
    84
                outrest = SIZESTEP;
viric@1
    85
                outsize += SIZESTEP;
viric@0
    86
                more_memory((void **) &out, outsize);
viric@0
    87
            }
viric@0
    88
        }
viric@0
    89
viric@0
    90
        if (FD_ISSET(writeto, &writeset))
viric@0
    91
        {
viric@0
    92
            int res;
viric@1
    93
            res = write(writeto, def+defptr, deflen - defptr);
viric@1
    94
            defptr += res;
viric@0
    95
            if (defptr >= deflen)
viric@0
    96
                close(writeto);
viric@0
    97
        }
viric@0
    98
    } while(1);
viric@0
    99
viric@2
   100
    /*
viric@2
   101
    {
viric@2
   102
        int i;
viric@2
   103
        printf("In : ");
viric@2
   104
        for(i=0; i < deflen; ++i)
viric@2
   105
            putchar(def[i]);
viric@2
   106
        printf("\nOut: ");
viric@2
   107
        for(i=0; i < outptr; ++i)
viric@2
   108
            putchar(out[i]);
viric@2
   109
        putchar('\n');
viric@2
   110
    }
viric@2
   111
    */
viric@2
   112
viric@0
   113
    if (defptr < deflen)
viric@0
   114
    {
viric@0
   115
        fprintf(stderr, "Error in filter! not all written.\n");
viric@0
   116
        exit(-1);
viric@0
   117
    }
viric@0
   118
viric@3
   119
    /* Give away memory don't needed */
viric@3
   120
    more_memory((void **) &out, outptr);
viric@3
   121
viric@0
   122
    *outlen = outptr;
viric@0
   123
    return out;
viric@0
   124
}
viric@0
   125
viric@0
   126
static char * filter(char *def, int deflen, const char *filter_par, int *outlen)
viric@0
   127
{
viric@0
   128
    int write_pipe[2];
viric@0
   129
    int read_pipe[2];
viric@0
   130
    int pid;
viric@0
   131
    int res;
viric@0
   132
    int status;
viric@0
   133
    char *out;
viric@0
   134
viric@0
   135
    pipe(write_pipe);
viric@0
   136
    pipe(read_pipe);
viric@0
   137
viric@0
   138
viric@0
   139
    pid = fork();
viric@0
   140
    switch(pid)
viric@0
   141
    {
viric@0
   142
        case 0:  /* child */
viric@0
   143
            close(0);
viric@0
   144
            dup(write_pipe[0]);
viric@0
   145
            close(write_pipe[0]);
viric@0
   146
            close(write_pipe[1]);
viric@0
   147
            close(1);
viric@0
   148
            dup(read_pipe[1]);
viric@0
   149
            close(read_pipe[1]);
viric@0
   150
            close(read_pipe[0]);
viric@2
   151
            execl(filter_par, filter_par, 0);
viric@0
   152
            perror("execlp");
viric@0
   153
            exit(-1);
viric@0
   154
            break;
viric@0
   155
        case -1:
viric@0
   156
            perror("fork");
viric@0
   157
            exit(-1);
viric@0
   158
            break;
viric@0
   159
        default:  /* parent */
viric@0
   160
            close(write_pipe[0]);
viric@0
   161
            close(read_pipe[1]);
viric@0
   162
            break;
viric@0
   163
    }
viric@0
   164
viric@0
   165
    /* parent */
viric@0
   166
    out = manage_filter(def, deflen, write_pipe[1], read_pipe[0], outlen);
viric@0
   167
viric@0
   168
    res = wait(&status);
viric@0
   169
    if (res != pid || WEXITSTATUS(status) != 0)
viric@0
   170
    {
viric@0
   171
        fprintf(stderr, "Error filtering: pid=%i status=%i",
viric@0
   172
                pid, WEXITSTATUS(status));
viric@0
   173
        exit(-1);
viric@0
   174
    }
viric@0
   175
viric@0
   176
    return out;
viric@0
   177
}
viric@0
   178
viric@0
   179
static int in_dont_touch(int n)
viric@0
   180
{
viric@0
   181
    int i;
viric@0
   182
    for(i =0; i < ndont_touch; ++i)
viric@0
   183
    {
viric@0
   184
        if (n == dont_touch[i])
viric@0
   185
        {
viric@0
   186
            return 1;
viric@0
   187
        }
viric@0
   188
    }
viric@0
   189
    return 0;
viric@0
   190
}
viric@0
   191
viric@0
   192
void filter_all(const char *filter_par)
viric@0
   193
{
viric@0
   194
    int i;
viric@2
   195
    static int dispndefs = 0;
viric@2
   196
    static int filtereddefs = 0;
viric@0
   197
viric@0
   198
    for(i=0; i < ndefs; ++i)
viric@0
   199
    {
viric@0
   200
        char *newdef;
viric@0
   201
        int newdeflen;
viric@0
   202
        if (!in_dont_touch(i))
viric@0
   203
        {
viric@1
   204
            if (defs[i].length > 0)
viric@1
   205
            {
viric@1
   206
                newdef = filter(defs[i].d, defs[i].length,
viric@1
   207
                        filter_par, &newdeflen);
viric@1
   208
                defs[i].length = newdeflen;
viric@1
   209
                free(defs[i].d);
viric@1
   210
                defs[i].d = newdef;
viric@1
   211
            }
viric@2
   212
            filtereddefs++; /* Not really all filtered. All but the 00-database* */
viric@2
   213
            dispndefs++;
viric@3
   214
            if (dispndefs >= 1000)
viric@2
   215
            {
viric@2
   216
                dispndefs = 0;
viric@3
   217
                printf("Filtered: %i/%i (%f%%)\n", filtereddefs, ndefs,
viric@4
   218
                        (float) filtereddefs / (float) ndefs * 100.);
viric@2
   219
            }
viric@0
   220
        }
viric@0
   221
    }
viric@0
   222
}