#include #include #include #include #include #ifdef HAVE_GETOPT_H #include #endif #include "debug.h" #include "document.h" #include "mf_cob_expand.h" /* These are only useful in the optarg loop */ #define SET_LAST_INDEX(X) if( d.index_count > 0 ) { X; } else { fprintf( stderr, "No indexes set\n" ); } #define SET_LAST_INDEX_STRING(X) if( d.index_count > 0 )\ {\ d.index_def[ d.index_count - 1 ].X = optarg;\ }else\ {\ fprintf( stderr, "No indexes set\n" );\ } #define SET_LAST_INDEX_INT(X) if( d.index_count > 0 )\ {\ sscanf( optarg, "%i", d.index_def[ d.index_count - 1 ].X );\ }else\ {\ fprintf( stderr, "No indexes set\n" );\ } char *myfgets( char *b, int size, FILE *f, int form_feed_as_terminator ); int usage( const char *name ); void init_index( document *d, indexd *i ); char *myfgets( char *b, int size, FILE *f, int form_feed_as_terminator ) { int cnt; /* Read in up to size chars from f */ bzero( b, size ); for( cnt = 0; cnt < size; cnt++ ) { b[ cnt ] = (char)fgetc( f ); if( b[ cnt ] == 0 ) { /* Get rid of nulls */ b[ cnt ] = ' '; } dmsg( 6, "Read character '%c'", b[cnt] ); if( b[ cnt ] == '\n' ) { dmsg( 4, "Returning, eol" ); return( b ); } if( ( b[ cnt ] == '\f' ) && ( form_feed_as_terminator ) ) { /* Thanks Nievin */ dmsg( 4, "Returning, form feed" ); return( b ); } if( feof( f ) ) { dmsg( 4, "Returning, eof" ); return( b ); } if( b[ cnt ] == '\b' ) { /* Handle backspaces thanks "Nievin, Garrett" */ b[ cnt-- ] = 0; /* Get rid of \b */ if( cnt >= 0 ) /* Can't cut across line boundries.. */ b[ cnt-- ] = 0; /* Get rid of last char */ /* Dec'd so loop will inc */ } } return( b ); } int usage( const char *name ) { printf( "Indexed PDF Creator V." VERSION "\n" "Author: Steve Slaven - http://hoopajoo.net\n" "Usage: %s [-hDIixSobdTLlcfFmatsKk] [inputfile]\n" " -h\n" " This help message\n" " -D LEVEL\n" " Set debug level to LEVEL\n" " -i LINE[:START:STOP[:SUPPRESS:REPARENT]\n" " Index line #LINE starting at char START and stopping at STOP.\n" " See the man page for details about indexing.\n" " -I PAGE:LINE[:START:STOP:SUPPRESS:REPARENT]\n" " Create an absolute index at page PAGE on line LINE from START\n" " to STOP chars. See the man page for details about indexing.\n" " -x REGEX\n" " Use a regex to index lines\n" " -S STRING\n" " Format string for last index defined, include a %%s where\n" " the text slurped in between START:STOP should be inserted,\n" " e.g. 'Index: %%s'\n" " -C START[:STOP]\n" " Start/stop chars for last index defined.\n" " -Q [1|0]\n" " Set the suppress_duplicates flag for last index. 1 is\n" " suppress duplicates, 0 is do not suppress\n" " -q [1|0]\n" " Set the suppress_space flag for last index.\n" " -r NUMBER\n" " Reparent the last index defined. Shortcut instead of doing\n" " all the colons\n" " -R NUMBER\n" " Set the default reparent value for all indexes after this\n" " -o FILENAME\n" " Output filename, if not set STDOUT is used\n" " -b FILENAME\n" " Background image filename, must be a TIFF image\n" " -d DPI\n" " Background image DPI, default is 72\n" " -T NUMBER\n" " Set top margin\n" " -L NUMBER\n" " Set left margin\n" " -l NUMBER\n" " Max number of lines per page\n" " -c NUMBER\n" " Max number of chars per line\n" " -n\n" " Do not treat form feed as a line terminator\n" " -N\n" " Do not strip extended ascii character, needed to use extended\n" " ascii in output, not compatible with mf_cob_expand. Without\n" " this option all non-english characters will be stripped\n" " -m[letter-p|letter-l]\n" " Page mode, letter-p is portrait, letter-l is landscape, 8.5x11\n" " US letter size paper. There are several other sizes, see the\n" " man page for details\n" " -f NUMBER\n" " Font size\n" " -F[Courier]\n" " Font face name\n" " (these are for document meta-info)\n" " -a AUTHOR\n" " Author name\n" " -t TITLE\n" " Document title\n" " -s SUBJECT\n" " Document subject\n" " -K KEY\n" " Set the extra key to KEY. Default is 'Keywords', which\n" " is recognized by Acrobat Reader in the Keywords field.\n" " -k DATA\n" " Data to attach to the extra key\n" "\n" "Default reparent: '%i'\n" "Global config: '%s'\n" , name, DEFAULT_REPARENT, SYSTEM_CONFIG ); #ifdef DEBUG_A_LOO printf( "DEBUG enabled\n" ); #endif #ifdef MF_COB_EXPAND printf( "MF Cobol expansion enabled\n" ); #endif exit( 1 ); } void init_index( document *d, indexd *i ) { i -> page_no = 0; i -> index_type = IDX_RELATIVE; i -> line_no = 1; i -> start_char = 1; i -> stop_char = d -> chars_per_line; i -> suppress_spaces = 1; i -> suppress_duplicates = 1; i -> reparent = d -> default_reparent; i -> format = NULL; } int main( int argc, char *argv[] ) { document d; indexd i; int tmp, tmp2; /* default_reparent = DEFAULT_REPARENT; */ FILE *fin = NULL; char *tmpc, *home; int ffat; /* form feed as term flag */ /* Default to stdin/out. We let good ol' linux clean up these extra chars. */ char *input_filename, line[ 255 ], dash_fname[ 3 ] = "-"; /* Initialize vars */ /* debug_level( 5 ); */ ffat = 1; input_filename = dash_fname; document_init( &d ); /* Global config */ parse_config( &d, SYSTEM_CONFIG ); home = getenv( "HOME" ); if( home ) { tmpc = (char *)malloc( strlen( home ) + 10 ); strcpy( tmpc, home ); strcat( tmpc, "/.ipdfrc" ); parse_config( &d, tmpc ); free( tmpc ); } parse_config( &d, "./ipdf.conf" ); /* Parse opts */ while( ( tmp = getopt( argc, argv, "hnD:i:I:x:S:C:q:Q:r:R:o:b:d:T:L:l:c:f:F:m:s:t:a:k:K:N" ) ) != EOF ) { switch( tmp ) { case 'h': usage( argv[ 0 ] ); break; case 'n': /* Ignore \f as line terminator */ ffat = 0; break; case 'N': /* Don't strip non-printable, for extended ascii chars */ d.strip_non_printable = 0; break; case 'd': /* Image dpi */ sscanf( optarg, "%f", &d.image_dpi ); break; case 'b': /* Image filename */ d.image_filename = optarg; break; case 'D': sscanf( optarg, "%i", &tmp2 ); debug_level( tmp2 ); break; case 'I': init_index( &d, &i ); i.index_type = IDX_ABSOLUTE; sscanf( optarg, "%i:%i:%i:%i:%i:%i", &i.page_no, &i.line_no, &i.start_char, &i.stop_char, &i.suppress_spaces, &i.reparent ); document_add_index( &d, &i ); break; case 'x': init_index( &d, &i ); i.index_type = IDX_REGEX; i.regstring = optarg; document_add_index( &d, &i ); break; case 'i': init_index( &d, &i ); i.index_type = IDX_RELATIVE; sscanf( optarg, "%i:%i:%i:%i:%i", &i.line_no, &i.start_char, &i.stop_char, &i.suppress_spaces, &i.reparent ); document_add_index( &d, &i ); break; case 'C': SET_LAST_INDEX( sscanf( optarg, "%i:%i", &d.index_def[ d.index_count - 1 ].start_char, &d.index_def[ d.index_count - 1 ].stop_char ); ); break; case 'Q': SET_LAST_INDEX( sscanf( optarg, "%i", &d.index_def[ d.index_count - 1 ].suppress_duplicates ); ); break; case 'q': SET_LAST_INDEX( sscanf( optarg, "%i", &d.index_def[ d.index_count - 1 ].suppress_spaces ); ); break; case 'S': SET_LAST_INDEX( d.index_def[ d.index_count - 1 ].format = optarg; ); break; case 'r': SET_LAST_INDEX( sscanf( optarg, "%i", &d.index_def[ d.index_count - 1 ].reparent ); ); break; case 'R': sscanf( optarg, "%i", &d.default_reparent ); break; case 'o': d.output_filename = optarg; break; case 'm': /* page mode */ document_set_page( &d, optarg ); break; case 'l': /* Lines per page */ sscanf( optarg, "%i", &d.lines_per_page ); break; case 'c': /* Chars per line */ sscanf( optarg, "%i", &d.chars_per_line ); break; case 'f': /* Font size */ sscanf( optarg, "%f", &d.font_size ); break; case 'F': /* Font face */ d.font_face = optarg; break; case 'T': /* Set top margin */ sscanf( optarg, "%f", &d.top_margin ); break; case 'L': /* Left margin */ sscanf( optarg, "%f", &d.left_margin ); break; case 'a': /* Author */ d.author = optarg; break; case 't': /* title */ d.title = optarg; break; case 's': /* subj */ d.subject = optarg; break; case 'K': /* extra key */ d.extra_key = optarg; break; case 'k': /* extra value */ d.extra_data = optarg; break; } } if( argc != optind ) { input_filename = argv[ optind ]; } /* Open in */ if( strcmp( input_filename, "-" ) == 0 ) { dmsg( 4, "Reading from STDIN" ); fin = stdin; }else { fin = fopen( input_filename, "rb" ); } if( ! fin ) { perror( input_filename ); return( 0 ); } /* Init lib */ PDF_boot(); /* Create a new doc */ d.input_filename = input_filename; document_start( &d ); /* Reset some infos */ /* PDF_set_info( d.pdf, "Subject", "Document" ); PDF_set_info( d.pdf, "Title", input_filename ); PDF_set_info( d.pdf, "Creator", "ipdf V." VERSION ); PDF_set_info( d.pdf, "Author", "ipdf by Steve Slaven " ); */ /* Read input, make output, drink beer. */ while( ! feof( fin ) ) { bzero( line, 255 ); myfgets( line, 255, fin, ffat ); #ifdef MF_COB_EXPAND /* This handles expansion of an RLE encoding in 'compressed' print files from MF COBOL */ mf_cob_expand( line ); #endif /* fixes extra line printing bug if line length is exactly * the same length as a multiple of your page length, * from Carlo Benna - benna@larasrl.com */ if( ! feof( fin ) ) document_addline( &d, line ); } document_end( &d ); PDF_shutdown(); return( 0 ); }