~ubuntu-branches/debian/sid/skksearch/sid

« back to all changes in this revision

Viewing changes to .pc/clean-build-errors-and-warnings.diff/main.c

  • Committer: Package Import Robot
  • Author(s): Tatsuya Kinoshita
  • Date: 2012-09-01 20:53:26 UTC
  • Revision ID: package-import@ubuntu.com-20120901205326-rmw3gwmmpn3tkx23
Tags: 0.0-21
* debian/rules: Revive the -Wall option for CFLAGS
* debian/control: Fix Vcs-Browser
* debian/patches/*: Revert the previous changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1999 Hideki Sakurada
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 */
 
14
 
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
#include <string.h>
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#ifdef SYSLOG
 
22
#include <syslog.h>
 
23
#endif /* SYSLOG */
 
24
#include "err.h"
 
25
 
 
26
#include "dic.h"
 
27
 
 
28
#define STDIN 0
 
29
#define STDOUT 1
 
30
 
 
31
#define COMBUFSIZE 1024
 
32
char combuf[COMBUFSIZE];
 
33
 
 
34
#define CLIENT_END       '0'
 
35
#define CLIENT_REQUEST   '1'
 
36
#define CLIENT_VERSION   '2'
 
37
#define CLIENT_HOST      '3'
 
38
 
 
39
#define SERVER_ERROR     '0'
 
40
#define SERVER_FOUND     '1'
 
41
#define SERVER_NOT_FOUND '4'
 
42
#define SERVER_FULL      '9'
 
43
 
 
44
char *version_string = "0.0";
 
45
 
 
46
/* declared in err.c */
 
47
extern int loglevel;
 
48
extern int use_syslog;
 
49
extern char *logfile;
 
50
 
 
51
/* dic */
 
52
#define MAXDICNUM 100
 
53
struct dic *diclist[MAXDICNUM];
 
54
int dicnum;
 
55
 
 
56
#define MAXENTNUM 1024
 
57
char *entlist[MAXENTNUM];
 
58
int entnum;
 
59
int inentry(char *);
 
60
void searchdicts();
 
61
 
 
62
 
 
63
main(int argc, char *argv[]) {
 
64
  int c;
 
65
  int s;
 
66
  int i;
 
67
  char *p;
 
68
  int errflag = 0;
 
69
  int terminal = 0;
 
70
  char *conf = NULL;
 
71
  extern char *optarg;
 
72
  extern int optind;
 
73
 
 
74
  /* process arguments */
 
75
  while ((c = getopt(argc, argv, "l:f:sic:t")) != EOF) {
 
76
   switch (c) {
 
77
    case 'l':
 
78
      loglevel = atoi(optarg);
 
79
      break;
 
80
    case 'f':
 
81
      logfile = optarg;
 
82
      break;
 
83
    case 's':
 
84
      use_syslog = 1;
 
85
      break;
 
86
    case 't':
 
87
      terminal = 1;
 
88
    case 'c':
 
89
      conf = optarg;
 
90
      break;
 
91
    case '?':
 
92
      errflag = 1;
 
93
    }
 
94
  }
 
95
  if (errflag) {
 
96
    fprintf(stderr,
 
97
            "usage: %s [-l digit] [-f logfile] [-s] [-c config] [dictinary1 ...] \n",
 
98
            argv[0]);
 
99
    exit(1);
 
100
  }
 
101
 
 
102
  /* open logfile or syslog */
 
103
  openerr();
 
104
  err(LOG_INFO, "%s started\n", argv[0]);
 
105
 
 
106
  /* open dictionaries */
 
107
  if ((dicnum = argc - optind) > MAXDICNUM) {
 
108
    err(LOG_ERR, "too many %d dicts\n", argc - optind);
 
109
    exit(1);
 
110
  }
 
111
  for (i = 0; i < dicnum; i++) {
 
112
    err(LOG_DEBUG, "openning dictionary %s", argv[optind + i]);
 
113
    diclist[i] = dic_open(argv[optind + i]);
 
114
  }
 
115
  if (conf != NULL) {
 
116
     FILE *fp;
 
117
     char line[1024];
 
118
     char *p;
 
119
     fp = fopen(conf, "r");
 
120
     if (fp == NULL) {
 
121
        err(LOG_ERR, "cannot open config file `%s'\n", conf);
 
122
        exit(1);
 
123
     }
 
124
     while ((p = fgets(line, sizeof(line), fp)) != NULL) {
 
125
        if (p[0] == '#' || p[0] == '\n') 
 
126
            continue;
 
127
        p[strlen(p)-1] = '\0'; /* chop */
 
128
        if (i >= MAXDICNUM) {
 
129
            err(LOG_ERR, "too many dicts in config file\n");
 
130
            exit(1);
 
131
        }
 
132
        err(LOG_DEBUG, "openning dictionary %s", p);
 
133
        dicnum++;
 
134
        diclist[i++] = dic_open(p);
 
135
     }
 
136
     fclose(fp);
 
137
  }
 
138
  if (dicnum == 0) {
 
139
     err(LOG_ERR, "no dict specified\n");
 
140
     exit(1);
 
141
  }
 
142
 
 
143
  /* main loop */
 
144
#ifdef DEBUG_FROM_TERMINAL
 
145
  while (fgets(combuf, COMBUFSIZE, stdin) != NULL) {
 
146
#else
 
147
  while ((s = read(STDIN, combuf, COMBUFSIZE - 1)) > 0) {
 
148
    combuf[s] = '\0';
 
149
#endif
 
150
    switch (combuf[0]) {
 
151
    case CLIENT_END:
 
152
      goto end;
 
153
    case CLIENT_REQUEST:
 
154
      searchdicts();
 
155
      break;
 
156
    case CLIENT_VERSION:
 
157
      write(STDOUT, version_string, strlen(version_string));
 
158
      break;
 
159
    case CLIENT_HOST:           /* not yet XXX */
 
160
      write(STDOUT, "localhost:", strlen("localhost:"));
 
161
      break;
 
162
    }
 
163
  }
 
164
 
 
165
end:
 
166
  /* close dictionaries */
 
167
  for (i = 0; i < dicnum; i++) {
 
168
    diclist[i]->close(diclist[i]);
 
169
  }
 
170
 
 
171
  /* closing logfile or syslog */
 
172
  err(LOG_DEBUG, "closing log\n");
 
173
  closelog();
 
174
 
 
175
  exit(0);
 
176
}
 
177
 
 
178
 
 
179
 
 
180
void searchdicts(){
 
181
  int i;
 
182
  char *p;
 
183
  int keylen;
 
184
  char *key;
 
185
 
 
186
  /* initialize */
 
187
  entnum = 0;
 
188
 
 
189
  key = &combuf[1];
 
190
 
 
191
  if ((p = strchr(key, ' ')) == NULL) {
 
192
    goto output;
 
193
  }
 
194
  keylen = p - key;
 
195
 
 
196
  /* search */
 
197
  for (i = 0; i < dicnum; i++) {
 
198
    if ((p = diclist[i]->search(diclist[i], key, keylen)) != NULL) {
 
199
      if ((p = strtok(p, "/")) == NULL) {
 
200
        err(LOG_ERR, "searchdicts: invalid entry %*s\n", keylen, key);
 
201
        exit(1);
 
202
      }
 
203
      do {
 
204
        if (!inentry(p)) {
 
205
          entlist[entnum++] = p;
 
206
          if (entnum == MAXENTNUM) goto output;
 
207
        }
 
208
      } while ((p = strtok(NULL, "/\n")) != NULL);
 
209
    }
 
210
  }
 
211
 
 
212
  /* output */
 
213
output:
 
214
  if (entnum == 0) {
 
215
    combuf[0] = SERVER_NOT_FOUND;
 
216
    fputs(combuf, stdout);
 
217
  } else {
 
218
    putchar(SERVER_FOUND);
 
219
    for (i = 0; i < entnum; i++) {
 
220
      putchar('/');
 
221
      fputs(entlist[i], stdout);
 
222
    }
 
223
    fputs("/\n", stdout);
 
224
  }
 
225
  fflush(stdout);
 
226
}
 
227
 
 
228
 
 
229
int inentry(char *entry) {
 
230
  int i;
 
231
  for (i = 0; i < entnum; i++) {
 
232
    if (strcmp(entry, entlist[i]) == 0) {
 
233
      return 1;
 
234
    }
 
235
  }
 
236
  return 0;
 
237
}