コード例 #1
0
ファイル: Format.c プロジェクト: idunham/cdesktop
/*****************************************************************************
 * Function:	int _DtHelpFormatAsciiStringDynamic (char *input_string,
 *					CEParagraph **ret_para, int *ret_num)
 *
 * Parameters:
 *		input_string	Specifies the ascii string to format.
 *		ret_para	Returns a pointer to a list of CEParagraph
 *				structures.
 *		ret_num		Returns the number of structures in 'ret_para'.
 *
 * Returns:	0 if successful, -1 if errors
 *
 * errno Values:
 *
 * Purpose:	_DtHelpFormatAsciiStringDynamic formats a string as if it were
 *		a dynamic string - it uses newline characters to terminate
 *		the current paragraph, not a line.
 *
 *****************************************************************************/
int
_DtHelpFormatAsciiStringDynamic(
	XtPointer	  client_data,
	char		 *input_string,
	XtPointer	 *ret_handle)
{
    int      result = -1;
    _DtHelpFontHints fontAttrs;
    XtPointer varHandle;
    _DtCvTopicPtr	  topic    = NULL;
    DtHelpDispAreaStruct *pDAS     = (DtHelpDispAreaStruct *) client_data;
    _FrmtUiInfo           myUiInfo = defUiInfo;

    /*
     * check the parameters.
     */
    if (input_string == NULL || ret_handle == NULL)
      {
	errno = EINVAL;
	return -1;
      }

    /*
     * fill out the ui information
     */
    myUiInfo.load_font    = _DtHelpDAResolveFont;
    myUiInfo.client_data  = (_DtCvPointer) pDAS;
    myUiInfo.line_width   = pDAS->lineThickness;
    myUiInfo.line_height  = pDAS->lineHeight;
    myUiInfo.leading      = pDAS->leading;
    myUiInfo.avg_char     = (int)(pDAS->charWidth / 10 +
				    ((pDAS->charWidth % 10) ? 1 : 0));
    myUiInfo.nl_to_space  = pDAS->nl_to_space;

    /*
     * Fake the flag and give the string as the input buffer.
     */
    *ret_handle  = NULL;

    _DtHelpCeCopyDefFontAttrList (&fontAttrs);
    _DtHelpCeXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,setlocale(LC_CTYPE,NULL),
				NULL, &(fontAttrs.language),
				&(fontAttrs.char_set));

    varHandle = __DtHelpCeSetUpVars(fontAttrs.language, fontAttrs.char_set,
						&myUiInfo);
    if (varHandle == NULL)
      {
        free(fontAttrs.language);
        free(fontAttrs.char_set);
	return -1;
      }

    result = __DtHelpCeProcessString (varHandle, NULL,
				_DtCvDYNAMIC,
				ScanString, input_string,
				strlen(input_string),
				0, True,
				&fontAttrs);

    if (result != -1)
	result = __DtHelpCeGetParagraphList (varHandle, False, _DtCvDYNAMIC,
								&topic);
    *ret_handle = (XtPointer) topic;

    free(fontAttrs.language);
    free(fontAttrs.char_set);
    free(varHandle);

    return result;

}  /* End _DtHelpFormatAsciiStringDynamic */
コード例 #2
0
ファイル: FormatMan.c プロジェクト: arcfide/CDesktopEnv
/*********************************************************************
 * Function: _DtHelpFormatManPage
 *
 *    _DtHelpFormatManPage formats a man page
 *	into a form understood by a display area.
 *
 *********************************************************************/
int
_DtHelpFormatManPage(
	XtPointer	 client_data,
	char		*man_spec,
	XtPointer	*ret_handle)
{
    int      result = -1;
    FILE    *myFile;
    int      writeBufSize = 0;
    int      writeBufMax  = 0;
    char    *ptr;
    char    *writeBuf = NULL;
    char     readBuf[BUFSIZ];
    _DtHelpFontHints    fontAttr;
    VarHandle  myVars;
    _DtCvTopicInfo *topicStruct;
    BufFilePtr myBufFile;
    static char manString[]     = "man ";
    static char devNullString[] = " 2>/dev/null";
    _DtCvTopicPtr topic = NULL;
    DtHelpDispAreaStruct *pDAS     = (DtHelpDispAreaStruct *) client_data;
    _FrmtUiInfo		  myUiInfo = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 1, False };

    /*
     * fill out the ui information
     */
    myUiInfo.load_font    = _DtHelpDAResolveFont;
    myUiInfo.client_data  = (_DtCvPointer) pDAS;
    myUiInfo.avg_char     = (int)(pDAS->charWidth / 10 +
					((pDAS->charWidth % 10) ? 1 : 0));
    myUiInfo.nl_to_space  = pDAS->nl_to_space;

    /*
     * pre-append the man command to man specification
     */
    ptr = (char *) malloc(sizeof(manString) + strlen(man_spec) +
						   sizeof(devNullString) - 1);
    if (!ptr)
	return -1;
    strcpy (ptr, manString);
    strcat (ptr, man_spec);
    strcat (ptr, devNullString);

    myFile = popen(ptr, "r");

    /*
     * free the man command
     */
    free (ptr);

    /*
     * check for problems
     */
    if (!myFile) /* couldn't create man(1) process */
	return -1;

    /*
     * make sure we don't try to read compressed.
     */
    myBufFile = _DtHelpCeCreatePipeBufFile(myFile);
    if (myBufFile == NULL)
      {
	(void) pclose(myFile); /* don't check for error, it was popen'd */
	return -1;
      }

    /*
     * get the font quark list - but force to mono-space
     */
    _DtHelpCeCopyDefFontAttrList (&fontAttr);
    fontAttr.spacing  = _DtHelpFontSpacingMono;
    _DtHelpCeXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,setlocale(LC_CTYPE,NULL),
			NULL, &(fontAttr.language), &(fontAttr.char_set));

    myVars = __DtHelpCeSetUpVars(fontAttr.language, fontAttr.char_set, &myUiInfo);
    if (myVars == NULL)
      {
	free(fontAttr.language);
	free(fontAttr.char_set);
	_DtHelpCeBufFileClose (myBufFile, True);
	return -1;
      }

    readBuf[0] = '\0';
    ptr        = readBuf;

    result = _DtHelpCeGetNxtBuf (myBufFile, readBuf, &ptr, BUFSIZ);
    if (result > 0)
	 result = FormatManPage (myVars, myBufFile,
				     readBuf,
				     BUFSIZ,
				     &fontAttr,
				     &writeBuf,
				     &writeBufSize,
				     &writeBufMax );

    if ((result != -1) && writeBufSize)
	result = __DtHelpCeProcessString(myVars, NULL, _DtCvLITERAL,
			ScanString, writeBuf, writeBufSize,
			0, False, &fontAttr);

    /*
     * free the buffer
     */
    if (writeBuf)
        free (writeBuf);

    /*
     * close the pipe
     */
    _DtHelpCeBufFileClose (myBufFile, True);

    /*
     * clean up the last segment.
     */
    if (result != -1)
	__DtHelpCeGetParagraphList (myVars, True, _DtCvLITERAL, &topic);

    topicStruct = (_DtCvTopicInfo *) (topic);

   /*
    * did we have any paragraphs to format?
    */
   if (topic != NULL && NULL == topicStruct->seg_list
		|| NULL == _DtCvContainerListOfSeg(topicStruct->seg_list))
      {
	_DtHelpFreeSegments(topicStruct->seg_list, _DtCvFALSE,
						NULL, (_DtCvPointer) pDAS);
	free ((char *) topicStruct);
	topic = NULL;
	errno = ENOENT; /* we'll just assume no man page existed */
	result = -1;
      }

    free(fontAttr.language);
    free(fontAttr.char_set);
    free(myVars);

    *ret_handle = (_DtCvPointer) topic;

    return (result);

}  /* End _DtHelpFormatManPage */
コード例 #3
0
ファイル: Format.c プロジェクト: idunham/cdesktop
/*****************************************************************************
 * Function:	int _DtHelpFormatAsciiFile (char *filename,
 *					CEParagraph **ret_para, int *ret_num)
 *
 * Parameters:
 *		filename	Specifies the ascii file to read.
 *		ret_para	Returns a pointer to a list of CEParagraph
 *				structures.
 *		ret_num		Returns the number of structures in 'ret_para'.
 *
 * Returns:	0 if successful, -1 if errors
 *
 * errno Values:
 *		EINVAL
 *
 * Purpose:	_DtHelpFormatAsciiFile formats Ascii Files into a list of
 *		CEParagraph structures.
 *
 *****************************************************************************/
int
_DtHelpFormatAsciiFile(
	XtPointer	  client_data,
	char		 *filename,
	XtPointer	 *ret_handle)
{
    int	       myFile;
    int	       result = -1;
    _DtHelpFontHints fontAttrs;
    char       buffer [BUFF_SIZE];
    BufFilePtr rawInput;
    XtPointer   varHandle;
    _DtCvTopicPtr	  topic    = NULL;
    DtHelpDispAreaStruct *pDAS     = (DtHelpDispAreaStruct *) client_data;
    _FrmtUiInfo           myUiInfo = defUiInfo;

    /*
     * check the parameters
     */
    if (filename == NULL || ret_handle == NULL)
      {
	errno = EINVAL;
	return -1;
      }

    /*
     * Initialize the pointers, buffers and counters
     */
    *ret_handle  = NULL;

    /*
     * open the file.
     */
    myFile = open (filename, O_RDONLY);
    if (myFile != -1)
      {
	/*
	 * set the information
	 */
	rawInput = _DtHelpCeBufFileRdWithFd(myFile);
	if (rawInput == 0)
	  {
	    close (myFile);
	    return -1;
	  }

        result = _DtHelpCeReadBuf (rawInput, buffer, BUFF_SIZE);

	if (result != -1)
	  {
	    _DtHelpCeCopyDefFontAttrList (&fontAttrs);
	    fontAttrs.spacing = _DtHelpFontSpacingMono;
	    _DtHelpCeXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
				setlocale(LC_CTYPE,NULL), NULL,
				&(fontAttrs.language), &(fontAttrs.char_set));

	    /*
	     * fill out the ui information
	     */
	    myUiInfo.load_font    = _DtHelpDAResolveFont;
	    myUiInfo.client_data  = (_DtCvPointer) pDAS;
	    myUiInfo.line_width   = pDAS->lineThickness;
	    myUiInfo.line_height  = pDAS->lineHeight;
	    myUiInfo.leading      = pDAS->leading;
	    myUiInfo.avg_char     = (int)(pDAS->charWidth / 10 +
					((pDAS->charWidth % 10) ? 1 : 0));
	    myUiInfo.nl_to_space  = pDAS->nl_to_space;

	    /*
	     * get the formatting structure.
	     */
	    varHandle = __DtHelpCeSetUpVars(fontAttrs.language,
						fontAttrs.char_set, &myUiInfo);
	    if (varHandle == NULL)
	      {
	        free(fontAttrs.language);
	        free(fontAttrs.char_set);
		return -1;
	      }

	    result = __DtHelpCeProcessString (varHandle, rawInput,
				_DtCvLITERAL,
				ScanString, buffer, BUFF_SIZE,
				0, False, &fontAttrs);

	    if (result != -1)
		result = __DtHelpCeGetParagraphList (varHandle, True,
						_DtCvLITERAL,
						&topic);

	    free(fontAttrs.language);
	    free(fontAttrs.char_set);
	    free(varHandle);
	  }

	_DtHelpCeBufFileClose(rawInput, True);
      }

    *ret_handle = (XtPointer) topic;
    return result;

}  /* End _DtHelpFormatAsciiFile */
コード例 #4
0
ファイル: FormatMan.c プロジェクト: arcfide/CDesktopEnv
/*********************************************************************
 * Function: FormatManPage
 *
 *    FormatManPage is the top entry point for formating man pages
 *	into a form understood by a display area.
 *
 *********************************************************************/
static	int
FormatManPage(
	VarHandle	  my_vars,
	BufFilePtr	  in_file,
	char		 *in_buf,
	int		  in_size,
	_DtHelpFontHints	*font_attr,
	char		**out_buf,
	int		 *out_size,
	int		 *out_max )
{
    int		 italicCnt = 0;
    int		 result = 0;
    int		 cread;
    int		 lastLen;
    int		 checkLen;
    int		 retWCLen;
    wchar_t	 lastWC;
    wchar_t	 retWC;
    char	*rloc = in_buf;
    char	*retStrPtr;
    char	 c;
    char	 retC;
    Boolean      flag = False;
    enum State	 newState;
    enum State	 state = Char;

    cread = strlen (rloc);

    do
      {
	/*
	 * while I can read information process; loop.
	 */
	while (result != -1 && cread > 0)
	  {
	    /**
	     * check for the size of the character
	     **/
	    checkLen = mblen(rloc, MB_CUR_MAX);

	    /*
	     * if we hit a null character before we've run out of characters,
	     * we've got corrupt data.
	     */
	    if (checkLen == 0)
		return -1;

	    if (checkLen > 0)
	      {
		/*
		 * check for end of line
		 */
		if (checkLen == 1 && *rloc == '\n')
		  {
		    cread--;
		    if (state == Bold || state == Italic)
		        result = WriteToken(EndToken, EndTokenSize,
						out_buf, out_size, out_max);

		    if (result != -1)
		        result = _DtHelpCeAddCharToBuf (&rloc, out_buf,
						out_size, out_max, 128);
		    if (result != -1)
		      {
		        result = __DtHelpCeProcessString(
					my_vars, NULL,
					_DtCvLITERAL,
					ScanString,
					*out_buf,
					*out_size,
					0,
					False,
					font_attr);
		        *out_size = 0;
		        (*out_buf)[0] = '\0';
		        state = Char;
		      }
		  }
		else
		  {
		    switch (state)
		      {
			case Char:
			case BoldDone:
			case BoldItalicDone:
				/*
				 * get the character and wide character
				 * representation of the next character.
				 */
				c        = *rloc;
				lastLen  = mbtowc (&lastWC, rloc, MB_CUR_MAX);

				/*
				 * skip past this character.
				 */
				rloc     = rloc  + lastLen;
				cread    = cread - lastLen;

				/*
				 * Check ahead for bold or italic sequences
				 */
				newState = GetNextState (c, lastWC, lastLen,
						rloc,
						&retC, &retWC, &retWCLen,
						&retStrPtr, &flag);

				if (newState == Bold)
				  {
				    if (state == BoldDone)
					RemoveToken(EndTokenSize, out_buf, out_size);
				    else
					result = WriteToken(BoldToken,
							BoldTokenSize,
							out_buf, out_size,
							out_max);

				    /*
				     * skip the backspaces and the extra
				     * copy of the character.
				     */
				    cread = cread - (retStrPtr - rloc);
				    rloc  = retStrPtr;
				  }
				else if (newState == Italic)
				  {
				    if (state != BoldItalicDone)
					result = WriteToken(ItalicToken,
							ItalicTokenSize,
							out_buf, out_size,
							out_max);

				    /*
				     * skip the blanks after the current
				     * character plus the character after
				     * that. The returned wide character
				     * is the character that is to be
				     * italicized.
				     */
				    cread   = cread - (retStrPtr - rloc);
				    rloc    = retStrPtr;
				    c       = retC;
				    lastWC  = retWC;
				    lastLen = retWCLen;
				    italicCnt = 1;

				    if (state == BoldItalicDone &&
					GetNextState (c, lastWC, lastLen, rloc,
						&retC, &retWC, &retWCLen,
						&retStrPtr, &flag) == Bold)
				      {
					RemoveToken(EndTokenSize, out_buf, out_size);
					newState = BoldItalic;
				      }
				  }
				else if (state == BoldItalicDone)
				    result = WriteToken(EndToken, EndTokenSize,
							out_buf, out_size,
							out_max);

				state = newState;


				result = WriteOutChar(lastLen, lastWC, c,
						out_buf, out_size, out_max, flag);
				break;

			case BoldItalic:
			case Bold:
				if (GetNextState (c, lastWC, lastLen, rloc,
						&retC, &retWC, &retWCLen,
						&retStrPtr, &flag) == Bold)
				  {
				  /* skip backspaces and copy characters */
				    cread = cread - (retStrPtr - rloc);
				    rloc  = retStrPtr;
				  }
				else
				  {
				    result = WriteToken(EndToken, EndTokenSize,
						out_buf, out_size, out_max);
				    if (state == BoldItalic)
				        state = BoldItalicDone;
				    else
				        state = BoldDone;
				  }
				break;

			case Italic:
				c = *rloc;
				newState = GetNextState (c, lastWC, lastLen,
						rloc,
						&retC, &retWC, &retWCLen,
						&retStrPtr, &flag);

				if (newState == Italic)
				  {
				    italicCnt++;
				    cread   = cread - (retStrPtr - rloc);
				    rloc    = retStrPtr;
				    c       = retC;
				    lastWC  = retWC;
				    lastLen = retWCLen;

				    result = WriteOutChar(lastLen, lastWC, c,
						out_buf, out_size, out_max, flag);
				  }
				else if (italicCnt == 1 && lastWC == retWC
						&& newState == Bold)
				  {
				    RemoveToken(lastLen, out_buf, out_size);

				    result  = WriteToken(BoldToken,
							BoldTokenSize,
							out_buf, out_size,
							out_max);

				    cread   = cread - (retStrPtr - rloc);
				    rloc    = retStrPtr;
				    c       = retC;
				    lastWC  = retWC;
				    lastLen = retWCLen;

				    result = WriteOutChar(lastLen, lastWC, c,
						out_buf, out_size, out_max, flag);

				    state = BoldItalic;
				  }
				else
				  {
				    result    = WriteToken(EndToken,
							EndTokenSize,
							out_buf, out_size,
							out_max);
				    state     = Char;
				    italicCnt = 0;
				  }

				break;
		      }
		  }

		if (cread < (3 * ((int) MB_CUR_MAX)) &&
						!feof (FileStream(in_file)))
		    cread = 0;
	      }
	    else
	      {
		/**
		 * if it is an invalid character - skip.
		 * But be careful.
		 * If this is the start of a multi-byte character,
		 * I must save it and try again on the next read.
		 **/
		if (cread < ((int) MB_CUR_MAX))
		    cread = 0;
		else
		  {
		    /*
		     * otherwise we've got corrupt data.
		     */
		    return -1;
		  }
	      }
          }
	if (result != -1 && !feof(FileStream(in_file)))
	  {
	    if (_DtHelpCeGetNxtBuf (in_file, in_buf, &rloc, in_size) == -1)
		result = -1;

	    if (result != -1)
	        cread = strlen (rloc);
	  }
      } while (result != -1 && cread > 0);

    return(result);

} /* End FormatManPage */