home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0739.lha / Keti / main.c < prev    next >
C/C++ Source or Header  |  1992-09-26  |  7KB  |  309 lines

  1.  
  2. /*************  main.c  **********************
  3.  *                                           *
  4.  *                                           *
  5.  *  Exe:        Keti                         *
  6.  *  Version:    1.0                          *
  7.  *                                           *
  8.  *  Why:        Print 3.5" Disklabel         *
  9.  *                                           *
  10.  *  Syntax:     Keti <inputfile>             *
  11.  *                                           *
  12.  *  Input:      Ascii-File                   *
  13.  *               1 Headline  (max 25 chars)  *
  14.  *              14 Textlines (max 44 chars)  *
  15.  *                                           *
  16.  *  Labelsize:  71.5 x 69.6 mm               *
  17.  *              e.g. Zweckform No.3642       *
  18.  *                                           *
  19.  *  Hardware:   Nec P6 (compatible)          *
  20.  *                                           *
  21.  *  Software:   Kickstart 2.xx               *
  22.  *                                           *
  23.  *  Compiler:   DICE - thanx Matt            *
  24.  *              dcc main.c -o Keti -2.0      *
  25.  *              (only 2000 byte !!!)         *
  26.  *                                           *
  27.  *  Date:       14/08/92                     *
  28.  *                                           *
  29.  *  Author:     K÷ssi                        *
  30.  *  Contact:    (0049) (0)2192 7630          *
  31.  *                                           *
  32.  *********************************************/
  33.  
  34. #include <exec/types.h>
  35. #include <exec/exec.h>
  36. #include <exec/libraries.h>
  37. #include <dos/dos.h>
  38. #include <clib/exec_protos.h>
  39. #include <clib/dos_protos.h>
  40. #include <clib/alib_protos.h>
  41.  
  42. #define MAXCHAR     45    /*  incl. '\n' */
  43. #define MAXHEAD     26    /*  incl. '\n' */
  44. #define MAXLINES    14
  45.  
  46. #define RESET       65
  47. #define CONTLEN      7
  48. #define HEADLEN      6
  49. #define OS20STR     20
  50.  
  51. #define BUFSIZE    512
  52.  
  53. #define SI          15
  54. #define DC2         18
  55. #define CR          13
  56. #define ESC         27
  57. #define FS          28
  58. #define CSI      '\x9B'
  59. #define LF         '\n'
  60. #define TAB        '\t'
  61. #define SPACE       ' '
  62. #define EOS        '\0'
  63.  
  64.  
  65. const UBYTE version[]     = {'$','V','E','R',':',' '};
  66. const UBYTE myname[]      = "Keti v1.0 ⌐ Koessi 92    \n\n";
  67. const UBYTE usagestr[]    = "USAGE:  Keti <textfile>\n\n"
  68.                             "textfile may have:   1 headline  (25 chars)\n"
  69.                             "\t\tand 14 textlines (44 chars)\n";
  70. const UBYTE os20str[]     = "No OS2.xx, sorry...\n";
  71. const UBYTE parstr[]      = "par:";
  72. const UBYTE prtstr[]      = "prt:";
  73. const UBYTE fileisempty[] = ": File is empty !\n";
  74. const UBYTE errorstr[]    =
  75. {
  76.   CSI, '3','2','m','*','*','*',' ','E','r','r','o','r',' ', EOS
  77. };
  78. const UBYTE normal[]      =
  79. {
  80.   CSI, '0','m', EOS
  81. };
  82.  
  83. const UBYTE reset[RESET]  =
  84. {
  85.    FS, '@',       /*  RESET   */
  86.   ESC, 'P',       /*  PICA    */
  87.   ESC, 'x',  1,   /*  NLQ     */
  88.   ESC, 'l',  2,   /*  LM  2   */
  89.   ESC, 'Q', 28,   /*  RM 28   */
  90.   ESC, 'R',  0,   /*  USA     */
  91.   ESC, '0',       /*  88 LPP  */
  92.    LF,  CR,       /*  CR LF   */
  93.    SI,            /*  COND ON */
  94.   ESC, 'a',  2,   /*  RIGHT   */
  95.  
  96.   'W', 'R','I','T','E',' ',' ','E','N','A','B','L','E',' ','-','>', LF, LF,
  97.   'W', 'R','I','T','E',' ','P','R','O','T','E','C','T',' ','-','>', LF,
  98.  
  99.   ESC, 'a', 0,    /*  LEFT    */
  100.   ESC, 'J', 15,   /*  15/180  */
  101. };
  102.  
  103. const UBYTE headline[HEADLEN] =
  104. {
  105.   DC2,  LF, CR,   /*  COND OF */
  106.   ESC, 'a',  1    /*  CENTER  */
  107. };
  108.  
  109. const UBYTE contline[CONTLEN] =
  110. {
  111.    SI,            /*  COND ON */
  112.   ESC, 'a',  0,   /*  LEFT    */
  113.   ESC, 'Q', 48    /*  RM 48   */
  114. };
  115.  
  116. BPTR input;     /* file         */
  117. BPTR out;       /* par: | prt:  */
  118.  
  119. UBYTE buffer[BUFSIZE];
  120.  
  121. extern struct Library *SysBase;
  122.  
  123. int cleanup(int error);
  124.  
  125. /***********************  main  **********************************/
  126. int
  127. _main(short len, char *arg)
  128. {
  129.   char  *buf    = buffer;
  130.   char  *ptr    = buf;
  131.   int    chars  = 0;
  132.  
  133.   if (SysBase->lib_Version < 36)
  134.   {
  135.     BPTR out0;
  136.     if (out0 = Output())
  137.     {
  138.       Write(out0, os20str, OS20STR);
  139.       Close(out0);
  140.     }
  141.     return(RETURN_FAIL);
  142.   }
  143.  
  144.   PutStr(myname);
  145.  
  146.   if (*arg == LF)
  147.   {
  148.     PutStr(usagestr);
  149.     return(cleanup(RETURN_OK));
  150.   }
  151.  
  152.   arg[len - 1] = EOS;
  153.  
  154.   if (((input = Open(arg, MODE_OLDFILE))   == DOSFALSE) ||
  155.       ((chars = Read(input, buf, BUFSIZE)) == -1))
  156.     return(cleanup(RETURN_WARN));
  157.  
  158.   if (chars < BUFSIZE)                /*  everything red */
  159.   {
  160.     Close(input);
  161.     input = NULL;
  162.   }
  163.  
  164.   while (chars)                       /*  skip empty space */
  165.   {
  166.     char p = *ptr;
  167.     if (!(p == SPACE || p == LF || p == TAB))
  168.       break;
  169.  
  170.     ++ptr; --chars;
  171.   }
  172.  
  173.   if (chars == 0)                     /*  file empty  */
  174.   {
  175.     PutStr(errorstr);
  176.     PutStr(fileisempty);
  177.     return(cleanup(RETURN_OK));
  178.   }
  179.  
  180.   if ((out = Open(prtstr, MODE_NEWFILE)) == DOSFALSE)
  181.     return(cleanup(RETURN_WARN));
  182.   else
  183.     Close(out);
  184.  
  185.   if ((out = Open(parstr, MODE_NEWFILE)) == DOSFALSE)
  186.     return(cleanup(RETURN_WARN));
  187.  
  188.   buf = ptr;
  189.   len = 0;
  190.   while (chars)     /*  read headline */
  191.   {
  192.     ++len; --chars;
  193.     if (*ptr++ == LF)
  194.       break;
  195.   }
  196.  
  197.   short cnt = MAXCHAR;
  198.   if (len > cnt)
  199.   {
  200.     len = cnt;
  201.     buf[MAXCHAR - 1] = LF;
  202.   }
  203.  
  204.   if (!((Write(out,    reset,   RESET) ==   RESET) &&
  205.         (Write(out,      buf,     len) ==     len) &&
  206.         (Write(out, headline, HEADLEN) == HEADLEN)))
  207.     return(cleanup(RETURN_WARN));
  208.  
  209.   cnt = MAXHEAD;
  210.   if (len > cnt)
  211.   {
  212.     len = cnt;
  213.     buf[MAXHEAD - 1] = LF;
  214.   }
  215.  
  216.   if (!((Write(out,      buf,     len) ==     len) &&
  217.         (Write(out, contline, CONTLEN) == CONTLEN)))
  218.     return(cleanup(RETURN_WARN));
  219.  
  220.   Close(out);
  221.  
  222.   if ((out = Open(prtstr, MODE_NEWFILE)) == DOSFALSE)
  223.     return(cleanup(RETURN_WARN));
  224.  
  225.   len = 0;
  226.   buf = buffer;
  227.   cnt = 1;          /*  cnt lin */
  228.   short xtr = 0;    /*  cnt esc */
  229.  
  230.  
  231.   while (chars)
  232.   {
  233.     while (chars)
  234.     {
  235.       if (*ptr == (BYTE)CSI)   /*  esc codes */
  236.         xtr += 3;
  237.  
  238.       *buf = *ptr++;
  239.  
  240.       ++len; --chars;
  241.  
  242.       if (*buf++ == LF || chars == 0)   /*  eol */
  243.       {
  244.         buf = buffer;
  245.         if ((len - xtr) > MAXCHAR)
  246.         {
  247.           len = MAXCHAR;
  248.           buf[MAXCHAR - 1] = LF;
  249.         }
  250.  
  251.         if (Write(out, buf, len) != len)
  252.           return(cleanup(RETURN_WARN));
  253.  
  254.         len = 0;
  255.         if (chars)
  256.         {
  257.           if (cnt < MAXLINES)
  258.           {
  259.             cnt++;
  260.             xtr = 0;
  261.           }
  262.           else
  263.             chars = 0;
  264.         }
  265.         else
  266.         {
  267.           if (xtr)
  268.           {
  269.             short mx = 3;
  270.             while (xtr > mx)
  271.               xtr -= mx;
  272.             xtr = mx - xtr;
  273.           }
  274.         }
  275.       }
  276.     }
  277.     if (input && (cnt < MAXLINES))
  278.     {
  279.       ptr = buffer;
  280.       if ((chars = Read(input, ptr, BUFSIZE)) == -1)
  281.         return(cleanup(RETURN_WARN));
  282.     }
  283.   }                         /*  chars > 0 */
  284.   for (*buf = LF; cnt <= MAXLINES; cnt++)
  285.   {
  286.     if (Write(out, buf, 1) != 1)
  287.       return(cleanup(RETURN_WARN));
  288.   }
  289.   return(cleanup(RETURN_OK));
  290. }
  291.  
  292.  
  293. /***********************  cleanup() ********************************/
  294. int
  295. cleanup(int error)
  296. {
  297.   if (error)
  298.     PrintFault(IoErr(), errorstr);
  299.  
  300.   PutStr(normal);
  301.  
  302.   if (input)
  303.     Close(input);
  304.   if (out)
  305.     Close(out);
  306.  
  307.   return(error);
  308. }
  309.