1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/prtgif.c,v 1.6 2011/05/16 16:21:59 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_PRTGIF_C_
22 
23 #define PRTGIF_NO_TGIF_DBG 1
24 
25 #include "tgifdefs.h"
26 
27 #include "prtgif.e"
28 
main(argc,argv)29 int main(argc, argv)
30    int	argc;
31    char	* argv[];
32 {
33    register int	i;
34    int		total_len;
35    char		tgif_path[MAXSTRING+1], print_cmd[MAXSTRING+1], * cmd, * c_ptr;
36    char		tmp_str[MAXSTRING+1], **s_ptr;
37    FILE		* fp;
38 
39    if ((c_ptr=getenv("TGIFPATH")) == NULL) {
40       strcpy(tgif_path, TGIF_PATH);
41    } else {
42       if (strlen(c_ptr) >= MAXSTRING) {
43          /* must be an error */
44          strcpy(tgif_path, TGIF_PATH);
45       } else {
46          strcpy(tgif_path, c_ptr);
47       }
48    }
49 
50 #ifdef PRINT_CMD
51    strcpy(print_cmd, PRINT_CMD);
52 #else
53 #ifdef VMS
54    strcpy(print_cmd, "print");
55 #else
56 #ifdef SYSV
57    strcpy(print_cmd, "lp -dpostscript");
58 #else
59    strcpy(print_cmd, "lpr");
60 #endif /* SYSV */
61 #endif /* VMS */
62 #endif /* PRINT_CMD */
63 
64    total_len = strlen("tgif ")+strlen("-prtgif ");
65    total_len += strlen(tgif_path); total_len += strlen("-tgif_path")+4;
66    total_len += strlen(print_cmd); total_len += strlen("-print_cmd")+4;
67    for (i=argc-1, s_ptr=(&argv[1]); i > 0; i--, s_ptr++) {
68       total_len += strlen(*s_ptr)+1;
69    }
70    cmd = (char*)malloc((total_len+2)*sizeof(char));
71    if (cmd == NULL) {
72       fprintf(stderr, "Out of virtual memory and cannot malloc().\n");
73    }
74    c_ptr = cmd;
75    sprintf(c_ptr, "tgif "); c_ptr += strlen("tgif")+1;
76    sprintf(c_ptr, "-prtgif "); c_ptr += strlen("-prtgif")+1;
77    sprintf(c_ptr, "-tgif_path \"%s\" ", tgif_path);
78    c_ptr += strlen(tgif_path)+strlen("-tgif_path")+4;
79    sprintf(c_ptr, "-print_cmd \"%s\" ", print_cmd);
80    c_ptr += strlen(print_cmd)+strlen("-print_cmd")+4;
81    for (argc--, argv++; argc > 0; argc--, argv++) {
82       sprintf(c_ptr, "%s ", *argv);
83       c_ptr += strlen(*argv)+1;
84    }
85    *(--c_ptr) = '\0';
86 
87    if ((fp=(FILE*)popen(cmd, "r")) == NULL) {
88       fprintf(stderr, "Cannot popen() to tgif.  Abort!\n");
89       exit (-1);
90    }
91    while (fgets(tmp_str, MAXSTRING, fp) != NULL) {
92       fprintf(stderr, "%s", tmp_str);
93    }
94    pclose(fp);
95    free(cmd);
96    return 0;
97 }
98