Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /* $Id: lx_support.c,v 1.1.1.1 2003/11/04 23:34:57 mjames Exp $ |
| 2 | * |
||
| 3 | * $Log: lx_support.c,v $ |
||
| 4 | * Revision 1.1.1.1 2003/11/04 23:34:57 mjames |
||
| 5 | * Imported into local repositrory |
||
| 6 | * |
||
| 7 | * Revision 1.8 2002/09/09 10:29:56 mjames |
||
| 8 | * Moved pin remapping function to pin ident editing function from |
||
| 9 | * sorting pin name routine. |
||
| 10 | * |
||
| 11 | * Revision 1.7 2002/01/16 11:22:45 mjames |
||
| 12 | * database.h header file is read in first as it undefined DLL stuff irrelevant |
||
| 13 | * to HPUX |
||
| 14 | * |
||
| 15 | * Revision 1.6 2002/01/15 12:34:12 mjames |
||
| 16 | * DLL declarations put in |
||
| 17 | * |
||
| 18 | * Revision 1.5 2001/12/13 22:14:25 mjames |
||
| 19 | * Corrected nested command handlers to allow variable passing without corruption. |
||
| 20 | * |
||
| 21 | * Revision 1.4 2001/10/31 22:20:08 mjames |
||
| 22 | * Tidying up problematical comments caused by CVS |
||
| 23 | * 'intelligent' comment guessing |
||
| 24 | * |
||
| 11 | mjames | 25 | * |
| 2 | mjames | 26 | */ |
| 11 | mjames | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
||
| 29 | #include <stdlib.h> |
||
| 2 | mjames | 30 | |
| 31 | #include "expression.h" |
||
| 32 | #include "generic.h" |
||
| 11 | mjames | 33 | #include "database.h" |
| 34 | |||
| 35 | |||
| 2 | mjames | 36 | #include "vertcl_main.h" |
| 11 | mjames | 37 | #include "lx_support.h" |
| 2 | mjames | 38 | |
| 39 | |||
| 11 | mjames | 40 | |
| 41 | #include "cmdparse.h" |
||
| 42 | #include "cmdlog.h" |
||
| 43 | #include "cmdutil.h" |
||
| 44 | #include "cmdexec.h" |
||
| 45 | |||
| 46 | #include "acf_yacc.h" |
||
| 47 | |||
| 48 | |||
| 2 | mjames | 49 | /* purpose to queue lex tokens until all have been used on a line or in a statement */ |
| 50 | |||
| 51 | int yyval; /* propose to confuse LEX ... */ |
||
| 11 | mjames | 52 | int yy_nArgs; |
| 53 | char ** yy_Args; |
||
| 2 | mjames | 54 | |
| 11 | mjames | 55 | #ident "@(#)$Header: c:\\cygwin\\cvsroot/Vert03/vertlib/lx_support.c,v 1.1.1.1 2003/11/04 23:34:57 mjames Exp $" |
| 2 | mjames | 56 | |
| 11 | mjames | 57 | __declspec (dllexport)struct str * lx_first = NULL; |
| 58 | __declspec (dllexport)struct str * lx_last = NULL; |
||
| 2 | mjames | 59 | |
| 11 | mjames | 60 | |
| 61 | |||
| 62 | |||
| 2 | mjames | 63 | /* the Args values are for the current argc/argv context */ |
| 11 | mjames | 64 | void expand_string( char * source , char * dest, int nArgs, char * Args[]) { |
| 65 | char num_buff[LINELEN]; |
||
| 2 | mjames | 66 | |
| 11 | mjames | 67 | char * Ptr = source; |
| 68 | char * Optr= dest; |
||
| 69 | generic_info_t ** chip_generics = NULL; |
||
| 70 | |||
| 71 | /* printf("IN ='%s'\n",source); */ |
||
| 72 | |||
| 73 | if(!source || !dest) |
||
| 74 | return; |
||
| 2 | mjames | 75 | |
| 76 | |||
| 11 | mjames | 77 | dest[0] = 0; |
| 78 | while(*Ptr && (Optr-dest)<LINELEN-1) { |
||
| 79 | |||
| 80 | if(*Ptr=='\\') { |
||
| 81 | Ptr++; |
||
| 82 | if (*Ptr) { |
||
| 83 | *Optr++ = *Ptr++; |
||
| 84 | *Optr = 0; |
||
| 85 | }; |
||
| 86 | continue; |
||
| 87 | }; |
||
| 2 | mjames | 88 | |
| 11 | mjames | 89 | if(*Ptr=='$') { /* variable expansion */ |
| 90 | |||
| 91 | /* pattern 1 : $n - lookup argv[n] */ |
||
| 92 | if(Ptr[1] >='0' && Ptr[1] <='9') { |
||
| 93 | int n = Ptr[1]-'0'; |
||
| 94 | if (n < nArgs ) { |
||
| 95 | |||
| 96 | /* space to place new word ? */ |
||
| 97 | if((Optr+strlen(Args[n])-dest)<LINELEN ) { |
||
| 98 | strcpy(Optr, Args[n]); |
||
| 99 | Optr+= strlen(Args[n]); |
||
| 100 | *Optr = 0; |
||
| 101 | }; |
||
| 102 | |||
| 103 | Ptr+=2; |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | /* pattern 2 : look up a generic SYSVAR */ |
||
| 108 | else if (Ptr[1]=='(') { |
||
| 109 | char var_name[WORDWIDTH], * env_val; |
||
| 110 | int c = 0; |
||
| 111 | generic_info_t generic[1]; |
||
| 112 | generic_type_t gen_type; |
||
| 113 | Ptr+=2; /* skip '$(' */ |
||
| 114 | var_name[0] = '\0'; |
||
| 115 | while(*Ptr && *Ptr != ')') { |
||
| 116 | if(c<WORDWIDTH-1) { |
||
| 117 | var_name[c++] = *Ptr; |
||
| 118 | var_name[c] = '\0'; |
||
| 119 | } |
||
| 120 | Ptr++; |
||
| 121 | } |
||
| 122 | if(*Ptr) Ptr++; |
||
| 123 | /* now lookup result */ |
||
| 124 | |||
| 125 | env_val = NULL; |
||
| 126 | /* try generic lookup first */ |
||
| 2 | mjames | 127 | |
| 11 | mjames | 128 | gen_type = get_generic_value(&global_generics,var_name,generic); |
| 129 | if(!gen_type) |
||
| 130 | gen_type = get_generic_value(&partition_generics,var_name,generic); |
||
| 131 | /* accept a string */ |
||
| 132 | if (gen_type==IS_STRING || gen_type == IS_ENV_VAL) { |
||
| 133 | if(generic->expr) |
||
| 134 | env_val = generic->expr->left.s; |
||
| 135 | }; |
||
| 136 | if (gen_type==IS_INTEGER) |
||
| 137 | { |
||
| 138 | if(generic->expr) |
||
| 139 | { |
||
| 140 | sprintf(num_buff,"%d",eval_expression(generic->expr,chip_generics)); |
||
| 141 | env_val = num_buff; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | if (gen_type==TO) |
||
| 145 | { |
||
| 146 | if(generic->expr) |
||
| 147 | { |
||
| 148 | sprintf(num_buff,"%d TO %d",eval_expression(generic->expr->left.e,chip_generics),eval_expression(generic->expr->right.e,chip_generics)); |
||
| 149 | env_val = num_buff; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if (gen_type==DOWNTO) |
||
| 153 | { |
||
| 154 | if(generic->expr) |
||
| 155 | { |
||
| 156 | sprintf(num_buff,"%d DOWNTO %d",eval_expression(generic->expr->left.e,chip_generics),eval_expression(generic->expr->right.e,chip_generics)); |
||
| 157 | env_val = num_buff; |
||
| 158 | } |
||
| 159 | } |
||
| 2 | mjames | 160 | |
| 161 | |||
| 11 | mjames | 162 | /* try OS environment variables */ |
| 163 | if (!env_val) |
||
| 164 | env_val = getenv(var_name); |
||
| 165 | if (env_val && (Optr+strlen(env_val)-dest)<LINELEN ) { |
||
| 166 | strcpy(Optr,env_val ); |
||
| 167 | Optr+= strlen(env_val); |
||
| 168 | *Optr = 0; |
||
| 169 | continue; |
||
| 170 | }; |
||
| 171 | if ((Optr+strlen(var_name)-dest)<LINELEN ) { |
||
| 172 | sprintf(Optr,"$(%s)",var_name); |
||
| 173 | Optr+= strlen(var_name)+3; |
||
| 174 | *Optr = 0; |
||
| 175 | } |
||
| 176 | |||
| 177 | } |
||
| 178 | } |
||
| 179 | if(*Ptr != '\r' && *Ptr != '\n') |
||
| 180 | { |
||
| 181 | *Optr++ = *Ptr; |
||
| 182 | *Optr = 0; |
||
| 183 | } |
||
| 184 | Ptr++; |
||
| 185 | } |
||
| 186 | /* printf("OUT='%s'\n",dest); */ |
||
| 187 | } |
||
| 2 | mjames | 188 | |
| 11 | mjames | 189 | char * make_string(char * token ,struct str ** pfirst,struct str ** plast ) |
| 190 | { |
||
| 191 | char * t; |
||
| 192 | struct str * s; |
||
| 193 | int l; |
||
| 194 | char workbuff[LINELEN]; |
||
| 195 | /* perform checks in the right order 18-02-2000 */ |
||
| 196 | if(!token) |
||
| 197 | return NULL; |
||
| 198 | l = strlen(token); |
||
| 199 | s = calloc(1,sizeof(struct str )); |
||
| 200 | if (!s) |
||
| 201 | return NULL; |
||
| 202 | |||
| 203 | /* eliminate quotes from the string : |
||
| 204 | * !! ASSUMES string is in Read/Write memory */ |
||
| 205 | if (l>=2 && token[0]=='\"' && token[l-1]=='\"') |
||
| 206 | { |
||
| 207 | strncpy (token,token+1,l-2); /* chuck quotes off string */ |
||
| 208 | token[l-2] = '\0'; /* null terminate */ |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /* the values yy_nArgs and yy_Args were filled |
||
| 213 | in just before calling yyparse */ |
||
| 214 | expand_string(token,workbuff,yy_nArgs,yy_Args); |
||
| 215 | l = strlen(workbuff); |
||
| 216 | |||
| 217 | t = malloc(l+1); |
||
| 218 | if (!t) |
||
| 219 | return NULL; |
||
| 2 | mjames | 220 | |
| 11 | mjames | 221 | strcpy(t,workbuff); /* copy string , join to structure */ |
| 222 | s->dat = t; |
||
| 223 | s->next = NULL; |
||
| 2 | mjames | 224 | |
| 225 | |||
| 226 | |||
| 227 | |||
| 11 | mjames | 228 | if (!*pfirst) |
| 229 | *pfirst = s; |
||
| 2 | mjames | 230 | |
| 11 | mjames | 231 | if(*plast) |
| 232 | (*plast)->next = s; |
||
| 2 | mjames | 233 | |
| 11 | mjames | 234 | *plast = s; |
| 235 | /* printf("MADE\n"); */ |
||
| 2 | mjames | 236 | |
| 237 | |||
| 11 | mjames | 238 | return (t); /* pointer to the string that can be later tidily freed */ |
| 2 | mjames | 239 | |
| 240 | |||
| 241 | |||
| 11 | mjames | 242 | } |
| 2 | mjames | 243 | |
| 11 | mjames | 244 | |
| 245 | void free_strings( struct str ** pfirst, struct str ** plast) |
||
| 246 | { |
||
| 247 | struct str * s,* t; |
||
| 248 | s = *pfirst; |
||
| 249 | while(s) /* run down the list, freeing entries */ |
||
| 250 | { |
||
| 251 | if (s->dat) |
||
| 252 | free(s->dat); |
||
| 253 | t = s; |
||
| 254 | s= s->next; |
||
| 255 | free(t); |
||
| 256 | } |
||
| 257 | *pfirst = NULL; |
||
| 258 | *plast = NULL; |
||
| 259 | |||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 2 | mjames | 263 | /* called from YACC to deallocate memory structures cleanly */ |
| 11 | mjames | 264 | void free_lex_strings(void) { |
| 265 | free_strings(&lx_first,&lx_last); |
||
| 266 | }; |
||
| 2 | mjames | 267 | |
| 11 | mjames | 268 | #define to_lower(c) ((c>='A' && c<='Z') ? c-'A'+'a':c) |
| 2 | mjames | 269 | |
| 11 | mjames | 270 | |
| 271 | int streq(char *s1,char *s2) |
||
| 2 | mjames | 272 | { |
| 11 | mjames | 273 | while (*s1 != '\0') { |
| 274 | if (to_lower(*s2) != to_lower(*s1)) |
||
| 275 | return( 0 ); |
||
| 276 | s1++; |
||
| 277 | s2++; |
||
| 278 | } |
||
| 279 | return( *s2 == '\0' ); |
||
| 2 | mjames | 280 | } |
| 281 | |||
| 11 | mjames | 282 | |
| 283 | int strneq(char *s1,char *s2,int n) |
||
| 2 | mjames | 284 | { |
| 11 | mjames | 285 | while (n && *s1 != '\0' ) { |
| 286 | if (to_lower(*s2) != to_lower(*s1)) |
||
| 287 | return( 0 ); |
||
| 288 | s1++; |
||
| 289 | s2++; |
||
| 290 | --n; |
||
| 291 | } |
||
| 292 | return( n==0 || *s2 == '\0' ); |
||
| 2 | mjames | 293 | } |