1 /********************************************************************/
2 /*                                                                  */
3 /*  s7   Seed7 interpreter                                          */
4 /*  Copyright (C) 1990 - 2015, 2021  Thomas Mertes                  */
5 /*                                                                  */
6 /*  This program is free software; you can redistribute it and/or   */
7 /*  modify it under the terms of the GNU General Public License as  */
8 /*  published by the Free Software Foundation; either version 2 of  */
9 /*  the License, or (at your option) any later version.             */
10 /*                                                                  */
11 /*  This program is distributed in the hope that it will be useful, */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of  */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   */
14 /*  GNU General Public License for more details.                    */
15 /*                                                                  */
16 /*  You should have received a copy of the GNU General Public       */
17 /*  License along with this program; if not, write to the           */
18 /*  Free Software Foundation, Inc., 51 Franklin Street,             */
19 /*  Fifth Floor, Boston, MA  02110-1301, USA.                       */
20 /*                                                                  */
21 /*  Module: General                                                 */
22 /*  File: seed7/src/syvarutl.c                                      */
23 /*  Changes: 1991 - 1994, 2010, 2013, - 2015, 2021  Thomas Mertes   */
24 /*  Content: Maintains the interpreter system variables.            */
25 /*                                                                  */
26 /********************************************************************/
27 
28 #define LOG_FUNCTIONS 0
29 #define VERBOSE_EXCEPTIONS 0
30 
31 #include "version.h"
32 
33 #include "stdio.h"
34 #include "string.h"
35 
36 #include "common.h"
37 #include "data.h"
38 #include "heaputl.h"
39 #include "striutl.h"
40 
41 #undef EXTERN
42 #define EXTERN
43 #include "syvarutl.h"
44 
45 
46 #define MAX_CSTRI_BUFFER_LEN 25
47 
48 static const const_cstriType sys_name[NUMBER_OF_SYSVARS] = {
49     "empty",
50     "memory_error",
51     "numeric_error",
52     "overflow_error",
53     "range_error",
54     "index_error",
55     "file_error",
56     "database_error",
57     "close_error",
58     "illegal_action",
59     "false",
60     "true",
61     "expr",
62     "integer",
63     "bigInteger",
64     "char",
65     "string",
66     "proc",
67     "float",
68     "assign",
69     "create",
70     "destroy",
71     "ord",
72     "in",
73     "dot",
74     "value",
75     "prot_outfile",
76     "flush",
77     "write",
78     "writeln",
79     "main",
80   };
81 
82 static objectRecord dummy_expr_type;
83 
84 
85 
86 /**
87  *  Determine the index of a system variable with a given name.
88  *  @return the index of the system variable, or
89  *          -1 if no system variable with the name is found.
90  */
findSysvar(const_striType stri)91 int findSysvar (const_striType stri)
92 
93   {
94     char sysvar_name[MAX_CSTRI_BUFFER_LEN + NULL_TERMINATION_LEN];
95     int result;
96 
97   /* findSysvar */
98     logFunction(printf("findSysvar(\"%s\")\n",
99                        striAsUnquotedCStri(stri)););
100     if (stri->size > MAX_CSTRI_BUFFER_LEN) {
101       result = -1;
102     } else {
103       if (unlikely(conv_to_cstri(sysvar_name, stri) == NULL)) {
104         result = -1;
105       } else {
106         result = NUMBER_OF_SYSVARS - 1;
107         while (result >= 0 &&
108             strcmp(sysvar_name, sys_name[result]) != 0) {
109           result--;
110         } /* while */
111         /* printf("findSysvar: %s -> %d\n", sysvar_name, result); */
112       } /* if */
113     } /* if */
114     logFunction(printf("findSysvar --> %d\n", result););
115     return result;
116   } /* findSysvar */
117 
118 
119 
init_sysvar(progType aProgram)120 void init_sysvar (progType aProgram)
121 
122   {
123     int number;
124 
125   /* init_sysvar */
126     logFunction(printf("init_sysvar\n"););
127     for (number = 0; number < NUMBER_OF_SYSVARS; number++) {
128       aProgram->sys_var[number] = NULL;
129     } /* for */
130     /* Initialize SYS_EXPR_TYPE (EXPR_TYPE) to avoid an */
131     /* error, if the source has no include directive.   */
132     dummy_expr_type.type_of = NULL;
133     dummy_expr_type.descriptor.property = NULL;
134     INIT_CATEGORY_OF_OBJ(&dummy_expr_type, TYPEOBJECT);
135     dummy_expr_type.value.typeValue = NULL;
136     EXPR_TYPE(aProgram) = &dummy_expr_type;
137     logFunction(printf("init_sysvar -->\n"););
138   } /* init_sysvar */
139