dthelp: compiler warning and coverity warning fixes
[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 librararies 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       return status;                           /* RETURN */
196    }
197
198    /* make sure there is a DISPLAY environment variable */
199    {
200        char *dispfmt = "DISPLAY=%s";
201        char *dispenv = malloc(strlen(dispfmt) + strlen(options->display) + 1);
202        sprintf(dispenv, dispfmt, options->display);
203        putenv(dispenv);
204    }
205
206    /* put the shell print script in there */
207    sprintf(printCommand,"%s", options->shCommand);
208
209    /* set all the options that are IPC to the print script */
210    PutOpt(printCommand,OPT_LPDEST,options->printer,True);
211    PutOpt(printCommand,OPT_COMMAND,options->lpCommand,True);
212    PutOpt(printCommand,OPT_COPYCOUNT,options->copies,True);
213    PutOpt(printCommand,OPT_SILENT,NULL,False);
214    PutOpt(printCommand,OPT_FILEREMOVE,NULL,False);
215    PutOpt(printCommand,OPT_FILE,tmpfile,True);
216    PutOpt(printCommand,OPT_USERFILE,userfile,True);
217  
218    /* execute the shell command to cause printing */
219    if(options->debugHelpPrint) printf("%s\n",printCommand);
220    status = system(printCommand);
221
222    /* unlink(tmpfile);  ** NOTE: don't unlink; let the printCommand do it */
223                         /* note the DTPRINTFILEREMOVE env var setting above */
224    free(tmpfile);
225    return(status);
226 } /*$END$*/
227
228
229
230 \f
231 #ifndef NO_MESSAGE_CATALOG
232 #if DOC
233 ===================================================================
234 $FUNBEG$:  _DtHPrGetMessage()
235 $1LINER$:  Gets a message string from the msg cat; uses dflt if no msg
236 $DESCRIPT$:
237 Gets a message string from the msg cat; uses dflt if no message defined
238 or LANG is undefined or "C".
239 $RETURNS$:
240 $ARGS$:
241 ========================================================$SKIP$=====*/
242 #endif /*DOC*/
243
244 char * _DtHPrGetMessage(
245         int set,
246         int n,
247         char *s)
248 {       /*$CODE$*/
249    char *msg;
250    char *lang;
251    nl_catd catopen();
252    char *catgets();
253    static int s_First = 1;
254    static nl_catd s_Nlmsg_fd;
255    static char * s_CatFileName = NULL;
256
257    if ( s_First ) 
258    {
259       /* Setup our default message catalog names if none have been set! */
260       if (s_CatFileName  == NULL)
261       {
262          /* Setup the short and long versions */
263 #ifdef __ultrix
264          s_CatFileName = strdup(HELPPRINT_CAT_WITH_SUFFIX);
265 #else 
266          s_CatFileName = strdup(HELPPRINT_CAT);
267 #endif
268       }
269       s_First = 0;
270
271       lang = (char *) getenv ("LANG");
272
273       /* If LANG is not set or if LANG=C, then there
274        * is no need to open the message catalog - just
275        * return the built-in string "s".  */
276       if (!lang || !(strcmp (lang, "C"))) 
277          s_Nlmsg_fd = (nl_catd) -1;
278       else
279          s_Nlmsg_fd = catopen(s_CatFileName, 0);
280    }  /* end of first-time processing */
281
282    msg = catgets(s_Nlmsg_fd,set,n,s);
283    return (msg);
284
285 }
286 #endif  /* NO_MESSAGE_CATALOG */
287
288
289