Bayonne 3 - API
 All Classes Namespaces Files Functions Variables Typedefs Macros
baylint.cpp
Go to the documentation of this file.
1 // Copyright (C) 2008-2009 David Sugar, Tycho Softworks.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include <ucommon/ucommon.h>
17 #include <bayonne.h>
18 #include <config.h>
19 
20 using namespace BAYONNE_NAMESPACE;
21 using namespace UCOMMON_NAMESPACE;
22 
23 class lint : public Script::interp
24 {
25 public:
26 };
27 
28 static Script::keyword_t keywords[] = {
29  {NULL}};
30 
31 static const char *defs[128];
32 static const char *clist = " \t,;:";
33 unsigned defcount = 0;
34 
35 static shell::flagopt helpflag('h',"--help", _TEXT("display this list"));
36 static shell::flagopt althelp('?', NULL, NULL);
37 static shell::stringopt driver('d', "--driver", _TEXT("driver lint definitions to use"), "name", NULL);
38 static shell::stringopt modules('m', "--modules", _TEXT("names of optional modules we added"), "list", NULL);
39 static shell::flagopt ver('v', "--version", _TEXT("version of application"));
40 
41 static void version(void)
42 {
43  printf("Bayonne Lint " VERSION "\n"
44  "Copyright (C) 2009 David Sugar, Tycho Softworks\n"
45  "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
46  "This is free software: you are free to change and redistribute it.\n"
47  "There is NO WARRANTY, to the extent permitted by law.\n");
48  exit(0);
49 }
50 
51 static void usage(void)
52 {
53  printf("%s\n", _TEXT("usage: baylint [options] scriptfile\n"
54  "Options:"));
55  shell::help();
56  printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org"));
57  exit(0);
58 }
59 
60 PROGRAM_MAIN(argc, argv)
61 {
62  linked_pointer<Script::error> ep;
63  const char *modname = NULL;
64  const char *fn, *dn;
65  Script *lib = NULL;
66  unsigned def = 0;
67  char buffer[256];
68  char *tokens = NULL;
69  const char *ext;
70 
71  shell::bind("baylint");
72  shell args(argc, argv);
73 
74  Env::tool(&args);
75 
76  const char *lintfiles = Env::env("definitions");
77 
78  if(is(helpflag) || is(althelp))
79  usage();
80 
81  if(is(ver))
82  version();
83 
84  argv = args.argv();
85  fn = *(argv++);
86 
87  if(!fn) {
88  fprintf(stderr, "*** baylint: no filename specified\n");
89  exit(-1);
90  }
91 
92  ext = strrchr(fn, '.');
93 
94  if(!fsys::isfile(fn)) {
95  fprintf(stderr, "*** baylint: %s: not found\n", fn);
96  exit(-1);
97  }
98 
99  if((is(driver) || is(modules)) && !fsys::isdir(lintfiles)) {
100  fprintf(stderr, "*** baylint: %s: invalid data directory\n", lintfiles);
101  exit(-1);
102  }
103 
104  Script::init();
105  Script::assign(keywords);
106 
107  if(is(driver))
108  defs[defcount++] = *driver;
109 
110  if(modules)
111  modname = String::token((char *)*modules, &tokens, clist);
112 
113  while(modname) {
114  defs[defcount++] = modname;
115  modname = String::token(NULL, &tokens, clist);
116  }
117 
118  while(def < defcount) {
119  dn = defs[def++];
120  if(!strchr(dn, '/') && !strchr(dn, '.')) {
121  snprintf(buffer, sizeof(buffer), "%s/%s.lint", lintfiles, dn);
122  dn = buffer;
123  }
124  if(!fsys::isfile(dn))
125  continue;
126  lib = Script::merge(dn, lib);
127  ep = lib->getListing();
128  while(is(ep)) {
129  fprintf(stderr, "%s:%d: warning: %s\n", lib->getFilename(), ep->errline, ep->errmsg);
130  ep.next();
131  }
132  }
133 
134  Script *img = Script::compile(fn, lib);
135 
136  if(!img) {
137  fprintf(stderr, "*** baylint: %s: failed to compile\n", fn);
138  exit(-1);
139  }
140 
141  unsigned errors = img->getErrors();
142 
143  if(!String::equal(ext, ".ics") && !String::equal(ext, ".ocs") && !String::equal(ext, ".dcs")) {
144  fprintf(stderr, "*** %s: not a valid script file name\n", fn);
145  exit(1);
146  }
147 
148  if(errors) {
149  fprintf(stderr, "*** %d total errors in %s\n", errors, fn);
150  ep = img->getListing();
151  while(is(ep)) {
152  fprintf(stderr, "%s:%d: error: %s\n", img->getFilename(), ep->errline, ep->errmsg);
153  ep.next();
154  }
155  exit(2);
156  }
157  PROGRAM_EXIT(0);
158 }
159 
Definition: baylint.cpp:23
#define BAYONNE_NAMESPACE
Definition: bayonne.h:25
PROGRAM_MAIN(argc, argv)
Definition: baylint.cpp:60
unsigned defcount
Definition: baylint.cpp:33
GNU Bayonne library namespace.