Release 970112
[wine/multimedia.git] / debugger / source.c
blobd80d386421c3a1a6abd76864b8f238e809af5c95
1 /*
2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <sys/mman.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <limits.h>
15 #include <strings.h>
16 #include <unistd.h>
17 #include <malloc.h>
19 #include "win.h"
20 #include "pe_image.h"
21 #include "peexe.h"
22 #include "debugger.h"
23 #include "peexe.h"
24 #include "xmalloc.h"
26 struct searchlist
28 char * path;
29 struct searchlist * next;
33 struct open_filelist
35 char * path;
36 char * real_path;
37 struct open_filelist * next;
38 unsigned int size;
39 signed int nlines;
40 unsigned int * linelist;
43 static struct open_filelist * ofiles;
45 static struct searchlist * listhead;
46 static char DEBUG_current_sourcefile[PATH_MAX];
47 static int DEBUG_start_sourceline = -1;
48 static int DEBUG_end_sourceline = -1;
50 void
51 DEBUG_ShowDir()
53 struct searchlist * sl;
55 fprintf(stderr,"Search list :\n");
56 for(sl = listhead; sl; sl = sl->next)
58 fprintf(stderr, "\t%s\n", sl->path);
60 fprintf(stderr, "\n");
63 void
64 DEBUG_AddPath(const char * path)
66 struct searchlist * sl;
68 sl = (struct searchlist *) xmalloc(sizeof(struct searchlist));
69 if( sl == NULL )
71 return;
74 sl->next = listhead;
75 sl->path = xstrdup(path);
76 listhead = sl;
79 void
80 DEBUG_NukePath()
82 struct searchlist * sl;
83 struct searchlist * nxt;
85 for(sl = listhead; sl; sl = nxt)
87 nxt = sl->next;
88 free(sl->path);
89 free(sl);
92 listhead = NULL;
95 static
96 void
97 DEBUG_DisplaySource(char * sourcefile, int start, int end)
99 char * addr;
100 char buffer[1024];
101 int fd;
102 int i;
103 struct open_filelist * ol;
104 int nlines;
105 char * pnt;
106 struct searchlist * sl;
107 struct stat statbuf;
108 int status;
109 char tmppath[PATH_MAX];
112 * First see whether we have the file open already. If so, then
113 * use that, otherwise we have to try and open it.
115 for(ol = ofiles; ol; ol = ol->next)
117 if( strcmp(ol->path, sourcefile) == 0 )
119 break;
123 if( ol == NULL )
126 * Try again, stripping the path from the opened file.
128 for(ol = ofiles; ol; ol = ol->next)
130 pnt = strrchr(ol->path, '/');
131 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
133 break;
139 if( ol == NULL )
142 * See if this is a DOS style name or not.
144 pnt = strchr(sourcefile, '\\' );
145 if( pnt == NULL )
147 pnt = strchr(sourcefile, '/' );
148 if( pnt == NULL )
150 pnt = sourcefile;
155 * Crapola. We need to try and open the file.
157 status = stat(sourcefile, &statbuf);
158 if( status != -1 )
160 strcpy(tmppath, sourcefile);
162 else
164 for(sl = listhead; sl; sl = sl->next)
166 strcpy(tmppath, sl->path);
167 if( tmppath[strlen(tmppath)-1] != '/' )
169 strcat(tmppath, "/");
172 * Now append the base file name.
174 strcat(tmppath, pnt);
176 status = stat(tmppath, &statbuf);
177 if( status != -1 )
179 break;
183 if( sl == NULL )
186 * Still couldn't find it. Ask user for path to add.
188 fprintf(stderr,"Enter path to file %s: ", sourcefile);
189 fgets(tmppath, sizeof(tmppath), stdin);
191 if( tmppath[strlen(tmppath)-1] == '\n' )
193 tmppath[strlen(tmppath)-1] = '\0';
196 if( tmppath[strlen(tmppath)-1] != '/' )
198 strcat(tmppath, "/");
201 * Now append the base file name.
203 strcat(tmppath, pnt);
205 status = stat(tmppath, &statbuf);
206 if( status == -1 )
209 * OK, I guess the user doesn't really want to see it
210 * after all.
212 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
213 ol->path = xstrdup(sourcefile);
214 ol->real_path = NULL;
215 ol->next = ofiles;
216 ol->nlines = 0;
217 ol->linelist = NULL;
218 ofiles = ol;
219 fprintf(stderr,"Unable to open file %s\n", tmppath);
220 return;
225 * Create header for file.
227 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
228 ol->path = xstrdup(sourcefile);
229 ol->real_path = xstrdup(tmppath);
230 ol->next = ofiles;
231 ol->nlines = 0;
232 ol->linelist = NULL;
233 ol->size = statbuf.st_size;
234 ofiles = ol;
237 * Now open and map the file.
239 fd = open(tmppath, O_RDONLY);
240 if( fd == -1 )
242 return;
245 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
246 if( addr == (char *) -1 )
248 return;
252 * Now build up the line number mapping table.
254 ol->nlines = 1;
255 pnt = addr;
256 while(pnt < addr + ol->size )
258 if( *pnt++ == '\n' )
260 ol->nlines++;
264 ol->nlines++;
265 ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
267 nlines = 0;
268 pnt = addr;
269 ol->linelist[nlines++] = 0;
270 while(pnt < addr + ol->size )
272 if( *pnt++ == '\n' )
274 ol->linelist[nlines++] = pnt - addr;
277 ol->linelist[nlines++] = pnt - addr;
280 else
283 * We know what the file is, we just need to reopen it and remap it.
285 fd = open(ol->real_path, O_RDONLY);
286 if( fd == -1 )
288 return;
291 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
292 if( addr == (char *) -1 )
294 return;
299 * All we need to do is to display the source lines here.
301 for(i=start - 1; i <= end - 1; i++)
303 if( i < 0 || i >= ol->nlines - 1)
305 continue;
308 memset(&buffer, 0, sizeof(buffer));
309 if( ol->linelist[i+1] != ol->linelist[i] )
311 memcpy(&buffer, addr + ol->linelist[i],
312 (ol->linelist[i+1] - ol->linelist[i]) - 1);
314 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
317 munmap(addr, ol->size);
318 close(fd);
322 void
323 DEBUG_List(struct list_id * source1, struct list_id * source2,
324 int delta)
326 int end;
327 int start;
328 char * sourcefile;
331 * We need to see what source file we need. Hopefully we only have
332 * one specified, otherwise we might as well punt.
334 if( source1 != NULL
335 && source2 != NULL
336 && source1->sourcefile != NULL
337 && source2->sourcefile != NULL
338 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
340 fprintf(stderr, "Ambiguous source file specification.\n");
341 return;
344 sourcefile = NULL;
345 if( source1 != NULL && source1->sourcefile != NULL )
347 sourcefile = source1->sourcefile;
350 if( sourcefile == NULL
351 && source2 != NULL
352 && source2->sourcefile != NULL )
354 sourcefile = source2->sourcefile;
357 if( sourcefile == NULL )
359 sourcefile = (char *) &DEBUG_current_sourcefile;
362 if( sourcefile == NULL )
364 fprintf(stderr, "No source file specified.\n");
365 return;
369 * Now figure out the line number range to be listed.
371 start = -1;
372 end = -1;
374 if( source1 != NULL )
376 start = source1->line;
379 if( source2 != NULL )
381 end = source2->line;
384 if( start == -1 && end == -1 )
386 if( delta < 0 )
388 end = DEBUG_start_sourceline;
389 start = end + delta;
391 else
393 start = DEBUG_end_sourceline;
394 end = start + delta;
397 else if( start == -1 )
399 start = end + delta;
401 else if (end == -1)
403 end = start + delta;
407 * Now call this function to do the dirty work.
409 DEBUG_DisplaySource(sourcefile, start, end);
411 if( sourcefile != (char *) &DEBUG_current_sourcefile )
413 strcpy(DEBUG_current_sourcefile, sourcefile);
415 DEBUG_start_sourceline = start;
416 DEBUG_end_sourceline = end;
421 #if 0
422 main()
424 int i, j;
425 DEBUG_AddPath("../../de");
426 while(1==1)
428 fscanf(stdin,"%d %d", &i, &j);
429 DEBUG_DisplaySource("dumpexe.c", i, j);
431 return 0;
433 #endif