source: git/Singular/checklibs.c @ 2a6d35

spielwiese
Last change on this file since 2a6d35 was 2a6d35, checked in by Hans Schoenemann <hannes@…>, 14 years ago
check for ascii chars git-svn-id: file:///usr/local/Singular/svn/trunk@13419 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.1 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;
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)
289      {
290        have_version++;
291        pp=p+8;
292        while((*pp)==' ') pp++;
293        if (((*pp)!='"') || (strstr(pp,"$Id")==NULL))
294          printf("error: version string should start with $""Id");
295      }
296    }
297    if ((p=strstr(buf,"category="))!=NULL)
298    {
299      char *pp=buf;
300      while (pp!=p)
301      {
302       if ((*pp!=' ')&&(*pp!='\t')) break;
303       pp++;
304      }
305      if (p=pp) have_category++;
306    }
307    if ((p=strstr(buf,"info="))!=NULL)
308    {
309      char *pp=buf;
310      while (pp!=p)
311      {
312       if ((*pp!=' ')&&(*pp!='\t')) break;
313       pp++;
314      }
315      if (p=pp) { have_info++; scan_info(&header); }
316    }
317    if ((p=strstr(buf,"LIB\""))!=NULL)
318    {
319      printf("error: use a space between LIB and \"\n");
320      if (p!=buf)
321      { printf("end of header ? LIB should be in col. 1:>>%s<<\n",buf); }
322      break; /* end of header */
323    }
324    if ((p=strstr(buf,"LIB \""))!=NULL)
325    {
326      if (p!=buf)
327      { printf("end of header ? LIB should be in col. 1:>>%s<<\n",buf); }
328      break; /* end of header */
329    }
330    if ((p=strstr(buf,"proc "))!=NULL)
331    {
332      if ((p!=buf)&&(strncmp(buf,"static proc ",12)!=0))
333      { printf("end of header ? proc should be in col. 1:>>%s<<\n",buf); }
334      break; /* end of header */
335    }
336    get_next(); header++;
337    if(feof(f)) break;
338  }
339  printf("header parsed: %d lines of %s\n\n",header,argv[1]);
340  /* part 2: procs */
341  while(!feof(f))
342  {
343    if ((strstr(buf,"static")==buf) && (strstr(buf,"proc")==NULL))
344    {
345      printf("error: 'static' without 'proc' found\n");
346      get_next();
347    }
348    if(((p=strstr(buf,"proc "))!=NULL)
349    &&(strncmp(buf,"static proc ",12)!=0))
350    {
351      char *pp=buf;
352      int i;
353      while(*pp==' ') pp++;
354      if ((pp!=buf)&&(pp==p))
355      {
356        printf("warning: proc should be in col. 1: line %d:%s",lines,buf);
357      }
358      else if (pp!=p)
359      {
360        footer++; get_next(); continue; /* this is not a proc start*/
361      }
362      p+=5; /* skip proc+blank*/
363      while(*p==' ') p++;
364      pp=p;
365      while(isalnum(*p)||(*p=='_')) p++;
366      *p='\0';
367      for(i=proc_cnt-1;i>=0;i--)
368      {
369        if(strcmp(proc[i],pp)==0) break;
370      }
371      if (i<0)
372      {
373        printf("hint: global proc %s not found in header\n",pp);
374        footer++; get_next();
375      }
376      else
377      {
378        proc_found[i]=1;
379        footer++; get_next(); /* doc should start at next line */
380        p=buf;
381        while(*p==' ') p++;
382        if (*p == '"') have_doc[i]=1;
383        /* serach for example */
384        while(!feof(f))
385        {
386           if(strncmp(buf,"proc ",5)==0) break;
387           if(strncmp(buf,"static proc ",12)==0) break;
388           if(strncmp(buf,"example",7)==0)
389           {
390             have_example[i]=1;
391             break;
392           }
393           footer++; get_next();
394        }
395      }
396    }
397    else {get_next();footer++;}
398  }
399  {
400    int i;
401    for(i=proc_cnt-1; i>=0;i--)
402    {
403      if(proc_found[i]==0) printf("proc %s not found\n",proc[i]);
404      else
405      {
406        if(have_doc[i]==0) printf("proc %s has no documentation\n",proc[i]);
407        if(have_example[i]==0) printf("proc %s has no example (or it does not start in col. 1)\n",proc[i]);
408      }
409    }
410  }
411  /* part 3: summary*/
412  printf("\nproc part parsed: %d lines of %s\n",footer,argv[1]);
413  if (have_version!=1) printf("version missing/dupplicate (%d)\n",have_version);
414  if (have_category!=1) printf("category missing/dupplicate (%d)\n",have_category);
415  if (have_info!=1) printf("info missing/dupplicate (%d)\n",have_info);
416
417  printf("\nGENERAL SUMMARY:\n");
418  if(tabs!=0) printf("warning: lib should not contain tabs, >=%d found\n",tabs);
419  if(trailing_spaces!=0) printf("hint: lib should not contain trailing_spaces, >=%d found\n",trailing_spaces);
420  if(verylong_lines!=0) printf("hint: lib should not contain very long lines, >=%d found\n",verylong_lines);
421  if(non_ascii>0)
422  printf("error: lib shlould not contain non-ascii characters, %d found, last in line %d\n",non_ascii, non_ascii_line);
423  if (crlf>=lines-1)
424  {
425    printf("warning: DOS format (%d)\n",crlf);
426  }
427  else if (crlf>0)
428  {
429    printf("error: some lines are in DOS format, some not (%d/%d)\n",crlf,lines);
430  }
431  printf("%d lines parsed\n",lines);
432  printf("%d proc found in header\n",proc_cnt);
433  fclose(f);
434  return 0;
435}
Note: See TracBrowser for help on using the repository browser.