FormatMan.c 23 KB

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