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