1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "namazu.h"
6 
7 /*
8  * for pageindex
9  */
put_query(uchar * qs,int w)10 void put_query(uchar * qs, int w)
11 {
12     while (*qs) {
13 	if (!strncmp(qs, "whence=", 7)) {
14 	    printf("whence=%d", w);
15 	    for (qs += 7; isdigit(*qs); qs++);
16 	} else
17 	    fputc(*(qs++), stdout);
18     }
19 }
20 
21 
22 /* displayin page index */
put_page_index(int n)23 void put_page_index(int n)
24 {
25     int i;
26 
27     if (HtmlOutput && ModeTknamazu)
28 	;
29     else if (HtmlOutput)
30 	printf("<STRONG>Page:</STRONG> ");
31     else
32 	printf("Page: ");
33     for (i = 0; i < PAGE_MAX; i++) {
34 	if (i * HListMax >= n)
35 	    break;
36 	if (HtmlOutput) {
37 	    if (i * HListMax != HListWhence) {
38 		printf("<A HREF=\"");
39 		fputs(ScriptName, stdout);
40 		fputc('?', stdout);
41 		put_query(QueryString, i * HListMax);
42 		printf("\">");
43 	    } else {
44 		printf("<STRONG>");
45 	    }
46 	}
47     if (ModeTknamazu)
48 	printf("Page: %d", i + 1);
49 	else
50 	printf("[%d]", i + 1);
51 	if (HtmlOutput) {
52 	    if (i * HListMax != HListWhence) {
53 		printf("</A> ");
54 	    } else
55 		printf("</STRONG> ");
56 	}
57 	if (AllList)
58 	    break;
59     }
60 }
61 
62 /* output current range */
put_current_extent(int listmax)63 void put_current_extent(int listmax)
64 {
65     if (HtmlOutput)
66 	printf("<STRONG>Current List: %d", HListWhence + 1);
67     else
68 	printf("Current List: %d", HListWhence + 1);
69 
70     printf(" - ");
71     if (!AllList && ((HListWhence + HListMax) < listmax))
72 	printf("%d", HListWhence + HListMax);
73     else
74 	printf("%d", listmax);
75     if (HtmlOutput)
76 	printf("</STRONG><BR>\n");
77     else
78 	fputc('\n', stdout);
79 }
80 
fputs_with_codeconv(uchar * s,FILE * fp)81 void fputs_with_codeconv(uchar * s, FILE *fp)
82 {
83     uchar buf[BUFSIZE];
84 
85     strcpy(buf, s);
86 #if	defined(WIN32) || defined(OS2)
87     if (is_lang_ja(Lang)) { /* Lang == ja*/
88         euctosjis(buf);
89     }
90 #endif
91     fputs(buf, fp);
92 }
93 
94 /* output string without HTML elements
95  * if Win32 or OS/2, output in Shift_JIS encoding */
fputs_without_html_tag(uchar * s,FILE * fp)96 void fputs_without_html_tag(uchar * s, FILE *fp)
97 {
98     int f, i;
99     uchar buf[BUFSIZE];
100 
101     for (f = 0, i = 0; *s && i < BUFSIZE; s++) {
102 	if (!strncmp(s, "<BR>", 4)) {
103 	    buf[i++] = '\n';
104 	    s += 3;
105 	    continue;
106 	}
107 	if (*s == '<') {
108 	    f = 1;
109 	    continue;
110 	}
111 	if (*s == '>') {
112 	    f = 0;
113 	    continue;
114 	}
115 	if (!f) {
116 	    if (!strncmp(s, "&lt;", 4)) {
117 		buf[i++] = '<';
118 		s += 3;
119 	    } else if (!strncmp(s, "&gt;", 4)) {
120 		buf[i++] = '>';
121 		s += 3;
122 	    } else if (!strncmp(s, "&amp;", 5)) {
123 		buf[i++] = '&';
124 		s += 4;
125 	    } else {
126 		buf[i++] = *s;
127 	    }
128 	}
129     }
130     buf[i] = '\0';
131     fputs_with_codeconv(buf, fp);
132 }
133 
134 
euctojisput(uchar * s,FILE * fp,int mode)135 void euctojisput(uchar *s, FILE *fp, int mode)
136 {
137     int c, c2, state = 0, non_ja;
138 
139     if (! is_lang_ja(Lang)) { /* Lang != ja */
140         non_ja = 1;
141     } else {
142         non_ja = 0;
143     }
144     if (!(c = (int) *(s++)))
145 	return;
146     while (1) {
147 	if (non_ja || c < 0x80) {
148 	    if (state) {
149 		set1byte();
150 		state = 0;
151 	    }
152 	    if (mode) {
153 		/* < > & " are converted to entities like &lt; */
154 		if (c == '<')
155 		    printf("&lt;");
156 		else if (c == '>')
157 		    printf("&gt;");
158 		else if (c == '&')
159 		    printf("&amp;");
160 		else if (c == '"')
161 		    printf("&quot;");
162 		else
163 		    fputc(c, fp);
164 	    } else
165 		fputc(c, fp);
166 	} else if (iseuc(c)) {
167 	    if (!(c2 = (int) *(s++))) {
168 		fputc(c, fp);
169 		return;
170 	    }
171 	    if (!state) {
172 		set2byte();
173 		state = 1;
174 	    }
175 	    if (iseuc(c2)) {
176 		fputc(c & 0x7f, fp);
177 		fputc(c2 & 0x7f, fp);
178 	    } else {
179 		fputc(c, fp);
180 		set1byte();
181 		state = 0;
182 		fputc(c2, fp);
183 	    }
184 	} else {
185 	    if (state) {
186 		set1byte();
187 		state = 0;
188 	    }
189 	    fputc(c, fp);
190 	}
191 	if (!(c = (int) *(s++))) {
192 	    if (state)
193 		set1byte();
194 	    return;
195 	}
196     }
197 }
198 
199 /* fputs Namazu version, it works with considereation of mode */
fputx(uchar * str,FILE * fp)200 void fputx(uchar *str, FILE *fp)
201 {
202     uchar buf[BUFSIZE];
203     int is_html = 0;
204 
205     if ((int)*str == (int)'\t') {
206         is_html = 1;
207     }
208 
209     strcpy(buf, str + is_html);
210     if (HtmlOutput) {
211         /* if string is Namazu's HTML message, it will be printed as is,
212            if not, it will be printed with entity conversion */
213         euctojisput(buf, fp, ! is_html);
214     } else {
215         /* if string is Namazu's HTML message, it will be printed without
216            HTML tag, if not, it will be printed as is */
217         if (is_html) {
218             fputs_without_html_tag(buf, fp);
219         } else {
220             fputs_with_codeconv(buf, fp);
221         }
222     }
223 }
224