summaryrefslogtreecommitdiffstats
path: root/addon/doxywizard/config_doxyw.l
blob: 960b7cb43ad4675108b67c77363c4a6a07569baf (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
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
/******************************************************************************
 *
 * $Id: config_templ.l,v 1.8 2001/01/01 10:15:16 root Exp $
 *
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 */

%option never-interactive
%option prefix="config_doxywYY"
%{

/*
 *	includes
 */
#include "config.h"
#include "input.h"

#include <QString>
#include <QVariant>
#include <QStack>
#include <QTextCodec>
#include <QByteArray>
#include <QFileInfo>
#include <QStringList>
#include <QRegExp>
#include <QTextStream>

#define YY_NO_UNISTD_H 1

#define MAX_INCLUDE_DEPTH 10


/* -----------------------------------------------------------------
 *
 *	static variables
 */

struct ConfigFileState
{
  int lineNr;
  FILE *file;
  YY_BUFFER_STATE oldState;
  YY_BUFFER_STATE newState;
  QString fileName;
};  

static const QHash<QString,Input*>   *g_options;
static FILE                          *g_file;
static QString                        g_yyFileName;
static QString                        g_includeName;
static QVariant                       g_includePathList;
static QStack<ConfigFileState*>       g_includeStack;  
static int                            g_includeDepth;
static QVariant                      *g_arg;
static Input                         *g_curOption=0;
static QString                        g_elemStr;
static QTextCodec                    *g_codec     = QTextCodec::codecForName("UTF-8");
static QString                        g_codecName = QString::fromLatin1("UTF-8");
static int                            g_lastState;
static QByteArray                     g_tmpString;

/* -----------------------------------------------------------------
 */
#undef	YY_INPUT
#define	YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);

static int yyread(char *buf,int maxSize)
{
    // no file included
    if (g_includeStack.isEmpty()) 
    {
      return fread(buf,1,maxSize,g_file);
    } 
    else 
    {
      return fread(buf,1,maxSize,g_includeStack.top()->file);
    }
}

static QString warning_str = QString::fromLatin1("warning: ");
static QString error_str = QString::fromLatin1("error: ");

void config_err(const char *fmt, ...)
{
  QString msg = error_str;
  msg.append(QString::fromLatin1(fmt));
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, qPrintable(msg), args);
  va_end(args);
}
void config_warn(const char *fmt, ...)
{
  QString msg = warning_str;
  msg.append(QString::fromLatin1(fmt));
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, qPrintable(msg), args);
  va_end(args);
}

static void substEnvVarsInStrList(QStringList &sl);
static void substEnvVarsInString(QString &s);

static void checkEncoding()
{
  Input *option = g_options->value(QString::fromLatin1("DOXYFILE_ENCODING"));
  if (option && option->value().toString()!=g_codecName)
  {
    QTextCodec *newCodec = QTextCodec::codecForName(option->value().toString().toLatin1());
    if (newCodec)
    {
      g_codec = newCodec;
      g_codecName = option->value().toString();
    }
  }
}

static FILE *tryPath(const QString &path,const QString &fileName)
{
  QString absName=!path.isEmpty() ? path+QString::fromLatin1("/")+fileName : fileName;
  QFileInfo fi(absName);
  if (fi.exists() && fi.isFile())
  {
    FILE *f = fopen(absName.toLocal8Bit(),"r");
    if (f==NULL)
      config_err("could not open file %s for reading\n",qPrintable(absName));
    else 
      return f;
  }
  return NULL;
}

static FILE *findFile(const QString &fileName)
{
  if (QFileInfo(fileName).isAbsolute()) // absolute path
  {
    return tryPath(QString(), fileName);
  }

  // relative path, try with include paths in the list
  QStringList sl = g_includePathList.toStringList();
  substEnvVarsInStrList(sl);
  foreach (QString s, sl) 
  {
    FILE *f = tryPath(s,fileName);
    if (f) return f;
  }
  // try cwd if g_includePathList fails
  return tryPath(QString::fromLatin1("."),fileName);
}

static void readIncludeFile(const QString &incName)
{
  if (g_includeDepth==MAX_INCLUDE_DEPTH) 
  {
    config_err("maximum include depth (%d) reached, %s is not included. Aborting...\n",
	MAX_INCLUDE_DEPTH,qPrintable(incName));
    exit(1);
  } 

  QString inc = incName;
  substEnvVarsInString(inc);
  inc = inc.trimmed();
  uint incLen = inc.length();
  if (inc.at(0)==QChar::fromLatin1('"') && 
      inc.at(incLen-1)==QChar::fromLatin1('"')) // strip quotes
  {
    inc=inc.mid(1,incLen-2);
  }

  FILE *f = findFile(inc);
  if (f) // see if the include file can be found
  {
    // For debugging
#if SHOW_INCLUDES
    for (i=0;i<includeStack.count();i++) msg("  ");
    msg("@INCLUDE = %s: parsing...\n",qPrintable(inc));
#endif

    // store the state of the old file 
    ConfigFileState *fs=new ConfigFileState;
    fs->oldState=YY_CURRENT_BUFFER;
    fs->fileName=g_yyFileName;
    fs->file=f;
    // push the state on the stack
    g_includeStack.push(fs);
    // set the scanner to the include file
    yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
    fs->newState=YY_CURRENT_BUFFER;
    g_yyFileName=inc;
    g_includeDepth++;
  } 
  else
  {
    config_err("@INCLUDE = %s: not found!\n",qPrintable(inc));
    exit(1);
  }
}


%}

%option nounput
%option noyywrap
%option yylineno

%x      Start
%x	SkipComment
%x      SkipInvalid
%x      GetString
%x      GetStrList
%x      GetQuotedString
%x      GetEnvVar
%x      Include

%%

<*>\0x0d
<Start,GetString,GetStrList,SkipInvalid>"#"	 { BEGIN(SkipComment); }
<Start>[a-z_A-Z][a-z_A-Z0-9]*[ \t]*"="	 { QString cmd = g_codec->toUnicode(yytext);
                                           cmd=cmd.left(cmd.length()-1).trimmed(); 
					   g_curOption = g_options->value(cmd);
					   if (g_curOption==0) // oops not known
					   {
					     config_warn("ignoring unsupported tag `%s' at line %d, file %s\n",
						 qPrintable(cmd),yylineno,qPrintable(g_yyFileName)); 
					     BEGIN(SkipInvalid);
					   }
					   else // known tag
					   {
					     //option->setEncoding(encoding);
					     g_arg = &g_curOption->value();
					     switch(g_curOption->kind())
					     {
					       case Input::StrList:
						 g_elemStr = QString();
						 *g_arg = QStringList();
					         BEGIN(GetStrList);
					         break;
					       case Input::String:
					         BEGIN(GetString);
					         break;
					       case Input::Int:
					         BEGIN(GetString);
					         break;
					       case Input::Bool:
					         BEGIN(GetString);
						 break;
					       case Input::Obsolete:
					         config_warn("Tag `%s' at line %d of file %s has become obsolete.\n"
						            "To avoid this warning please update your configuration "
							    "file using \"doxygen -u\"\n", qPrintable(cmd),
							    yylineno,qPrintable(g_yyFileName)); 
					         BEGIN(SkipInvalid);
						 break;
					     }
					   }
					}
<Start>[a-z_A-Z][a-z_A-Z0-9]*[ \t]*"+="	{ QString cmd=g_codec->toUnicode(yytext);
                                          cmd=cmd.left(cmd.length()-2).trimmed(); 
					  g_curOption = g_options->value(cmd);
					  if (g_curOption==0) // oops not known
					  {
					    config_warn("ignoring unsupported tag `%s' at line %d, file %s\n",
						yytext,yylineno,qPrintable(g_yyFileName)); 
					    BEGIN(SkipInvalid);
					  }
					  else // known tag
					  {
					    switch(g_curOption->kind())
					    {
					      case Input::StrList:
						g_arg = &g_curOption->value();
						g_elemStr=QString();
					        BEGIN(GetStrList);
					        break;
					      case Input::String:
					      case Input::Int:
					      case Input::Bool:
					        config_warn("operator += not supported for `%s'. Ignoring line at line %d, file %s\n",
						    yytext,yylineno,qPrintable(g_yyFileName)); 
					        BEGIN(SkipInvalid);
						break;
					      case Input::Obsolete:
					         config_warn("Tag `%s' at line %d of file %s has become obsolete.\n"
						            "To avoid this warning please update your configuration "
							    "file using \"doxygen -u\"\n", 
							    qPrintable(cmd),yylineno,qPrintable(g_yyFileName)); 
					         BEGIN(SkipInvalid);
						 break;
					     }
					   }
					}
<Start>"@INCLUDE_PATH"[ \t]*"=" 	{ BEGIN(GetStrList); g_arg=&g_includePathList; *g_arg = QStringList(); g_elemStr=QString(); }
  /* include a config file */
<Start>"@INCLUDE"[ \t]*"="     		{ BEGIN(Include);}
<Include>([^ \"\t\r\n]+)|("\""[^\n\"]+"\"") { 
  					  readIncludeFile(g_codec->toUnicode(yytext)); 
  					  BEGIN(Start);
					}
<<EOF>>					{
                                          //printf("End of include file\n");
					  //printf("Include stack depth=%d\n",g_includeStack.count());
                                          if (g_includeStack.isEmpty())
					  {
					    //printf("Terminating scanner!\n");
					    yyterminate();
					  }
					  else
					  {
					    ConfigFileState *fs = g_includeStack.pop();
					    fclose(fs->file);
					    YY_BUFFER_STATE oldBuf = YY_CURRENT_BUFFER;
					    yy_switch_to_buffer( fs->oldState );
					    yy_delete_buffer( oldBuf );
					    g_yyFileName=fs->fileName;
					    delete fs; 
                                            g_includeDepth--;
					  }
  					}

<Start>[a-z_A-Z0-9]+			{ config_warn("ignoring unknown tag `%s' at line %d, file %s\n",yytext,yylineno,qPrintable(g_yyFileName)); }
<GetString,SkipInvalid>\n	        { BEGIN(Start); }
<GetStrList>\n				{ 
					  if (!g_elemStr.isEmpty())
					  {
					    //printf("elemStr1=`%s'\n",qPrintable(elemStr));
					    *g_arg = QVariant(g_arg->toStringList() << g_elemStr);
					  }
					  BEGIN(Start); 
					}
<GetStrList>[ \t]+			{
  				          if (!g_elemStr.isEmpty())
					  {
					    //printf("elemStr2=`%s'\n",qPrintable(elemStr));
  					    *g_arg = QVariant(g_arg->toStringList() << g_elemStr);
					  }
					  g_elemStr = QString();
  					}
<GetString>[^ \"\t\r\n]+		{ 
                                          *g_arg = QVariant(g_codec->toUnicode(yytext)); 
                                          checkEncoding();
                                        }
<GetString,GetStrList,SkipInvalid>"\""	{ g_lastState=YY_START;
  					  BEGIN(GetQuotedString); 
                                          g_tmpString="";
					}
<GetQuotedString>"\""|"\n" 		{ 
                                          // we add a bogus space to signal that the string was quoted. This space will be stripped later on.
                                          g_tmpString+=" ";
  					  //printf("Quoted String = `%s'\n",qPrintable(tmpString));
  					  if (g_lastState==GetString)
					  {
					    *g_arg = g_codec->toUnicode(g_tmpString);
                                            checkEncoding();
					  }
					  else
					  {
					    g_elemStr+=g_codec->toUnicode(g_tmpString);
					  }
					  if (*yytext=='\n')
					  {
					    config_warn("Missing end quote (\") on line %d, file %s\n",yylineno,
                                                qPrintable(g_yyFileName));
					  }
					  BEGIN(g_lastState);
  					}
<GetQuotedString>"\\\""			{
  					  g_tmpString+='"';
  					}
<GetQuotedString>.			{ g_tmpString+=*yytext; }
<GetStrList>[^ \#\"\t\r\n]+		{
  					  g_elemStr+=g_codec->toUnicode(yytext);
  					}
<SkipComment>\n				{ BEGIN(Start); }
<SkipComment>\\[ \r\t]*\n		{ BEGIN(Start); }
<*>\\[ \r\t]*\n				{ }
<*>\n
<*>.					

%%

/*@ ----------------------------------------------------------------------------
 */

static void substEnvVarsInString(QString &s)
{
  static QRegExp re(QString::fromLatin1("\\$\\([a-z_A-Z0-9]+\\)"));
  if (s.isEmpty()) return;
  int p=0;
  int i,l;
  //printf("substEnvVarInString(%s) start\n",qPrintable(s));
  while ((i=re.indexIn(s,p))!=-1)
  {
    l = re.matchedLength();
    //printf("Found environment var s.mid(%d,%d)=`%s'\n",i+2,l-3,qPrintable(s.mid(i+2,l-3)));
    QString env=g_codec->toUnicode(getenv(s.mid(i+2,l-3).toLatin1()));
    substEnvVarsInString(env); // recursively expand variables if needed.
    s = s.left(i)+env+s.right(s.length()-i-l);
    p=i+env.length(); // next time start at the end of the expanded string
  }
  s=s.trimmed(); // to strip the bogus space that was added when an argument
                         // has quotes
  //printf("substEnvVarInString(%s) end\n",qPrintable(s));
}

static void substEnvVarsInStrList(QStringList &sl)
{
  QStringList out;

  foreach (QString result, sl)
  {
    // an argument with quotes will have an extra space at the end, so wasQuoted will be TRUE.
    bool wasQuoted = (result.indexOf(QChar::fromLatin1(' '))!=-1) || 
                     (result.indexOf(QChar::fromLatin1('\t'))!=-1);
    // here we strip the quote again
    substEnvVarsInString(result);

    //printf("Result %s was quoted=%d\n",qPrintable(result),wasQuoted);

    if (!wasQuoted) /* as a result of the expansion, a single string
		       may have expanded into a list, which we'll
		       add to sl. If the original string already
		       contained multiple elements no further 
		       splitting is done to allow quoted items with spaces! */
    {
      int l=result.length();
      int i,p=0;
      // skip spaces
      // search for a "word"
      for (i=0;i<l;i++)
      {
	QChar c=0;
	// skip until start of new word
	while (i<l && ((c=result.at(i))==QChar::fromLatin1(' ') || c==QChar::fromLatin1('\t'))) i++; 
	p=i; // p marks the start index of the word
	// skip until end of a word
	while (i<l && ((c=result.at(i))!=QChar::fromLatin1(' ') && 
	              c!=QChar::fromLatin1('\t') && 
		      c!=QChar::fromLatin1('"'))) i++;
	if (i<l) // not at the end of the string
	{
	  if (c==QChar::fromLatin1('"')) // word within quotes
	  {
	    p=i+1;
	    for (i++;i<l;i++)
	    {
	      c=result.at(i);
	      if (c==QChar::fromLatin1('"')) // end quote
	      {
                out += result.mid(p,i-p);
		p=i+1;
		break; 
	      }
	      else if (c==QChar::fromLatin1('\\')) // skip escaped stuff
	      {
		i++;
	      }
	    }
	  }
	  else if (c==QChar::fromLatin1(' ') || c==QChar::fromLatin1('\t')) // separator
	  {
            out += result.mid(p,i-p);
	    p=i+1;
	  }
	}
      }
      if (p!=l) // add the leftover as a string
      {
        out += result.right(l-p);
      }
    }
    else // just goto the next element in the list
    {
      out += result;
    }
  }
  sl = out;
}

//--------------------------------------------------------------------------

bool parseConfig(
      const QString &fileName,
      const QHash<QString,Input *> &options
    )
{
  QHashIterator<QString, Input*> i(options);
  g_file = fopen(fileName.toLocal8Bit(),"r");
  if (g_file==NULL) return false;

  // reset all values
  i.toFront();
  while (i.hasNext()) 
  {
    i.next();
    if (i.value())
    {
      i.value()->reset();
    }
  }

  // parse config file
  g_options       = &options;
  g_yyFileName    = fileName;
  g_includeStack.clear();
  g_includeDepth  = 0;
  config_doxywYYrestart( config_doxywYYin );
  BEGIN( Start );
  config_doxywYYlex();

  // update the values in the UI
  i.toFront();
  while (i.hasNext()) 
  {
    i.next();
    if (i.value())
    {
      //printf("Updating: %s\n",qPrintable(i.key()));
      i.value()->update();
    }
    else
    {
      printf("Invalid option: %s\n",qPrintable(i.key()));
    }
  } 
  fclose(g_file);
  return true;
}

void writeStringValue(QTextStream &t,QTextCodec *codec,const QString &s)
{
  QChar c;
  bool needsEscaping=false;
  bool needsHashEscaping=false;
  // convert the string back to it original encoding
  //QByteArray se = codec->fromUnicode(s);
  t.setCodec(codec);
  const QChar *p=s.data();
  if (!s.isEmpty() && !p->isNull())
  {
    if (*p != QChar::fromLatin1('"'))
    {
      while (!(c=*p++).isNull() && !needsEscaping) 
      {
        needsEscaping = (c==QChar::fromLatin1(' ')  || 
	                 c==QChar::fromLatin1('\n') || 
		         c==QChar::fromLatin1('\t') || 
		         c==QChar::fromLatin1('"'));
      }
      p=s.data();
      while (!(c=*p++).isNull() && !needsHashEscaping) 
      {
        needsHashEscaping = (c==QChar::fromLatin1('#'));
      }
    }
    if (needsHashEscaping || needsEscaping)
    { 
      t << "\"";
    }
    if (needsEscaping)
    { 
      p=s.data();
      while (!p->isNull())
      {
	if (*p   ==QChar::fromLatin1(' ') && 
	   *(p+1)==QChar::fromLatin1('\0')) break; // skip inserted space at the end
	if (*p   ==QChar::fromLatin1('"')) t << "\\"; // escape quotes
	t << *p++;
      }
    }
    else
    {
      t << s;
    }
    if (needsHashEscaping || needsEscaping)
    { 
      t << "\"";
    }
  }
}