Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

eval.c

Go to the documentation of this file.
00001 /*
00002  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
00003  *      For copying and distribution information, see the file
00004  *      "mit-copyright.h".
00005  *
00006  *      Modified for jwgc by Daniel Henninger.
00007  */
00008 
00009 #include "mit-copyright.h"
00010 
00011 /****************************************************************************/
00012 /* */
00013 /* Code to evaluate an expression:                     */
00014 /* */
00015 /****************************************************************************/
00016 
00017 #include "node.h"
00018 #include "eval.h"
00019 #include "substitute.h"
00020 #include "port.h"
00021 #include "buffer.h"
00022 #include "regexp.h"
00023 #include "text_operations.h"
00024 #include "main.h"
00025 #include "variables.h"
00026 
00027 /****************************************************************************/
00028 /* */
00029 /* Code to deal with string/boolean conversion:              */
00030 /* */
00031 /****************************************************************************/
00032 
00033 /*
00034  *  Internal Routine:
00035  *
00036  *    int string_to_bool(string str)
00037  *       Effects: Returns true iff the string str represents true.
00038  *                True is represented by any string which is equal to
00039  *                "true" when case is disregraded.
00040  */
00041 
00042 #define  string_to_bool(str)      (!strcasecmp(str,"true"))
00043 
00044 /*
00045  *  Internal Routine:
00046  *
00047  *    string bool_to_string(int bool)
00048  *       Effects: Returns a string representive for the C boolean bool.
00049  *                (In C, true == non-zero)  I.e.,
00050  *                string_to_bool(bool_to_string(x)) == !!x.
00051  *                The string returned is on the heap & must be freed
00052  *                eventually.
00053  */
00054 
00055 static string 
00056 bool_to_string(bool)
00057         int bool;
00058 {
00059         return (bool ? string_Copy("TRUE") : string_Copy("FALSE"));
00060 }
00061 
00062 /*
00063  *    int eval_bool_expr(Node *expr)
00064  *        Modifies: dict
00065  *        Requires: expr is a proper expression or NULL.  (see node.c)
00066  *        Effects: Evaluates expr to its boolean value which is returned.
00067  *                 NULL is defined to have the boolean value true.
00068  */
00069 
00070 int 
00071 eval_bool_expr(expr)
00072         Node *expr;
00073 {
00074         string temp;
00075         int result;
00076 
00077         if (!expr)
00078                 return (1);
00079 
00080         temp = eval_expr(expr);
00081         result = string_to_bool(temp);
00082         free(temp);
00083 
00084         return (result);
00085 }
00086 
00087 /****************************************************************************/
00088 /* */
00089 /* Code to evaluate an expression:                     */
00090 /* */
00091 /****************************************************************************/
00092 
00093 /*
00094  *    string eval_expr(Node *expr)
00095  *        Modifies: dict
00096  *        Requires: expr is a proper expression (NOT NULL).  (see node.c)
00097  *        Effects: Evaluates expr to its string value which is returned.
00098  *                 The returned string is on the heap and must be freed
00099  *                 eventually.
00100  */
00101 
00102 string 
00103 eval_expr(expr)
00104         Node *expr;
00105 {
00106         int opcode = expr->opcode;
00107         int bool_result = 0;
00108         string first, second;
00109         char *result = 0;
00110         string *text_ptr;
00111         char *getenv();         /* UNIX get environment variable function */
00112 
00113         dprintf(dEval, "Eval Opcode %d\n", opcode);
00114 
00115         /*
00116          * Dispatch based on the opcode of the top node in the expression:
00117          */
00118         switch (opcode) {
00119                 case STRING_CONSTANT_OPCODE:
00120                         return (string_Copy(expr->d.string_constant));
00121 
00122                 case VARREF_OPCODE:
00123                         return (string_Copy(var_get_variable(expr->d.string_constant)));
00124 
00125                 case BUFFER_OPCODE:
00126                         return (string_Copy(buffer_to_string()));
00127 
00128                         /*
00129                          * Handle unary expressions:
00130                          */
00131                 case NOT_OPCODE:
00132                 case SUBSTITUTE_OPCODE:
00133                 case PROTECT_OPCODE:
00134                 case VERBATIM_OPCODE:
00135                 case GETENV_OPCODE:
00136                 case UPCASE_OPCODE:
00137                 case DOWNCASE_OPCODE:
00138                 case JVAR_OPCODE:
00139                 case GET_OPCODE:
00140                         first = eval_expr(expr->d.nodes.first);
00141 
00142                         switch (opcode) {
00143                                 case NOT_OPCODE:
00144                                         result = bool_to_string(!string_to_bool(first));
00145                                         break;
00146 
00147                                 case SUBSTITUTE_OPCODE:
00148                                         result = substitute(var_get_variable, first);
00149                                         break;
00150 
00151                                 case PROTECT_OPCODE:
00152                                         result = protect(first);
00153                                         break;
00154 
00155                                 case VERBATIM_OPCODE:
00156                                         return (verbatim(first, 0));
00157 
00158                                 case GETENV_OPCODE:
00159                                         result = getenv(first);
00160                                         if (!result)
00161                                                 result = string_Copy("");
00162                                         else
00163                                                 result = string_Copy(result);
00164                                         break;
00165 
00166                                 case UPCASE_OPCODE:
00167                                         return (string_Upcase(first));
00168 
00169                                 case DOWNCASE_OPCODE:
00170                                         return (string_Downcase(first));
00171 
00172                                 case JVAR_OPCODE:
00173                                         result = jVars_get(jVars_stoi(first));
00174                                         if (!result)
00175                                                 result = string_Copy("");
00176                                         else
00177                                                 result = string_Copy(result);
00178                                         break;
00179 
00180                                 case GET_OPCODE:
00181                                         result = read_from_port(first);
00182                                         break;
00183                         }
00184                         free(first);
00185                         break;
00186 
00187                         /*
00188                          * Handle binary operators:
00189                          */
00190                 case PLUS_OPCODE:
00191                 case AND_OPCODE:
00192                 case OR_OPCODE:
00193                 case EQ_OPCODE:
00194                 case NEQ_OPCODE:
00195                 case REGEQ_OPCODE:
00196                 case REGNEQ_OPCODE:
00197                         first = eval_expr(expr->d.nodes.first);
00198                         second = eval_expr(expr->d.nodes.second);
00199 
00200                         switch (opcode) {
00201                                 case PLUS_OPCODE:
00202                                         result = string_Concat(first, second);
00203                                         free(first);
00204                                         free(second);
00205                                         return (result);
00206 
00207                                 case AND_OPCODE:
00208                                         bool_result = string_to_bool(first) && string_to_bool(second);
00209                                         break;
00210 
00211                                 case OR_OPCODE:
00212                                         bool_result = string_to_bool(first) || string_to_bool(second);
00213                                         break;
00214 
00215                                 case EQ_OPCODE:
00216                                         bool_result = string_Eq(first, second);
00217                                         break;
00218 
00219                                 case NEQ_OPCODE:
00220                                         bool_result = string_Neq(first, second);
00221                                         break;
00222 
00223                                 case REGEQ_OPCODE:
00224                                         bool_result = ed_regexp_match_p(first, second);
00225                                         break;
00226 
00227                                 case REGNEQ_OPCODE:
00228                                         bool_result = !ed_regexp_match_p(first, second);
00229                                         break;
00230                         }
00231                         free(first);
00232                         free(second);
00233                         result = bool_to_string(bool_result);
00234                         break;
00235 
00236                         /*
00237                          * Handle text-manipulation operators:
00238                          */
00239                 case LANY_OPCODE:
00240                 case RANY_OPCODE:
00241                 case LBREAK_OPCODE:
00242                 case RBREAK_OPCODE:
00243                 case LSPAN_OPCODE:
00244                 case RSPAN_OPCODE:
00245                 case PARAGRAPH_OPCODE:
00246                         first = eval_expr(expr->d.nodes.first);
00247                         second = eval_expr(expr->d.nodes.second);
00248                         text_ptr = &first;
00249 
00250                         switch (opcode) {
00251                                 case LANY_OPCODE:
00252                                         result = lany(text_ptr, second);
00253                                         break;
00254 
00255                                 case RANY_OPCODE:
00256                                         result = rany(text_ptr, second);
00257                                         break;
00258 
00259                                 case LBREAK_OPCODE:
00260                                         result = lbreak(text_ptr, string_to_character_class(second));
00261                                         break;
00262 
00263                                 case RBREAK_OPCODE:
00264                                         result = rbreak(text_ptr, string_to_character_class(second));
00265                                         break;
00266 
00267                                 case LSPAN_OPCODE:
00268                                         result = lspan(text_ptr, string_to_character_class(second));
00269                                         break;
00270 
00271                                 case RSPAN_OPCODE:
00272                                         result = rspan(text_ptr, string_to_character_class(second));
00273                                         break;
00274 
00275                                 case PARAGRAPH_OPCODE:
00276                                         result = paragraph(text_ptr, atoi(second));
00277                                         break;
00278                         }
00279 
00280                         if (expr->d.nodes.first->opcode == VARREF_OPCODE)
00281                                 var_set_variable(expr->d.nodes.first->d.string_constant, first);
00282                         free(first);
00283                         free(second);
00284                         break;
00285         }
00286 
00287         return (result);
00288 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye