1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <ctype.h> |
---|
5 | #include "fegetopt.h" |
---|
6 | #include "utils.h" |
---|
7 | #ifdef __MWERKS__ |
---|
8 | #define __GNU_LIBRARY__ |
---|
9 | #include "fegetopt.h" |
---|
10 | #endif |
---|
11 | |
---|
12 | extern FILE *yylpin; |
---|
13 | extern char *optarg; |
---|
14 | extern int optind, opterr, optopt; |
---|
15 | extern int lpverbose, check; |
---|
16 | extern int texinfo_out; |
---|
17 | extern int category_out; |
---|
18 | extern int found_version, found_info, found_oldhelp, found_proc_in_proc; |
---|
19 | int warning_info = 0, warning_version = 0; |
---|
20 | |
---|
21 | static void usage(char *progname) |
---|
22 | { |
---|
23 | printf("libparse: a syntax-checker for Singular Libraries.\n"); |
---|
24 | printf("USAGE: %s [options] singular-library\n", progname); |
---|
25 | printf("Options:\n"); |
---|
26 | printf(" -f <singular library> : performs syntax-checks\n"); |
---|
27 | printf(" -d [digit] : digit=1,..,4 increases the verbosity of the checks\n"); |
---|
28 | printf(" -s : turns on reporting about violations of unenforced syntax rules\n"); |
---|
29 | printf(" -i : perl output of examples and help of procs\n"); |
---|
30 | printf(" -c : print category of lib to stdout and exit\n"); |
---|
31 | printf(" -h : print this message\n"); |
---|
32 | exit(1); |
---|
33 | } |
---|
34 | |
---|
35 | static char* lib_file = NULL; |
---|
36 | extern "C" { int fe_getopt (); } |
---|
37 | |
---|
38 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
39 | void main_init(int argc, char *argv[]) |
---|
40 | { |
---|
41 | char c; |
---|
42 | |
---|
43 | while((c=fe_getopt(argc, argv, "ihdc:sf:"))>=0) { |
---|
44 | switch(c) { |
---|
45 | case 'd': |
---|
46 | lpverbose = 1; |
---|
47 | if(isdigit(argv[fe_optind-1][0])) sscanf(optarg, "%d", &lpverbose); |
---|
48 | else fe_optind--; |
---|
49 | break; |
---|
50 | case 'f': lib_file = argv[fe_optind-1]; |
---|
51 | break; |
---|
52 | case 's': |
---|
53 | check++; |
---|
54 | break; |
---|
55 | case 'i': |
---|
56 | texinfo_out = 1; |
---|
57 | break; |
---|
58 | case 'c': |
---|
59 | category_out = 1; |
---|
60 | break; |
---|
61 | |
---|
62 | case 'h' : |
---|
63 | usage(argv[0]); |
---|
64 | break; |
---|
65 | case -1 : printf("no such option:%s\n", argv[fe_optind]); |
---|
66 | usage(argv[0]); |
---|
67 | break; |
---|
68 | default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[fe_optind]); |
---|
69 | usage(argv[0]); |
---|
70 | } |
---|
71 | } |
---|
72 | if (texinfo_out || category_out) lpverbose = 0; |
---|
73 | |
---|
74 | if(lib_file!=NULL) { |
---|
75 | yylpin = fopen( lib_file, "rb" ); |
---|
76 | if (! (texinfo_out || category_out)) |
---|
77 | printf("Checking library '%s'\n", lib_file); |
---|
78 | else if (! category_out) |
---|
79 | printf("$library = \"%s\";\n", lib_file); |
---|
80 | } else { |
---|
81 | while(argc>fe_optind && yylpin==NULL) { |
---|
82 | yylpin = fopen( argv[fe_optind], "rb" ); |
---|
83 | if(yylpin!=NULL) |
---|
84 | { |
---|
85 | lib_file = argv[fe_optind]; |
---|
86 | if (! (texinfo_out || category_out) ) |
---|
87 | printf("Checking library '%s'\n", argv[fe_optind]); |
---|
88 | else if (! category_out) |
---|
89 | printf("$library = \"%s\";\n", lib_file); |
---|
90 | } |
---|
91 | else fe_optind++; |
---|
92 | } |
---|
93 | } |
---|
94 | if(yylpin == NULL) { |
---|
95 | printf("No library found to parse.\n"); |
---|
96 | usage(argv[0]); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
101 | void main_result(char *libname) |
---|
102 | { |
---|
103 | if(!found_info) printf("*** No info-string found!\n"); |
---|
104 | if(!found_version) printf("*** No version-string found!\n"); |
---|
105 | if(found_oldhelp) printf("*** Library has stil OLD library-format.\n"); |
---|
106 | if(found_info && warning_info) |
---|
107 | printf("*** INFO-string should come before every procedure definition.\n"); |
---|
108 | if(found_version && warning_version) |
---|
109 | printf("*** VERSION-string should come before every procedure definition.\n"); |
---|
110 | } |
---|
111 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
112 | |
---|
113 | procinfo *iiInitSingularProcinfo(procinfov pi, char *libname, |
---|
114 | char *procname, int line, long pos, |
---|
115 | BOOLEAN pstatic = FALSE) |
---|
116 | { |
---|
117 | pi->libname = (char *)malloc(strlen(libname)+1); |
---|
118 | memcpy(pi->libname, libname, strlen(libname)); |
---|
119 | *(pi->libname+strlen(libname)) = '\0'; |
---|
120 | |
---|
121 | pi->procname = (char *)malloc(strlen(procname)+1); |
---|
122 | strcpy(pi->procname, procname/*, strlen(procname)*/); |
---|
123 | pi->language = LANG_SINGULAR; |
---|
124 | pi->ref = 1; |
---|
125 | pi->is_static = pstatic; |
---|
126 | pi->data.s.proc_start = pos; |
---|
127 | pi->data.s.def_end = 0L; |
---|
128 | pi->data.s.help_start = 0L; |
---|
129 | pi->data.s.body_start = 0L; |
---|
130 | pi->data.s.body_end = 0L; |
---|
131 | pi->data.s.example_start = 0L; |
---|
132 | pi->data.s.proc_lineno = line; |
---|
133 | pi->data.s.body_lineno = 0; |
---|
134 | pi->data.s.example_lineno = 0; |
---|
135 | pi->data.s.body = NULL; |
---|
136 | pi->data.s.help_chksum = 0; |
---|
137 | return(pi); |
---|
138 | } |
---|
139 | |
---|
140 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
141 | void pi_clear(procinfov pi) |
---|
142 | { |
---|
143 | free(pi->libname); |
---|
144 | free(pi->procname); |
---|
145 | free(pi); |
---|
146 | } |
---|
147 | |
---|
148 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
149 | |
---|
150 | #ifndef SEEK_SET |
---|
151 | #define SEEK_SET 0 |
---|
152 | #endif |
---|
153 | |
---|
154 | static void PrintOut(FILE *fd, int pos_start, int pos_end) |
---|
155 | { |
---|
156 | if (pos_start <= 0 || pos_end - pos_start <= 4) return; |
---|
157 | char c = 0; |
---|
158 | |
---|
159 | fseek(fd, pos_start, SEEK_SET); |
---|
160 | while (pos_start++ <= pos_end) |
---|
161 | { |
---|
162 | if (c == '\\') |
---|
163 | { |
---|
164 | c = fgetc(fd); |
---|
165 | if (c != '"') putchar('\\'); |
---|
166 | } |
---|
167 | else |
---|
168 | c = fgetc(fd); |
---|
169 | if (c == '@' || c == '$') putchar('\\'); |
---|
170 | if (c != '\r') putchar(c); |
---|
171 | } |
---|
172 | if (c == '\\') putchar('\\'); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | void printpi(procinfov pi) |
---|
177 | { |
---|
178 | char *buf, name[256]; |
---|
179 | int len1, len2; |
---|
180 | /* pi->libname is badly broken -- use file, instead */ |
---|
181 | FILE *fp = fopen( lib_file, "rb"); |
---|
182 | |
---|
183 | if (fp == NULL) |
---|
184 | { |
---|
185 | printf("Can not open %s\n", lib_file); |
---|
186 | return; |
---|
187 | } |
---|
188 | |
---|
189 | if(!found_info && !warning_info) warning_info++; |
---|
190 | if(!found_version && !warning_version) warning_version++; |
---|
191 | if(pi->data.s.body_end==0) |
---|
192 | pi->data.s.body_end = pi->data.s.proc_end; |
---|
193 | |
---|
194 | if (texinfo_out) |
---|
195 | { |
---|
196 | if ((! pi->is_static) && |
---|
197 | (pi->data.s.body_start - pi->data.s.def_end > 10) && |
---|
198 | (! found_proc_in_proc)) |
---|
199 | { |
---|
200 | printf("push(@procs, \"%s\");\n", pi->procname); |
---|
201 | printf("$help{\"%s\"} = <<EOT;\n", pi->procname); |
---|
202 | PrintOut(fp, pi->data.s.help_start, pi->data.s.body_start-3); |
---|
203 | printf("\nEOT\n"); |
---|
204 | if ((pi->data.s.example_start > 0) && |
---|
205 | (pi->data.s.proc_end - pi->data.s.example_start > 10)) |
---|
206 | { |
---|
207 | printf("$example{\"%s\"} = <<EOT;\n", pi->procname); |
---|
208 | PrintOut(fp, pi->data.s.example_start, pi->data.s.proc_end); |
---|
209 | printf("\nEOT\n"); |
---|
210 | } |
---|
211 | printf("$chksum{\"%s\"} = %d;\n", pi->procname, pi->data.s.help_chksum); |
---|
212 | } |
---|
213 | } |
---|
214 | else if (! category_out) |
---|
215 | { |
---|
216 | if(lpverbose) printf("// "); |
---|
217 | printf( "%c %-15s %20s ", pi->is_static ? 'l' : 'g', pi->libname, |
---|
218 | pi->procname); |
---|
219 | printf("line %4d,%5ld-%-5ld %4d,%5ld-%-5ld %4d,%5ld-%-5ld\n", |
---|
220 | pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end, |
---|
221 | pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end, |
---|
222 | pi->data.s.example_lineno, pi->data.s.example_start, |
---|
223 | pi->data.s.proc_end); |
---|
224 | if(check) { |
---|
225 | if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4) |
---|
226 | printf("*** Procedure '%s' is global and has no help-section.\n", |
---|
227 | pi->procname); |
---|
228 | if(!pi->is_static && !pi->data.s.example_start) |
---|
229 | printf("*** Procedure '%s' is global and has no example-section.\n",\ |
---|
230 | pi->procname); |
---|
231 | if(found_proc_in_proc) |
---|
232 | printf("*** found proc within procedure '%s'.\n", pi->procname); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | if (fp != NULL) fclose(fp); |
---|
237 | |
---|
238 | #if 0 |
---|
239 | if( fp != NULL) { // loading body |
---|
240 | len1 = pi->data.s.def_end - pi->data.s.proc_start; |
---|
241 | if(pi->data.s.body_end==0) |
---|
242 | len2 = pi->data.s.proc_end - pi->data.s.body_start; |
---|
243 | else len2 = pi->data.s.body_end - pi->data.s.body_start; |
---|
244 | buf = (char *)malloc(len1 + len2 + 1); |
---|
245 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
246 | fread( buf, len1, 1, fp); |
---|
247 | *(buf+len1) = '\n'; |
---|
248 | fseek(fp, pi->data.s.body_start, SEEK_SET); |
---|
249 | fread( buf+len1+1, len2, 1, fp); |
---|
250 | *(buf+len1+len2+1)='\0'; |
---|
251 | printf("##BODY:'%s'##\n", buf); |
---|
252 | free(buf); |
---|
253 | |
---|
254 | // loading help |
---|
255 | len1 = pi->data.s.body_start - pi->data.s.proc_start; |
---|
256 | printf("len1=%d;\n", len1); |
---|
257 | buf = (char *)malloc(len1+1); |
---|
258 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
259 | fread( buf, len1, 1, fp); |
---|
260 | *(buf+len1)='\0'; |
---|
261 | printf("##HELP:'%s'##\n", buf); |
---|
262 | free(buf); |
---|
263 | |
---|
264 | if(pi->data.s.example_start>0) { // loading example |
---|
265 | fseek(fp, pi->data.s.example_start, SEEK_SET); |
---|
266 | len2 = pi->data.s.proc_end - pi->data.s.example_start; |
---|
267 | buf = (char *)malloc(len2+1); |
---|
268 | fread( buf, len2, 1, fp); |
---|
269 | *(buf+len2)='\0'; |
---|
270 | printf("##EXAMPLE:'%s'##\n", buf); |
---|
271 | free(buf); |
---|
272 | } |
---|
273 | |
---|
274 | //getchar(); |
---|
275 | } |
---|
276 | #endif |
---|
277 | } |
---|
278 | |
---|
279 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|