home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xdbx / part01 / command.c next >
Encoding:
C/C++ Source or Header  |  1989-03-14  |  9.6 KB  |  365 lines

  1. /****************************************************************************** 
  2.  *
  3.  *  xdbx - X Window System interface to dbx
  4.  *
  5.  *  Copyright 1989 The University of Texas at Austin
  6.  *
  7.  *  Author:    Po Cheung
  8.  *  Date:    March 10, 1989
  9.  *
  10.  *  Permission to use, copy, modify, and distribute this software and
  11.  *  its documentation for any purpose and without fee is hereby granted,
  12.  *  provided that the above copyright notice appear in all copies and that
  13.  *  both that copyright notice and this permission notice appear in
  14.  *  supporting documentation.  The University of Texas at Austin makes no 
  15.  *  representations about the suitability of this software for any purpose.  
  16.  *  It is provided "as is" without express or implied warranty.
  17.  *
  18.  ******************************************************************************/
  19.  
  20.  
  21. #include <signal.h>
  22. #include <ctype.h>
  23. #include <X11/Xos.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #ifdef sparc
  27. #include <dirent.h>
  28. #else
  29. #include <sys/dir.h>
  30. #endif
  31. #include "global.h"
  32.  
  33. char         Command[LINESIZ] = "";    /* xdbx command string */
  34.  
  35. static String      filelist[MAXFILES];     /* list of file names in fileMenu */
  36. static int    nfiles = 0;        /* number of files in filelist */
  37. static Widget    fileMenu = NULL,     /* list widget as file menu */
  38.         popupshell;        /* parent of fileMenu */
  39.  
  40.  
  41. /* ARGSUSED */
  42. static void DoIt (w, client_data, call_data)
  43.     Widget w;
  44.     caddr_t client_data;
  45.     caddr_t call_data;
  46. {
  47.     /* run, cont, next, step, where, up, down, status */
  48.     strcpy(Command, client_data);
  49.     AppendDialogText(Command);
  50.     writeDbx(Command);
  51. }
  52.  
  53. /* ARGSUSED */
  54. static void Stop_at(w, client_data, call_data)
  55.     Widget w;
  56.     caddr_t client_data;
  57.     caddr_t call_data;
  58. {
  59.     XtTextPosition pos;
  60.     Line line;
  61.  
  62.     if (displayedFile == NULL) {
  63.     Bell(0);
  64.     return;
  65.     }
  66.     pos = XtTextGetInsertionPoint(sourceWindow);
  67.     line = TextPositionToLine(pos);
  68.     sprintf(Command, "stop at %d\n", line);
  69.     AppendDialogText(Command);
  70.     writeDbx(Command);
  71. }
  72.  
  73. /* ARGSUSED */
  74. static void Stop_in(w, client_data, call_data)
  75.     Widget w;
  76.     caddr_t client_data;
  77.     caddr_t call_data;
  78. {
  79.     char *funcname;
  80.     int  nbytes;
  81.  
  82.     funcname = XFetchBytes(XtDisplay(w), &nbytes);    /* from CUT_BUFFER0 */
  83.     if (nbytes == 0) {
  84.     XBell(XtDisplay(w), 0);
  85.     return;
  86.     }
  87.     sprintf(Command, "stop in %s\n", funcname);
  88.     AppendDialogText(Command);
  89.     writeDbx(Command);
  90. }
  91.  
  92. /*
  93.  *  Delete removes the stop_no associated with a given line number.
  94.  *  RemoveStop() is called to undisplay the stop sign only when there
  95.  *  are no more stop_no's associated with that line number.
  96.  */
  97. /* ARGSUSED */
  98. static void Delete(w, client_data, call_data)
  99.     Widget w;
  100.     caddr_t client_data;
  101.     caddr_t call_data;
  102. {
  103.     XtTextPosition pos;
  104.     Line       line;
  105.     Cardinal       i;
  106.  
  107.     if (displayedFile == NULL) {
  108.     XBell(XtDisplay(w), 0);
  109.     return;
  110.     }
  111.     pos = XtTextGetInsertionPoint(sourceWindow);
  112.     line = TextPositionToLine(pos);
  113.     if (i = LineToStop_no(line)) {
  114.         sprintf(Command, "delete %d\n", i);
  115.         AppendDialogText(Command);
  116.         writeDbx(Command);
  117.     }
  118.     else
  119.     XBell(XtDisplay(w), 0);
  120. }
  121.  
  122. /* ARGSUSED */
  123. static void Print(w, client_data, call_data)
  124.     Widget w;
  125.     caddr_t client_data;
  126.     caddr_t call_data;
  127. {
  128.     char *string;
  129.     int nbytes;
  130.  
  131.     string = XFetchBytes(XtDisplay(w), &nbytes);
  132.     if (nbytes == 0) {
  133.     XBell(XtDisplay(w), 0);
  134.     return;
  135.     }
  136.     if ((int)client_data == 0)
  137.         sprintf(Command, "print %s\n", string);
  138.     else if ((int)client_data == 1)
  139.         sprintf(Command, "print *%s\n", string);
  140.     AppendDialogText(Command);
  141.     writeDbx(Command);
  142. }
  143.  
  144. /* ARGSUSED */
  145. static void Func(w, client_data, call_data)
  146.     Widget w;
  147.     caddr_t client_data;
  148.     caddr_t call_data;
  149. {
  150.     char *funcname;
  151.     int nbytes;
  152.  
  153.     funcname = XFetchBytes(XtDisplay(w), &nbytes);
  154.     sprintf(Command, "func %s\n", funcname);
  155.     AppendDialogText(Command);
  156.     writeDbx(Command);
  157. }
  158.  
  159. /* ARGSUSED */
  160. /*  Callback for the fileMenu list widget, display the file selected in the 
  161.  *  menu on the source window.
  162.  */
  163. static void DisplayMenuFile(w, client_data, call_data)
  164.     Widget w;
  165.     Widget client_data;
  166.     XtListReturnStruct *call_data;
  167. {
  168.     XtPopdown(client_data);
  169.     if (call_data->string == NULL || 
  170.     strcmp(call_data->string, CANCEL) == NULL) return;
  171.     displayedFile = LoadFile(call_data->string);
  172.     sprintf(Command, "file %s\n", call_data->string);
  173.     AppendDialogText(Command);
  174.     writeDbx(Command);
  175. }
  176.  
  177.  
  178. /*  A directory entry is included in the display list if it is a regular
  179.  *  file but not an executable file.
  180.  */
  181. static int InList(entry)
  182. Directory *entry;
  183. {
  184.     char buffer[16];
  185.     int i, n;
  186.     FILE *fp;
  187.  
  188.     if ((fp = fopen(entry->d_name, "r")) == NULL)
  189.         return False;
  190.     if (n = fread(buffer, sizeof(char), sizeof(buffer), fp)) {
  191.         fclose(fp);
  192.         for (i=0; i<n && (isprint(buffer[i]) || isspace(buffer[i])); i++);
  193.         return ((i-sizeof(buffer)) ? False : True);
  194.     }
  195.     fclose(fp);
  196.     return False;
  197. }
  198.  
  199. /*  Scans the working directory for files selected by InList(), sorted
  200.  *  alphabetically, and stored in an array of pointers to directory
  201.  *  entries called namelist.
  202.  *  The names of the selected files are stored in filelist.
  203.  */
  204. static void ScanDir()
  205. {
  206.     extern     alphasort();
  207.     Directory   **namelist;
  208.     char     *dir, message[LINESIZ];
  209.     int        i;
  210.  
  211.     i = 0;
  212.     filelist[i++] = "";
  213.     dir = dbxpwd();
  214.     nfiles = scandir(dir, &namelist, InList, alphasort);
  215.     for (i=1; i<nfiles; i++) {
  216.     filelist[i] = XtNewString(namelist[i]->d_name);
  217.     }
  218.     filelist[0] = CANCEL;
  219.     filelist[i] = NULL;
  220.     return;
  221. }
  222.  
  223. /*  Creates a popup shell with its child being a list widget containing
  224.  *  file names returned from ScanDir().
  225.  *  When an item in the list is selected, DisplayMenuFile will be called.
  226.  */
  227. static void SetUpFileMenu() 
  228. {
  229.     Arg     args[MAXARGS];
  230.     Cardinal    n;
  231.     int        ncolumns;
  232.  
  233.     static String translations = "\
  234.     <Btn1Down>,<Btn1Up>:    Set() Notify() Unset()";
  235.  
  236.     n = 0;
  237.     popupshell = XtCreatePopupShell("popup", overrideShellWidgetClass, 
  238.                     toplevel, args, n);
  239.  
  240.     ScanDir();
  241.     n = 0;
  242.     ncolumns = nfiles/app_resources.filesPerColumn + 1;
  243.     XtSetArg(args[n], XtNlist, filelist);                n++;
  244.     XtSetArg(args[n], XtNverticalList, True);                n++;
  245.     XtSetArg(args[n], XtNcolumnSpacing, app_resources.columnSpacing);    n++;
  246.     XtSetArg(args[n], XtNdefaultColumns, (XtArgVal) ncolumns);        n++;
  247.     XtSetArg(args[n], XtNtranslations, XtParseTranslationTable(translations));
  248.     n++;
  249.     fileMenu = XtCreateManagedWidget("fileMenu", listWidgetClass, 
  250.                      popupshell, args, n);
  251.     XtAddCallback(fileMenu, XtNcallback, DisplayMenuFile, popupshell);
  252. }
  253.  
  254. /*  Destroy the existing popup shell and creates another one. */
  255. void UpdateFileMenu()
  256. {
  257.     XtDestroyWidget(popupshell);
  258.     SetUpFileMenu();
  259. }
  260.     
  261. /* ARGSUSED */
  262. static void File(w, client_data, call_data)
  263.     Widget w;
  264.     caddr_t client_data;
  265.     caddr_t call_data;
  266. {
  267.     Arg     args[MAXARGS];
  268.     Cardinal    n;
  269.     Position    x, y, x_offset;
  270.     Dimension    fileMenu_width, border_width, source_width;
  271.  
  272.     n = 0;
  273.     XtSetArg(args[n], XtNwidth, &fileMenu_width);            n++;
  274.     XtSetArg(args[n], XtNborderWidth, &border_width);            n++;
  275.     XtGetValues(fileMenu, args, n);
  276.  
  277.     n = 0;
  278.     XtSetArg(args[n], XtNwidth, &source_width);                n++;
  279.     XtGetValues(sourceWindow, args, n);
  280.  
  281.     x_offset = (Position) (source_width - fileMenu_width - 2*border_width);
  282.     XtTranslateCoords(sourceWindow, x_offset, 0, &x, &y);
  283.     x = MAX(0, x);
  284.     y = MAX(0, y);
  285.     XtMoveWidget(popupshell, x, y);
  286.     XtPopup(popupshell, XtGrabNonexclusive);
  287. }
  288.  
  289.  
  290. /* ARGSUSED */
  291. static void Quit(w, client_data, call_data)
  292.     Widget w;
  293.     caddr_t client_data;
  294.     caddr_t call_data;
  295. {
  296.     union wait status;
  297.  
  298.     writeDbx("quit\n");
  299.     XtDestroyWidget(toplevel);
  300.     wait3(&status, WNOHANG, NULL);
  301.     exit(0);
  302. }
  303.  
  304. static void AddButton (parent, name, function, client_data)
  305. Widget parent;
  306. char *name;
  307. void (*function) ();
  308. caddr_t client_data;        /* callback registered data */
  309. {
  310.     Widget     button;
  311.     Arg     args[10];
  312.     Cardinal     n;
  313.  
  314.     n = 0;
  315.     XtSetArg(args[n], XtNwidth, (XtArgVal) app_resources.buttonWidth);    n++;
  316.     XtSetArg(args[n], XtNresize, (XtArgVal) False);            n++;
  317.     button = XtCreateManagedWidget(name, commandWidgetClass, parent, args, n);
  318.     XtAddCallback(button, XtNcallback, function, client_data);
  319. }
  320.  
  321. static void CreateButtons (parent)
  322. Widget parent;
  323. {
  324.     AddButton (parent, "run", DoIt, "run\n");
  325.     AddButton (parent, "cont", DoIt, "cont\n");
  326.     AddButton (parent, "next", DoIt, "next\n");
  327.     AddButton (parent, "step", DoIt, "step\n");
  328.  
  329.     AddButton (parent, "stop at", Stop_at, NULL);
  330.     AddButton (parent, "stop in", Stop_in, NULL);
  331.     AddButton (parent, "delete", Delete, NULL);
  332.     AddButton (parent, "status", DoIt, "status\n");
  333.  
  334.     AddButton (parent, "where", DoIt, "where\n");
  335.     AddButton (parent, "up", DoIt, "up\n");
  336.     AddButton (parent, "down", DoIt, "down\n");
  337.  
  338.     AddButton (parent, "print", Print, 0);
  339.     AddButton (parent, "print*", Print, 1);
  340.  
  341.     AddButton (parent, "func", Func, NULL);
  342.     AddButton (parent, "file", File, NULL);
  343.  
  344.     AddButton (parent, "quit", Quit, NULL);
  345. }
  346.  
  347. /*  Create a command widget, and the buttons, and set up the file menu.  */
  348. void CreateCommandPanel(parent)
  349. Widget parent;
  350. {
  351.     Arg args[10];
  352.     Cardinal n;
  353.  
  354.     n = 0;
  355.     XtSetArg(args[n], XtNhSpace, app_resources.commandHSpace);        n++;
  356.     XtSetArg(args[n], XtNvSpace, app_resources.commandVSpace);        n++;
  357.     XtSetArg(args[n], XtNmax, app_resources.commandMaxHeight);        n++;
  358.     XtSetArg(args[n], XtNmin, app_resources.commandMinHeight);        n++;
  359.     XtSetArg(args[n], XtNallowResize, (XtArgVal) True);            n++;
  360.     commandWindow = XtCreateManagedWidget("commandWindow", boxWidgetClass, 
  361.                       parent, args, n);
  362.     CreateButtons(commandWindow);
  363.     SetUpFileMenu();
  364. }
  365.