home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xdbx / part01 / dbx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  5.5 KB  |  194 lines

  1. /*****************************************************************************
  2.  *
  3.  *  xdbx - X Window System interface to the dbx debugger
  4.  *
  5.  *  Copyright 1989 The University of Texas at Austin
  6.  *  Copyright 1990 Microelectronics and Computer Technology Corporation
  7.  *
  8.  *  Permission to use, copy, modify, and distribute this software and its
  9.  *  documentation for any purpose and without fee is hereby granted,
  10.  *  provided that the above copyright notice appear in all copies and that
  11.  *  both that copyright notice and this permission notice appear in
  12.  *  supporting documentation, and that the name of The University of Texas
  13.  *  and Microelectronics and Computer Technology Corporation (MCC) not be 
  14.  *  used in advertising or publicity pertaining to distribution of
  15.  *  the software without specific, written prior permission.  The
  16.  *  University of Texas and MCC makes no representations about the 
  17.  *  suitability of this software for any purpose.  It is provided "as is" 
  18.  *  without express or implied warranty.
  19.  *
  20.  *  THE UNIVERSITY OF TEXAS AND MCC DISCLAIMS ALL WARRANTIES WITH REGARD TO
  21.  *  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22.  *  FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TEXAS OR MCC BE LIABLE FOR
  23.  *  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  24.  *  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  25.  *  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  26.  *  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  *
  28.  *  Author:      Po Cheung
  29.  *  Created:       March 10, 1989
  30.  *
  31.  *****************************************************************************/
  32.  
  33. /*
  34.  *  dbx.c
  35.  *
  36.  *    Handle dbx command initialization file (.dbxinit) and communication 
  37.  *    between dbx and xdbx.
  38.  *
  39.  *    dbx_init():    Handle .dbxinit
  40.  *    debug_init():    
  41.  *    read_dbx():    Read dbx output, parse and filter it before displaying
  42.  *            onto the dialog window.
  43.  *    write_dbx():    Send a command to dbx.
  44.  *    query_dbx():    Send a command to dbx and process it.
  45.  */
  46.  
  47. #include "global.h"
  48.  
  49. Boolean    Prompt;            /* True when dbx prompt arrives */
  50. char     *concat();
  51. char    *dbxprompt; 
  52. char    *xdbxprompt;
  53.  
  54. /*  Given a dbx command initialization file, this routine executes each dbx 
  55.  *  command in the file.  It sends the command to dbx, and calls read_dbx() 
  56.  *  directly to process output returned from dbx.
  57.  */
  58. static void dbx_init(xdbxinit)
  59. char *xdbxinit;
  60. {
  61.     FILE *fp;
  62.     char s[LINESIZ];
  63.  
  64.     if (strcmp(xdbxinit, "") == NULL)
  65.     return;
  66.     if (fp = fopen(xdbxinit, "r")) {
  67.     while (fgets(s, LINESIZ, fp)) {
  68.         send_command(s);
  69.         AppendDialogText(s);
  70.         Prompt = False;
  71.         while (!Prompt)
  72.         read_dbx();
  73.     }
  74.     close(fp);
  75.     }
  76. }
  77.  
  78. /*
  79.  *  This routine is called after getting the first dbx prompt.  
  80.  *  > check the use list to create a list of directories for searching
  81.  *    source files.
  82.  *  > ask dbx for the source file and display it if it exists.
  83.  *  > open the command initialization file and executed the commands;
  84.  *    if Tstartup is true, remove the initialization file.
  85.  */
  86. void debug_init()
  87. {
  88.     static visited = False;
  89.  
  90.     if (!visited) {
  91.     visited = True;
  92.     dbx_init(xdbxinit);
  93.     if (Tstartup)
  94.         unlink(xdbxinit);
  95.     strcpy(xdbxinit, "");
  96.     }
  97. }
  98.  
  99. /*
  100.  *  This is a callback procedure invoked everytime when input is pending
  101.  *  on the file descriptor to dbx.
  102.  *  o reads all the data available on the file descriptor line by line
  103.  *    into local variable 'string' and global variable 'output'.
  104.  *    'output' records the entire dbx output whereas 'string' records
  105.  *    only the data read in this invocation of read_dbx().
  106.  *  o in Echo mode, the contents in 'string' is edited by filter()
  107.  *    before it gets displayed on the dialog window.
  108.  *  o once the dbx prompt is read, calls parse() to analyse the dbx output
  109.  *    and take appropriate action.
  110.  */
  111. /* ARGSUSED */
  112. void read_dbx(master, source, id)
  113. XtPointer master;
  114. int       *source;
  115. XtInputId *id;
  116. {
  117.     static char *output = NULL;     /* buffer for dbx output */
  118.     static char *next_string = NULL;
  119.     static char *command;
  120.     char     *string = NULL;
  121.     char     s[LINESIZ];
  122.     Boolean     more;
  123.  
  124.     more = True;
  125.     while (more) {
  126.     Prompt = False;
  127.     /* keep reading until no more or until prompt arrives */
  128.     while (more = fgets(s, LINESIZ, dbxfp) && !Prompt) {
  129.         if (debug)
  130.         fprintf(stderr, "=>%s", s);
  131.         /* receive prompt? */
  132.         if (strncmp(s, dbxprompt, strlen(dbxprompt)) == NULL) {
  133.         Prompt = True;
  134.         /* more stuff behind prompt? */
  135.         if (s[strlen(dbxprompt)])
  136.             /* remember it */
  137.             next_string = XtNewString(s+strlen(dbxprompt));
  138.         /* destroy contents */
  139.         strcpy(s, "");
  140.         }
  141.         string = concat(string, s);
  142.         strcpy(s, "");
  143.     }
  144.     output = concat(output, string);
  145.     command = get_command();
  146.     if (Echo) {
  147.         filter(string, output, command);
  148.         if (Prompt) AppendDialogText(xdbxprompt);
  149.     }
  150.     if (string) {
  151.         XtFree(string);
  152.         string = NULL;
  153.     }
  154.     if (next_string) {
  155.         string = concat(string, next_string);
  156.         XtFree(next_string);
  157.         next_string = NULL;
  158.     }
  159.     if (Prompt) {
  160.         parse(output, command);
  161.         delete_command();
  162.         XtFree(output);
  163.         output = NULL;
  164.     }
  165.     }
  166. }
  167.  
  168. /*  Write string s to dbx, and flush the output.  */
  169.  
  170. void write_dbx(s)
  171. char *s;
  172. {
  173.     fputs(s, dbxfp);
  174.     fflush(dbxfp);
  175. }
  176.  
  177. /*  Sends a command to dbx and read the corresponding output, directly
  178.  *  invoking the Xt input procedure, read_dbx().
  179.  */
  180. void query_dbx(command)
  181. char *command;
  182. {
  183.     write_dbx(command);
  184.     insert_command(command);
  185.  
  186.     Echo = False;
  187.     Prompt = False;
  188.     while (!Prompt)
  189.         read_dbx();
  190.  
  191.     Parse = True;    /* Always reset Parse and Echo to True */
  192.     Echo = True;
  193. }
  194.