source: git/Singular/checklibs.c @ 9d58ee7

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