source: git/Singular/checklibs.c @ e3c628

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