source: git/Singular/utils.cc @ 021751

spielwiese
Last change on this file since 021751 was fe72e1, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: symbol duplicate: iiInitSingularProcinfo
  • Property mode set to 100644
File size: 6.3 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include <kernel/fegetopt.h>
6#include <Singular/utils.h>
7#include <Singular/libparse.h>
8
9extern FILE *yylpin;
10extern char *optarg;
11extern int optind, opterr, optopt;
12extern int lpverbose, check;
13extern int texinfo_out;
14extern int category_out;
15extern int found_version, found_info, found_oldhelp, found_proc_in_proc;
16int warning_info = 0, warning_version = 0;
17
18static void usage(char *progname)
19{
20  printf("libparse: a syntax-checker for Singular Libraries.\n");
21  printf("USAGE: %s [options] singular-library\n", progname);
22  printf("Options:\n");
23  printf("   -f <singular library> : performs syntax-checks\n");
24  printf("   -d [digit]            : digit=1,..,4 increases the verbosity of the checks\n");
25  printf("   -s                    : turns on reporting about violations of unenforced syntax rules\n");
26  printf("   -i                    : perl output of examples and help of procs\n");
27  printf("   -c                    : print category of lib to stdout and exit\n");
28  printf("   -h                    : print this message\n");
29  exit(1);
30}
31
32static char* lib_file = NULL;
33
34/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
35void main_init(int argc, char *argv[])
36{
37  char c;
38
39  while((c=fe_getopt(argc, argv, "ihdc:sf:"))>=0)
40  {
41    switch(c)
42    {
43        case 'd':
44          lpverbose = 1;
45          if(isdigit(argv[fe_optind-1][0])) sscanf(optarg, "%d", &lpverbose);
46          else fe_optind--;
47          break;
48        case 'f': lib_file = argv[fe_optind-1];
49          break;
50        case 's':
51          check++;
52          break;
53        case 'i':
54          texinfo_out = 1;
55          break;
56        case 'c':
57          category_out = 1;
58          break;
59
60        case 'h' :
61          usage(argv[0]);
62          break;
63        case -1 : printf("no such option:%s\n", argv[fe_optind]);
64          usage(argv[0]);
65          break;
66        default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[fe_optind]);
67          usage(argv[0]);
68    }
69  }
70  if (texinfo_out || category_out) lpverbose = 0;
71
72  if(lib_file!=NULL)
73  {
74    yylpin = fopen( lib_file, "rb" );
75    if (! (texinfo_out || category_out))
76      printf("Checking library '%s'\n", lib_file);
77    else if (! category_out)
78      printf("$library = \"%s\";\n", lib_file);
79  }
80  else
81  {
82    while(argc>fe_optind && yylpin==NULL)
83    {
84      yylpin = fopen( argv[fe_optind], "rb" );
85      if(yylpin!=NULL)
86      {
87        lib_file = argv[fe_optind];
88        if (! (texinfo_out || category_out) )
89          printf("Checking library '%s'\n", argv[fe_optind]);
90        else if (! category_out)
91          printf("$library = \"%s\";\n", lib_file);
92      }
93      else fe_optind++;
94    }
95  }
96  if(yylpin == NULL)
97  {
98    printf("No library found to parse.\n");
99    usage(argv[0]);
100  }
101}
102
103/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
104void main_result(char *libname)
105{
106  if(!found_info)    printf("*** No info-string found!\n");
107  if(!found_version) printf("*** No version-string found!\n");
108  if(found_oldhelp)  printf("*** Library has stil OLD library-format.\n");
109  if(found_info && warning_info)
110    printf("*** INFO-string should come before every procedure definition.\n");
111  if(found_version && warning_version)
112    printf("*** VERSION-string should come before every procedure definition.\n");
113}
114/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
115
116/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
117void pi_clear(procinfov pi)
118{
119  free(pi->libname);
120  free(pi->procname);
121  free(pi);
122}
123
124/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
125
126#ifndef SEEK_SET
127#define SEEK_SET 0
128#endif
129
130static void PrintOut(FILE *fd, int pos_start, int pos_end)
131{
132  if (pos_start <= 0 || pos_end - pos_start <= 4) return;
133  char c = 0;
134
135  fseek(fd, pos_start, SEEK_SET);
136  while (pos_start++ <= pos_end)
137  {
138    if (c == '\\')
139    {
140      c = fgetc(fd);
141      if (c != '"') putchar('\\');
142    }
143    else
144      c = fgetc(fd);
145    if (c == '@' || c == '$') putchar('\\');
146    if (c != '\r') putchar(c);
147  }
148  if (c == '\\') putchar('\\');
149}
150
151
152void printpi(procinfov pi)
153{
154  char *buf, name[256];
155  int len1, len2;
156  /* pi->libname is badly broken -- use file, instead */
157  FILE *fp = fopen( lib_file, "rb");
158
159  if (fp == NULL)
160  {
161    printf("Can not open %s\n", lib_file);
162    return;
163  }
164
165  if(!found_info && !warning_info) warning_info++;
166  if(!found_version && !warning_version) warning_version++;
167  if(pi->data.s.body_end==0)
168    pi->data.s.body_end = pi->data.s.proc_end;
169
170  if (texinfo_out)
171  {
172   if ((! pi->is_static) &&
173        (pi->data.s.body_start - pi->data.s.def_end > 10) &&
174        (! found_proc_in_proc))
175    {
176      printf("push(@procs, \"%s\");\n", pi->procname);
177      printf("$help{\"%s\"} = <<EOT;\n", pi->procname);
178      PrintOut(fp, pi->data.s.help_start, pi->data.s.body_start-3);
179      printf("\nEOT\n");
180      if ((pi->data.s.example_start > 0) &&
181          (pi->data.s.proc_end - pi->data.s.example_start > 10))
182      {
183        printf("$example{\"%s\"} = <<EOT;\n", pi->procname);
184        PrintOut(fp, pi->data.s.example_start, pi->data.s.proc_end);
185        printf("\nEOT\n");
186      }
187      printf("$chksum{\"%s\"} = %ld;\n", pi->procname, pi->data.s.help_chksum);
188    }
189  }
190  else if (! category_out)
191  {
192    if(lpverbose) printf("//     ");
193    printf( "%c %-15s  %20s ", pi->is_static ? 'l' : 'g', pi->libname,
194            pi->procname);
195    printf("line %4d,%5ld-%-5ld  %4d,%5ld-%-5ld  %4d,%5ld-%-5ld\n",
196           pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end,
197           pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end,
198           pi->data.s.example_lineno, pi->data.s.example_start,
199           pi->data.s.proc_end);
200    if(check) {
201      if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4)
202        printf("*** Procedure '%s' is global and has no help-section.\n",
203               pi->procname);
204      if(!pi->is_static && !pi->data.s.example_start)
205        printf("*** Procedure '%s' is global and has no example-section.\n",\
206               pi->procname);
207      if(found_proc_in_proc)
208        printf("*** found proc within procedure '%s'.\n", pi->procname);
209    }
210  }
211
212  if (fp != NULL) fclose(fp);
213
214}
215
216/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.