static int register_function (lua_State *L) { const char* fnname = luaL_checkstring(L, 1); // old code using aliases //const char* fmt="luabash call %s "; //char* fullname=(char*) malloc(strlen(fmt)+strlen(fnname)); //sprintf(fullname, fmt, fnname); //add_alias(fnname, fullname); WORD_LIST* wluabash=(WORD_LIST*) malloc(sizeof(WORD_LIST)); WORD_LIST* wcall=(WORD_LIST*) malloc(sizeof(WORD_LIST)); WORD_LIST* wfnname=(WORD_LIST*) malloc(sizeof(WORD_LIST)); WORD_LIST* warguments=(WORD_LIST*) malloc(sizeof(WORD_LIST)); wluabash->next = wcall; wcall->next=wfnname; wfnname->next=warguments; warguments->next=0; wluabash->word=make_word("luabash"); wcall->word=make_word("call"); wfnname->word=make_word(fnname); warguments->word=make_word("$@"); SIMPLE_COM* call_luabash=newSimpleCom(wluabash); COMMAND* function_body=make_command(cm_simple, call_luabash); bind_function(fnname,function_body); return 0; }
static int __init usb_eth_init(void) { int rc; #ifndef DMA_NO_COPY dmabuf = kmalloc( usb_rsize, GFP_KERNEL | GFP_DMA ); if (!dmabuf) return -ENOMEM; #endif strncpy(usb_eth_device.name, usb_eth_name, IFNAMSIZ); usb_eth_device.init = usb_eth_probe; if (register_netdev(&usb_eth_device) != 0) return -EIO; rc = sa1100_usb_open( "usb-eth" ); if ( rc == 0 ) { string_desc_t * pstr; desc_t * pd = sa1100_usb_get_descriptor_ptr(); pd->b.ep1.wMaxPacketSize = make_word( usb_rsize ); pd->b.ep2.wMaxPacketSize = make_word( usb_wsize ); pd->dev.idVendor = ETHERNET_VENDOR_ID; pd->dev.idProduct = ETHERNET_PRODUCT_ID; pstr = sa1100_usb_kmalloc_string_descriptor( "SA1100 USB NIC" ); if ( pstr ) { sa1100_usb_set_string_descriptor( 1, pstr ); pd->dev.iProduct = 1; } rc = sa1100_usb_start(); } return rc; }
void parser_a_file(Queue *queue, const char *file) { FILE *fp; char *ret, *tmp; char buffer[4096]; fp = fopen(file, "r"); if (fp == NULL) { perror("testing"); exit(0); } assert(fp); while(fgets(buffer, sizeof(buffer), fp) != NULL) { tmp = buffer; string_filter(buffer); while((ret = strsep(&tmp, " ")) != NULL) { if (strlen(ret) > 0) { word_t *w; if ((w = queue_find(queue, ret, com_find)) != NULL) { w->counter ++; } else { w = make_word(ret); queue_enqueue(queue, w); } } } } fclose(fp); }
dict_t *make_dict(char *word) { dict_t *nd = (dict_t *) malloc( sizeof(dict_t) ); nd->word = make_word( word ); nd->count = 1; nd->next = NULL; return nd; }
bool load_words(const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) { return false; } const int buf_len = 48; char buf[buf_len]; char *ptr; while(fgets(buf, buf_len, fp)) { // NOTE: there SHOULD be a newline at the end of the file buf[strlen(buf)-1] = '\0'; // truncate the newline ptr = strchr(buf, ' '); if (!ptr) continue; // illegal input *ptr = '\0'; add(make_word(ptr+1, atoi(buf), 0)); } fclose(fp); return true; }
static int call_bashfunction (lua_State *L) { int no_args=lua_gettop(L); int retval=0; int i; WORD_LIST* list=0; WORD_LIST* start=0; for (i=0; i<no_args; i++) { const char* string=luaL_checkstring(L, i+1); if (list) { list->next=(WORD_LIST*) malloc(sizeof(WORD_LIST)); list=list -> next; } else { list=(WORD_LIST*) malloc(sizeof(WORD_LIST)); start=list; } list->word=make_word(string); list->next=0; } if (!list) retval=127; else { SIMPLE_COM* cmd=newSimpleCom(start); retval=execute_command(make_command(cm_simple, cmd)); } lua_pushinteger(L,retval); return 1; }
FastRational::FastRational( const char * s, const int base ) { mpq_init(mpq); mpq_set_str(mpq, s, base); mpq_canonicalize( mpq ); has_mpq = true; make_word( ); if ( has_word ) kill_mpq( ); assert( isWellFormed( ) ); }
static int execute_bash_trampoline(ffi_cif *cif, void *retval, void **args, char **proto) { SHELL_VAR *function; WORD_LIST *params; char *result; // Decode parameters // callback hello pointer pointer int int // FIXME this is really ugly, do it properly and check cif->argtypes // if (!(function = find_function(*proto))) { fprintf(stderr, "error: unable to resolve function %s in thunk", *proto); return -1; } params = NULL; for (unsigned i = 0; i < cif->nargs; i++) { char parameter[1024]; ffi_raw *p = args[i]; // Decode the parameters snprintf(parameter, sizeof parameter, proto[i+1], p->ptr); params = make_word_list(make_word(parameter), params); } // The first parameter should be the return location asprintf(&result, "pointer:%p", retval); params = make_word_list(make_word(result), params); params = make_word_list(make_word(*proto), params); execute_shell_function(function, params); free(result); return 0; }
void parse (FILE *in_file) { char c; char name[45]; //length of the longest english word int i = 0; int count = 0; int found_word = 0; GList* list = NULL; GList* l = NULL; while ((c =fgetc(in_file)) != EOF ) { //if it's an alpha, convert it to lower case if (isalpha(c)) { found_word = 1; c = tolower(c); name[i] = c; i++; } else { if (found_word) { name[i] ='\0'; for (l = list; l != NULL; l = l->next) { if (strcmp(((Word*)l->data)->name, name)==0) { ((Word*)l->data)->occurances++; count++; } } if (count == 0) { Word *first = make_word(strdup(name), 1); list = g_list_append(list, first); } else { count = 0; } i = 0; found_word=0; name[0] = '\0'; } } } print_list(list); g_list_foreach(list, (GFunc)g_free, NULL); g_list_free(list); }
/* optional: kmalloc and unicode up a string descriptor */ string_desc_t * s3c2410_usb_kmalloc_string_descriptor( const char * p ) { string_desc_t * pResult = NULL; LOG("\n"); if ( p ) { int len = strlen( p ); int uni_len = len * sizeof( __u16 ); pResult = (string_desc_t*) kmalloc( uni_len + 2, GFP_KERNEL ); /* ugh! */ if ( pResult != NULL ) { int i; pResult->bLength = uni_len + 2; pResult->bDescriptorType = USB_DESC_STRING; for( i = 0; i < len ; i++ ) { pResult->bString[i] = make_word( (__u16) p[i] ); } } } return pResult; }
command_t createCommand(enum command_type new_cmd, char *command_string) { command_t x = (command_t) checked_malloc(sizeof(*x)); x->type = new_cmd; x->status = -1; x->input = 0; x->output = 0; x->tree_number = 0; switch (new_cmd) { case SIMPLE_COMMAND: { int i=0; int white_space_counter = 0; while (command_string[i] != '\0' && command_string[i] != '<' && command_string[i] != '>'){ if (command_string[i] == ' '){ white_space_counter++; } i++; } //number of words in simple command = white space + 1 //malloc appropriate memory x->u.word = checked_malloc((white_space_counter+1) * sizeof(char*)); memset(x->u.word, '\0', (white_space_counter+1)*sizeof(char*)); //WARNING: How do you make a union return a certain item? x->u.word = make_word(command_string, white_space_counter+1); break; } case AND_COMMAND: //set both pointers to null to initialize x->u.command[0] = '\0'; x->u.command[1] = '\0'; break; case SEQUENCE_COMMAND: x->u.command[0] = '\0'; x->u.command[1] = '\0'; break; case OR_COMMAND: x->u.command[0] = '\0'; x->u.command[1] = '\0'; break; case PIPE_COMMAND: x->u.command[0] = '\0'; x->u.command[1] = '\0'; break; case SUBSHELL_COMMAND: { x->u.subshell_command = NULL; break; } default: break; } return x; }
uforth_stat exec(CELL wd_idx, char toplevelprim,uint8_t last_exec_rdix) { while(1) { if (wd_idx == 0) { uforth_abort_request(ABORT_ILLEGAL); uforth_abort(); /* bad instruction */ return E_NOT_A_WORD; } cmd = uforth_dict[wd_idx++]; switch (cmd) { case 0: uforth_abort_request(ABORT_ILLEGAL); uforth_abort(); /* bad instruction */ return E_NOT_A_WORD; case ABORT: uforth_abort_request(ABORT_WORD); break; case IMMEDIATE: make_immediate(); break; case IRAM_FETCH: r1 = dpop(); dpush(((DCELL*)uforth_iram)[r1]); break; case URAM_BASE_ADDR: dpush((RAM_START_IDX + (sizeof(struct uforth_iram))) + curtask_idx); break; case SKIP_IF_ZERO: r1 = dpop(); r2 = dpop(); if (r2 == 0) wd_idx += r1; break; case DROP: dpop(); break; case JMP: wd_idx = dpop(); break; case JMP_IF_ZERO: r1 = dpop(); r2 = dpop(); if (r2 == 0) wd_idx = r1; break; case HERE: dpush(dict_here()); break; case INCR_HERE: dict_incr_here(1); break; case LIT: dpush(uforth_dict[wd_idx++]); break; case DLIT: dpush((((uint32_t)uforth_dict[wd_idx])<<16) | (uint16_t)uforth_dict[wd_idx+1]); wd_idx+=2; break; case LESS_THAN: r1 = dpop(); r2 = dpop(); dpush(r2 < r1); break; case ABS: r1 = dpop(); dpush(abs32(r1)); break; case ADD: r1 = dpop(); r2 = dtop(); dtop() = r1+r2; break; case SUB: r1 = dpop(); r2 = dtop(); dtop() = r2-r1; break; case AND: r1 = dpop(); r2 = dtop(); dtop() = r1&r2; break; case LSHIFT: r1 = dpop(); r2 = dtop(); dtop() = r2<<r1; break; case RSHIFT: r1 = dpop(); r2 = dtop(); dtop() = r2>>r1; break; case OR: r1 = dpop(); r2 = dtop(); dtop() = r1|r2; break; case XOR: r1 = dpop(); r2 = dtop(); dtop() = r1^r2; break; case INVERT: dtop() = ~dtop(); break; case MULT: r1 = dpop(); r2 = dtop(); dtop() = r1*r2; break; case DIV : r1 = dpop(); r2 = dtop(); dtop() = r2/r1; break; case SWAP: r1 = dpop(); r2 = dpop(); dpush(r1); dpush(r2); break; case RPICK: r1 = dpop(); r2 = rpick(r1); dpush(r2); break; case PICK: r1 = dpop(); r2 = dpick(r1); dpush(r2); break; case EQ_ZERO: dtop() = (dtop() == 0); break; case RPUSH: rpush(dpop()); break; case RPOP: dpush(rpop()); break; case FETCH: r1 = dpop(); if (r1 >= RAM_START_IDX) { dpush(uforth_ram[r1-RAM_START_IDX]); } else { dpush(uforth_dict[r1]); } break; case STORE: r1 = dpop(); r2 = dpop(); if (r1 >= RAM_START_IDX) { uforth_ram[r1-RAM_START_IDX] = r2; } else { dict_write(r1,r2); } break; case EXEC: r1 = dpop(); rpush(wd_idx); wd_idx = r1; break; case EXIT: if (uforth_uram->ridx > last_exec_rdix) return OK; wd_idx = rpop(); break; case CNEXT: b = next_char(); dpush(b); break; case STR_STORE: r1 = dpop(); r2 = uforth_ram[(r1-RAM_START_IDX)]; str1 =(char*)&uforth_ram[(r1-RAM_START_IDX)+1]; str1+=r2; b = dpop(); *str1 = b; uforth_ram[(r1-RAM_START_IDX)]++; break; case NEXT: str2 = PAD_STR; str1 = uforth_next_word(); memcpy(str2,str1, uforth_iram->currwordlen); PAD_STRLEN = uforth_iram->currwordlen; /* length */ dpush(PAD_ADDR+RAM_START_IDX); break; case COMMA_STRING: if (uforth_iram->compiling > 0) { dict_append(LIT); dict_append(dict_here()+4); /* address of counted string */ dict_append(LIT); rpush(dict_here()); /* address holding adress */ dict_incr_here(1); /* place holder for jump address */ dict_append(JMP); } rpush(dict_here()); dict_incr_here(1); /* place holder for count*/ r1 = 0; b = next_char(); /* eat space */ while (b != 0 && b!= '"') { r2 = 0; b = next_char(); if (b == 0 || b == '"') break; r2 |= BYTEPACK_FIRST(b); ++r1; b = next_char(); if (b != 0 && b != '"') { ++r1; r2 |= BYTEPACK_SECOND(b); } dict_append(r2); } dict_write(rpop(),r1); if (uforth_iram->compiling > 0) { dict_write(rpop(),dict_here()); /* jump over string */ } break; case CALLC: r1 = c_handle(); if (r1 != OK) return (uforth_stat)r1; break; case VAR_ALLOT: dpush(VAR_ALLOT_1()); break; case DEF: uforth_iram->compiling = 1; case _CREATE: dict_start_def(); uforth_next_word(); make_word(uforth_iram->currword,uforth_iram->currwordlen); if (cmd == _CREATE) { dict_end_def(); } else { defining_word = dict_here(); } break; case COMMA: dict_append(dpop()); break; case DCOMMA: r1 = dpop(); dict_append((uint32_t)r1>>16); dict_append(r1); break; case PARSE_NUM: r1 = dpop(); str1=uforth_count_str((CELL)r1,(CELL*)&r1); str1[r1] = '\0'; dpush(parse_num(str1,uforth_uram->base)); break; case PARSE_FNUM: r1 = dpop(); str1=uforth_count_str((CELL)r1,(CELL*)&r1); str1[r1] = '.'; str1[r1+1] = '\0'; dpush(parse_num(str1,uforth_uram->base)); break; case FIND: case FIND_ADDR: r1 = dpop(); str1=uforth_count_str((CELL)r1,(CELL*)&r1); r1 = find_word(str1, r1, &r2, 0, &b); if (r1 > 0) { if (b) r1 = uforth_dict[r1]; } if (cmd == FIND) { dpush(r1); } else { dpush(r2); } break; case POSTPONE: str1 = uforth_next_word(); r1 = find_word(str1, uforth_iram->currwordlen, 0, 0, &b); if (r1 == 0) { uforth_abort_request(ABORT_NAW); uforth_abort(); return E_NOT_A_WORD; } if (b) { dict_append(uforth_dict[r1]); } else { dict_append(r1); } break; case MOVE: r1 = dpop(); /* count */ rpush(r1); r1 = dpop(); /* dest */ r2 = dpop(); /* source */ if (r1 < RAM_START_IDX) { (void)rpop(); uforth_abort_request(ABORT_ILLEGAL); uforth_abort(); /* can't cmove into dictionary */ return E_ABORT; } str1 =(char*)&uforth_ram[(r1-RAM_START_IDX)]; if (r2 > RAM_START_IDX) { str2 =(char*)&uforth_ram[(r2-RAM_START_IDX)]; } else { str2 = (char*)&uforth_dict[r2]; } memcpy(str1, str2, rpop()); break; case MAKE_TASK: r1 = dpop(); r2 = dpop(); r3 = dpop(); r4 = dpop(); uforth_make_task(r1-RAM_START_IDX, r2, r3, r4); dpush(r1-RAM_START_IDX); break; case SELECT_TASK: uforth_select_task(dpop()); break; default: /* Execute user word by calling until we reach primitives */ rpush(wd_idx); wd_idx = uforth_dict[wd_idx-1]; /* wd_idx-1 is current word */ break; } if (uforth_aborting()) { uforth_abort(); return E_ABORT; } if (toplevelprim) return OK; } /* while(1) */ }
void store_prim(char* str, CELL val) { make_word(str,strlen(str)); dict_append(val); dict_append(EXIT); dict_write((dict->last_word_idx+1), uforth_dict[dict->last_word_idx+1]|PRIM_BIT); }
void M6507::step() { switch (this->cpu_status) { case CPU_FETCH_OPCODE: this->opcode_pc = this->PC()->get(); this->opcode = this->fetch_data(); this->PC()->increment(); this->cpu_status = CPU_PARSE_OPCODE; this->opcode_byte_hi = 0; break; //CPU_FETCH_OPCODE END case CPU_PARSE_OPCODE: this->opcode_byte_lo = this->fetch_data(); switch(this->opcode_mode[this->opcode]) { case _immediate: case _relative: this->PC()->increment(); case _none: case _implied: this->cpu_status = CPU_EXEC_OPCODE; break; case _absolute: case _absoluteX: case _absoluteY: case _indirect: this->PC()->increment(); this->cpu_status = CPU_FETCH_BYTE_HI; break; case _zeropage: this->PC()->increment(); this->cpu_status = CPU_FETCH_DATA; break; case _zeropageX: case _zeropageY: this->PC()->increment(); this->cpu_status = CPU_CALC_ADDRESS; break; case _indexedIndirect: this->PC()->increment(); this->cpu_status = CPU_FETCH_INDEXED; break; case _indirectIndexed: this->PC()->increment(); this->cpu_status = CPU_FETCH_INDIRECT; break; } break; //CPU_PARSE_OPCODE END case CPU_FETCH_BYTE_HI: this->opcode_byte_hi = this->fetch_data(); this->PC()->increment(); this->cpu_status = CPU_FETCH_DATA; break; //CPU_FETCH_BYTE_HI END case CPU_CALC_ADDRESS: if (this->cpu_boundary[this->opcode] > 0) { } else { } break; //CPU_CALC_ADDRESS END case CPU_FETCH_DATA: this->fetch_data(make_word(this->opcode_byte_lo, this->opcode_byte_hi)); this->cpu_status = CPU_EXEC_OPCODE; break; //CPU_FETCH_DATA END case CPU_FETCH_INDEXED: break; //CPU_FETCH_INDEXED END case CPU_FETCH_INDIRECT: break; //CPU_FETCH_INDIRECT END case CPU_EXEC_OPCODE: this->cpu_status = CPU_FETCH_OPCODE; switch(this->opcode_id[this->opcode]) { case _ADC: this->ADC(); this->step(); break; case _AHX: this->AHX(); this->step(); break; case _ALR: this->ALR(); this->step(); break; case _ANC: this->ANC(); this->step(); break; case _AND: this->AND(); this->step(); break; case _ARR: this->ARR(); this->step(); break; case _ASL: this->ASL(); this->step(); break; case _AXS: this->AXS(); this->step(); break; case _BCC: this->BCC(); this->step(); break; case _BCS: this->BCS(); this->step(); break; case _BEQ: this->BEQ(); this->step(); break; case _BIT: this->BIT(); this->step(); break; case _BMI: this->BMI(); this->step(); break; case _BNE: this->BNE(); this->step(); break; case _BPL: this->BPL(); this->step(); break; case _BRK: this->BRK(); this->step(); break; case _BVC: this->BVC(); this->step(); break; case _CLC: this->CLC(); this->step(); break; case _CLD: this->CLD(); this->step(); break; case _CLI: this->CLI(); this->step(); break; case _CLV: this->CLV(); this->step(); break; case _CMP: this->CMP(); this->step(); break; case _CPX: this->CPX(); this->step(); break; case _CPY: this->CPY(); this->step(); break; case _DCP: this->DCP(); this->step(); break; case _DEC: this->DEC(); this->step(); break; case _EOR: this->EOR(); this->step(); break; case _INC: this->INC(); this->step(); break; case _INX: this->INX(); this->step(); break; case _INY: this->INY(); this->step(); break; case _ISC: this->ISC(); this->step(); break; case _JMP: this->JMP(); this->step(); break; case _JSR: this->JSR(); this->step(); break; case _KIL: this->KIL(); this->step(); break; case _LAS: this->LAS(); this->step(); break; case _LAX: this->LAX(); this->step(); break; case _LDA: this->LDA(); this->step(); break; case _LDX: this->LDX(); this->step(); break; case _LDY: this->LDY(); this->step(); break; case _LSR: this->LSR(); this->step(); break; case _NOP: this->NOP(); this->step(); break; case _ORA: this->ORA(); this->step(); break; case _PHA: this->PHA(); this->step(); break; case _PHP: this->PHP(); this->step(); break; case _PLA: this->PLA(); this->step(); break; case _RLA: this->RLA(); this->step(); break; case _ROL: this->ROL(); this->step(); break; case _ROR: this->ROR(); this->step(); break; case _RRA: this->RRA(); this->step(); break; case _RTI: this->RTI(); this->step(); break; case _RTS: this->RTS(); this->step(); break; case _SAX: this->SAX(); this->step(); break; case _SBC: this->SBC(); this->step(); break; case _SEC: this->SEC(); this->step(); break; case _SED: this->SED(); this->step(); break; case _SEI: this->SEI(); this->step(); break; case _SHA: this->SHA(); this->step(); break; case _SHY: this->SHY(); this->step(); break; case _SLO: this->SLO(); this->step(); break; case _SRE: this->SRE(); this->step(); break; case _STA: this->STA(); this->step(); break; case _STX: this->STX(); this->step(); break; case _STY: this->STY(); this->step(); break; case _TAS: this->TAS(); this->step(); break; case _TAX: this->TAX(); this->step(); break; case _TAY: this->TAY(); this->step(); break; case _TSX: this->TSX(); this->step(); break; case _TXA: this->TXA(); this->step(); break; case _TXS: this->TXS(); this->step(); break; case _TYA: this->TYA(); this->step(); break; case _XAA: this->XAA(); this->step(); break; } break; //CPU_EXEC_OPCODE END } }