Back to index

im-sdk  12.3.91
XimpParser.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 /******************************************************************
00043 
00044               Copyright 1992 by Oki Technosystems Laboratory, Inc.
00045               Copyright 1992 by Fuji Xerox Co., Ltd.
00046 
00047 Permission to use, copy, modify, distribute, and sell this software
00048 and its documentation for any purpose is hereby granted without fee,
00049 provided that the above copyright notice appear in all copies and
00050 that both that copyright notice and this permission notice appear
00051 in supporting documentation, and that the name of Oki Technosystems
00052 Laboratory and Fuji Xerox not be used in advertising or publicity
00053 pertaining to distribution of the software without specific, written
00054 prior permission.
00055 Oki Technosystems Laboratory and Fuji Xerox make no representations
00056 about the suitability of this software for any purpose.  It is provided
00057 "as is" without express or implied warranty.
00058 
00059 OKI TECHNOSYSTEMS LABORATORY AND FUJI XEROX DISCLAIM ALL WARRANTIES
00060 WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
00061 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL OKI TECHNOSYSTEMS
00062 LABORATORY AND FUJI XEROX BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00063 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00064 OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00065 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
00066 OR PERFORMANCE OF THIS SOFTWARE.
00067 
00068   Author: Yasuhiro Kawai    Oki Technosystems Laboratory
00069   Author: Kazunori Nishihara       Fuji Xerox
00070 
00071 ******************************************************************/
00072 
00073 
00074 #ifndef lint
00075 #ifdef sccs
00076 static char     sccsid[] = "@(#)XimpParser.c     2.8 95/09/12 SMI";
00077 #endif
00078 #endif
00079 
00080 #include <stdio.h>
00081 #ifndef __XLIBINT_HH
00082 #define __XLIBINT_HH
00083 #include <X11/Xlibint.h>
00084 #endif /* __XLIBINT_HH */
00085 #include <X11/Xos.h>
00086 #include <X11/Sunkeysym.h>
00087 #include "composeIM.h"
00088 
00089 #define RmTop           (1 << 0)
00090 #define RmBottom        (1 << 1)
00091 #define RmLeft          (1 << 2)
00092 #define RmRight         (1 << 3)
00093 #define RmOverTheSpot              (1 << 5)
00094 #define RmCenterOfScreen    (1 << 6)
00095 
00096 typedef enum {
00097     RmScreen         = 107,
00098     RmClient         = 108,
00099     RmCursor         = 109,
00100     RmSpotLocation      = 110 
00101 } RmWindowPosition;
00102 
00103 /*
00104  *     Parsing File Format:
00105  *
00106  *     FILE          ::= { [PRODUCTION] [COMMENT] "\n"}
00107  *     PRODUCTION    ::= LHS ":" RHS [ COMMENT ]
00108  *     COMMENT       ::= "#" {<any character except null or newline>}
00109  *     LHS           ::= EVENT { EVENT }
00110  *     EVENT         ::= [MODIFIER_LIST] "<" keysym ">" [KEYCODE]
00111  *     KEYCODE             ::= "[" keycode "]"
00112  *     MODIFIER_LIST ::= ("!" {MODIFIER} ) | "None"
00113  *     MODIFIER      ::= ["~"] modifier_name
00114  *     RHS           ::= ( STRING | keysym | STRING keysym )
00115  *     STRING        ::= '"' { CHAR } '"'
00116  *     CHAR          ::= GRAPHIC_CHAR | ESCAPED_CHAR
00117  *     GRAPHIC_CHAR  ::= locale (codeset) dependent code
00118  *     ESCAPED_CHAR  ::= ('\\' | '\"' | OCTAL | HEX )
00119  *     OCTAL         ::= '\' OCTAL_CHAR [OCTAL_CHAR [OCTAL_CHAR]]
00120  *     OCTAL_CHAR    ::= (0|1|2|3|4|5|6|7)
00121  *     HEX           ::= '\' (x|X) HEX_CHAR [HEX_CHAR]]
00122  *     HEX_CHAR      ::= (0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f)
00123  *
00124  */
00125 
00126 #define ENDOFFILE 0
00127 #define ENDOFLINE 1
00128 #define COLON 2
00129 #define LESS 3
00130 #define GREATER 4
00131 #define EXCLAM 5
00132 #define TILDE 6
00133 #define STRING 7
00134 #define KEY 8
00135 #define ERROR 9
00136 #define RIGHT_BRACKET 10
00137 #define LEFT_BRACKET 11
00138 #define ATTRIBUTES 12
00139 
00140 #define T_STATE                    101
00141 #define T_END_STATE         102
00142 #define T_SWITCH_STATE_TO   103
00143 #define T_ATTRIBUTES        104
00144 #define T_END_ATTRIBUTES    105
00145 #define T_ACTIONS           106
00146 #define T_END_ACTIONS              107
00147 #define T_DO_CONV           108
00148 #define T_NEXT_PAGE         109
00149 #define T_PREV_PAGE         110
00150 #define T_TOP_PAGE          111
00151 #define T_LAST_PAGE         112
00152 #define T_STATE_TYPE        113
00153 #define T_COMPOSE_STATE            114
00154 #define T_CODE_INPUT_STATE  115
00155 #define T_LOOKUP_STATE             116
00156 #define T_TABLELOOKUP              117
00157 #define T_REMOTE_IM_STATE       151     /* Added for remote IM */
00158 #define T_LANGUAGE          152    
00159 #ifdef USE_CAND_SELECTION
00160 #define T_NEXT_CAND         118
00161 #define T_PREV_CAND         119
00162 #define T_UP_CAND           120
00163 #define T_DOWN_CAND         121
00164 #define T_SELECT_CAND              122
00165 #endif
00166 
00167 #define ATTRIBUTE_TOKEN            200
00168 #define A_MAX_INPUT_LENGTH  201
00169 #define A_TYPE_OF_INPUT            202
00170 #define A_MAX_NUMBER_OF_ROWS       203
00171 #define A_MAX_NUMBER_OF_COLUMNS    204
00172 #define A_MAX_NUMBER_OF_CANDIDATES 205
00173 #define A_TITLE_STRING             206
00174 #define A_CODE_RANGE        207
00175 #define A_FONTSET           208
00176 #define A_POSITION          209
00177 #define A_PLACEMENT         210
00178 
00179 #define EMPTY_GLYPH_CHAR 0x3f
00180 
00181 typedef struct {
00182     char      *name;
00183     int              val;
00184 } StateKeyWordRec;
00185 
00186 static StateKeyWordRec keyword_tbl[] = {
00187     {"STATE", T_STATE},
00188     {"END_STATE", T_END_STATE},
00189     {"STATE_TYPE", T_STATE_TYPE},
00190     {"SWITCH_STATE_TO", T_SWITCH_STATE_TO},
00191     {"#attributes", T_ATTRIBUTES},
00192     {"#end of attributes", T_END_ATTRIBUTES},
00193     {"#actions", T_ACTIONS},
00194     {"#end of actions", T_END_ACTIONS},
00195     {"DO_CONV", T_DO_CONV},
00196     {"NEXT_PAGE", T_NEXT_PAGE},
00197     {"PREV_PAGE", T_PREV_PAGE},
00198     {"TOP_PAGE", T_TOP_PAGE},
00199     {"LAST_PAGE", T_LAST_PAGE},
00200 #ifdef USE_CAND_SELECTION
00201     {"NEXT_CAND", T_NEXT_CAND},
00202     {"PREV_CAND", T_PREV_CAND},
00203     {"UP_CAND", T_UP_CAND},
00204     {"DOWN_CAND", T_DOWN_CAND},
00205     {"SELECT_CAND", T_SELECT_CAND},
00206 #endif
00207     {"COMPOSE", T_COMPOSE_STATE},
00208     {"LOOKUP", T_LOOKUP_STATE},
00209     {"DO_LOOKUP", T_TABLELOOKUP},
00210     {"CODE_INPUT", T_CODE_INPUT_STATE},
00211     {"REMOTE_IM", T_REMOTE_IM_STATE},
00212     {"LANGUAGE", T_LANGUAGE},
00213     {NULL, 0}
00214 };
00215 
00216 typedef enum {
00217     StringAttr,
00218     IntegerAttr,
00219     LiteralAttr
00220 } AttribType;
00221 
00222 typedef struct {
00223     char      *name;
00224     int              val;
00225     AttribType   type;
00226 } AttribKeyWordRec;
00227 
00228 static AttribKeyWordRec attr_tbl[] = {
00229     {"MAX_INPUT_LENGTH", A_MAX_INPUT_LENGTH, IntegerAttr},
00230     {"TYPE_OF_INPUT", A_TYPE_OF_INPUT, LiteralAttr},
00231     {"MAX_NUMBER_OF_ROWS", A_MAX_NUMBER_OF_ROWS, IntegerAttr},
00232     {"MAX_NUMBER_OF_COLUMNS", A_MAX_NUMBER_OF_COLUMNS, IntegerAttr},
00233     {"MAX_NUMBER_OF_CANDIDATES", A_MAX_NUMBER_OF_CANDIDATES},
00234     {"TITLE_STRING", A_TITLE_STRING, StringAttr},
00235     {"FONTSET", A_FONTSET, StringAttr},
00236     {"POSITION", A_POSITION, LiteralAttr},
00237     {"PLACEMENT", A_PLACEMENT, LiteralAttr},
00238     {NULL, 0, 0}
00239 };
00240 
00241 #define MAXSTRLEN 100
00242 
00243 static int lastch = 0;
00244 
00245 static int
00246 nextch(fp)
00247     FILE *fp;
00248 {
00249     int c;
00250 
00251     if (lastch != 0) {
00252        c = lastch;
00253        lastch = 0;
00254     } else {
00255        c = getc(fp);
00256        if (c == '\\') {
00257            c = getc(fp);
00258            if (c == '\n') {
00259               c = getc(fp);
00260            } else {
00261               ungetc(c, fp);
00262               c = '\\';
00263            }
00264        }
00265     }
00266     return(c);
00267 }
00268 
00269 static void
00270 putbackch(c)
00271     int c;
00272 {
00273     lastch = c;
00274 }
00275 
00276 #ifndef isalnum
00277 #define isalnum(c)      \
00278     (('0' <= (c) && (c) <= '9')  || \
00279      ('A' <= (c) && (c) <= 'Z')  || \
00280      ('a' <= (c) && (c) <= 'z'))
00281 #endif
00282 
00283 static char tokenbuf[MAXSTRLEN];
00284 
00285 static int
00286 check_quoted_string(fp)
00287     FILE      *fp;
00288 {
00289     int i, j;
00290     char      *p;
00291     int              c;
00292     int              token;
00293 
00294     p = tokenbuf;
00295     while ((c = nextch(fp)) != '"') {
00296         if (c == '\n' || c == EOF) {
00297            putbackch(c);
00298            token = ERROR;
00299            goto string_error;
00300         } else if (c == '\\') {
00301            c = nextch(fp);
00302            switch (c) {
00303              case '\\':
00304              case '"':
00305                *p++ = c;
00306                break;
00307              case '0':
00308              case '1':
00309              case '2':
00310              case '3':
00311              case '4':
00312              case '5':
00313              case '6':
00314              case '7':
00315                i = c - '0';
00316                c = nextch(fp);
00317                for (j = 0; j < 2 && c >= '0' && c <= '7'; j++) {
00318                      i <<= 3;
00319                      i += c - '0';
00320                      c = nextch(fp);
00321                }
00322                putbackch(c);
00323                *p++ = (char)i;
00324                break;
00325              case 'X':
00326              case 'x':
00327                i = 0;
00328                c = nextch(fp);
00329 #define ishexch(c) (((c) >= '0' && (c) <= '9') || \
00330                   ((c) >= 'A' && (c) <= 'F') || \
00331                   ((c) >= 'a' && (c) <= 'f'))
00332                for (j = 0; j < 2 && ishexch(c); j++) {
00333                      i <<= 4;
00334                      if (c >= '0' && c <= '9') {
00335                          i += c - '0';
00336                      } else if (c >= 'A' && c <= 'F') {
00337                          i += c - 'A' + 10;
00338                      } else {
00339                          i += c - 'a' + 10;
00340                      }
00341                      c = nextch(fp);
00342                }
00343                if (j == 0) {
00344                       token = ERROR;
00345                       goto string_error;
00346                }
00347                putbackch(c);
00348                *p++ = (char)i;
00349 #undef ishexch
00350                break;
00351              case '\n':
00352              case EOF:
00353                putbackch(c);
00354                token = ERROR;
00355                goto string_error;
00356                /* break; */
00357              default:
00358                *p++ = c;
00359                break;
00360            }
00361        } else {
00362               *p++ = c;
00363        }
00364     }
00365 
00366     *p = '\0';
00367     token = STRING;
00368 
00369 string_error:
00370     return (token);
00371 }
00372 
00373 static int
00374 get_statetoken(w)
00375     char             *w;
00376 {
00377     StateKeyWordRec  *k = keyword_tbl;
00378 
00379     if(w == 0 || *w == 0)
00380        return (-1);
00381 
00382     for ( ; k->name; k++) {
00383        if(!strcmp(k->name, w)) {
00384               return (k->val);
00385        }      
00386     }
00387 
00388     return (-1);
00389 }
00390 
00391 static int
00392 get_attributetoken(w)
00393     char             *w;
00394 {
00395     AttribKeyWordRec *k = attr_tbl;
00396 
00397     if(w == 0 || *w == 0)
00398        return (-1);
00399 
00400     for ( ; k->name; k++) {
00401        if(!strcmp(k->name, w)) {
00402               return (k->val);
00403        }      
00404     }
00405 
00406     return (-1);
00407 }
00408 
00409 static int
00410 nexttoken(fp)
00411     FILE *fp;
00412 {
00413     int c;
00414     int token, state_token, attr_token;
00415     char *p;
00416 
00417     while ((c = nextch(fp)) == ' ' || c == '\t') {
00418     }
00419     switch (c) {
00420       case EOF:
00421        token = ENDOFFILE;
00422        break;
00423       case '\n':
00424        token = ENDOFLINE;
00425        break;
00426       case '<':
00427        token = LESS;
00428        break;
00429       case '[':
00430        token = LEFT_BRACKET;
00431        break;
00432       case ']':
00433        token = RIGHT_BRACKET;
00434        break;
00435       case '>':
00436        token = GREATER;
00437        break;
00438       case ':':
00439        token = COLON;
00440        break;
00441       case '!':
00442        token = EXCLAM;
00443        break;
00444       case '~':
00445        token = TILDE;
00446        break;
00447       case '"':
00448        token = check_quoted_string(fp);
00449        break;
00450       case '#':
00451        p = tokenbuf;
00452        *p++ = c;
00453        c = nextch(fp);
00454        while (isalnum(c) || c == ' ') {
00455            *p++ = c;
00456            c = nextch(fp);
00457        }
00458        *p = '\0';
00459        putbackch(c);
00460        if((state_token = get_statetoken(tokenbuf)) > 0) {
00461            return (state_token);
00462 
00463        } else {
00464            while ((c = nextch(fp)) != '\n' && c != EOF) {
00465            }
00466            if (c == '\n') {
00467               token = ENDOFLINE;
00468            } else {
00469               token = ENDOFFILE;
00470            }
00471         }
00472        break;
00473       default:
00474        if (isalnum(c) || c == '_' || c == '-') {
00475            p = tokenbuf;
00476            *p++ = c;
00477            c = nextch(fp);
00478            while (isalnum(c) || c == '_' || c == '-') {
00479               *p++ = c;
00480               c = nextch(fp);
00481            }
00482            *p = '\0';
00483            putbackch(c);
00484            token = KEY;
00485            if((state_token = get_statetoken(tokenbuf)) > 0)
00486               return (state_token);
00487             if((attr_token = get_attributetoken(tokenbuf)) > 0)
00488                 return (attr_token);
00489        } else {
00490            token = ERROR;
00491        }
00492        break;
00493     }
00494     return(token);
00495 }
00496 
00497 static long
00498 modmask(name)
00499     char *name;
00500 {
00501     long mask;
00502 
00503     struct _modtbl {
00504        char *name;
00505        long mask;
00506     };
00507     struct _modtbl *p;
00508 
00509     static struct _modtbl tbl[] = {
00510        { "Ctrl",     ControlMask   },
00511         { "Lock",    LockMask      },
00512         { "Caps",    LockMask      },
00513         { "Shift",   ShiftMask     },
00514         { "Alt",     Mod1Mask      },
00515         { "Meta",    Mod1Mask      },
00516         { "AltGraph",       Mod2Mask      },
00517         { NULL,             0             }};
00518 
00519     p = tbl;
00520     mask = 0;
00521     for (p = tbl; p->name != NULL; p++) {
00522        if (strcmp(name, p->name) == 0) {
00523            mask = p->mask;
00524            break;
00525        }
00526     }
00527     return(mask);
00528 }
00529 
00530 #define AllMask (ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask) 
00531 #define LOCAL_WC_BUFSIZE 128
00532 #define SEQUENCE_MAX 10
00533 
00534 static LocalIMState  *
00535 create_state(xim, name)
00536 Ximp_XIM xim;
00537 char   *name;
00538 {
00539     XimCommon im = (XimCommon)xim;
00540     LocalIMState  *tmps = im->local_impart->top_state;
00541     LocalIMState  *lasts = 0;
00542 
00543     while(tmps) {
00544        if(!strcmp(tmps->name, tokenbuf)) {
00545               return 0;
00546        }
00547        lasts = tmps;
00548        tmps = tmps->next;
00549     }
00550 
00551     tmps = (LocalIMState*)Xmalloc(sizeof(LocalIMState));
00552     memset(tmps, 0, sizeof(LocalIMState));
00553     tmps->name = (char*)Xmalloc(strlen(name) +1);
00554     tmps->type = ComposeState;
00555     strcpy(tmps->name, name);
00556 
00557     if(!lasts)
00558        im->local_impart->top_state = tmps;
00559     else
00560        lasts->next = tmps;
00561               
00562     return (tmps);
00563 }
00564 
00565 static StateAttrib *
00566 new_attribute()
00567 {
00568        return (StateAttrib *)Xmalloc(sizeof(StateAttrib));
00569 }
00570 
00571 static int
00572 parseline(fp, im, state)
00573 FILE *fp;
00574 XimCommon     im;
00575 LocalIMState  **state;
00576 {
00577     int token, prev_token;
00578     unsigned modifier_mask;
00579     unsigned modifier;
00580     unsigned tmp;
00581     KeySym keysym;
00582     DefTree *p = NULL;
00583     Bool exclam, tilde;
00584     KeySym rhs_keysym =  (KeySym)NoSymbol;
00585     char *rhs_string_mb = NULL;
00586     DefTree   **top;
00587     char *rhs_target_name = NULL, *ret;
00588     char *range_name = NULL;
00589 
00590     struct DefBuffer {
00591        unsigned modifier_mask;
00592        unsigned modifier;
00593        KeySym keysym;
00594        int keycode;
00595     };
00596 
00597     struct DefBuffer buf[SEQUENCE_MAX];
00598     int i, n;
00599 
00600     ActionType act = NoAction;
00601 
00602     do {
00603        token = nexttoken(fp);
00604     } while (token == ENDOFLINE);
00605     
00606     if (token == ENDOFFILE) {
00607        return(-1);
00608     }
00609 
00610     switch (token) {
00611        case T_STATE: {      /* begin state */
00612               if(*state)
00613                      goto error;
00614 
00615               token = nexttoken(fp);
00616               if(token != STRING)
00617                      goto error;
00618 
00619               *state = create_state((Ximp_XIM)im, tokenbuf);
00620               if(!(*state))
00621                      goto error;
00622               return (0);
00623                /* break; */
00624        }
00625        case T_END_STATE:
00626               if(!(*state))
00627                      goto error;
00628               *state = 0;
00629               return(0);
00630                /* break; */
00631        case T_ATTRIBUTES:
00632               if(!(*state) || (*state)->attrflag) 
00633                      goto error;
00634               (*state)->attr = new_attribute();
00635               if ( (*state)->attr == NULL ) goto error;
00636               memset((*state)->attr, 0, sizeof(StateAttrib));
00637               (*state)->attrflag = True;
00638                 return(0);
00639                /* break; */
00640        case T_END_ATTRIBUTES:
00641               if(!(*state) || !(*state)->attrflag) 
00642                      goto error;
00643               (*state)->attrflag = False;
00644                 return(0);
00645                /* break; */
00646        case T_ACTIONS:
00647               if(!(*state) || (*state)->actionflag) 
00648                      goto error;
00649               (*state)->actionflag = True;
00650                 return(0);
00651                /* break; */
00652        case T_END_ACTIONS:
00653               if(!(*state) || !(*state)->actionflag) 
00654                      goto error;
00655               (*state)->actionflag = False;
00656                 return(0);
00657                /* break; */
00658        case T_STATE_TYPE:
00659               if(!(*state)) 
00660                      goto error;
00661 
00662               token = nexttoken(fp);
00663               if(token != COLON)
00664                  goto error;
00665 
00666               token = nexttoken(fp);
00667               if (token == T_LOOKUP_STATE) {
00668                      (*state)->type = LookupState;
00669               } else if (token == T_CODE_INPUT_STATE) {
00670                      (*state)->type = CodeInputState;
00671               } else if (token == T_REMOTE_IM_STATE) {
00672                      (*state)->type = RemoteIMState;
00673               }
00674                 return(0);
00675                /* break; */
00676        case T_LANGUAGE:
00677               if(!(*state)) 
00678                      goto error;
00679 
00680               token = nexttoken(fp);
00681               if(token != COLON)
00682                  goto error;
00683 
00684               token = nexttoken(fp);
00685               if (( (*state)->language = (char *)Xmalloc(strlen(tokenbuf)+1)) == NULL)
00686                  goto error;
00687               memset((*state)->language, 0, strlen(tokenbuf)+1);
00688               strcpy((*state)->language, tokenbuf);
00689               return(0);
00690            /* break; */
00691        default:
00692               break;
00693     }
00694 
00695     n = 0;
00696     prev_token = 0;
00697     do {
00698        if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
00699            modifier = 0;
00700            modifier_mask = AllMask;
00701            token = nexttoken(fp);
00702        } else if (token > ATTRIBUTE_TOKEN) {
00703             if ( ! (*state)->attrflag ) goto error;
00704            prev_token = token;
00705            token = nexttoken(fp);
00706            continue;
00707         } else if ( token == STRING ){
00708             if ( ! (*state)->attrflag ) goto error;
00709 
00710            if ((range_name = (char*)Xmalloc(strlen(tokenbuf) + 1)) == NULL )
00711               goto error;
00712            memset(range_name, 0, strlen(tokenbuf)+1);
00713            strcpy(range_name, tokenbuf);
00714            prev_token = A_CODE_RANGE;
00715            token = nexttoken(fp);
00716            continue;
00717        } else {
00718            modifier = 0;
00719            modifier_mask = AllMask;
00720            exclam = False;
00721            if (token == EXCLAM) {
00722               exclam = True;
00723               token = nexttoken(fp);
00724            }
00725            while (token == TILDE || token == KEY) {
00726               tilde = False;
00727               if (token == TILDE) {
00728                   token = nexttoken(fp);
00729                   tilde = True;
00730                   if (token != KEY)
00731                      goto error;
00732               }
00733               token = nexttoken(fp);
00734               tmp = modmask(tokenbuf);
00735               if (!tmp) {
00736                   goto error;
00737               }
00738               modifier_mask |= tmp;
00739               if (tilde) {
00740                   modifier &= ~tmp;
00741               } else {
00742                   modifier |= tmp;
00743               }
00744            }
00745            if (exclam) {
00746               modifier_mask = AllMask;
00747            }
00748        }
00749 
00750        if (token != LESS && token != LEFT_BRACKET) {
00751            goto error;
00752        }
00753        prev_token = token;
00754 
00755        token = nexttoken(fp);
00756        if (token != KEY) {
00757            goto error;
00758        }
00759 
00760        token = nexttoken(fp);
00761        if (!(prev_token == LESS && token == GREATER)
00762            && !(prev_token == LEFT_BRACKET && token == RIGHT_BRACKET)) {
00763            goto error;
00764        }
00765 
00766        if (prev_token == LESS) {
00767            keysym = XStringToKeysym(tokenbuf);
00768            if (keysym == NoSymbol) {
00769               goto error;
00770            }
00771            buf[n].keysym = keysym;
00772            buf[n].keycode = 0;     /* default */
00773            buf[n].modifier = modifier;
00774            buf[n].modifier_mask = modifier_mask;
00775            n++;
00776            if( n >= SEQUENCE_MAX )
00777               goto error;
00778        } else /* PREV_TOKEN == LEFT_BRACKET */ {
00779            int keycode = atoi(tokenbuf);
00780            if (n < 1) goto error; /* hopefully never happened */
00781            buf[n - 1].keycode = keycode;
00782            SET_KEYCODEINFO(*state);
00783        }
00784        token = nexttoken(fp);
00785     } while (token != COLON);
00786 
00787     token = nexttoken(fp);
00788     if ( prev_token > ATTRIBUTE_TOKEN ) {
00789        switch ((*state)->type) {
00790        case CodeInputState:
00791          if ( (*state)->attr == NULL ) goto error;
00792          switch (prev_token) {
00793          case A_MAX_INPUT_LENGTH:
00794            (*state)->attr->_attr_u.CIstruct.max_input_len = atoi(tokenbuf);
00795            return(0);
00796            /* break; */
00797          case A_TYPE_OF_INPUT:
00798            if ( !strcmp(tokenbuf, "HEX") ) {
00799              (*state)->attr->_attr_u.CIstruct.type_of_input = HEX;
00800            } else if ( !strcmp(tokenbuf, "OCTET") ) {
00801              (*state)->attr->_attr_u.CIstruct.type_of_input = OCTET;
00802            } else goto error;
00803            return(0);
00804            /* break; */
00805          default:
00806            goto error;
00807            /* break; */
00808          }
00809          /* break; */
00810        case LookupState:
00811          if ( (*state)->attr == NULL ) goto error;
00812          switch ( prev_token ) {
00813          case A_MAX_NUMBER_OF_ROWS:
00814            (*state)->attr->_attr_u.LUstruct.num_of_rows = atoi(tokenbuf);
00815            return(0);
00816            /* break; */
00817          case A_MAX_NUMBER_OF_COLUMNS:
00818            (*state)->attr->_attr_u.LUstruct.num_of_cols = atoi(tokenbuf);
00819            return(0);
00820            /* break; */
00821          case A_MAX_NUMBER_OF_CANDIDATES:
00822            (*state)->attr->_attr_u.LUstruct.num_of_cands = atoi(tokenbuf);
00823            return(0);
00824            /* break; */
00825          case A_CODE_RANGE:
00826            {
00827              LuRange *rangep = (*state)->attr->_attr_u.LUstruct.range;
00828              int nrange = (*state)->attr->_attr_u.LUstruct.nrange;
00829 
00830              if (rangep == NULL )
00831               rangep = (LuRange *)Xmalloc(sizeof(LuRange));
00832              else
00833               rangep = (LuRange *)Xrealloc(rangep,
00834                                         sizeof(LuRange)* (nrange + 1));
00835              rangep[nrange].start_range = strtol(tokenbuf, &ret, 0);
00836              token = nexttoken(fp);
00837              rangep[nrange].end_range = strtol(tokenbuf, &ret, 0);
00838              memset(rangep[nrange].RangeName, 0, sizeof(char) * 80);
00839              strcpy(rangep[nrange].RangeName, range_name);
00840              (*state)->attr->_attr_u.LUstruct.nrange++;
00841              (*state)->attr->_attr_u.LUstruct.range = rangep;
00842              Xfree(range_name);
00843              return(0);
00844              /* break; */
00845            }
00846          case A_TITLE_STRING:
00847            (*state)->attr->_attr_u.LUstruct.title = (char *)Xmalloc(strlen(tokenbuf)+1);
00848            strcpy((*state)->attr->_attr_u.LUstruct.title, tokenbuf);
00849            return(0);
00850            /* break; */
00851          case A_FONTSET:
00852            return(0);
00853            /* break; */
00854          case A_PLACEMENT:
00855            return(0);
00856            /* break; */
00857          case A_POSITION:
00858            return(0);
00859            /* break; */
00860          default:
00861            goto error;
00862            /* break; */
00863          }
00864          /* break; */
00865        default:
00866          goto error;
00867          /* break; */
00868        }
00869     } else if (token == STRING) {
00870        if( (rhs_string_mb = Xmalloc(strlen(tokenbuf) + 1)) == NULL )
00871            goto error;
00872        memset(rhs_string_mb, 0, strlen(tokenbuf)+1);
00873        strcpy(rhs_string_mb, tokenbuf);
00874        token = nexttoken(fp);
00875        if (token == KEY) {
00876            rhs_keysym = XStringToKeysym(tokenbuf);
00877            if (rhs_keysym == NoSymbol) {
00878               Xfree(rhs_string_mb);
00879               rhs_string_mb = NULL;
00880               goto error;
00881            }
00882            token = nexttoken(fp);
00883        }
00884        if (token != ENDOFLINE && token != ENDOFFILE) {
00885            Xfree(rhs_string_mb);
00886            rhs_string_mb = NULL;
00887            goto error;
00888        }
00889     } else if (token == KEY) {
00890        rhs_keysym = XStringToKeysym(tokenbuf);
00891        if (rhs_keysym == NoSymbol) {
00892            goto error;
00893        }
00894        token = nexttoken(fp);
00895        if (token != ENDOFLINE && token != ENDOFFILE) {
00896            goto error;
00897        }
00898        if( (rhs_string_mb = Xmalloc(1)) == NULL ) {
00899            goto error;
00900        }
00901        memset(rhs_string_mb, 0, 1);
00902     } else if (token == T_SWITCH_STATE_TO) {
00903        token = nexttoken(fp);
00904        if(token == STRING) {
00905            if((rhs_target_name = Xmalloc(strlen(tokenbuf) + 1)) == NULL){
00906               goto error;
00907            }
00908            memset(rhs_target_name, 0, strlen(tokenbuf)+1);
00909            strcpy(rhs_target_name, tokenbuf);
00910            act = SwitchTo;
00911        } else {
00912               goto error;
00913        }
00914     } else if (token == T_DO_CONV) {
00915        act = DoConv;
00916     } else if (token == T_TABLELOOKUP) {
00917        act = DoLookup;
00918     } else if (token == T_NEXT_PAGE) {
00919        act = NextPage;
00920     } else if (token == T_PREV_PAGE) {
00921        act = PrevPage;
00922     } else if (token == T_TOP_PAGE) {
00923        act = TopPage;
00924     } else if (token == T_LAST_PAGE) {
00925        act = LastPage;
00926 #ifdef USE_CAND_SELECTION
00927     } else if (token == T_NEXT_CAND) {
00928        act = NextCand;
00929     } else if (token == T_PREV_CAND) {
00930        act = PrevCand;
00931     } else if (token == T_UP_CAND) {
00932        act = UpCand;
00933     } else if (token == T_DOWN_CAND) {
00934        act = DownCand;
00935     } else if (token == T_SELECT_CAND) {
00936        act = SelectCand;
00937 #endif
00938     } else {
00939        goto error;
00940     }
00941 
00942     if(!(*state)) {
00943        *state = create_state((Ximp_XIM)im, "");
00944        if(!(*state))
00945               goto error;
00946     }
00947 
00948     if ( n == 0 ) return(0);
00949 
00950     top = &((*state)->parser);
00951 
00952     for (i = 0; i < n; i++) {
00953        for (p = *top; p; p = p->next) {
00954            if (buf[i].keysym        == p->keysym &&
00955               buf[i].keycode       == p->keycode &&
00956               buf[i].modifier      == p->modifier &&
00957               buf[i].modifier_mask == p->modifier_mask) {
00958               break;
00959            }
00960        }
00961        if (p) {
00962            top = &p->succession;
00963        } else {
00964            if( (p = (DefTree*)Xmalloc(sizeof(DefTree))) == NULL ) {
00965               if ( rhs_string_mb ) Xfree( rhs_string_mb );
00966               rhs_string_mb = NULL;
00967               goto error;
00968            }
00969            memset(p, 0, sizeof(DefTree));
00970            p->keysym        = buf[i].keysym;
00971            p->keycode       = buf[i].keycode;
00972            p->modifier      = buf[i].modifier;
00973            p->modifier_mask = buf[i].modifier_mask;
00974            p->succession    = NULL;
00975            p->next          = *top;
00976            p->mb            = NULL;
00977            p->keysym_return = NoSymbol;
00978            p->action      = NoAction;
00979            p->target_name   = NULL;
00980            p->target      = NULL;
00981            *top = p;
00982            top = &p->succession;
00983        }
00984     }
00985 
00986     if( p->mb != NULL ) {
00987        Xfree( p->mb );
00988        p->mb = NULL;
00989     }
00990     if( p->target_name != NULL ) {
00991        Xfree( p->target_name );
00992        p->target_name = NULL;
00993     }
00994     if( p->target != NULL ) {
00995        Xfree( p->target );
00996        p->target = NULL;
00997     }
00998 
00999     p->action = act;
01000 
01001     if(rhs_target_name == NULL){
01002        p->mb = NULL;
01003        if (p->action == SwitchTo) {
01004            if ( rhs_string_mb ) Xfree(rhs_string_mb);
01005        } else {
01006            if ( rhs_string_mb ) {
01007               p->mb = rhs_string_mb;
01008            }
01009            p->keysym_return = rhs_keysym;
01010        }
01011     } else{
01012        p->target_name = rhs_target_name;
01013     }
01014     return(n);
01015 
01016 error:
01017     while (token != ENDOFLINE && token != ENDOFFILE) {
01018        token = nexttoken(fp);
01019     }
01020     return(0);
01021 }
01022 
01023 static int
01024 set_change_state_info(xim, p)
01025 Ximp_XIM xim;
01026 DefTree *p;
01027 {
01028     XimCommon im = (XimCommon)xim;
01029     LocalIMState     *state;
01030 
01031     /* next */
01032     if (p->next) {
01033       if (set_change_state_info((Ximp_XIM)im, p->next) == -1)
01034        return -1;
01035     }
01036 
01037     /* succession */
01038     if (p->succession)
01039       return set_change_state_info((Ximp_XIM)im, p->succession);
01040 
01041     /* self */
01042     if (p->target_name == NULL)
01043       return 0;
01044 
01045     state = im->local_impart->top_state;
01046 
01047     while(state) {
01048       if (!strcmp(state->name, p->target_name)){
01049        p->target = state;
01050        break;
01051       }
01052       state = state->next;
01053     }
01054     if (state != NULL)
01055       return 0;
01056     else
01057       return -1;
01058 }
01059 
01060 int
01061 Ximp_ParseStringFile(fp, im)
01062 FILE *fp;
01063 XimCommon im;
01064 {
01065     int max_ev_seq = 0, i;
01066     LocalIMState *state = 0;
01067 
01068     memset(tokenbuf, 0, sizeof(tokenbuf));
01069     while ((i = parseline(fp, im, &state)) >= 0) {
01070        if (i > max_ev_seq) max_ev_seq = i;
01071     }
01072 
01073     state = im->local_impart->top_state;
01074     while(state) {
01075       if (set_change_state_info((Ximp_XIM)im, state->parser) == -1)
01076        return (-1);
01077       state = state->next;
01078     }
01079 
01080     return (max_ev_seq);
01081 }