Release 970202
[wine/multimedia.git] / debugger / source.c
blob8cad5683c6a8b3349690226bc3b8ea495e01431d
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 <string.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 int
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 int rtn;
107 struct searchlist * sl;
108 struct stat statbuf;
109 int status;
110 char tmppath[PATH_MAX];
113 * First see whether we have the file open already. If so, then
114 * use that, otherwise we have to try and open it.
116 for(ol = ofiles; ol; ol = ol->next)
118 if( strcmp(ol->path, sourcefile) == 0 )
120 break;
124 if( ol == NULL )
127 * Try again, stripping the path from the opened file.
129 for(ol = ofiles; ol; ol = ol->next)
131 pnt = strrchr(ol->path, '/');
132 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
134 break;
140 if( ol == NULL )
143 * See if this is a DOS style name or not.
145 pnt = strchr(sourcefile, '\\' );
146 if( pnt == NULL )
148 pnt = strchr(sourcefile, '/' );
149 if( pnt == NULL )
151 pnt = sourcefile;
156 * Crapola. We need to try and open the file.
158 status = stat(sourcefile, &statbuf);
159 if( status != -1 )
161 strcpy(tmppath, sourcefile);
163 else
165 for(sl = listhead; sl; sl = sl->next)
167 strcpy(tmppath, sl->path);
168 if( tmppath[strlen(tmppath)-1] != '/' )
170 strcat(tmppath, "/");
173 * Now append the base file name.
175 strcat(tmppath, pnt);
177 status = stat(tmppath, &statbuf);
178 if( status != -1 )
180 break;
184 if( sl == NULL )
187 * Still couldn't find it. Ask user for path to add.
189 fprintf(stderr,"Enter path to file %s: ", sourcefile);
190 fgets(tmppath, sizeof(tmppath), stdin);
192 if( tmppath[strlen(tmppath)-1] == '\n' )
194 tmppath[strlen(tmppath)-1] = '\0';
197 if( tmppath[strlen(tmppath)-1] != '/' )
199 strcat(tmppath, "/");
202 * Now append the base file name.
204 strcat(tmppath, pnt);
206 status = stat(tmppath, &statbuf);
207 if( status == -1 )
210 * OK, I guess the user doesn't really want to see it
211 * after all.
213 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
214 ol->path = xstrdup(sourcefile);
215 ol->real_path = NULL;
216 ol->next = ofiles;
217 ol->nlines = 0;
218 ol->linelist = NULL;
219 ofiles = ol;
220 fprintf(stderr,"Unable to open file %s\n", tmppath);
221 return FALSE;
226 * Create header for file.
228 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
229 ol->path = xstrdup(sourcefile);
230 ol->real_path = xstrdup(tmppath);
231 ol->next = ofiles;
232 ol->nlines = 0;
233 ol->linelist = NULL;
234 ol->size = statbuf.st_size;
235 ofiles = ol;
238 * Now open and map the file.
240 fd = open(tmppath, O_RDONLY);
241 if( fd == -1 )
243 return FALSE;
246 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
247 if( addr == (char *) -1 )
249 return FALSE;
253 * Now build up the line number mapping table.
255 ol->nlines = 1;
256 pnt = addr;
257 while(pnt < addr + ol->size )
259 if( *pnt++ == '\n' )
261 ol->nlines++;
265 ol->nlines++;
266 ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
268 nlines = 0;
269 pnt = addr;
270 ol->linelist[nlines++] = 0;
271 while(pnt < addr + ol->size )
273 if( *pnt++ == '\n' )
275 ol->linelist[nlines++] = pnt - addr;
278 ol->linelist[nlines++] = pnt - addr;
281 else
284 * We know what the file is, we just need to reopen it and remap it.
286 fd = open(ol->real_path, O_RDONLY);
287 if( fd == -1 )
289 return FALSE;
292 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
293 if( addr == (char *) -1 )
295 return FALSE;
300 * All we need to do is to display the source lines here.
302 rtn = FALSE;
303 for(i=start - 1; i <= end - 1; i++)
305 if( i < 0 || i >= ol->nlines - 1)
307 continue;
310 rtn = TRUE;
311 memset(&buffer, 0, sizeof(buffer));
312 if( ol->linelist[i+1] != ol->linelist[i] )
314 memcpy(&buffer, addr + ol->linelist[i],
315 (ol->linelist[i+1] - ol->linelist[i]) - 1);
317 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
320 munmap(addr, ol->size);
321 close(fd);
323 return rtn;
327 void
328 DEBUG_List(struct list_id * source1, struct list_id * source2,
329 int delta)
331 int end;
332 int rtn;
333 int start;
334 char * sourcefile;
337 * We need to see what source file we need. Hopefully we only have
338 * one specified, otherwise we might as well punt.
340 if( source1 != NULL
341 && source2 != NULL
342 && source1->sourcefile != NULL
343 && source2->sourcefile != NULL
344 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
346 fprintf(stderr, "Ambiguous source file specification.\n");
347 return;
350 sourcefile = NULL;
351 if( source1 != NULL && source1->sourcefile != NULL )
353 sourcefile = source1->sourcefile;
356 if( sourcefile == NULL
357 && source2 != NULL
358 && source2->sourcefile != NULL )
360 sourcefile = source2->sourcefile;
363 if( sourcefile == NULL )
365 sourcefile = (char *) &DEBUG_current_sourcefile;
368 if( sourcefile == NULL )
370 fprintf(stderr, "No source file specified.\n");
371 return;
375 * Now figure out the line number range to be listed.
377 start = -1;
378 end = -1;
380 if( source1 != NULL )
382 start = source1->line;
385 if( source2 != NULL )
387 end = source2->line;
390 if( start == -1 && end == -1 )
392 if( delta < 0 )
394 end = DEBUG_start_sourceline;
395 start = end + delta;
397 else
399 start = DEBUG_end_sourceline;
400 end = start + delta;
403 else if( start == -1 )
405 start = end + delta;
407 else if (end == -1)
409 end = start + delta;
413 * Now call this function to do the dirty work.
415 rtn = DEBUG_DisplaySource(sourcefile, start, end);
417 if( sourcefile != (char *) &DEBUG_current_sourcefile )
419 strcpy(DEBUG_current_sourcefile, sourcefile);
421 DEBUG_start_sourceline = start;
422 DEBUG_end_sourceline = end;
427 #if 0
428 main()
430 int i, j;
431 DEBUG_AddPath("../../de");
432 while(1==1)
434 fscanf(stdin,"%d %d", &i, &j);
435 DEBUG_DisplaySource("dumpexe.c", i, j);
437 return 0;
439 #endif