bp_Document * bp_parseDoc(bp_Context * context, xmlDocPtr doc) { bp_Document * result = malloc(sizeof(bp_Document)); xmlNodePtr rootNode; context->doc = (void *) result; context->nLabels = 0; result->root = NULL; for (rootNode = doc->children; rootNode; rootNode = rootNode->next) { switch (ELEM(rootNode)) { case BPE_BOOK: result->root = (bp_Node *) bp_parseBook(context, rootNode); break; default:; } } if (!result->root) { char errmsg[500]; snprintf(errmsg, sizeof(errmsg), "%s: root element must be \"book\"" , doc->name); LOG_ERROR(errmsg); exit(1); } context->labels = st_create(context->nLabels + 1); st_addptr(context->labels, "", NULL); // allow for empty "to" attributes bp_collectLabels(context, result->root); st_commit(context->labels); bp_linkReferences(context, result->root); st_destroy(context->labels); return result; }
/* main function takes a PLATYPUS source file as * an argument at the command line. * usage: parser source_file_name [-stz size][-sts:A | -sts:D] */ int main(int argc, char ** argv){ FILE *fi; /* input file handle */ int loadsize = 0; /*the size of the file loaded in the buffer */ int st_def_size = ST_DEF_SIZE; /* Sumbol Table default size */ char sort_st = 0; /*Symbol Table sort switch */ int ansi_c = !ANSI_C; /* ANSI C flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if(ansi_c){ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /*check for correct arrguments - source file name */ if (argc <= 1){ /* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("Runtime error at line %d in file %s", __LINE__, __FILE__); err_printf("%s%s%s",argv[0],": ","Missing source file name."); err_printf("%s%s%s","Usage: ", "parser", " source_file_name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* check for optional switches - symbol table size and/or sort */ if (argc == 3){ if (strcmp(argv[2],"-sts:A") && strcmp(argv[2],"-sts:D") ){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if(strcmp(argv[2],"-sts:A")) sort_st = 'D'; else sort_st = 'A'; } /* symbol table size specified */ if (argc == 4){ if (strcmp(argv[2],"-stz")){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* convert the symbol table size */ st_def_size = atoi(argv[3]); if (st_def_size <= 0){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } } if (argc == 5){ if (strcmp(argv[2],"-stz")){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* convert the symbol table size */ st_def_size = atoi(argv[3]); if (st_def_size <= 0){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if (strcmp(argv[4],"-sts:A")&& strcmp(argv[4],"-sts:D") ){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if(strcmp(argv[4],"-sts:A")) sort_st = 'D'; else sort_st = 'A'; } /* create a source code input buffer - multiplicative mode */ sc_buf = b_create(INIT_CAPACITY,INC_FACTOR,'m'); if (sc_buf == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create source buffer"); exit(EXIT_FAILURE); } /* create symbol table */ sym_table = st_create(st_def_size); if (!sym_table.st_size){ err_printf("%s%s%s",argv[0],": ","Could not create symbol table"); exit (EXIT_FAILURE); } /*open source file */ if ((fi = fopen(argv[1],"r")) == NULL){ err_printf("%s%s%s%s",argv[0],": ", "Cannot open file: ",argv[1]); exit (1); } /* load source file into input buffer */ printf("Reading file %s ....Please wait\n",argv[1]); loadsize = b_load (fi,sc_buf); if(loadsize == R_FAIL_1) err_printf("%s%s%s",argv[0],": ","Error in loading buffer."); /* close source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1],"is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* pack and display the source buffer */ if(b_pack(sc_buf)){ display(sc_buf); } /* create string Literal Table */ str_LTBL = b_create(INIT_CAPACITY,INC_FACTOR,'a'); if (str_LTBL == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create string buffer"); exit(EXIT_FAILURE); } /*registrer exit function */ atexit(garbage_collect); /*Testbed for buffer, scanner,symbol table and parser*/ /* Initialize scanner */ scanner_init(sc_buf); /* Add SEOF to input buffer */ b_addc(sc_buf, EOF); /* Start parsing */ printf("\nParsing the source file...\n\n"); parser(sc_buf); /* print Symbol Table */ /* if(sym_table.st_size && sort_st){ st_print(sym_table); if(sort_st){ printf("\nSorting symbol table...\n"); st_sort(sym_table,sort_st); st_print(sym_table); } } */ return (EXIT_SUCCESS); /* same effect as exit(0) */ }/*end of main */
void fd_load() { char path[256]; xmlDocPtr doc = NULL; xmlNodePtr rootNode, fontNode; int maxFont = 0; snprintf(path, sizeof(path), "%s/fontdef.xml", datadir); doc = xmlParseFile(path); if (doc) { // find root tag for (rootNode = doc->children; rootNode && strcmp(rootNode->name, "fontdef"); rootNode = rootNode->next); if (rootNode) { // find highest font id for (fontNode = rootNode->children; fontNode; fontNode = fontNode->next) { if (!strcmp(fontNode->name, "font")) { xmlChar * strval = xmlGetProp(fontNode, "id"); if (strval) { int fontID = atoi(strval); if (fontID > maxFont) maxFont = fontID; xmlFree(strval); } } } // allocate space fd_nFonts = maxFont + 1; fd_fonts = calloc(fd_nFonts, sizeof(fd_Font *)); // calloc fills memory with zeroes bp_fonts = st_create(fd_nFonts); // load the fonts for (fontNode = rootNode->children; fontNode; fontNode = fontNode->next) { if (!strcmp(fontNode->name, "font")) { fd_Font * font = malloc(sizeof(fd_Font)); xmlChar *strval; int maxChar = 0; xmlNodePtr texNode, charNode; strval = xmlGetProp(fontNode, "linesize"); if (strval) { int linesize = atoi(strval); if (linesize > 0) { font->linesize = linesize; } else { LOG_ERROR("fontdef: 'linesize' attribute needs to be positive"); free(font); continue; } } else { LOG_ERROR("fontdef: font needs to carry 'linesize' attribute"); free(font); continue; } strval = xmlGetProp(fontNode, "baseline"); if (strval) { int baseline = atoi(strval); if (baseline >= 0) { font->baseline = baseline; } else { LOG_ERROR("fontdef: 'baseline' attribute needs to be non-negative"); free(font); continue; } } else { font->baseline = 0; } strval = xmlGetProp(fontNode, "name"); if (strval) { int size = strlen(strval) + 1; font->name = malloc(size); memcpy(font->name, strval, size); } else { LOG_ERROR("fontdef: font needs to carry 'name' attribute"); free(font); continue; } strval = xmlGetProp(fontNode, "id"); if (strval) { int fontID = atoi(strval); if (fontID >= 0) { // add font to font and symbol table fd_fonts[fontID] = font; st_addnum(bp_fonts, font->name, fontID); } xmlFree(strval); } else { LOG_ERROR("fontdef: font needs to carry 'id' attribute"); free(font->name); free(font); continue; } // find highest char value for (texNode = fontNode->children; texNode; texNode = texNode->next) { if (!strcmp(texNode->name, "texture")) { for (charNode = texNode->children; charNode; charNode = charNode->next) { if (!strcmp(charNode->name, "text")) { Uint8 * pch = charNode->content; if (pch) { while (*pch) { Uint32 uch = fd_utf8_decode(pch++); if (uch != 0xffffffff && !isspace(uch) && uch > maxChar) maxChar = uch; // find next line while (*pch && *pch != '\n') pch++; if (!*pch) break; pch++; } } } } } } // allocate space font->nChars = maxChar + 1; font->chars = calloc(font->nChars, sizeof(fd_Char)); // sets the array to zero // load the chars for (texNode = fontNode->children; texNode; texNode = texNode->next) { if (!strcmp(texNode->name, "texture")) { int tex; float texWidth, texHeight; char * texPath; texPath = xmlGetProp(texNode, "src"); if (texPath) { tex = load_texture_cache_deferred(texPath,0); xmlFree(texPath); } else { LOG_ERROR("fontdef: texture element needs to carry 'src' attribute"); continue; } strval = xmlGetProp(texNode, "width"); if (strval) { texWidth = atof(strval); xmlFree(strval); } else { LOG_ERROR("fontdef: texture element needs to carry 'width' attribute"); continue; } strval = xmlGetProp(texNode, "height"); if (strval) { texHeight = atof(strval); xmlFree(strval); } else { LOG_ERROR("fontdef: texture element needs to carry 'height' attribute"); continue; } for (charNode = texNode->children; charNode; charNode = charNode->next) { if (!strcmp(charNode->name, "text")) { const Uint8 * pch = charNode->content; if (pch) { while (*pch) { // get the char Uint32 uch = fd_utf8_decode(pch++); pch = fd_utf8_next_char(pch); if (uch != 0xffffffff && !isspace(uch)) { fd_Char * dest = &font->chars[uch]; int left, top, width, height, baseline; // get the char properties sscanf(pch, " %i %i %i %i %i", &left, &top, &width, &height, &baseline); dest->tex = tex; dest->left = ((float) left) / texWidth; dest->top = ((float) top) / texHeight; dest->right = ((float) left + width) / texWidth; dest->bottom = ((float) top + height) / texHeight; dest->width = width; dest->height = height; dest->baseline = baseline; dest->valid = 1; } // find next line while (*pch && *pch != '\n') pch++; if (!*pch) break; pch++; } } } } } } } } } st_commit(bp_fonts); free(doc); } }
int main(int argc, char **argv) { assert(HEAPSIZE < MAXSIZE_FILE); // otherwise config.h is screwed int i=0,rv=0; int first_byte=0,second_byte=0; FILE* infile = NULL; ramword_t *inputbuffer = NULL; stack_t* stack = NULL; ramaddr_t* heap = NULL; cpu_t* cpu = NULL; switch(argc) { case 0: printf("Unsuitable environment (argc==0)\n"); rv = -1; case 1: printf("Usage: emma input\n"); rv = -1; case 2: if((infile = fopen(argv[1],"r")) == NULL) { printf("[error] could not open input file, check permissions.\n"); rv = -1; } break; default: printf("Usage: emma input\n"); rv = -1; } // if we're successful, get on with the business of the day if(rv==0) { // initialize the CPU cpu = malloc(sizeof(cpu_t)); if(cpu == NULL) return -1; // error memset(cpu,0,sizeof(cpu_t)); if((inputbuffer = malloc(MAXSIZE_FILE * sizeof(*inputbuffer)))==NULL) return -1; stack = st_create(STACKSIZE); #ifdef EMMA_DEBUG fprintf(stderr,"HEAPSIZE: %d\n",HEAPSIZE); #endif heap = heap_init(HEAPSIZE); // read the file into memory (more complex than it first seemed) while(((first_byte = getc(infile)) != EOF)&&((second_byte = getc(infile)) != EOF)) { inputbuffer[i] = (first_byte<<8) | second_byte; i++; if(i>=MAXSIZE_FILE) break; // this fixes the cause of a segfault } #ifdef EMMA_DEBUG int j=0; printf("Loaded program: "); while(j<i) { printf("%.4X ",inputbuffer[j]); j++; } putchar('\n'); #endif // warn about program truncation if(i > HEAPSIZE) { #ifdef EMMA_DEBUG printf("Warning: input file \"%s\" has been truncated\n",argv[1]); #endif #ifdef FATAL_ERRORS rv = -1; #endif } // move the input buffer onto the heap at offset 0x0000, truncated to HEAPSIZE if(!heap_load(heap,inputbuffer,HEAPSIZE)) { #ifdef EMMA_DEBUG printf("Error: heap loading failed\n"); #endif #ifdef FATAL_ERRORS rv = -1; #endif } // the input buffer is no longer necessary, so we should free it free(inputbuffer); // set up the cpu with the stack and the heap cpu->stack = stack; cpu->pc = heap; if(rv==0) { // set the program running, return the state of the CPU at exit cpu = emu_run(cpu); } #ifdef EMMA_DEBUG if((cpu->flag_reg & FLAG_ERROR)!=0) // this if was previously backwards { printf("Program halted with error: %.2X\n",cpu->errno); core_dump(cpu); } #endif // shutdown safely (free any used memory) st_free(stack); heap_free(heap); free(cpu); fclose(infile); } return rv; }
void httpd(void) { int listenfd; struct sockaddr_in saddr; fd_set readset; fd_set writeset; int i, maxfdp1; http_state_t *hs; listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0) { printf("httpd: socket create failed\n"); return; } memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(80); if (bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { printf("httpd: bind failed\n"); close(listenfd); return; } if (listen(listenfd, 10) < 0) { printf("httpd: listen failed\n"); close(listenfd); return; } st_init(); printf("httpd: listening for connections on socket %d\n", listenfd); for ( ; ; ) { maxfdp1 = listenfd + 1; FD_ZERO(&readset); FD_ZERO(&writeset); FD_SET(listenfd, &readset); // maxfdp1 = st_add_fds(&readset, maxfdp1); // st_add_fds(&writeset); i = select(maxfdp1, &readset, &writeset, 0, 0); if (i == 0) continue; // Check for new incoming connections if (FD_ISSET(listenfd, &readset)) { int tmp = 1; hs = st_create(); hs->client_size = sizeof(hs->client); hs->socket = accept(listenfd, (struct sockaddr *)&hs->client, &hs->client_size); printf("httpd: connect from %08lx, port %d, socket %d\n", hs->client.sin_addr.s_addr, hs->client.sin_port, hs->socket); if (hs->socket < 0) { st_destroy(hs); } else { hs->thd = thd_create(client_thread, hs); // hs->thd = sys_thread_new(client_thread, hs, 0); } /* else if (ioctl(hs->socket, FIONBIO, &tmp) < 0) { printf("httpd: failed to set non-blocking\n"); st_destroy(hs); } */ } #if 0 // Process data from connected clients st_foreach(hs) { if (FD_ISSET(hs->socket, &readset)) { if (handle_read(hs) < 0) { printf("httpd: disconnected socket %d\n", hs->socket); close(hs->socket); st_destroy(hs); break; } } /* if (FD_ISSET(hs->socket, &writeset)) { } */ } #endif } }