1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <ctype.h> |
---|
5 | #include "getopt.h" |
---|
6 | #include "utils.h" |
---|
7 | #ifdef __MWERKS__ |
---|
8 | #define __GNU_LIBRARY__ |
---|
9 | #include "getopt.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 found_version, found_info, found_oldhelp, found_proc_in_proc; |
---|
17 | int warning_info = 0, warning_version = 0; |
---|
18 | |
---|
19 | static usage(char *progname) |
---|
20 | { |
---|
21 | printf("libparse: a syntax-checker for Singular Libraries.\n"); |
---|
22 | printf("USAGE: %s [options] singular-library\n", progname); |
---|
23 | printf("Options:\n"); |
---|
24 | printf(" -f <singular library> : performs syntax-checks\n"); |
---|
25 | printf(" -d [digit] : digit=1,..,4 increases the verbosity of the checks\n"); |
---|
26 | printf(" -s : turns on reporting about violations of unenforced syntax rules\n"); |
---|
27 | printf(" -h : print this message\n"); |
---|
28 | exit(1); |
---|
29 | } |
---|
30 | |
---|
31 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
32 | void main_init(int argc, char *argv[]) |
---|
33 | { |
---|
34 | char c, *file=NULL; |
---|
35 | |
---|
36 | while((c=getopt(argc, argv, "hd:sf:"))>=0) { |
---|
37 | switch(c) { |
---|
38 | case 'd': |
---|
39 | lpverbose = 1; |
---|
40 | if(isdigit(argv[optind-1][0])) sscanf(optarg, "%d", &lpverbose); |
---|
41 | else optind--; |
---|
42 | break; |
---|
43 | case 'f': file = argv[optind-1]; |
---|
44 | break; |
---|
45 | case 's': |
---|
46 | check++; |
---|
47 | break; |
---|
48 | case 'h' : |
---|
49 | usage(argv[0]); |
---|
50 | break; |
---|
51 | case -1 : printf("no such option:%s\n", argv[optind]); |
---|
52 | usage(argv[0]); |
---|
53 | break; |
---|
54 | default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[optind]); |
---|
55 | usage(argv[0]); |
---|
56 | } |
---|
57 | } |
---|
58 | if(file!=NULL) { |
---|
59 | yylpin = fopen( file, "rb" ); |
---|
60 | printf("Checking library '%s'\n", file); |
---|
61 | } else { |
---|
62 | while(argc>optind && yylpin==NULL) { |
---|
63 | yylpin = fopen( argv[optind], "rb" ); |
---|
64 | if(yylpin!=NULL) printf("Checking library '%s'\n", argv[optind]); |
---|
65 | else optind++; |
---|
66 | } |
---|
67 | } |
---|
68 | if(yylpin == NULL) { |
---|
69 | printf("No library found to parse.\n"); |
---|
70 | usage(argv[0]); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
75 | void main_result(char *libname) |
---|
76 | { |
---|
77 | if(!found_info) printf("*** No info-string found!\n"); |
---|
78 | if(!found_version) printf("*** No version-string found!\n"); |
---|
79 | if(found_oldhelp) printf("*** Library has stil OLD library-format.\n"); |
---|
80 | if(found_info && warning_info) |
---|
81 | printf("*** INFO-string should come before every procedure definition.\n"); |
---|
82 | if(found_version && warning_version) |
---|
83 | printf("*** VERSION-string should come before every procedure definition.\n"); |
---|
84 | } |
---|
85 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
86 | |
---|
87 | procinfo *iiInitSingularProcinfo(procinfov pi, char *libname, |
---|
88 | char *procname, int line, long pos, |
---|
89 | BOOLEAN pstatic = FALSE) |
---|
90 | { |
---|
91 | pi->libname = (char *)malloc(strlen(libname)+1); |
---|
92 | memcpy(pi->libname, libname, strlen(libname)); |
---|
93 | *(pi->libname+strlen(libname)) = '\0'; |
---|
94 | |
---|
95 | pi->procname = (char *)malloc(strlen(procname)+1); |
---|
96 | strcpy(pi->procname, procname/*, strlen(procname)*/); |
---|
97 | pi->language = LANG_SINGULAR; |
---|
98 | pi->ref = 1; |
---|
99 | pi->is_static = pstatic; |
---|
100 | pi->data.s.proc_start = pos; |
---|
101 | pi->data.s.def_end = 0L; |
---|
102 | pi->data.s.help_start = 0L; |
---|
103 | pi->data.s.body_start = 0L; |
---|
104 | pi->data.s.body_end = 0L; |
---|
105 | pi->data.s.example_start = 0L; |
---|
106 | pi->data.s.proc_lineno = line; |
---|
107 | pi->data.s.body_lineno = 0; |
---|
108 | pi->data.s.example_lineno = 0; |
---|
109 | pi->data.s.body = NULL; |
---|
110 | return(pi); |
---|
111 | } |
---|
112 | |
---|
113 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
114 | pi_clear(procinfov pi) |
---|
115 | { |
---|
116 | free(pi->libname); |
---|
117 | free(pi->procname); |
---|
118 | free(pi); |
---|
119 | } |
---|
120 | |
---|
121 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
122 | printpi(procinfov pi) |
---|
123 | { |
---|
124 | FILE *fp = fopen( pi->libname, "rb"); |
---|
125 | char *buf, name[256]; |
---|
126 | int len1, len2; |
---|
127 | |
---|
128 | if(!found_info && !warning_info) warning_info++; |
---|
129 | if(!found_version && !warning_version) warning_version++; |
---|
130 | if(pi->data.s.body_end==0) |
---|
131 | pi->data.s.body_end = pi->data.s.proc_end; |
---|
132 | |
---|
133 | if(lpverbose) printf("// "); |
---|
134 | printf( "%c %-15s %20s ", pi->is_static ? 'l' : 'g', pi->libname, |
---|
135 | pi->procname); |
---|
136 | printf("line %4d,%5ld-%-5ld %4d,%5ld-%-5ld %4d,%5ld-%-5ld\n", |
---|
137 | pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end, |
---|
138 | pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end, |
---|
139 | pi->data.s.example_lineno, pi->data.s.example_start, |
---|
140 | pi->data.s.proc_end); |
---|
141 | if(check) { |
---|
142 | if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4) |
---|
143 | printf("*** Procedure '%s' is global and has no help-section.\n", |
---|
144 | pi->procname); |
---|
145 | if(!pi->is_static && !pi->data.s.example_start) |
---|
146 | printf("*** Procedure '%s' is global and has no example-section.\n",\ |
---|
147 | pi->procname); |
---|
148 | if(found_proc_in_proc) |
---|
149 | printf("*** found proc within procedure '%s'.\n", pi->procname); |
---|
150 | } |
---|
151 | |
---|
152 | #if 0 |
---|
153 | if( fp != NULL) { // loading body |
---|
154 | len1 = pi->data.s.def_end - pi->data.s.proc_start; |
---|
155 | if(pi->data.s.body_end==0) |
---|
156 | len2 = pi->data.s.proc_end - pi->data.s.body_start; |
---|
157 | else len2 = pi->data.s.body_end - pi->data.s.body_start; |
---|
158 | buf = (char *)malloc(len1 + len2 + 1); |
---|
159 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
160 | fread( buf, len1, 1, fp); |
---|
161 | *(buf+len1) = '\n'; |
---|
162 | fseek(fp, pi->data.s.body_start, SEEK_SET); |
---|
163 | fread( buf+len1+1, len2, 1, fp); |
---|
164 | *(buf+len1+len2+1)='\0'; |
---|
165 | printf("##BODY:'%s'##\n", buf); |
---|
166 | free(buf); |
---|
167 | |
---|
168 | // loading help |
---|
169 | len1 = pi->data.s.body_start - pi->data.s.proc_start; |
---|
170 | printf("len1=%d;\n", len1); |
---|
171 | buf = (char *)malloc(len1+1); |
---|
172 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
173 | fread( buf, len1, 1, fp); |
---|
174 | *(buf+len1)='\0'; |
---|
175 | printf("##HELP:'%s'##\n", buf); |
---|
176 | free(buf); |
---|
177 | |
---|
178 | if(pi->data.s.example_start>0) { // loading example |
---|
179 | fseek(fp, pi->data.s.example_start, SEEK_SET); |
---|
180 | len2 = pi->data.s.proc_end - pi->data.s.example_start; |
---|
181 | buf = (char *)malloc(len2+1); |
---|
182 | fread( buf, len2, 1, fp); |
---|
183 | *(buf+len2)='\0'; |
---|
184 | printf("##EXAMPLE:'%s'##\n", buf); |
---|
185 | free(buf); |
---|
186 | } |
---|
187 | |
---|
188 | fclose(fp); |
---|
189 | //getchar(); |
---|
190 | } |
---|
191 | #endif |
---|
192 | } |
---|
193 | |
---|
194 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|