source: git/Singular/utils.cc @ 97dcf5

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