source: git/Singular/utils.cc @ eb0708

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