source: git/Singular/checklibs.c @ c90500

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