source: git/Singular/utils.cc @ c90500

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