home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * xdbx - X Window System interface to dbx
- *
- * Copyright 1989 The University of Texas at Austin
- *
- * Author: Po Cheung
- * Date: March 10, 1989
- *
- * Permission to use, copy, modify, and distribute this software and
- * its documentation for any purpose and without fee is hereby granted,
- * provided that the above copyright notice appear in all copies and that
- * both that copyright notice and this permission notice appear in
- * supporting documentation. The University of Texas at Austin makes no
- * representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- ******************************************************************************/
-
-
- #include "global.h"
- #include <X11/Xos.h>
- #include <sys/stat.h>
-
- static FileRec fileTable[MAXFILES]; /* table of file records */
- FileRec *displayedFile; /* pointer to table entry of currently
- displayed file */
- Boolean Echo = True; /* to intercept dbx output */
-
-
- void source_init()
- {
- int i;
-
- for (i=0; i<MAXFILES; i++) {
- fileTable[i].filename = XtNewString("");
- }
- }
-
- /*
- * Update topline, bottomline, arrow sign, updown sign, stop signs, and
- * line label. Invoked by scrollbar action.
- */
- /* ARGSUSED */
- static XtActionProc Update(w, event, params, num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- TextWidget ctx = (TextWidget) sourceWindow;
- XtTextPosition pos;
- Line line;
- FileRec *file;
-
- if (displayedFile) {
- file = displayedFile;
- pos = ctx->text.lt.top;
- for(line=1; pos >= file->linepos[line]; line++);
- if (file->topline != line-1) {
- file->topline = line-1;
- file->bottomline = MIN (file->topline + file->lines - 1,
- file->lastline);
- XtTextSetInsertionPoint(sourceWindow, file->linepos[file->topline]);
- UpdateLineLabel(file->topline);
- UpdateStops(file);
- UpdateArrow(file);
- UpdateUpdown(file);
- }
- }
- }
-
- /* Invoked by left mouse button down, update the line label.
- */
- /* ARGSUSED */
- static XtActionProc UpdateLine(w, event, params, num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- XtTextPosition pos;
- Line line;
-
- pos = XtTextGetInsertionPoint(sourceWindow);
- line = TextPositionToLine(pos);
- UpdateLineLabel(line);
- }
-
- /*
- * Update bottomline, arrow sign, updown sign and stop signs
- * Invoked by ConfigureNotify event.
- * Note that topline never changes with resize, only bottomline gets changed.
- */
- /* ARGSUSED */
- static XtActionProc NotifyResize(w, event, params, num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- TextWidget ctx = (TextWidget) sourceWindow;
- FileRec *file;
-
- if (file = displayedFile) {
- file->lines = ctx->text.lt.lines;
- file->bottomline = MIN (file->topline + file->lines - 1,
- file->lastline);
- UpdateStops(file);
- UpdateArrow(file);
- UpdateUpdown(file);
- }
- }
-
-
- /*
- * On top of a form widget, we have a text widget with scrollbar, a label
- * widget for the arrow sign, one for the updown sign and some for stop signs.
- */
- void CreateSourceWindow(parent)
- Widget parent;
- {
- TextWidget ctx;
- Arg args[MAXARGS];
- Cardinal n;
-
- static XtActionsRec actionTable[] = {
- {"NotifyResize", (XtActionProc) NotifyResize},
- {"MySelectWord", (XtActionProc) MySelectWord},
- {"UpdateLine", (XtActionProc) UpdateLine},
- {"Update", (XtActionProc) Update},
- {NULL, NULL}
- };
-
- static String translations = "\
- <Btn1Down>: select-start() ClearCutBuffer0() UpdateLine()\n\
- <Btn1Up>(2): MySelectWord()";
-
- static String sbarTranslations = "\
- <Configure>: NotifyResize() \n\
- <Btn2Down>: StartScroll(Continuous) MoveThumb() NotifyThumb() \
- Update() \n\
- <Btn2Motion>: MoveThumb() NotifyThumb() Update() \n\
- <BtnUp>: NotifyScroll(Proportional) EndScroll() Update()";
-
-
- n = 0;
- XtSetArg(args[n], XtNdefaultDistance, 0); n++;
- sourceWidget = XtCreateManagedWidget("sourceWidget", formWidgetClass,
- parent, args, n);
-
- n = 0;
- XtSetArg(args[n], XtNheight, app_resources.sourceHeight); n++;
- XtSetArg(args[n], XtNleftMargin, app_resources.leftMargin); n++;
- XtSetArg(args[n], XtNborderWidth, 0); n++;
- XtSetArg(args[n], XtNtextOptions, (XtArgVal) scrollVertical); n++;
-
- sourceWindow = XtCreateManagedWidget("sourceWindow", asciiStringWidgetClass,
- sourceWidget, args, n);
- ctx = (TextWidget) sourceWindow;
- XtOverrideTranslations(sourceWindow,
- XtParseTranslationTable(translations));
- XtOverrideTranslations(ctx->text.sbar,
- XtParseTranslationTable(sbarTranslations));
- XtAddActions(actionTable, XtNumber(actionTable));
- }
-
-
- /*
- * Build the array which gives the starting text position of each line
- * look for CR, get the position, add 1, which becomes the starting
- * position of next line.
- */
- static void BuildLinePos (file)
- FileRec *file;
- {
- char *s;
- Line line, nlines;
-
- nlines = file->filesize/CHARS_PER_LINE;
- file->linepos = (XtTextPosition *)XtMalloc(nlines * sizeof(XtTextPosition));
- s = file->buf;
- line = 0;
- file->linepos[line++] = 0;
- file->linepos[line++] = 0;
- while (*s) {
- if (*s++ == '\n') {
- if (line == nlines) {
- file->linepos = (XtTextPosition *) XtRealloc (file->linepos,
- (nlines + ADD_LINES) * sizeof(XtTextPosition));
- nlines += ADD_LINES;
- }
- file->linepos[line++] = s - file->buf;
- }
- }
- file->lastline = line - 2;
- file->linepos = (XtTextPosition *) XtRealloc
- (file->linepos, line * sizeof(XtTextPosition));
- }
-
-
- static long GetFileSize(fd)
- int fd;
- {
- struct stat fileinfo;
-
- if (fstat(fd, &fileinfo))
- return -1;
- return (fileinfo.st_size + 1);
- }
-
- /*
- * Look up the file table for an entry with "filename"
- * If not found, create an entry and initialize proper fields,
- * else, return pointer to entry found.
- */
- static FileRec *LookUpFileTable(filename, fd)
- char *filename;
- int fd;
- {
- int i, c1, c2;
- char message[LINESIZ];
-
- for (i=0; fileTable[i].filename &&
- (c1 = strcmp(fileTable[i].filename, "")) &&
- (c2 = strcmp(fileTable[i].filename, filename)) &&
- i < MAXFILES; i++);
-
- if (i >= MAXFILES) { /* too many files */
- i = 0;
- c1 = 0;
- XtFree(fileTable[i].buf);
- }
- if (c1 == 0) { /* file does not exist in table */
- if ((fileTable[i].filesize = GetFileSize(fd)) == -1)
- return NULL ;
- fileTable[i].buf = XtMalloc(fileTable[i].filesize);
- if (read(fd, fileTable[i].buf, fileTable[i].filesize) == -1) {
- sprintf(message, "Can't read %s: read error\n", filename);
- UpdateMessageWindow(message);
- XtFree(fileTable[i].buf);
- return NULL;
- }
- fileTable[i].filename = XtNewString(filename);
- strcpy(fileTable[i].funcname, "");
- fileTable[i].currentline = 1;
- fileTable[i].topline = 1;
- fileTable[i].bottomline = 0;
- fileTable[i].topPosition = 0;
- BuildLinePos(&fileTable[i]);
- return &fileTable[i];
- }
- if (c2 == 0) { /* file exists in table */
- return &fileTable[i];
- }
- }
-
-
- /*
- * Get the name of the file returned by the 'file' command to dbx
- * If no source file is specified, QueryFile returns NULL
- */
- char *QueryFile()
- {
- char *string, s[LINESIZ], t[LINESIZ];
-
- Echo = FALSE;
- writeDbx("file\n");
- while (fgets(s, LINESIZ, dbxfp) == NULL);
- if (string = (char *) strtok(s, " \n")) {
- if (strtok(NULL, " \n"))
- string = NULL;
- }
- while (fgets(t, LINESIZ, dbxfp));
- Echo = TRUE;
- if (string)
- return XtNewString(string);
- else
- return NULL;
- }
-
- /*
- * Remember file position before closing.
- * Clear field funcname for proper operation of parse() & UpdateFuncLabel()
- */
- static void SaveDisplayedFileInfo()
- {
- XtTextPosition pos;
-
- if (displayedFile) {
- displayedFile->topPosition = XtTextTopPosition(sourceWindow);
- pos = XtTextGetInsertionPoint(sourceWindow);
- displayedFile->currentline = TextPositionToLine(pos);
- strcpy(displayedFile->funcname, "");
- }
- }
-
-
- static void DisplayFile(file)
- FileRec *file;
- {
- Arg args[MAXARGS];
- Cardinal n;
- TextWidget ctx = (TextWidget) sourceWindow;
-
- n = 0;
- XtSetArg(args[n], XtNstring, (XtArgVal) file->buf); n++;
- XtSetArg(args[n], XtNlength, (XtArgVal) file->filesize); n++;
- XtSetArg(args[n], XtNeditType, (XtArgVal) XttextRead); n++;
-
- XtTextSetSource(sourceWindow,
- XtStringSourceCreate(sourceWindow, args, n),
- file->topPosition);
- file->lines = ctx->text.lt.lines;
- file->bottomline = MIN (file->topline + file->lines - 1, file->lastline);
- }
-
-
- static void FullPath(filename, path)
- char *filename, *path;
- {
- char *dir;
-
- dir = dbxpwd();
- if (dir) {
- strcpy(path, dir);
- strcat(path, "/");
- strcat(path, filename);
- XtFree(dir);
- }
- else
- strcpy(path, filename);
- }
-
- /*
- * Given a file name, LoadFile attempts to open it and displays it onto
- * the source window.
- * LookUpFileTable checks if the file is already in the file table.
- * if it exists, a pointer to the file structure is retured;
- * otherwise, an entry is created and its info initialized;
- * returns NULL only if file table full.
- * SaveDisplayedFileInfo saves important information about the file
- * back in the file table; they include: topPosition, breakpoints.
- * DisplayFile displays the file onto the source window. It
- * uses topPosition to remember where it was last opened. But it
- * must recalculate bottomline because the window size might be
- * different.
- */
- FileRec *LoadFile(filename)
- char *filename;
- {
- FileRec *file;
- int fd;
- char message[LINESIZ];
- char pathname[LINESIZ];
-
- if (filename == NULL)
- return displayedFile;
- if (displayedFile && strcmp(filename, displayedFile->filename) == 0)
- return displayedFile;
- FullPath(filename, pathname);
- if ((fd = open(pathname, O_RDONLY)) == -1) {
- sprintf(message, "open: file not found : %s", pathname);
- UpdateMessageWindow(message);
- return displayedFile;
- }
- else if (file = LookUpFileTable(filename, fd)) {
- SaveDisplayedFileInfo();
- DisplayFile(file);
- UpdateFileLabel(file->filename);
- XtTextSetInsertionPoint(sourceWindow, file->linepos[file->currentline]);
- UpdateLineLabel(file->currentline);
- UpdateStops(file);
- UpdateArrow(file);
- UpdateUpdown(file);
- close(fd);
- return file;
- }
- else {
- close(fd);
- return displayedFile;
- }
- }
-