FormatMan.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. /* $TOG: FormatMan.c /main/14 1999/10/14 15:06:59 mgreess $ */
  24. /************************************<+>*************************************
  25. ****************************************************************************
  26. **
  27. ** File: FormatMan.c
  28. **
  29. ** Project: Text Graphic Display Library
  30. **
  31. ** Description: This body of formats the input into a form understood by
  32. ** a Display Area.
  33. **
  34. **
  35. ** (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
  36. **
  37. ** (c) Copyright 1993, 1994 Hewlett-Packard Company
  38. ** (c) Copyright 1993, 1994 International Business Machines Corp.
  39. ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  40. ** (c) Copyright 1993, 1994 Novell, Inc.
  41. **
  42. **
  43. **
  44. ****************************************************************************
  45. ************************************<+>*************************************/
  46. /*
  47. * system includes
  48. */
  49. #include <errno.h>
  50. #if defined(sun)
  51. #include <sys/utsname.h>
  52. #endif
  53. #include <limits.h>
  54. #include <locale.h>
  55. #include <nl_types.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <Xm/Xm.h>
  61. /*
  62. * Canvas Engine
  63. */
  64. #include "CanvasP.h"
  65. #include "CanvasSegP.h"
  66. /*
  67. * private includes
  68. */
  69. #include "bufioI.h"
  70. #include "DisplayAreaP.h"
  71. #include "Access.h"
  72. #include "CleanUpI.h"
  73. #include "FontAttrI.h"
  74. #include "FormatUtilI.h"
  75. #include "FormatCCDFI.h"
  76. #include "FormatManI.h"
  77. #include "HelpXlate.h"
  78. #include "XInterfaceI.h"
  79. #ifdef NLS16
  80. #endif
  81. /******************************************************************************
  82. *
  83. * Private variables and defines.
  84. *
  85. *****************************************************************************/
  86. enum State {
  87. Char,
  88. Bold,
  89. BoldDone,
  90. Italic,
  91. BoldItalic,
  92. BoldItalicDone};
  93. #define STDOUT_FD (1) /* File descriptor for std. output. */
  94. #define STDERR_FD (1) /* File descriptor for std. error. */
  95. #define PIPE_OUT (0) /* The output end of a pipe. */
  96. #define PIPE_IN (1) /* The input end of a pipe. */
  97. static char *ScanString = "<\n\\ ";
  98. static char *EndToken = "</>";
  99. static char *BoldToken = "<weight bold>";
  100. static char *ItalicToken = "<angle italic>";
  101. static int EndTokenSize = 3;
  102. static int BoldTokenSize = 13;
  103. static int ItalicTokenSize = 14;
  104. /******** Private Function Declarations ********/
  105. static int FormatManPage(
  106. VarHandle my_vars,
  107. BufFilePtr in_file,
  108. char *in_buf,
  109. int in_size,
  110. _DtHelpFontHints *font_attr,
  111. char **out_buf,
  112. int *out_size,
  113. int *out_max );
  114. /******** End Public Function Declarations ********/
  115. #define FileStream(f) ((FILE *)(f)->hidden)
  116. /******************************************************************************
  117. *
  118. * Private Functions
  119. *
  120. *****************************************************************************/
  121. /*********************************************************************
  122. * Procedure: GetNextState (char c, wchar_t wide_c, int length, char *str,
  123. * wchar_t ret_wc, int ret_wc_len, char ret_str_ptr,
  124. * Boolean ret_flag)
  125. *
  126. * Parameters: wide_c Specifies the wide char value of the
  127. * previous character.
  128. * length Specifies the length in bytes of the
  129. * previous character.
  130. * str Specifies the pointer to the string of
  131. * bytes to be processed next.
  132. * ret_wc Returns the wide character that is to be
  133. * italicized iff the state returned is Italic.
  134. * ret_wc_len Returns the length in bytes of ret_wc.
  135. * ret_str_ptr Returns the pointer to the next character
  136. * after the bold or italic character.
  137. * ret_flag Returns whether we have encountered a
  138. * + followed by a _, to convert to +/-
  139. *
  140. * Returns: Bold, Italic, Char
  141. *
  142. * Purpose: To determine if the sequence of characters represent
  143. * a bold or italic character.
  144. *
  145. *********************************************************************/
  146. static enum State
  147. GetNextState (
  148. char c,
  149. wchar_t wide_c,
  150. int len,
  151. char *str,
  152. char *ret_c,
  153. wchar_t *ret_wc,
  154. int *ret_wc_len,
  155. char **ret_str_ptr,
  156. Boolean *ret_flag )
  157. {
  158. register int i = len;
  159. /* check to see if we have the pattern of a bold + followed by a bold _
  160. purpose is to eventually convert this to +/-
  161. This is a hack because the nroff output produced for the sequence
  162. of a + with an _ UNDER it, as in indicating statistical significance,
  163. looks like a bold +, backspace, and then a bold _.
  164. However, the nroff output indicates <c>bs_ as an italic, so
  165. this sequence has to be treated as a special case. */
  166. if (*str == '+')
  167. {
  168. if (str[7] == '\010' && str[8] == '_' &&
  169. str[9] == '\010' && str[10] == '_' &&
  170. str[11] == '\010' && str[12] == '_' &&
  171. str[13] == '\010' && str[14] == '_' )
  172. {
  173. *ret_flag = TRUE;
  174. }
  175. }
  176. /* We need to eat the bold '_' -- continuation of aforementioned hack */
  177. if (TRUE == *ret_flag)
  178. {
  179. if (c == '+' &&
  180. str[0] == '\010' && str[1] == '_' &&
  181. str[2] == '\010' && str[3] == '_' &&
  182. str[4] == '\010' && str[5] == '_' &&
  183. str[6] == '\010' && str[7] == '_' )
  184. {
  185. *ret_flag = FALSE;
  186. *ret_str_ptr = str + 8;
  187. return Bold;
  188. }
  189. }
  190. /*
  191. * check for multi-byte italic which is '_ _bsbs<c>'
  192. */
  193. if (len == 1 && c == '_' && mblen (str, MB_CUR_MAX) == 1 && *str == '_')
  194. {
  195. str++;
  196. i++;
  197. }
  198. /*
  199. * check for backspaces equal to the number of bytes making up wide_c.
  200. */
  201. while (i)
  202. {
  203. if (mblen (str, MB_CUR_MAX) != 1 || *str != '\010')
  204. return Char;
  205. i--;
  206. str++;
  207. }
  208. /*
  209. * convert the next character
  210. */
  211. *ret_wc_len = mbtowc(ret_wc, str, MB_CUR_MAX);
  212. /*
  213. * check for validity
  214. */
  215. if ((*ret_wc_len) < 1)
  216. return Char;
  217. /*
  218. * check to make sure the next character is equal to the first character
  219. * if so, the sequence was <c>bs<c> indicating a bold character.
  220. */
  221. if (*ret_wc_len == len && *ret_wc == wide_c)
  222. {
  223. /*
  224. * Do not set the return string ptr prior to this return.
  225. * If the user specifies the same variable for both the str and
  226. * ret_str_ptr, then we will overwrite it improperly if the
  227. * sequence is not an italic or bold.
  228. */
  229. *ret_str_ptr = str + *ret_wc_len;
  230. return Bold;
  231. }
  232. /*
  233. * Check to see if the sequence is _bs<c> for italics.
  234. * If so, take the next character for the ret_c (even if it
  235. * is a multi-byte character). ret_wc, and ret_wc_len are
  236. * already set from the mbtowc call. Move the ptr to after
  237. * the processed character.
  238. */
  239. if (len == 1 && c == '_')
  240. {
  241. *ret_c = *str;
  242. *ret_str_ptr = str + *ret_wc_len;
  243. return Italic;
  244. }
  245. /*
  246. * Check to see if the sequence is <c>bs_ for italics.
  247. * If so, return the given c for the ret_c (even if it
  248. * is a multi-byte character). Set ret_wc and ret_wc_len
  249. * to the passed in wide character and wide character len.
  250. * Move the ptr to after the underscore.
  251. */
  252. if (*ret_wc_len == 1 && *str == '_')
  253. {
  254. *ret_c = c;
  255. *ret_str_ptr = str + *ret_wc_len;
  256. *ret_wc = wide_c;
  257. *ret_wc_len = len;
  258. return Italic;
  259. }
  260. /*
  261. * failed all the tests.
  262. * It could be a character followed by backspaces
  263. * followed by another character (which would cause over-
  264. * striking), but we don't know how to deal with this.
  265. */
  266. return Char;
  267. }
  268. /******************************************************************************
  269. *
  270. * Formatting a man page is done based on the current input and the
  271. * current state of the processing.
  272. *
  273. * Note:
  274. * Bold multi-byte characters have one backspace per byte. Therefore,
  275. * a two-byte character would be be followed by two backspaces
  276. * and the same two-bytes to indicate that the character is to
  277. * be a bold character.
  278. * Italic characters can either be preceeded by a backspace/underline
  279. * or followed by a backspace/underline. I.E. _bsAB<bs_
  280. * would be an italic capitol A, a regular capital B and
  281. * an italic less than sign.
  282. * All backslashes and less than signs are output with a backslash
  283. * preceeding them.
  284. *
  285. * The following characters have special meaning in the state table:
  286. * <c> indicates a character.
  287. * <?> indicates the next character in the sequence.
  288. * <_> indicates an underscore character.
  289. * bs indicates a backspace character.
  290. * nl indicates a new-line character.
  291. *
  292. * Cur State Cur Sequence Action New State
  293. * ------------------------------------------------------------------------
  294. * Char <c>bs<c><?> Bold -> buf Bold
  295. * <c> -> buf
  296. * lastChar = <c>
  297. * incr to <?>
  298. *
  299. * <_>bs<c><?> Italic -> buf Italic
  300. * <c> -> buf
  301. * incr to <?>
  302. *
  303. * <c>bs<_><?> Italic -> buf Italic
  304. * <c> -> buf
  305. * incr to <?>
  306. *
  307. * nl<?> dump buf Char
  308. * incr to <?>
  309. *
  310. * <c><?> <c> -> buf Char
  311. * incr to <b>
  312. *
  313. * Bold bs<c><?>
  314. * <c> = lastChar skip to <?> Bold
  315. *
  316. * nl<?> <\> -> buf Char
  317. * dump buf
  318. * incr to <?>
  319. *
  320. * <c><?> <\> -> buf BoldDone
  321. *
  322. * BoldDone <c>bs<c><?> erase <\> Bold
  323. * <c> -> buf
  324. * lastChar = <c>
  325. * incr to <?>
  326. *
  327. * <_>bs<c><?> Italic -> buf Italic
  328. * <c> -> buf
  329. * incr to <?>
  330. *
  331. * <c>bs<_><?> Italic -> buf Italic
  332. * <c> -> buf
  333. * incr to <?>
  334. *
  335. * nl<?> dump buf Char
  336. * incr to <?>
  337. *
  338. * <c><?> <c> -> buf Char
  339. * incr to <?>
  340. *
  341. * Italic <c>bs<c><?> <\> -> buf Char
  342. *
  343. * <_>bs<c><?> <c> -> buf Italic
  344. * incr to <?>
  345. *
  346. * <c>bs<_><?> <c> -> buf Italic
  347. * incr to <?>
  348. *
  349. * bs<c><?> erase lastChar BoldItalic
  350. * Bold -> buf
  351. * <c> -> buf
  352. * lastChar = <c>
  353. * incr to <?>
  354. *
  355. * nl<?> <\> -> buf Char
  356. * dump buf
  357. * incr to <?>
  358. *
  359. * <c><?> </> -> buf Char
  360. *
  361. * BoldItalic bs<c><?> skip to <?> BoldItalic
  362. *
  363. * <c><?> <\> -> buf BoldItalicDone
  364. *
  365. * BoldItalicDone _bs<c>bs<c><?> erase <\> ItalicBold
  366. * <c> -> buf
  367. * lastChar = <c>
  368. * inc to <?>
  369. *
  370. * _bs<c><?> <c> -> buf Italic
  371. * lastChar = <c>
  372. * inc to <?>
  373. *
  374. * <c><?> <\> ->buf Char
  375. * <c> -> buf
  376. * lastChar = <c>
  377. * inc to <?>
  378. *
  379. ******************************************************************************/
  380. /******************************************************************************
  381. * Function: RemoveToken
  382. ******************************************************************************/
  383. static void
  384. RemoveToken(
  385. int size,
  386. char **out_buf,
  387. int *out_size)
  388. {
  389. *out_size = *out_size - size;
  390. if (size == 1 &&
  391. ((*out_buf)[*out_size] == '\\' || (*out_buf)[*out_size] == '<'))
  392. *out_size = *out_size - 1;
  393. (*out_buf)[*out_size] = '\0';
  394. }
  395. /*********************************************************************
  396. * Function: WriteToken
  397. *********************************************************************/
  398. static int
  399. WriteToken(
  400. char *token,
  401. int size,
  402. char **out_buf,
  403. int *out_size,
  404. int *out_max )
  405. {
  406. char *ptr = token;
  407. return (_DtHelpCeAddStrToBuf(&ptr, out_buf, out_size, out_max, size, 128));
  408. }
  409. /*********************************************************************
  410. * Function: WriteOutChar
  411. *********************************************************************/
  412. static int
  413. WriteOutChar(
  414. int last_len,
  415. wchar_t last_wc,
  416. char c,
  417. char **out_buf,
  418. int *out_size,
  419. int *out_max,
  420. Boolean flag)
  421. {
  422. char *ptr;
  423. char temp[10];
  424. if (last_len == 1 && c == '<')
  425. ptr = "\\<";
  426. else if (last_len == 1 && c == '\\')
  427. ptr = "\\\\";
  428. else
  429. {
  430. (void) wctomb (temp, last_wc);
  431. temp[last_len] = '\0';
  432. ptr = temp;
  433. }
  434. /* here is the conversion for the bold '+' followed by bold '_'
  435. part of a hack to treat this special character sequence */
  436. if (flag && c == '+')
  437. ptr = "+/-";
  438. return (_DtHelpCeAddStrToBuf(&ptr, out_buf, out_size, out_max,
  439. strlen(ptr), 128));
  440. }
  441. /*********************************************************************
  442. * Function: FormatManPage
  443. *
  444. * FormatManPage is the top entry point for formating man pages
  445. * into a form understood by a display area.
  446. *
  447. *********************************************************************/
  448. static int
  449. FormatManPage(
  450. VarHandle my_vars,
  451. BufFilePtr in_file,
  452. char *in_buf,
  453. int in_size,
  454. _DtHelpFontHints *font_attr,
  455. char **out_buf,
  456. int *out_size,
  457. int *out_max )
  458. {
  459. int italicCnt = 0;
  460. int result = 0;
  461. int cread;
  462. int lastLen;
  463. int checkLen;
  464. int retWCLen;
  465. wchar_t lastWC;
  466. wchar_t retWC;
  467. char *rloc = in_buf;
  468. char *retStrPtr;
  469. char c;
  470. char retC;
  471. Boolean flag = False;
  472. enum State newState;
  473. enum State state = Char;
  474. cread = strlen (rloc);
  475. do
  476. {
  477. /*
  478. * while I can read information process; loop.
  479. */
  480. while (result != -1 && cread > 0)
  481. {
  482. /**
  483. * check for the size of the character
  484. **/
  485. checkLen = mblen(rloc, MB_CUR_MAX);
  486. /*
  487. * if we hit a null character before we've run out of characters,
  488. * we've got corrupt data.
  489. */
  490. if (checkLen == 0)
  491. return -1;
  492. if (checkLen > 0)
  493. {
  494. /*
  495. * check for end of line
  496. */
  497. if (checkLen == 1 && *rloc == '\n')
  498. {
  499. cread--;
  500. if (state == Bold || state == Italic)
  501. result = WriteToken(EndToken, EndTokenSize,
  502. out_buf, out_size, out_max);
  503. if (result != -1)
  504. result = _DtHelpCeAddCharToBuf (&rloc, out_buf,
  505. out_size, out_max, 128);
  506. if (result != -1)
  507. {
  508. result = __DtHelpCeProcessString(
  509. my_vars, NULL,
  510. _DtCvLITERAL,
  511. ScanString,
  512. *out_buf,
  513. *out_size,
  514. 0,
  515. False,
  516. font_attr);
  517. *out_size = 0;
  518. (*out_buf)[0] = '\0';
  519. state = Char;
  520. }
  521. }
  522. else
  523. {
  524. switch (state)
  525. {
  526. case Char:
  527. case BoldDone:
  528. case BoldItalicDone:
  529. /*
  530. * get the character and wide character
  531. * representation of the next character.
  532. */
  533. c = *rloc;
  534. lastLen = mbtowc (&lastWC, rloc, MB_CUR_MAX);
  535. /*
  536. * skip past this character.
  537. */
  538. rloc = rloc + lastLen;
  539. cread = cread - lastLen;
  540. /*
  541. * Check ahead for bold or italic sequences
  542. */
  543. newState = GetNextState (c, lastWC, lastLen,
  544. rloc,
  545. &retC, &retWC, &retWCLen,
  546. &retStrPtr, &flag);
  547. if (newState == Bold)
  548. {
  549. if (state == BoldDone)
  550. RemoveToken(EndTokenSize, out_buf, out_size);
  551. else
  552. result = WriteToken(BoldToken,
  553. BoldTokenSize,
  554. out_buf, out_size,
  555. out_max);
  556. /*
  557. * skip the backspaces and the extra
  558. * copy of the character.
  559. */
  560. cread = cread - (retStrPtr - rloc);
  561. rloc = retStrPtr;
  562. }
  563. else if (newState == Italic)
  564. {
  565. if (state != BoldItalicDone)
  566. result = WriteToken(ItalicToken,
  567. ItalicTokenSize,
  568. out_buf, out_size,
  569. out_max);
  570. /*
  571. * skip the blanks after the current
  572. * character plus the character after
  573. * that. The returned wide character
  574. * is the character that is to be
  575. * italicized.
  576. */
  577. cread = cread - (retStrPtr - rloc);
  578. rloc = retStrPtr;
  579. c = retC;
  580. lastWC = retWC;
  581. lastLen = retWCLen;
  582. italicCnt = 1;
  583. if (state == BoldItalicDone &&
  584. GetNextState (c, lastWC, lastLen, rloc,
  585. &retC, &retWC, &retWCLen,
  586. &retStrPtr, &flag) == Bold)
  587. {
  588. RemoveToken(EndTokenSize, out_buf, out_size);
  589. newState = BoldItalic;
  590. }
  591. }
  592. else if (state == BoldItalicDone)
  593. result = WriteToken(EndToken, EndTokenSize,
  594. out_buf, out_size,
  595. out_max);
  596. state = newState;
  597. result = WriteOutChar(lastLen, lastWC, c,
  598. out_buf, out_size, out_max, flag);
  599. break;
  600. case BoldItalic:
  601. case Bold:
  602. if (GetNextState (c, lastWC, lastLen, rloc,
  603. &retC, &retWC, &retWCLen,
  604. &retStrPtr, &flag) == Bold)
  605. {
  606. /* skip backspaces and copy characters */
  607. cread = cread - (retStrPtr - rloc);
  608. rloc = retStrPtr;
  609. }
  610. else
  611. {
  612. result = WriteToken(EndToken, EndTokenSize,
  613. out_buf, out_size, out_max);
  614. if (state == BoldItalic)
  615. state = BoldItalicDone;
  616. else
  617. state = BoldDone;
  618. }
  619. break;
  620. case Italic:
  621. c = *rloc;
  622. newState = GetNextState (c, lastWC, lastLen,
  623. rloc,
  624. &retC, &retWC, &retWCLen,
  625. &retStrPtr, &flag);
  626. if (newState == Italic)
  627. {
  628. italicCnt++;
  629. cread = cread - (retStrPtr - rloc);
  630. rloc = retStrPtr;
  631. c = retC;
  632. lastWC = retWC;
  633. lastLen = retWCLen;
  634. result = WriteOutChar(lastLen, lastWC, c,
  635. out_buf, out_size, out_max, flag);
  636. }
  637. else if (italicCnt == 1 && lastWC == retWC
  638. && newState == Bold)
  639. {
  640. RemoveToken(lastLen, out_buf, out_size);
  641. result = WriteToken(BoldToken,
  642. BoldTokenSize,
  643. out_buf, out_size,
  644. out_max);
  645. cread = cread - (retStrPtr - rloc);
  646. rloc = retStrPtr;
  647. c = retC;
  648. lastWC = retWC;
  649. lastLen = retWCLen;
  650. result = WriteOutChar(lastLen, lastWC, c,
  651. out_buf, out_size, out_max, flag);
  652. state = BoldItalic;
  653. }
  654. else
  655. {
  656. result = WriteToken(EndToken,
  657. EndTokenSize,
  658. out_buf, out_size,
  659. out_max);
  660. state = Char;
  661. italicCnt = 0;
  662. }
  663. break;
  664. }
  665. }
  666. if (cread < (3 * ((int) MB_CUR_MAX)) &&
  667. !feof (FileStream(in_file)))
  668. cread = 0;
  669. }
  670. else
  671. {
  672. /**
  673. * if it is an invalid character - skip.
  674. * But be careful.
  675. * If this is the start of a multi-byte character,
  676. * I must save it and try again on the next read.
  677. **/
  678. if (cread < ((int) MB_CUR_MAX))
  679. cread = 0;
  680. else
  681. {
  682. /*
  683. * otherwise we've got corrupt data.
  684. */
  685. return -1;
  686. }
  687. }
  688. }
  689. if (result != -1 && !feof(FileStream(in_file)))
  690. {
  691. if (_DtHelpCeGetNxtBuf (in_file, in_buf, &rloc, in_size) == -1)
  692. result = -1;
  693. if (result != -1)
  694. cread = strlen (rloc);
  695. }
  696. } while (result != -1 && cread > 0);
  697. return(result);
  698. } /* End FormatManPage */
  699. /******************************************************************************
  700. *
  701. * Public Functions
  702. *
  703. *****************************************************************************/
  704. /*********************************************************************
  705. * Function: _DtHelpFormatManPage
  706. *
  707. * _DtHelpFormatManPage formats a man page
  708. * into a form understood by a display area.
  709. *
  710. *********************************************************************/
  711. int
  712. _DtHelpFormatManPage(
  713. XtPointer client_data,
  714. char *man_spec,
  715. XtPointer *ret_handle)
  716. {
  717. int result = -1;
  718. FILE *myFile;
  719. int writeBufSize = 0;
  720. int writeBufMax = 0;
  721. char *ptr;
  722. char *writeBuf = NULL;
  723. char readBuf[BUFSIZ];
  724. _DtHelpFontHints fontAttr;
  725. VarHandle myVars;
  726. _DtCvTopicInfo *topicStruct;
  727. BufFilePtr myBufFile;
  728. static char manString[] = "man ";
  729. static char devNullString[] = " 2>/dev/null";
  730. _DtCvTopicPtr topic = NULL;
  731. DtHelpDispAreaStruct *pDAS = (DtHelpDispAreaStruct *) client_data;
  732. _FrmtUiInfo myUiInfo = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 1, False };
  733. /*
  734. * fill out the ui information
  735. */
  736. myUiInfo.load_font = _DtHelpDAResolveFont;
  737. myUiInfo.client_data = (_DtCvPointer) pDAS;
  738. myUiInfo.avg_char = (int)(pDAS->charWidth / 10 +
  739. ((pDAS->charWidth % 10) ? 1 : 0));
  740. myUiInfo.nl_to_space = pDAS->nl_to_space;
  741. /*
  742. * pre-append the man command to man specification
  743. */
  744. ptr = (char *) malloc(sizeof(manString) + strlen(man_spec) +
  745. sizeof(devNullString) - 1);
  746. if (!ptr)
  747. return -1;
  748. strcpy (ptr, manString);
  749. strcat (ptr, man_spec);
  750. strcat (ptr, devNullString);
  751. myFile = popen(ptr, "r");
  752. /*
  753. * free the man command
  754. */
  755. free (ptr);
  756. /*
  757. * check for problems
  758. */
  759. if (!myFile) /* couldn't create man(1) process */
  760. return -1;
  761. /*
  762. * make sure we don't try to read compressed.
  763. */
  764. myBufFile = _DtHelpCeCreatePipeBufFile(myFile);
  765. if (myBufFile == NULL)
  766. {
  767. (void) pclose(myFile); /* don't check for error, it was popen'd */
  768. return -1;
  769. }
  770. /*
  771. * get the font quark list - but force to mono-space
  772. */
  773. _DtHelpCeCopyDefFontAttrList (&fontAttr);
  774. fontAttr.spacing = _DtHelpFontSpacingMono;
  775. _DtHelpCeXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,setlocale(LC_CTYPE,NULL),
  776. NULL, &(fontAttr.language), &(fontAttr.char_set));
  777. myVars = __DtHelpCeSetUpVars(fontAttr.language, fontAttr.char_set, &myUiInfo);
  778. if (myVars == NULL)
  779. {
  780. free(fontAttr.language);
  781. free(fontAttr.char_set);
  782. _DtHelpCeBufFileClose (myBufFile, True);
  783. return -1;
  784. }
  785. readBuf[0] = '\0';
  786. ptr = readBuf;
  787. result = _DtHelpCeGetNxtBuf (myBufFile, readBuf, &ptr, BUFSIZ);
  788. if (result > 0)
  789. result = FormatManPage (myVars, myBufFile,
  790. readBuf,
  791. BUFSIZ,
  792. &fontAttr,
  793. &writeBuf,
  794. &writeBufSize,
  795. &writeBufMax );
  796. if ((result != -1) && writeBufSize)
  797. result = __DtHelpCeProcessString(myVars, NULL, _DtCvLITERAL,
  798. ScanString, writeBuf, writeBufSize,
  799. 0, False, &fontAttr);
  800. /*
  801. * free the buffer
  802. */
  803. if (writeBuf)
  804. free (writeBuf);
  805. /*
  806. * close the pipe
  807. */
  808. _DtHelpCeBufFileClose (myBufFile, True);
  809. /*
  810. * clean up the last segment.
  811. */
  812. if (result != -1)
  813. __DtHelpCeGetParagraphList (myVars, True, _DtCvLITERAL, &topic);
  814. topicStruct = (_DtCvTopicInfo *) (topic);
  815. /*
  816. * did we have any paragraphs to format?
  817. */
  818. if ((topic != NULL && NULL == topicStruct->seg_list)
  819. || NULL == _DtCvContainerListOfSeg(topicStruct->seg_list))
  820. {
  821. _DtHelpFreeSegments(topicStruct->seg_list, _DtCvFALSE,
  822. NULL, (_DtCvPointer) pDAS);
  823. free ((char *) topicStruct);
  824. topic = NULL;
  825. errno = ENOENT; /* we'll just assume no man page existed */
  826. result = -1;
  827. }
  828. free(fontAttr.language);
  829. free(fontAttr.char_set);
  830. free(myVars);
  831. *ret_handle = (_DtCvPointer) topic;
  832. return (result);
  833. } /* End _DtHelpFormatManPage */