#endif
/*
* Copyright 1989 Massachusetts Institute of Technology
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. M.I.T. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Chris Peterson, MIT X Consortium.
*
* Much code taken from X11R3 String and Disk Sources.
*/
/*
* AsciiSrc.c - AsciiSrc object. (For use with the text widget).
*
*/
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <./Xaw3_1XawInit.h>
#include <./Xaw3_1AsciiSrcP.h>
#ifdef ASCII_STRING
#include <./Xaw3_1AsciiText.h> /* for Widget Classes. */
#endif
#ifdef ASCII_DISK)
#include <./Xaw3_1AsciiText.h> /* for Widget Classes. */
#endif
/****************************************************************
*
* Full class record constant
*
****************************************************************/
/* Private Data */
#ifdef ASCII_DISK
#endif /* ASCII_DISK */
};
static int ReplaceText();
static FILE * InitStringOrFile();
static void RemoveOldStringOrFile(), CvtStringToAsciiType();
extern char *tmpnam();
#ifndef SYSV
void bcopy();
#endif
extern char* sys_errlist[];
{
/* core_class fields */
/* class_name */ "AsciiSrc",
/* widget_size */ sizeof(AsciiSrcRec),
/* class_initialize */ ClassInitialize,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ NULL,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* xrm_class */ NULLQUARK,
/* compress_motion */ FALSE,
/* compress_exposure */ FALSE,
/* compress_enterleave */ FALSE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ NULL,
/* expose */ NULL,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ NULL,
/* get_values_hook */ GetValuesHook,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ NULL,
/* query_geometry */ NULL,
/* display_accelerator */ NULL,
/* extension */ NULL
},
/* textSrc_class fields */
{
/* Read */ ReadText,
/* Replace */ ReplaceText,
/* Scan */ Scan,
/* Search */ Search,
/* SetSelection */ XtInheritSetSelection,
/* ConvertSelection */ XtInheritConvertSelection
},
/* asciiSrc_class fields */
{
/* Keep the compiler happy */ NULL
}
};
/************************************************************
*
* Semi-Public Interfaces.
*
************************************************************/
/* Function Name: ClassInitialize
* Description: Class Initialize routine, called only once.
* Arguments: none.
* Returns: none.
*/
static void
{
}
/* Function Name: Initialize
* Description: Initializes the simple menu widget
* Arguments: request - the widget requested by the argument list.
* new - the new widget with both resource and non
* resource values.
* Returns: none.
*/
/* ARGSUSED */
static void
{
/*
* Set correct flags (override resources) depending upon widget class.
*/
#ifdef ASCII_DISK
}
#endif
#ifdef ASCII_STRING
}
#endif
}
/* Function Name: ReadText
* Description: This function reads the source.
* Arguments: w - the AsciiSource widget.
* pos - position of the text to retreive.
* RETURNED text - text block that will contain returned text.
* length - maximum number of characters to read.
* Returns: The number of characters read into the buffer.
*/
static XawTextPosition
Widget w;
int length;
{
}
/* Function Name: ReplaceText.
* Description: Replaces a block of text with new text.
* Arguments: w - the AsciiSource widget.
* startPos, endPos - ends of text that will be removed.
* text - new text to be inserted into buffer at startPos.
* Returns: XawEditError or XawEditDone.
*/
/*ARGSUSED*/
static int
Widget w;
{
/*
* Editing a read only source is not allowed.
*/
return(XawEditError);
/*
* Remove Old Stuff.
*/
if (start_piece != end_piece) {
/*
* If empty and not the only piece then remove it.
*/
while (temp_piece != end_piece) {
}
}
else { /* We are fully in one piece. */
}
else {
}
}
/*
* Put in the New Stuff.
*/
while (length > 0) {
char * ptr;
int fill;
/*
* If we are in ascii string emulation mode. Then the
* string is not allowed to grow.
*/
return(XawEditError);
}
}
}
}
}
return(XawEditDone);
}
/* Function Name: Scan
* Description: Scans the text source for the number and type
* of item specified.
* Arguments: w - the AsciiSource widget.
* position - the position to start scanning.
* type - type of thing to scan for.
* dir - direction to scan.
* count - which occurance if this thing to search for.
* include - whether or not to include the character found in
* the position that is returned.
* Returns: the position of the item found.
*
* Note: While there are only 'n' characters in the file there are n+1
* possible cursor positions (one before the first character and
* one after the last character.
*/
static
Widget w;
int count;
Boolean include;
{
register int inc;
register char * ptr;
if (dir == XawsdRight)
return(0); /* else. */
}
if ( dir == XawsdRight ) {
/*
* Scanning right from src->ascii_src.length???
*/
inc = 1;
}
else {
if (position == 0)
return(0); /* Scanning left from 0??? */
inc = -1;
position--;
}
/*
* If the buffer is empty then return 0.
*/
switch (type) {
case XawstEOL:
case XawstParagraph:
case XawstWhiteSpace:
while (TRUE) {
register unsigned char c = *ptr;
if (type == XawstWhiteSpace) {
if (isspace(c)) {
if (non_space)
break;
}
else
}
if (c == '\n') break;
}
else { /* XawstParagraph */
if (first_eol) {
if (c == '\n') {
}
}
else
if ( c == '\n')
break;
else if ( !isspace(c) )
}
return(0);
}
}
}
}
if (!include) {
if ( type == XawstParagraph)
}
break;
case XawstPositions:
break;
/* case XawstAll: ---- handled in special code above */
}
position++;
if (position < 0)
return(0);
return(position);
}
/* Function Name: Search
* Description: Searchs the text source for the text block passed
* Arguments: w - the AsciiSource Widget.
* position - the position to start scanning.
* dir - direction to scan.
* text - the text block to search for.
* Returns: the position of the item found.
*/
static XawTextPosition
Widget w;
XawTextBlock * text;
{
register char * ptr;
char * buf;
if ( dir == XawsdRight )
inc = 1;
else {
inc = -1;
if (position == 0)
return(XawTextSearchError); /* scanning left from 0??? */
position--;
}
while (TRUE) {
break;
else
count++;
}
else {
if (count != 0) {
}
count = 0;
}
return(XawTextSearchError);
}
}
return(XawTextSearchError);
}
}
}
return(position);
}
/* Function Name: SetValues
* Description: Sets the values for the AsciiSource.
* Arguments: current - current state of the widget.
* request - what was requested.
* new - what the widget will become.
* Returns: True if redisplay is needed.
*/
/* ARGSUSED */
static Boolean
{
"AsciiSrc: The XtNuseStrinInPlace resources may not be changed.");
}
/* Fool it into not freeing the string */
}
else {
}
what happened. */
total_reset = TRUE;
}
if ( !total_reset &&
}
return(FALSE);
}
/* Function Name: GetValuesHook
* Description: This is a get values hook routine that sets the
* values specific to the ascii source.
* Arguments: w - the AsciiSource Widget.
* args - the argument list.
* num_args - the number of args.
* Returns: none.
*/
static void
Widget w;
{
register int i;
return;
for (i = 0; i < *num_args ; i++ )
if (XawAsciiSave(w)) /* If save sucessful. */
break;
}
}
}
/* Function Name: Destroy
* Description: Destroys an ascii source (frees all data)
* Arguments: src - the Ascii source Widget to free.
* Returns: none.
*/
static void
Destroy (w)
Widget w;
{
}
/************************************************************
*
* Public routines
*
************************************************************/
/* Function Name: XawAsciiSourceFreeString
* Description: Frees the string returned by a get values call
* on the string when the source is of type string.
* Arguments: w - the AsciiSrc widget.
* Returns: none.
*/
void
Widget w;
{
}
}
/* Function Name: XawAsciiSave
* Description: Saves all the pieces into a file or string as required.
* Arguments: w - the asciiSrc Widget.
* Returns: TRUE if the save was successful.
*/
XawAsciiSave(w)
Widget w;
{
/*
* If using the string in place then there is no need to play games
* to get the internal info into a readable string.
*/
return(TRUE);
char * string;
return(TRUE);
return(FALSE);
}
}
else { /* This is a string widget. */
else
}
return(TRUE);
}
/* Function Name: XawAsciiSaveAsFile
* Description: Save the current buffer as a file.
* Arguments: w - the AsciiSrc widget.
* name - name of the file to save this file into.
* Returns: True if the save was sucessful.
*/
Widget w;
{
return(ret);
}
/* Function Name: XawAsciiSourceChanged
* Description: Returns true if the source has changed since last saved.
* Arguments: w - the ascii source widget.
* Returns: a Boolean (see description).
*/
Widget w;
{
}
/************************************************************
*
* Private Functions.
*
************************************************************/
static void
{
}
/* Function Name: WriteToFile
* Description: Write the string specified to the begining of the file
* specified.
* Arguments: w - the widget. (for error messages only)
* string - string to write.
* name - the name of the file
* file - file to write it to.
* Returns: returns TRUE if sucessful, FALSE otherwise.
*/
static Boolean
{
int fd;
return(FALSE);
}
return(FALSE);
}
return(TRUE);
}
/* Function Name: StorePiecesInString
* Description: store the pieces in memory into a standard ascii string.
* Arguments: data - the ascii pointer data.
* Returns: none.
*/
static String
{
/*
* This will refill all pieces to capacity.
*/
}
return(string);
}
/* Function Name: InitStringOrFile.
* Description: Initializes the string or file.
* Arguments: src - the AsciiSource.
* Returns: none - May exit though.
*/
static FILE *
{
char * open_mode;
else
else
}
return(NULL);
}
/*
* type is XawAsciiFile.
*/
case XawtextRead:
"Creating a read only disk widget and no file specified.",
NULL, 0);
open_mode = "r";
break;
case XawtextAppend:
case XawtextEdit:
}
else {
}
open_mode = "r+";
}
break;
default:
"Bad editMode for ascii source; must be Read, Append or Edit.",
}
else {
}
}
}
else {
return(NULL);
}
return(file);
}
static void
char * string;
{
}
}
else
}
else
/*
* If we are using teh string in place then set the other fields as follows:
*
* piece_size = length;
* piece->used = src->ascii_src.length;
*/
return;
}
do {
} while (left > 0);
}
/* Function Name: AllocNewPiece
* Description: Allocates a new piece of memory.
* Arguments: src - The AsciiSrc Widget.
* prev - the piece just before this one, or NULL.
* Returns: the allocated piece.
*/
static Piece *
{
}
else {
}
return(piece);
}
/* Function Name: FreeAllPieces
* Description: Frees all the pieces
* Arguments: src - The AsciiSrc Widget.
* Returns: none.
*/
static void
{
printf("Programmer Botch in FreeAllPieces, there may be a memory leak.\n");
}
}
/* Function Name: RemovePiece
* Description: Removes a piece from the list.
* Arguments:
* piece - the piece to remove.
* Returns: none.
*/
static void
{
else
}
/* Function Name: FindPiece
* Description: Finds the piece containing the position indicated.
* Arguments: src - The AsciiSrc Widget.
* position - the position that we are searching for.
* RETURNED first - the position of the first character in this piece.
* Returns: piece - the piece that contains this position.
*/
static Piece *
{
return(piece);
}
return(old_piece); /* if we run off the end the return the last piece */
}
/* Function Name: MyStrncpy
* Description: Just like string copy, but slower and will always
* work on overlapping strings.
* Arguments: (same as strncpy) - s1, s2 - strings to copy (2->1).
* n - the number of chars to copy.
* Returns: s1.
*/
static String
int n;
{
return(s1);
}
/* Function Name: BreakPiece
* Description: Breaks a full piece into two new pieces.
* Arguments: src - The AsciiSrc Widget.
* piece - the piece to break.
* Returns: none.
*/
static void
{
}
/* ARGSUSED */
static void
{
XrmQuark q;
if (!haveQuarks) {
haveQuarks = TRUE;
}
q = XrmStringToQuark(lowerName);
return;
}
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
# include <./Xaw3_1Cardinals.h>
#endif
#ifdef ASCII_STRING
/************************************************************
*
* Compatability functions.
*
************************************************************/
/* Function Name: AsciiStringSourceCreate
* Description: Creates a string source.
* Arguments: parent - the widget that will own this source.
* args, num_args - the argument list.
* Returns: a pointer to the new text source.
*/
{
XtFree((char *)ascii_args);
return(src);
}
/*
* This is hacked up to try to emulate old functionality, it
* may not work, as I have not old code to test it on.
*
* Chris D. Peterson 8/31/89.
*/
void
Widget w;
{
}
#endif /* ASCII_STRING */
#ifdef ASCII_DISK
/* Function Name: AsciiDiskSourceCreate
* Description: Creates a disk source.
* Arguments: parent - the widget that will own this source.
* args, num_args - the argument list.
* Returns: a pointer to the new text source.
*/
{
register int i;
for (i = 0; i < num_args; i++)
XtFree((char *)ascii_args);
return(src);
}
#endif /* ASCII_DISK */