1 /* Copyright 1992 NEC Corporation, Tokyo, Japan.
2  *
3  * Permission to use, copy, modify, distribute and sell this software
4  * and its documentation for any purpose is hereby granted without
5  * fee, provided that the above copyright notice appear in all copies
6  * and that both that copyright notice and this permission notice
7  * appear in supporting documentation, and that the name of NEC
8  * Corporation not be used in advertising or publicity pertaining to
9  * distribution of the software without specific, written prior
10  * permission.  NEC Corporation makes no representations about the
11  * suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
16  * NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19  * OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 #ifndef	lint
24 static char rcsid[] = "@(#) 112.1 $Id: splitwd.c,v 1.2.4.2 2003/12/27 17:15:23 aida_s Exp $";
25 #endif
26 
27 #include	<stdio.h>
28 #include	<signal.h>
29 #include	"ccompat.h"
30 
31 #if defined(__STDC__) || defined(SVR4)
32 #include <locale.h>
33 #endif
34 
35 #ifdef SVR4
36 extern char *gettxt();
37 #else
38 #define	gettxt(x,y)  (y)
39 #endif
40 
41 #define		SIZE			4192
42 #define		ISSPACE(c)		('\n' == c || ' ' == c || '\t' == c)
43 
44 #ifndef         AIXV3
45 typedef unsigned char	uchar;
46 #endif
47 
48 
49 struct head{
50     uchar	yomi[SIZE];
51     struct tango	*next;
52 }word;
53 
54 struct tango{
55     uchar	*tsuduri;
56     uchar	*hinshi;
57     struct tango	*next;
58 };
59 
getword(p,Word)60 uchar *getword(p,Word)
61 uchar	*p;
62 uchar	*Word;
63 {
64     while( ISSPACE(*p) )	p++;
65 
66     while( !ISSPACE(*p) && '\0' != *p )	{
67       if (*p == '\\' && *(p + 1)) {
68 	*Word++ = *p++;
69       }
70       *Word++ = *p++;
71     }
72     *Word = '\0';
73 
74     return(p);
75 }
76 
newtango(tsuduri,hinshi)77 struct tango *newtango(tsuduri,hinshi)
78 uchar   *tsuduri;
79 uchar   *hinshi;
80 {
81     struct tango   *tp;
82     uchar   *p;
83 
84     if( !(tp = (struct tango *)malloc(sizeof(struct tango))) )
85 	fprintf(stderr, gettxt("cannacmd:41",
86 	       "cannnot malloc %lu\n"), (unsigned long)sizeof(struct tango) );
87 
88     if( !(p = (uchar *)malloc(strlen((char *)tsuduri) + 1)) )
89 	fprintf(stderr, gettxt("cannacmd:42",
90 	       "cannnot malloc %lu\n"),
91 		(unsigned long)strlen((char *)tsuduri)+1 );
92 
93     tp->tsuduri = p;
94     strcpy((char *)p,(char *)tsuduri);
95 
96     if( !(p = (uchar *)malloc(strlen((char *)hinshi) + 1)) )
97 	fprintf(stderr, gettxt("cannacmd:43",
98 	       "cannnot malloc %lu\n"),
99 		(unsigned long)strlen((char *)hinshi)+1 );
100     tp->hinshi = p;
101     strcpy((char *)p, (char *)hinshi);
102 
103     tp->next = NULL;
104 
105     return(tp);
106 }
107 
savetango(tsuduri,hinshi)108 void savetango(tsuduri, hinshi)
109 uchar	*tsuduri;
110 uchar	*hinshi;
111 {
112     struct tango	*tp;
113 
114     if( !word.next ){
115 	word.next = newtango(tsuduri,hinshi);
116 	return;
117     }
118 
119     tp = word.next;
120     while(tp->next)
121 	tp = tp->next;
122 
123     tp->next = newtango(tsuduri, hinshi);
124 }
125 
save_factor(line,nline)126 void save_factor(line, nline)
127 uchar	*line;
128 int	nline;
129 {
130     uchar	*lp;
131     uchar	hinshi[SIZE];
132     uchar	tsuduri[SIZE];
133 
134     lp = line;
135 
136     lp = getword(lp,word.yomi);			/* head ���ɤߤ������ */
137     lp = getword(lp,hinshi);			/* hinshi ������� */
138     if ('#' != word.yomi[0] && '#' != hinshi[0])
139         fprintf(stderr, gettxt("cannacmd:48", "No hinshi in line %d\n"),
140 		nline);
141 
142     next:
143     while(1){ 		       			/* ɽ���ɤ߹��� loop */
144 	lp = getword(lp,tsuduri);
145 	if( '\0' == tsuduri[0] ) 		/* 1 �Խ���� */
146 	    break;
147 	if( '#' == tsuduri[0] ){ 		/* �ʻ줬�ɤ߹��ޤ줿 */
148 	    strcpy((char *)hinshi, (char *)tsuduri);
149 	    goto next;
150 	}
151 	savetango(tsuduri,hinshi);
152     }
153 }
154 
155 
disp_factor()156 void disp_factor()
157 {
158     struct tango	*tp;
159 
160     tp = word.next;
161     while( tp ){
162 #ifdef USE_ATMARK
163 	if(!strcmp((char *)word.yomi, (char *)tp->tsuduri))
164 	    printf("%s %s @\n", word.yomi, tp->hinshi);
165 	else
166 #endif
167 	    printf("%s %s %s\n", word.yomi, tp->hinshi, tp->tsuduri);
168 	tp = tp->next;
169     }
170 }
171 
172 void
free_factor(tp)173 free_factor(tp)
174 struct tango *tp;
175 {
176   struct tango *ftp;
177 
178   while (tp) {
179     ftp = tp;
180     tp = ftp->next;
181     free((char *)ftp->tsuduri);
182     free((char *)ftp->hinshi);
183     free((char *)ftp);
184   }
185 }
186 
187 void
catch(sig)188 catch(sig)
189 int sig;
190 {
191   fprintf(stderr, gettxt("cannacmd:44", "Dictionary format error.\n"));
192   exit(1);
193 }
194 
195 static void
splitword(fp,name)196 splitword(fp, name)
197 FILE *fp;
198 char *name;
199 {
200   int nline = 0; /* �ɤ߹���Կ�������� */
201   uchar line[SIZE];
202 
203   while (fgets((char *)line, sizeof(line), fp)) {
204 
205     nline++;
206     if ('\n' != line[strlen((char *)line) - 1]) {
207       fprintf(stderr, gettxt("cannacmd:47",
208 			     "%s:Line %d is too long.\n"), name, nline);
209     }
210     else {
211       line[strlen((char *)line) - 1] = '\0';
212     }
213 
214     save_factor(line, nline);
215     disp_factor();
216     free_factor(word.next);
217     word.next = NULL;
218   }
219 }
220 
main(argc,argv)221 main( argc, argv )
222 int	argc;
223 char	*argv[];
224 {
225     FILE	*fp;
226     int		i;
227 
228     signal(SIGSEGV, catch);
229 #ifdef SIGBUS
230     signal(SIGBUS, catch);
231 #endif
232 #if defined(__STDC__) || defined(SVR4)
233     (void)setlocale(LC_ALL,"");
234 #endif
235 
236     if( argc == 1 ) {		/* ���ޥ�ɤ����λ� */
237       splitword(stdin, argv[0]);
238     }
239 
240     for( i = 1; i < argc ; i++ ){
241 	if( !(fp = fopen( argv[i], "r" )) )
242 	    fprintf(stderr, gettxt("cannacmd:46",
243 		   "cannot open file %s\n"), argv[i] );
244 
245 	splitword(fp, argv[0]);
246 
247 	fclose( fp );
248     }
249     exit(0);
250 }
251