1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 /* gretl_commands.c */
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 
26 #include "gretl_commands.h"
27 
28 struct gretl_cmd {
29     int cnum;
30     const char *cword;
31 };
32 
33 static struct gretl_cmd gretl_cmds[] = {
34     { 0,        "" },
35     { ADD,      "add" },
36     { ADF,      "adf" },
37     { ANOVA,    "anova" },
38     { APPEND,   "append" },
39     { AR,       "ar" },
40     { AR1,      "ar1" },
41     { ARCH,     "arch" },
42     { ARMA,     "arima" },
43     { BDS,      "bds" },
44     { BIPROBIT, "biprobit" },
45     { BKW,      "bkw" },
46     { BREAK,    "break" },
47     { BXPLOT,   "boxplot" },
48     { CHOW,     "chow" },
49     { CLEAR,    "clear" },
50     { COEFFSUM, "coeffsum" },
51     { COINT,    "coint" },
52     { COINT2,   "johansen" },
53     { CORR,     "corr" },
54     { CORRGM,   "corrgm" },
55     { CUSUM,    "cusum" },
56     { DATA,     "data" },
57     { DATAMOD,  "dataset" },
58     { DELEET,   "delete" },
59     { DIFF,     "diff" },
60     { DIFFTEST, "difftest" },
61     { DISCRETE, "discrete" },
62     { DPANEL,   "dpanel" },
63     { DUMMIFY,  "dummify" },
64     { DURATION, "duration" },
65     { ELIF,     "elif" },
66     { ELSE,     "else" },
67     { END,      "end" },
68     { ENDIF,    "endif" },
69     { ENDLOOP,  "endloop" },
70     { EQNPRINT, "eqnprint" },
71     { EQUATION, "equation" },
72     { ESTIMATE, "estimate" },
73     { EVAL,     "eval" },
74     { FCAST,    "fcast" },
75     { FLUSH,    "flush" },
76     { FOREIGN,  "foreign" },
77     { FRACTINT, "fractint" },
78     { FREQ,     "freq" },
79     { FUNC,     "function" },
80     { FUNCERR,  "funcerr" },
81     { GARCH,    "garch" },
82     { GENR,     "genr" },
83     { GMM,      "gmm" },
84     { GNUPLOT,  "gnuplot" },
85     { GRAPHPG,  "graphpg" },
86     { HECKIT,   "heckit" },
87     { HELP,     "help" },
88     { HFPLOT,   "hfplot" },
89     { HSK,      "hsk" },
90     { HURST,    "hurst" },
91     { IF,       "if" },
92     { INCLUDE,  "include" },
93     { INFO,     "info" },
94     { INTREG,   "intreg" },
95     { JOIN,     "join" },
96     { KPSS,     "kpss" },
97     { LABELS,   "labels" },
98     { LAD,      "lad" },
99     { LAGS,     "lags" },
100     { LDIFF,    "ldiff" },
101     { LEVERAGE, "leverage" },
102     { LEVINLIN, "levinlin" },
103     { LOGISTIC, "logistic" },
104     { LOGIT,    "logit" },
105     { LOGS,     "logs" },
106     { LOOP,     "loop" },
107     { MAHAL,    "mahal" },
108     { MAKEPKG,  "makepkg" },
109     { MARKERS,  "markers" },
110     { MEANTEST, "meantest" },
111     { MIDASREG, "midasreg" },
112     { MLE,      "mle" },
113     { MODELTAB, "modeltab" },
114     { MODPRINT, "modprint" },
115     { MODTEST,  "modtest" },
116     { MPI,      "mpi" },
117     { MPOLS,    "mpols" },
118     { NEGBIN,   "negbin" },
119     { NLS,      "nls" },
120     { NORMTEST, "normtest" },
121     { NULLDATA, "nulldata" },
122     { OLS,      "ols" },
123     { OMIT,     "omit" },
124     { OPEN,     "open" },
125     { ORTHDEV,  "orthdev" },
126     { OUTFILE,  "outfile" },
127     { PANEL,    "panel" },
128     { PANPLOT,  "panplot" },
129     { PANSPEC,  "panspec" },
130     { PCA,      "pca" },
131     { PERGM,    "pergm" },
132     { PLOT,     "plot" },
133     { POISSON,  "poisson" },
134     { PRINT,    "print" },
135     { PRINTF,   "printf" },
136     { PROBIT,   "probit" },
137     { PVAL,     "pvalue" },
138     { QUANTREG, "quantreg" },
139     { QLRTEST,  "qlrtest" },
140     { QQPLOT,   "qqplot" },
141     { QUIT,     "quit" },
142     { RENAME,   "rename" },
143     { RESET,    "reset" },
144     { RESTRICT, "restrict" },
145     { RMPLOT,   "rmplot" },
146     { RUN,      "run" },
147     { RUNS,     "runs" },
148     { SCATTERS, "scatters" },
149     { SDIFF,    "sdiff" },
150     { SET,      "set" },
151     { SETINFO,  "setinfo" },
152     { SETOBS,   "setobs" },
153     { SETOPT,   "setopt" },
154     { SETMISS,  "setmiss" },
155     { SHELL,    "shell" },
156     { SMPL,     "smpl" },
157     { SPEARMAN, "spearman" },
158     { SPRINTF,  "sprintf" },
159     { SQUARE,   "square" },
160     { SSCANF,   "sscanf" },
161     { STDIZE,   "stdize" },
162     { STORE,    "store" },
163     { SUMMARY,  "summary" },
164     { SYSTEM,   "system" },
165     { TABPRINT, "tabprint" },
166     { TEXTPLOT, "textplot" },
167     { TOBIT,    "tobit" },
168     { IVREG,    "tsls", },
169     { VAR,      "var" },
170     { VARLIST,  "varlist" },
171     { VARTEST,  "vartest" },
172     { VECM,     "vecm" },
173     { VIF,      "vif" },
174     { WLS,      "wls" },
175     { XCORRGM,  "xcorrgm" },
176     { XTAB,     "xtab" },
177     { FUNDEBUG, "debug" },
178     { FUNCRET,  "return" },
179     { CATCH,    "catch" },
180     { PKG,      "pkg" },
181     { NC,       NULL}
182 };
183 
184 static struct gretl_cmd gretl_cmd_aliases[] = {
185     { GENR,    "series" },
186     { GENR,    "scalar" },
187     { GENR,    "matrix" },
188     { GENR,    "string" },
189     { GENR,    "list" },
190     { GENR,    "bundle" },
191     { GENR,    "strings" },
192     { GENR,    "matrices" },
193     { GENR,    "bundles" },
194     { GENR,    "lists" },
195     { GENR,    "arrays" },
196     { ARMA,    "arma" },
197     { COINT2,  "coint2" },
198     { PANSPEC, "hausman" },
199     { NC,   NULL }
200 };
201 
word_is_genr_alias(const char * s)202 int word_is_genr_alias (const char *s)
203 {
204     int i;
205 
206     for (i=0; gretl_cmd_aliases[i].cnum == GENR; i++) {
207 	if (!strcmp(s, gretl_cmd_aliases[i].cword)) {
208 	    return 1;
209 	}
210     }
211 
212     return 0;
213 }
214 
gretl_command_word(int i)215 const char *gretl_command_word (int i)
216 {
217     if (i > 0 && i < NC) {
218 	return gretl_cmds[i].cword;
219     } else {
220 	return "";
221     }
222 }
223 
224 static GHashTable *ht;
225 
gretl_command_hash_init(void)226 static void gretl_command_hash_init (void)
227 {
228     int i;
229 
230     ht = g_hash_table_new(g_str_hash, g_str_equal);
231 
232     for (i=1; gretl_cmds[i].cword != NULL; i++) {
233 	g_hash_table_insert(ht, (gpointer) gretl_cmds[i].cword,
234 			    GINT_TO_POINTER(gretl_cmds[i].cnum));
235     }
236 
237     for (i=0; gretl_cmd_aliases[i].cword != NULL; i++) {
238 	g_hash_table_insert(ht, (gpointer) gretl_cmd_aliases[i].cword,
239 			    GINT_TO_POINTER(gretl_cmd_aliases[i].cnum));
240     }
241 }
242 
gretl_command_number(const char * s)243 int gretl_command_number (const char *s)
244 {
245     gpointer p;
246     int ret = 0;
247 
248     if (ht == NULL) {
249 	gretl_command_hash_init();
250     }
251 
252     p = g_hash_table_lookup(ht, s);
253     if (p != NULL) {
254 	ret = GPOINTER_TO_INT(p);
255     }
256 
257     return ret;
258 }
259 
gretl_command_hash_cleanup(void)260 void gretl_command_hash_cleanup (void)
261 {
262     if (ht != NULL) {
263 	g_hash_table_destroy(ht);
264     }
265 }
266 
gretl_command_complete_next(const char * s,int idx)267 const char *gretl_command_complete_next (const char *s,
268 					 int idx)
269 {
270     size_t n = strlen(s);
271     int i;
272 
273     for (i=idx; i<NC; i++) {
274 	if (!strncmp(s, gretl_cmds[i].cword, n)) {
275 	    return gretl_cmds[i].cword;
276 	}
277     }
278 
279     return NULL;
280 }
281 
gretl_command_complete(const char * s)282 const char *gretl_command_complete (const char *s)
283 {
284     return gretl_command_complete_next(s, 0);
285 }
286