Back to index

radiance  4R0+20100331
lam.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: lam.c,v 1.9 2006/05/07 15:44:28 greg Exp $";
00003 #endif
00004 /*
00005  *  lam.c - simple program to laminate files.
00006  *
00007  *     7/14/88              Greg Ward
00008  */
00009 
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <stdio.h>
00013 #include <ctype.h>
00014 
00015 #include "platform.h"
00016 #include "rtprocess.h"
00017 
00018 #define MAXFILE             512           /* maximum number of files */
00019 
00020 #define MAXLINE             4096          /* maximum input line */
00021 
00022 FILE   *input[MAXFILE];
00023 int    bytsiz[MAXFILE];
00024 char   *tabc[MAXFILE];
00025 int    nfiles;
00026 
00027 char   buf[MAXLINE];
00028 
00029 int
00030 main(argc, argv)
00031 int    argc;
00032 char   *argv[];
00033 {
00034        register int  i;
00035        char   *curtab;
00036        int    curbytes;
00037        int    running, puteol;
00038 
00039        curtab = "\t";
00040        curbytes = 0;
00041        nfiles = 0;
00042        for (i = 1; i < argc; i++) {
00043               if (argv[i][0] == '-') {
00044                      switch (argv[i][1]) {
00045                      case 't':
00046                             curtab = argv[i]+2;
00047                             break;
00048                      case 'i':
00049                             switch (argv[i][2]) {
00050                             case 'f':
00051                                    curbytes = sizeof(float);
00052                                    break;
00053                             case 'd':
00054                                    curbytes = sizeof(double);
00055                                    break;
00056                             case 'w':
00057                                    curbytes = sizeof(int);
00058                                    break;
00059                             case 'a':
00060                                    curbytes = argv[i][3] ? 1 : 0;
00061                                    break;
00062                             default:
00063                                    goto badopt;
00064                             }
00065                             if (isdigit(argv[i][3]))
00066                                    curbytes *= atoi(argv[i]+3);
00067                             if (curbytes < 0 || curbytes > MAXLINE) {
00068                                    fputs(argv[0], stderr);
00069                                    fputs(": illegal input size\n", stderr);
00070                                    exit(1);
00071                             }
00072                             if (curbytes)
00073                                    curtab = "";
00074                             break;
00075                      case '\0':
00076                             tabc[nfiles] = curtab;
00077                             bytsiz[nfiles] = curbytes;
00078                             input[nfiles++] = stdin;
00079                             break;
00080                      badopt:;
00081                      default:
00082                             fputs(argv[0], stderr);
00083                             fputs(": bad option\n", stderr);
00084                             exit(1);
00085                      }
00086               } else if (argv[i][0] == '!') {
00087                      tabc[nfiles] = curtab;
00088                      bytsiz[nfiles] = curbytes;
00089                      if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
00090                             fputs(argv[i], stderr);
00091                             fputs(": cannot start command\n", stderr);
00092                             exit(1);
00093                      }
00094                      if (bytsiz[nfiles])
00095                             SET_FILE_BINARY(input[nfiles]);
00096                      ++nfiles;
00097               } else {
00098                      tabc[nfiles] = curtab;
00099                      bytsiz[nfiles] = curbytes;
00100                      if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
00101                             fputs(argv[i], stderr);
00102                             fputs(": cannot open file\n", stderr);
00103                             exit(1);
00104                      }
00105                      if (bytsiz[nfiles])
00106                             SET_FILE_BINARY(input[nfiles]);
00107                      ++nfiles;
00108               }
00109               if (nfiles >= MAXFILE) {
00110                      fputs(argv[0], stderr);
00111                      fputs(": too many input streams\n", stderr);
00112                      exit(1);
00113               }
00114        }
00115        puteol = 0;                        /* check for tab character */
00116        for (i = nfiles; i--; )
00117               if (isprint(tabc[i][0]) || tabc[i][0] == '\t') {
00118                      puteol++;
00119                      break;
00120               }
00121        do {
00122               running = 0;
00123               for (i = 0; i < nfiles; i++) {
00124                      if (bytsiz[i]) {            /* binary file */
00125                             if (fread(buf, bytsiz[i], 1, input[i]) == 1) {
00126                                    if (i)
00127                                           fputs(tabc[i], stdout);
00128                                    fwrite(buf, bytsiz[i], 1, stdout);
00129                                    running++;
00130                             }
00131                      } else if (fgets(buf, MAXLINE, input[i]) != NULL) {
00132                             if (i)
00133                                    fputs(tabc[i], stdout);
00134                             buf[strlen(buf)-1] = '\0';
00135                             fputs(buf, stdout);
00136                             puteol++;
00137                             running++;
00138                      }
00139               }
00140               if (running && puteol)
00141                      putchar('\n');
00142        } while (running);
00143 
00144        exit(0);
00145 }