home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Ascii-Ansi / vec3.231.lha / vec3231 / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-08  |  7.6 KB  |  405 lines

  1. /*
  2.  * MAIN.C (VEC)
  3.  *
  4.  * Copyright (c) 1993 Ville Saari
  5.  * All rights reserved
  6.  *
  7.  * Created: 07-Apr-93
  8.  * Updated: 06-Dec-93
  9.  */
  10.  
  11. #include <limits.h>
  12. #include "vec.h"
  13.  
  14. const unsigned short crc16tab[256]=
  15.     {
  16.     0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  17.     0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  18.     0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  19.     0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  20.     0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  21.     0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  22.     0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  23.     0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  24.     0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  25.     0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  26.     0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  27.     0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  28.     0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  29.     0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  30.     0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  31.     0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  32.     0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  33.     0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  34.     0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  35.     0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  36.     0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  37.     0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  38.     0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  39.     0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  40.     0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  41.     0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  42.     0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  43.     0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  44.     0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  45.     0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  46.     0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  47.     0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
  48.     };
  49.  
  50. extern const unsigned char usagestring[];
  51.  
  52. unsigned char method=DEFMETHOD;
  53. long width=DEFWIDTH;
  54. int quick, silent, decode, nomode, cr, shortlines=1, literaltabs;
  55. int lines=0, pipe=0, resetuu=0;
  56. static int append;
  57. char *name=0;
  58. static filehandle infile;
  59. filehandle outfile, stdoutfile;
  60. static int infileflag=0;
  61. int outfileflag=0;
  62. unsigned char *inbuf, *inptr, *outptr, *inbufend;
  63. static unsigned char *outbuf;
  64. unsigned long bufsize=BUFSIZE;
  65. unsigned long trueread;
  66. unsigned char *outbufend;
  67. static int eof;
  68.  
  69. void printerrh(const char *string)
  70.     {
  71.     printerr(decode?UDNAME": ":UENAME": ");
  72.     printerr(string);
  73.     }
  74.  
  75. int main(int ac, char **arg)
  76.     {
  77.     int t, matches=0, retval=OK;
  78.     char *p, *filename=0, *workdir=0;
  79.  
  80.     if(!ac) return FAIL;
  81.  
  82.     init();
  83.  
  84.     if((t=strlen(arg[0]))>1 && tolower(arg[0][t-2])=='d') decode=1;
  85.  
  86.     while(arg++, --ac && **arg=='-' && *(p=*arg+1))
  87.         do switch(*p)
  88.             {
  89.             case '0':
  90.             case '1':
  91.             case '2':
  92.             case '3':
  93.             case 'a':
  94.             case 'i':
  95.             case 'u':
  96.             case 'x':
  97.                 method=*p;
  98.                 break;
  99.  
  100.             case 'd':
  101.                 decode=1;
  102.                 break;
  103.  
  104.             case 'e':
  105.                 decode=0;
  106.                 break;
  107.  
  108.             case 'f':
  109.                 shortlines=0;
  110.                 break;
  111.  
  112.             case 'L':
  113.                 lines=2;
  114.                 break;
  115.  
  116.             case 'l':
  117.                 lines=1;
  118.                 break;
  119.  
  120.             case 'p':
  121.                 pipe=1;
  122.                 break;
  123.  
  124.             case 'q':
  125.                 quick=1;
  126.                 break;
  127.  
  128.             case 'r':
  129.                 cr=1;
  130.                 break;
  131.  
  132.             case 'R':
  133.                 resetuu=1;
  134.                 break;
  135.  
  136.             case 's':
  137.                 silent=1;
  138.                 break;
  139.  
  140.             case 't':
  141.                 literaltabs=1;
  142.                 break;
  143.  
  144.             case 'D':
  145.                 if(!--ac) goto usage;
  146.                 workdir=*++arg;
  147.                 break;
  148.  
  149.             case 'n':
  150.                 if(!--ac) goto usage;
  151.                 name=*++arg;
  152.                 break;
  153.  
  154.             case 'N':
  155.                 nomode=1;
  156.                 break;
  157.  
  158.             case 'A':
  159.                 if(!--ac) goto usage;
  160.                 filename=*++arg;
  161.                 append=1;
  162.                 break;
  163.  
  164.             case 'o':
  165.                 if(!--ac) goto usage;
  166.                 filename=*++arg;
  167.                 break;
  168.  
  169.             case 'b':
  170.                 if(!--ac || (bufsize=atoi(*++arg))<512) goto usage;
  171.                 break;
  172.  
  173.             case 'w':
  174.                 if(--ac && (width=atoi(*++arg))>=0)
  175.                     {
  176.                     if(!width) width=LONG_MAX;
  177.                     break;
  178.                     }
  179.  
  180.             default:
  181.             usage:
  182.                 printerr(usagestring);
  183.                 cleanup();
  184.                 return FAIL;
  185.             }
  186.         while(*++p);
  187.  
  188.     if(width<3 && (method=='a' || method=='i')) width=3;
  189.  
  190.     if(ac && **arg=='-') arg++, ac--;
  191.  
  192.     if(!(inbuf=malloc(bufsize)) || !(outbuf=malloc(bufsize+SPAREBUF)))
  193. #ifdef FINNISH
  194.         errcleanup("Muisti ei riit" a_ " I/O puskureille.\n");
  195. #else
  196.         errcleanup("Can't allocate memory for I/O buffers.\n");
  197. #endif
  198.     inbufend=inptr=inbuf;
  199.     outptr=outbuf;
  200.     outbufend=outbuf+bufsize;
  201.  
  202.     if(workdir && !changedir(workdir))
  203. #ifdef FINNISH
  204.         errcleanup("Ty" o_ "hakemiston vaihto ei onnistu.\n");
  205. #else
  206.         errcleanup("Can't change work directory.\n");
  207. #endif
  208.  
  209.     stdoutfile=getstdout(decode && pipe);
  210.  
  211.     if(filename)
  212.         {
  213.         if(failedopen(outfile=
  214.             append?openappend(filename, decode):openwrite(filename, decode)))
  215. #ifdef FINNISH
  216.             errcleanup("Tulostustiedosto ei aukea.\n");
  217. #else
  218.             errcleanup("Can't open output file.\n");
  219. #endif
  220.         else
  221.             outfileflag=1;
  222.         }
  223.     else if(!decode || pipe)
  224.         outfile=stdoutfile;
  225.  
  226.     outptr=outbuf;
  227.  
  228.     if(ac)
  229.         {
  230.         while(ac--)
  231.             {
  232.             p=firstmatch(*arg);
  233.  
  234.             while(p)
  235.                 {
  236.                 matches++;
  237.  
  238.                 if(failedopen(infile=openread(p, !decode)))
  239.                     {
  240. #ifdef FINNISH
  241.                     printerrh("Tiedosto `");
  242.                     printerr(p);
  243.                     printerr("' ei aukea.\n");
  244. #else
  245.                     printerrh("Can't open input file `");
  246.                     printerr(p);
  247.                     printerr("'.\n");
  248. #endif
  249.                     retval=ERROR;
  250.                     }
  251.                 else
  252.                     {
  253.                     infileflag=1;
  254.                     eof=0;
  255.                     inptr=inbufend;
  256.  
  257.                     if(decode)
  258.                         {
  259.                         if(!vdc()) retval=ERROR;
  260.                         }
  261.                     else
  262.                         vec(p);
  263.  
  264.                     closefile(infile);
  265.                     infileflag=0;
  266.                     }
  267.  
  268.                 p=nextmatch();
  269.                 }
  270.  
  271.             arg++;
  272.             }
  273.  
  274.         if(!matches)
  275.             {
  276. #ifdef FINNISH
  277.             printerr("Ei tiedostoja.\n");
  278. #else
  279.             printerr("No match.\n");
  280. #endif
  281.             retval=WARN;
  282.             }
  283.         }
  284.     else
  285.         {
  286.         infile=getstdin(!decode);
  287.         decode?vdc():vec("");
  288.         }
  289.  
  290.     if(outfile) flushoutput();
  291.     if(filename) closefile(outfile);
  292.  
  293.     cleanup();
  294.     return retval;
  295.     }
  296.  
  297. void errcleanup(const char *msg)
  298.     {
  299.     if(infileflag) closefile(infile);
  300.     if(outfileflag) closefile(outfile);
  301.  
  302.     if(msg)
  303.         printerrh(msg);
  304.  
  305.     cleanup();
  306.     exit(FAIL);
  307.     }
  308.  
  309. int fillbuffer(int clear)
  310.     {
  311.     unsigned char *t=inbuf+bufsize;
  312.     long s;
  313.  
  314.     if(interrupt()) errcleanup("*** Break\n");
  315.  
  316.     if(eof) return 0;
  317.  
  318.     if(clear) inbufend=inptr=inbuf;
  319.  
  320.     do inbufend+=s=readfile(infile, inbufend, t-inbufend);
  321.     while(s>0 && inbufend<t);
  322.  
  323. #ifdef FINNISH
  324.     if(s<0) errcleanup("Virhe tiedostoa lukiessa.\n");
  325. #else
  326.     if(s<0) errcleanup("Error while reading input file.\n");
  327. #endif
  328.     if(!s) eof=1;
  329.  
  330.     return 1;
  331.     }
  332.  
  333. unsigned char *get(unsigned long size)
  334.     {
  335.     unsigned char *t=inptr;
  336.     long s;
  337.  
  338.     trueread=size;
  339.  
  340.     if((inptr+=size)<=inbufend) return t;
  341.  
  342.     memcpy(inbuf, t, s=inbufend-t);
  343.     inbufend=inbuf+s;
  344.  
  345.     fillbuffer(0);
  346.  
  347.     if((inptr=inbuf+size)>inbufend)
  348.         {
  349.         inptr=inbufend;
  350.         trueread=inbufend-inbuf;
  351.         }
  352.  
  353.     return inbuf;
  354.     }
  355.  
  356. void flushoutput(void)
  357.     {
  358.     unsigned char *t=outbuf;
  359.     long s;
  360.  
  361.     if(interrupt()) errcleanup("*** Break\n");
  362.  
  363.     while(t<outptr)
  364.         {
  365.         if((s=writefile(outfile, t, outptr-t))<=0)
  366. #ifdef FINNISH
  367.             errcleanup("Virhe tiedostoa kirjoittaessa.\n");
  368. #else
  369.             errcleanup("Error while writing output file.\n");
  370. #endif
  371.  
  372.         t+=s;
  373.         }
  374.  
  375.     outptr=outbuf;
  376.     }
  377.  
  378. void put(const unsigned char *data, unsigned long size)
  379.     {
  380.     while(outptr+size>outbufend)
  381.         {
  382.         unsigned long t=outbufend-outptr;
  383.  
  384.         memcpy(outptr, data, t);
  385.         outptr=outbufend;
  386.         data+=t;
  387.         size-=t;
  388.  
  389.         flushoutput();
  390.         }
  391.  
  392.     memcpy(outptr, data, size);
  393.     outptr+=size;
  394.     }
  395.  
  396. char *ltoa(unsigned long l)
  397.     {
  398.     static char buf[10];
  399.     char *p=buf+9;
  400.  
  401.     do    *--p='0'+l%10; while(l/=10);
  402.  
  403.     return p;
  404.     }
  405.