source: git/Singular/utils.cc @ dcc635

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