source: git/Singular/checklibs.c @ 0997b93

spielwiese
Last change on this file since 0997b93 was 0997b93, checked in by Hans Schoenemann <hannes@…>, 14 years ago
messages improved git-svn-id: file:///usr/local/Singular/svn/trunk@13318 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.3 KB
Line 
1#include <stdio.h>
2#include <string.h>
3
4#define NUM_PROC 200
5#define LINE_LEN 200
6#define RECOMMENDED_LEN 100
7FILE *f;
8int trailing_spaces=0;
9int tabs=0;
10int verylong_lines=0;
11int lines=0;
12char buf[LINE_LEN];
13int proc_cnt=0;
14char *proc[NUM_PROC];
15char have_doc[NUM_PROC];
16char have_example[NUM_PROC];
17char proc_found[NUM_PROC];
18int non_ascii=0;
19int non_ascii_line=0;
20int star_nl=0;
21int footer=0;
22int header=0;
23int crlf=0;
24
25void get_next()
26{
27  int i;
28  memset(buf,0,LINE_LEN);
29  fgets(buf,LINE_LEN,f);
30  lines++;
31  if (buf[0]!='\0')
32  {
33    if (strchr(buf,'\r')!=NULL) crlf++;
34    if ((buf[LINE_LEN-1]!='\0')||(strlen(buf)>RECOMMENDED_LEN))
35    {
36      if (verylong_lines==0) printf("warning: very long line (%d):\n%s\n",lines,buf);
37      verylong_lines++;
38    }
39    if ((strstr(buf," \n")!=NULL)||(strstr(buf," \r\n")!=NULL)) trailing_spaces++;
40    if (strchr(buf,'\t')!=NULL) tabs++;
41    for(i=0;(i<LINE_LEN) && (buf[i]!='\0'); i++)
42    {
43      if (buf[i]>127) { non_ascii++;non_ascii_line=lines; break; }
44    }
45    if (footer==0) /* we are still in the header */
46    {
47      if (strstr(buf,"@*")!=NULL) star_nl++;
48    }
49  }
50}
51
52void scan_proc(int *l)
53{
54  char *p;
55  while(1)
56  {
57    get_next(); (*l)++;
58    if (((p=strchr(buf,'('))!=NULL)&&(isalnum(*(--p))||(*p=='_')))
59    {
60      char *s=buf;
61      while(*s==' ') s++;
62      p++; (*p)='\0';
63      if ((((int)(long)(s-buf))>10)||(strchr(s,' ')!=NULL))
64      {
65        printf("warning: probably not a proc ? (%s)\n",s);
66      }
67      else
68      {
69        proc[proc_cnt]=strdup(s); proc_cnt++;
70      }
71    }
72    else if (strstr(buf,"LIB ")!=NULL) break;
73    else if (strstr(buf,"LIB\"")!=NULL) break;
74    else if (strstr(buf,"proc ")!=NULL) break;
75    else if (strncmp(buf,"\";",2)==0) break; /* poor mans end-of-info*/
76    else if ((p=strstr(buf,":"))!=NULL)
77    { /* handles all capital letters + : */
78      /* SEE ALSO, KEYWORDS, NOTE, ... */
79      int ch;
80      char *pp=buf;
81      while((*pp==' ')||(*pp=='\t')) pp++;
82      ch=strspn(pp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
83      if ((ch>1)||(pp+ch==p))
84      {
85        break;
86      }
87    }
88  }
89  if (proc_cnt==0)
90    printf("warning: no proc found in the section PROCEDURES ?\n");
91  printf("\n# proc mentioned in the header: %d\n",proc_cnt);
92}
93
94void scan_keywords(int *l)
95{
96  /* the main problem with KEYWORDS: seperator between is ;,
97   * but it MUST NOT appear at the end */
98  char *p;
99  while(!feof(f))
100  {
101    p=strrchr(buf,';'); /* the last ; in the line*/
102    if (p==NULL) { get_next(); (*l)++; return; }
103    while (*p==' ') p++;
104    if (isalpha(*p)) { get_next(); (*l)++; return; }
105    if (*p=='\0') { get_next(); (*l)++; }
106    else if (strstr(buf,"LIB ")!=NULL) break;
107    else if (strstr(buf,"LIB\"")!=NULL) break;
108    else if (strstr(buf,"proc ")!=NULL) break;
109    else if (strncmp(buf,"\";",2)==0) break; /* poor mans end-of-info*/
110    else if ((p=strstr(buf,":"))!=NULL)
111    { /* handles all capital letters + : */
112      /* SEE ALSO, KEYWORDS, NOTE, ... */
113      int ch;
114      char *pp=buf;
115      while((*pp==' ')||(*pp=='\t')) pp++;
116      ch=strspn(pp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
117      if ((ch>1)||(pp+ch==p))
118      {
119        break;
120      }
121    }
122  }
123  printf("error: seperate keywords by ; but do not have ; after the last keyword\n");
124}
125void scan_info(int *l)
126{
127  int have_LIBRARY=0;
128  int have_AUTHORS=0;
129  int have_PROCEDURES=0;
130  int have_SEEALSO=0;
131  int have_KEYWORDS=0;
132  int have_OVERVIEW=0;
133  int have_NOTE=0;
134  int have_other=0;
135  char *p;
136
137  while(!feof(f))
138  {
139    if (strstr(buf,"LIBRARY: ")!=NULL)
140    {
141      have_LIBRARY++;
142      /* musrt be first*/
143      if (have_other+have_AUTHORS+have_PROCEDURES+have_KEYWORDS+have_SEEALSO!=0)
144        printf("error: LIBRARY: must be the first section in info\n");
145    }
146    else if (strstr(buf,"NOTE:")!=NULL)
147    {
148      if (have_PROCEDURES!=0)
149        printf("error: only KEYWORDS/SEE ALSO may follow PROCEDURES\n");
150      have_NOTE++;
151    }
152    else if (strstr(buf,"OVERVIEW:")!=NULL)
153    {
154      have_OVERVIEW++;
155      if (have_PROCEDURES!=0)
156        printf("error: only KEYWORDS/SEE ALSO may follow PROCEDURES\n");
157    }
158    else if (strstr(buf,"KEYWORDS: ")!=NULL)
159    {
160      have_KEYWORDS++;
161    }
162    else if (strstr(buf,"SEE ALSO: ")!=NULL)
163    {
164      have_SEEALSO++;
165    }
166    else if ((strstr(buf,"AUTHORS: ")!=NULL)
167      ||(strstr(buf,"AUTHOR: ")!=NULL))
168    {
169      have_AUTHORS++;
170      if (have_PROCEDURES!=0)
171        printf("error: only KEYWORDS/SEE ALSO may follow PROCEDURES\n");
172    }
173    else if ((p=strstr(buf,"PROCEDURES:"))!=NULL)
174    {
175      char *pp=buf;
176      while (pp!=p)
177      {
178       if ((*pp!=' ')&&(*pp!='\t')) break;
179       pp++;
180      }
181      if (p==pp)
182      {
183        have_PROCEDURES++;
184        scan_proc(l);
185        continue;
186      }
187      else
188      {
189        printf("error: unknown section in library header: %s",buf);
190        have_other++;
191      }
192    }
193    else if ((p=strstr(buf,":"))!=NULL)
194    {
195      int ch;
196      char *pp=buf;
197      while((*pp==' ')||(*pp=='\t')) pp++;
198      ch=strspn(pp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
199      if ((ch>1)||(pp+ch==p))
200      {
201        printf("error: unknown section in library header: %s",buf);
202        have_other++;
203      }
204    }
205    else if (strncmp(buf,"\";",2)==0) goto e_o_info; /* poor mans end-of-info*/
206    get_next(); (*l)++;
207  }
208  e_o_info:
209  printf("\nSUMMARY OF THE HEADER:\n");
210    if (have_LIBRARY!=1)
211      printf("error: missing/duplicate LIBRARY (%d lines found, should be 1)\n",have_LIBRARY);
212    if (have_AUTHORS!=1)
213      printf("error: missing/duplicate AUTHOR/AUTHORS (%d lines found, should be 1)\n",have_AUTHORS);
214    if (have_PROCEDURES!=1)
215      printf("error: missing/duplicate PROCEDURES (%d lines found, should be 1)\n",have_PROCEDURES);
216    if (have_SEEALSO>1)
217      printf("error: duplicate SEE ALSO (%d lines found)\n",have_SEEALSO);
218    if (have_KEYWORDS>1)
219      printf("error: duplicate KEYWORDS (%d lines found)\n",have_KEYWORDS);
220    if (have_NOTE==1)
221      printf("hint: avoid NOTE: if not used for a library requirement\n");
222    else if (have_NOTE>1)
223      printf("error: duplicate NOTE (%d lines found)\n",have_NOTE);
224    if ((have_OVERVIEW==1)&&(proc_cnt<3))
225      printf("hint: avoid OVERVIEW: for small libraries\n");
226    else if (have_OVERVIEW>1)
227      printf("error: duplicate OVERVIEW (%d lines found)\n",have_OVERVIEW);
228
229    if (have_other!=0)
230      printf("error: other header entries found (illegal ?) :%d lines found, should be 0\n",have_other);
231    if ((star_nl>0)&&(star_nl*10>=header))
232    {
233      printf("warning: %d forced line braks in %d header lines: @* should be used very rarely!\n",star_nl,header);
234    }
235}
236
237int main(int argc, char** argv)
238{
239  int have_version=0;
240  int have_category=0;
241  int have_info=0;
242  char *p;
243
244  memset(proc,0,NUM_PROC*sizeof(char*));
245  memset(have_doc,0,NUM_PROC);
246  memset(have_example,0,NUM_PROC);
247  memset(proc_found,0,NUM_PROC);
248  if (argc!=2) { printf("usage: %s lib-file\n",argv[0]); return 1;}
249
250  printf("\n          CHECKING LIBRARY %s\n\n",argv[1]);
251
252  f=fopen(argv[1],"r");
253  if(f==NULL) { printf("cannot read %s\n",argv[1]); return 2; }
254  buf[0]='\0';
255  get_next(); header++;
256  if (strncmp(buf,"//",2)!=0) { printf("error: lib must start with //\n"); }
257  else { get_next(); header++; }
258  /* pass 1: check header */
259  while(1)
260  {
261    if ((p=strstr(buf,"version="))!=NULL)
262    {
263      char *pp=buf;
264      while (pp!=p)
265      {
266       if ((*pp!=' ')&&(*pp!='\t')) break;
267       pp++;
268      }
269      if (p=pp) have_version++;
270    }
271    if ((p=strstr(buf,"category="))!=NULL)
272    {
273      char *pp=buf;
274      while (pp!=p)
275      {
276       if ((*pp!=' ')&&(*pp!='\t')) break;
277       pp++;
278      }
279      if (p=pp) have_category++;
280    }
281    if ((p=strstr(buf,"info="))!=NULL)
282    {
283      char *pp=buf;
284      while (pp!=p)
285      {
286       if ((*pp!=' ')&&(*pp!='\t')) break;
287       pp++;
288      }
289      if (p=pp) { have_info++; scan_info(&header); }
290    }
291    if ((p=strstr(buf,"LIB\""))!=NULL)
292    {
293      printf("error: use a space between LIB and \"\n");
294      if (p!=buf)
295      { printf("end of header ? LIB should be in col. 1:>>%s<<\n",buf); }
296      break; /* end of header */
297    }
298    if ((p=strstr(buf,"LIB \""))!=NULL)
299    {
300      if (p!=buf)
301      { printf("end of header ? LIB should be in col. 1:>>%s<<\n",buf); }
302      break; /* end of header */
303    }
304    if ((p=strstr(buf,"proc "))!=NULL)
305    {
306      if ((p!=buf)&&(strncmp(buf,"static proc ",12)!=0))
307      { printf("end of header ? proc should be in col. 1:>>%s<<\n",buf); }
308      break; /* end of header */
309    }
310    get_next(); header++;
311    if(feof(f)) break;
312  }
313  printf("header parsed: %d lines of %s\n\n",header,argv[1]);
314  /* part 2: procs */
315  while(!feof(f))
316  {
317    if ((strstr(buf,"static")==buf) && (strstr(buf,"proc")==NULL))
318    {
319      printf("error: 'static' without 'proc' found\n");
320      get_next();
321    }
322    if(((p=strstr(buf,"proc "))!=NULL)
323    &&(strncmp(buf,"static proc ",12)!=0))
324    {
325      char *pp=buf;
326      int i;
327      while(*pp==' ') pp++;
328      if ((pp!=buf)&&(pp==p))
329      {
330        printf("warning: proc should be in col. 1: line %d:%s",lines,buf);
331      }
332      else if (pp!=p)
333      {
334        footer++; get_next(); continue; /* this is not a proc start*/
335      }
336      p+=5; /* skip proc+blank*/
337      while(*p==' ') p++;
338      pp=p;
339      while(isalnum(*p)||(*p=='_')) p++;
340      *p='\0';
341      for(i=proc_cnt-1;i>=0;i--)
342      {
343        if(strcmp(proc[i],pp)==0) break;
344      }
345      if (i<0) 
346      {
347        printf("hint: global proc %s not found in header\n",pp);
348        footer++; get_next();
349      }
350      else
351      {
352        proc_found[i]=1;
353        footer++; get_next(); /* doc should start at next line */
354        p=buf;
355        while(*p==' ') p++;
356        if (*p == '"') have_doc[i]=1;
357        /* serach for example */
358        while(!feof(f))
359        {
360           if(strncmp(buf,"proc ",5)==0) break;
361           if(strncmp(buf,"static proc ",12)==0) break;
362           if(strncmp(buf,"example",7)==0)
363           {
364             have_example[i]=1;
365             break;
366           }
367           footer++; get_next();
368        }
369      }
370    }
371    else {get_next();footer++;}
372  }
373  {
374    int i;
375    for(i=proc_cnt-1; i>=0;i--)
376    {
377      if(proc_found[i]==0) printf("proc %s not found\n",proc[i]);
378      else
379      {
380        if(have_doc[i]==0) printf("proc %s has no documentation\n",proc[i]);
381        if(have_example[i]==0) printf("proc %s has no example (or it does not start in col. 1)\n",proc[i]);
382      }
383    }
384  }
385  /* part 3: summary*/
386  printf("\nproc part parsed: %d lines of %s\n",footer,argv[1]);
387  if (have_version!=1) printf("version missing/dupplicate (%d)\n",have_version);
388  if (have_category!=1) printf("category missing/dupplicate (%d)\n",have_category);
389  if (have_info!=1) printf("info missing/dupplicate (%d)\n",have_info);
390
391  printf("\nGENERAL SUMMARY:\n");
392  if(tabs!=0) printf("warning: lib should not contain tabs, >=%d found\n",tabs);
393  if(trailing_spaces!=0) printf("hint: lib should not contain trailing_spaces, >=%d found\n",trailing_spaces);
394  if(verylong_lines!=0) printf("hint: lib should not contain very long lines, >=%d found\n",verylong_lines);
395  if(non_ascii>0)
396  printf("error: lib shlould not contain non-ascii characters, %d found, last in line %d\n",non_ascii, non_ascii_line);
397  if (crlf>=lines-1)
398  {
399    printf("warning: DOS format (%d)\n",crlf);
400  }
401  else if (crlf>0)
402  {
403    printf("error: some lines are in DOS format, some not (%d/%d)\n",crlf,lines);
404  }
405  printf("%d lines parsed\n",lines);
406  printf("%d proc found in header\n",proc_cnt);
407  fclose(f);
408  return 0;
409}
Note: See TracBrowser for help on using the repository browser.