File: formatman.sci

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (949 lines) | stat: -rw-r--r-- 23,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
function formatman(path,to,dtd)
[lhs,rhs]=argn(0)
if rhs<1 then path='./',end
if rhs<2 then to='ascii',end
if rhs<3 then dtd='../../man.dtd',end
select to
case 'ascii'
  ext='.cat'
  whatis='whatis'
  convert=man2ascii
  getwhatis=asciiwhatis
case 'tex'
  ext='.tex'
  whatis='whatis.tex'
  convert=man2tex
  getwhatis=texwhatis
case 'html'
  ext='.htm'
  whatis='index.'+ext
  convert=man2html
  getwhatis=htmlwhatis
case 'xml'
  recurse = %f;
  ext='.xml'
  whatis='index.xml'
  convert=man2xml
  getwhatis="xmlwhatis"
else
   error('Only ascii, tex, html or xml output format supported')
end

path=stripblanks(path)
if MSDOS then
  if part(path,length(path))<>'\' then
     path=path+'\';
  end
else
  if part(path,length(path))<>'/' then
     path=path+'/';
  end
end

path1=path
if part(path,1:4)=='SCI/' then path=SCI+part(path,4:length(path)),end
if part(path,1:2)=='~/' then path=getenv('HOME')+part(path,2:length(path)),end

if MSDOS then
  if part(path,1:4)=='SCI\' then path=SCI+part(path,4:length(path)),end
  path=strsubst(path,'/','\')
  lst=unix_g('dir /B  '""+path+'* ""')
  lst=lst($:-1:1)
else
  lst=unix_g('ls  -t1 '+path+'*.*') 
end
// lookfor .man files
man=[];
for k=1:size(lst,'*')
  ke=strindex(lst(k),'.man')
  if ke<>[] then 
    if ke($)==length(lst(k))-3 then
      man=[man;k];
    end
  end
end
modified=%f
for k1=1:size(man,'*')  // loop on .man files
  k=man(k1);
  if MSDOS then
    fl=path+lst(k)
    fnam=lst(k)
  else
    fl=lst(k)
    ks=strindex(fl,'/')
    if ks==[] then fnam=fl;else fnam=part(fl,ks($)+1:length(fl));end
  end
  cat=find(strsubst(lst(k),'.man',ext)==lst)
  if cat==[]| cat>k then
    modified=%t
    write(%io(2),'Processing '+fl+' to '+to)
    mputl(convert(getman(fl,to)),strsubst(fl,'.man',ext))
  end
end

if to<>"xml" then
  if modified then //create whatis file
    WH=[]
    for k1=1:size(man,'*')  // loop on .man files
      k=man(k1);
      if MSDOS then
	fl=path+lst(k)
	fnam=lst(k)
      else
	fl=lst(k)
	ks=strindex(fl,'/')
	if ks==[] then fnam=fl;else fnam=part(fl,ks($)+1:length(fl));end
      end
      WH=[WH;getwhatis(fl,strsubst(fnam,'.man',''))]
    end
    mputl(gsort(WH,'g','i'),path+whatis)
  end
end
endfunction

function txt=man2ascii(man,ind)
if argn(2)<2 then ind=1,end
ll=75
txt=[];k=0
n=size(man)
item=[]
while k<n
  k=k+1;mk=man(k)
  select mk(1)
  case 'TH' then
    n1=ll-length(mk(2))*2-length(mk(4))-length(mk(5))
    txt=[txt;
	mk(2)+part(' ',ones(1,n1/3))+mk(4)+part(' ',ones(1,n1/3))+..
               mk(5)+part(' ',ones(1,n1-2*int(n1/3)))+mk(2)]
  case 'SH' then
    txt=[txt;convstr(mk(2),'u')]   
    ind=ind(1)
  case 'begin_indent' then
    ind($+1)=mk(2)
  case 'end_indent' then
    if ind<>[] then ind($)=[],end
  case 'item' then
    item=mk(2)
  case 'latex' then
  case 'latex_ignore' then
    txt=[txt;man2ascii(mk(2),ind)]
  case 'table' then
    txt=[txt;maketable(mk(2),ind)]
  case 'fill' then
    txt=[txt;justify_text(item,mk(2),ind,mk(3))]
    item=[]
  case 'verbatim' then
    txt=[txt;part(' ',ones(1,sum(ind)))+mk(2)]
  case 'font' then
  else
  end
end
endfunction

function txt=man2tex(man)
txt=[];k=0
n=size(man)
while k<n
  k=k+1;mk=man(k)
  select mk(1)
  case 'TH' then
    txt=[txt;
	'\phead{'+texsubstitute(mk(2))+'}{1}{'+texsubstitute(mk(3))+..
	    '}{'+texsubstitute(mk(4))+'}{'+texsubstitute(mk(5))+'}']
  case 'SH' then
    t=convstr(mk(2),'u')
    if t=='NAME' then
      k=k+1
      t=man(k)(2)
      p=strindex(t,' -');nm=texsubstitute(stripblanks(part(t,1:p-1)))
      txt=[txt;
	  '\Sdoc{'+nm+'}{'+part(t,p:length(t))+'}\index{'+nm+'}\label{'+nm+'}'
	  '\begin{flushleft}'
          '\end{flushleft}']
    elseif t=='SEE ALSO' then
      txt=[txt;
	  '\Seealso{SEE ALSO}']
      k=k+1
      t=strcat(man(k)(2),' ')
      p=[0 strindex(t,',') length(t)+1]
      nm=[]
      for i=2:size(p,2)
	nm=[nm,part(t,p(i-1)+1:p(i)-1)]
      end
      nm=stripblanks(nm)
      txt=[txt;
	  strcat('{\verb?'+nm+'?}'+' \pageref{'+nm+'}',',')]
    else
      txt=[txt;'\Shead{'+texsubstitute(t)+'}']
    end
  case 'begin_indent' then
    txt=[txt;'\begin{scitem}']
  case 'end_indent' then
    txt=[txt;'\end{scitem}% end Env']
  case 'item' then
    txt=[txt;'\item[{\tt '+mk(2)(1)+'}]']
  case 'latex' then
    txt=[txt;mk(2)]
  case 'latex_ignore' then

  case 'table' then
    txt=[txt;table2tex(mk(2))]
  case 'fill' then
    mk(2)=texsubstitute(mk(2))
    txt=[txt;mk(2)]
  case 'verbatim' then
    txt=[txt;
	'\begin{verbatim}'
	mk(2)
	'\end{verbatim}']
  case 'font' then
  else
  end
end
endfunction

function txt=man2html(man)
txt=[];k=0
n=size(man)
bl='&nbsp'
bl10=strcat(bl(ones(1,10)))+' '
while k<n
  k=k+1;mk=man(k)
  select mk(1)
  case 'TH' then
    txt=[txt;
	'<body>'
	mk(2)+bl10+mk(3)+bl10+mk(4)+bl10+mk(5)]
  case 'SH' then
    mk(2)=htmlsubstitute(mk(2))
    t=convstr(mk(2),'u')
    txt=[txt;
	  '<h1>'
	  t+'</h1>']
    if t=='SEE ALSO' then
      k=k+1
      if size(man(k))>=2
	t=strcat(man(k)(2),' ')
	p=[0 strindex(t,',') length(t)+1]
	nm=[]
	for i=2:size(p,2)
	  nm=[nm,part(t,p(i-1)+1:p(i)-1)]
	end
	nm=stripblanks(nm)
	txt=[txt;
	    '<br>'+strcat('<a href=""'+gethtmlref(nm)+'"">'+nm+'</a>',', ')]
      end
    end

  case 'begin_indent' then
    txt=[txt;'<ul>']
  case 'end_indent' then
    txt=[txt;'</ul>']
  case 'item' then
    txt=[txt;
	  '';
	  '<li>'
	  +'<b>'+mk(2)(1)+'</b></li>']
  case 'latex' then
  case 'latex_ignore' then
    txt=[txt;man2html(mk(2))]
  case 'table' then
    txt=[txt;table2html(mk(2))]
  case 'fill' then
    mk(2)=htmlsubstitute(mk(2))
    txt=[txt;
	strcat(mk(2)(1:$),' ')+'<br>']
  case 'verbatim' then
    mk(2)=htmlsubstitute(mk(2))
    txt=[txt;
	'<br><TT>'+strsubst(mk(2),' ','&nbsp;')+'</TT>'
	'<br>']
  case 'font' then
  else
  end
end
endfunction

function txt=man2xml(man)
k=0;txt=[];lang="eng";
n=size(man)
bl=' '
bl10=strcat(bl(ones(1,10)))+' '
while k<n
  k=k+1;mk=man(k)
  select mk(1)
  case 'TH' then
	txt=["<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""no""?> ";"<!DOCTYPE MAN SYSTEM """+dtd+""">" ;"<MAN>"];    
	txt=[txt;
	"  <LANGUAGE>eng</LANGUAGE>"  ;
	"  <TITLE>" + mk(2) + "  </TITLE>" ; 
        "  <TYPE>"+xmlsubstitute(mk(5))+"  </TYPE>" ; 
        "  <DATE>"+ xmlsubstitute(mk(3))+"  </DATE>"]
  case 'SH' then
    mk(2)=xmlsubstitute(mk(2))
    t=convstr(mk(2),'u');
    select(t)
    case 'NAME' then
        k=k+1;
	mk = man(k);
	if mk(1) == 'fill' then
	  mk2=mk(2)
	  for i=1:size(mk2,"r") 
	    idtmp = strindex(mk2(i),' - ');
	    txt=[txt; "  <SHORT_DESCRIPTION name=""" + xmlsubstitute(part(mk2(i),1:idtmp-1)) + """> " + xmlsubstitute(part(mk2(i),idtmp+3:length(mk2(i)))) + "  </SHORT_DESCRIPTION>"]
	    
	  end
	end
      case 'NOM' then
	lang="fr"
        k=k+1;
	mk = man(k);
	if mk(1) == 'fill' then
	  mk2=mk(2)
	  for i=1:size(mk2,"r") 
	    idtmp = strindex(mk2(i),' - ');
	    txt=[txt; "  <SHORT_DESCRIPTION name=""" + xmlsubstitute(part(mk2(i),1:idtmp-1)) + """> " + xmlsubstitute(part(mk2(i),idtmp+3:length(mk2(i)))) + "  </SHORT_DESCRIPTION>"]
	  
	  end
	end    
      case 'CALLING SEQUENCE' then
	k=k+1;
        mk = man(k);
	if mk(1) == 'verbatim' then
 	txt=[txt; "  <CALLING_SEQUENCE>" ; "  <CALLING_SEQUENCE_ITEM>" + xmlsubstitute(mk(2)) + "  </CALLING_SEQUENCE_ITEM>";
                  "  </CALLING_SEQUENCE>"];
	    end
      case 'SQUENCE D&APOS;APPEL' then
	lang="fr"
	k=k+1;
	mk = man(k);
	if mk(1) == 'verbatim' then
	  txt=[txt; "  <CALLING_SEQUENCE>" ; "  <CALLING_SEQUENCE_ITEM>" + xmlsubstitute(mk(2)) + "  </CALLING_SEQUENCE_ITEM>";
                  "  </CALLING_SEQUENCE>"];
	    end
	case 'DESCRIPTION' then
	  k=k+1;
	  mk = man(k);
	  txt=[txt; '  <DESCRIPTION>'];
	  while ((mk(1)~='SH') & (k<=n))
	    mk = man(k);
	    select(mk(1))
	  case 'fill' then
	    tt=mk(2)
	    txt=[txt;"  <P>"];
	    for i=1:size(tt,"*")
	      if length(tt(i))==0 then
		txt=[txt;"  </P>"];
		txt=[txt;"  <P>"];
	      else
		txt=[txt; "    " + strcat(xmlsubstitute(tt(i))," ")];
	      end
	    end
	    txt=[txt; "  </P>"];
	  case 'begin_indent' then
	    txt=[txt;' '];
	  case 'end_indent' then
	  txt=[txt;' '];
	case 'item' then
    		txt=[txt;'';'  <DESCRIPTION_ITEM  label='''+ xmlsubstituteforlabel(mk(2)(1))+'''> '];
        	if (k+1 < n) then
			k=k+1;
	        	mk = man(k);
			if mk(1) == 'fill' then
 	  		  txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
			else
        	  	  k=k-1;
			end
        	end
    		txt=[txt;'  </DESCRIPTION_ITEM>'];
  	    case 'table' then
    		txt=[txt;table2xml(xmlsubstitute(mk(2)))];
  	    case 'latex' then
  	    case 'latex_ignore' then
		recurse=%t;
    		txt=[txt;man2xml(mk(2))];
		recurse=%f
  	    case 'verbatim' then
    		mk(2)=xmlsubstituteforcdata(mk(2));
    		//txt=[txt;'  <P><VERB>' + strsubst( xmlsubstitute(mk(2)) ,' ',' ')+ '  </VERB>'+'  </P>'];
		txt=[txt;'  <VERBATIM><![CDATA[' ; strsubst(mk(2),' ',' ') ; '   ]]></VERBATIM>'];
  	    case 'font' then
  	    else
  	  end
	  k=k+1;
          if (k<n) then 
	    mk = man(k);
	  end;
	end;//while
 	k=k-1;
	mk = man(k);
 	txt=[txt; "  </DESCRIPTION>"];
    case 'PARAMETERS' then
        k=k+1;
        mk = man(k);
	txt=[txt; '  <PARAM>'];
	while  ((mk(1)~='SH') & (k<=n))
          mk = man(k);
	  select(mk(1))
	    case 'fill' then
		txt=[txt;"  <P>"];
		txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
		txt=[txt; "  </P>"];
	    case 'begin_indent' then
		txt=[txt;' <PARAM_INDENT>'];
	    case 'end_indent' then
		txt=[txt;' </PARAM_INDENT>'];
	    case 'item' then
    		txt=[txt;'  <PARAM_ITEM>';
                             '  <PARAM_NAME>' + xmlsubstitute(mk(2)(1))+ '  </PARAM_NAME>' ; 
                             '  <PARAM_DESCRIPTION>'];
        	if (k+1 < n) then
			k=k+1;
	        	mk = man(k);
			select(mk(1))
			  case 'fill' then
			    txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
			  else
        	  	    k=k-1;
			  end
        	end
    		txt=[txt;'  </PARAM_DESCRIPTION> ' ; 
                            '  </PARAM_ITEM>'];
  	    case 'table' then
    		txt=[txt;xmlsubstituteforlabel(table2xml(mk(2)))];
  	    case 'latex' then
  	    case 'latex_ignore' then
		recurse=%t;
    		txt=[txt;man2xml(mk(2))];
		recurse=%f
  	    case 'verbatim' then
    		mk(2)=xmlsubstituteforcdata(mk(2))
		txt=[txt;'  <VERBATIM><![CDATA[' ; strsubst(mk(2),' ',' ') ; '   ]]></VERBATIM>'];
  	    case 'font' then
  	    else
  	  end   
	  k=k+1;
          if (k<n) then 
	    mk = man(k);
	  end;
	end;//while
	k=k-1;
	mk = man(k);
 	txt=[txt; "  </PARAM>"];
    case 'PARAMTRES' then
        k=k+1;lang="fr"
        mk = man(k);
	txt=[txt; '  <PARAM>'];
	while  ((mk(1)~='SH') & (k<=n))
          mk = man(k);
	  select(mk(1))
	    case 'fill' then
		txt=[txt;"  <P>"];
		txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
		txt=[txt; "  </P>"];
	    case 'begin_indent' then
		txt=[txt;' <PARAM_INDENT>'];
	    case 'end_indent' then
		txt=[txt;' </PARAM_INDENT>'];
	    case 'item' then
    		txt=[txt;'  <PARAM_ITEM>';
                             '  <PARAM_NAME>' + xmlsubstitute(mk(2)(1))+ '  </PARAM_NAME>' ; 
                             '  <PARAM_DESCRIPTION>'];
        	if (k+1 < n) then
			k=k+1;
	        	mk = man(k);

			//if mk(1) == 'fill' then
 	  		  //txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
			//else
        	  	  //k=k-1;
			//end
			select(mk(1))
			  case 'fill' then
			    txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
			  else
        	  	    k=k-1;
			  end
        	end
    		txt=[txt;'  </PARAM_DESCRIPTION> ' ; 
                            '  </PARAM_ITEM>'];
  	    case 'table' then
    		txt=[txt;xmlsubstituteforlabel(table2xml(mk(2)))];
  	    case 'latex' then
  	    case 'latex_ignore' then
		recurse=%t;
    		txt=[txt;man2xml(mk(2))];
		recurse=%f
  	    case 'verbatim' then
    		mk(2)=xmlsubstituteforcdata(mk(2))
    		//txt=[txt;'  <P><VERB>'+strsubst(mk(2),' ',' ')+'  </VERB>'+'  </P>']
		txt=[txt;'  <VERBATIM><![CDATA[' ; strsubst(mk(2),' ',' ') ; '   ]]></VERBATIM>'];
  	    case 'font' then
  	    else
  	  end   
	  k=k+1;
          if (k<n) then 
	    mk = man(k);
	  end;
	end;//while
	k=k-1;
	mk = man(k);
      txt=[txt; "  </PARAM>"];    
    case 'EXAMPLE' then
        k=k+1;
        mk = man(k);
	if mk(1) == 'verbatim' then
 	  //txt=[txt ; "  <EXAMPLE><![CDATA[" + strcat(" " + mk(2)," ") + " ]]></EXAMPLE>"];
 	  txt=[txt ; "  <EXAMPLE><![CDATA[" ];
	  for ii=1:size(mk(2),"*")
	    txt=[txt; mk(2)(ii)];
	  end
 	  txt=[txt ; " ]]></EXAMPLE>"];
        end
    case 'EXEMPLE' then
        k=k+1;lang="fr"
        mk = man(k);
	if mk(1) == 'verbatim' then
 	  //txt=[txt ; "  <EXAMPLE><![CDATA[" + strcat(" " + mk(2)," ") + " ]]></EXAMPLE>"];
 	  txt=[txt ; "  <EXAMPLE><![CDATA[" ];
	  for ii=1:size(mk(2),"*")
	    txt=[txt; mk(2)(ii)];
	  end
 	  txt=[txt ; " ]]></EXAMPLE>"];
  end    
  case 'AUTHOR' then 
        k=k+1;
        mk = man(k);
	if mk(1) == 'fill' then
 	txt=[txt; "  <AUTHOR>"+strcat(xmlsubstitute(mk(2)),"; ")+ "  </AUTHOR>"];
         end
    case 'AUTEUR' then 
        k=k+1;lang="fr"
        mk = man(k);
	if mk(1) == 'fill' then
 	txt=[txt; "  <AUTHOR>"+strcat(xmlsubstitute(mk(2)),"; ")+ "  </AUTHOR>"];
      end    
    case 'SEE ALSO' then
      k=k+1
      if size(man(k))>=2
	t=strcat(man(k)(2),' ')
	p=[0 strindex(t,',') length(t)+1]
	nm=[]
	for i=2:size(p,2)
	  nm=[nm,part(t,p(i-1)+1:p(i)-1)]
	end
	nm=stripblanks(nm)
	txt=[txt; "  <SEE_ALSO>" ; strcat('    <SEE_ALSO_ITEM> <LINK>' + nm + '</LINK> </SEE_ALSO_ITEM>',' ') ; "  </SEE_ALSO>"]     
      end
    case 'VOIR AUSSI' then
      k=k+1;lang="fr"
      if size(man(k))>=2
	t=strcat(man(k)(2),' ')
	p=[0 strindex(t,',') length(t)+1]
	nm=[]
	for i=2:size(p,2)
	  nm=[nm,part(t,p(i-1)+1:p(i)-1)]
	end
	nm=stripblanks(nm)
		txt=[txt; "  <SEE_ALSO>" ; strcat('    <SEE_ALSO_ITEM> <LINK>' + nm + '</LINK> </SEE_ALSO_ITEM>',' ') ; "  </SEE_ALSO>"]
      end    
    else //s'il existe un autre .SH nom reconnu
	txt=[txt; '  <ITEMIZE label=''' + man(k)(2) + '''>'];
	k=k+1;
        mk = man(k);	while ((mk(1)~='SH') & (k<=n))
          mk = man(k);
	  select(mk(1))
	    case 'fill' then
		txt=[txt;"  <P>"];
		txt=[txt; "    " + strcat(xmlsubstitute(mk(2))," ")];
		txt=[txt; "  </P>"];
	    case 'begin_indent' then
	    case 'end_indent' then
	    case 'item' then
    		txt=[txt;'  <ITEM label=''' + xmlsubstitute(mk(2)(1)) + '''>'];
        	if (k+1 < n) then
			k=k+1;
	        	mk = man(k);
			if mk(1) == 'fill' then
 	  		  txt=[txt;strcat(xmlsubstitute(mk(2))," ")];
			else
        	  	  k=k-1;
			end
        	end
    		txt=[txt;'';'  </ITEM>'];
  	    case 'table' then
    		txt=[txt;table2xml(xmlsubstituteforlabel(mk(2)))];
  	    case 'latex' then
  	    case 'latex_ignore' then
		recurse=%t;
    		txt=[txt;man2xml(mk(2))];
		recurse=%f
	      case 'verbatim' then
		mk(2)=xmlsubstituteforcdata(mk(2))
    		//txt=[txt;'  <P><VERB>'+strsubst(xmlsubstitute(mk(2)),' ',' ')+'  </VERB>'+'  </P>'];
		txt=[txt;'  <VERBATIM><![CDATA[' ; strsubst(mk(2),' ',' ') ; '   ]]></VERBATIM>'];
  	    case 'font' then
  	    else
  	  end
	  k=k+1;
          if (k<n) then 
	    mk = man(k);
	  end;
	end;//while
 	k=k-1;
	mk = man(k);
 	txt=[txt; "  </ITEMIZE>"];    end//.SH
  case 'fill' then
	txt=[txt; strcat("  <P>" + xmlsubstitute(mk(2)) +"  </P>"," ")];
  case 'table' then
    	txt=[txt;table2xml(xmlsubstitute(mk(2)))]
  else
  end
end
if ~recurse then
  txt(4)="  <LANGUAGE>"+lang+"</LANGUAGE>"
  txt=[txt;"</MAN>"]
end
endfunction

function man=getman(path,to)
//given a man file, this function analyse it and returns a data structure 
ind_def=4
man=list()
txt=mgetl(path);
if txt==[] then return,end
txt=strsubst(txt,code2str(-40)," ")
txt($+1)='.LP '
//
debugflag=%f
com=find(part(txt,1)=='.');//les index des commandes
k=0
begin=%t;tp=0
fill=%t;table=%f;output=%f;k1=1
while k<size(com,'*')
  k=k+1
  tk=txt(com(k))
  OP=part(tk,1:4);//disp(OP)
  select OP
  case '.TH ' then
    th=stripblanks(part(tk,5:length(tk)))
    p=strindex(th,' ')
    nm=strsubst(part(th,1:p(1)-1),'""','')
    th=part(th,p(1)+1:length(th));p=strindex(th,'""')
    if size(p,2)>=2 then dat=part(th,p(1)+1:p(2)-1),else dat='',end
    if size(p,2)>=4 then aut=part(th,p(3)+1:p(4)-1),else aut='',end
    if size(p,2)>=6 then typ=part(th,p(5)+1:p(6)-1),else typ='',end
    man($+1)=list('TH',nm,dat,aut,typ)
    k1=com(k)+1
  case '.nf '
    if com(k)-1>=k1 then
      k2=com(k)-1
      man($+1)=list('fill',replacefonts(txt(k1:k2),to),tp),
    end
    fill=%f
    k1=com(k)+1
  case '.RS '
    output=%t
    begin=%t
  case '.FI '
    man(pos)=list('latex_ignore',list(man(pos:$)))
    for kp=pos+1:size(man),man(kp)=null(),end
    output=%t
  case '.ft '
    man($+1)=list('font')
    output=%t
  case '.fi' then
    if com(k)-1>=k1 then
      k2=com(k)-1
      man($+1)=list('verbatim',strsubst(txt(k1:k2),'\\','\')),
    end
    output=%f
  else
    output=%t
  end

  if output then
    k2=com(k)-1
    if table then //.TS ... .TE
      man($+1)=list('table',gettable(txt(k1:k2),to),)
      table=%f
    elseif fill then
      if k2>=k1 then
	tt=replacefonts(txt(k1:k2),to)
	man($+1)=list('fill',tt,tp),
      end
    else //.nf  .fi
      if k2>=k1 then 
	man($+1)=list('verbatim',strsubst(txt(k1:k2),'\\','\')),
      end
      fill=%t
    end
    output=%f
    k1=k2+2
    select OP
    case '.SH ' then
      if tp>0 then man($+1)=list('end_indent'),tp=tp-1;end
      man($+1)=list('SH',stripblanks(part(tk,5:length(tk)))) 
      begin=%t
      fill=%t
      k1=com(k)+1
    case '.LA ' then 
      if man($)(1)=='latex' then
	man($)(2)($+1)=part(tk,5:length(tk))
      else
	man($+1)=list('latex',part(tk,5:length(tk)))
      end
      k1=com(k)+1
    case '.RE ' then
      man($+1)=list('end_indent')
      tp=tp-1
    case '.IG '
      pos=size(man)+1
    case '.TP '
      if begin then
	if stripblanks(part(tk,5:length(tk)))<>'' then
	  man($+1)=list('begin_indent',evstr(part(tk,5:length(tk))))
	else
	  man($+1)=list('begin_indent',ind_def)
	end
	begin=%f
	k1=com(k)+1
	tp=tp+1
      end
      man($+1)=list('item',stripblanks(txt(k1)))
      k1=k1+1
    case '.IP ' then
      if begin then
	man($+1)=list('begin_indent',ind_def)
	begin=%f
	tp=tp+1
      end
      txt(com(k))=part(txt(com(k)),4:length(txt(com(k))))
      k1=com(k)
    end
    table=OP=='.TS '
  end
end
if tp>0 then man($+1)=list('end_indent'),tp=tp-1,end
endfunction

function tab=gettable(txt,to)
//given troff instruction defining a table this function returns a 
//matrix of string
txt=replacefonts(txt,to)
t1=txt(1)
k=strindex(t1,')')
if part(t1,1:4)<>'tab('|k==[] then error('invalid table definition'),end
del=part(t1,5:k-1) // the column delimiter
t2=txt(2) //alignment def
tab=[]
for k=1:size(txt,1)-2
  tk=txt(k+2)
  c=[0 strindex(tk,del) length(tk)+1]
  for i=2:size(c,'*')
    tab(k,i-1)=part(tk,c(i-1)+1:c(i)-1)
  end
end
endfunction

function wh=asciiwhatis(path,fnam)
txt=mgetl(path)
d=find(part(txt,1:8)=='.SH NAME')
if d==[] then 
  d=find(part(txt,1:7)=='.SH NOM')
  if d==[] then wh=[],return,end
end
d=d(1)+1
f=find(part(txt(d:$),1:4)=='.SH ')
if f==[] then f=$+1,end
wh=txt(d:d+f(1)-2)
k=find(part(wh,1:3)=='.nf'|part(wh,1:3)=='.fi')
if k<>[] then wh(k)=[];end
wh=stripblanks(wh)
k=find(wh=='');if k<>[] then wh(k)=[];end
wh=wh+'  @'+fnam
endfunction

function wh=texwhatis(path,fnam)
wh='\input '+fnam
endfunction

function wh=htmlwhatis(path,fnam)
txt=mgetl(path)
d=find(part(txt,1:8)=='.SH NAME')
if d==[] then wh=[],return,end
d=d(1)+1
f=find(part(txt(d:$),1:4)=='.SH ')
if f==[] then f=$+1,end
wh=txt(d:d+f(1)-2)
k=find(part(wh,1:3)=='.nf'|part(wh,1:3)=='.fi')
if k<>[] then wh(k)=[];end
fnam=fnam+'.htm'
for k=1:size(wh,1)
  whk=stripblanks(wh(k))
  p=strindex(wh(k),' - ');
  if p==[] then
    p=strindex(wh(k),' ')
  end
  p=p(1)

  whk=part(whk,1:p-1)+'</a>'+part(whk,p:length(whk))
  wh(k)='<br><a href=""'+fnam+'"">'+whk+'<br>'
end
endfunction

function txt=replacefonts(txt,to)
if to<>"xml" then
  fonts='\f'+['V','R','B','P','I','(CR']
  for f=fonts,txt=strsubst(txt,f,''),end
end
//suppress inline comments
[l,k]=grep(txt,'\""')
for i=l
  txt(i)=part(txt(i),1:k-1)
end
if to<>"xml" then txt=strsubst(txt,'\\','\'); end
txt=txt
endfunction

function t=maketable(tab,ind)
t=part(' ',ones(1,sum(ind)))+emptystr(size(tab,1),1)+'|'
for k=1:size(tab,2)
  t=t+part(tab(:,k),1:max(length(tab(:,k))))+'|'
end
endfunction

function t=justify_text(item,txt,ind,tp)
curind=sum(ind)
tp=max(0,tp)
t=[]
if txt==[] then return,end
txt=replacefonts(txt,"ascii")
if item<>[]&ind<>[] then
  item=replacefonts(item,"ascii")
  if length(item)>ind($) then
    t=[t;part(' ',ones(1,sum(ind(1:$-1))))+item]
    txt(1)=part(' ',ones(1,sum(ind(1:$-1))))+txt(1)
  else 
    txt=[part(' ',ones(1,sum(ind(1:$-1))))+part(item,1:ind($));txt]
  end
else
  txt(1)=part(' ',ones(1,curind))+txt(1)
end
txt=strcat(txt,' ')+' '

ind=strindex(txt,' ')

k0=1
shift=0
while %t
  k=find(ind<k0+ll-shift);
  if k==[] then break,end
  k=k($)
  t=[t;part(txt,k0:ind(k)-1)]
  k0=ind(k)+1
  ind(1:k)=[]
  shift=curind
end
t=[t;part(txt,k0:length(txt))]
if tp then k1=2,else k1=1,end
t(k1:$)=part(' ',ones(1,curind+1))+t(k1:$)
endfunction

function t=texsubstitute(t)
    t=strsubst(t,'_','\_')
    t=strsubst(t,'%','\%')
    t=strsubst(t,'&','\&')
    t=strsubst(t,'<','$<$')
    t=strsubst(t,'>','$>$')
endfunction

function t=htmlsubstitute(t)
    t=strsubst(t,'<','&lt ')
endfunction

function t=xmlsubstitute(t)
    t=strsubst(t,'&','&amp;');
    t=strsubst(t,'<','&lt;');
    t=strsubst(t,'>','&gt;');
    t=strsubst(t,'''','&apos;');
    t=strsubst(t,'""','&quot;');
    t=strsubst(t,'\fV','<VERB>');
    t=strsubst(t,'\fv','<VERB>');
    t=strsubst(t,'\fB','<VERB>');
    t=strsubst(t,'\fP','<VERB>');
    t=strsubst(t,'\fI','<VERB>');
    t=strsubst(t,'\f(CR',' ');
    t=strsubst(t,'\fR','</VERB>');
endfunction

function t=xmlsubstituteforlabel(t)
    t=strsubst(t,'&','&amp;');
    t=strsubst(t,'<','&lt;');
    t=strsubst(t,'>','&gt;');
    t=strsubst(t,'''','&apos;');
    t=strsubst(t,'""','&quot;');
    t=strsubst(t,'\fV','');
    t=strsubst(t,'\fv','');
    t=strsubst(t,'\fB','');
    t=strsubst(t,'\fP','');
    t=strsubst(t,'\fI','');
    t=strsubst(t,'\f(CR',' ');
    t=strsubst(t,'\fR','');
endfunction

function t=xmlsubstituteforcdata(t)
    t=strsubst(t,'\fV','');
    t=strsubst(t,'\fv','');
    t=strsubst(t,'\fB','');
    t=strsubst(t,'\fP','');
    t=strsubst(t,'\fI','');
    t=strsubst(t,'\f(CR',' ');
    t=strsubst(t,'\fR','');
endfunction

function t=gethtmlref(nm)
global %helps
nm=stripblanks(nm)
if MSDOS then del='\',else del='/',end
t=emptystr(nm)
ok=t==nm;
nh=size(%helps,1)
k=0
while ~and(ok)&k<nh
  k=k+1
  for l=1:size(nm,'*')
    if ~ok(l) then
      //if find(part(mgetl(%helps(k,1)+del+'whatis'),1:length(nm(l)))==nm(l))<>[] then
      if grep(mgetl(%helps(k,1)+del+'whatis.htm'),'>'+nm(l)+'<')<>[] then
	t(l)=%helps(k,1)+del+nm(l)
	ok(l)=%t
      end
    end
  end
end
undef=find(t=='')
t(undef)=nm(undef)
t=t+'.htm'
endfunction

function t=table2html(tab)
t='<table BORDER NOSAVE >'
//t='<table NOSAVE >'
for l=1:size(tab,1)
  t=[t;'<tr>']
  for k=1:size(tab,2),t=[t;'<td>'+tab(l,k)+'</td>'],end
  t=[t;'</tr>']
end
t=[t;'</table>']
endfunction

function t=table2xml(tab)
t='<TABLE>'
for l=1:size(tab,1)
  t=[t;'<TABLE_NEW_ROW>']
  for k=1:size(tab,2),t=[t;'<TABLE_NEW_COLUMN>'+tab(l,k)+'</TABLE_NEW_COLUMN>'],end
  t=[t;'</TABLE_NEW_ROW>']
end
t=[t;'</TABLE>']
endfunction