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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  
     | 
    
      /*
 * Copyright (C) 2002 The Free Software Initiative of Japan
 * Author: NIIBE Yutaka
 */
/*
 * ANTHY Low Level Agent
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <anthy/anthy.h>
#include "config.h"
/*
 *            connection        context 
 *  S -- open -> [ ] -- new ----> [ ] -- convert --> [ ] --> get_candidates
 *   <- close --     <- release -     <-- commit --      --> resize_segment
 *                                    <-- cancel --      --> select_candidate
 *                                                
 */
struct context {
  struct anthy_context *ac;
  int buflen;
  unsigned char *buf;
  int removed, inserted;
  int sellen;
  int *selection;
};
#define MAX_CONTEXT 16
static struct context contexts[MAX_CONTEXT];
extern int use_utf8;
#define INITIAL_BUFLEN 512
#define INITIAL_SELLEN 128
/*
 * Returns -1 on error.
 * Returns >= 0 on success, and the number is  context descriptor.
 */
static int
new_context (void)
{
  int i;
  for (i = 0; i < MAX_CONTEXT; i++)
    if (contexts[i].buflen == 0) { /* Found free one */
      struct context *c = &contexts[i];
      if ((c->buf = (unsigned char *)malloc (INITIAL_BUFLEN)) == NULL)
	return -1;
      if ((c->selection = (int *)malloc (INITIAL_SELLEN)) == NULL) {
	free (c->buf);
	c->buf = NULL;
	return -1;
      }
      if ((c->ac = anthy_create_context ()) == NULL) {
	free (c->buf);
	c->buf = NULL;
	free (c->selection);
	c->selection = NULL;
	return -1;
      }
      if (use_utf8) {
	anthy_context_set_encoding(c->ac, ANTHY_UTF8_ENCODING);
      }
      c->buflen = INITIAL_BUFLEN;
      c->sellen = INITIAL_SELLEN;
      return i;
    }
  /* No free context to be used */
  return -1;
}
static int
release_context (int c_desc)
{
  struct context *c = &contexts[c_desc];
  anthy_release_context(c->ac);
  free (c->buf);
  c->buf = NULL;
  c->buflen = 0;
  free (c->selection);
  c->selection = NULL;
  c->sellen = 0;
  return 0;
}
static struct context *
c_desc_to_context (int c_desc)
{
  return &contexts[c_desc];
}
static int
get_number_of_segments (struct context *c)
{
  struct anthy_conv_stat cs;
  if (anthy_get_stat(c->ac, &cs) < 0)
    return -1;
  return cs.nr_segment;
}
static int
begin_conversion (struct context *c, const char *input)
{
  int i;
  int seg_num;
  if (anthy_set_string(c->ac, (char *)input) < 0)
    return -1;
  seg_num = get_number_of_segments (c);
  if (seg_num >= c->sellen) {
    c->sellen *= 2;
    c->selection = realloc (c->selection, c->sellen);
    if (c->selection == NULL) { /* Fatal */
      c->sellen = -1;
      return -1;
    }
  }
  for (i = 0; i < seg_num; i++)
    c->selection[i] = 0;
  return 0;
}
static int
end_conversion (struct context *c, int cancel)
{
  int n;
  int seg_num;
  int can_num;
  if (!cancel) {
    n = get_number_of_segments (c);
    for (seg_num = 0; seg_num < n; seg_num++) {
      can_num = c->selection[seg_num];
      anthy_commit_segment(c->ac, seg_num, can_num);
    }
  }
  return 0;
}
static int
get_segment_number_of_candidates (struct context *c, int seg_num)
{
  struct anthy_segment_stat ss;
  if (anthy_get_segment_stat (c->ac, seg_num, &ss) != 0)
    return -1;
  return ss.nr_candidate;
}
static const unsigned char *
get_segment_candidate (struct context *c, int seg_num, int cand_num)
{
  int len;
  while (1) {
    len = anthy_get_segment (c->ac, seg_num, cand_num,
			     (char *)c->buf, c->buflen);
    if (len < 0)
      return NULL;
    if (len < c->buflen)
      return c->buf;
    c->buflen *= 2;
    c->buf = realloc (c->buf, c->buflen);
    if (c->buf == NULL) { /* Fatal */
      c->buflen = -1;
      return NULL;
    }
  }
}
static const unsigned char *
get_segment_yomi (struct context *c, int seg_num)
{
  return get_segment_candidate (c, seg_num, NTH_UNCONVERTED_CANDIDATE);
}
static const unsigned char *
get_segment_converted (struct context *c, int seg_num)
{
  return get_segment_candidate (c, seg_num, 0);
}
static int
resize_segment (struct context *c, int seg_num, int inc_dec)
{
  int i;
  struct anthy_conv_stat cs;
  if (anthy_get_stat(c->ac, &cs) < 0)
    return -1;
  /* Replace all segments after SEG_NUM */
  c->removed = cs.nr_segment - seg_num;
  anthy_resize_segment(c->ac, seg_num, inc_dec?-1:1);
  if (anthy_get_stat(c->ac, &cs) < 0)
    return -1;
  c->inserted = cs.nr_segment - seg_num;
  if (cs.nr_segment >= c->sellen) {
    c->sellen *= 2;
    c->selection = realloc (c->selection, c->sellen);
    if (c->selection == NULL) { /* Fatal */
      c->sellen = -1;
      return -1;
    }
  }
  for (i = seg_num; i < cs.nr_segment; i++)
    c->selection[i] = 0;
  return seg_num;
}
/* Only valid after call of resize_segment or select_candidate */
static int
get_number_of_segments_removed (struct context *c, int seg_num)
{
  (void)seg_num;
  return c->removed;
}
/* Only valid after call of resize_segment or select_candidate */
static int
get_number_of_segments_inserted (struct context *c, int seg_num)
{
  (void)seg_num;
  return c->inserted;
}
static int
select_candidate (struct context *c, int seg_num, int can_num)
{
  /*
   * Anthy does not have capability to affect the result of selection
   * to other segments.
   */
  c->removed = 0;
  c->inserted = 0;
  /*
   * Record, but not call anthy_commit_segment.
   */
  c->selection[seg_num] = can_num;
  return seg_num;
}
static int
say_hello (void)
{
  const char *options = "";
  printf ("Anthy (Version %s) [%s] : Nice to meet you.\r\n", VERSION, 
	  options);
  fflush (stdout);
  return 0;
}
#define ERROR_CODE_UNKNOWN	400
#define ERROR_CODE_UNSUPPOTED	401
static int
say_unknown (void)
{
  printf ("-ERR %d Unknown command.\r\n", ERROR_CODE_UNKNOWN);
  fflush (stdout);
  return 0;
}
static int
do_commit (const char *line)
{
  char *p;
  struct context *c;
  int c_desc, cancel, r;
  c_desc = strtol (line+7, &p, 10);
  c = c_desc_to_context (c_desc);
  cancel = strtol (p+1, &p, 10);
  r = end_conversion (c, cancel);
  if (r < 0)
    printf ("-ERR %d commit failed.\r\n", -r);
  else
    printf ("+OK\r\n");
  fflush (stdout);
  return 0;
}
static void
output_segments (struct context *c, int seg_num, int removed, int inserted)
{
  int i;
  printf ("+DATA %d %d %d\r\n", seg_num, removed, inserted);
  for (i = seg_num; i < seg_num + inserted; i++) {
    int nc;
    nc = get_segment_number_of_candidates (c, i);
    printf ("%d " ,nc);
    printf ("%s ", get_segment_converted (c, i));
    printf ("%s\r\n", get_segment_yomi (c, i));
  }
  printf ("\r\n");
}
static int
do_convert (const char *line)
{
  char *p;
  struct context *c;
  int c_desc, r;
  c_desc = strtol (line+8, &p, 10);
  c = c_desc_to_context (c_desc);
  r = begin_conversion (c, p+1);
  if (r < 0)
    printf ("-ERR %d convert failed.\r\n", -r);
  else
    {
      int n = get_number_of_segments (c);
      output_segments (c, 0, 0, n);
    }
  fflush (stdout);
  return 0;
}
static int
do_get_candidates (const char *line)
{
  char *p;
  struct context *c;
  int c_desc, seg_num, cand_offset, max_cands;
  int nc, i, max;
  c_desc = strtol (line+15, &p, 10);
  seg_num = strtol (p+1, &p, 10);
  cand_offset = strtol (p+1, &p, 10);
  max_cands = strtol (p+1, &p, 10);
  c = c_desc_to_context (c_desc);
  nc = get_segment_number_of_candidates (c, seg_num);
  max = cand_offset + max_cands;
  if (nc < cand_offset + max_cands)
    max = nc;
  printf ("+DATA %d %d\r\n", cand_offset, max);
  for (i = cand_offset; i < max; i++)
    printf ("%s\r\n", get_segment_candidate (c, seg_num, i));
  printf ("\r\n");  
  fflush (stdout);
  return 0;
}
static int
do_new_context (const char *line)
{
  int r;
  /* XXX: Should check arguments */
  if (strncmp (" INPUT=#18 OUTPUT=#18", line+11, 20) != 0) {
    printf ("-ERR %d unsupported context\r\n", ERROR_CODE_UNSUPPOTED);
    return 1;
  }
  r = new_context ();
  if (r < 0)
    printf ("-ERR %d new context failed.\r\n", -r);
  else
    printf ("+OK %d\r\n", r);
  fflush (stdout);
  return 0;
}
static int
do_release_context (const char *line)
{
  int c_desc;
  int r;
  char *p;
  c_desc = strtol (line+15, &p, 10);
  r = release_context (c_desc);
  if (r < 0)
    printf ("-ERR %d release context failed.\r\n", -r);
  else
    printf ("+OK\r\n");
  fflush (stdout);
  return 0;
}
static int
do_resize_segment (const char *line)
{
  char *p;
  struct context *c;
  int c_desc, seg_num, inc_dec, r;
  c_desc = strtol (line+15, &p, 10);
  seg_num = strtol (p+1, &p, 10);
  inc_dec= strtol (p+1, &p, 10);
  c = c_desc_to_context (c_desc);
  r = resize_segment (c, seg_num, inc_dec);
  if (r < 0)
    printf ("-ERR %d resize failed.\r\n", -r);
  else {
    int removed, inserted;
    seg_num = r;
    removed = get_number_of_segments_removed (c, seg_num);
    inserted = get_number_of_segments_inserted (c, seg_num);
    output_segments (c, seg_num, removed, inserted);
  }
  fflush (stdout);
  return 0;
}
static int
do_select_candidate (const char *line)
{
  char *p;
  struct context *c;
  int c_desc, seg_num, cand_num, r;
  c_desc = strtol (line+17, &p, 10);
  seg_num = strtol (p+1, &p, 10);
  cand_num = strtol (p+1, &p, 10);
  c = c_desc_to_context (c_desc);
  r = select_candidate (c, seg_num, cand_num);
  if (r < 0)
    printf ("-ERR %d select failed.\r\n", -r);
  else {
    int removed;
    seg_num = r;
    removed = get_number_of_segments_removed (c, seg_num);
    if (removed == 0)
      printf ("+OK\r\n");
    else {
      int inserted = get_number_of_segments_inserted (c, seg_num);
      output_segments (c, seg_num, removed, inserted);
    }
  }
  fflush (stdout);
  return 0;
}
static int
do_quit (const char *line)
{
  (void)line;
  return 1;
}
struct dispatch_table {
  const char *command;
  int size;
  int (*func)(const char *line);
};
static struct dispatch_table dt[] = {
  { "COMMIT", 6, do_commit },
  { "CONVERT", 7, do_convert },
  { "GET-CANDIDATES", 14, do_get_candidates },
  { "NEW-CONTEXT", 11, do_new_context },
  { "QUIT", 4, do_quit },
  { "RELEASE-CONTEXT", 15, do_release_context },
  { "RESIZE-SEGMENT", 14, do_resize_segment },
  { "SELECT-CANDIDATE", 16, do_select_candidate },
};
static int
dt_cmp (const char *line, struct dispatch_table *d)
{
  return strncmp (line, d->command, d->size);
}
#define MAX_LINE 512
static char line[MAX_LINE];
void egg_main (void);
void
egg_main (void)
{
  int done = 0;
  char *s, *p;
  say_hello ();
  while (!done) {
    struct dispatch_table *d;
    s = fgets (line, MAX_LINE, stdin);
    if (s == NULL) {
      fprintf (stderr, "null input\n");
      break;
    }
    if ((p = (char *)memchr(s, '\n', MAX_LINE)) == NULL) {
      fprintf (stderr, "no newline\n");
      break;
    }
    if (p > s && *(p-1) == '\r')
      *(p-1) = '\0';
    else
      *p = '\0';
    d = (struct dispatch_table *)
      bsearch (s, dt, 
	       sizeof (dt) / sizeof (struct dispatch_table), 
	       sizeof (struct dispatch_table), 
	       (int (*)(const void *, const void *))dt_cmp);
    if (d != NULL)
      done = d->func (s);
    else
      say_unknown ();
  }
}
 
     |