source: git/Singular/utils.cc @ a3bc95e

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