2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 /*===================================================================
25 $FILEBEG$: PrintUtil.c
26 $COMPONENT$: dthelpprint
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 $
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$==============================================================*/
43 #include <nl_types.h> /* for message cat processing */
50 #include "HelpPrintI.h"
52 /*======== flexible constsnts ==============*/
53 /* message catalog file */
54 #define HELPPRINT_CAT_WITH_SUFFIX "dthelpprint.cat"
55 #define HELPPRINT_CAT "dthelpprint"
57 /*======== dthelpprint.sh options ==============*/
58 #define OPT_LPDEST "-d"
59 #define OPT_COMMAND "-m"
60 #define OPT_COPYCOUNT "-n"
61 #define OPT_USERFILE "-u"
63 #define OPT_SILENT "-s"
64 #define OPT_FILEREMOVE "-e"
67 /*======== helper values ===============*/
70 /*======== helper variables ===============*/
73 * check roman 8/Latin 1
74 * check PAGER env variable
78 /*======== data structs ==============*/
80 /*======== static variables ===============*/
82 /*======== functions ==============*/
84 ===================================================================
86 $1LINER$: Concats option and value strings into cmd str
88 Concats option and value strings into cmd str
91 ========================================================$SKIP$=====*/
99 Boolean optionHasValue)
107 || ( optionHasValue == True
108 && (value == NULL || value[0] == EOS) ) )
111 start = &cmdStr[strlen(cmdStr)];
112 if ( value == NULL ) fmt = " %s";
113 else fmt = " %s '%s'";
114 sprintf(start,fmt,option,value);
119 ===================================================================
120 $FUNBEG$: _DtHPrGetPrOffsetArg()
121 $1LINER$: Builds the pr offset argument string, if needed
123 Concats option and value strings into cmd str
126 ========================================================$SKIP$=====*/
129 void _DtHPrGetPrOffsetArg(
130 _DtHPrOptions * options,
133 if ( options->outputFile && options->outputFile[0] != EOS )
136 sprintf(argStr,options->prOffsetArg, options->colsAdjLeftMargin);
141 ===================================================================
142 $FUNBEG$: _DtHPrGenFileOrPrint()
143 $1LINER$: Executes print Command to generate file; prints if needed
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.
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
156 printCommand: should pt to a very large (e.g. >5000 char) string
157 that can be modified by this routine
158 ========================================================$SKIP$=====*/
161 int _DtHPrGenFileOrPrint(
162 _DtHPrOptions * options,
170 /* put into specified output file?? */
171 if (options->outputFile[0] != EOS)
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 */
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 */
188 if(options->debugHelpPrint)
189 printf("%s\n",printCommand);
191 strcat(printCommand,"\n");
192 if ( (status = system(printCommand))!= 0)
196 return status; /* RETURN */
199 /* make sure there is a DISPLAY environment variable */
201 char *dispfmt = "DISPLAY=%s";
202 char *dispenv = malloc(strlen(dispfmt) + strlen(options->display) + 1);
203 sprintf(dispenv, dispfmt, options->display);
207 /* put the shell print script in there */
208 sprintf(printCommand,"%s", options->shCommand);
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);
219 /* execute the shell command to cause printing */
220 if(options->debugHelpPrint) printf("%s\n",printCommand);
221 status = system(printCommand);
223 /* unlink(tmpfile); ** NOTE: don't unlink; let the printCommand do it */
224 /* note the DTPRINTFILEREMOVE env var setting above */
232 #ifndef NO_MESSAGE_CATALOG
234 ===================================================================
235 $FUNBEG$: _DtHPrGetMessage()
236 $1LINER$: Gets a message string from the msg cat; uses dflt if no msg
238 Gets a message string from the msg cat; uses dflt if no message defined
239 or LANG is undefined or "C".
242 ========================================================$SKIP$=====*/
245 char * _DtHPrGetMessage(
254 static int s_First = 1;
255 static nl_catd s_Nlmsg_fd;
256 static char * s_CatFileName = NULL;
260 /* Setup our default message catalog names if none have been set! */
261 if (s_CatFileName == NULL)
263 /* Setup the short and long versions */
264 s_CatFileName = strdup(HELPPRINT_CAT);
268 lang = (char *) getenv ("LANG");
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;
276 s_Nlmsg_fd = catopen(s_CatFileName, 0);
277 } /* end of first-time processing */
279 msg = catgets(s_Nlmsg_fd,set,n,s);
283 #endif /* NO_MESSAGE_CATALOG */