double evaluate(std::string expression) { init(); m_original = expression; char *source = (char*)m_original.c_str(); return parse_commands(source); }
static void run_command(const struct command *cmds, char **av) { const struct command *cmd = find_command(cmds, av[0]); const char *pf; char **args; if (!cmd) { PTR_ARRAY(array); const char *alias_name = av[0]; const char *alias_value = find_alias(alias_name); struct error *err = NULL; int i; if (alias_value == NULL) { error_msg("No such command or alias: %s", alias_name); return; } if (!parse_commands(&array, alias_value, &err)) { error_msg("Parsing alias %s: %s", alias_name, err->msg); error_free(err); ptr_array_free(&array); return; } /* remove NULL */ array.count--; for (i = 1; av[i]; i++) ptr_array_add(&array, xstrdup(av[i])); ptr_array_add(&array, NULL); run_commands(cmds, &array); ptr_array_free(&array); return; } if (config_file && cmds == commands && !allowed_command(cmd->name)) { error_msg("Command %s not allowed in config file.", cmd->name); return; } // By default change can't be merged with previous on. // Any command can override this by calling begin_change() again. begin_change(CHANGE_MERGE_NONE); current_command = cmd; args = av + 1; pf = parse_args(args, cmd->flags, cmd->min_args, cmd->max_args); if (pf) cmd->cmd(pf, args); current_command = NULL; end_change(); }
int glkunix_startup_code(glkunix_startup_t *data) { set_defaults(); #ifdef USE_GARGLK_FEATURES garglk_set_program_name("Nitfol 0.5"); garglk_set_program_info( "Nitfol 0.5 by Evin Robertson\n" "With countless patches by other people.\n"); #endif return parse_commands(data->argc, data->argv); }
int main(void) { char *line=(char *)malloc(1024*sizeof(char)); char *argv[64]; while (1) { printf("Custom_Shell$"); printf(" "); gets(line); //printf("\n"); command commLine[6]; int commands=parse_commands(line,commLine); execute(commands,commLine); } }
void handle_command(const struct command *cmds, const char *cmd) { struct error *err = NULL; PTR_ARRAY(array); if (!parse_commands(&array, cmd, &err)) { error_msg("%s", err->msg); error_free(err); ptr_array_free(&array); return; } run_commands(cmds, &array); ptr_array_free(&array); }
int process_file(std::string input_filename) { bool ok = true; try { environment temp_env(env); io_state temp_ios(ios); if (!parse_commands(temp_env, temp_ios, input_filename.c_str(), optional<std::string>(), false, num_threads)) { ok = false; } } catch (lean::exception & ex) { simple_pos_info_provider pp(input_filename.c_str()); ok = false; lean::display_error(diagnostic(env, ios), &pp, ex); } return ok ? 0 : 1; }
int cmd_do_repeat (struct lexer *lexer, struct dataset *ds) { struct hmap dummies; bool ok; if (!parse_specification (lexer, dataset_dict (ds), &dummies)) return CMD_CASCADING_FAILURE; ok = parse_commands (lexer, &dummies); destroy_dummies (&dummies); return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE; }
double parse_atom(char *& expression) { FAIL_CHECK; skip_spaces(expression); bool negative = false; if (*expression == '-') { negative = true; expression++; } if (*expression == '+') { expression++; } // PAR-EN-THESIS if (*expression == ')' && !m_parenthesis) { RETURN_FAILURE(NO_OPEN_PARENTHESES, expression); } if (*expression == '(') { expression++; m_parenthesis++; double result = parse_commands(expression); if (*expression != ')') { RETURN_FAILURE(NO_CLOSE_PARENTHESES, expression); } expression++; m_parenthesis--; return negative ? -result : result; } // convert the string to a double, gives point in the string where the number ends char *end_ptr = nullptr; double res = strtod(expression, &end_ptr); expression = end_ptr; return negative ? -res : res; }
int main(int argc, const char * argv[]) { UsbParser* parser; int status = parse_commands(&parser, argc, argv); if(status == OK_PARAMS) // parsed valid data { if(test_selected(parser)) // test parser { print_parsed_data(parser); }else if(help_selected(parser)) // show help { print_help(); }else if(list_selected(parser)) // list usb devices { UsbDevice** devices; list_usb_devices(&devices); print_list_log(devices, 0); }else // control requests { UsbResponse** responses; int response = setup_packet(parser, &responses); if(response == 0) print_responses_log(responses, OK_RESPONSES); else if(response == LIBUSB_INIT_FAILED) print_usb_error(LIBUSB_INIT_FAILED); else if(response == NO_DEVICE_PRESENT) print_usb_error(NO_DEVICE_PRESENT); else if(response == INTERFACE_NOT_CLAIMED) print_usb_error(INTERFACE_NOT_CLAIMED); } }else { print_parsing_error(status); } return 0; }
int parse_doc(xmlDocPtr doc) { xmlNodePtr cur; cur = xmlDocGetRootElement(doc); if(cur == NULL) { llog(LOG_ERR, "Empty document"); return 0; } if(xmlStrcmp(cur->name, (const xmlChar *) "script")) { llog(LOG_ERR, "Document of the wrong type"); return 0; } cur = cur->xmlChildrenNode; while(cur != NULL) { if((!xmlStrcmp(cur->name, (const xmlChar *)"commands"))) { parse_commands(doc, cur); } cur = cur->next; } return 1; }
//Read the string and execute instructions void process_string(uint8_t *instruction) { uint8_t code; uint16_t k; float temp; //command commands = NULL; FloatPoint fp; //the character / means delete block... used for comments and stuff. if (instruction[0] == '/') { Serial.println("ok"); return; } enable_steppers(); purge_commands(); //clear old commands parse_commands(instruction); //create linked list of arguments if (command_exists('G')) { code = getValue('G'); switch(code) { case 0: //Rapid Motion setXYZ(&fp); set_target(&fp); r_move(0); //fast motion in all axis break; case 1: //Coordinated Motion setXYZ(&fp); set_target(&fp); if (command_exists('F')) _feedrate = getValue('F'); //feedrate persists till changed. r_move( _feedrate ); break; case 2://Clockwise arc case 3://Counterclockwise arc FloatPoint cent; float angleA, angleB, angle, radius, length, aX, aY, bX, bY; //Set fp Values setXYZ(&fp); // Centre coordinates are always relative cent.x = xaxis->current_units + getValue('I'); cent.y = yaxis->current_units + getValue('J'); aX = (xaxis->current_units - cent.x); aY = (yaxis->current_units - cent.y); bX = (fp.x - cent.x); bY = (fp.y - cent.y); if (code == 2) { // Clockwise angleA = atan2(bY, bX); angleB = atan2(aY, aX); } else { // Counterclockwise angleA = atan2(aY, aX); angleB = atan2(bY, bX); } // Make sure angleB is always greater than angleA // and if not add 2PI so that it is (this also takes // care of the special case of angleA == angleB, // ie we want a complete circle) if (angleB <= angleA) angleB += 2 * M_PI; angle = angleB - angleA; radius = sqrt(aX * aX + aY * aY); length = radius * angle; int steps, s, step; steps = (int) ceil(length / curve_section); FloatPoint newPoint; for (s = 1; s <= steps; s++) { step = (code == 3) ? s : steps - s; // Work backwards for CW newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps)); newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps)); newPoint.z = zaxis->current_units; set_target(&newPoint); // Need to calculate rate for each section of curve feedrate_micros = (feedrate > 0) ? feedrate : getMaxFeedrate(); // Make step r_move(feedrate_micros); } break; case 4: //Dwell //delay((int)getValue('P')); break; case 20: //Inches for Units _units[0] = X_STEPS_PER_INCH; _units[1] = Y_STEPS_PER_INCH; _units[2] = Z_STEPS_PER_INCH; curve_section = CURVE_SECTION_INCHES; calculate_deltas(); break; case 21: //mm for Units _units[0] = X_STEPS_PER_MM; _units[1] = Y_STEPS_PER_MM; _units[2] = Z_STEPS_PER_MM; curve_section = CURVE_SECTION_MM; calculate_deltas(); break; case 28: //go home. set_target(&zeros); r_move(getMaxFeedrate()); break; case 30://go home via an intermediate point. //Set Target setXYZ(&fp); set_target(&fp); //go there. r_move(getMaxFeedrate()); //go home. set_target(&zeros); r_move(getMaxFeedrate()); break; case 81: // drilling operation temp = zaxis->current_units; //Move only in the XY direction setXYZ(&fp); set_target(&fp); zaxis->target_units = temp; calculate_deltas(); r_move(getMaxFeedrate()); //Drill DOWN zaxis->target_units = getValue('Z') + ((abs_mode) ? 0 : zaxis->current_units); calculate_deltas(); r_move(getMaxFeedrate()); //Drill UP zaxis->target_units = temp; calculate_deltas(); r_move(getMaxFeedrate()); case 90://Absolute Positioning abs_mode = true; break; case 91://Incremental Positioning abs_mode = false; break; case 92://Set as home set_position(&zeros); break; case 93://Inverse Time Feed Mode break; //TODO: add this case 94://Feed per Minute Mode break; //TODO: add this default: Serial.print("huh? G"); Serial.println(code,DEC); } } if (command_exists('M')) { code = getValue('M'); switch(code) { case 3: // turn on motor case 4: motor_on(); break; case 5: // turn off motor motor_off(); break; case 82: DDRC |= _BV(1); PORTC &= ~_BV(1); DDRC &= ~_BV(0); PORTC |= _BV(0); // setup initial position for (int i=0; i<20; i++) { k=0; PORTB |= _BV(5); //go down while(PINC & _BV(0)) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); k++; } //print result for this point Serial.println(k,DEC); PORTB &= ~_BV(5); //move up to origin while (k--) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(12.5*stepping); } } break; case 81: DDRC |= _BV(1); PORTC &= ~_BV(1); DDRC &= ~_BV(0); PORTC |= _BV(0); while(1) { if (PINC & _BV(0)) Serial.println("high"); else Serial.println("low"); } break; case 80: //plot out surface of milling area DDRC |= _BV(1); PORTC &= ~_BV(1); DDRC &= ~_BV(0); PORTC |= _BV(0); // setup initial position fp.x = 0; fp.y = 0; fp.z = 0; set_target(&fp); set_position(&fp); r_move(0); for (int i=0; i<160; i+=2) { for (float j=0; j<75; j+=2) { fp.x=i; fp.y=j; fp.z=0; set_target( &fp ); r_move( 0 ); k=0; PORTB &= ~(_BV(5)); //go down while(PINC & _BV(0)) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); k++; } //print result for this point Serial.print(i,DEC); Serial.print(","); Serial.print(j,DEC); Serial.print(","); Serial.println(k,DEC); PORTB |= _BV(5); //move up to origin while (k--) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); } } } break; case 90: //plot out surface of milling area DDRC |= _BV(1); PORTC &= ~_BV(1); DDRC &= ~_BV(0); PORTC |= _BV(0); // setup initial position fp.x = 0; fp.y = 135; fp.z = 0; set_target(&fp); set_position(&fp); r_move(0); for (int i=0; i<1; i++) { for (float j=135; j!=0; j-=.25) { fp.x=i; fp.y=j; fp.z=0; set_target( &fp ); r_move( 0 ); k=0; PORTB |= _BV(5); //go down while(PINC & _BV(0)) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); k++; } //print result for this point Serial.print(i,DEC); Serial.print(","); Serial.print(j,DEC); Serial.print(","); Serial.println(k,DEC); PORTB &= ~_BV(5); //move up to origin while (k--) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); } } } break; case 98: //M98 Find Z0 where it is one step from touching. DDRC |= _BV(1); PORTC &= ~_BV(1); DDRC &= ~_BV(0); PORTC |= _BV(0); PORTB |= _BV(5); while(PINC & _BV(0)) { PORTB |= _BV(2); delayMicroseconds(1); PORTB &= ~_BV(2); delayMicroseconds(200); } break; case 99: //M99 S{1,2,4,8,16} -- set stepping mode if (command_exists('S')) { code = getValue('S'); if (code == 1 || code == 2 || code == 4 || code == 8 || code == 16) { stepping = code; setStep(stepping); break; } } default: Serial.print("huh? M"); Serial.println(code,DEC); } } Serial.println("ok");//tell our host we're done. }
int main(int argc, char **argv) { struct ifaddrs *ifaddr; struct ifaddrs *ifa; // struct sockaddr_ll *saddr; sockinfo_t *saddr; int family; char host[NI_MAXHOST]; char mac[20]; struct _config_s config; unsigned char byte; char *pos; int len; int i; memset(&config, 0, sizeof(config)); if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(EXIT_FAILURE); } parse_commands(argc, argv, &config); /* Walk through linked list, maintaining head pointer so we can free list later */ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) continue; family = ifa->ifa_addr->sa_family; /* Display interface name and family (including symbolic form of the latter for the common families) */ if (config.iface != NULL) { // user specified an inteface of intrest. skip all others. if (strcmp(config.iface, ifa->ifa_name) !=0 ) continue; } if (family == AF_PACKET && config.flags & FLAG_SHOW_MAC) { saddr = (sockinfo_t*)ifa->ifa_addr; memset(mac, 0 , sizeof(mac)); len = 0; #ifdef __linux // skip loopback devices. they dont have a hwaddr if (ifa->ifa_flags & IFF_LOOPBACK) continue; // saddr = (struct sockaddr_ll*)ifa->ifa_addr; for(i = 0;i < 6;i++) { byte = (unsigned char)saddr->sll_addr[i]; len += sprintf(mac + len, "%02X%s", byte, i < 5 ? ":":""); } #endif #ifdef __FreeBSD__ // skip loopback devices. they dont have a hwaddr if (saddr->sdl_type == IFT_LOOP) continue; for(i = 0;i < 6;i++) { byte = (unsigned char)LLADDR(saddr)[i]; len += sprintf(mac + len, "%02X%s", byte, i < 5 ? ":":""); } #endif printf("%s %s\n", ifa->ifa_name, mac); } if (family == AF_INET || family == AF_INET6) { if ((family == AF_INET) && !(config.flags & FLAG_SHOW_IPV4)) continue; if ((family == AF_INET6) && !(config.flags & FLAG_SHOW_IPV6)) continue; pos = NULL; memset(host, 0, sizeof(host)); // TODO: check return value and handle gracefully getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); // linux inserts a %<ifname> suffix on IPv6 addresses. trim out. pos = strchr(host, '%'); if(pos != NULL) *pos = '\0'; printf("%s %s\n", ifa->ifa_name, host); } } freeifaddrs(ifaddr); exit(EXIT_SUCCESS); }