summaryrefslogtreecommitdiff
path: root/src/bin/e_static_grab.c
blob: 8fb7a3e35901de855fe43a5ca6c80e1746b4a586 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# ifdef HAVE_CONFIG_H
#  include "config.h"
# endif

#include <Eet.h>

typedef struct _E_Static_Grab        E_Static_Grab;
typedef struct _E_Static_Grab_Module E_Static_Grab_Module;

struct _E_Static_Grab
{
   struct
   {
      int         core_count;
      int         thread_count;
      int         bogo;
      int         model;
      int         family;
      int         stepping;
      int         cache_size;
      int         addr_space;

      const char *vendor;
      const char *model_name;

      Eina_Bool   fpu;
   } cpu;

   struct
   {
      const char *name;
      const char *release;
      const char *os;
      const char *kernel_release;
   } distribution;

   struct
   {
      struct
      {
         const char *vendor;
         const char *renderer;
         const char *version;
      } gpu;

      const char *name;
      const char *release_date;
      const char *build_date;

      Eina_List  *modules;
   } x;
};

struct _E_Static_Grab_Module
{
   const char *name;
   const char *vendor;
   const char *compiled_for;
   const char *version;
   const char *class;
   const char *ABI_class;
   const char *ABI_version;
};

/* For /proc/cpuinfo */
#define CPU_CORES     "cpu cores"
#define CACHE_SIZE    "cache size"
#define CPU_FAMILY    "cpu family"
#define STEPPING      "stepping"
#define SIBLINGS      "siblings"
#define BOGOMIPS      "bogomips"
#define MODEL         "model"
#define MODEL_NAME    "model name"
#define VENDOR_ID     "vendor_id"
#define FPU           "fpu"

/* For /var/log/Xorg.0.log */
#define RELEASE_DATE  "Release Date"
#define BUILD_DATE    "Build Date"
#define LOAD_MODULE   "(II) LoadModule"
#define UNLOAD_MODULE "(II) UnloadModule"
#define MODULE_OF     "(II) Module %s"
#define LOADING       "(II) Loading"
#define VENDOR        "vendor=\""
#define COMPILED      "compiled for "
#define MODULE_CLASS  "Module class"
#define ABI_CLASS     "ABI class"
#undef VERSION
#define VERSION       ", version "

static const char *
_e_static_grab_name(const char *line, const char *end, const char *name)
{
   size_t l;

   l = strlen(name);
   if (line + l + 3 > end) return end;
   if (strncmp(name, line, l)) return end;

   return line + l;
}

static Eina_Bool
_e_static_grab_int(const char *line, const char *end, const char *txt, int *v)
{
   static const char *value = "0123456789";
   const char *over;
   int r = 0;

   over = _e_static_grab_name(line, end, txt);
   while (over < end)
     {
        if (strchr(value, *over)) break;
        over++;
     }
   if (over == end) return EINA_FALSE;

   while (over < end)
     {
        const char *offset = strchr(value, *over);

        if (!offset) break;
        r = r * 10 + (offset - value);

        over++;
     }

   *v = r;

   return EINA_TRUE;
}

static Eina_Bool
_e_static_grab_string(const char *line, const char *end, const char *txt, const char **s)
{
   static const char *value = " :\t";
   const char *over;

   over = _e_static_grab_name(line, end, txt);
   while (over < end)
     {
        if (!strchr(value, *over)) break;
        over++;
     }
   if (over == end) return EINA_FALSE;

   if (*over == '"' && *(end - 2) == '"')
     {
        over++;
        end--;
     }

   *s = eina_stringshare_add_length(over, end - over - 1);
   return EINA_TRUE;
}

static const char *
_e_static_grab_discard(const char *current, Eina_File_Line *line, int begin, int end)
{
   while (current < line->end &&
          strchr(" \t", *current))
     current++;

   if (*current != begin) return current;

   while (current < line->end &&
          *current != end)
     current++;

   if (current == line->end) return current;

   for (current++;
        current < line->end && strchr(" \t", *current);
        current++)
     ;

   return current;
}

static void
_e_static_grab_cpu(E_Static_Grab *grab)
{
   char buf[4096];
   FILE *f;
   const char *fpu = NULL;
   int siblings = 0;

   grab->cpu.addr_space = sizeof (void *);

   f = fopen("/proc/cpuinfo", "r");
   if (!f) return;

   while (!feof(f))
     {
        char *end;
        size_t length = 0;

        if (!fgets(buf, sizeof (buf), f)) break;
        length = strlen(buf);
        end = buf + length;

        if (length < 3) continue;

        switch (*buf)
          {
           case 'c':
             if (_e_static_grab_int(buf, end, CPU_CORES, &grab->cpu.core_count)) break;
             if (_e_static_grab_int(buf, end, CACHE_SIZE, &grab->cpu.cache_size)) break;
             _e_static_grab_int(buf, end, CPU_FAMILY, &grab->cpu.family);
             break;

           case 's':
             if (_e_static_grab_int(buf, end, STEPPING, &grab->cpu.stepping)) break;
             _e_static_grab_int(buf, end, SIBLINGS, &siblings);
             break;

           case 'b':
             _e_static_grab_int(buf, end, BOGOMIPS, &grab->cpu.bogo);
             break;

           case 'm':
             if (_e_static_grab_string(buf, end, MODEL_NAME, &grab->cpu.model_name)) break;
             _e_static_grab_int(buf, end, MODEL, &grab->cpu.model);
             break;

           case 'v':
             _e_static_grab_string(buf, end, VENDOR_ID, &grab->cpu.vendor);
             break;

           case 'f':
             _e_static_grab_string(buf, end, FPU, &fpu);
             break;
          }
     }
   fclose(f);

   if (siblings) grab->cpu.thread_count = siblings / grab->cpu.core_count;
   if (fpu)
     {
        if (!strcmp(fpu, "yes")) grab->cpu.fpu = EINA_TRUE;
        eina_stringshare_del(fpu);
     }
}

static void
_e_static_grab_x(E_Static_Grab *grab)
{
   Eina_Bool in_module = EINA_FALSE;
   E_Static_Grab_Module *module;
   Eina_File_Line *line;
   Eina_Iterator *it;
   Eina_File *f;

   f = eina_file_open("/var/log/Xorg.0.log", EINA_FALSE);
   if (!f) return;

   module = calloc(1, sizeof (E_Static_Grab_Module));
   if (!module)
     {
        eina_file_close(f);
        return;
     }

   it = eina_file_map_lines(f);
   EINA_ITERATOR_FOREACH(it, line)
     {
        const char *current;

        current = _e_static_grab_discard(line->start, line, '[', ']');
        if (current >= line->end) continue;

        if (!grab->x.name)
          {
             if (line->end - current - 1 <= 0) continue;
             grab->x.name = eina_stringshare_add_length(current, line->end - current - 1);
             continue;
          }

        if (!in_module)
          {
             switch (*current)
               {
                case 'R':
                  if (_e_static_grab_string(current, line->end, RELEASE_DATE, &grab->x.release_date)) break;
                  break;

                case 'B':
                  if (_e_static_grab_string(current, line->end, BUILD_DATE, &grab->x.build_date)) break;
                  break;

                case '(':
                {
                   E_Static_Grab_Module *md;
                   Eina_List *l;
                   const char *tmp;

                   if (_e_static_grab_string(current, line->end, LOAD_MODULE, &module->name))
                     {
                        EINA_LIST_FOREACH(grab->x.modules, l, md)
                          if (md->name == module->name)
                            {
                               eina_stringshare_del(module->name);
                               module->name = NULL;
                               break;
                            }

                        if (module->name) in_module = EINA_TRUE;
                        break;
                     }
                   else if (_e_static_grab_string(current, line->end, UNLOAD_MODULE, &tmp))
                     {
                        EINA_LIST_FOREACH(grab->x.modules, l, md)
                          if (md->name == tmp)
                            {
                               grab->x.modules = eina_list_remove_list(grab->x.modules, l);

                               eina_stringshare_del(md->name);
                               eina_stringshare_del(md->vendor);
                               eina_stringshare_del(md->compiled_for);
                               eina_stringshare_del(md->version);
                               eina_stringshare_del(md->class);
                               eina_stringshare_del(md->ABI_class);
                               eina_stringshare_del(md->ABI_version);
                               free(md);
                               break;
                            }
                        eina_stringshare_del(tmp);
                     }
                   break;
                }
               }
          }
        else
          {
             char *buffer;
             Eina_Bool found = EINA_FALSE;

             switch (*current)
               {
                case '(':
                {
                   const char *vendor;

                   if (strncmp(current, LOADING, strlen(LOADING)) == 0)
                     {
                        found = EINA_TRUE;
                        break;
                     }

                   buffer = alloca(strlen(MODULE_OF) + strlen(module->name));
                   sprintf(buffer, MODULE_OF, module->name);

                   if (_e_static_grab_string(current, line->end, buffer, &vendor))
                     {
                        if (strncmp(vendor, VENDOR, strlen(VENDOR)) == 0)
                          {
                             module->vendor = eina_stringshare_add_length(vendor + strlen(VENDOR),
                                                                          eina_stringshare_strlen(vendor) - strlen(VENDOR) - 1);
                             found = EINA_TRUE;
                          }
                        eina_stringshare_del(vendor);
                     }
                   break;
                }

                case 'c':
                  if (strncmp(current, COMPILED, strlen(COMPILED)) == 0)
                    {
                       const char *lookup = current + strlen(COMPILED);

                       while (lookup < line->end && *lookup != ',')
                         lookup++;

                       module->compiled_for = eina_stringshare_add_length(current + strlen(COMPILED),
                                                                          lookup - current - strlen(COMPILED));
                       found = EINA_TRUE;
                       break;
                    }
                  break;

                case 'M':
                  if (_e_static_grab_string(current, line->end, MODULE_CLASS, &module->class))
                    {
                       found = EINA_TRUE;
                       break;
                    }
                  break;

                case 'A':
                {
                   const char *tmp;

                   if (_e_static_grab_string(current, line->end, ABI_CLASS, &tmp))
                     {
                        const char *lookup = strchr(tmp, ',');

                        if (lookup)
                          {
                             module->ABI_class = eina_stringshare_add_length(tmp, lookup - tmp);
                             if (strncmp(lookup, VERSION, strlen(VERSION)) == 0)
                               {
                                  module->ABI_version = eina_stringshare_add_length(lookup + strlen(VERSION),
                                                                                    eina_stringshare_strlen(tmp) - (lookup - tmp + strlen(VERSION) - 1));
                               }
                             eina_stringshare_del(tmp);
                          }
                        else
                          {
                             module->ABI_class = tmp;
                          }
                     }
                   break;
                }
               }

             if (!found)
               {
                  in_module = EINA_FALSE;
                  grab->x.modules = eina_list_append(grab->x.modules, module);
                  module = calloc(1, sizeof (E_Static_Grab_Module));
               }
          }
     }

   free(module);

   eina_iterator_free(it);
   eina_file_close(f);
}

int
main(int argc, char **argv EINA_UNUSED)
{
   E_Static_Grab_Module *module;
   Eina_List *l;
   E_Static_Grab grab;

   if (argc != 2) exit(0);

#if 0
   /* FIXME: Latter push information into an eet file when updated */
   if ((!strcmp(argv[1], "-h")) ||
       (!strcmp(argv[1], "-help")) ||
       (!strcmp(argv[1], "--help")))
     {
        printf("This is an internal tool for Enlightenment.\n"
               "do not use it.\n");
        exit(0);
     }
#endif

   eina_init();
   eet_init();

   memset(&grab, 0, sizeof (grab));
   _e_static_grab_cpu(&grab); /* FIXME: please provide patch for more Unix */
   _e_static_grab_x(&grab);

   fprintf(stderr, "%i core with %i thread running in %i bits at %i bogomips with %i cache from vendor %s with model %s\n",
           grab.cpu.core_count, grab.cpu.thread_count, grab.cpu.addr_space * 8, grab.cpu.bogo, grab.cpu.cache_size, grab.cpu.vendor, grab.cpu.model_name);
   fprintf(stderr, "X Server: '%s' released '%s' and build '%s'\n",
           grab.x.name, grab.x.release_date, grab.x.build_date);
   fprintf(stderr, "*** X Modules ***\n");
   EINA_LIST_FOREACH(grab.x.modules, l, module)
     fprintf(stderr, "[%s - '%s' - '%s' - '%s' - '%s','%s' ]\n",
             module->name, module->vendor, module->compiled_for,
             module->class, module->ABI_class, module->ABI_version);
   fprintf(stderr, "*** ***\n");

   /* FIXME: Get information about what Unix (Linux, BSD, ...) it is run on */
   /* FIXME: run a mainloop and start gathering result from df, free, lspci and lsusb */
   /* FIXME: use xrandr to figure out the screen config */

   eet_shutdown();
   eina_shutdown();

   return 0;
}