home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d174 / titlepage.lha / TitlePage / TitlePage.c < prev    next >
C/C++ Source or Header  |  1989-02-04  |  13KB  |  573 lines

  1. /*
  2.  * titlepage.c : title page printing program. Create a banner page
  3.  *               for listing heading.
  4.  *
  5.  *       by Joel Swank 9-23-88
  6.  *
  7.  * This program is in the public domain, no rights reserved.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <graphics/text.h>
  12. #include <libraries/diskfont.h>
  13. #include <functions.h>
  14. #include <ctype.h>
  15.  
  16. #ifdef NULL
  17. #undef NULL
  18. #endif
  19. #define NULL ((void *)0)
  20.  
  21. /****************************************************************/
  22. /*         Data Area Definitions                                */
  23. /****************************************************************/
  24.  
  25.  
  26. /**** printer escape sequences ****/
  27.  
  28. char eliteon[] = "\033[2w";
  29. char eliteoff[] = "\033[1w";
  30. char condensedon[] = "\033[4w";
  31. char condensedoff[] = "\033[3w";
  32. char lpi8on[] = "\033[0z";
  33. char lpi8off[] = "\033[1z";
  34. char boldon[] = "\033[1m";
  35. char boldoff[] = "\033[22m";
  36. char nlqon[] = "\033[2\"z";
  37. char nlqoff[] = "\033[1\"z";
  38. char doubon[] = "\033[6w";
  39. char douboff[] = "\033[5w";
  40. char italon[] = "\033[3m";
  41. char italoff[] = "\033[23m";
  42.  
  43.  
  44. /****   Buffers and Pointers      ****/
  45.  
  46. unsigned char printbuf[1000];   /* buffer for output line */
  47. unsigned char stringbuf[100];   /* buffer to hold processed output string */
  48. unsigned char *printstring;     /* pointer to string to print */
  49. unsigned char *pbptr;           /* pointer into printbuf  */
  50. unsigned char hchar;            /* hold user specified print character */
  51.  
  52.  
  53. /**** option mode flags ****/
  54.  
  55. int eflag = FALSE;  /* flag for elite printing */
  56. int cflag = FALSE;  /* flag for condensed printing */
  57. int bflag = FALSE;  /* flag for bold printing */
  58. int uflag = FALSE;  /* flag for upper printing */
  59. int wflag = FALSE;  /* flag for double wide printing */
  60. int qflag = FALSE;  /* flag for NLQ printing */
  61. int iflag = FALSE;  /* flag for italics printing */
  62. int flag8 = FALSE;  /* flag for 8lpi printing */
  63. int lflag = FALSE;  /* flag for linesize specified by user */
  64. int hflag = FALSE;  /* flag for print character  specified by user */
  65. int centerflag = FALSE;  /* flag for centering output */
  66.  
  67.  
  68. /**** Things for dealing with the font data structure ****/
  69.  
  70. unsigned short *chardata;    /* pointer to array of character patterns */
  71. unsigned short *charspace;   /* pointer to array of character widths   */
  72.  
  73. struct charDef {       /* from RKM Devices & Libraries page 207  */
  74.     WORD charOffset;   /* bit offset to character in chardata    */
  75.     WORD charBitWidth; /* bit width of character data            */
  76.     };
  77.  
  78. struct charDef *charloc; /* pointer to array of chardefs */
  79. struct TextFont *myfont = NULL; /* pointer to open font  */
  80.  
  81. char fontname[100] = "topaz.font";
  82.  
  83. struct    TextAttr myattr = {
  84.     (STRPTR) fontname,
  85.     8,
  86.     0,
  87.     FPB_DISKFONT};
  88.  
  89.  
  90. /**** Things needed to do time/date ****/
  91.  
  92. long clock, time();   /* place to store time */
  93. char *ctime(), *timebuf;
  94.  
  95. /****  Misc data ****/
  96.  
  97. struct Library *DiskfontBase = NULL;
  98. struct GfxBase *GfxBase = NULL;
  99. FILE *out = NULL;
  100. int maxline;        /* max output line size */
  101. unsigned char *rindex();
  102.  
  103.  
  104. /****************************************************************/
  105. /*         Main program starts here                             */
  106. /****************************************************************/
  107.  
  108. main(argc,argv)
  109. int argc;
  110. char *argv[];
  111. {
  112.     int argindex;
  113.  
  114.     if (argc < 2)
  115.         {
  116.         usage();
  117.         exit(1);
  118.         }
  119.  
  120.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  121.     if (GfxBase == NULL)
  122.         {
  123.         fprintf(stderr,"titlep:cannot open graphics library\n");
  124.         exit(1);
  125.         }
  126.  
  127.     DiskfontBase = (struct Library *)OpenLibrary("diskfont.library", NULL);
  128.     if (DiskfontBase == NULL) {
  129.         fprintf(stderr,"titlep:cannot open font library\n");
  130.         done(22);
  131.         }
  132.  
  133.     /* parse the command line and execute the parameters */
  134.  
  135.     argindex = 0;
  136.  
  137.     while(--argc > 0)
  138.         {
  139.         argindex++;
  140.         do_parm(argv[argindex]);
  141.         }
  142.  
  143.  
  144. done(0);
  145. }
  146.  
  147. /*
  148.  * do_file : process an alternate input file
  149.  */
  150.  
  151. do_file(filename)
  152. char *filename;
  153. {
  154.     FILE *in;
  155.     unsigned char inbuf[100], *inptr, parmbuf[100], *parmptr;
  156.  
  157.     in = fopen(filename,"r");
  158.     if (in == NULL)
  159.         {
  160.         fprintf(stderr,"titlep:cannot open %s\n",filename);
  161.         done(7);
  162.         }
  163.  
  164.     while (fgets(inbuf,100,in))
  165.         {
  166.         unsigned char *t;
  167.         t = rindex(inbuf,'\n');
  168.         if (t) *t = '\0'; /* blasted newline! */
  169.         inptr = inbuf;
  170.         while (*inptr != '\0')    /* do till end of line */
  171.             {
  172.             parmptr = parmbuf;
  173.             /*** parse out a parm ***/
  174.             while (*inptr != ' ' && *inptr != '\t' && *inptr != '\0')
  175.                 {
  176.                 if (*inptr == '\\' && inptr[1] == '\"')
  177.                     {    /* its an escaped quote */
  178.                     *parmptr++ = '\"';
  179.                     inptr += 2;
  180.                     continue;
  181.                     }
  182.                 if (*inptr == '\"')
  183.                     {      /*** handle a quoted string  ***/
  184.                     inptr++;
  185.                     while (*inptr != '\"' && *inptr != '\0')
  186.                         {
  187.                         if (*inptr == '\\' && inptr[1] == '\"')
  188.                             {    /* its an escaped quote */
  189.                             *parmptr++ = '\"';
  190.                             inptr += 2;
  191.                             continue;
  192.                             }
  193.                         *parmptr++ = *inptr++;
  194.                         }
  195.                     if (*inptr == '\"') inptr++;
  196.                     continue;
  197.                     }
  198.                 *parmptr++ = *inptr++;
  199.                 }
  200.             *parmptr = '\0';
  201.             if (parmbuf[0] != '\0') do_parm(parmbuf);
  202.             if (*inptr++ == '\0') break;
  203.             }    /* end while *inptr */
  204.         }    /* end while fgets */
  205.     fclose(in);
  206. }
  207.  
  208.  
  209. /*
  210.  * do_parm : process one parameter
  211.  */
  212.  
  213. do_parm(parm)
  214. unsigned char *parm;
  215. {
  216.     register int i,j;
  217.  
  218.     if (parm[0] == '-')
  219.         {
  220.         switch (parm[1])
  221.             {
  222.             case 'a':    /* alternate Input file */
  223.               do_file(&parm[2]);
  224.               break;
  225.             case 'o':    /* alternate Output file */
  226.               if (out != NULL)
  227.                   {
  228.                 fprintf(stderr,"titlep: Output file already open\n");
  229.                 done(5);
  230.                 }
  231.               if (parm[2] == '\0') out = stdout;
  232.               else open_out(&parm[2]);
  233.               break;
  234.             case 'u':    /* Upper mode ON */
  235.               uflag = TRUE;
  236.               break;
  237.             case 'b':    /* Bold mode ON */
  238.               do_out(boldon);
  239.               bflag = TRUE;
  240.               break;
  241.             case 't':    /* Centering mode ON */
  242.               centerflag = TRUE;
  243.               break;
  244.             case 'q':    /* NLQ mode ON */
  245.               do_out(nlqon);
  246.               qflag = TRUE;
  247.               break;
  248.             case 'w':    /* Double Wide mode ON */
  249.               do_out(doubon);
  250.               wflag = TRUE;
  251.               break;
  252.             case 'e':    /* Elite mode ON */
  253.               do_out(eliteon);
  254.               eflag = TRUE;
  255.               break;
  256.             case 'i':    /* Italics mode ON */
  257.               do_out(italon);
  258.               iflag = TRUE;
  259.               break;
  260.             case 'c':    /* Condensed mode ON */
  261.               do_out(condensedon);
  262.               cflag = TRUE;
  263.               break;
  264.             case '8':    /* 8 LPI mode ON */
  265.               do_out(lpi8on);
  266.               flag8 = TRUE;
  267.               break;
  268.             case 'h':    /* Set the print character */
  269.               hchar = parm[2];
  270.               if (hchar == '\0') hflag = FALSE;
  271.               else hflag = TRUE;
  272.               break;
  273.             case 'l':    /* set maximum line size */
  274.               maxline = atoi(&parm[2]);
  275.               if (maxline <20 || maxline > 999)
  276.                 {
  277.                   fprintf(stderr,"titlep:Bad linesize: %s\n",
  278.                    parm);
  279.                 done(6);
  280.                 }
  281.               lflag = TRUE;
  282.               break;
  283.             case 'f':    /* Specify a font */
  284.               strncpy(fontname,&parm[2],95);
  285.               strcat(fontname,".font");
  286.               break;
  287.             case 's':    /* specify a font size */
  288.               myattr.ta_YSize = (UWORD) atoi(&parm[2]);
  289.               break;
  290.             case 'd':    /* Print date and time */
  291.               clock = time(NULL);
  292.               timebuf = ctime(&clock);
  293.               setmax();
  294.               for (j=0; j < ((maxline-strlen(timebuf))/2); j++) do_out(" ");
  295.               do_out(timebuf);
  296.               break;
  297.             case 'n':    /* Print some newlines */
  298.               i = atoi(&parm[2]);
  299.               if (i == 0) i=1;
  300.               for (j=0; j<i; j++) do_out("\n");
  301.               break;
  302.             case 'p':    /* Print a form feed */
  303.               do_out("\f");
  304.               break;
  305.             default:
  306.               fprintf(stderr,"titlep:Bad flag: %s\n",parm);
  307.               usage();
  308.               done(21);
  309.             }    /* end switch */
  310.     }else if (parm[0] == '+'){
  311.         switch (parm[1])
  312.             {
  313.             case 'u':    /* Upper mode OFF */
  314.               uflag = FALSE;
  315.               break;
  316.             case 'b':    /* Bold mode OFF */
  317.               do_out(boldoff);
  318.               bflag = FALSE;
  319.               break;
  320.             case 't':    /* Centering mode OFF */
  321.               centerflag = FALSE;
  322.               break;
  323.             case 'q':    /* NLQ mode OFF */
  324.               do_out(nlqoff);
  325.               qflag = FALSE;
  326.               break;
  327.             case 'w':    /* Double Wide mode OFF */
  328.               do_out(douboff);
  329.               wflag = FALSE;
  330.               break;
  331.             case 'e':    /* Elite mode OFF */
  332.               do_out(eliteoff);
  333.               eflag = FALSE;
  334.               break;
  335.             case 'i':    /* Italics mode OFF */
  336.               do_out(italoff);
  337.               iflag = FALSE;
  338.               break;
  339.             case 'c':    /* Condensed mode OFF */
  340.               do_out(condensedoff);
  341.               cflag = FALSE;
  342.               break;
  343.             case '8':    /* 8 LPI mode OFF */
  344.               do_out(lpi8off);
  345.               flag8 = FALSE;
  346.               break;
  347.             case 'l':    /* Reset to default linesize */
  348.               lflag = FALSE;
  349.               break;
  350.             case 'h':    /* Reset to default print character */
  351.               hflag = FALSE;
  352.               break;
  353.             default:
  354.               fprintf(stderr,"titlep:Bad flag: %s\n",parm);
  355.               usage();
  356.               done(20);
  357.             }   /* end switch */
  358.     }else {
  359.         /* not a flag, must be a string, so print it */
  360.         printstring = parm;
  361.         do_string();
  362.         }
  363. }
  364.  
  365.  
  366. /*
  367.  * do_string : output one string of data
  368.  */
  369.  
  370. do_string()
  371. {
  372.  
  373.     int i,j;
  374.  
  375.     if ((myfont = (struct TextFont *)OpenDiskFont(&myattr)) == NULL) {
  376.         fprintf(stderr,"titlep:cannot find font: %s\n", myattr.ta_Name);
  377.         done(2);
  378.     }
  379.  
  380.     setmax();
  381.  
  382.     charspace = (unsigned short *) myfont->tf_CharSpace;
  383.     charloc = (struct charDef *) myfont->tf_CharLoc;
  384.  
  385.     if (out == NULL) open_out("PRT:");
  386.  
  387.     proc_str();    /* process string for ~ */
  388.  
  389.     for (i=0; i<myfont->tf_YSize; i++)
  390.         {
  391.         for (j=0; j<maxline; j++) printbuf[j] =' ';
  392.         do_row(i);
  393.         printbuf[maxline] ='\0';
  394.         if (centerflag)
  395.             {
  396.             j = (maxline - strlen(printbuf))/2;
  397.             if (j > 0) while(j-- > 0) aputc(' ',out);
  398.             }
  399.         fputs(printbuf,out);
  400.         aputc('\n',out);
  401.         }
  402.  
  403.     if (myfont   != NULL) CloseFont( myfont );
  404.     myfont = NULL;
  405.  
  406. }
  407.  
  408.  
  409. /*
  410.  * do_row : build one row of printdata.
  411.  */
  412.  
  413. do_row(row)
  414. int row;   /* which row to do */
  415. {
  416.     int i;
  417.     register unsigned short next16;
  418.     unsigned char *pstrptr;
  419.  
  420.     pbptr = printbuf;
  421.     pstrptr = printstring;
  422.     chardata = (unsigned short *) myfont->tf_CharData +
  423.                     (myfont->tf_Modulo * row)/2;
  424.  
  425.     if (iflag) pbptr += (myfont->tf_YSize - 1 - row);
  426.  
  427.     while (*pstrptr != '\0') /* do each charcter  */
  428.         {
  429.         long bitoff, wordoff, bitnum, bitcnt, charoff, bumpcnt;
  430.         unsigned char *cptr;
  431.         charoff = ((unsigned long) *pstrptr) - myfont->tf_LoChar;
  432.         if (uflag) charoff += 128;
  433.         if ((unsigned long) *pstrptr > myfont->tf_HiChar || charoff < 0)
  434.             charoff = myfont->tf_HiChar+1 - myfont->tf_LoChar;
  435.         bitoff = charloc[charoff].charOffset;
  436.         bitcnt = charloc[charoff].charBitWidth;
  437.         bumpcnt = bitcnt;
  438.         wordoff = bitoff/16;
  439.         bitnum = bitoff%16;
  440.  
  441.         cptr = pbptr;
  442.         while (bitcnt > 0)    /* do up to 16 bits */
  443.             {
  444.             /* construct dot info in next16 */
  445.             next16 = chardata[wordoff];
  446.             if (bitnum != 0) next16 = (next16 << bitnum)
  447.                 | (chardata[wordoff+1] >> (16-bitnum));
  448.             for (i=0; i<16; i++)
  449.                 {
  450.                 if (next16 & 0x08000) *cptr = (hflag) ? hchar : *pstrptr;
  451.                 if (*cptr > 128) *cptr -= 128;
  452.                 if (!isprint(*cptr)) *cptr = 'O';
  453.                 cptr++;
  454.                 if (--bitcnt < 1) break;
  455.                 next16 = next16 << 1;
  456.                 }
  457.             wordoff++;
  458.             }
  459.         if (myfont->tf_Flags & FPF_PROPORTIONAL && charspace[charoff] > 0)
  460.             pbptr += charspace[charoff]; /* bump output ptr */
  461.         else pbptr += bumpcnt;
  462.         pstrptr++;                   /* bump input ptr */
  463.         if (pbptr > printbuf + maxline) break;
  464.         }
  465.     if (iflag) pbptr += row;
  466.     *pbptr = '\0';
  467. }
  468.  
  469. /*
  470.  * done - just clean up that which is open, and then leave.
  471.  */
  472. done(how)
  473. int how;
  474.     {
  475.     res_pr();
  476.     if (GfxBase) CloseLibrary(GfxBase) ;
  477.     if (DiskfontBase) CloseLibrary(DiskfontBase) ;
  478.     if (myfont   != NULL) CloseFont( myfont );
  479.     exit(how) ;
  480.     }
  481.  
  482. /*
  483.  * do_out - put stuff to output file, open it if necessary
  484.  */
  485.  
  486. do_out(str)
  487. char *str;
  488. {
  489.     if (out == NULL) open_out("PRT:");
  490.     fputs(str,out);
  491. }
  492.  
  493.  
  494. /*
  495.  * open_out - open the output file
  496.  */
  497.  
  498. open_out(filename)
  499. char *filename;
  500. {
  501.     out = fopen(filename,"w");
  502.     if (out == NULL)
  503.         {
  504.         fprintf(stderr,"titlep:cannot open %s\n",filename);
  505.         done(4);
  506.         }
  507. }
  508.  
  509. /*
  510.  * res_pr - reset printer to default conditions
  511.  */
  512.  
  513. res_pr()
  514. {
  515.     /* restore printer to default condition */
  516.     if (bflag) do_out(boldoff);
  517.     if (eflag) do_out(eliteoff);
  518.     if (cflag) do_out(condensedoff);
  519.     if (flag8) do_out(lpi8off);
  520.     if (wflag) do_out(douboff);
  521.     if (qflag) do_out(nlqoff);
  522.     if (iflag) do_out(italoff);
  523. }
  524.  
  525. /*
  526.  * setmax - set maxline according to print size
  527.  */
  528.  
  529. setmax()
  530. {
  531.     if (!lflag)    /* if user has specified, skippit */
  532.         {
  533.         maxline = 80; /* 80 is default */
  534.         if (eflag) maxline = 96; /* 96 for elite */
  535.         if (cflag) maxline = 132; /* 132 for condensed */
  536.         if (cflag && eflag) maxline = 160; /* 160 for both */
  537.         if (wflag) maxline /= 2;    /* halve it for double wide */
  538.         }
  539. }
  540.  
  541. /*
  542.  * usage - display syntax information
  543.  */
  544.  
  545. usage()
  546. {
  547.     fprintf(stderr,"Usage: titlepage [options] [strings] . . .\n");
  548.     fprintf(stderr,"Print a banner title page using Amiga fonts\n");
  549.     fprintf(stderr,"[options] = -abcdefhilnopqstw, +bcehiqtw\n");
  550.     fprintf(stderr,"[strings] = strings to print\n");
  551. }
  552.  
  553. /*
  554.  * proc_str - process printstring for ~ shifting
  555.  */
  556.  
  557. proc_str()
  558. {
  559.     unsigned char *pstr, *nstr;
  560.     pstr = printstring;
  561.     nstr = stringbuf;
  562.  
  563.     while (*pstr != '\0')
  564.         if (*pstr != '~') *nstr++ = *pstr++;
  565.         else {
  566.             if (*(++pstr) != '~') *nstr++ = *pstr + 128;
  567.             else *nstr++ = '~';
  568.             pstr++;
  569.             }
  570.     *nstr = '\0';
  571.     printstring = stringbuf;
  572. }
  573.