File: discover_main.c

package info (click to toggle)
grok 1.20110708.1-4.3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,460 kB
  • sloc: ansic: 3,469; ruby: 987; makefile: 276; sh: 124; yacc: 106
file content (81 lines) | stat: -rw-r--r-- 1,853 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define _GNU_SOURCE
#include "grok.h"
#include "grok_program.h"
#include "grok_config.h"

#include "conf.tab.h"
#include <unistd.h>
#include <getopt.h>

extern char *optarg; /* from unistd.h, getopt */
extern FILE *yyin; /* from conf.lex (flex provides this) */
static char *g_prog;

void usage() {
  printf("Usage: %s [--verbose] [--patterns PATTERNSFILE]\n", g_prog);
  printf("       --verbose\n");
  printf("       --patterns PATTERNSFILE\n");
}

int main(int argc, char **argv) {
  int opt = 0;
  g_prog = argv[0];

  struct option options[] = {
    { "patterns", required_argument, NULL, 'p' },
    { "help", no_argument, NULL, 'h' },
    { "verbose", no_argument, NULL, 'v' },
    { 0, 0, 0, 0 }
  };

  const char *prog = argv[0];
  grok_t grok;
  grok_init(&grok);

  int pattern_count = 0;
  while ((opt = getopt_long_only(argc, argv, "hp:v", options, &optind)) != -1) {
    switch (opt) {
      case 'h':
        usage();
        return 0;
      case 'p':
        pattern_count++;
        grok_patterns_import_from_file(&grok, optarg);
        break;
      case 'v':
        grok.logmask =~ 0;
        break;
      default:
        usage();
        return 1;
    }
  }

  if (pattern_count == 0) {
    fprintf(stderr, "%s: No patterns loaded.\n", prog);
    fprintf(stderr, "You want to specify at least one patterns file to load\n");
    return 1;
  }

  argc -= optind;
  argv += optind;

  int i;
  FILE *fp = stdin;
  if (argc > 0 && strcmp(argv[0], "-")) {
    fp = fopen(argv[0], "r");
  }

  char buf[4096];
  grok_discover_t *gdt;
  gdt = grok_discover_new(&grok);
  char *discovery;
  int unused_length;
  while (fgets(buf, 4096, fp) != NULL) {
    strrchr(buf, '\n')[0] = '\0';
    grok_discover(gdt, buf, &discovery, &unused_length);
    printf("%s\n", discovery);
    free(discovery);
  }
  grok_discover_free(gdt);
  return 0; }