sj4/src/sj4mkdic/readline.c

346 lines
6.9 KiB
C
Raw Normal View History

2008-01-11 11:57:08 +00:00
/*
* Copyright (c) 1991-1994 Sony Corporation
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
/*
2026-06-19 01:49:53 +09:00
* $SonyRCSfile: readline.c,v $
* $SonyRevision: 1.1 $
2008-01-11 11:57:08 +00:00
* $SonyDate: 1994/06/03 08:00:44 $
*/
#include "sj_euc.h"
2026-06-20 02:12:04 +09:00
#include "sj4_dict_const.h"
2008-01-11 11:57:08 +00:00
2026-06-20 02:12:04 +09:00
#include "sj4mkdic.h"
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
static int yomi[MAXYOMILENGTH + 1];
static int kanji[MAXKANJILENGTH + 1];
static int hinsi[MAXHINSINUMBER + 1];
static int atr[MAXATRNUMBER + 1];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
#define Isblank(c) ((c) == 0x20 || (c) == 0x09 || (c) == 0xa1a1)
#define Isillegal(c) ((c) < 0x20)
2008-01-11 11:57:08 +00:00
static void
2026-06-19 01:49:53 +09:00
error(char* s) {
if(s)
2008-01-11 11:57:08 +00:00
fprintf(stderr, "%s\n", s);
mark_file(stderr);
exit(1);
}
2026-06-19 04:55:57 +09:00
static const char utf8_bytes[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2026-06-19 18:37:29 +09:00
static int code = 0;
2008-01-11 11:57:08 +00:00
static int
2026-06-19 01:49:53 +09:00
readchar() {
2026-06-19 04:55:57 +09:00
#ifdef UTF8
int c[4];
int i = 0, j;
2026-06-19 18:37:29 +09:00
int n = 0, cod;
if(code == -1) {
code = 0;
return ']';
} else if(code != 0) {
int h = code & 0xf;
char* hex = "0123456789ABCDEF";
code = code >> 4;
if(code == 0) code = -1;
return hex[h];
}
2026-06-19 04:55:57 +09:00
do {
if((c[i++] = getch()) == EOF) return EOF;
} while(i < utf8_bytes[c[0]]);
if(i == 1) {
n = c[0];
} else {
2026-06-19 18:37:29 +09:00
int f = 0xff;
if(i == 2) f = f >> 3;
if(i == 3) f = f >> 4;
if(i == 4) f = f >> 5;
n |= c[0] & f;
2026-06-19 04:55:57 +09:00
for(j = 1; j < i; j++) {
n = n << 6;
n |= c[j] & 63;
}
}
2026-06-19 18:37:29 +09:00
if((cod = unicode_to_eucjp(n)) == -1) {
code = n;
return '[';
}
return cod;
2026-06-19 04:55:57 +09:00
#else
2026-06-19 01:49:53 +09:00
int c1;
int c2;
2008-01-11 11:57:08 +00:00
c1 = getch();
2026-06-19 01:49:53 +09:00
if(c1 == EOF)
2008-01-11 11:57:08 +00:00
return EOF;
2026-06-19 01:49:53 +09:00
if(c1 != SS3) {
if(c1 & 0x80) {
2008-01-11 11:57:08 +00:00
c2 = getch();
2026-06-19 01:49:53 +09:00
if(c2 == EOF)
2008-01-11 11:57:08 +00:00
error(NULL);
c1 = ((c1 & 0xff) << 8) + (c2 & 0xff);
}
} else {
c2 = getch();
2026-06-19 01:49:53 +09:00
if(c2 == EOF)
2008-01-11 11:57:08 +00:00
error(NULL);
c1 = ((c1 & 0xff) << 8) + (c2 & 0xff);
c2 = getch();
2026-06-19 01:49:53 +09:00
if(c2 == EOF)
2008-01-11 11:57:08 +00:00
error(NULL);
c1 = ((c1 & 0xffff) << 8) + (c2 & 0xff);
}
return c1;
2026-06-19 04:55:57 +09:00
#endif
2008-01-11 11:57:08 +00:00
}
static int
2026-06-19 01:49:53 +09:00
skip_blank() {
int c;
2008-01-11 11:57:08 +00:00
c = readchar();
2026-06-19 01:49:53 +09:00
while(Isblank(c))
2008-01-11 11:57:08 +00:00
c = readchar();
return c;
}
static int
2026-06-19 01:49:53 +09:00
readhinsi() {
static int c = 0;
int i;
unsigned char hinsi[128];
2008-01-11 11:57:08 +00:00
retry:
2026-06-19 01:49:53 +09:00
if(c == '\n') {
2008-01-11 11:57:08 +00:00
c = 0;
return 0;
}
c = skip_blank();
2026-06-19 01:49:53 +09:00
if(c == '\n') {
2008-01-11 11:57:08 +00:00
c = 0;
return 0;
}
2026-06-19 01:49:53 +09:00
for(i = 0; !Isblank(c);) {
2026-06-19 04:55:57 +09:00
int j = 0, k;
2026-06-19 01:49:53 +09:00
if(c == '\n' || c == ':')
2008-01-11 11:57:08 +00:00
break;
2026-06-19 01:49:53 +09:00
if(Isillegal(c))
2008-01-11 11:57:08 +00:00
error(ILLEGALFORMAT);
2026-06-19 04:55:57 +09:00
2026-06-19 01:49:53 +09:00
if(c > 0xffffff) {
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 24) & 0xff);
2026-06-19 01:49:53 +09:00
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 16) & 0xff);
2026-06-19 01:49:53 +09:00
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 8) & 0xff);
2026-06-19 01:49:53 +09:00
} else if(c > 0xffff) {
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 16) & 0xff);
2026-06-19 01:49:53 +09:00
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 8) & 0xff);
2026-06-19 01:49:53 +09:00
} else if(c > 0xff) {
if(i >= 126)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = ((c >> 8) & 0xff);
}
2026-06-19 01:49:53 +09:00
if(i >= 127)
2008-01-11 11:57:08 +00:00
error(TOOLONGHINSI);
hinsi[i++] = (c & 0xff);
2026-06-19 18:21:53 +09:00
c = readchar();
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
if(i == 0)
2008-01-11 11:57:08 +00:00
return 0;
2026-06-19 01:49:53 +09:00
if(hinsi[0] == '[' && hinsi[i - 1] == ']') {
2008-01-11 11:57:08 +00:00
hinsi[i - 1] = 0;
2026-06-21 08:00:28 +09:00
i = cnvhinsi(hinsi + 1);
2026-06-19 01:49:53 +09:00
if(i > 0) {
2026-06-19 04:55:57 +09:00
fprintf(stderr,
2026-06-19 18:21:53 +09:00
#ifdef ENGLISH
"Verb \"%s\" has parenthis\n"
2026-06-19 04:55:57 +09:00
#else
"\311\312\273\354 \"%s\" \244\313\263\347\270\314\244\254\244\304\244\244\244\306\244\244\244\353\n"
#endif
,
hinsi +
1);
2026-06-19 01:49:53 +09:00
} else if(!i) {
2026-06-19 04:55:57 +09:00
fprintf(stderr,
2026-06-19 18:21:53 +09:00
#ifdef ENGLISH
"Cannot turn \"%s\" into code\n"
2026-06-19 04:55:57 +09:00
#else
"\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n"
#endif
,
hinsi +
1);
2008-01-11 11:57:08 +00:00
goto retry;
}
2026-06-19 01:49:53 +09:00
} else {
2008-01-11 11:57:08 +00:00
hinsi[i] = 0;
2026-06-19 01:49:53 +09:00
i = cnvhinsi(hinsi);
if(!i) {
2026-06-19 04:55:57 +09:00
fprintf(stderr,
2026-06-19 18:21:53 +09:00
#ifdef ENGLISH
"Cannot turn \"%s\" into code\n"
2026-06-19 04:55:57 +09:00
#else
"\"%s\" \244\254\245\263\241\274\245\311\262\275\244\307\244\255\244\336\244\273\244\363\n"
#endif
,
hinsi);
2008-01-11 11:57:08 +00:00
goto retry;
}
}
return i;
}
2026-06-19 01:49:53 +09:00
int* readline() {
int c;
int i;
int j;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(;;) {
2008-01-11 11:57:08 +00:00
mark_file(NULL);
c = skip_blank();
2026-06-19 01:49:53 +09:00
if(c == '#') {
while(c != '\n')
2008-01-11 11:57:08 +00:00
c = skip_blank();
}
2026-06-19 01:49:53 +09:00
if(c == EOF)
2008-01-11 11:57:08 +00:00
return NULL;
2026-06-19 01:49:53 +09:00
if(c != '\n')
2008-01-11 11:57:08 +00:00
break;
}
2026-06-19 01:49:53 +09:00
for(i = 0; !Isblank(c);) {
if(Isillegal(c))
2008-01-11 11:57:08 +00:00
error(ILLEGALFORMAT);
2026-06-19 01:49:53 +09:00
if(i >= MAXYOMILENGTH)
2008-01-11 11:57:08 +00:00
error(TOOLONGYOMI);
yomi[i++] = c;
2026-06-19 01:49:53 +09:00
c = readchar();
2008-01-11 11:57:08 +00:00
}
yomi[i] = 0;
2026-06-19 01:49:53 +09:00
if(i == 0)
2008-01-11 11:57:08 +00:00
error(NOYOMISTRING);
c = skip_blank();
2026-06-19 01:49:53 +09:00
if(c != '"') {
for(i = 0; !Isblank(c);) {
if(Isillegal(c))
2008-01-11 11:57:08 +00:00
error(ILLEGALFORMAT);
2026-06-19 01:49:53 +09:00
if(i >= MAXKANJILENGTH)
2008-01-11 11:57:08 +00:00
error(TOOLONGKANJI);
kanji[i++] = c;
2026-06-19 01:49:53 +09:00
c = readchar();
2008-01-11 11:57:08 +00:00
}
} else {
c = readchar();
2026-06-19 01:49:53 +09:00
for(i = 0; c != '"';) {
if(Isillegal(c))
2008-01-11 11:57:08 +00:00
error(ILLEGALFORMAT);
2026-06-19 01:49:53 +09:00
if(i >= MAXKANJILENGTH)
2008-01-11 11:57:08 +00:00
error(TOOLONGKANJI);
kanji[i++] = c;
2026-06-19 01:49:53 +09:00
c = readchar();
2008-01-11 11:57:08 +00:00
}
}
kanji[i] = 0;
2026-06-19 01:49:53 +09:00
if(i == 0)
2008-01-11 11:57:08 +00:00
error(NOKANJISTRING);
i = j = 0;
2026-06-19 02:57:26 +09:00
while((c = readhinsi())) {
2026-06-19 01:49:53 +09:00
if(c < 0) {
if(i >= MAXATRNUMBER)
2008-01-11 11:57:08 +00:00
error(TOOMANYATR);
atr[i++] = -c;
2026-06-19 01:49:53 +09:00
} else {
if(j >= MAXHINSINUMBER)
2008-01-11 11:57:08 +00:00
error(TOOMANYHINSI);
hinsi[j++] = c;
}
}
atr[i] = 0;
2026-06-19 01:49:53 +09:00
if(j == 0)
2008-01-11 11:57:08 +00:00
hinsi[j++] = HINSITANKAN;
hinsi[j] = 0;
return yomi;
}
2026-06-19 01:49:53 +09:00
void setline(void (*func)(int*, int*, int, int*)) {
int i;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(i = 0; hinsi[i]; i++)
2008-01-11 11:57:08 +00:00
(*func)(yomi, kanji, hinsi[i], atr);
}