#ifndef DEFAULT #define DEFAULT "Courier" #endif #ifndef COMMENT #define COMMENT "Helvetica-Oblique" #endif #ifndef PREPROC #define PREPROC "Courier-BoldOblique" #endif #ifndef KEYWORD #define KEYWORD "Courier-Bold" #endif #ifndef TYPE #define TYPE "Courier-Bold" #endif #ifndef STRINGS #define STRINGS "Courier-Oblique" #endif #ifndef LNUMBER #define LNUMBER "Times-Roman" #endif #ifndef FUNCTION #define FUNCTION "Bookman-LightItalic" #endif #ifndef DIM #define DIM 0.75 #endif #ifdef LETTER #define LETTER 1 #else #define LETTER 0 #endif #define VerMsg "\ c2ps version %s\n\ Copyright (C) 1995,1996 Dmitri Shtilman, Dmitri Makarov\n" #define HelpMsg "\ Usage: c2ps [ -?h124abceikmnruvwx ] [ -F default_font ] [ -C comment_font ]\n\ [ -K keyword_font ] [ -N lnumber_font ] [ -P preproc_font ]\n\ [ -S string_font ] [ -T type_font ] [ -U function_font ]\n\ [ -d dim_comments ] [ -f fontsize ] [ -l lines ]\n\ [ -o output_file ] [ -p paper ] [ -s space ]\n\ [ -t tabsize ] [ -_ fun_highlight ] [ file ... ]\n" #include #include #include #include #include #include FILE* out; /* output stream */ struct fun { /* strucure to hold info about fun id definition */ char* name; long unsigned page; long unsigned line; struct fun *next; }; /* FLAGS */ unsigned noc = 0; /* global: disable c highlighting */ unsigned number_lines = 1; /* global: number lines */ unsigned line_cut = 0; /* global: cut long lines */ unsigned ppl = 1; /* global: columns per page */ unsigned lpp = 0; /* global: lines per page */ unsigned letter = LETTER; /* global: use letter paper type */ unsigned frame = 0; /* global: draw frame around each column */ unsigned iso = 1; /* global: support ISO Latin 1 encoding */ unsigned last_modified = 1; /* global: print the time when file was last modified */ unsigned header = 1; /* global: print header */ unsigned adjust_comments = 1; /* global: adjust multiple line comments */ unsigned ext = 1; /* global: */ unsigned c_ext = 0; /* global: highlight files according to extension */ unsigned fun_box = 1; /* global: */ unsigned fun_entry_exit = 1; /* global: print fun id's at entry and exit */ unsigned fun_highlight_id = 2; /* global: fun id highlight: 0-none, 1-box, 2-underline (default) */ #ifdef YY_USE_PROTOS int PString(char *str) #else int PString(str) char *str; #endif { unsigned long s; unsigned char *p = (unsigned char *) str; int i; int t = 0; if ((s = strlen(p)) <= 0) return 1; processed += s; while (*p == '\n') { NextLine(); ++p; } fprintf(out, "("); while (*p) { ++str_length; if (line_cut == 0) if (str_length > 550.*5./(3.*fontsize)) { fprintf(out, ") s\n"); line--; line_split = 1; NextLine(); if(number_lines) { fprintf(out, "%.2f 0 rmoveto\n", 6.*chw); str_length = 6; } fprintf(out,"("); } if (*p == '\n') { fprintf(out, ") s\n"); do NextLine(); while(*++p == '\n'); if (!*p) return 0; fprintf(out, "("); continue; } if (*p == '\f') { fprintf(out, ") s\n"); NextPage(); fprintf(out, "("); p++; continue; } if (*p == '\t') { t = tab - (str_length - 1 - number_lines * 6) % tab; for (i = 0; i < t; i++) fprintf(out, " "); str_length += t - 1; p++; continue; } if (*p < 32 || *p > 127) { fprintf(out, "\\%03o", *p++); continue; } if (*p == '(' || *p == ')' || *p == '\\') fprintf(out, "\\"); fprintf(out, "%c", *p); p++; } fprintf(out, ") s\n"); return 0; } char* PRatio() { int r; int i; r = (10 * processed / b.st_size) + 1; for (i = 1; i <= 10 ; i++) ratio[i] = (i <= r) ? '.' : ' '; ratio[r + 1] = ')'; return ratio; } int PFlush() { if (lc > 0) { buffer[lc] = '\0'; PString(buffer); buffer[lc = 0] = '\0'; } return 0; } #ifdef YY_USE_PROTOS int PFill(char *c) #else int PFill(c) char *c; #endif { if (*c == '\n' && YYSTATE == preps) BEGIN((nesting > 0)? INITIAL : nnested); else if (*c == '{') { if (++nesting > 0 && YYSTATE == nnested) BEGIN(INITIAL); } else if (*c == '}') { if (--nesting <= 0) { if (YYSTATE == INITIAL) BEGIN(nnested); nesting = 0; function_body_expected = 0; if (cur_fun) { free(cur_fun); cur_fun = NULL; } } } buffer[lc++] = *c; if (lc == BUFL - 1) PFlush(); return 0; } int PFunctionBox() { struct fun *f; if (first == NULL) return 1; fprintf(out, "\nmark\n"); for (f = first; f != NULL; f = f->next) fprintf(out, "(%s) (%lu:%lu)\n", f->name, f->page, f->line); fprintf(out, "FunctionBox\n"); return 0; } int NextPage() { x = PAGE - fontsize - spacing; line_on_page = 0; if(fun_box) PFunctionBox(); if(fun_entry_exit && header && page>0 && cur_fun) fprintf(out, "\n(%s) OutFunction\n", cur_fun); switch (page % ppl) { case 0: if (page > 0 ) { fprintf(out, "\n%s showpage\n", (comment)? "currentgray" : ""); } fprintf(out, "\n%%%%Page: %s%lu %lu\n\n", (page == 0 && fp_num > 1) ? "__" : "", page / ppl + 1, total_page / ppl +1); if (page > 0 && comment) fprintf(out, "setgray\n"); if (letter) fprintf(out, "0.93 0.93 scale 31 8 translate\n"); switch(ppl) { case 1: break; case 2: fprintf(out, "595 4 translate 90 rotate 0.7 0.7 scale\n"); break; case 4: fprintf(out, "0.47 0.47 scale 40 880 translate\n"); } break; case 1: fprintf(out, "590 0 translate\n"); break; case 2: fprintf(out, "-590 -830 translate\n"); break; case 3: fprintf(out, "590 0 translate\n"); } fprintf(stderr, "%-25.25s Page %4lu, Column %1lu %s\r", (fp_num > 0) ? fp[fp_cur] : "stdin", page / ppl + 1, page % ppl + 1, (fp_num > 0) ? PRatio() : ""); page++; total_page++; fprintf(out, "initclip "); if (header) { fprintf(out, "(%s) (%s) DrawFrame gsave 0 setgray fhd 523 800 moveto \ (%8lu) s grestore\n", date, (fp_num > 0) ? fp[fp_cur] : "stdin", page); if (fun_entry_exit && page > 0 && cur_fun) fprintf(out, "(%s) InFunction", cur_fun); } /* fprintf(out," ver "); */ fprintf(out, " clp\n"); NextLine(); return 0; } int NextLine() { str_length = 0; if (x > 20 + fontsize) { if (lpp > 0) if(line_on_page++ == lpp) { NextPage(); return 0; } fprintf(out, "%.2f nl ", x); line++; x -= fontsize + spacing; if (number_lines) { if (line_split) line_split=0; else fprintf(out, "sn (%5lu) n\n", line); /* else fprintf(out,"sn (%5d,%d) n\n",YYSTATE,nesting); */ /* there may be any other useful info instead of line numbers such as nesting depth */ str_length = 6; } } else NextPage(); return 0; } int Version() { fprintf(stderr, VerMsg, VERSION); exit(0); } int Help() { fprintf(stderr, HelpMsg); exit(0); } #ifdef YY_USE_PROTOS int PCext(char *fn) #else int PCext(fn) char *fn; #endif { int l = strlen(fn); return ( ((l >= 2) && (!strcmp(&fn[l - 2], ".c") || !strcmp(&fn[l - 2], ".h"))) || ((l >= 3) && (!strcmp(&fn[l - 3], ".cc") || !strcmp(&fn[l - 3], ".hh"))) || ((l >= 4) && (!strcmp(&fn[l - 4], ".cpp") || !strcmp(&fn[l - 4], ".hpp")))); }