1 /* Copyright (C) 2005-2008 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _SCRIPTING_H
29 #define _SCRIPTING_H
30 
31 #include "fontforgevw.h"
32 #include <setjmp.h>
33 #include <stdarg.h>
34 
35 /* If users want to write user defined scripting built-in functions they will */
36 /*  need this file. The most relevant structure is the Context */
37 
38 struct dictentry {
39     char *name;
40     Val val;
41 };
42 
43 struct dictionary {
44     struct dictentry *entries;
45     int cnt, max;
46 };
47 
48 typedef struct array {
49     int argc;
50     Val *vals;
51 } Array;
52 
53 #define TOK_MAX	256
54 enum token_type { tt_name, tt_string, tt_number, tt_unicode, tt_real,
55 	tt_lparen, tt_rparen, tt_comma, tt_eos,		/* eos is end of statement, semicolon, newline */
56 	tt_lbracket, tt_rbracket,
57 	tt_minus, tt_plus, tt_not, tt_bitnot, tt_colon,
58 	tt_mul, tt_div, tt_mod, tt_and, tt_or, tt_bitand, tt_bitor, tt_xor,
59 	tt_eq, tt_ne, tt_gt, tt_lt, tt_ge, tt_le,
60 	tt_assign, tt_pluseq, tt_minuseq, tt_muleq, tt_diveq, tt_modeq,
61 	tt_incr, tt_decr,
62 
63 	tt_if, tt_else, tt_elseif, tt_endif, tt_while, tt_foreach, tt_endloop,
64 	tt_shift, tt_return, tt_break,
65 
66 	tt_eof,
67 
68 	tt_error = -1
69 };
70 
71 typedef struct context {
72     struct context *caller;		/* The context of the script that called us */
73     Array a;				/* The argument array */
74     Array **dontfree;			/* Irrelevant for user defined funcs */
75     struct dictionary locals;		/* Irrelevant for user defined funcs */
76     FILE *script;			/* Irrelevant for user defined funcs */
77     unsigned int backedup: 1;		/* Irrelevant for user defined funcs */
78     unsigned int donteval: 1;		/* Irrelevant for user defined funcs */
79     unsigned int returned: 1;		/* Irrelevant for user defined funcs */
80     unsigned int broken: 1;		/* Irrelevant for user defined funcs */
81     char tok_text[TOK_MAX+1];		/* Irrelevant for user defined funcs */
82     enum token_type tok;		/* Irrelevant for user defined funcs */
83     Val tok_val;			/* Irrelevant for user defined funcs */
84     Val return_val;			/* Initialized to void. If user wants */
85     					/*  return something set the return */
86 			                /*  value here */
87     Val trace;				/* Irrelevant for user defined funcs */
88     Val argsval;			/* Irrelevant for user defined funcs */
89     char *filename;			/* Irrelevant for user defined funcs */
90     int lineno;				/* Irrelevant for user defined funcs */
91     int ungotch;			/* Irrelevant for user defined funcs */
92     FontViewBase *curfv;		/* Current fontview */
93     jmp_buf *err_env;			/* place to longjump to on an error */
94 } Context;
95 
96 void arrayfree(Array *);
97 
98 void FontImage(SplineFont *sf,char *filename,Array *arr,int width,int height);
99 
100  /* Adds a user defined scripting function to the interpretter */
101  /* (you can't override a built-in name) */
102  /* (you can replace a previous user defined function */
103  /* Most functions will require a font to be loaded, but a few do not */
104  /*  Open(), Exit(), Sin() don't.  ff uses the needs_font flag to perform */
105  /*  this check for you */
106  /* Returns 1 if the addition was successful, 2 if it replaced a previous func */
107  /* Returns 0 on failure (ie. if it attempts to replace a builtin function) */
108 typedef void (*UserDefScriptFunc)(Context *);
109 extern int AddScriptingCommand(char *name,UserDefScriptFunc func,int needs_font);
110 
111  /* Returns whether a user defined scripting command already exists with the */
112  /*  given name */
113 extern UserDefScriptFunc HasUserScriptingCommand(char *name);
114 
115  /* Scripts used to be in latin1, and we still support that if the user sets */
116  /*  an environment variable. Now scripts are by default utf8. These two funcs */
117  /*  will interconvert between latin1 & utf8 if appropriate, or just make a */
118  /*  utf8 copy if not. They always make a copy. */
119 extern char *utf82script_copy(const char *ustr);
120 extern char *script2utf8_copy(const char *str);
121 
122  /* Various error routines. */
123 void ScriptError( Context *c, const char *msg );
124 	/* Prints an error message and exits. msg is in the script's encoding */
125 void ScriptErrorString( Context *c, const char *msg, const char *name);
126 	/* Prints an error message followed by a string and exits. */
127 	/*  both strings are in the script's encoding */
128 void ScriptErrorF( Context *c, const char *fmt, ... );
129 	/* Standard printf-style spec. All string arguments assumed to be in */
130 	/* utf8 */
131 
132 extern int running_script;
133 
134 /* Hooks so a scripting dlg can execute fontforge's legacy scripting language */
135 extern void ff_VerboseCheck(void);
136 extern enum token_type ff_NextToken(Context *c);
137 extern void ff_backuptok(Context *c);
138 extern void ff_statement(Context*);
139 
140 #endif	/* _SCRIPTING_H */
141