| File: | AsciiSrc.c |
| Location: | line 1146, column 7 |
| Description: | Access to field 'prev' results in a dereference of a null pointer (loaded from variable 'first') |
| 1 | /* | |||
| 2 | ||||
| 3 | Copyright (c) 1989, 1994 X Consortium | |||
| 4 | ||||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | |||
| 6 | of this software and associated documentation files (the "Software"), to deal | |||
| 7 | in the Software without restriction, including without limitation the rights | |||
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
| 9 | copies of the Software, and to permit persons to whom the Software is | |||
| 10 | furnished to do so, subject to the following conditions: | |||
| 11 | ||||
| 12 | The above copyright notice and this permission notice shall be included in | |||
| 13 | all copies or substantial portions of the Software. | |||
| 14 | ||||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||
| 18 | X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |||
| 19 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |||
| 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||
| 21 | ||||
| 22 | Except as contained in this notice, the name of the X Consortium shall not be | |||
| 23 | used in advertising or otherwise to promote the sale, use or other dealings | |||
| 24 | in this Software without prior written authorization from the X Consortium. | |||
| 25 | ||||
| 26 | */ | |||
| 27 | ||||
| 28 | /* | |||
| 29 | * AsciiSrc.c - AsciiSrc object. (For use with the text widget). | |||
| 30 | * | |||
| 31 | */ | |||
| 32 | ||||
| 33 | #ifdef HAVE_CONFIG_H1 | |||
| 34 | #include "config.h" | |||
| 35 | #endif | |||
| 36 | #include <X11/Xaw3d/Xaw3dP.h> | |||
| 37 | #include <X11/IntrinsicP.h> | |||
| 38 | #include <stdio.h> | |||
| 39 | #include <ctype.h> | |||
| 40 | #include <errno(*__error()).h> | |||
| 41 | #include <X11/StringDefs.h> | |||
| 42 | #include <X11/Xos.h> | |||
| 43 | #include <stdlib.h> | |||
| 44 | #include <X11/Xfuncs.h> | |||
| 45 | #include <X11/Xaw3d/XawInit.h> | |||
| 46 | #include <X11/Xaw3d/AsciiSrcP.h> | |||
| 47 | #include <X11/Xmu/Misc.h> | |||
| 48 | #include <X11/Xmu/CharSet.h> | |||
| 49 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 50 | #include <X11/Xaw3d/MultiSrcP.h> | |||
| 51 | #endif | |||
| 52 | ||||
| 53 | ||||
| 54 | #if (defined(ASCII_STRING) || defined(ASCII_DISK)) | |||
| 55 | # include <X11/Xaw3d/AsciiText.h> /* for Widget Classes. */ | |||
| 56 | #endif | |||
| 57 | ||||
| 58 | ||||
| 59 | /**************************************************************** | |||
| 60 | * | |||
| 61 | * Full class record constant | |||
| 62 | * | |||
| 63 | ****************************************************************/ | |||
| 64 | ||||
| 65 | /* Private Data */ | |||
| 66 | ||||
| 67 | static int magic_value = MAGIC_VALUE((XawTextPosition) -1); | |||
| 68 | ||||
| 69 | #define offset(field) XtOffsetOf(AsciiSrcRec, ascii_src.field)__builtin_offsetof(AsciiSrcRec, ascii_src.field) | |||
| 70 | ||||
| 71 | static XtResource resources[] = { | |||
| 72 | {XtNstring((char*)&XtStrings[733]), XtCString((char*)&XtStrings[1416]), XtRString((char*)&XtStrings[1797]), sizeof (char *), | |||
| 73 | offset(string), XtRString((char*)&XtStrings[1797]), NULL((void*)0)}, | |||
| 74 | {XtNtype"type", XtCType"Type", XtRAsciiType"AsciiType", sizeof (XawAsciiType), | |||
| 75 | offset(type), XtRImmediate((char*)&XtStrings[1695]), (XtPointer)XawAsciiString}, | |||
| 76 | {XtNdataCompression"dataCompression", XtCDataCompression"DataCompression", XtRBoolean((char*)&XtStrings[1561]), sizeof (Boolean), | |||
| 77 | offset(data_compression), XtRImmediate((char*)&XtStrings[1695]), (XtPointer) TRUE1}, | |||
| 78 | {XtNpieceSize"pieceSize", XtCPieceSize"PieceSize", XtRInt((char*)&XtStrings[1718]), sizeof (XawTextPosition), | |||
| 79 | offset(piece_size), XtRImmediate((char*)&XtStrings[1695]), (XtPointer) BUFSIZ1024}, | |||
| 80 | {XtNcallback((char*)&XtStrings[136]), XtCCallback((char*)&XtStrings[952]), XtRCallback((char*)&XtStrings[1569]), sizeof(XtPointer), | |||
| 81 | offset(callback), XtRCallback((char*)&XtStrings[1569]), (XtPointer)NULL((void*)0)}, | |||
| 82 | {XtNuseStringInPlace"useStringInPlace", XtCUseStringInPlace"UseStringInPlace", XtRBoolean((char*)&XtStrings[1561]), sizeof (Boolean), | |||
| 83 | offset(use_string_in_place), XtRImmediate((char*)&XtStrings[1695]), (XtPointer) FALSE0}, | |||
| 84 | {XtNlength((char*)&XtStrings[435]), XtCLength((char*)&XtStrings[1157]), XtRInt((char*)&XtStrings[1718]), sizeof (int), | |||
| 85 | offset(ascii_length), XtRInt((char*)&XtStrings[1718]), (XtPointer) &magic_value}, | |||
| 86 | ||||
| 87 | #ifdef ASCII_DISK | |||
| 88 | {XtNfile((char*)&XtStrings[194]), XtCFile((char*)&XtStrings[1012]), XtRString((char*)&XtStrings[1797]), sizeof (String), | |||
| 89 | offset(filename), XtRString((char*)&XtStrings[1797]), NULL((void*)0)}, | |||
| 90 | #endif /* ASCII_DISK */ | |||
| 91 | }; | |||
| 92 | #undef offset | |||
| 93 | ||||
| 94 | static XawTextPosition Scan(Widget, XawTextPosition, XawTextScanType, | |||
| 95 | XawTextScanDirection, int, Boolean); | |||
| 96 | static XawTextPosition Search(Widget, XawTextPosition, XawTextScanDirection, | |||
| 97 | XawTextBlock *); | |||
| 98 | static XawTextPosition ReadText(Widget, XawTextPosition, XawTextBlock *, int); | |||
| 99 | static int ReplaceText(Widget, XawTextPosition, XawTextPosition, XawTextBlock *); | |||
| 100 | static Piece * FindPiece(AsciiSrcObject, XawTextPosition, XawTextPosition *); | |||
| 101 | static Piece * AllocNewPiece(AsciiSrcObject, Piece *); | |||
| 102 | static FILE * InitStringOrFile(AsciiSrcObject, Boolean); | |||
| 103 | static void FreeAllPieces(AsciiSrcObject); | |||
| 104 | static void RemovePiece(AsciiSrcObject, Piece *); | |||
| 105 | static void BreakPiece(AsciiSrcObject, Piece *); | |||
| 106 | static void LoadPieces(AsciiSrcObject, FILE *, char *); | |||
| 107 | static void RemoveOldStringOrFile(AsciiSrcObject, Boolean); | |||
| 108 | static void CvtStringToAsciiType(XrmValuePtr, Cardinal *, XrmValuePtr, XrmValuePtr); | |||
| 109 | static void ClassInitialize(void); | |||
| 110 | static void Initialize(Widget, Widget, ArgList, Cardinal *); | |||
| 111 | static void Destroy(Widget); | |||
| 112 | static void GetValuesHook(Widget, ArgList, Cardinal *); | |||
| 113 | static String MyStrncpy(char *, char *, int); | |||
| 114 | static String StorePiecesInString(AsciiSrcObject); | |||
| 115 | static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *); | |||
| 116 | static Boolean WriteToFile(_Xconstconst _XtStringchar*, _Xconstconst _XtStringchar*); | |||
| 117 | ||||
| 118 | ||||
| 119 | #define superclass(&textSrcClassRec) (&textSrcClassRec) | |||
| 120 | AsciiSrcClassRec asciiSrcClassRec = { | |||
| 121 | { | |||
| 122 | /* core_class fields */ | |||
| 123 | /* superclass */ (WidgetClass) superclass(&textSrcClassRec), | |||
| 124 | /* class_name */ "AsciiSrc", | |||
| 125 | /* widget_size */ sizeof(AsciiSrcRec), | |||
| 126 | /* class_initialize */ ClassInitialize, | |||
| 127 | /* class_part_initialize */ NULL((void*)0), | |||
| 128 | /* class_inited */ FALSE0, | |||
| 129 | /* initialize */ Initialize, | |||
| 130 | /* initialize_hook */ NULL((void*)0), | |||
| 131 | /* realize */ NULL((void*)0), | |||
| 132 | /* actions */ NULL((void*)0), | |||
| 133 | /* num_actions */ 0, | |||
| 134 | /* resources */ resources, | |||
| 135 | /* num_resources */ XtNumber(resources)((Cardinal) (sizeof(resources) / sizeof(resources[0]))), | |||
| 136 | /* xrm_class */ NULLQUARK((XrmQuark) 0), | |||
| 137 | /* compress_motion */ FALSE0, | |||
| 138 | /* compress_exposure */ FALSE0, | |||
| 139 | /* compress_enterleave */ FALSE0, | |||
| 140 | /* visible_interest */ FALSE0, | |||
| 141 | /* destroy */ Destroy, | |||
| 142 | /* resize */ NULL((void*)0), | |||
| 143 | /* expose */ NULL((void*)0), | |||
| 144 | /* set_values */ SetValues, | |||
| 145 | /* set_values_hook */ NULL((void*)0), | |||
| 146 | /* set_values_almost */ NULL((void*)0), | |||
| 147 | /* get_values_hook */ GetValuesHook, | |||
| 148 | /* accept_focus */ NULL((void*)0), | |||
| 149 | /* version */ XtVersion(11 * 1000 + 6), | |||
| 150 | /* callback_private */ NULL((void*)0), | |||
| 151 | /* tm_table */ NULL((void*)0), | |||
| 152 | /* query_geometry */ NULL((void*)0), | |||
| 153 | /* display_accelerator */ NULL((void*)0), | |||
| 154 | /* extension */ NULL((void*)0) | |||
| 155 | }, | |||
| 156 | /* textSrc_class fields */ | |||
| 157 | { | |||
| 158 | /* Read */ ReadText, | |||
| 159 | /* Replace */ ReplaceText, | |||
| 160 | /* Scan */ Scan, | |||
| 161 | /* Search */ Search, | |||
| 162 | /* SetSelection */ XtInheritSetSelection((_XawSrcSetSelectionProc) _XtInherit), | |||
| 163 | /* ConvertSelection */ XtInheritConvertSelection((_XawSrcConvertSelectionProc) _XtInherit) | |||
| 164 | }, | |||
| 165 | /* asciiSrc_class fields */ | |||
| 166 | { | |||
| 167 | /* Keep the compiler happy */ '\0' | |||
| 168 | } | |||
| 169 | }; | |||
| 170 | ||||
| 171 | WidgetClass asciiSrcObjectClass = (WidgetClass)&asciiSrcClassRec; | |||
| 172 | ||||
| 173 | /************************************************************ | |||
| 174 | * | |||
| 175 | * Semi-Public Interfaces. | |||
| 176 | * | |||
| 177 | ************************************************************/ | |||
| 178 | ||||
| 179 | /* Function Name: ClassInitialize | |||
| 180 | * Description: Class Initialize routine, called only once. | |||
| 181 | * Arguments: none. | |||
| 182 | * Returns: none. | |||
| 183 | */ | |||
| 184 | ||||
| 185 | static void | |||
| 186 | ClassInitialize(void) | |||
| 187 | { | |||
| 188 | XawInitializeWidgetSet(); | |||
| 189 | XtAddConverter( XtRString((char*)&XtStrings[1797]), XtRAsciiType"AsciiType", CvtStringToAsciiType, | |||
| 190 | NULL((void*)0), (Cardinal) 0); | |||
| 191 | } | |||
| 192 | ||||
| 193 | /* Function Name: Initialize | |||
| 194 | * Description: Initializes the simple menu widget | |||
| 195 | * Arguments: request - the widget requested by the argument list. | |||
| 196 | * new - the new widget with both resource and non | |||
| 197 | * resource values. | |||
| 198 | * Returns: none. | |||
| 199 | */ | |||
| 200 | ||||
| 201 | /* ARGSUSED */ | |||
| 202 | static void | |||
| 203 | Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args) | |||
| 204 | { | |||
| 205 | AsciiSrcObject src = (AsciiSrcObject) new; | |||
| 206 | FILE * file; | |||
| 207 | ||||
| 208 | /* | |||
| 209 | * Set correct flags (override resources) depending upon widget class. | |||
| 210 | */ | |||
| 211 | ||||
| 212 | src->text_src.text_format = XawFmt8Bit; /* data format. */ | |||
| 213 | ||||
| 214 | #ifdef ASCII_DISK | |||
| 215 | if (XtIsSubclass(XtParent(new)((new)->core.parent), asciiDiskWidgetClass)) { | |||
| 216 | src->ascii_src.type = XawAsciiFile; | |||
| 217 | src->ascii_src.string = src->ascii_src.filename; | |||
| 218 | } | |||
| 219 | #endif | |||
| 220 | ||||
| 221 | #ifdef ASCII_STRING | |||
| 222 | if (XtIsSubclass(XtParent(new)((new)->core.parent), asciiStringWidgetClass)) { | |||
| 223 | src->ascii_src.use_string_in_place = TRUE1; | |||
| 224 | src->ascii_src.type = XawAsciiString; | |||
| 225 | } | |||
| 226 | #endif | |||
| 227 | ||||
| 228 | src->ascii_src.changes = FALSE0; | |||
| 229 | src->ascii_src.allocated_string = FALSE0; | |||
| 230 | ||||
| 231 | file = InitStringOrFile(src, src->ascii_src.type == XawAsciiFile); | |||
| 232 | LoadPieces(src, file, NULL((void*)0)); | |||
| 233 | ||||
| 234 | if (file != NULL((void*)0)) fclose(file); | |||
| 235 | } | |||
| 236 | ||||
| 237 | /* Function Name: ReadText | |||
| 238 | * Description: This function reads the source. | |||
| 239 | * Arguments: w - the AsciiSource widget. | |||
| 240 | * pos - position of the text to retreive. | |||
| 241 | * RETURNED text - text block that will contain returned text. | |||
| 242 | * length - maximum number of characters to read. | |||
| 243 | * Returns: The number of characters read into the buffer. | |||
| 244 | */ | |||
| 245 | ||||
| 246 | static XawTextPosition | |||
| 247 | ReadText(Widget w, XawTextPosition pos, XawTextBlock *text, int length) | |||
| 248 | { | |||
| 249 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 250 | XawTextPosition count, start; | |||
| 251 | Piece * piece = FindPiece(src, pos, &start); | |||
| 252 | ||||
| 253 | text->firstPos = pos; | |||
| 254 | text->ptr = piece->text + (pos - start); | |||
| 255 | count = piece->used - (pos - start); | |||
| 256 | text->length = (length > count) ? count : length; | |||
| 257 | return(pos + text->length); | |||
| 258 | } | |||
| 259 | ||||
| 260 | /* Function Name: ReplaceText. | |||
| 261 | * Description: Replaces a block of text with new text. | |||
| 262 | * Arguments: w - the AsciiSource widget. | |||
| 263 | * startPos, endPos - ends of text that will be removed. | |||
| 264 | * text - new text to be inserted into buffer at startPos. | |||
| 265 | * Returns: XawEditError or XawEditDone. | |||
| 266 | */ | |||
| 267 | ||||
| 268 | /*ARGSUSED*/ | |||
| 269 | static int | |||
| 270 | ReplaceText (Widget w, XawTextPosition startPos, XawTextPosition endPos, | |||
| 271 | XawTextBlock *text) | |||
| 272 | { | |||
| 273 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 274 | Piece *start_piece, *end_piece, *temp_piece; | |||
| 275 | XawTextPosition start_first, end_first; | |||
| 276 | int length, firstPos; | |||
| 277 | ||||
| 278 | /* | |||
| 279 | * Editing a read only source is not allowed. | |||
| 280 | */ | |||
| 281 | ||||
| 282 | if (src->text_src.edit_mode == XawtextRead) | |||
| 283 | return(XawEditError1); | |||
| 284 | ||||
| 285 | start_piece = FindPiece(src, startPos, &start_first); | |||
| 286 | end_piece = FindPiece(src, endPos, &end_first); | |||
| 287 | ||||
| 288 | src->ascii_src.changes = TRUE1; /* We have changed the buffer. */ | |||
| 289 | ||||
| 290 | /* | |||
| 291 | * Remove Old Stuff. | |||
| 292 | */ | |||
| 293 | ||||
| 294 | if (start_piece != end_piece) { | |||
| 295 | temp_piece = start_piece->next; | |||
| 296 | ||||
| 297 | /* | |||
| 298 | * If empty and not the only piece then remove it. | |||
| 299 | */ | |||
| 300 | ||||
| 301 | if ( ((start_piece->used = startPos - start_first) == 0) && | |||
| 302 | !((start_piece->next == NULL((void*)0)) && (start_piece->prev == NULL((void*)0))) ) | |||
| 303 | RemovePiece(src, start_piece); | |||
| 304 | ||||
| 305 | while (temp_piece != end_piece) { | |||
| 306 | temp_piece = temp_piece->next; | |||
| 307 | RemovePiece(src, temp_piece->prev); | |||
| 308 | } | |||
| 309 | end_piece->used -= endPos - end_first; | |||
| 310 | if (end_piece->used != 0) | |||
| 311 | MyStrncpy(end_piece->text, (end_piece->text + endPos - end_first), | |||
| 312 | (int) end_piece->used); | |||
| 313 | } | |||
| 314 | else { /* We are fully in one piece. */ | |||
| 315 | if ( (start_piece->used -= endPos - startPos) == 0) { | |||
| 316 | if ( !((start_piece->next == NULL((void*)0)) && (start_piece->prev == NULL((void*)0))) ) | |||
| 317 | RemovePiece(src, start_piece); | |||
| 318 | } | |||
| 319 | else { | |||
| 320 | MyStrncpy(start_piece->text + (startPos - start_first), | |||
| 321 | start_piece->text + (endPos - start_first), | |||
| 322 | (int) (start_piece->used - (startPos - start_first)) ); | |||
| 323 | if ( src->ascii_src.use_string_in_place && | |||
| 324 | ((src->ascii_src.length - (endPos - startPos)) < | |||
| 325 | (src->ascii_src.piece_size - 1)) ) | |||
| 326 | start_piece->text[src->ascii_src.length - (endPos - startPos)] = '\0'; | |||
| 327 | } | |||
| 328 | } | |||
| 329 | ||||
| 330 | src->ascii_src.length += -(endPos - startPos) + text->length; | |||
| 331 | ||||
| 332 | if ( text->length != 0) { | |||
| 333 | ||||
| 334 | /* | |||
| 335 | * Put in the New Stuff. | |||
| 336 | */ | |||
| 337 | ||||
| 338 | start_piece = FindPiece(src, startPos, &start_first); | |||
| 339 | ||||
| 340 | length = text->length; | |||
| 341 | firstPos = text->firstPos; | |||
| 342 | ||||
| 343 | while (length > 0) { | |||
| 344 | char * ptr; | |||
| 345 | int fill; | |||
| 346 | ||||
| 347 | if (src->ascii_src.use_string_in_place) { | |||
| 348 | if (start_piece->used == (src->ascii_src.piece_size - 1)) { | |||
| 349 | /* | |||
| 350 | * If we are in ascii string emulation mode. Then the | |||
| 351 | * string is not allowed to grow. | |||
| 352 | */ | |||
| 353 | start_piece->used = src->ascii_src.length = | |||
| 354 | src->ascii_src.piece_size - 1; | |||
| 355 | start_piece->text[src->ascii_src.length] = '\0'; | |||
| 356 | return(XawEditError1); | |||
| 357 | } | |||
| 358 | } | |||
| 359 | ||||
| 360 | ||||
| 361 | if (start_piece->used == src->ascii_src.piece_size) { | |||
| 362 | BreakPiece(src, start_piece); | |||
| 363 | start_piece = FindPiece(src, startPos, &start_first); | |||
| 364 | } | |||
| 365 | ||||
| 366 | fill = Min((int)(src->ascii_src.piece_size - start_piece->used), length)((((int)(src->ascii_src.piece_size - start_piece->used) ) < (length)) ? ((int)(src->ascii_src.piece_size - start_piece ->used)) : (length)); | |||
| 367 | ||||
| 368 | ptr = start_piece->text + (startPos - start_first); | |||
| 369 | MyStrncpy(ptr + fill, ptr, | |||
| 370 | (int) start_piece->used - (startPos - start_first)); | |||
| 371 | strncpy(ptr, text->ptr + firstPos, fill)__builtin___strncpy_chk (ptr, text->ptr + firstPos, fill, __builtin_object_size (ptr, 2 > 1 ? 1 : 0)); | |||
| 372 | ||||
| 373 | startPos += fill; | |||
| 374 | firstPos += fill; | |||
| 375 | start_piece->used += fill; | |||
| 376 | length -= fill; | |||
| 377 | } | |||
| 378 | } | |||
| 379 | ||||
| 380 | if (src->ascii_src.use_string_in_place) | |||
| 381 | start_piece->text[start_piece->used] = '\0'; | |||
| 382 | ||||
| 383 | XtCallCallbacks(w, XtNcallback((char*)&XtStrings[136]), NULL((void*)0)); /* Call callbacks, we have changed | |||
| 384 | the buffer. */ | |||
| 385 | ||||
| 386 | return(XawEditDone0); | |||
| 387 | } | |||
| 388 | ||||
| 389 | /* Function Name: Scan | |||
| 390 | * Description: Scans the text source for the number and type | |||
| 391 | * of item specified. | |||
| 392 | * Arguments: w - the AsciiSource widget. | |||
| 393 | * position - the position to start scanning. | |||
| 394 | * type - type of thing to scan for. | |||
| 395 | * dir - direction to scan. | |||
| 396 | * count - which occurance if this thing to search for. | |||
| 397 | * include - whether or not to include the character found in | |||
| 398 | * the position that is returned. | |||
| 399 | * Returns: the position of the item found. | |||
| 400 | * | |||
| 401 | * Note: While there are only 'n' characters in the file there are n+1 | |||
| 402 | * possible cursor positions (one before the first character and | |||
| 403 | * one after the last character. | |||
| 404 | */ | |||
| 405 | ||||
| 406 | static | |||
| 407 | XawTextPosition | |||
| 408 | Scan (Widget w, XawTextPosition position, XawTextScanType type, | |||
| 409 | XawTextScanDirection dir, int count, Boolean include) | |||
| 410 | { | |||
| 411 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 412 | int inc; | |||
| 413 | Piece* piece; | |||
| 414 | XawTextPosition first, first_eol_position = 0; | |||
| 415 | char* ptr; | |||
| 416 | ||||
| 417 | if (type == XawstAll) { /* Optomize this common case. */ | |||
| 418 | if (dir == XawsdRight) | |||
| 419 | return(src->ascii_src.length); | |||
| 420 | return(0); /* else. */ | |||
| 421 | } | |||
| 422 | ||||
| 423 | if (position > src->ascii_src.length) | |||
| 424 | position = src->ascii_src.length; | |||
| 425 | ||||
| 426 | if ( dir == XawsdRight ) { | |||
| 427 | if (position == src->ascii_src.length) | |||
| 428 | /* | |||
| 429 | * Scanning right from src->ascii_src.length??? | |||
| 430 | */ | |||
| 431 | return(src->ascii_src.length); | |||
| 432 | inc = 1; | |||
| 433 | } | |||
| 434 | else { | |||
| 435 | if (position == 0) | |||
| 436 | return(0); /* Scanning left from 0??? */ | |||
| 437 | inc = -1; | |||
| 438 | position--; | |||
| 439 | } | |||
| 440 | ||||
| 441 | piece = FindPiece(src, position, &first); | |||
| 442 | ||||
| 443 | /* | |||
| 444 | * If the buffer is empty then return 0. | |||
| 445 | */ | |||
| 446 | ||||
| 447 | if ( piece->used == 0 ) return(0); | |||
| 448 | ||||
| 449 | ptr = (position - first) + piece->text; | |||
| 450 | ||||
| 451 | switch (type) { | |||
| 452 | case XawstEOL: | |||
| 453 | case XawstParagraph: | |||
| 454 | case XawstWhiteSpace: | |||
| 455 | for ( ; count > 0 ; count-- ) { | |||
| 456 | Boolean non_space = FALSE0, first_eol = TRUE1; | |||
| 457 | /* CONSTCOND */ | |||
| 458 | while (TRUE1) { | |||
| 459 | unsigned char c = *ptr; | |||
| 460 | ||||
| 461 | ptr += inc; | |||
| 462 | position += inc; | |||
| 463 | ||||
| 464 | if (type == XawstWhiteSpace) { | |||
| 465 | if (isspace(c)) { | |||
| 466 | if (non_space) | |||
| 467 | break; | |||
| 468 | } | |||
| 469 | else | |||
| 470 | non_space = TRUE1; | |||
| 471 | } | |||
| 472 | else if (type == XawstEOL) { | |||
| 473 | if (c == '\n') break; | |||
| 474 | } | |||
| 475 | else { /* XawstParagraph */ | |||
| 476 | if (first_eol) { | |||
| 477 | if (c == '\n') { | |||
| 478 | first_eol_position = position; | |||
| 479 | first_eol = FALSE0; | |||
| 480 | } | |||
| 481 | } | |||
| 482 | else | |||
| 483 | if ( c == '\n') | |||
| 484 | break; | |||
| 485 | else if ( !isspace(c) ) | |||
| 486 | first_eol = TRUE1; | |||
| 487 | } | |||
| 488 | ||||
| 489 | ||||
| 490 | if ( ptr < piece->text ) { | |||
| 491 | piece = piece->prev; | |||
| 492 | if (piece == NULL((void*)0)) /* Begining of text. */ | |||
| 493 | return(0); | |||
| 494 | ptr = piece->text + piece->used - 1; | |||
| 495 | } | |||
| 496 | else if ( ptr >= (piece->text + piece->used) ) { | |||
| 497 | piece = piece->next; | |||
| 498 | if (piece == NULL((void*)0)) /* End of text. */ | |||
| 499 | return(src->ascii_src.length); | |||
| 500 | ptr = piece->text; | |||
| 501 | } | |||
| 502 | } | |||
| 503 | } | |||
| 504 | if (!include) { | |||
| 505 | if ( type == XawstParagraph) | |||
| 506 | position = first_eol_position; | |||
| 507 | position -= inc; | |||
| 508 | } | |||
| 509 | break; | |||
| 510 | case XawstPositions: | |||
| 511 | position += count * inc; | |||
| 512 | break; | |||
| 513 | case XawstAll: /* handled in special code above */ | |||
| 514 | default: | |||
| 515 | break; | |||
| 516 | } | |||
| 517 | ||||
| 518 | if ( dir == XawsdLeft ) | |||
| 519 | position++; | |||
| 520 | ||||
| 521 | if (position >= src->ascii_src.length) | |||
| 522 | return(src->ascii_src.length); | |||
| 523 | if (position < 0) | |||
| 524 | return(0); | |||
| 525 | ||||
| 526 | return(position); | |||
| 527 | } | |||
| 528 | ||||
| 529 | /* Function Name: Search | |||
| 530 | * Description: Searchs the text source for the text block passed | |||
| 531 | * Arguments: w - the AsciiSource Widget. | |||
| 532 | * position - the position to start scanning. | |||
| 533 | * dir - direction to scan. | |||
| 534 | * text - the text block to search for. | |||
| 535 | * Returns: the position of the item found. | |||
| 536 | */ | |||
| 537 | ||||
| 538 | static XawTextPosition | |||
| 539 | Search(Widget w, XawTextPosition position, XawTextScanDirection dir, | |||
| 540 | XawTextBlock *text) | |||
| 541 | { | |||
| 542 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 543 | int inc, count = 0; | |||
| 544 | char * ptr; | |||
| 545 | Piece * piece; | |||
| 546 | char * buf; | |||
| 547 | XawTextPosition first; | |||
| 548 | ||||
| 549 | if ( dir == XawsdRight ) | |||
| 550 | inc = 1; | |||
| 551 | else { | |||
| 552 | inc = -1; | |||
| 553 | if (position == 0) | |||
| 554 | return(XawTextSearchError(-12345L)); /* scanning left from 0??? */ | |||
| 555 | position--; | |||
| 556 | } | |||
| 557 | ||||
| 558 | buf = XtMalloc((unsigned)sizeof(unsigned char) * text->length); | |||
| 559 | strncpy(buf, (text->ptr + text->firstPos), text->length)__builtin___strncpy_chk (buf, (text->ptr + text->firstPos ), text->length, __builtin_object_size (buf, 2 > 1 ? 1 : 0)); | |||
| 560 | piece = FindPiece(src, position, &first); | |||
| 561 | ptr = (position - first) + piece->text; | |||
| 562 | ||||
| 563 | /* CONSTCOND */ | |||
| 564 | while (TRUE1) { | |||
| 565 | if (*ptr == ((dir == XawsdRight) ? *(buf + count) | |||
| 566 | : *(buf + text->length - count - 1)) ) { | |||
| 567 | if (count == (text->length - 1)) | |||
| 568 | break; | |||
| 569 | else | |||
| 570 | count++; | |||
| 571 | } | |||
| 572 | else { | |||
| 573 | if (count != 0) { | |||
| 574 | position -=inc * count; | |||
| 575 | ptr -= inc * count; | |||
| 576 | } | |||
| 577 | count = 0; | |||
| 578 | } | |||
| 579 | ||||
| 580 | ptr += inc; | |||
| 581 | position += inc; | |||
| 582 | ||||
| 583 | while ( ptr < piece->text ) { | |||
| 584 | piece = piece->prev; | |||
| 585 | if (piece == NULL((void*)0)) { /* Begining of text. */ | |||
| 586 | XtFree(buf); | |||
| 587 | return(XawTextSearchError(-12345L)); | |||
| 588 | } | |||
| 589 | ptr = piece->text + piece->used - 1; | |||
| 590 | } | |||
| 591 | ||||
| 592 | while ( ptr >= (piece->text + piece->used) ) { | |||
| 593 | piece = piece->next; | |||
| 594 | if (piece == NULL((void*)0)) { /* End of text. */ | |||
| 595 | XtFree(buf); | |||
| 596 | return(XawTextSearchError(-12345L)); | |||
| 597 | } | |||
| 598 | ptr = piece->text; | |||
| 599 | } | |||
| 600 | } | |||
| 601 | ||||
| 602 | XtFree(buf); | |||
| 603 | if (dir == XawsdLeft) | |||
| 604 | return(position); | |||
| 605 | return(position - (text->length - 1)); | |||
| 606 | } | |||
| 607 | ||||
| 608 | /* Function Name: SetValues | |||
| 609 | * Description: Sets the values for the AsciiSource. | |||
| 610 | * Arguments: current - current state of the widget. | |||
| 611 | * request - what was requested. | |||
| 612 | * new - what the widget will become. | |||
| 613 | * Returns: True if redisplay is needed. | |||
| 614 | */ | |||
| 615 | ||||
| 616 | /* ARGSUSED */ | |||
| 617 | static Boolean | |||
| 618 | SetValues(Widget current, Widget request, Widget new, ArgList args, | |||
| 619 | Cardinal * num_args) | |||
| 620 | { | |||
| 621 | AsciiSrcObject src = (AsciiSrcObject) new; | |||
| 622 | AsciiSrcObject old_src = (AsciiSrcObject) current; | |||
| 623 | Boolean total_reset = FALSE0, string_set = FALSE0; | |||
| 624 | FILE * file; | |||
| 625 | int i; | |||
| 626 | ||||
| 627 | if ( old_src->ascii_src.use_string_in_place != | |||
| ||||
| 628 | src->ascii_src.use_string_in_place ) { | |||
| 629 | XtAppWarning( XtWidgetToApplicationContext(new), | |||
| 630 | "AsciiSrc: The XtNuseStringInPlace resource may not be changed."); | |||
| 631 | src->ascii_src.use_string_in_place = | |||
| 632 | old_src->ascii_src.use_string_in_place; | |||
| 633 | } | |||
| 634 | ||||
| 635 | for (i = 0; i < *num_args ; i++ ) | |||
| 636 | if (streq(args[i].name, XtNstring)( strcmp((args[i].name), (((char*)&XtStrings[733]))) == 0 )) { | |||
| 637 | string_set = TRUE1; | |||
| 638 | break; | |||
| 639 | } | |||
| 640 | ||||
| 641 | if ( string_set || (old_src->ascii_src.type != src->ascii_src.type) ) { | |||
| 642 | RemoveOldStringOrFile(old_src, string_set); /* remove old info. */ | |||
| 643 | file = InitStringOrFile(src, string_set); /* Init new info. */ | |||
| 644 | LoadPieces(src, file, NULL((void*)0)); /* load new info into internal buffers. */ | |||
| 645 | if (file != NULL((void*)0)) fclose(file); | |||
| 646 | XawTextSetSource( XtParent(new)((new)->core.parent), new, 0); /* Tell text widget | |||
| 647 | what happened. */ | |||
| 648 | total_reset = TRUE1; | |||
| 649 | } | |||
| 650 | ||||
| 651 | if ( old_src->ascii_src.ascii_length != src->ascii_src.ascii_length ) | |||
| 652 | src->ascii_src.piece_size = src->ascii_src.ascii_length; | |||
| 653 | ||||
| 654 | if ( !total_reset && | |||
| 655 | (old_src->ascii_src.piece_size != src->ascii_src.piece_size) ) { | |||
| 656 | String string = StorePiecesInString(old_src); | |||
| 657 | FreeAllPieces(old_src); | |||
| 658 | LoadPieces(src, NULL((void*)0), string); | |||
| 659 | XtFree(string); | |||
| 660 | } | |||
| 661 | ||||
| 662 | return(FALSE0); | |||
| 663 | } | |||
| 664 | ||||
| 665 | /* Function Name: GetValuesHook | |||
| 666 | * Description: This is a get values hook routine that sets the | |||
| 667 | * values specific to the ascii source. | |||
| 668 | * Arguments: w - the AsciiSource Widget. | |||
| 669 | * args - the argument list. | |||
| 670 | * num_args - the number of args. | |||
| 671 | * Returns: none. | |||
| 672 | */ | |||
| 673 | ||||
| 674 | static void | |||
| 675 | GetValuesHook(Widget w, ArgList args, Cardinal * num_args) | |||
| 676 | { | |||
| 677 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 678 | int i; | |||
| 679 | ||||
| 680 | if (src->ascii_src.type == XawAsciiString) { | |||
| 681 | for (i = 0; i < *num_args ; i++ ) | |||
| 682 | if (streq(args[i].name, XtNstring)( strcmp((args[i].name), (((char*)&XtStrings[733]))) == 0 )) { | |||
| 683 | if (src->ascii_src.use_string_in_place) { | |||
| 684 | *((char **) args[i].value) = src->ascii_src.first_piece->text; | |||
| 685 | } | |||
| 686 | else { | |||
| 687 | if (XawAsciiSave(w)) /* If save sucessful. */ | |||
| 688 | *((char **) args[i].value) = src->ascii_src.string; | |||
| 689 | } | |||
| 690 | break; | |||
| 691 | } | |||
| 692 | } | |||
| 693 | } | |||
| 694 | ||||
| 695 | /* Function Name: Destroy | |||
| 696 | * Description: Destroys an ascii source (frees all data) | |||
| 697 | * Arguments: src - the Ascii source Widget to free. | |||
| 698 | * Returns: none. | |||
| 699 | */ | |||
| 700 | ||||
| 701 | static void | |||
| 702 | Destroy (Widget w) | |||
| 703 | { | |||
| 704 | RemoveOldStringOrFile((AsciiSrcObject) w, True1); | |||
| 705 | } | |||
| 706 | ||||
| 707 | /************************************************************ | |||
| 708 | * | |||
| 709 | * Public routines | |||
| 710 | * | |||
| 711 | ************************************************************/ | |||
| 712 | ||||
| 713 | /* Function Name: XawAsciiSourceFreeString | |||
| 714 | * Description: Frees the string returned by a get values call | |||
| 715 | * on the string when the source is of type string. | |||
| 716 | * Arguments: w - the AsciiSrc widget. | |||
| 717 | * Returns: none. | |||
| 718 | */ | |||
| 719 | ||||
| 720 | void | |||
| 721 | XawAsciiSourceFreeString(Widget w) | |||
| 722 | { | |||
| 723 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 724 | ||||
| 725 | /* If the src is really a multi, call the multi routine.*/ | |||
| 726 | ||||
| 727 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 728 | if ( XtIsSubclass( w, multiSrcObjectClass ) ) { | |||
| 729 | _XawMultiSourceFreeString( w ); | |||
| 730 | return; | |||
| 731 | } | |||
| 732 | else | |||
| 733 | #endif | |||
| 734 | if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) { | |||
| 735 | XtErrorMsg("bad argument", "asciiSource", "XawError", | |||
| 736 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 737 | "XawAsciiSourceFreeString's parameter must be an asciiSrc or multiSrc.", | |||
| 738 | #else | |||
| 739 | "XawAsciiSourceFreeString's parameter must be an asciiSrc.", | |||
| 740 | #endif | |||
| 741 | NULL((void*)0), NULL((void*)0)); | |||
| 742 | } | |||
| 743 | ||||
| 744 | if (src->ascii_src.allocated_string && src->ascii_src.type != XawAsciiFile) { | |||
| 745 | src->ascii_src.allocated_string = FALSE0; | |||
| 746 | XtFree(src->ascii_src.string); | |||
| 747 | src->ascii_src.string = NULL((void*)0); | |||
| 748 | } | |||
| 749 | } | |||
| 750 | ||||
| 751 | /* Function Name: XawAsciiSave | |||
| 752 | * Description: Saves all the pieces into a file or string as required. | |||
| 753 | * Arguments: w - the asciiSrc Widget. | |||
| 754 | * Returns: TRUE if the save was successful. | |||
| 755 | */ | |||
| 756 | ||||
| 757 | Boolean | |||
| 758 | XawAsciiSave(Widget w) | |||
| 759 | { | |||
| 760 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 761 | ||||
| 762 | /* If the src is really a multi, call the multi save. */ | |||
| 763 | ||||
| 764 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 765 | if ( XtIsSubclass( w, multiSrcObjectClass ) ) | |||
| 766 | return( _XawMultiSave( w ) ); | |||
| 767 | else | |||
| 768 | #endif | |||
| 769 | if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) { | |||
| 770 | XtErrorMsg("bad argument", "asciiSource", "XawError", | |||
| 771 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 772 | "XawAsciiSave's parameter must be an asciiSrc or multiSrc.", | |||
| 773 | #else | |||
| 774 | "XawAsciiSave's parameter must be an asciiSrc.", | |||
| 775 | #endif | |||
| 776 | NULL((void*)0), NULL((void*)0)); | |||
| 777 | } | |||
| 778 | ||||
| 779 | /* | |||
| 780 | * If using the string in place then there is no need to play games | |||
| 781 | * to get the internal info into a readable string. | |||
| 782 | */ | |||
| 783 | ||||
| 784 | if (src->ascii_src.use_string_in_place) | |||
| 785 | return(TRUE1); | |||
| 786 | ||||
| 787 | if (src->ascii_src.type == XawAsciiFile) { | |||
| 788 | char * string; | |||
| 789 | ||||
| 790 | if (!src->ascii_src.changes) /* No changes to save. */ | |||
| 791 | return(TRUE1); | |||
| 792 | ||||
| 793 | string = StorePiecesInString(src); | |||
| 794 | ||||
| 795 | if (WriteToFile(string, src->ascii_src.string) == FALSE0) { | |||
| 796 | XtFree(string); | |||
| 797 | return(FALSE0); | |||
| 798 | } | |||
| 799 | XtFree(string); | |||
| 800 | } | |||
| 801 | else { | |||
| 802 | if (src->ascii_src.allocated_string == TRUE1) | |||
| 803 | XtFree(src->ascii_src.string); | |||
| 804 | else | |||
| 805 | src->ascii_src.allocated_string = TRUE1; | |||
| 806 | ||||
| 807 | src->ascii_src.string = StorePiecesInString(src); | |||
| 808 | } | |||
| 809 | src->ascii_src.changes = FALSE0; | |||
| 810 | return(TRUE1); | |||
| 811 | } | |||
| 812 | ||||
| 813 | /* Function Name: XawAsciiSaveAsFile | |||
| 814 | * Description: Save the current buffer as a file. | |||
| 815 | * Arguments: w - the AsciiSrc widget. | |||
| 816 | * name - name of the file to save this file into. | |||
| 817 | * Returns: True if the save was sucessful. | |||
| 818 | */ | |||
| 819 | ||||
| 820 | Boolean | |||
| 821 | XawAsciiSaveAsFile(Widget w, _Xconstconst char* name) | |||
| 822 | { | |||
| 823 | AsciiSrcObject src = (AsciiSrcObject) w; | |||
| 824 | String string; | |||
| 825 | Boolean ret; | |||
| 826 | ||||
| 827 | /* If the src is really a multi, call the multi save. - */ | |||
| 828 | ||||
| 829 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 830 | if ( XtIsSubclass( w, multiSrcObjectClass ) ) | |||
| 831 | return( _XawMultiSaveAsFile( w, name ) ); | |||
| 832 | else | |||
| 833 | #endif | |||
| 834 | if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) { | |||
| 835 | XtErrorMsg("bad argument", "asciiSource", "XawError", | |||
| 836 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 837 | "XawAsciiSaveAsFile's 1st parameter must be an asciiSrc or multiSrc.", | |||
| 838 | #else | |||
| 839 | "XawAsciiSaveAsFile's 1st parameter must be an asciiSrc.", | |||
| 840 | #endif | |||
| 841 | NULL((void*)0), NULL((void*)0)); | |||
| 842 | } | |||
| 843 | ||||
| 844 | string = StorePiecesInString(src); | |||
| 845 | ||||
| 846 | ret = WriteToFile(string, name); | |||
| 847 | XtFree(string); | |||
| 848 | return(ret); | |||
| 849 | } | |||
| 850 | ||||
| 851 | /* Function Name: XawAsciiSourceChanged | |||
| 852 | * Description: Returns true if the source has changed since last saved. | |||
| 853 | * Arguments: w - the ascii source widget. | |||
| 854 | * Returns: a Boolean (see description). | |||
| 855 | */ | |||
| 856 | ||||
| 857 | Boolean | |||
| 858 | XawAsciiSourceChanged(Widget w) | |||
| 859 | { | |||
| 860 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 861 | if ( XtIsSubclass( w, multiSrcObjectClass ) ) | |||
| 862 | return( ( (MultiSrcObject) w )->multi_src.changes ); | |||
| 863 | #endif | |||
| 864 | ||||
| 865 | if ( XtIsSubclass( w, asciiSrcObjectClass ) ) | |||
| 866 | return( ( (AsciiSrcObject) w)->ascii_src.changes ); | |||
| 867 | ||||
| 868 | XtErrorMsg("bad argument", "asciiSource", "XawError", | |||
| 869 | #ifdef XAW_INTERNATIONALIZATION1 | |||
| 870 | "XawAsciiSourceChanged parameter must be an asciiSrc or multiSrc.", | |||
| 871 | #else | |||
| 872 | "XawAsciiSourceChanged parameter must be an asciiSrc.", | |||
| 873 | #endif | |||
| 874 | NULL((void*)0), NULL((void*)0)); | |||
| 875 | ||||
| 876 | return( True1 ); /* for gcc -Wall */ | |||
| 877 | } | |||
| 878 | ||||
| 879 | /************************************************************ | |||
| 880 | * | |||
| 881 | * Private Functions. | |||
| 882 | * | |||
| 883 | ************************************************************/ | |||
| 884 | ||||
| 885 | static void | |||
| 886 | RemoveOldStringOrFile(AsciiSrcObject src, Boolean checkString) | |||
| 887 | { | |||
| 888 | FreeAllPieces(src); | |||
| 889 | ||||
| 890 | if (checkString && src->ascii_src.allocated_string) { | |||
| 891 | XtFree(src->ascii_src.string); | |||
| 892 | src->ascii_src.allocated_string = False0; | |||
| 893 | src->ascii_src.string = NULL((void*)0); | |||
| 894 | } | |||
| 895 | } | |||
| 896 | ||||
| 897 | /* Function Name: WriteToFile | |||
| 898 | * Description: Write the string specified to the begining of the file | |||
| 899 | * specified. | |||
| 900 | * Arguments: string - string to write. | |||
| 901 | * name - the name of the file | |||
| 902 | * Returns: returns TRUE if sucessful, FALSE otherwise. | |||
| 903 | */ | |||
| 904 | ||||
| 905 | static Boolean | |||
| 906 | WriteToFile(_Xconstconst _XtStringchar* string, _Xconstconst _XtStringchar* name) | |||
| 907 | { | |||
| 908 | int fd; | |||
| 909 | ||||
| 910 | if ( ((fd = creat(name, 0666)) == -1 ) || | |||
| 911 | (write(fd, string, sizeof(unsigned char) * strlen(string)) == -1) ) | |||
| 912 | return(FALSE0); | |||
| 913 | ||||
| 914 | if ( close(fd) == -1 ) | |||
| 915 | return(FALSE0); | |||
| 916 | ||||
| 917 | return(TRUE1); | |||
| 918 | } | |||
| 919 | ||||
| 920 | /* Function Name: StorePiecesInString | |||
| 921 | * Description: store the pieces in memory into a standard ascii string. | |||
| 922 | * Arguments: data - the ascii pointer data. | |||
| 923 | * Returns: none. | |||
| 924 | */ | |||
| 925 | ||||
| 926 | static String | |||
| 927 | StorePiecesInString(AsciiSrcObject src) | |||
| 928 | { | |||
| 929 | String string; | |||
| 930 | XawTextPosition first; | |||
| 931 | Piece * piece; | |||
| 932 | ||||
| 933 | string = XtMalloc((unsigned) sizeof(unsigned char) * | |||
| 934 | src->ascii_src.length + 1); | |||
| 935 | ||||
| 936 | for (first = 0, piece = src->ascii_src.first_piece ; piece != NULL((void*)0); | |||
| 937 | first += piece->used, piece = piece->next) | |||
| 938 | strncpy(string + first, piece->text, piece->used)__builtin___strncpy_chk (string + first, piece->text, piece ->used, __builtin_object_size (string + first, 2 > 1 ? 1 : 0)); | |||
| 939 | ||||
| 940 | string[src->ascii_src.length] = '\0'; /* NULL terminate this sucker. */ | |||
| 941 | ||||
| 942 | /* | |||
| 943 | * This will refill all pieces to capacity. | |||
| 944 | */ | |||
| 945 | ||||
| 946 | if (src->ascii_src.data_compression) { | |||
| 947 | FreeAllPieces(src); | |||
| 948 | LoadPieces(src, NULL((void*)0), string); | |||
| 949 | } | |||
| 950 | ||||
| 951 | return(string); | |||
| 952 | } | |||
| 953 | ||||
| 954 | /* Function Name: InitStringOrFile. | |||
| 955 | * Description: Initializes the string or file. | |||
| 956 | * Arguments: src - the AsciiSource. | |||
| 957 | * Returns: none - May exit though. | |||
| 958 | */ | |||
| 959 | ||||
| 960 | static FILE * | |||
| 961 | InitStringOrFile(AsciiSrcObject src, Boolean newString) | |||
| 962 | { | |||
| 963 | char * open_mode = NULL((void*)0); | |||
| 964 | FILE * file; | |||
| 965 | char fileName[TMPSIZ1024]; | |||
| 966 | ||||
| 967 | if (src->ascii_src.type == XawAsciiString) { | |||
| 968 | ||||
| 969 | if (src->ascii_src.string == NULL((void*)0)) | |||
| 970 | src->ascii_src.length = 0; | |||
| 971 | ||||
| 972 | else if (! src->ascii_src.use_string_in_place) { | |||
| 973 | src->ascii_src.string = XtNewString(src->ascii_src.string)((src->ascii_src.string) != ((void*)0) ? (__builtin___strcpy_chk (XtMalloc((unsigned)strlen(src->ascii_src.string) + 1), src ->ascii_src.string, __builtin_object_size (XtMalloc((unsigned )strlen(src->ascii_src.string) + 1), 2 > 1 ? 1 : 0))) : ((void*)0)); | |||
| 974 | src->ascii_src.allocated_string = True1; | |||
| 975 | src->ascii_src.length = strlen(src->ascii_src.string); | |||
| 976 | } | |||
| 977 | ||||
| 978 | if (src->ascii_src.use_string_in_place) { | |||
| 979 | src->ascii_src.length = strlen(src->ascii_src.string); | |||
| 980 | /* In case the length resource is incorrectly set */ | |||
| 981 | if (src->ascii_src.length > src->ascii_src.ascii_length) | |||
| 982 | src->ascii_src.ascii_length = src->ascii_src.length; | |||
| 983 | ||||
| 984 | if (src->ascii_src.ascii_length == MAGIC_VALUE((XawTextPosition) -1)) | |||
| 985 | src->ascii_src.piece_size = src->ascii_src.length; | |||
| 986 | else | |||
| 987 | src->ascii_src.piece_size = src->ascii_src.ascii_length + 1; | |||
| 988 | } | |||
| 989 | ||||
| 990 | return(NULL((void*)0)); | |||
| 991 | } | |||
| 992 | ||||
| 993 | /* | |||
| 994 | * type is XawAsciiFile. | |||
| 995 | */ | |||
| 996 | ||||
| 997 | src->ascii_src.is_tempfile = FALSE0; | |||
| 998 | ||||
| 999 | switch (src->text_src.edit_mode) { | |||
| 1000 | case XawtextRead: | |||
| 1001 | if (src->ascii_src.string == NULL((void*)0)) | |||
| 1002 | XtErrorMsg("NoFile", "asciiSourceCreate", "XawError", | |||
| 1003 | "Creating a read only disk widget and no file specified.", | |||
| 1004 | NULL((void*)0), 0); | |||
| 1005 | open_mode = "r"; | |||
| 1006 | break; | |||
| 1007 | case XawtextAppend: | |||
| 1008 | case XawtextEdit: | |||
| 1009 | if (src->ascii_src.string == NULL((void*)0)) { | |||
| 1010 | src->ascii_src.string = fileName; | |||
| 1011 | (void) tmpnam(src->ascii_src.string); | |||
| 1012 | src->ascii_src.is_tempfile = TRUE1; | |||
| 1013 | open_mode = "w"; | |||
| 1014 | } else | |||
| 1015 | open_mode = "r+"; | |||
| 1016 | break; | |||
| 1017 | default: | |||
| 1018 | XtErrorMsg("badMode", "asciiSourceCreate", "XawError", | |||
| 1019 | "Bad editMode for ascii source; must be Read, Append or Edit.", | |||
| 1020 | NULL((void*)0), NULL((void*)0)); | |||
| 1021 | } | |||
| 1022 | ||||
| 1023 | /* Allocate new memory for the temp filename, because it is held in | |||
| 1024 | * a stack variable, not static memory. This widget does not need | |||
| 1025 | * to keep the private state field is_tempfile -- it is only accessed | |||
| 1026 | * in this routine, and its former setting is unused. | |||
| 1027 | */ | |||
| 1028 | if (newString || src->ascii_src.is_tempfile) { | |||
| 1029 | src->ascii_src.string = XtNewString(src->ascii_src.string)((src->ascii_src.string) != ((void*)0) ? (__builtin___strcpy_chk (XtMalloc((unsigned)strlen(src->ascii_src.string) + 1), src ->ascii_src.string, __builtin_object_size (XtMalloc((unsigned )strlen(src->ascii_src.string) + 1), 2 > 1 ? 1 : 0))) : ((void*)0)); | |||
| 1030 | src->ascii_src.allocated_string = TRUE1; | |||
| 1031 | } | |||
| 1032 | ||||
| 1033 | if (!src->ascii_src.is_tempfile) { | |||
| 1034 | if ((file = fopen(src->ascii_src.string, open_mode)) != 0) { | |||
| 1035 | (void) fseek(file, 0, SEEK_END2); | |||
| 1036 | src->ascii_src.length = (XawTextPosition) ftell(file); | |||
| 1037 | return file; | |||
| 1038 | } else { | |||
| 1039 | String params[2]; | |||
| 1040 | Cardinal num_params = 2; | |||
| 1041 | ||||
| 1042 | params[0] = src->ascii_src.string; | |||
| 1043 | params[1] = strerror(errno(*__error())); | |||
| 1044 | XtAppWarningMsg(XtWidgetToApplicationContext((Widget)src), | |||
| 1045 | "openError", "asciiSourceCreate", "XawWarning", | |||
| 1046 | "Cannot open file %s; %s", params, &num_params); | |||
| 1047 | } | |||
| 1048 | } | |||
| 1049 | src->ascii_src.length = 0; | |||
| 1050 | return((FILE *)NULL((void*)0)); | |||
| 1051 | } | |||
| 1052 | ||||
| 1053 | static void | |||
| 1054 | LoadPieces(AsciiSrcObject src, FILE * file, char * string) | |||
| 1055 | { | |||
| 1056 | char *local_str, *ptr; | |||
| 1057 | Piece * piece = NULL((void*)0); | |||
| 1058 | XawTextPosition left; | |||
| 1059 | ||||
| 1060 | if (string == NULL((void*)0)) { | |||
| 1061 | if (src->ascii_src.type == XawAsciiFile) { | |||
| 1062 | local_str = XtMalloc((unsigned) (src->ascii_src.length + 1) | |||
| 1063 | * sizeof(unsigned char)); | |||
| 1064 | if (src->ascii_src.length != 0) { | |||
| 1065 | fseek(file, 0, SEEK_SET0); | |||
| 1066 | src->ascii_src.length = fread(local_str, sizeof(unsigned char), | |||
| 1067 | (size_t)src->ascii_src.length, file); | |||
| 1068 | if (src->ascii_src.length <= 0) | |||
| 1069 | XtErrorMsg("readError", "asciiSourceCreate", "XawError", | |||
| 1070 | "fread returned error.", NULL((void*)0), NULL((void*)0)); | |||
| 1071 | } | |||
| 1072 | local_str[src->ascii_src.length] = '\0'; | |||
| 1073 | } | |||
| 1074 | else | |||
| 1075 | local_str = src->ascii_src.string; | |||
| 1076 | } | |||
| 1077 | else | |||
| 1078 | local_str = string; | |||
| 1079 | ||||
| 1080 | if (src->ascii_src.use_string_in_place) { | |||
| 1081 | piece = AllocNewPiece(src, piece); | |||
| 1082 | piece->used = Min(src->ascii_src.length, src->ascii_src.piece_size)(((src->ascii_src.length) < (src->ascii_src.piece_size )) ? (src->ascii_src.length) : (src->ascii_src.piece_size )); | |||
| 1083 | piece->text = src->ascii_src.string; | |||
| 1084 | return; | |||
| 1085 | } | |||
| 1086 | ||||
| 1087 | ptr = local_str; | |||
| 1088 | left = src->ascii_src.length; | |||
| 1089 | ||||
| 1090 | do { | |||
| 1091 | piece = AllocNewPiece(src, piece); | |||
| 1092 | ||||
| 1093 | piece->text = XtMalloc((unsigned)src->ascii_src.piece_size | |||
| 1094 | * sizeof(unsigned char)); | |||
| 1095 | piece->used = Min(left, src->ascii_src.piece_size)(((left) < (src->ascii_src.piece_size)) ? (left) : (src ->ascii_src.piece_size)); | |||
| 1096 | if (piece->used != 0) | |||
| 1097 | strncpy(piece->text, ptr, piece->used)__builtin___strncpy_chk (piece->text, ptr, piece->used, __builtin_object_size (piece->text, 2 > 1 ? 1 : 0)); | |||
| 1098 | ||||
| 1099 | left -= piece->used; | |||
| 1100 | ptr += piece->used; | |||
| 1101 | } while (left > 0); | |||
| 1102 | ||||
| 1103 | if ( (src->ascii_src.type == XawAsciiFile) && (string == NULL((void*)0)) ) | |||
| 1104 | XtFree(local_str); | |||
| 1105 | } | |||
| 1106 | ||||
| 1107 | /* Function Name: AllocNewPiece | |||
| 1108 | * Description: Allocates a new piece of memory. | |||
| 1109 | * Arguments: src - The AsciiSrc Widget. | |||
| 1110 | * prev - the piece just before this one, or NULL. | |||
| 1111 | * Returns: the allocated piece. | |||
| 1112 | */ | |||
| 1113 | ||||
| 1114 | static Piece * | |||
| 1115 | AllocNewPiece(AsciiSrcObject src, Piece * prev) | |||
| 1116 | { | |||
| 1117 | Piece * piece = XtNew(Piece)((Piece *) XtMalloc((unsigned) sizeof(Piece))); | |||
| 1118 | ||||
| 1119 | if (prev == NULL((void*)0)) { | |||
| 1120 | src->ascii_src.first_piece = piece; | |||
| 1121 | piece->next = NULL((void*)0); | |||
| 1122 | } | |||
| 1123 | else { | |||
| 1124 | if (prev->next != NULL((void*)0)) | |||
| 1125 | (prev->next)->prev = piece; | |||
| 1126 | piece->next = prev->next; | |||
| 1127 | prev->next = piece; | |||
| 1128 | } | |||
| 1129 | ||||
| 1130 | piece->prev = prev; | |||
| 1131 | ||||
| 1132 | return(piece); | |||
| 1133 | } | |||
| 1134 | ||||
| 1135 | /* Function Name: FreeAllPieces | |||
| 1136 | * Description: Frees all the pieces | |||
| 1137 | * Arguments: src - The AsciiSrc Widget. | |||
| 1138 | * Returns: none. | |||
| 1139 | */ | |||
| 1140 | ||||
| 1141 | static void | |||
| 1142 | FreeAllPieces(AsciiSrcObject src) | |||
| 1143 | { | |||
| 1144 | Piece * next, * first = src->ascii_src.first_piece; | |||
| 1145 | ||||
| 1146 | if (first->prev != NULL((void*)0)) | |||
| ||||
| 1147 | (void) printf("Xaw AsciiSrc Object: possible memory leak in FreeAllPieces().\n"); | |||
| 1148 | ||||
| 1149 | for ( ; first != NULL((void*)0) ; first = next ) { | |||
| 1150 | next = first->next; | |||
| 1151 | RemovePiece(src, first); | |||
| 1152 | } | |||
| 1153 | } | |||
| 1154 | ||||
| 1155 | /* Function Name: RemovePiece | |||
| 1156 | * Description: Removes a piece from the list. | |||
| 1157 | * Arguments: | |||
| 1158 | * piece - the piece to remove. | |||
| 1159 | * Returns: none. | |||
| 1160 | */ | |||
| 1161 | ||||
| 1162 | static void | |||
| 1163 | RemovePiece(AsciiSrcObject src, Piece * piece) | |||
| 1164 | { | |||
| 1165 | if (piece->prev == NULL((void*)0)) | |||
| 1166 | src->ascii_src.first_piece = piece->next; | |||
| 1167 | else | |||
| 1168 | (piece->prev)->next = piece->next; | |||
| 1169 | ||||
| 1170 | if (piece->next != NULL((void*)0)) | |||
| 1171 | (piece->next)->prev = piece->prev; | |||
| 1172 | ||||
| 1173 | if (!src->ascii_src.use_string_in_place) | |||
| 1174 | XtFree(piece->text); | |||
| 1175 | ||||
| 1176 | XtFree((char *)piece); | |||
| 1177 | } | |||
| 1178 | ||||
| 1179 | /* Function Name: FindPiece | |||
| 1180 | * Description: Finds the piece containing the position indicated. | |||
| 1181 | * Arguments: src - The AsciiSrc Widget. | |||
| 1182 | * position - the position that we are searching for. | |||
| 1183 | * RETURNED first - the position of the first character in this piece. | |||
| 1184 | * Returns: piece - the piece that contains this position. | |||
| 1185 | */ | |||
| 1186 | ||||
| 1187 | static Piece * | |||
| 1188 | FindPiece(AsciiSrcObject src, XawTextPosition position, XawTextPosition * first) | |||
| 1189 | { | |||
| 1190 | Piece * old_piece = NULL((void*)0), * piece = src->ascii_src.first_piece; | |||
| 1191 | XawTextPosition temp; | |||
| 1192 | ||||
| 1193 | for ( temp = 0 ; piece != NULL((void*)0) ; temp += piece->used, piece = piece->next ) { | |||
| 1194 | *first = temp; | |||
| 1195 | old_piece = piece; | |||
| 1196 | ||||
| 1197 | if ((temp + piece->used) > position) | |||
| 1198 | return(piece); | |||
| 1199 | } | |||
| 1200 | return(old_piece); /* if we run off the end the return the last piece */ | |||
| 1201 | } | |||
| 1202 | ||||
| 1203 | /* Function Name: MyStrncpy | |||
| 1204 | * Description: Just like string copy, but slower and will always | |||
| 1205 | * work on overlapping strings. | |||
| 1206 | * Arguments: (same as strncpy) - s1, s2 - strings to copy (2->1). | |||
| 1207 | * n - the number of chars to copy. | |||
| 1208 | * Returns: s1. | |||
| 1209 | */ | |||
| 1210 | ||||
| 1211 | static String | |||
| 1212 | MyStrncpy(char * s1, char * s2, int n) | |||
| 1213 | { | |||
| 1214 | char buf[256]; | |||
| 1215 | char* temp; | |||
| 1216 | ||||
| 1217 | if (n == 0) return s1; | |||
| 1218 | ||||
| 1219 | if (n < sizeof buf) temp = buf; | |||
| 1220 | else temp = XtMalloc((unsigned)sizeof(unsigned char) * n); | |||
| 1221 | ||||
| 1222 | strncpy(temp, s2, n)__builtin___strncpy_chk (temp, s2, n, __builtin_object_size ( temp, 2 > 1 ? 1 : 0)); /* Saber has a bug that causes it to generate*/ | |||
| 1223 | strncpy(s1, temp, n)__builtin___strncpy_chk (s1, temp, n, __builtin_object_size ( s1, 2 > 1 ? 1 : 0)); /* a bogus warning message here (CDP 6/32/89)*/ | |||
| 1224 | ||||
| 1225 | if (temp != buf) XtFree(temp); | |||
| 1226 | return s1; | |||
| 1227 | } | |||
| 1228 | ||||
| 1229 | /* Function Name: BreakPiece | |||
| 1230 | * Description: Breaks a full piece into two new pieces. | |||
| 1231 | * Arguments: src - The AsciiSrc Widget. | |||
| 1232 | * piece - the piece to break. | |||
| 1233 | * Returns: none. | |||
| 1234 | */ | |||
| 1235 | ||||
| 1236 | #define HALF_PIECE(src->ascii_src.piece_size/2) (src->ascii_src.piece_size/2) | |||
| 1237 | ||||
| 1238 | static void | |||
| 1239 | BreakPiece(AsciiSrcObject src, Piece * piece) | |||
| 1240 | { | |||
| 1241 | Piece * new = AllocNewPiece(src, piece); | |||
| 1242 | ||||
| 1243 | new->text = XtMalloc(src->ascii_src.piece_size * sizeof(unsigned char)); | |||
| 1244 | strncpy(new->text, piece->text + HALF_PIECE,__builtin___strncpy_chk (new->text, piece->text + (src-> ascii_src.piece_size/2), src->ascii_src.piece_size - (src-> ascii_src.piece_size/2), __builtin_object_size (new->text, 2 > 1 ? 1 : 0)) | |||
| 1245 | src->ascii_src.piece_size - HALF_PIECE)__builtin___strncpy_chk (new->text, piece->text + (src-> ascii_src.piece_size/2), src->ascii_src.piece_size - (src-> ascii_src.piece_size/2), __builtin_object_size (new->text, 2 > 1 ? 1 : 0)); | |||
| 1246 | piece->used = HALF_PIECE(src->ascii_src.piece_size/2); | |||
| 1247 | new->used = src->ascii_src.piece_size - HALF_PIECE(src->ascii_src.piece_size/2); | |||
| 1248 | } | |||
| 1249 | ||||
| 1250 | /* ARGSUSED */ | |||
| 1251 | static void | |||
| 1252 | CvtStringToAsciiType(XrmValuePtr args, Cardinal * num_args, XrmValuePtr fromVal, | |||
| 1253 | XrmValuePtr toVal) | |||
| 1254 | { | |||
| 1255 | static XawAsciiType type; | |||
| 1256 | static XrmQuark XtQEstring = NULLQUARK((XrmQuark) 0); | |||
| 1257 | static XrmQuark XtQEfile; | |||
| 1258 | XrmQuark q; | |||
| 1259 | char lowerName[40]; | |||
| 1260 | ||||
| 1261 | if (XtQEstring == NULLQUARK((XrmQuark) 0)) { | |||
| 1262 | XtQEstring = XrmPermStringToQuark(XtEstring"string"); | |||
| 1263 | XtQEfile = XrmPermStringToQuark(XtEfile"file"); | |||
| 1264 | } | |||
| 1265 | ||||
| 1266 | if (strlen ((char*)fromVal->addr) < sizeof lowerName) { | |||
| 1267 | XmuCopyISOLatin1Lowered(lowerName, (char *) fromVal->addr); | |||
| 1268 | q = XrmStringToQuark(lowerName); | |||
| 1269 | ||||
| 1270 | if (q == XtQEstring) type = XawAsciiString; | |||
| 1271 | else if (q == XtQEfile) type = XawAsciiFile; | |||
| 1272 | else { | |||
| 1273 | toVal->size = 0; | |||
| 1274 | toVal->addr = NULL((void*)0); | |||
| 1275 | return; | |||
| 1276 | } | |||
| 1277 | toVal->size = sizeof type; | |||
| 1278 | toVal->addr = (XPointer) &type; | |||
| 1279 | return; | |||
| 1280 | } | |||
| 1281 | toVal->size = 0; | |||
| 1282 | toVal->addr = NULL((void*)0); | |||
| 1283 | } | |||
| 1284 | ||||
| 1285 | #if (defined(ASCII_STRING) || defined(ASCII_DISK)) | |||
| 1286 | # include <X11/Xaw3d/Cardinals.h> | |||
| 1287 | #endif | |||
| 1288 | ||||
| 1289 | #ifdef ASCII_STRING | |||
| 1290 | /************************************************************ | |||
| 1291 | * | |||
| 1292 | * Compatability functions. | |||
| 1293 | * | |||
| 1294 | ************************************************************/ | |||
| 1295 | ||||
| 1296 | /* Function Name: AsciiStringSourceCreate | |||
| 1297 | * Description: Creates a string source. | |||
| 1298 | * Arguments: parent - the widget that will own this source. | |||
| 1299 | * args, num_args - the argument list. | |||
| 1300 | * Returns: a pointer to the new text source. | |||
| 1301 | */ | |||
| 1302 | ||||
| 1303 | Widget | |||
| 1304 | XawStringSourceCreate(Widget parent, ArgList args, Cardinal num_args) | |||
| 1305 | { | |||
| 1306 | XawTextSource src; | |||
| 1307 | ArgList ascii_args; | |||
| 1308 | Arg temp[2]; | |||
| 1309 | ||||
| 1310 | XtSetArg(temp[0], XtNtype, XawAsciiString)((void)( (temp[0]).name = ("type"), (temp[0]).value = (XtArgVal )(XawAsciiString) )); | |||
| 1311 | XtSetArg(temp[1], XtNuseStringInPlace, TRUE)((void)( (temp[1]).name = ("useStringInPlace"), (temp[1]).value = (XtArgVal)(1) )); | |||
| 1312 | ascii_args = XtMergeArgLists(temp, TWO, args, num_args); | |||
| 1313 | ||||
| 1314 | src = XtCreateWidget("genericAsciiString", asciiSrcObjectClass, parent, | |||
| 1315 | ascii_args, num_args + TWO); | |||
| 1316 | XtFree((char *)ascii_args); | |||
| 1317 | return(src); | |||
| 1318 | } | |||
| 1319 | ||||
| 1320 | /* | |||
| 1321 | * This is hacked up to try to emulate old functionality, it | |||
| 1322 | * may not work, as I have not old code to test it on. | |||
| 1323 | * | |||
| 1324 | * Chris D. Peterson 8/31/89. | |||
| 1325 | */ | |||
| 1326 | ||||
| 1327 | void | |||
| 1328 | XawTextSetLastPos (Widget w, XawTextPosition lastPos) | |||
| 1329 | { | |||
| 1330 | AsciiSrcObject src = (AsciiSrcObject) XawTextGetSource(w); | |||
| 1331 | ||||
| 1332 | src->ascii_src.piece_size = lastPos; | |||
| 1333 | } | |||
| 1334 | #endif /* ASCII_STRING */ | |||
| 1335 | ||||
| 1336 | #ifdef ASCII_DISK | |||
| 1337 | /* Function Name: AsciiDiskSourceCreate | |||
| 1338 | * Description: Creates a disk source. | |||
| 1339 | * Arguments: parent - the widget that will own this source. | |||
| 1340 | * args, num_args - the argument list. | |||
| 1341 | * Returns: a pointer to the new text source. | |||
| 1342 | */ | |||
| 1343 | ||||
| 1344 | Widget | |||
| 1345 | XawDiskSourceCreate(Widget parent, ArgList args, Cardinal num_args) | |||
| 1346 | { | |||
| 1347 | XawTextSource src; | |||
| 1348 | ArgList ascii_args; | |||
| 1349 | Arg temp[1]; | |||
| 1350 | int i; | |||
| 1351 | ||||
| 1352 | XtSetArg(temp[0], XtNtype, XawAsciiFile)((void)( (temp[0]).name = ("type"), (temp[0]).value = (XtArgVal )(XawAsciiFile) )); | |||
| 1353 | ascii_args = XtMergeArgLists(temp, ONE, args, num_args); | |||
| 1354 | num_args++; | |||
| 1355 | ||||
| 1356 | for (i = 0; i < num_args; i++) | |||
| 1357 | if (streq(ascii_args[i].name, XtNfile)( strcmp((ascii_args[i].name), (((char*)&XtStrings[194])) ) == 0 ) || | |||
| 1358 | streq(ascii_args[i].name, XtCFile)( strcmp((ascii_args[i].name), (((char*)&XtStrings[1012]) )) == 0 )) | |||
| 1359 | ascii_args[i].name = XtNstring((char*)&XtStrings[733]); | |||
| 1360 | ||||
| 1361 | src = XtCreateWidget("genericAsciiDisk", asciiSrcObjectClass, parent, | |||
| 1362 | ascii_args, num_args); | |||
| 1363 | XtFree((char *)ascii_args); | |||
| 1364 | return(src); | |||
| 1365 | } | |||
| 1366 | #endif /* ASCII_DISK */ |