1 /*
2  * Copyright (C) 1991,1992,1993 NEC Corporation.
3  */
4 #ifndef lint
5 static char rcsid[]=
6 	"$Id: readfile.c,v 2.12 1994/04/19 10:17:00 uchida Exp $ (NEC)";
7 #endif
8 
9 #include <stdio.h>
10 #include "plain2.h"
11 #include "kanji.h"
12 
13 char *
strsave(str)14 strsave(str)
15 char *str;
16 {
17 	char *save;
18 	save = (char *)malloc(strlen(str) + 1);
19 	if (save == NULL) {
20 		fprintf(stderr, "PANIC(malloc in strsave)\n");
21 		exit (2);
22 	}
23 	return strcpy(save, str);
24 }
25 
26 /*
27  * expand tab and Zenkaku space
28  *	also convert JIS kanji code into EUC code
29  */
30 #if	INTERNAL_CODE == CODE_EUC
31 void
expand(expanded,str)32 expand(expanded, str)
33 register char	*expanded;
34 register char	*str;
35 {
36 	int	colPos = 0;
37 	int	secondByte = 0;
38 	int	jisInKanji = 0;
39 
40 #ifdef	KANJI
41 	if (inputCode == CODE_SJIS) {
42 		static	char	cvtBuf[MAX_LINE_LEN];
43 		str = sftj2eucStr(str, cvtBuf);
44 	}
45 #endif
46 	for (; *str; str++) {
47 		/* for Jis Kanji Code		 */
48 		if (*str == ESC) {
49 			if (*(str+1) == '$'
50 			    && (*(str+2) == '@' || *(str+2) == 'B')) {
51 				jisInKanji = 1;
52 				str+=2;
53 				continue;
54 			}
55 			if (*(str+1) == '('
56 			    && (*(str+2) == 'J' || *(str+2) == 'B')) {
57 				jisInKanji = 0;
58 				str+=2;
59 				continue;
60 			}
61 		}
62 		if (jisInKanji && secondByte == 0){
63 			*str |= 0x80;
64 			*(str+1) |= 0x80;
65 		}
66 #ifdef	KANJI
67 		/* end for Jis Kanji Code	 */
68 		if (secondByte == 0
69 		    && isZenkakuSpc(str)) {
70 			/* Zenkaku Space	*/
71 			*expanded++ = ' ';
72 			*expanded++ = ' ';
73 			colPos+=2;
74 			str++;
75 		}
76 		else {
77 #endif
78 			if (*str == '\t')
79 				do {
80 					*expanded++ = ' ';
81 					colPos++;
82 				} while (colPos % 8 != 0);
83 			else {
84 				*expanded++ = *str;
85 				colPos++;
86 			}
87 			if (*str & 0x80) {
88 				secondByte = secondByte?0:1;
89 			}
90 #ifdef	KANJI
91 		}
92 #endif
93 	}
94 	if (secondByte) {
95 		/* input line split in 2-Byte Kanji char	*/
96 		(void)ungetc(*(expanded - 1), inFile);
97 		*(--expanded) = '\0';
98 	}
99 	else
100 		*expanded = '\0';
101 	while (*(--expanded) == ' ')
102 		*expanded = '\0';
103 }
104 #else
105 #if	INTERNAL_CODE == CODE_SJIS
106 void
expand(expanded,str)107 expand(expanded, str)
108 register char	*expanded;
109 register char	*str;
110 {
111     int	colPos = 0;
112     for (; *str; str++) {
113 #ifdef	KANJI
114 	    if( isZenkakuSpc(str)) {
115 		    /* Zenkaku Space	*/
116 		    *expanded++ = ' ';
117 		    *expanded++ = ' ';
118 		    colPos+=2;
119 		    str++;
120 	    }
121 	    else if( isZenkaku(str)) {
122 		    *expanded++ = *str++;
123 		    *expanded++ = *str;
124 		    colPos+=2;
125 	    }
126 	    else {
127 #endif
128 		    if (*str == '\t')
129 			    do {
130 				    *expanded++ = ' ';
131 				    colPos++;
132 			    } while (colPos % 8 != 0);
133 		    else {
134 			    *expanded++ = *str;
135 			    colPos++;
136 		    }
137 #ifdef	KANJI
138 	    }
139 #endif
140     }
141     *expanded = '\0';
142     while(
143 #ifdef	KANJI
144 	  !isZenkaku( expanded-sizeof(char)) &&
145 #endif
146 	  *(expanded) == ' ' )
147 	    *(expanded--) = '\0';
148 }
149 #else
150 unknown code;
151 #endif
152 #endif
153 storeLine(textp, str)
154 struct	text	*textp;
155 char	*str;
156 {
157 	static char	expanded[MAX_LINE_LEN];
158 	int	i, spaces, nonspace, indent;
159 	short	japCode;
160 	expand(expanded, str);
161 	textp->body   = str = strsave(expanded);
162 	indent   = 0;
163 	japCode  = 0;
164 	nonspace = 0;
165 	spaces   = 0;
166 	for (i=0; *str; str++, i++) {
167 		if (nonspace == 0 && *str != ' ') {
168 			indent   = i;
169 			nonspace =1;
170 		}
171 		if (*str & 0x80)
172 			japCode = 1;
173 		else if (nonspace && *str == ' ')
174 			spaces++;
175 	}
176 	textp->japanese = japCode;
177 	textp->indent   = indent;
178 	japaneseText   |=japCode;
179 	if (nonspace) {
180 		textp->length = i;
181 		textp->spaces = spaces;
182 	}
183 	else {
184 		textp->blank  = 1;
185 		textp->length = 0;
186 		textp->spaces = 0;
187 	}
188 }
189 /*
190  * Read and store file
191  */
readAndStoreFile()192 readAndStoreFile()
193 {
194 	struct	text	*textp;
195 	struct	text	Top;
196 	char	buf[MAX_LINE_LEN];
197 	int	i, len;
198 	japaneseText =   0;
199 	textp = &Top;
200 	textLines = 1;
201 	while (fgets(buf, MAX_LINE_LEN, inFile) != NULL) {
202 		textp->next   = (struct text *)malloc(sizeof(struct text));
203 		if (textp->next == NULL){
204 		malloc_error:
205 			fprintf(stderr, "PANIC(malloc in readAndStore)\n");
206 			exit (2);
207 		}
208 		textp  = textp->next;
209 		bzero((char *)textp, sizeof(struct text));
210 		textp->next   = NULL;
211 
212 		if (buf[len=strlen(buf)-1] == '\n')
213 			buf[len] = '\0';
214 		storeLine(textp, buf);
215 		textLines++;
216 	}
217 	textTop  = Top.next;
218 	/*
219 	 * Build texts[] array for later access
220 	 *	texts[0] : not exist
221 	 *	texts[1] - texts[textLines - 1] : text lines
222 	 *	texts[textLines] : terminator (null)
223 	 */
224 	texts = (struct text **)
225 		malloc(sizeof(struct text *) * (textLines + 1));
226 	if(texts == NULL) goto malloc_error; /* Add Nide */
227 	for (textp = textTop, i=1; i < textLines; textp = textp->next, i++)
228 		texts[i] = textp;
229 	texts[textLines] = (struct text *)malloc(sizeof(struct text));
230 	if(texts[textLines] == NULL) goto malloc_error; /* Add Nide */
231 	bzero((char *)texts[textLines], sizeof(struct text));
232 }
233