1 /*
2  * Copyright (C) 1991,1992 NEC Corporation.
3  */
4 #ifndef lint
5 static char rcsid[] =
6 	"$Id: renumout.c,v 2.11 1994/04/19 10:17:01 uchida Exp $ (NEC)";
7 #endif
8 
9 #include <stdio.h>
10 #include "plain2.h"
11 
12 /*
13  * Renumber list and section numbers
14  */
15 #define	bodyStr(textp)	((textp)->blank\
16 			 ?(textp)->body\
17 			 :(textp)->body+(textp)->indent)
18 
19 #ifdef	KANJI
20 char	*list_sroman[] = {"��", "�ɣ�", "�ɣɣ�", "�ɣ�",
21 				  "��","�֣�","�֣ɣ�","�֣ɣɣ�","�ɣ�","��"};
22 char	*list_lroman[] = {"��", "���", "�����", "���",
23 				  "��","����","�����","������","i��","��"};
24 #else
25 char	*list_sroman[] = {"I", "II", "III", "IV",
26 				  "V","VI","VII","VIII","IX","X"};
27 char	*list_lroman[] = {"i", "ii", "iiii", "iv",
28 				  "v","vi","vii","viii","ix","x"};
29 #endif
30 char	*list_salph[] = {"a","b","c","d","e","f","g","h","i","j","k",
31 				 "l","m","n","o","p","q","r","s","t","u",
32 				 "v","w","x","y","z"};
33 char	*list_lalph[] = {"A","B","C","D","E","F","G","H","I","J","K",
34 				 "L","M","N","O","P","Q","R","S","T","U",
35 				 "V","W","X","Y","Z"};
36 #define	LMAX_SROMAN	(sizeof(list_sroman)/sizeof(char *))
37 #define	LMAX_LROMAN	(sizeof(list_lroman)/sizeof(char *))
38 #define	LMAX_SALPHA	(sizeof(list_salph)/sizeof(char *))
39 #define	LMAX_LALPHA	(sizeof(list_lalph)/sizeof(char *))
40 
41 static	listLevel = 0;
42 
renumPrint(begin,end)43 renumPrint(begin, end)
44 int	begin;
45 int	end;
46 {
47 	int	l;
48 	for (l = begin; l < end; l++)
49 		printf ("%s\n", texts[l]->body);
50 }
51 
52 char	*
listNumStr(lbp,num)53 listNumStr(lbp, num)
54 struct textBlock *lbp;
55 {
56 	static char	buf[32];
57 	char	*numstr, *listFormat;
58 	switch (texts[lbp->rbegin]->listType) {
59 	    case L_BULLET:
60 	    case L_DASH:
61 	    case L_DLIST:
62 		break;
63 	    case L_SROMAN:
64 		numstr = list_sroman[num> LMAX_SROMAN? LMAX_SROMAN:num];
65 		goto moreStyle;
66 	    case L_LROMAN:
67 		numstr = list_lroman[num> LMAX_LROMAN? LMAX_LROMAN:num];
68 		goto moreStyle;
69 	    case L_LALPHA:
70 		numstr = list_lalph[num> LMAX_LALPHA? LMAX_LALPHA:num];
71 		goto moreStyle;
72 	    case L_SALPHA:
73 		numstr = list_salph[num> LMAX_SALPHA? LMAX_SALPHA:num];
74 		goto moreStyle;
75 	    case L_NUMBER:
76 		{
77 			static char numb[8];
78 			sprintf(numb, "%d", num+1);
79 			numstr = numb;
80 			goto moreStyle;
81 		}
82 	    moreStyle:
83 		switch (texts[lbp->rbegin]->listHint) {
84 		    case LH_PAREN:
85 			listFormat = "(%s)";
86 			break;
87 		    case LH_RPAREN:
88 			listFormat = "%s)";
89 			break;
90 		    case LH_BRACKET:
91 			listFormat = "[%s]";
92 			break;
93 		    case LH_RBRACKET:
94 			listFormat = "%s]";
95 			break;
96 		    case LH_DOTTED:
97 			listFormat = "%s.";
98 			break;
99 		    default:
100 			listFormat = "%s";
101 			break;
102 		}
103 		break;
104 	}
105 	sprintf(buf, listFormat, numstr);
106 	return buf;
107 }
108 renumList(lbp)
109 struct textBlock *lbp;
110 {
111 	struct	textBlock *lhbp;
112 	int	lnum, rend;
113 	int	list_num = 0;
114 	listLevel++;
115 
116 	for (lhbp = lbp->nextBlock; lhbp != NULL; lhbp = lhbp->nextBlock) {
117 		lnum = lhbp->rbegin;
118 		/* renum first block (list head block)	*/
119 		if (texts[lnum]->listType == L_DLIST
120 		    || texts[lnum]->listType == L_DASH
121 		    || texts[lnum]->listType == L_BULLET) {
122 			printf ("%s\n", texts[lnum]->body);
123 		}
124 		else {
125 			int i;
126 			for (i=0; i<texts[lnum]->indent; i++)
127 				putchar(' ');
128 			printf ("%s %s\n", listNumStr(lhbp, list_num++),
129 				listSecBody(texts[lnum]));
130 		}
131 		PRINTED2(lnum,  lhbp->rend);
132 		renumPrint(lnum + 1, lhbp->rend);
133 		/* renum rest of the list body	*/
134 		if (lhbp->nextBlock != NULL) {
135 			rend = lhbp->nextBlock->rbegin;
136 		}
137 		else
138 			rend = lbp->rend;
139 		renumRegion(lhbp->rend, rend);
140 	}
141 	listLevel--;
142 }
143 #define	SEC_MAX_DEPTH	8
144 static int	lastDepth = SEC_MAX_DEPTH;
145 static int	snum[SEC_MAX_DEPTH] = {0,0,0,0,0,0,0,0,};
146 initSnum(textp)
147 struct	text	*textp;
148 {
149 	char	*snumStr;
150 	int	i, num;
151 	snumStr = textp->body + textp->indent;
152 	for (i = 1; i <= textp->secDepth; i++) {
153 		snumStr = strNum(snumStr, &num);
154 		if (num != 1
155 		    || textp->secDepth != 1) {
156 			if (i == textp->secDepth)
157 				num--;
158 			snum[i - 1] = num;
159 		}
160 	}
161 }
renumSection(depth,body)162 renumSection(depth, body)
163 int	depth;
164 char	*body;
165 {
166 	int	i;
167 	if (depth >= SEC_MAX_DEPTH)
168 		depth = SEC_MAX_DEPTH - 1;
169 
170 	for (i = lastDepth - 1; i >= depth; i--)
171 		snum[i] = 0;
172 
173 	snum[depth - 1]++;
174 	for (i = 0; i < depth; i++)
175 		printf ("%d.", snum[i]);
176 	printf (" %s\n", body);
177 }
renumFtitle(ft,indent,str)178 renumFtitle(ft, indent, str)
179 int	ft;
180 int	indent;
181 char	*str;
182 {
183 	static int	figNumber = 1;
184 	static int	tblNumber = 1;
185 	int	i;
186 	for (i = 0; i < indent; i++)
187 		putchar(' ');
188 #ifdef	KANJI
189 	if (japaneseText) {
190 		if (ft == FT_FIGURE)
191 			printf ("%s %d.", codeCvt("��"), figNumber++);
192 		else if (ft == FT_TABLE)
193 			printf ("%s %d.", codeCvt("ɽ"), tblNumber++);
194 	}
195 	else {
196 #endif
197 		if (ft == FT_FIGURE)
198 			printf ("Figure %d.}", figNumber++);
199 		else if (ft == FT_TABLE)
200 			printf ("Table %d.}", tblNumber++);
201 #ifdef	KANJI
202 	}
203 #endif
204 	printf (" %s\n", str);
205 }
206 
207 renumCapsule(blockTbp)
208 struct	textBlock	*blockTbp;
209 {
210 	struct	textBlock	*tbp1, *tbp2;
211 	int	l;
212 
213 	tbp1 = blockTbp->nextBlock;
214 	tbp2 = tbp1->nextBlock;
215 	renumBlock(tbp1);
216 	for (l = tbp1->rend; l < tbp2->rbegin; l++) {
217 		if (texts[l]->block == NULL && texts[l]->blank)
218 			PRINTED(l);
219 	}
220 	renumBlock(tbp2);
221 }
222 renumBlock(tbp)
223 struct	textBlock *tbp;
224 {
225 	int	l;
226 	l = tbp->rbegin;
227 	switch (tbp->type) {
228 	    case TB_SECNUM:
229 		renumSection(texts[l]->secDepth, listSecBody(texts[l]));
230 		PRINTED(l);
231 		break;
232 	    case TB_FTITLE:
233 		renumFtitle(texts[l]->fTitle,
234 			    texts[l]->indent,
235 			    texts[l]->body + texts[l]->indent
236 			    + texts[l]->headLen);
237 		PRINTED(l);
238 		break;
239 	    case TB_PAGE:
240 	    case TB_APPENDIX:
241 		printf ("%s\n", texts[l]->body);
242 		PRINTED(l);
243 		break;
244 	    case TB_COMMENT:
245 	    case TB_SPACE:
246 	    case TB_RIGHT:
247 	    case TB_CENTER:
248 	    case TB_QUOTE:
249 	    case TB_EXAMPLE:
250 	    case TB_PLAIN:
251 	    case TB_TABLE:
252 	    case TB_PICTURE:
253 	    case TB_RAW:
254 		renumPrint(l, tbp->rend);
255 		PRINTED2(l, tbp->rend);
256 		break;
257 	    case TB_LISTHD:
258 		printf("ERROR(List Renum%d)%s\n", l, texts[l]->body);
259 		break;
260 	    case TB_LIST:
261 		renumList(tbp);
262 		break;
263 	    case TB_CAPSULE:
264 		renumCapsule(tbp);
265 		break;
266 		break;
267 	    default:
268 		fprintf(stderr, "PANIC(unknown in renum)\n");
269 		exit(2);
270 	}
271 }
renumRegion(begin,end)272 renumRegion(begin, end)
273 int	begin;
274 int	end;
275 {
276 	int	l;
277 	struct	textBlock	*tbp;
278 	l = begin;
279 	if (begin >= end)
280 		return;
281 	while (l < end) {
282 		tbp = texts[l]->block;
283 		if (tbp) {
284 			renumBlock(tbp);
285 			l = tbp->rend;
286 		}
287 		else {
288 			if (!texts[l]->blank)
289 				printf("undef%d:%s\n", l, texts[l]->body);
290 			else {
291 				printf ("\n");
292 				PRINTED(l);
293 			}
294 			l++;
295 		}
296 	}
297 }
renumDocument()298 renumDocument()
299 {
300 	int	l;
301 	for (l = 1; l < textBegin; l++)
302 		printf ("%s\n", texts[l]->body);
303 
304 	for (l = textBegin; l < textLines; ) {
305 		if (texts[l]->block) {
306 			if (texts[l]->block->type == TB_SECNUM) {
307 				initSnum(texts[l]);
308 				break;
309 			}
310 			l = texts[l]->block->rend;
311 		}
312 		else
313 			l++;
314 	}
315 	renumRegion(textBegin, textLines);
316 }
317