📄 variables.c
字号:
/* Autoconf patching by David Hedbor, neotron@lysator.liu.se *//*********************************************************************//* file: variables.c - functions related the the variables *//* TINTIN ++ *//* (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *//* coded by peter unold 1992 *//*********************************************************************/#ifdef HAVE_STRING_H#include <string.h>#else#ifdef HAVE_STRINGS_H#include <strings.h>#endif#endif#include <ctype.h>#include "tintin.h"#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endifextern char *get_arg_in_braces();extern struct listnode *search_node_with_wild();extern struct listnode *searchnode_list();extern struct listnode *common_myvars;extern char vars[10][BUFFER_SIZE]; /* the %0, %1, %2,....%9 variables */extern int varnum;extern int mesvar[5];/*************************//* the #variable command *//*************************/void var_command(arg, ses) char *arg; struct session *ses;{ char left[BUFFER_SIZE], right[BUFFER_SIZE], arg2[BUFFER_SIZE]; struct listnode *tempvars, *ln; /* char right2[BUFFER_SIZE]; */ tempvars=(ses) ? ses->myvars : common_myvars; arg=get_arg_in_braces(arg, left,0); arg=get_arg_in_braces(arg, right,1); if(!*left) { tintin_puts2("#THESE VARIABLES HAVE BEEN SET:", ses); show_list(tempvars); prompt(ses); } else if(*left && !*right) { if ((ln=search_node_with_wild(tempvars,left))!=NULL) { while((tempvars=search_node_with_wild(tempvars, left))!=NULL) { shownode_list(tempvars); } prompt(ses); } else if (mesvar[5]) tintin_puts2("#THAT VARIABLE IS NOT DEFINED.", ses); } else { if((ln=searchnode_list(tempvars, left))!=NULL) deletenode_list(tempvars, ln); insertnode_list(tempvars, left, right, "0", ALPHA); varnum++; if (mesvar[5]) { sprintf(arg2, "#Ok. $%s is now set to {%s}.",left, right); tintin_puts2(arg2, ses); } }}/************************//* the #unvar command *//************************/void unvar_command(arg, ses) char *arg; struct session *ses;{ char left[BUFFER_SIZE] ,result[BUFFER_SIZE]; struct listnode *tempvars, *ln, *temp; int flag; flag=FALSE; tempvars=(ses) ? ses->myvars : common_myvars; temp=tempvars; arg=get_arg_in_braces(arg,left,1); while ((ln=search_node_with_wild(temp, left))!=NULL) { if (mesvar[5]) { sprintf(result, "#Ok. $%s is no longer a variable.", ln->left); tintin_puts2(result, ses); } deletenode_list(tempvars, ln); flag=TRUE; temp=ln; } if (!flag && mesvar[5]) tintin_puts2("#THAT VARIABLE IS NOT DEFINED.", ses);}/*************************************************************************//* copy the arg text into the result-space, but substitute the variables *//* $<string> with the values they stand for *//* *//* Notes by Sverre Normann (sverreno@stud.ntnu.no): *//* xxxxxxxxx 1996: added 'simulation' of secstotick variable *//* Tue Apr 8 1997: added ${<string>} format to allow use of non-alpha *//* characters in variable name *//*************************************************************************//* Note: $secstotick will return number of seconds to tick (from tt's *//* internal counter) provided no variable named 'secstotick' *//* already exists *//*************************************************************************/void substitute_myvariables(arg, result, ses) char *arg; char *result; struct session *ses;{ char varname[BUFFER_SIZE], temp[BUFFER_SIZE]; int nest=0,counter,varlen; int specvar; char message[BUFFER_SIZE]; struct listnode *ln, *tempvars; tempvars=(ses) ? ses->myvars : common_myvars; fflush(stdout); while(*arg) { if(*arg=='$') { /* substitute variable */ counter=0; while (*(arg+counter)=='$') counter++; varlen=0;/* ${name} code added by Sverre Normann */ if(*(arg+counter)!=DEFAULT_OPEN) { specvar=FALSE; while(isalpha(*(arg+varlen+counter))) varlen++; if (varlen>0) strncpy(varname,arg+counter,varlen); *(varname+varlen)='\0'; } else { specvar=TRUE; get_arg_in_braces(arg+counter, temp, 0); substitute_myvars(temp, varname, ses); /* RECURSIVE CALL */ varlen=strlen(temp); } if(specvar) varlen+=2; if (counter==nest+1 && !isdigit(*(arg+counter+1))) { if((ln=searchnode_list(tempvars, varname))!= NULL) { strcpy(result, ln->right); result+=strlen(ln->right); arg+=counter+varlen; } else {/* secstotick code added by Sverre Normann */ if (strcmp(varname,"secstotick")) { strncpy(result,arg,counter+varlen); result+=varlen+counter; } else { char buf[BUFFER_SIZE]; sprintf(buf,"%d",timetilltick()); strcpy(result, buf); result+=strlen(buf); } arg+=counter+varlen; } } else { strncpy(result,arg,counter+varlen); result+=varlen+counter; arg+=varlen+counter; } } else if (*arg==DEFAULT_OPEN) { nest++; *result++= *arg++; } else if (*arg==DEFAULT_CLOSE) { nest--; *result++= *arg++; } else if (*arg=='\\' && *(arg+1)=='$' && nest==0) { arg++; *result++= *arg++; } else *result++= *arg++; } *result='\0';}/*****************************************************//* the #getvarvalue command * By Sverre Normann *//*****************************************************//* Needed since tintin's var substitute can't handle *//* variable names with non-alpha characters like *//* space, numbers and others. *//*****************************************************//* NB! #getvarvalue is redundant after the ${<name>} *//* format was made. Kept only for to keep comp. *//* with older comfiles... *//*****************************************************/void getvarvalue_command(arg, ses) char *arg; struct session *ses;{ char left[BUFFER_SIZE], right[BUFFER_SIZE], temp[BUFFER_SIZE]; struct listnode *ln, *tempvars; tempvars = (ses) ? ses->myvars : common_myvars; arg=get_arg_in_braces(arg, left, 0); arg=get_arg_in_braces(arg, right, 1); if (!*left || !*right) { tintin_puts2("#Syntax: #getvarvalue {dest var} {source var}", ses); } else { substitute_vars(left,temp); substitute_myvars(temp,left,ses); substitute_vars(right,temp); substitute_myvars(temp,right,ses); if((ln=searchnode_list(tempvars, right))!= NULL) { sprintf(temp,"{%s} {%s}",left,ln->right); var_command(temp, ses); } else { tintin_puts2("#Error in #getvarvalue: variable does not exists",ses); } }}/********************************************************//* the #getlistlength command * By Sverre Normann *//********************************************************//* Syntax: #getlistlength {destination variable} {list} *//*****************************************************************//* Note: This will return the number of items in the list. *//* An item is either a word, or grouped words in brackets. *//* Ex: #getl {listlength} {smile {say Hi!} flip bounce} *//* -> listlength = 4 *//*****************************************************************/void getlistlength_command(arg, ses) char *arg; struct session *ses;{ char left[BUFFER_SIZE], list[BUFFER_SIZE], temp[BUFFER_SIZE]; int i; arg = get_arg_in_braces(arg, left,0); if (!*left) { tintin_puts2("#Error - Syntax: #getlistlength {dest var} {list}", ses); } else { substitute_vars(left,temp); substitute_myvars(temp,left,ses);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -