static int bool_set(conf_entry_t *var, const char *s) { char *cp; /* source */ if (var->p == NULL) { var->p = malloc(sizeof(int)); } cp = (char *) s; LTRIM(cp); if (!strncasecmp(cp, "TRUE", 4) || !strncasecmp(cp, "YES", 3) || !strncasecmp(cp, "ON", 2) || !strncasecmp(cp, "1", 1)) { *((bool *) var->p) = true; return 1; } if (!strncasecmp(cp, "FALSE", 5) || !strncasecmp(cp, "NO", 2) || !strncasecmp(cp, "OFF", 3) || !strncasecmp(cp, "0", 1)) { *((bool *) var->p) = false; return 1; } return 0; }
static int char_set(conf_entry_t *var, const char *s) { char *cp; /* source */ if (var->p == NULL) { var->p = malloc(sizeof(char)); } cp = (char *) s; LTRIM(cp); //printf("*** %s: var '%s' source='%s'\n", __FUNCTION__, var->name, cp); sscanf(cp, "%c", (char *) (var->p)); return 1; }
/* Fetch a value from its key. */ void command_get(cli_t *cli, char *pt) { char *pt2, *key; ybin_t bkey, bdata; int rc; LTRIM(pt); if (*pt != '"') { printf_decorated("faint", "Bad key format (no quote)"); printf("\n"); return; } pt++; if (!*pt) { printf_decorated("faint", "Bad key"); printf("\n"); return; } key = pt2 = pt; pt2 = pt; if ((pt2 = strchr(pt2, '"')) == NULL) { printf_decorated("faint", "Bad key format (no trailing quote)"); printf("\n"); return; } *pt2 = '\0'; // check connection if needed if (!check_connection(cli)) return; // request bzero(&bdata, sizeof(bdata)); ybin_set(&bkey, key, strlen(key)); rc = finedb_get(cli->finedb, bkey, &bdata); if (rc) { printf_color("red", "Unable to get key '%s' (%d).", key, rc); printf("\n"); return; } if (bdata.data == NULL || bdata.len == 0) { printf_decorated("faint", "No data."); printf("\n"); } printf("%s\n", (char*)bdata.data); }
static int string_set(conf_entry_t *var, const char *s) { char *cp; /* source */ int len; char quote; char *ep; if ((!s) || (!var)) return 0; cp = (char *) s; LTRIM(cp); if ((*cp == '"') || (*cp == '\'')) { quote = *cp; cp++; if ((ep = strchr(cp, quote)) != NULL) len = ep - cp; else { #ifdef DEBUG fprintf(stderr, "Unterminated string.\n"); #endif len = 0; } } else { len = strlen(cp); } if (var->p == NULL) { var->p = malloc(len + 1); } if (var->len > 0) len = MIN(len, var->len); strncpy((char *) (var->p), cp, len); cp = (char *)(var->p); cp[len] = '\0'; return 1; }
/* Delete a key. */ void command_del(cli_t *cli, char *pt) { char *pt2, *key; ybin_t bkey; int rc; LTRIM(pt); if (*pt != '"') { printf_decorated("faint", "Bad key format (no quote)"); printf("\n"); return; } pt++; if (!*pt) { printf_decorated("faint", "Bad key"); printf("\n"); return; } key = pt2 = pt; pt2 = pt; if ((pt2 = strchr(pt2, '"')) == NULL) { printf_decorated("faint", "Bad key format (no trailing quote)"); printf("\n"); return; } *pt2 = '\0'; // check connection if needed if (!check_connection(cli)) return; // request ybin_set(&bkey, key, strlen(key)); rc = finedb_del(cli->finedb, bkey); if (rc) printf_color("red", "Unable to delete key '%s'.", key); printf("\n"); printf_decorated("faint", "OK"); printf("\n"); }
/* Main function. */ int main(int argc, char *argv[]) { cli_t cli; char *hostname = DEFAULT_HOST; char history_file[4096]; ybool_t interactive_mode = YTRUE; bzero(&cli, sizeof(cli_t)); cli.autocheck = YTRUE; if (argc == 2 && argv[1][0] != '-') hostname = argv[1]; if (argc == 3 && !strcmp(argv[2], "-")) interactive_mode = YFALSE; // init database connection if ((cli.finedb = finedb_create(hostname, 11138)) == NULL) { printf_color("red", "Memory error."); printf("\n"); exit(1); } if (finedb_connect(cli.finedb) != FINEDB_OK) { printf_color("red", "Unable to connect to server '%s' on port '%d'.", argv[1], 11138); printf("\n"); exit(2); } // interactive mode init if (interactive_mode) { char *home = NULL; if ((home = getenv("HOME")) != NULL) { FILE *hist; snprintf(history_file, sizeof(history_file), "%s/%s", home, HISTORY_FILE); if ((hist = fopen(history_file, "w+")) != NULL) { fclose(hist); linenoiseHistoryLoad(HISTORY_FILE); } linenoiseSetCompletionCallback(cli_completion); } } // main loop for (; ; ) { char buff[4096], *line = NULL, *pt, *cmd; if (!interactive_mode) { ssize_t bufsz, linesz = 0; while ((bufsz = read(0, buff, sizeof(buff))) > 0) { pt = (char*)malloc(linesz + bufsz + 1); memcpy(pt, line, linesz); memcpy((void*)((size_t)pt + linesz), buff, bufsz); linesz += bufsz; pt[linesz] = '\0'; if (line) free(line); line = pt; } } else { snprintf(buff, sizeof(buff), "%s > ", (cli.dbname ? cli.dbname : "default")); if ((line = linenoise(buff)) == NULL) break; } pt = line; LTRIM(pt); cmd = pt; // add command line to history linenoiseHistoryAdd(cmd); linenoiseHistorySave(history_file); // extract the command while (*pt && !IS_SPACE(*pt)) ++pt; *pt++ = '\0'; LTRIM(pt); /* command management */ if (cmd[0] == '\0') goto reloop; //continue; // local commands, no need for a running connection if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit")) exit(0); if (!strcasecmp(cmd, "help") || cmd[0] == '?') { command_help(); goto reloop; //continue; } else if (!strcasecmp(cmd, "sync")) { command_sync(&cli); goto reloop; //continue; } else if (!strcasecmp(cmd, "async")) { command_async(&cli); goto reloop; //continue; } // commands that need a running connection if (!strcasecmp(cmd, "use")) command_use(&cli, pt); else if (!strcasecmp(cmd, "get")) command_get(&cli, pt); else if (!strcasecmp(cmd, "del")) command_del(&cli, pt); else if (!strcasecmp(cmd, "put")) command_send_data(&cli, pt, YFALSE, YFALSE); else if (!strcasecmp(cmd, "add")) command_send_data(&cli, pt, YTRUE, YFALSE); else if (!strcasecmp(cmd, "update")) command_send_data(&cli, pt, YFALSE, YTRUE); else if (!strcasecmp(cmd, "inc")) command_inc(&cli, pt); else if (!strcasecmp(cmd, "dec")) command_dec(&cli, pt); else if (!strcasecmp(cmd, "start")) command_start(&cli); else if (!strcasecmp(cmd, "stop")) command_stop(&cli); #if 0 else if (!strcasecmp(cmd, "list")) command_list(&cli, pt); #endif else if (!strcasecmp(cmd, "ping")) command_ping(&cli); else if (!strcasecmp(cmd, "autocheck")) command_autocheck(&cli, pt); else { printf_color("red", "Bad command."); printf("\n"); } reloop: free(line); } return (0); }
/* List the keys stored in database. */ void command_list(cli_t *cli, char *pt) { char *buffer, c; size_t sz, offset, length, rc; LTRIM(pt); // create sending buffer sz = 1; if (cli->dbname) sz += 1 + strlen(cli->dbname); buffer = YMALLOC(sz); // set the code byte buffer[0] = PROTO_LIST; if (cli->dbname) buffer[0] = REQUEST_ADD_DBNAME(buffer[0]); // dbname offset = 1; if (cli->dbname) { length = strlen(cli->dbname); buffer[offset] = (char)length; offset++; memcpy(&buffer[offset], cli->dbname, length); offset += length; } { size_t n; for (n = 0; n < sz; n++) printf("%02x ", buffer[n]); printf("\n"); } // send data rc = write(cli->fd, buffer, sz); YFREE(buffer); if (rc != sz) { printf("%c[Connection error%c[0m\n", 27, 27); return; } // get response if (read(cli->fd, &c, 1) != 1) { printf("%c[Connection error%c[0m\n", 27, 27); return; } if (RESPONSE_CODE(c) != RESP_OK) { printf("%c[2mERROR: %s%c[0m\n", 27, (RESPONSE_CODE(c) == RESP_PROTO ? "protocol" : (RESPONSE_CODE(c) == RESP_SERVER_ERR ? "server" : (RESPONSE_CODE(c) == RESP_NO_DATA ? "no data" : "unknown"))), 27); return; } printf("%c[2mOK%c[0m\n", 27, 27); for (; ; ) { uint16_t ln = 0, lh; if (read(cli->fd, &ln, 2) < 2) break; lh = ntohs(ln); buffer = YMALLOC(lh + 1); if (read(cli->fd, buffer, lh) != lh) { printf("%c[Connection error%c[0m\n", 27, 27); YFREE(buffer); return; } printf("%c[2m%s%c[0m\n", 27, buffer, 27); YFREE(buffer); } }
/* Put, add or update a key/value in database. */ void command_send_data(cli_t *cli, char *pt, ybool_t create_only, ybool_t update_only) { char *pt2, *key, *data; ybin_t bkey, bdata; int rc; LTRIM(pt); if (*pt != '"') { printf_decorated("faint", "Bad key format (no quote)"); printf("\n"); return; } pt++; if (!*pt) { printf_decorated("faint", "Bad key"); printf("\n"); return; } key = pt2 = pt; pt2 = pt; if ((pt2 = strchr(pt2, '"')) == NULL) { printf_decorated("faint", "Bad key format (no trailing quote)"); printf("\n"); return; } *pt2 = '\0'; pt2++; if (!*pt2) { printf_decorated("faint", "Missing data"); printf("\n"); return; } *pt2 = '\0'; data = pt2 + 1; LTRIM(data); if (*data != '"') { printf_decorated("faint", "Bad data format (no quote)"); printf("\n"); return; } data++; if (!*data) { printf_decorated("faint", "Missing data"); printf("\n"); return; } pt2 = data + strlen(data) - 1; if (*pt2 != '"') { printf_decorated("faint", "Bad data format (no trailing quote)"); printf("\n"); return; } *pt2 = '\0'; // check connection if needed if (!check_connection(cli)) return; // request ybin_set(&bkey, key, strlen(key)); ybin_set(&bdata, data, strlen(data)); if (create_only) rc = finedb_add(cli->finedb, bkey, bdata); else if (update_only) rc = finedb_update(cli->finedb, bkey, bdata); else rc = finedb_put(cli->finedb, bkey, bdata); if (rc) printf_color("red", "Unable to %s key '%s'.", (create_only ? "add" : (update_only ? "update" : "put")), key); else printf_decorated("faint", "OK"); printf("\n"); }
/*! \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; }
///============================================================================= void TRIM(std::string& s, const char *sch) { LTRIM(s, sch); RTRIM(s, sch); }
void main(){ //TRISD = 0x00; //PORTD = 0b00000000; // CMCON| = 7; UART1_Init(9600); // Initialize UART module at 9600 bps //TRISA = 0xFF; //PORTA as input ADCON1 = 0b00001001; // Set AN0 channel pin as analog CMCON |= 7; // Disable comparators //X=0; ADC_init(); //UART1_Init(9600); //Lcd_Init(); /* Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1,"W"); delay1(); Lcd_Out(1,2,"E"); delay1(); Lcd_Out(1,3,"L"); delay1(); Lcd_Out(1,4,"C"); delay1(); Lcd_Out(1,5,"O"); delay1(); Lcd_Out(1,6,"M"); delay1(); Lcd_Out(1,7,"E"); delay1(); delay1(); Lcd_Out(2,1,"I"); delay2(); Lcd_out(2,2,"N"); delay2(); Lcd_Out(2,3,"I"); delay2(); Lcd_Out(2,4,"T"); delay2(); Lcd_Out(2,5,"I"); delay2(); Lcd_Out(2,6,"A"); delay2(); Lcd_Out(2,7,"L"); delay2(); Lcd_Out(2,8,"I"); delay2(); Lcd_Out(2,9,"Z"); delay2(); Lcd_Out(2,10,"I"); delay2(); Lcd_Out(2,11,"N"); delay2(); Lcd_Out(2,12,"G"); delay2(); Lcd_Out(2,13,"."); delay2(); Lcd_Out(2,14,"."); delay2(); Lcd_Out(2,15,"."); delay2(); delay1(); delay1(); delay_ms(350); lcd_cmd(_LCD_CLEAR); Lcd_Out(1,1, "Input char:"); */ while(1) { hold0 = adc_read(0)*0.4886; //0.406 delay_ms(12); hold1 = adc_read(1)*0.4886; delay_ms(12); hold2 = adc_read(2)*0.4886; delay_ms(12); hold3 = adc_read(3)*0.4886; delay_ms(12); hold4 = adc_read(4)*0.4886; delay_ms(12); hold5 = adc_read(5)*0.4886; delay_ms(12); intToStr(hold0, data1); LTRIM(data1); intToStr(hold1, data2); LTRIM(data2); intToStr(hold2, data3); LTRIM(data3); intToStr(hold3, data4); LTRIM(data4); intToStr(hold4, data5); LTRIM(data5); intToStr(hold5, data6); LTRIM(data6); //delay_us(10); //LCD_Chr(1,1,5*hold/1024+48); //LCD_Chr(1,2,46); //LCD_Chr(1,1,(50*hold/1024)%10+48); //LCD_Chr(1,2,(20*hold/41)%10+48); //20/41 =~ 500/1024 //LCD_Chr(1,3,0xdf); //LCD_out(1,4,"C"); //LCD_Out(2,1,"RAW ADC ="); //Lcd_Out(2,1,txt); //LTRIM(txt); //RTRIM(txt); //sprintf(txt,0.1f,hold); UART1_Write_text("Data1"); UART1_Write_text(" "); UART1_Write_text(data1); UART1_Write(0x0D); UART1_Write(0x0A); //delay_ms(1); UART1_Write_text("Data2"); UART1_Write_text(" "); UART1_Write_text(data2); UART1_Write(0x0D); UART1_Write(0x0A); //delay_ms(1); UART1_Write_text("Data3"); UART1_Write_text(" "); UART1_Write_text(data3); UART1_Write(0x0D); UART1_Write(0x0A); UART1_Write_text("Data4"); UART1_Write_text(" "); UART1_Write_text(data4); UART1_Write(0x0D); UART1_Write(0x0A); //delay_ms(1); UART1_Write_text("Data5"); UART1_Write_text(" "); UART1_Write_text(data5); UART1_Write(0x0D); UART1_Write(0x0A); //delay_ms(1); UART1_Write_text("Data6"); UART1_Write_text(" "); UART1_Write_text(data6); UART1_Write(0x0D); UART1_Write(0x0A); UART1_Write(0x0D); UART1_Write(0x0A); Wait(); X=0; //delay_ms(4000); /* if (UART1_Data_Ready() == 1) { uart_rd = UART1_Read(); //UART1_Write(uart_rd); temp = uart_rd; switch (temp) { case 'a':portd=0b00000010; delay_ms(450); portd=0b00000000; break; case 's':portd=0b00000100; delay_ms(450); portd=0b00000000; break; case 'd':portd=0b00001000; delay_ms(450); portd=0b00000000; break; case 'f':portd=0b00010000; delay_ms(450); portd=0b00000000; break; case 'k':for (X=0;X<=5;X++) aku(); break; } } */ } }
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; }