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