-
Notifications
You must be signed in to change notification settings - Fork 85
/
csdbparser.cpp
599 lines (519 loc) · 13 KB
/
csdbparser.cpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* CodeQuery
* Copyright (C) 2013-2017 ruben2020 https://github.com/ruben2020/
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
// Cscope database parser
#include <stdio.h>
#include "csdbparser.h"
#include "csdbheader.h"
#include "small_lib.h"
#define MAX_TEXT_LINE_LEN (800)
#define CSDBP_GENERAL_CHK() if ((m_fp == NULL) || \
(m_trailer_start <= 0) || \
(m_buf == NULL)) \
{return resOTHER_ERR;}
typedef struct
{
int chr;
sym_data::enSymType type;
const char* desc;
} chr2enum;
static const chr2enum symbtypetbl[] =
{
{'`', sym_data::symFuncCall, "FuncCall"},
{'=', sym_data::symDrctAssgnIncrDecr, "DirectAssgnIncrDecr"},
{'c', sym_data::symClass, "Class"},
{'e', sym_data::symEnum, "Enum"},
{'g', sym_data::symGlobal, "OtherGlobal"},
{'l', sym_data::symLocalFuncBlockDef, "LocalFuncBlockDef"},
{'m', sym_data::symGlobalEnumStructUnion, "GlobalEnumStructUnion"},
{'p', sym_data::symFuncParam, "FuncParam"},
{'s', sym_data::symStruct, "Struct"},
{'t', sym_data::symTypeDef, "TypeDef"},
{'u', sym_data::symUnion, "Union"},
{'$', sym_data::symFuncDef, "FuncDef"},
{'#', sym_data::symMacroDef, "MacroDef"},
{'~', sym_data::symIncl, "Include"},
{'@', sym_data::symFile, "File"},
{'X', sym_data::symNone, "NoType"}
};
#define symbtypetbl_SIZE (sizeof(symbtypetbl)/sizeof(chr2enum))
// sym_data ///////////////////////////////////////////////////
sym_data::sym_data()
:valid(false)
,sym_type(symNone)
{
}
sym_data::sym_data(const sym_data& copy)
{
valid = copy.valid;
symbname = copy.symbname;
sym_type = copy.sym_type;
calling_func = copy.calling_func;
calling_macro = copy.calling_macro;
}
sym_data& sym_data::operator= (const sym_data& copy)
{
if (this != ©)
{
valid = copy.valid;
symbname = copy.symbname;
sym_type = copy.sym_type;
calling_func = copy.calling_func;
calling_macro = copy.calling_macro;
}
return *this;
}
void sym_data::clear(void)
{
valid = false;
symbname.clear();
sym_type = symNone;
calling_func.clear();
calling_macro.clear();
}
std::string sym_data::symbname_escaped(void)
{
return add_escape_char(symbname, '"', '"');
}
const char* sym_data::getTypeDesc(void)
{
for (long i=0; i<symbtypetbl_SIZE; i++)
{
if (sym_type == symbtypetbl[i].type)
{
return symbtypetbl[i].desc;
}
}
return "";
}
const char sym_data::getTypeChar(void)
{
for (long i=0; i<symbtypetbl_SIZE; i++)
{
if (sym_type == symbtypetbl[i].type)
{
return static_cast<const char>(symbtypetbl[i].chr);
}
}
return '\0';
}
// symdata_pack ///////////////////////////////////////////////////
symdata_pack::symdata_pack()
:valid(false)
,line_num(0)
{
}
symdata_pack::symdata_pack(const symdata_pack& copy)
{
valid = copy.valid;
filename = copy.filename;
line_num = copy.line_num;
line_text = copy.line_text;
symbols = copy.symbols;
}
symdata_pack& symdata_pack::operator= (const symdata_pack& copy)
{
if (this != ©)
{
valid = copy.valid;
filename = copy.filename;
line_num = copy.line_num;
line_text = copy.line_text;
symbols = copy.symbols;
}
return *this;
}
void symdata_pack::clear(void)
{
valid = false;
filename.clear();
line_num = 0;
line_text.clear();
symbols.clear();
}
std::string symdata_pack::line_num_str(void)
{
char str[30];
sprintf(str, "%ld", line_num);
std::string s(str);
return s;
}
std::string symdata_pack::line_text_escaped(void)
{
return add_escape_char(line_text.substr(0, MAX_TEXT_LINE_LEN) , '"', '"');
}
std::string symdata_pack::line_text_replacetab(void)
{
std::string s;
long i;
long n = line_text.length();
for (i=0; i < n; i++)
{
if (line_text[i] == '\t') s += ' ';
else s += line_text[i];
}
s = s.substr(0, MAX_TEXT_LINE_LEN);
return s;
}
std::string symdata_pack::line_text_blob(void)
{
tempbuf buf(20000);
strcpy(buf.get(), line_text.substr(0, MAX_TEXT_LINE_LEN).c_str());
//packtext(buf.get(), true);
std::string s(buf.get());
return s;
}
// csdbparser ///////////////////////////////////////////////////
csdbparser::csdbparser()
: m_fp(NULL)
,m_buf(NULL)
,m_state(stIDLE)
,m_trailer_start(0)
,m_bufsize(0)
,m_debug(false)
{
}
csdbparser::~csdbparser()
{
close_file();
}
const char* csdbparser::getBasePath(void)
{
return m_base_path.c_str();
}
void csdbparser::setDebug(bool val)
{
m_debug = val;
}
void csdbparser::create_buf(long int size)
{
if (size >= CSDBP_MINIM_BUFSIZE)
{
destroy_buf();
m_buf = new char[size];
*m_buf = 0;
m_bufsize = size;
}
}
void csdbparser::destroy_buf(void)
{
if (m_buf != NULL)
{
delete[] m_buf;
m_buf = NULL;
m_bufsize = 0;
}
}
csdbparser::enResult csdbparser::open_file(const char *fn)
{
enResult res = file_sanity_check(fn);
if (res != resOK) {return res;}
close_file();
m_fp = fopen(fn, "r");
res = parse_headers();
if (res != resOK) {close_file();}
return res;
}
void csdbparser::close_file(void)
{
if (m_fp != NULL)
{
fclose(m_fp);
m_fp = NULL;
}
m_trailer_start = 0;
destroy_buf();
m_base_path.clear();
m_calling_func.clear();
m_calling_macro.clear();
m_current_srcfile.clear();
}
csdbparser::enResult csdbparser::file_sanity_check(const char *fn)
{
smartFILE fp;
tempbuf buf(20000);
long i;
std::string s;
bool chkok;
// Does the file exist?
if (check_fileExists(fn) == false) {return resFILE_NOT_FOUND;}
// Try to open the file for reading
fp = fopen(fn, "r");
if (fp == NULL) {return resFILE_ACCESS_ERR;}
// Read out the first line i.e. the header
if (fgets(buf.get(), 20000, fp.get()) == NULL) {return resFILE_ACCESS_ERR;}
chomp(buf.get());
s = static_cast<const char*>(buf.get());
csdbheader hdr;
hdr.set_header(s);
if (hdr.parse() == false)
{return resUNRECOG_FORMAT;}
// Compare the cscope version used to build the database
if (hdr.get_version() != CSDBP_SUPPORTED_VER_NUM)
{return resINCORRECT_VER;}
// Compare the parameters used to build the database with the supported one
// We must have "c", we don't mind "q" and we cannot have any other
chkok = false;
tVecStr vs = hdr.get_param_list();
for(i=0; i< vs.size(); i++)
{
if (vs[i].compare("c") == 0) chkok = true;
}
if (chkok == false) {return resUNSUPPORTED_PARAM;}
for(i=0; i< vs.size(); i++)
{
if ((vs[i].compare("c") != 0) &&
(vs[i].compare("q") != 0)) chkok = false;
}
if (chkok == false) {return resUNSUPPORTED_PARAM;}
// Trailer offset should be a positive number, normally > 20
if (hdr.get_trailer_start() < 20) {return resUNRECOG_FORMAT;}
// Header looks OK so far!
return resOK;
}
csdbparser::enResult csdbparser::parse_headers(void)
{
long slen;
if (m_fp == NULL) {return resFILE_NOT_OPEN;}
create_buf();
m_base_path.clear();
m_calling_func.clear();
m_calling_macro.clear();
m_current_srcfile.clear();
csdbheader hdr;
// Read out the first line i.e. the header
if (fgets(m_buf, CSDBP_MINIM_BUFSIZE, m_fp) == NULL) {return resFILE_ACCESS_ERR;}
slen = strlen(chomp(m_buf));
std::string s(static_cast<const char*>(m_buf));
hdr.set_header(s);
if (hdr.parse() == false)
{return resUNRECOG_FORMAT;}
m_trailer_start = hdr.get_trailer_start();
m_base_path = hdr.get_base_path();
return resOK;
}
csdbparser::enResult csdbparser::setup_srcfil_read(void)
{
CSDBP_GENERAL_CHK();
long int num;
if (fseek(m_fp, m_trailer_start, SEEK_SET) != 0) {return resFILE_ACCESS_ERR;}
fscanf(m_fp, "%ld\n", &num); // number of source directories
while (num-- > 0) {fgets(m_buf, CSDBP_MINIM_BUFSIZE, m_fp);}
fscanf(m_fp, "%ld\n", &num); // number of include directories
while (num-- > 0) {fgets(m_buf, CSDBP_MINIM_BUFSIZE, m_fp);}
fscanf(m_fp, "%ld\n", &num); // number of files
fscanf(m_fp, "%ld\n", &num); // string size required
create_buf(num);
return resOK;
}
csdbparser::enResult csdbparser::get_next_srcfil(std::string* srcfil)
{
CSDBP_GENERAL_CHK();
if (ftell(m_fp) < m_trailer_start) setup_srcfil_read();
if (fgets(m_buf, m_bufsize, m_fp) != NULL)
*srcfil = chomp(m_buf);
else srcfil->clear();
return resOK;
}
csdbparser::enResult csdbparser::setup_symbol_read(void)
{
CSDBP_GENERAL_CHK();
rewind(m_fp);
if (fgets(m_buf, m_bufsize, m_fp) == NULL) {return resFILE_ACCESS_ERR;}
m_calling_func.clear();
m_calling_macro.clear();
m_current_srcfile.clear();
//m_state = stSYMB_SETUP_DONE;
return resOK;
}
csdbparser::enResult csdbparser::get_next_symbol(symdata_pack* pack)
{
CSDBP_GENERAL_CHK();
enResult res;
bool endOfSymbData;
bool foundSomething;
int ch;
//if (m_state != stSYMB_SETUP_DONE) return resUNKNOWN_ERR;
pack->clear();
if (m_debug) printf("=====> get_next_symbol\n");
do
{
res = single_line_symbol(endOfSymbData, foundSomething);
if (res != resOK) return res;
if (endOfSymbData)
{
pack->valid = false;
if (m_debug) printf("End of symbols data!\n");
return resOK;
}
} while (foundSomething);
pack->valid = true;
pack->filename = m_current_srcfile;
if (m_debug) printf("=====> Back from get_next_symbol\n");
ch = fgetc(m_fp);
if (ch == 0x0A)
{
pack->line_num = -1; // empty line
return resOK; //EOL
}
else ungetc(ch, m_fp);
if (fscanf(m_fp, "%ld", &(pack->line_num)) != 1)
{
return resUNKNOWN_ERR;
}
ch = fgetc(m_fp); // the space after the line number
if (fgets(m_buf, m_bufsize, m_fp) == NULL) {return resFILE_ACCESS_ERR;}
pack->line_text = chomp(m_buf);
if (m_debug) {printf("fn = %s, lineno=%ld, firstline=%s\n",
pack->filename.c_str(), pack->line_num, pack->line_text.c_str());}
unsigned int loopcheck = 0; // prevent infinite loop
sym_data sd;
while(loopcheck++ < 65500)
{
// symbol line
ch = fgetc(m_fp);
if ((ch == 0x0A)&&(loopcheck > 1))
{
break; //EOL
}
else if ((ch >= 0x30)&&(ch <= 0x39)&&(loopcheck > 1))
{
ungetc(ch, m_fp);
break; // symbol shouldn't start with line numbers
}
else
{
ungetc(ch, m_fp);
res = symbolread(&sd, pack);
if (res != resOK) return res;
pack->line_text += sd.symbname;
if (sd.valid) {pack->symbols.push_back(sd);}
}
// no-symbol line
ch = fgetc(m_fp);
if ((ch == 0x0A)&&(loopcheck > 1)) {;}
else
{
ungetc(ch, m_fp);
if (fgets(m_buf, m_bufsize, m_fp) == NULL)
{return resFILE_ACCESS_ERR;}
pack->line_text += chomp(m_buf);
}
}
return resOK;
}
csdbparser::enResult csdbparser::single_line_symbol(bool& endOfSymbData, bool& foundSomething)
{
if (m_debug) printf("=====> single_line_symbol\n");
endOfSymbData = false;
foundSomething = false;
int ch;
ch = fgetc(m_fp);
if (m_debug) printf("check1! %c\n", (char)ch);
if (ch == 9) // TAB
{
ch = fgetc(m_fp);
foundSomething = true;
if (m_debug) printf("check2! %c\n", (char)ch);
switch(ch)
{
case '@': // start of a new file
if (fgets(m_buf, m_bufsize, m_fp) == NULL)
return resFILE_ACCESS_ERR;
m_current_srcfile = chomp(m_buf);
if (m_debug) printf("New file=%s\n", m_buf);
endOfSymbData = m_current_srcfile.empty();
break;
case ')': // end of a definition
if (m_calling_macro.size() > 0)
m_calling_macro.clear();
else if (m_debug) printf("no macro to clear!\n");
if (fgets(m_buf, m_bufsize, m_fp) == NULL)
{return resFILE_ACCESS_ERR;}
break;
default:
return resUNKNOWN_ERR;
};
fgets(m_buf, m_bufsize, m_fp); // empty line
}
else ungetc(ch, m_fp);
return resOK;
}
csdbparser::enResult csdbparser::symbolread(sym_data *data, symdata_pack* pack)
{
if (m_debug) printf("=====> symbolread\n");
int ch, ch2;
data->clear();
data->valid = true;
data->sym_type = sym_data::symNone;
ch = fgetc(m_fp);
if (m_debug) printf("check3! %c\n", (char)ch);
if (ch == 9) // TAB
{
ch = fgetc(m_fp);
if (m_debug) printf("check4! %c\n", (char)ch);
switch(ch)
{
case '}': // end of func
if (m_calling_func.size() > 0)
m_calling_func.clear();
else if (m_debug) printf("no func to clear!\n");
data->valid = false;
if (m_debug) printf("End of func found\n");
break;
case ')': // end of macro
if (m_calling_macro.size() > 0)
m_calling_macro.clear();
else if (m_debug) printf("no macro to clear!\n");
data->valid = false;
if (m_debug) printf("End of macro found\n");
break;
case '~': // include
ch2 = fgetc(m_fp);
if ((ch2 == '"')||(ch2 == '<'))
{
pack->line_text += (char) ch2;
}
else
{
ungetc(ch2, m_fp);
}
data->sym_type = sym_data::symIncl;
if (m_debug) printf("Incl found\n");
break;
default:
for (int i=0; i<symbtypetbl_SIZE; i++)
{
if (symbtypetbl[i].chr == ch)
{
data->sym_type =
symbtypetbl[i].type;
break;
}
}
if (data->sym_type == sym_data::symNone)
return resUNKNOWN_ERR;
break;
};
}
else ungetc(ch, m_fp);
if (fgets(m_buf, m_bufsize, m_fp) == NULL) {return resFILE_ACCESS_ERR;}
data->symbname = chomp(m_buf);
if (data->valid) data->valid = (strlen(data->symbname.c_str()) > 0);
if (m_debug) printf("sym name=%s, type = %s, valid=%d, ch=%c\n",
data->symbname.c_str(), data->getTypeDesc(), data->valid, ch);
if (ch == '$') {m_calling_func.assign(data->symbname);}
else if (ch == '#') {m_calling_macro.assign(data->symbname);}
else if (ch == '`')
{
data->calling_func = m_calling_func;
data->calling_macro = m_calling_macro;
}
return resOK;
}