source: git/Singular/utils.cc @ 9f7665

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