remove ultrix support
[oweals/cde.git] / cde / programs / dthelp / dthelpprint / PrintUtil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #if DOC
24 /*===================================================================
25 $FILEBEG$:   PrintUtil.c
26 $COMPONENT$: dthelpprint
27 $PROJECT$:   Cde1
28 $SYSTEM$:    HPUX 9.0; AIX 3.2; SunOS 5.3
29 $REVISION$:  $TOG: PrintUtil.c /main/6 1999/02/05 18:57:07 mgreess $
30 $CHGLOG$:    
31 $COPYRIGHT$:
32    (c) Copyright 1993, 1994 Hewlett-Packard Company
33    (c) Copyright 1993, 1994 International Business Machines Corp.
34    (c) Copyright 1993, 1994 Sun Microsystems, Inc.
35    (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of Novell, Inc.
36 ==$END$==============================================================*/
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <nl_types.h>  /* for message cat processing */
44 #if defined(sun)
45 #include <locale.h>
46 #else
47 #include <langinfo.h>
48 #endif
49
50 #include "HelpPrintI.h"
51
52 /*======== flexible constsnts ==============*/
53 /* message catalog file */
54 #define HELPPRINT_CAT_WITH_SUFFIX       "dthelpprint.cat"
55 #define HELPPRINT_CAT                   "dthelpprint"
56
57 /*======== dthelpprint.sh options ==============*/
58 #define OPT_LPDEST              "-d"
59 #define OPT_COMMAND             "-m"
60 #define OPT_COPYCOUNT           "-n"
61 #define OPT_USERFILE            "-u"
62 #define OPT_FILE                "-f"
63 #define OPT_SILENT              "-s"
64 #define OPT_FILEREMOVE          "-e"
65 #define OPT_RAW                 "-w"
66
67 /*======== helper values ===============*/
68 #define EOS           '\0'
69
70 /*======== helper variables ===============*/
71
72 /* To do:
73         * check roman 8/Latin 1
74         * check PAGER env variable
75         * do character wrap
76 */
77
78 /*======== data structs ==============*/
79
80 /*======== static variables ===============*/
81
82 /*======== functions ==============*/
83 #if DOC
84 ===================================================================
85 $PFUNBEG$:  PutOpt()
86 $1LINER$:  Concats option and value strings into cmd str
87 $DESCRIPT$:
88 Concats option and value strings into cmd str
89 $RETURNS$:
90 $ARGS$:
91 ========================================================$SKIP$=====*/
92 #endif /*DOC*/
93
94 static
95 void PutOpt(
96       char * cmdStr,
97       char * option,
98       char * value,
99       Boolean optionHasValue)
100 {       /*$CODE$*/
101    char * start;
102    char * fmt;
103    
104    /* check params */
105    if (    option == NULL 
106         || option[0] == EOS 
107         || (   optionHasValue == True 
108             && (value == NULL || value[0] == EOS) ) )
109        return;                                          /* RETURN */
110    
111    start = &cmdStr[strlen(cmdStr)];
112    if ( value == NULL ) fmt = " %s";
113    else fmt = " %s '%s'";
114    sprintf(start,fmt,option,value);
115 } /*$END$*/
116
117
118 #if DOC
119 ===================================================================
120 $FUNBEG$:  _DtHPrGetPrOffsetArg()
121 $1LINER$:  Builds the pr offset argument string, if needed
122 $DESCRIPT$:
123 Concats option and value strings into cmd str
124 $RETURNS$:
125 $ARGS$:
126 ========================================================$SKIP$=====*/
127 #endif /*DOC*/
128
129 void _DtHPrGetPrOffsetArg(
130    _DtHPrOptions * options,
131    char *          argStr)
132 {       /*$CODE$*/
133    if ( options->outputFile && options->outputFile[0] != EOS )
134       argStr[0] = EOS;
135    else
136       sprintf(argStr,options->prOffsetArg, options->colsAdjLeftMargin);
137 } /*$END$*/
138
139
140 #if DOC
141 ===================================================================
142 $FUNBEG$:  _DtHPrGenFileOrPrint()
143 $1LINER$:  Executes print Command to generate file; prints if needed
144 $DESCRIPT$:
145 Executes the printCommand that is passed in to generate 
146 either the desired output file or to print the results of
147 the command.  If printing the results, the results are first
148 put in a temporary file, which is then printed indirectly
149 by invoking a shell script that should print the file.  The
150 temp file is deleted after the shell script executes.
151 $RETURNS$:
152 If generating a file:  result of system(printCommand)
153 If printing a file:  result of system(printCommand) if fails
154                      result of system("sh -c <shellCommand>") otherwise
155 $ARGS$:
156 printCommand: should pt to a very large (e.g. >5000 char) string
157               that can be modified by this routine
158 ========================================================$SKIP$=====*/
159 #endif /*DOC*/
160
161 int _DtHPrGenFileOrPrint(
162    _DtHPrOptions * options,
163    char *          userfile,
164    char *          printCommand)
165 {       /*$CODE$*/
166    int    status;
167    char * tmpfile;
168    char   cmdFormat[30];
169    
170    /* put into specified output file?? */
171    if (options->outputFile[0] != EOS) 
172    { 
173       strcat(printCommand," ");
174       sprintf(&printCommand[strlen(printCommand)],
175                       options->redirectCmdAndArgs,
176                       options->outputFile );     /* file */
177       if(options->debugHelpPrint) printf("%s\n",printCommand);
178       return (system(printCommand));           /* RETURN */
179    }
180
181    /* put into private tmp file */
182    strcat(printCommand," ");
183    tmpfile = _DtHPrCreateTmpFile(TMPFILE_PREFIX,TMPFILE_SUFFIX);
184    sprintf(&printCommand[strlen(printCommand)],
185                       options->redirectCmdAndArgs,
186                       tmpfile );                          /* file */
187
188    if(options->debugHelpPrint) 
189       printf("%s\n",printCommand);
190
191    strcat(printCommand,"\n");
192    if ( (status = system(printCommand))!= 0)
193    {
194       unlink(tmpfile);
195       free(tmpfile);
196       return status;                           /* RETURN */
197    }
198
199    /* make sure there is a DISPLAY environment variable */
200    {
201        char *dispfmt = "DISPLAY=%s";
202        char *dispenv = malloc(strlen(dispfmt) + strlen(options->display) + 1);
203        sprintf(dispenv, dispfmt, options->display);
204        putenv(dispenv);
205    }
206
207    /* put the shell print script in there */
208    sprintf(printCommand,"%s", options->shCommand);
209
210    /* set all the options that are IPC to the print script */
211    PutOpt(printCommand,OPT_LPDEST,options->printer,True);
212    PutOpt(printCommand,OPT_COMMAND,options->lpCommand,True);
213    PutOpt(printCommand,OPT_COPYCOUNT,options->copies,True);
214    PutOpt(printCommand,OPT_SILENT,NULL,False);
215    PutOpt(printCommand,OPT_FILEREMOVE,NULL,False);
216    PutOpt(printCommand,OPT_FILE,tmpfile,True);
217    PutOpt(printCommand,OPT_USERFILE,userfile,True);
218  
219    /* execute the shell command to cause printing */
220    if(options->debugHelpPrint) printf("%s\n",printCommand);
221    status = system(printCommand);
222
223    /* unlink(tmpfile);  ** NOTE: don't unlink; let the printCommand do it */
224                         /* note the DTPRINTFILEREMOVE env var setting above */
225    free(tmpfile);
226    return(status);
227 } /*$END$*/
228
229
230
231 \f
232 #ifndef NO_MESSAGE_CATALOG
233 #if DOC
234 ===================================================================
235 $FUNBEG$:  _DtHPrGetMessage()
236 $1LINER$:  Gets a message string from the msg cat; uses dflt if no msg
237 $DESCRIPT$:
238 Gets a message string from the msg cat; uses dflt if no message defined
239 or LANG is undefined or "C".
240 $RETURNS$:
241 $ARGS$:
242 ========================================================$SKIP$=====*/
243 #endif /*DOC*/
244
245 char * _DtHPrGetMessage(
246         int set,
247         int n,
248         char *s)
249 {       /*$CODE$*/
250    char *msg;
251    char *lang;
252    nl_catd catopen();
253    char *catgets();
254    static int s_First = 1;
255    static nl_catd s_Nlmsg_fd;
256    static char * s_CatFileName = NULL;
257
258    if ( s_First ) 
259    {
260       /* Setup our default message catalog names if none have been set! */
261       if (s_CatFileName  == NULL)
262       {
263          /* Setup the short and long versions */
264          s_CatFileName = strdup(HELPPRINT_CAT);
265       }
266       s_First = 0;
267
268       lang = (char *) getenv ("LANG");
269
270       /* If LANG is not set or if LANG=C, then there
271        * is no need to open the message catalog - just
272        * return the built-in string "s".  */
273       if (!lang || !(strcmp (lang, "C"))) 
274          s_Nlmsg_fd = (nl_catd) -1;
275       else
276          s_Nlmsg_fd = catopen(s_CatFileName, 0);
277    }  /* end of first-time processing */
278
279    msg = catgets(s_Nlmsg_fd,set,n,s);
280    return (msg);
281
282 }
283 #endif  /* NO_MESSAGE_CATALOG */
284
285
286