2 Copyright (C) 1994-2007 Frans Slothouber, Jacco van Weert, Petteri Kettunen,
3 Bernd Koesling, Thomas Aglassinger, Anthon Pang, Stefan Kost, David Druffner,
4 Sasha Vasko, Kai Hofmann, Thierry Pierron, Friedrich Haase, and Gergely Budai.
6 This file is part of ROBODoc
8 ROBODoc is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #if defined(__MINGW32__) || defined( RB_MSVC )
38 /* Set of dummy functions just to make it compile. */
39 char *RB_TROFF_Get_Default_Extension(
42 return ".3"; /* Section 3 of manual */
45 void RB_TROFF_Generate_Doc_End(
53 FILE *RB_TROFF_Generate_Header_Start(
55 struct RB_header *cur_header )
62 void RB_TROFF_Generate_Header_End(
64 struct RB_header *cur_header )
71 void RB_TROFF_Generate_Char(
79 void RB_TROFF_Generate_False_Link(
88 void RB_TROFF_Generate_Item_Name(
98 void RB_TROFF_Start_New_Line(
104 int RB_TROFF_Generate_Extra(
106 enum ItemType item_type,
115 void RB_TROFF_Set_Param(
123 void TROFF_Generate_Begin_List_Item(
129 void TROFF_Generate_End_List_Item(
135 void TROFF_Generate_Begin_Preformatted(
141 void TROFF_Generate_End_Preformatted(
147 void TROFF_Generate_End_Paragraph(
153 void TROFF_Generate_Begin_Paragraph(
161 #include <sys/param.h>
163 #include "troff_generator.h"
168 #include "generator.h"
172 static int skip_space = 1;
173 static int in_list = 0;
174 static int preformat = 0;
175 static int skippre = 0;
176 static int end_of_line = 0;
177 static int end_of_para = 0;
179 static char *compress_cmd = NULL;
180 static char *compress_ext = NULL;
181 static char *man_section = NULL;
183 /****h* ROBODoc/TROFF_Generator
185 * Generator for TROFF output.
191 char *RB_TROFF_Get_Default_Extension(
194 return ".3"; /* Section 3 of manual */
197 /****f* TROFF_Generator/RB_TROFF_Generate_Doc_End
199 * RB_TROFF_Generate_Doc_End --
203 void RB_TROFF_Generate_Doc_End(
210 static FILE *open_output_file(
217 malloc( strlen( out ) + 10 + strlen( compress_cmd ) );
219 sprintf( cmd, "%s > %s", compress_cmd, out );
221 file = popen( cmd, "w" );
226 return fopen( out, "w" );
229 static void write_comment(
239 fputs( ".\\\" ", out );
251 fputs( "\\\\", out );
254 start = 1; /* no break; */
264 static char *basename(
267 char *base = strrchr( str, '/' );
269 return base ? base + 1 : str;
272 /****f* TROFF_Generator/RB_TROFF_Generate_Header_Start
274 * RB_TROFF_Generate_Header_Start
278 FILE *RB_TROFF_Generate_Header_Start(
280 struct RB_header *cur_header )
282 static char *manpage = NULL;
284 if ( manpage == NULL )
285 manpage = malloc( MAXPATHLEN );
287 if ( cur_header->name )
290 RB_Get_FullDocname( cur_header->owner->filename );
291 char *name = basename( cur_header->name );
292 char *path = basename( file );
296 memcpy( manpage, file, len );
297 sprintf( manpage + len, "%s.%s%s", name, man_section, compress_ext );
299 dest_doc = open_output_file( manpage );
300 if ( dest_doc == NULL )
302 RB_Panic( "Cannot open %s: %s\n", manpage, strerror( errno ) );
305 RB_Say( "+ Generating man page \"%s\"\n", SAY_INFO, manpage );
307 /* Check for aliases */
308 for ( i = 1; i < cur_header->no_names; i++ )
310 char *base = basename( cur_header->names[i] );
311 char *buf = strchr( manpage, 0 ) + 1;
313 memcpy( buf, file, len );
314 sprintf( buf + len, "%s.%s%s", base, man_section, compress_ext );
316 symlink( basename( manpage ), buf );
317 RB_Say( "+ Linked with \"%s\"\n", SAY_INFO, buf );
320 /* Append document type and title */
321 fprintf( dest_doc, ".\\\" Source: %s\n",
322 cur_header->owner->filename->fullname );
324 write_comment( dest_doc, COMMENT_ROBODOC COMMENT_COPYRIGHT );
326 fprintf( dest_doc, ".TH %s %s \"", name, man_section );
329 char timeBuffer[255];
332 strftime( timeBuffer, sizeof timeBuffer, "%b %d, %Y",
334 fputs( timeBuffer, dest_doc );
336 if ( name > cur_header->name )
338 path = cur_header->name;
339 len = name - path - 1;
342 len = strlen( path = name );
343 if ( len >= sizeof timeBuffer )
344 len = sizeof timeBuffer - 1;
345 memcpy( timeBuffer, path, len );
347 fprintf( dest_doc, "\" %s \"%s Reference\"\n", timeBuffer,
355 /****f* TROFF_Generator/RB_TROFF_Generate_Header_End
357 * RB_TROFF_Generate_Header_End --
360 void RB_TROFF_Generate_Header_End(
362 struct RB_header *cur_header )
368 void RB_TROFF_Generate_Char(
372 if ( skip_space && utf8_isspace( c ) )
375 if ( in_list == 1 && utf8_isspace( c ) )
377 fprintf( dest_doc, "\n\\-\\ " );
394 fputc( '\\', dest_doc );
395 fputc( c, dest_doc );
398 fputc( c, dest_doc );
403 void RB_TROFF_Generate_False_Link(
407 fprintf( out, "\\fB%s\\fR", name );
409 fprintf( out, " \\- " );
413 void RB_TROFF_Generate_Item_Name(
420 fprintf( out, "\n.fi" );
422 fprintf( out, "\n.SH %s\n", name );
423 if ( ( preformat = pre ) )
425 fprintf( out, ".nf\n" );
433 void RB_TROFF_Start_New_Line(
436 if ( end_of_para && in_list )
438 fprintf( out, "\n.PP" );
446 int RB_TROFF_Generate_Extra(
448 enum ItemType item_type,
451 if ( end_of_line == 0 )
456 int len = strlen( str );
460 for ( skippre = 0; utf8_isspace( *str ); skippre++, str++ );
465 len = skippre > len ? len : skippre;
467 return len > 0 ? len - 1 : -1;
475 /* Start of a line */
476 while ( utf8_isspace( *p ) )
479 if ( strchr( "-o*", *p ) == NULL )
481 while ( !utf8_isspace( *p ) && *p )
484 while ( utf8_isspace( *p ) )
491 if ( !utf8_isspace( p[1] ) )
495 fprintf( out, ".TP\n.I %s\n\\-\\ ", skip );
502 if ( !utf8_isspace( p[1] ) )
504 fprintf( out, ".IP -\n" );
509 return skip > str ? skip - str - 1 : -1;
514 void RB_TROFF_Set_Param(
518 if ( compress == NULL || *compress == 0
519 || strcasecmp( compress, "none" ) == 0 )
524 else if ( strcasecmp( compress, "gzip" ) == 0 )
526 compress_cmd = "gzip -c";
527 compress_ext = ".gz";
529 else if ( strcasecmp( compress, "bzip2" ) == 0 )
531 compress_cmd = "bzip2 -c";
532 compress_ext = ".bz2";
536 RB_Panic( "Unknown compression compress \"%s\"\n", compress );
538 if ( section == NULL || *section == 0 )
541 man_section = section;
544 void TROFF_Generate_Begin_List_Item(
547 /* fprintf( out, ".IP -\n" ); */
550 void TROFF_Generate_End_List_Item(
553 /* fprintf( out, ".PP\n" ); */
556 void TROFF_Generate_Begin_Preformatted(
559 /* fprintf( out, ".nf\n" ); */
562 void TROFF_Generate_End_Preformatted(
565 /* fprintf( out, ".fi\n" ); */
568 void TROFF_Generate_End_Paragraph(
571 /* fputc( '\n', out ); */
574 void TROFF_Generate_Begin_Paragraph(
577 /* fprintf( out, ".PP\n" ); */