source: git/Singular/utils.cc @ 416465

fieker-DuValspielwiese
Last change on this file since 416465 was c06a32, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* New Handling of Command-line options git-svn-id: file:///usr/local/Singular/svn/trunk@3623 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.0 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 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=fe_getopt(argc, argv, "ihd:sf:"))>=0) {
41    switch(c) {
42        case 'd':
43          lpverbose = 1;
44          if(isdigit(argv[fe_optind-1][0])) sscanf(optarg, "%d", &lpverbose);
45          else fe_optind--;
46          break;
47        case 'f': lib_file = argv[fe_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[fe_optind]);
60          usage(argv[0]);
61          break;
62        default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[fe_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>fe_optind && yylpin==NULL) {
76      yylpin = fopen( argv[fe_optind], "rb" );
77      if(yylpin!=NULL) 
78      {
79        lib_file = argv[fe_optind];
80        if (! texinfo_out) 
81          printf("Checking library '%s'\n", argv[fe_optind]);
82        else
83          printf("$library = \"%s\";\n", lib_file);
84      }
85      else fe_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  pi->data.s.help_chksum = 0;
131  return(pi);
132}
133
134/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
135pi_clear(procinfov pi)
136{
137  free(pi->libname);
138  free(pi->procname);
139  free(pi);
140}
141
142/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
143
144#ifndef SEEK_SET
145#define SEEK_SET 0
146#endif
147
148static void PrintOut(FILE *fd, int pos_start, int pos_end)
149{
150  if (pos_start <= 0 || pos_end - pos_start <= 4) return;
151  char c = 0;
152 
153  fseek(fd, pos_start, SEEK_SET);
154  while (pos_start++ <= pos_end) 
155  {
156    if (c == '\\')
157    {
158      c = fgetc(fd);
159      if (c != '"') putchar('\\');
160    }
161    else
162      c = fgetc(fd);
163    if (c == '@' || c == '$') putchar('\\');
164    if (c != '\r') putchar(c);
165  }
166  if (c == '\\') putchar('\\');
167}
168
169
170printpi(procinfov pi)
171{
172  char *buf, name[256];
173  int len1, len2;
174  /* pi->libname is badly broken -- use file, instead */
175  FILE *fp = fopen( lib_file, "rb");
176
177  if (fp == NULL) 
178  {
179    printf("Can not open %s\n", lib_file);
180    return 0;
181  }
182
183  if(!found_info && !warning_info) warning_info++;
184  if(!found_version && !warning_version) warning_version++;
185  if(pi->data.s.body_end==0)
186    pi->data.s.body_end = pi->data.s.proc_end;
187
188  if (texinfo_out)
189  {
190   if ((! pi->is_static) &&
191        (pi->data.s.body_start - pi->data.s.def_end > 10) &&
192        (! found_proc_in_proc))
193    {
194      printf("push(@procs, \"%s\");\n", pi->procname);
195      printf("$help{\"%s\"} = <<EOT;\n", pi->procname);
196      PrintOut(fp, pi->data.s.help_start, pi->data.s.body_start-3);
197      printf("\nEOT\n");
198      if ((pi->data.s.example_start > 0) &&
199          (pi->data.s.proc_end - pi->data.s.example_start > 10))
200      {
201        printf("$example{\"%s\"} = <<EOT;\n", pi->procname);
202        PrintOut(fp, pi->data.s.example_start, pi->data.s.proc_end);
203        printf("\nEOT\n");
204      }
205      printf("$chksum{\"%s\"} = %d;\n", pi->procname, pi->data.s.help_chksum);
206    }
207  }
208  else
209  {
210    if(lpverbose) printf("//     ");
211    printf( "%c %-15s  %20s ", pi->is_static ? 'l' : 'g', pi->libname,
212            pi->procname);
213    printf("line %4d,%5ld-%-5ld  %4d,%5ld-%-5ld  %4d,%5ld-%-5ld\n",
214           pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end,
215           pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end,
216           pi->data.s.example_lineno, pi->data.s.example_start,
217           pi->data.s.proc_end);
218    if(check) {
219      if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4)
220        printf("*** Procedure '%s' is global and has no help-section.\n",
221               pi->procname);
222      if(!pi->is_static && !pi->data.s.example_start)
223        printf("*** Procedure '%s' is global and has no example-section.\n",\
224               pi->procname);
225      if(found_proc_in_proc)
226        printf("*** found proc within procedure '%s'.\n", pi->procname);
227    }
228  }
229 
230
231#if 0
232  if( fp != NULL) { // loading body
233    len1 = pi->data.s.def_end - pi->data.s.proc_start;
234    if(pi->data.s.body_end==0)
235      len2 = pi->data.s.proc_end - pi->data.s.body_start;
236    else len2 = pi->data.s.body_end - pi->data.s.body_start;
237    buf = (char *)malloc(len1 + len2 + 1);
238    fseek(fp, pi->data.s.proc_start, SEEK_SET);
239    fread( buf, len1, 1, fp);
240    *(buf+len1) = '\n';
241    fseek(fp, pi->data.s.body_start, SEEK_SET);
242    fread( buf+len1+1, len2, 1, fp);
243    *(buf+len1+len2+1)='\0';
244    printf("##BODY:'%s'##\n", buf);
245    free(buf);
246
247    // loading help
248    len1 = pi->data.s.body_start - pi->data.s.proc_start;
249    printf("len1=%d;\n", len1);
250    buf = (char *)malloc(len1+1);
251    fseek(fp, pi->data.s.proc_start, SEEK_SET);
252    fread( buf, len1, 1, fp);
253    *(buf+len1)='\0';
254    printf("##HELP:'%s'##\n", buf);
255    free(buf);
256
257    if(pi->data.s.example_start>0) { // loading example
258      fseek(fp, pi->data.s.example_start, SEEK_SET);
259      len2 = pi->data.s.proc_end - pi->data.s.example_start;
260      buf = (char *)malloc(len2+1);
261      fread( buf, len2, 1, fp);
262      *(buf+len2)='\0';
263      printf("##EXAMPLE:'%s'##\n", buf);
264      free(buf);
265    }
266
267    fclose(fp);
268    //getchar();
269  }
270#endif
271}
272
273/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.