File: main.c

package info (click to toggle)
skksearch 0.0-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 228 kB
  • sloc: ansic: 1,079; makefile: 23; sh: 18
file content (237 lines) | stat: -rw-r--r-- 4,841 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 1999 Hideki Sakurada
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"

#ifdef SYSLOG
#include <syslog.h>
#endif /* SYSLOG */
#include "err.h"

#include "dic.h"

#define STDIN 0
#define STDOUT 1

#define COMBUFSIZE 1024
char combuf[COMBUFSIZE];

#define CLIENT_END       '0'
#define CLIENT_REQUEST   '1'
#define CLIENT_VERSION   '2'
#define CLIENT_HOST      '3'

#define SERVER_ERROR     '0'
#define SERVER_FOUND     '1'
#define SERVER_NOT_FOUND '4'
#define SERVER_FULL      '9'

char *version_string = "0.0";

/* declared in err.c */
extern int loglevel;
extern int use_syslog;
extern char *logfile;

/* dic */
#define MAXDICNUM 100
struct dic *diclist[MAXDICNUM];
int dicnum;

#define MAXENTNUM 1024
char *entlist[MAXENTNUM];
int entnum;
int inentry(char *);
void searchdicts();


int main(int argc, char *argv[]) {
  int c;
  int s;
  int i;
  int errflag = 0;
  int terminal = 0;
  char *conf = NULL;
  extern char *optarg;
  extern int optind;

  /* process arguments */
  while ((c = getopt(argc, argv, "l:f:sic:t")) != EOF) {
   switch (c) {
    case 'l':
      loglevel = atoi(optarg);
      break;
    case 'f':
      logfile = optarg;
      break;
    case 's':
      use_syslog = 1;
      break;
    case 't':
      terminal = 1;
    case 'c':
      conf = optarg;
      break;
    case '?':
      errflag = 1;
    }
  }
  if (errflag) {
    fprintf(stderr,
	    "usage: %s [-l digit] [-f logfile] [-s] [-c config] [dictinary1 ...] \n",
	    argv[0]);
    exit(1);
  }

  /* open logfile or syslog */
  openerr();
  err(LOG_INFO, "%s started\n", argv[0]);

  /* open dictionaries */
  if ((dicnum = argc - optind) > MAXDICNUM) {
    err(LOG_ERR, "too many %d dicts\n", argc - optind);
    exit(1);
  }
  for (i = 0; i < dicnum; i++) {
    err(LOG_DEBUG, "opening dictionary %s", argv[optind + i]);
    diclist[i] = dic_open(argv[optind + i]);
  }
  if (conf != NULL) {
     FILE *fp;
     char line[1024];
     char *p;
     fp = fopen(conf, "r");
     if (fp == NULL) {
	err(LOG_ERR, "cannot open config file `%s'\n", conf);
	exit(1);
     }
     while ((p = fgets(line, sizeof(line), fp)) != NULL) {
	if (p[0] == '#' || p[0] == '\n') 
	    continue;
	p[strlen(p)-1] = '\0'; /* chop */
	if (i >= MAXDICNUM) {
	    err(LOG_ERR, "too many dicts in config file\n");
	    exit(1);
	}
	err(LOG_DEBUG, "opening dictionary %s", p);
	dicnum++;
	diclist[i++] = dic_open(p);
     }
     fclose(fp);
  }
  if (dicnum == 0) {
     err(LOG_ERR, "no dict specified\n");
     exit(1);
  }

  /* main loop */
#ifdef DEBUG_FROM_TERMINAL
  while (fgets(combuf, COMBUFSIZE, stdin) != NULL) {
#else
  while ((s = read(STDIN, combuf, COMBUFSIZE - 1)) > 0) {
    combuf[s] = '\0';
#endif
    switch (combuf[0]) {
    case CLIENT_END:
      goto end;
    case CLIENT_REQUEST:
      searchdicts();
      break;
    case CLIENT_VERSION:
      write(STDOUT, version_string, strlen(version_string));
      break;
    case CLIENT_HOST:		/* not yet XXX */
      write(STDOUT, "localhost:", strlen("localhost:"));
      break;
    }
  }

end:
  /* close dictionaries */
  for (i = 0; i < dicnum; i++) {
    diclist[i]->close(diclist[i]);
  }

  /* closing logfile or syslog */
  err(LOG_DEBUG, "closing log\n");
  closelog();

  exit(0);
}



void searchdicts(){
  int i;
  char *p;
  int keylen;
  char *key;

  /* initialize */
  entnum = 0;

  key = &combuf[1];

  if ((p = strchr(key, ' ')) == NULL) {
    goto output;
  }
  keylen = p - key;

  /* search */
  for (i = 0; i < dicnum; i++) {
    if ((p = diclist[i]->search(diclist[i], key, keylen)) != NULL) {
      if ((p = strtok(p, "/")) == NULL) {
	err(LOG_ERR, "searchdicts: invalid entry %*s\n", keylen, key);
	exit(1);
      }
      do {
	if (!inentry(p)) {
	  entlist[entnum++] = p;
	  if (entnum == MAXENTNUM) goto output;
	}
      } while ((p = strtok(NULL, "/\n")) != NULL);
    }
  }

  /* output */
output:
  if (entnum == 0) {
    combuf[0] = SERVER_NOT_FOUND;
    fputs(combuf, stdout);
  } else {
    putchar(SERVER_FOUND);
    for (i = 0; i < entnum; i++) {
      putchar('/');
      fputs(entlist[i], stdout);
    }
    fputs("/\n", stdout);
  }
  fflush(stdout);
}


int inentry(char *entry) {
  int i;
  for (i = 0; i < entnum; i++) {
    if (strcmp(entry, entlist[i]) == 0) {
      return 1;
    }
  }
  return 0;
}