PrintUtil.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <Dt/MsgCatP.h> /* for message cat processing */
  43. #if defined(sun)
  44. #include <locale.h>
  45. #else
  46. #include <langinfo.h>
  47. #endif
  48. #include "HelpPrintI.h"
  49. /*======== flexible constsnts ==============*/
  50. /* message catalog file */
  51. #define HELPPRINT_CAT_WITH_SUFFIX "dthelpprint.cat"
  52. #define HELPPRINT_CAT "dthelpprint"
  53. /*======== dthelpprint.sh options ==============*/
  54. #define OPT_LPDEST "-d"
  55. #define OPT_COMMAND "-m"
  56. #define OPT_COPYCOUNT "-n"
  57. #define OPT_USERFILE "-u"
  58. #define OPT_FILE "-f"
  59. #define OPT_SILENT "-s"
  60. #define OPT_FILEREMOVE "-e"
  61. #define OPT_RAW "-w"
  62. /*======== helper values ===============*/
  63. #define EOS '\0'
  64. /*======== helper variables ===============*/
  65. /* To do:
  66. * check roman 8/Latin 1
  67. * check PAGER env variable
  68. * do character wrap
  69. */
  70. /*======== data structs ==============*/
  71. /*======== static variables ===============*/
  72. /*======== functions ==============*/
  73. #if DOC
  74. ===================================================================
  75. $PFUNBEG$: PutOpt()
  76. $1LINER$: Concats option and value strings into cmd str
  77. $DESCRIPT$:
  78. Concats option and value strings into cmd str
  79. $RETURNS$:
  80. $ARGS$:
  81. ========================================================$SKIP$=====*/
  82. #endif /*DOC*/
  83. static
  84. void PutOpt(
  85. char * cmdStr,
  86. char * option,
  87. char * value,
  88. Boolean optionHasValue)
  89. { /*$CODE$*/
  90. char * start;
  91. char * fmt;
  92. /* check params */
  93. if ( option == NULL
  94. || option[0] == EOS
  95. || ( optionHasValue == True
  96. && (value == NULL || value[0] == EOS) ) )
  97. return; /* RETURN */
  98. start = &cmdStr[strlen(cmdStr)];
  99. if ( value == NULL ) fmt = " %s";
  100. else fmt = " %s '%s'";
  101. sprintf(start,fmt,option,value);
  102. } /*$END$*/
  103. #if DOC
  104. ===================================================================
  105. $FUNBEG$: _DtHPrGetPrOffsetArg()
  106. $1LINER$: Builds the pr offset argument string, if needed
  107. $DESCRIPT$:
  108. Concats option and value strings into cmd str
  109. $RETURNS$:
  110. $ARGS$:
  111. ========================================================$SKIP$=====*/
  112. #endif /*DOC*/
  113. void _DtHPrGetPrOffsetArg(
  114. _DtHPrOptions * options,
  115. char * argStr)
  116. { /*$CODE$*/
  117. if ( options->outputFile && options->outputFile[0] != EOS )
  118. argStr[0] = EOS;
  119. else
  120. sprintf(argStr,options->prOffsetArg, options->colsAdjLeftMargin);
  121. } /*$END$*/
  122. #if DOC
  123. ===================================================================
  124. $FUNBEG$: _DtHPrGenFileOrPrint()
  125. $1LINER$: Executes print Command to generate file; prints if needed
  126. $DESCRIPT$:
  127. Executes the printCommand that is passed in to generate
  128. either the desired output file or to print the results of
  129. the command. If printing the results, the results are first
  130. put in a temporary file, which is then printed indirectly
  131. by invoking a shell script that should print the file. The
  132. temp file is deleted after the shell script executes.
  133. $RETURNS$:
  134. If generating a file: result of system(printCommand)
  135. If printing a file: result of system(printCommand) if fails
  136. result of system("sh -c <shellCommand>") otherwise
  137. $ARGS$:
  138. printCommand: should pt to a very large (e.g. >5000 char) string
  139. that can be modified by this routine
  140. ========================================================$SKIP$=====*/
  141. #endif /*DOC*/
  142. int _DtHPrGenFileOrPrint(
  143. _DtHPrOptions * options,
  144. char * userfile,
  145. char * printCommand)
  146. { /*$CODE$*/
  147. int status;
  148. char * tmpfile;
  149. char cmdFormat[30];
  150. /* put into specified output file?? */
  151. if (options->outputFile[0] != EOS)
  152. {
  153. strcat(printCommand," ");
  154. sprintf(&printCommand[strlen(printCommand)],
  155. options->redirectCmdAndArgs,
  156. options->outputFile ); /* file */
  157. if(options->debugHelpPrint) printf("%s\n",printCommand);
  158. return (system(printCommand)); /* RETURN */
  159. }
  160. /* put into private tmp file */
  161. strcat(printCommand," ");
  162. tmpfile = _DtHPrCreateTmpFile(TMPFILE_PREFIX,TMPFILE_SUFFIX);
  163. sprintf(&printCommand[strlen(printCommand)],
  164. options->redirectCmdAndArgs,
  165. tmpfile ); /* file */
  166. if(options->debugHelpPrint)
  167. printf("%s\n",printCommand);
  168. strcat(printCommand,"\n");
  169. if ( (status = system(printCommand))!= 0)
  170. {
  171. unlink(tmpfile);
  172. free(tmpfile);
  173. return status; /* RETURN */
  174. }
  175. /* make sure there is a DISPLAY environment variable */
  176. {
  177. char *dispfmt = "DISPLAY=%s";
  178. char *dispenv = malloc(strlen(dispfmt) + strlen(options->display) + 1);
  179. sprintf(dispenv, dispfmt, options->display);
  180. putenv(dispenv);
  181. }
  182. /* put the shell print script in there */
  183. sprintf(printCommand,"%s", options->shCommand);
  184. /* set all the options that are IPC to the print script */
  185. PutOpt(printCommand,OPT_LPDEST,options->printer,True);
  186. PutOpt(printCommand,OPT_COMMAND,options->lpCommand,True);
  187. PutOpt(printCommand,OPT_COPYCOUNT,options->copies,True);
  188. PutOpt(printCommand,OPT_SILENT,NULL,False);
  189. PutOpt(printCommand,OPT_FILEREMOVE,NULL,False);
  190. PutOpt(printCommand,OPT_FILE,tmpfile,True);
  191. PutOpt(printCommand,OPT_USERFILE,userfile,True);
  192. /* execute the shell command to cause printing */
  193. if(options->debugHelpPrint) printf("%s\n",printCommand);
  194. status = system(printCommand);
  195. /* unlink(tmpfile); ** NOTE: don't unlink; let the printCommand do it */
  196. /* note the DTPRINTFILEREMOVE env var setting above */
  197. free(tmpfile);
  198. return(status);
  199. } /*$END$*/
  200. #ifndef NO_MESSAGE_CATALOG
  201. #if DOC
  202. ===================================================================
  203. $FUNBEG$: _DtHPrGetMessage()
  204. $1LINER$: Gets a message string from the msg cat; uses dflt if no msg
  205. $DESCRIPT$:
  206. Gets a message string from the msg cat; uses dflt if no message defined
  207. or LANG is undefined or "C".
  208. $RETURNS$:
  209. $ARGS$:
  210. ========================================================$SKIP$=====*/
  211. #endif /*DOC*/
  212. char * _DtHPrGetMessage(
  213. int set,
  214. int n,
  215. const char *s)
  216. { /*$CODE$*/
  217. static int s_First = 1;
  218. static nl_catd s_Nlmsg_fd = (nl_catd) -1;
  219. static char * s_CatFileName = NULL;
  220. if ( s_First )
  221. {
  222. /* Setup our default message catalog names if none have been set! */
  223. if (s_CatFileName == NULL)
  224. {
  225. /* Setup the short and long versions */
  226. s_CatFileName = strdup(HELPPRINT_CAT);
  227. }
  228. s_First = 0;
  229. s_Nlmsg_fd = CATOPEN(s_CatFileName, 0);
  230. } /* end of first-time processing */
  231. return CATGETS(s_Nlmsg_fd, set, n, s);
  232. }
  233. #endif /* NO_MESSAGE_CATALOG */