#include #include #include "spinix.h" #include "varsubs.h" static char retvalstr[16]; //****************************************************************************** // Copyright (c) 2013 Dave Hein // See end of file for terms of use. //****************************************************************************** char *varsubs_FindVar(char *str) { char *ptr = (char *)spinix_environ_vars; while (*ptr) { if (!strcmp(str, ptr + 1)) { if (*ptr != varsubs_ALIAS_FLAG) { return ptr; } } ptr += strlen(ptr) + 1; ptr += strlen(ptr) + 1; } return 0; } char *varsubs_FindAlias(char *str) { char *ptr = (char *)spinix_environ_vars; while (*ptr) { if (!strcmp(str, ptr + 1)) { if (*ptr == varsubs_ALIAS_FLAG) { return ptr; } } ptr += strlen(ptr) + 1; ptr += strlen(ptr) + 1; } return 0; } char *varsubs_FindEnd(void) { char *ptr = (char *)spinix_environ_vars; while (*ptr) { ptr += strlen(ptr) + 1; ptr += strlen(ptr) + 1; } return ptr; } char *varsubs_RemoveEntry(char *ptr1) { int i, len; char *ptr2 = ptr1 + strlen(ptr1) + 1; ptr2 += strlen(ptr2) + 1; while (*ptr2) { for (i = 0; i < 2; i++) { len = strlen(ptr2) + 1; memmove (ptr1, ptr2, len); ptr1 += len; ptr2 += len; } } *ptr1 = 0; return ptr1; } void varsubs_RemoveVar(char *name) { char *ptr; if ((ptr = varsubs_FindVar(name))) { ptr = varsubs_RemoveEntry(ptr); } } void varsubs_SaveVar(char *name, char *value, int type) { char *ptr; int space, len1, len2; // Check if variable already exists if ((ptr = varsubs_FindVar(name))) { type = *ptr; ptr = varsubs_RemoveEntry(ptr); } else { ptr = varsubs_FindEnd(); } // Check if space is available space = spinix_environ_vars_end - (int)ptr + 1; len1 = strlen(name) + 1; len2 = strlen(value) + 1; if ((space < len1 + len2 + 2)) { printf("Not enough variable space\n"); return; } else if ((space < len1 + len2 + 82)) { printf("space = %d, need = %d\n", space, len1 + len2 + 2); } // Add variable *ptr++ = type; memmove (ptr, name, len1); ptr += len1; memmove (ptr, value, len2); ptr += len2; *ptr = 0; } char *varsubs_GetVarVal(char *name) { char *ptr; if (!strcmp(name, "?")) { sprintf(retvalstr, "%d", *(int *)spinix_return_value); return retvalstr; } if ((ptr = varsubs_FindVar(name))) { return ptr + strlen(ptr) + 1; } return ""; } char *varsubs_Val(char *ptr) { if (*ptr != '$') return ptr; return varsubs_GetVarVal(ptr + 1); } int varsubs_NumVal(char *ptr) { if (*ptr == '$') { if (!strcmp(ptr, "$?")) { return *(int *)spinix_return_value; } ptr = varsubs_GetVarVal(ptr + 1); } return atol(ptr); } #if 0 char *varsubs_FindChar(char *str, int val) { while (*str && *str != val) str++; return str; } char *varsubs_SkipChar(char *str, int val) { while (*str && *str == val) str++; return str; } #endif /* +-----------------------------------------------------------------------------+ | TERMS OF USE: MIT License | +-----------------------------------------------------------------------------+ |Permission is hereby granted, free of charge, to any person obtaining a copy | |of this software and associated documentation files (the "Software"), to deal| |in the Software without restriction, including without limitation the rights | |to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |copies of the Software, and to permit persons to whom the Software is | |furnished to do so, subject to the following conditions: | | | |The above copyright notice and this permission notice shall be included in | |all copies or substantial portions of the Software. | | | |THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,| |OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE| |SOFTWARE. | +-----------------------------------------------------------------------------+ */