]> git.tdb.fi Git - gldbg.git/blob - source/glwrap.c
Use a centralized packet framework
[gldbg.git] / source / glwrap.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <dlfcn.h>
13 #include <fcntl.h>
14
15 static const char *get_lib_names(void)
16 {
17         const char *env = getenv("GLWRAP_LIBS");
18         if(env)
19                 return env;
20         return "libGL.so.1";
21 }
22
23 void *glsym(const char *name)
24 {
25         static void **gl_libs = NULL;
26         unsigned i;
27
28         if(!gl_libs)
29         {
30                 char *lib_names = strdup(get_lib_names());
31                 unsigned n_libs = 1;
32                 unsigned j;
33
34                 for(i=0; lib_names[i]; ++i)
35                         if(lib_names[i]==':')
36                                 ++n_libs;
37
38                 gl_libs = (void **)malloc((n_libs+1)*sizeof(void *));
39                 i = 0;
40                 n_libs = 0;
41                 for(j=0;; ++j)
42                 {
43                         if(lib_names[j]==':' || lib_names[j]==0)
44                         {
45                                 int at_end = (lib_names[j]==0);
46                                 lib_names[j] = 0;
47
48                                 gl_libs[n_libs] = dlopen(lib_names+i, RTLD_NOW);
49                                 if(!gl_libs[n_libs])
50                                 {
51                                         fprintf(stderr, "Could not open %s: %s\n", lib_names+i, dlerror());
52                                         abort();
53                                 }
54
55                                 i = j+1;
56                                 ++n_libs;
57
58                                 if(at_end)
59                                         break;
60                         }
61                 }
62
63                 gl_libs[n_libs] = 0;
64                 free(lib_names);
65         }
66
67         for(i=0; gl_libs[i]; ++i)
68         {
69                 void *sym = dlsym(gl_libs[i], name);
70                 if(sym)
71                         return sym;
72         }
73
74         return NULL;
75 }
76
77 int get_out_fd(void)
78 {
79         static int fd = -1;
80
81         if(fd<0)
82         {
83                 const char *var = getenv("GLWRAP_FD");
84                 if(var)
85                         fd = strtol(var, NULL, 0);
86                 else
87                 {
88                         var = getenv("GLWRAP_FILE");
89                         if(var)
90                         {
91                                 fd = open(var, O_WRONLY|O_CREAT|O_TRUNC, 0644);
92                                 if(fd==-1)
93                                 {
94                                         fprintf(stderr, "Couldn't open dumpfile %s for output: %s", var, strerror(errno));
95                                         abort();
96                                 }
97                         }
98                         else
99                                 fd = 2;
100                 }
101         }
102
103         return fd;
104 }