comdlg32: Do not crash with PageSetupDlg(NULL).
[wine/winequartzdrv.git] / programs / winedbg / source.c
blobc1fb2eb2d82733fcb96c6b638f04699f5877bb72
1 /*
2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
25 #ifndef PATH_MAX
26 #define PATH_MAX MAX_PATH
27 #endif
29 #include "debugger.h"
31 struct open_file_list
33 char* path;
34 char* real_path;
35 struct open_file_list* next;
36 unsigned int size;
37 signed int nlines;
38 unsigned int* linelist;
41 static struct open_file_list* source_ofiles;
43 static char* search_path; /* = NULL; */
44 static char source_current_file[PATH_MAX];
45 static int source_start_line = -1;
46 static int source_end_line = -1;
48 void source_show_path(void)
50 const char* ptr;
51 const char* next;
53 dbg_printf("Search list:\n");
54 for (ptr = search_path; ptr; ptr = next)
56 next = strchr(ptr, ';');
57 if (next)
58 dbg_printf("\t%.*s\n", next++ - ptr, ptr);
59 else
60 dbg_printf("\t%s\n", ptr);
62 dbg_printf("\n");
65 void source_add_path(const char* path)
67 char* new;
68 unsigned size;
70 size = strlen(path) + 1;
71 if (search_path)
73 unsigned pos = HeapSize(GetProcessHeap(), 0, search_path);
74 new = HeapReAlloc(GetProcessHeap(), 0, search_path, pos + size);
75 if (!new || !pos) return;
76 new[pos - 1] = ';';
77 strcpy(&new[pos], path);
79 else
81 new = HeapAlloc(GetProcessHeap(), 0, size);
82 if (!new) return;
83 strcpy(new, path);
85 search_path = new;
88 void source_nuke_path(void)
90 HeapFree(GetProcessHeap(), 0, search_path);
91 search_path = NULL;
94 static void* source_map_file(const char* name, HANDLE* hMap, unsigned* size)
96 HANDLE hFile;
98 hFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
99 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
100 if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
101 if (size != NULL && (*size = GetFileSize(hFile, NULL)) == -1) return (void*)-1;
102 *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
103 CloseHandle(hFile);
104 if (!*hMap) return (void*)-1;
105 return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
108 static void source_unmap_file(void* addr, HANDLE hMap)
110 UnmapViewOfFile(addr);
111 CloseHandle(hMap);
114 static struct open_file_list* source_search_open_file(const char* name)
116 struct open_file_list* ol;
118 for (ol = source_ofiles; ol; ol = ol->next)
120 if (strcmp(ol->path, name) == 0) break;
122 return ol;
125 static int source_display(const char* sourcefile, int start, int end)
127 char* addr;
128 int i;
129 struct open_file_list* ol;
130 int nlines;
131 const char* basename = NULL;
132 char* pnt;
133 int rtn;
134 HANDLE hMap;
135 char tmppath[PATH_MAX];
138 * First see whether we have the file open already. If so, then
139 * use that, otherwise we have to try and open it.
141 ol = source_search_open_file(sourcefile);
143 if (ol == NULL)
146 * Try again, stripping the path from the opened file.
148 basename = strrchr(sourcefile, '\\');
149 if (!basename) basename = strrchr(sourcefile, '/');
150 if (!basename) basename = sourcefile;
151 else basename++;
153 ol = source_search_open_file(basename);
156 if (ol == NULL)
159 * Crapola. We need to try and open the file.
161 strcpy(tmppath, sourcefile);
162 if (GetFileAttributes(sourcefile) == INVALID_FILE_ATTRIBUTES &&
163 (!search_path || !SearchPathA(search_path, sourcefile, NULL, MAX_PATH, tmppath, NULL)))
165 if (dbg_interactiveP)
167 char zbuf[256];
169 * Still couldn't find it. Ask user for path to add.
171 snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
172 input_read_line(zbuf, tmppath, sizeof(tmppath));
174 if (tmppath[strlen(tmppath) - 1] != '/')
176 strcat(tmppath, "/");
179 * Now append the base file name.
181 strcat(tmppath, basename);
184 if (GetFileAttributes(tmppath) == INVALID_FILE_ATTRIBUTES)
187 * OK, I guess the user doesn't really want to see it
188 * after all.
190 ol = HeapAlloc(GetProcessHeap(), 0, sizeof(*ol));
191 ol->path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile) + 1), sourcefile);
192 ol->real_path = NULL;
193 ol->next = source_ofiles;
194 ol->nlines = 0;
195 ol->linelist = NULL;
196 ol->size = 0;
197 source_ofiles = ol;
198 dbg_printf("Unable to open file '%s'\n", tmppath);
199 return FALSE;
203 * Create header for file.
205 ol = HeapAlloc(GetProcessHeap(), 0, sizeof(*ol));
206 ol->path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile) + 1), sourcefile);
207 ol->real_path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(tmppath) + 1), tmppath);
208 ol->next = source_ofiles;
209 ol->nlines = 0;
210 ol->linelist = NULL;
211 ol->size = 0;
212 source_ofiles = ol;
214 addr = source_map_file(tmppath, &hMap, &ol->size);
215 if (addr == (char*)-1) return FALSE;
217 * Now build up the line number mapping table.
219 ol->nlines = 1;
220 pnt = addr;
221 while (pnt < addr + ol->size)
223 if (*pnt++ == '\n') ol->nlines++;
226 ol->nlines++;
227 ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
229 nlines = 0;
230 pnt = addr;
231 ol->linelist[nlines++] = 0;
232 while (pnt < addr + ol->size)
234 if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
236 ol->linelist[nlines++] = pnt - addr;
239 else
241 addr = source_map_file(ol->real_path, &hMap, NULL);
242 if (addr == (char*)-1) return FALSE;
245 * All we need to do is to display the source lines here.
247 rtn = FALSE;
248 for (i = start - 1; i <= end - 1; i++)
250 char buffer[1024];
252 if (i < 0 || i >= ol->nlines - 1) continue;
254 rtn = TRUE;
255 memset(&buffer, 0, sizeof(buffer));
256 if (ol->linelist[i+1] != ol->linelist[i])
258 memcpy(&buffer, addr + ol->linelist[i],
259 (ol->linelist[i+1] - ol->linelist[i]) - 1);
261 dbg_printf("%d\t%s\n", i + 1, buffer);
264 source_unmap_file(addr, hMap);
265 return rtn;
268 void source_list(IMAGEHLP_LINE* src1, IMAGEHLP_LINE* src2, int delta)
270 int end;
271 int start;
272 const char* sourcefile;
275 * We need to see what source file we need. Hopefully we only have
276 * one specified, otherwise we might as well punt.
278 if (src1 && src2 && src1->FileName && src2->FileName &&
279 strcmp(src1->FileName, src2->FileName) != 0)
281 dbg_printf("Ambiguous source file specification.\n");
282 return;
285 sourcefile = NULL;
286 if (src1 && src1->FileName) sourcefile = src1->FileName;
287 if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
288 if (!sourcefile) sourcefile = source_current_file;
291 * Now figure out the line number range to be listed.
293 start = end = -1;
295 if (src1) start = src1->LineNumber;
296 if (src2) end = src2->LineNumber;
297 if (start == -1 && end == -1)
299 if (delta < 0)
301 end = source_start_line;
302 start = end + delta;
304 else
306 start = source_end_line;
307 end = start + delta;
310 else if (start == -1) start = end + delta;
311 else if (end == -1) end = start + delta;
314 * Now call this function to do the dirty work.
316 source_display(sourcefile, start, end);
318 if (sourcefile != source_current_file)
319 strcpy(source_current_file, sourcefile);
320 source_start_line = start;
321 source_end_line = end;
324 void source_list_from_addr(const ADDRESS64* addr, int nlines)
326 IMAGEHLP_LINE il;
327 ADDRESS64 la;
328 DWORD disp;
330 if (!addr)
332 memory_get_current_pc(&la);
333 addr = &la;
336 il.SizeOfStruct = sizeof(il);
337 if (SymGetLineFromAddr(dbg_curr_process->handle,
338 (unsigned long)memory_to_linear_addr(addr),
339 &disp, &il))
340 source_list(&il, NULL, nlines);