/* FIXME */ void sall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; struct kvd *kvd; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); } dstr_free(res); //-------------------------------------------------------------------------------------------------------------------- // FIXME //-------------------------------------------------------------------------------------------------------------------- }
/* FIXME */ void uall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey))) { struct kvd *kvd; if (NEW(kvd)) { dlist_node_t node, node2; kvd->key = skey; if ((node = dlist_find(dlist, kvd))) { FREE(kvd); kvd = (struct kvd *)dlist_node_value(node); if ((node2 = dlist_find(kvd->u.dlist, c))) dlist_remove(kvd->u.dlist, node2); if (dlist_length(kvd->u.dlist) == 0) { dlist_remove(dlist, node); kdfree(kvd); } if (dlist_length(dlist) == 0) { table_remove(subscribers, pkey); dlist_free(&dlist); } } else FREE(kvd); } else add_reply_error(c, "error allocating memory for kvd"); } table_rwlock_unlock(subscribers); dstr_free(skey); dstr_free(pkey); } add_reply_string(c, "\r\n", 2); dstr_free(res); }
/*! \param Operator */ xbShort xbExpn::AlphaOperation( char * Operator ) { xbShort ResultLen, i; char SaveType; xbExpNode * WorkNode; if( Operator[0] == '=' || Operator[0] == '<' || Operator[0] == '>' || Operator[0] == '#' || (strncmp( Operator, "!=", 2 ) == 0 ) || Operator[0] == '$'){ ResultLen = 0; SaveType = 'l'; } else { ResultLen = OpDataLen1 + OpDataLen2 + 1; SaveType = 's'; } WorkNode = new xbExpNode; if( !WorkNode ) return XB_PARSE_ERROR; WorkNode->ResultLen = ResultLen; WorkNode->Type = SaveType; if( WorkNode->Type == 'l' ) WorkNode->DataLen = 0; else WorkNode->DataLen = ResultLen - 1; if( Operator[0] == '+' ){ WorkNode->StringResult = Op2; WorkNode->StringResult += Op1; } else if( Operator[0] == '-' ) { WorkNode->StringResult = RTRIM( Op2 ); WorkNode->StringResult += Op1; i = WorkNode->StringResult.len(); for( ; i < ResultLen-1; i++) WorkNode->StringResult += " "; } /* == */ else if(( strncmp( Operator, "==", 2 ) == 0 ) && strcmp(Op1,Op2) == 0) WorkNode->IntResult = 1; else if(( strncmp( Operator, "==", 2 ) == 0 )) WorkNode->IntResult = 0; /* = */ else if( Operator[0] == '=' && strcmp(Op1,Op2) == 0 ) WorkNode->IntResult = 1; else if( Operator[0] == '=' ) WorkNode->IntResult = 0; /* not = */ else if(( strncmp( Operator, "<>", 2 ) == 0 ) || Operator[0] == '#' || strncmp( Operator, "!=", 2 ) == 0 ) WorkNode->IntResult = ( strcmp( Op1, Op2 ) != 0 ) ? 1 : 0; /* less than */ else if( Operator[0] == '<' ) WorkNode->IntResult = ( strcmp( Op2, Op1 ) < 0 ) ? 1 : 0; /* greater than */ else if( Operator[0] == '>' ) WorkNode->IntResult = ( strcmp( Op2, Op1 ) > 0 ) ? 1 : 0; else if(Operator[0] == '$') WorkNode->IntResult = (strstr(Op1,Op2)) ? 1 : 0; else return XB_PARSE_ERROR; Push(WorkNode); return XB_NO_ERROR; }
/*! \param Func */ xbShort xbExpn::ProcessFunction( char * Func ) { /* 1 - pop function from stack 2 - verify function name and get no of parms needed 3 - verify no of parms >= remainder of stack 4 - pop parms off stack 5 - execute function 6 - push result back on stack */ char *buf = 0; xbExpNode *p1, *p2, *p3, *WorkNode, *FuncNode; xbShort ParmsNeeded,len; char ptype = 0; /* process type s=string, l=logical, d=double */ xbDouble DoubResult = 0; xbLong IntResult = 0; FuncNode = (xbExpNode *) Pop(); ParmsNeeded = GetFuncInfo( Func, 1 ); if( ParmsNeeded == -1 ) { return XB_INVALID_FUNCTION; } else { ParmsNeeded = 0; if( FuncNode->Sibling1 ) ParmsNeeded++; if( FuncNode->Sibling2 ) ParmsNeeded++; if( FuncNode->Sibling3 ) ParmsNeeded++; } if( ParmsNeeded > GetStackDepth()) return XB_INSUFFICIENT_PARMS; p1 = p2 = p3 = NULL; if( ParmsNeeded > 2 ) p3 = (xbExpNode *) Pop(); if( ParmsNeeded > 1 ) p2 = (xbExpNode *) Pop(); if( ParmsNeeded > 0 ) p1 = (xbExpNode *) Pop(); memset( WorkBuf, 0x00, WorkBufMaxLen+1); if( strncmp( Func, "ABS", 3 ) == 0 ) { ptype = 'd'; DoubResult = ABS( GetDoub( p1 )); } else if( strncmp( Func, "ASC", 3 ) == 0 ) { ptype = 'd'; DoubResult = ASC( p1->StringResult ); } else if( strncmp( Func, "AT", 2 ) == 0 ) { ptype = 'd'; DoubResult = AT( p1->StringResult, p2->StringResult ); } else if( strncmp( Func, "CDOW", 4 ) == 0 ) { ptype = 's'; buf = CDOW( p1->StringResult ); } else if( strncmp( Func, "CHR", 3 ) == 0 ) { ptype = 's'; buf = CHR( GetInt( p1 )); } else if( strncmp( Func, "CMONTH", 6 ) == 0 ) { ptype = 's'; buf = CMONTH( p1->StringResult ); } else if( strncmp( Func, "CTOD", 4 ) == 0 ) { ptype = 's'; buf = CTOD( p1->StringResult ); } else if( strncmp( Func, "DATE", 4 ) == 0 ) { ptype = 's'; buf = DATE(); } else if( strncmp( Func, "DAY", 3 ) == 0 ) { ptype = 'd'; DoubResult = DAY( p1->StringResult ); } else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'C' ) { ptype = 's'; buf = DESCEND( p1->StringResult.c_str() ); } else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'N' ) { ptype = 'd'; DoubResult = DESCEND( GetDoub( p1 )); } else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'D' ) { xbDate d( p1->StringResult ); ptype = 'd'; DoubResult = DESCEND( d ); } else if( strncmp( Func, "DOW", 3 ) == 0 ) { ptype = 'd'; DoubResult = DOW( p1->StringResult ); } else if( strncmp( Func, "DTOC", 4 ) == 0 ) { ptype = 's'; buf = DTOC( p1->StringResult ); } else if( strncmp( Func, "DTOS", 4 ) == 0 ) { ptype = 's'; buf = DTOS( p1->StringResult ); } else if( strncmp( Func, "EXP", 3 ) == 0 ) { ptype = 'd'; DoubResult = EXP( GetDoub( p1 )); } else if( strncmp( Func, "IIF", 3 ) == 0 ){ ptype = 's'; buf = IIF( p1->IntResult, p2->StringResult, p3->StringResult ); } else if( strncmp( Func, "INT", 3 ) == 0 ) { ptype = 'd'; DoubResult = INT( GetDoub( p1 )); } else if( strncmp( Func, "ISALPHA", 7 ) == 0 ) { ptype = 'l'; IntResult = ISALPHA( p1->StringResult ); } else if( strncmp( Func, "ISLOWER", 7 ) == 0 ) { ptype = 'l'; IntResult = ISLOWER( p1->StringResult ); } else if( strncmp( Func, "ISUPPER", 7 ) == 0 ) { ptype = 'l'; IntResult = ISUPPER( p1->StringResult ); } else if( strncmp( Func, "LEN", 3 ) == 0 ) { ptype = 'd'; DoubResult = LEN( p1->StringResult ); } else if( strncmp( Func, "LEFT", 4 ) == 0 ) { ptype = 's'; buf = LEFT( p1->StringResult, INT( p2->DoubResult )); } else if( strncmp( Func, "LTRIM", 5 ) == 0 ) { ptype = 's'; buf = LTRIM( p1->StringResult ); } else if( strncmp( Func, "LOG", 3 ) == 0 ) { ptype = 'd'; DoubResult = LOG( GetDoub( p1 )); } else if( strncmp( Func, "LOWER", 5 ) == 0 ) { ptype = 's'; buf = LOWER( p1->StringResult ); } else if( strncmp( Func, "MAX", 3 ) == 0 ) { ptype = 'd'; DoubResult = MAX( GetDoub( p1 ), GetDoub( p2 )); } else if( strncmp( Func, "MIN", 3 ) == 0 ) { ptype = 'd'; DoubResult = MIN( GetDoub( p1 ), GetDoub( p2 )); } else if( strncmp( Func, "MONTH", 5 ) == 0 ) { ptype = 'd'; DoubResult = MONTH( p1->StringResult ); } else if( strncmp( Func, "RECNO", 5 ) == 0 ) { ptype = 'd'; DoubResult = RECNO( FuncNode->dbf ); } else if( strncmp( Func, "REPLICATE", 9 ) == 0 ) { ptype = 's'; buf = REPLICATE( p1->StringResult, GetInt( p2 )); } else if( strncmp( Func, "RIGHT", 5 ) == 0 ) { ptype = 's'; buf = RIGHT( p1->StringResult, GetInt( p2 )); } else if( strncmp( Func, "RTRIM", 5 ) == 0 ) { ptype = 's'; buf = RTRIM( p1->StringResult ); } else if( strncmp( Func, "SPACE", 5 ) == 0 ) { ptype = 's'; buf = SPACE( INT( GetDoub( p1 ))); } else if( strncmp( Func, "SQRT", 4 ) == 0 ) { ptype = 'd'; DoubResult = SQRT( GetDoub( p1 )); } else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 1 ) { ptype = 's'; buf = STRZERO( p1->StringResult ); } else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 2 ) { ptype = 's'; buf = STRZERO( p1->StringResult, GetInt( p2 )); } else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 3 ) { ptype = 's'; buf = STRZERO( p1->StringResult, GetInt( p2 ), GetInt( p3 )); } else if( strncmp( Func, "STR", 3 ) == 0 && p3 ) { ptype = 's'; if(p1->ExpressionType == 'N') buf = STR( p1->DoubResult, GetInt( p2 ), GetInt( p3 )); else buf = STR( p1->StringResult, GetInt( p2 ), GetInt( p3 )); } else if( strncmp( Func, "STR", 3 ) == 0 && p2 ) { ptype = 's'; buf = STR( p1->StringResult, GetInt( p2 )); } else if( strncmp( Func, "STR", 3 ) == 0 && p1 ) { ptype = 's'; buf = STR( p1->StringResult ); } else if( strncmp( Func, "SUBSTR", 6 ) == 0 ) { ptype = 's'; buf = SUBSTR( p1->StringResult, GetInt( p2 ), GetInt( p3 )); } else if( strncmp( Func, "TRIM", 4 ) == 0 ) { ptype = 's'; buf = TRIM( p1->StringResult ); } else if( strncmp( Func, "UPPER", 5 ) == 0 ) { ptype = 's'; buf = UPPER( p1->StringResult ); } else if( strncmp( Func, "VAL", 3 ) == 0 ) { ptype = 'd'; DoubResult = VAL( p1->StringResult ); } else if( strncmp( Func, "YEAR", 4 ) == 0 ) { ptype = 'd'; DoubResult = YEAR( p1->StringResult ); } if( p1 && !p1->InTree ) delete p1; if( p2 && !p2->InTree ) delete p2; if( p3 && !p3->InTree ) delete p3; if( !FuncNode->InTree ) delete FuncNode; if( buf ){ len = strlen( buf ); WorkNode = new xbExpNode; if( !WorkNode ) return XB_NO_MEMORY; WorkNode->ResultLen = len + 1; } else { len = 0; WorkNode = new xbExpNode; if( !WorkNode ) return XB_NO_MEMORY; WorkNode->ResultLen = 0; } switch( ptype ){ case 's': /* string or char result */ WorkNode->DataLen = len; WorkNode->ExpressionType = 'C'; WorkNode->Type = 's'; WorkNode->StringResult = buf; break; case 'd': /* numeric result */ WorkNode->DataLen = 0; WorkNode->ExpressionType = 'N'; WorkNode->Type = 'd'; WorkNode->DoubResult = DoubResult; break; case 'l': /* logical result */ WorkNode->DataLen = 0; WorkNode->ExpressionType = 'L'; WorkNode->Type = 'l'; WorkNode->IntResult = IntResult; break; default: std::cout << "\nInternal error. " << ptype; break; } Push(WorkNode); return XB_NO_ERROR; }
WHERE gs.identnr = :net AND va.identnr = :var AND \ tv1.id_tabella = gs.id_tabella_vm AND tv2.id_tabella = va.id_tabella_vm"; */ /* versione piu` semplice che non richiede la rete static char *tabquery = (char *) "SELECT \ RTRIM(tv.nome_tabella) FROM met_variabili_definite va, met_tabelle_vm tv \ WHERE va.identnr = :var AND tv.id_tabella = va.id_tabella_vm"; */ /* terza versione che richiede solo la rete, potrebbe non funzionare con la rete CAELAMI, ma per ora me ne frego */ static char *tabquery = (char *) "SELECT \ RTRIM(tv.nome_tabella) FROM met_gruppi_stazioni gs, met_tabelle_vm tv \ WHERE gs.identnr = :net AND tv.id_tabella = gs.id_tabella_vm"; static char *netquery = (char *) "SELECT G.IDENTNR FROM MET_GRUPPI_STAZIONI G WHERE G.DESCRIZIONE = :netdesc"; static char *anaquery = (char *) "SELECT NVL(st.identnr,-9999), \ NVL(pm.x_long_cent,-999.9), NVL(pm.y_lat_cent,-999.9), \ NVL(st.z_quota_stazione,-9999.9), NVL(st.z_quota_pozzetto,-9999.9), \ SUBSTR(NVL(st.nome,'S. Cresci in Valcava'),1,20), \ SUBSTR(gs.descrizione,1,20) \ FROM met_stazioni_misura st, met_punti_misura pm, met_gruppi_stazioni gs \ WHERE st.gsta_identnr = :net AND pm.identnr(+) = st.pmis_identnr AND \ gs.identnr = :net \ ORDER by upper(st.identnr)";
///============================================================================= void TRIM(std::string& s, const char *sch) { LTRIM(s, sch); RTRIM(s, sch); }
int main(int argc, char **argv) { int opt, count = 1; pthread_t thread; /* FIXME */ ip = dstr_new("127.0.0.1"); port = 33330; while ((opt = getopt(argc, argv, "h:p:xt?")) != -1) switch (opt) { case 'h': dstr_free(ip); ip = dstr_new(optarg); count += 2; break; case 'p': port = atoi(optarg); count += 2; break; case 'x': execute = 1; count += 1; break; case 't': timestamp = 1; count += 1; break; case '?': default: usage(); } if ((tcpsock = net_tcp_nonblock_connect(ip, port, neterr, sizeof neterr)) == -1) { fprintf(stderr, "Connecting %s:%d: %s\n", ip, port, neterr); exit(1); } if (errno == EINPROGRESS) { struct pollfd wfd[1]; int res, err = 0; socklen_t errlen = sizeof err; wfd[0].fd = tcpsock; wfd[0].events = POLLOUT; /* wait for 5 seconds */ if ((res = poll(wfd, 1, 5000)) == -1) { fprintf(stderr, "Connecting %s:%d: %s\n", ip, port, strerror(errno)); exit(1); } else if (res == 0) { errno = ETIMEDOUT; fprintf(stderr, "Connecting %s:%d: %s\n", ip, port, strerror(errno)); exit(1); } if (getsockopt(tcpsock, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { fprintf(stderr, "Connecting %s:%d: %s\n", ip, port, strerror(errno)); exit(1); } if (err) { errno = err; fprintf(stderr, "Connecting %s:%d: %s\n", ip, port, strerror(errno)); exit(1); } } if (execute && pipe(proceed_pipe) != 0) { fprintf(stderr, "Error creating pipe: %s\n", strerror(errno)); exit(1); } if (execute) { argc -= count; argv += count; if (argc > 0) { dstr cmd; int i; struct pollfd rfd[1]; if (pthread_create(&thread, NULL, recv_thread, NULL) != 0) { fprintf(stderr, "Error initializing receiver thread\n"); exit(1); } cmd = dstr_new(argv[0]); for (i = 1; i < argc; ++i) { cmd = dstr_cat(cmd, " "); cmd = dstr_cat(cmd, argv[i]); } cmd = dstr_cat(cmd, "\r\n"); net_try_write(tcpsock, cmd, dstr_length(cmd), 100, NET_NONBLOCK); /* dstr_free(cmd); */ rfd[0].fd = proceed_pipe[0]; rfd[0].events = POLLIN; if (poll(rfd, 1, -1) == -1) { fprintf(stderr, "Error polling: %s\n", strerror(errno)); exit(1); } } } else { fprintf(stdout, "XCUBE CLI, Copyright (c) 2013-2015, " "Dalian Futures Information Technology Co., Ltd.\n"); fprintf(stdout, "Type 'help' or '?' for help.\n"); init_readline(); stifle_history(100); if (pthread_create(&thread, NULL, recv_thread, NULL) != 0) { fprintf(stderr, "Error initializing receiver thread\n"); exit(1); } while (loop) { char *line; line = readline(prompt); if (rl_inited == 0) rl_inited = 1; if (line == NULL) continue; LTRIM(line); RTRIM(line); if (*line) { add_history(line); if (tcpsock == -1) execute_line(line); else { net_try_write(tcpsock, line, strlen(line), 100, NET_NONBLOCK); net_try_write(tcpsock, "\r\n", 2, 100, NET_NONBLOCK); if (!strncasecmp(line, "quit", 4)) com_quit(NULL); } } FREE(line); } } return 0; }