void directive_help(Dict *command, int interactive) { List *TopicWords = NULL; // A list of the help topic words supplied by the user char *TopicString = NULL; // The string of help topic words supplied by the user char filename[FNAME_LENGTH]; // The filename of ppl_help.xml char *HelpPosition[MAX_HELP_DEPTH]; // A list of xml tags, used to keep track of our position as we traverse the xml hierarchy char *HelpTexts [MAX_HELP_HITS ]; // A list of all of the help topics which have matched the user's request xmlDoc *doc = NULL; // The XML document ppl_help.xml xmlNode *root_element = NULL; // The root element of the above xmlNode *MatchNode = NULL; // The XML node which best fits the user's request int MatchTextPos = -1; // The position within HelpTexts of the node pointed to by MatchNode int ambiguous = 0; // Becomes one if we find that the user's request matches multiple help pages int Nmatches = 0; // Counts the number of pages the user's request matches int i; DictLookup(command, "topic", NULL, (void *)&TopicString); // Make a list of the requested topic words in TopicWords TopicWords = StrSplit( TopicString ); sprintf(filename, "%s%s%s", SRCDIR, PATHLINK, "ppl_help.xml"); // Find ppl_help.xml LIBXML_TEST_VERSION doc = xmlReadFile(filename, NULL, 0); if (doc==NULL) { sprintf(temp_err_string, "Help command cannot find help data in expected location of '%s'.", filename); ppl_error(ERR_INTERNAL, -1, -1, temp_err_string); return; } for (i=0;i<MAX_HELP_DEPTH;i++) if ((HelpPosition[i]=(char *)lt_malloc(SSTR_LENGTH*sizeof(char)) )==NULL) return; for (i=0;i<MAX_HELP_HITS ;i++) if ((HelpTexts [i]=(char *)lt_malloc(SSTR_LENGTH*sizeof(char)) )==NULL) return; root_element = xmlDocGetRootElement(doc); help_explore(root_element, &MatchNode, TopicWords, &MatchTextPos, &Nmatches, &ambiguous, HelpPosition, HelpTexts, 0); if (ambiguous == 1) { ppl_report("Ambiguous help request. The following help topics were matched:"); for(i=0;i<Nmatches;i++) ppl_report( HelpTexts[i] ); ppl_report("Please make your help request more specific, and try again."); } else if (MatchNode == NULL) { ppl_report("Please make your help request more specific, and try again."); } else { help_PagerDisplay( HelpTexts[MatchTextPos] , MatchNode , interactive ); } xmlFreeDoc(doc); // Tidy up xmlCleanupParser(); return; }
Dict *DictInit(int HashSize) { Dict *out; out = (Dict *)lt_malloc(sizeof(Dict)); if (out==NULL) return NULL; out->first = NULL; out->last = NULL; out->length = 0; out->HashSize = HashSize; out->HashTable = (DictItem **)lt_malloc(HashSize * sizeof(DictItem *)); memset(out->HashTable, 0, HashSize * sizeof(DictItem *)); if (out->HashTable==NULL) return NULL; out->memory_context = lt_GetMemContext(); return out; }
WBXML_DECLARE(void *) wbxml_malloc(size_t size) { #ifdef WBXML_USE_LEAKTRACKER return lt_malloc(size); #else return malloc(size); #endif }
List *ListInit() { List *out; out = (List *)lt_malloc(sizeof(List)); out->first = NULL; out->last = NULL; out->length = 0; out->memory_context = lt_GetMemContext(); return out; }
/* ARGSUSED */ void lt_klog_log(int level, pid_t pid, char *stack, lt_stat_type_t type, uint64_t value) { lt_stat_data_t *entry = NULL; char *psargs; char *str; int str_len; if ((level & klog_level) == 0) { return; } g_assert(klog_table != NULL && proc_table != NULL); psargs = (char *)g_hash_table_lookup(proc_table, LT_INT_TO_POINTER(pid)); if (psargs == NULL) { psargs = lt_get_proc_field(pid, LT_FIELD_PSARGS); if (psargs == NULL) { psargs = lt_get_proc_field(pid, LT_FIELD_FNAME); } if (psargs == NULL) { return; } g_hash_table_insert(proc_table, LT_INT_TO_POINTER(pid), psargs); } str_len = strlen(stack) + 20; str = lt_malloc(str_len); (void) snprintf(str, str_len, "%ld, \"%s\"", pid, stack); entry = (lt_stat_data_t *)g_hash_table_lookup(klog_table, str); if (entry == NULL) { entry = (lt_stat_data_t *)lt_zalloc(sizeof (lt_stat_data_t)); g_hash_table_insert(klog_table, str, entry); } else { free(str); } lt_update_stat_value(entry, type, value); }
void eps_point_YieldUpText(EPSComm *x) { CanvasTextItem *i; if (x->current->text != NULL) { x->current->FirstTextID = x->NTextItems; if (x->current->text[0]=='\0') return; i = (CanvasTextItem *)lt_malloc(sizeof(CanvasTextItem)); if (i==NULL) { ppl_error(ERR_MEMORY, -1, -1, "Out of memory"); *(x->status) = 1; return; } i->text = x->current->text; i->CanvasMultiplotID = x->current->id; ListAppendPtr(x->TextItems, i, sizeof(CanvasTextItem), 0, DATATYPE_VOID); x->NTextItems++; } return; }
void eps_image_RenderEPS(EPSComm *x) { bitmap_data data; FILE *infile; int ImageType, i, j; double xscale, yscale, r; unsigned char buff[10], *imagez; char *filename; uLongf zlen; // Length of buffer passed to zlib static unsigned char transparency_buff[3]; data.data = data.palette = data.trans = NULL; data.type = 0; data.XDPI = data.YDPI = 180; data.TargetCompression = BMP_ENCODING_FLATE; fprintf(x->epsbuffer, "%% Canvas item %d [bitmap image]\n", x->current->id); // Expand filename if it contains wildcards filename = ppl_glob_oneresult(x->current->text); if (filename == NULL) { *(x->status) = 1; return; } // Open input data file infile = fopen(filename, "r"); if (infile==NULL) { sprintf(temp_err_string, "Could not open input file '%s'", filename); ppl_error(ERR_FILE, -1, -1, temp_err_string); *(x->status) = 1; return; } // Use magic to determine file type for (i=0; i<3; i++) { j = fgetc(infile); if (j==EOF) { sprintf(temp_err_string, "Could not read any image data from the input file '%s'", filename); ppl_error(ERR_FILE, -1, -1, temp_err_string); *(x->status) = 1; fclose(infile); return; } buff[i] = (unsigned char)j; } if ((buff[0]=='G' )&&(buff[1]=='I' )&&(buff[2]=='F' )) ImageType = SW_BITMAP_GIF; else if ((buff[0]==0xff)&&(buff[1]==0xd8)&&(buff[2]==0xff)) ImageType = SW_BITMAP_JPG; else if ((buff[0]=='B' )&&(buff[1]=='M' ) ) ImageType = SW_BITMAP_BMP; else if ((buff[0]==137 )&&(buff[1]=='P' )&&(buff[2]=='N' )) ImageType = SW_BITMAP_PNG; else { sprintf(temp_err_string, "Could not determine the file type of input file '%s'. The image command only supports bmp, gif, jpeg and png images. The supplied image does not appear to be in any of these formats.", filename); ppl_error(ERR_FILE, -1, -1, temp_err_string); *(x->status) = 1; fclose(infile); return; } // Read data from file using appropriate input filter switch (ImageType) { case SW_BITMAP_BMP: bmp_bmpread (infile , &data); break; case SW_BITMAP_GIF: bmp_gifread (infile , &data); break; case SW_BITMAP_JPG: bmp_jpegread(infile , &data); break; case SW_BITMAP_PNG: bmp_pngread (infile , &data); break; default: ppl_error(ERR_INTERNAL, -1, -1, "Unrecognised image type"); *(x->status) = 1; fclose(infile); return; } fclose(infile); // Check to see whether reading of data failed if (data.data == NULL) { *(x->status) = 1; return; } // Apply palette optimisations to images if possible if ((data.depth == 8) && (data.type==BMP_COLOUR_PALETTE)) bmp_palette_check(&data); // If we did not construct palette, check for trailing unused entries if ((data.depth == 24) && (data.type==BMP_COLOUR_BMP )) bmp_colour_count(&data); // Check full colour image to ensure more than 256 colours if ((data.depth == 8) && (data.type==BMP_COLOUR_PALETTE)) bmp_grey_check(&data); // Check paletted images for greyscale conversion if ((data.type == BMP_COLOUR_PALETTE) && (data.pal_len <= 16) && (data.depth == 8)) bmp_compact(&data); // Compact images with few palette entries // If user has specified a transparent colour, change transparency properties now if (x->current->CustomTransparency) { data.trans = NULL; // Turn off any transparency which may have been present in the original image if (data.colour == BMP_COLOUR_PALETTE) { for(i=0; i<data.pal_len; i++) if ( ((unsigned char)x->current->TransColR == data.palette[3*i ]) && ((unsigned char)x->current->TransColG == data.palette[3*i+1]) && ((unsigned char)x->current->TransColB == data.palette[3*i+2]) ) { data.trans = transparency_buff; *data.trans = (unsigned char)i; break; } } else if (data.colour == BMP_COLOUR_RGB) // RGB { data.trans = transparency_buff; data.trans[0] = (unsigned char)x->current->TransColR; data.trans[1] = (unsigned char)x->current->TransColG; data.trans[2] = (unsigned char)x->current->TransColB; } else if ((data.colour == BMP_COLOUR_GREY) && (x->current->TransColR==x->current->TransColG) && (x->current->TransColR==x->current->TransColB)) // Greyscale { int magic; if (data.depth == 1) { magic=255 ; } // 2-colour greyscale else if (data.depth == 2) { magic=255/ 3; } // 4-colour greyscale else if (data.depth == 4) { magic=255/15; } // 16-colour greyscale else { magic= 1 ; } // 256-colour greyscale if ((x->current->TransColR % magic)==0) { data.trans = transparency_buff; data.trans[0] = (unsigned char)(x->current->TransColR / magic); } } } #ifdef FLATE_DISABLE if (data.TargetCompression==BMP_ENCODING_FLATE) data.TargetCompression=BMP_ENCODING_NULL; #endif // Apply compression to image data switch (data.TargetCompression) { case BMP_ENCODING_NULL: break; // No image compression case BMP_ENCODING_DCT: break; // Special case: JPEG data is encoded in DCT, but is already supplied from bmp_jpegread() in encoded form case BMP_ENCODING_FLATE: zlen = data.data_len*1.01+12; // Nasty guess at size of buffer needed. imagez = (unsigned char *)lt_malloc(zlen); if (imagez == NULL) { ppl_error(ERR_MEMORY, -1, -1,"Out of memory"); return; } if (DEBUG) { ppl_log("Calling zlib to compress image data"); } j = compress2(imagez,&zlen,data.data,data.data_len,9); // Call zlib to do deflation if (j!=0) { if (DEBUG) { sprintf(temp_err_string, "zlib returned error code %d\n",j); ppl_log(temp_err_string); } data.TargetCompression = BMP_ENCODING_NULL; // Give up trying to compress data break; } if (DEBUG) { sprintf(temp_err_string, "zlib has completed compression. Before flate: %ld bytes. After flate: %ld bytes", data.data_len, (long)zlen); ppl_log(temp_err_string); } if (zlen >= data.data_len) { if (DEBUG) { ppl_log("Using original uncompressed data since zlib made it bigger than it was to start with"); } data.TargetCompression = BMP_ENCODING_NULL; // Give up trying to compress data; result was larger than original data size break; } data.data = imagez; // Replace old data with new compressed data data.data_len = zlen; break; default: ppl_error(ERR_INTERNAL, -1, -1, "Unrecognised image compression type requested"); *(x->status) = 1; return; } // Work out dimensions of image if ((x->current->xpos2set) && (x->current->ypos2set)) // Both width and height have been specified { xscale = x->current->xpos2 * M_TO_PS; yscale = x->current->ypos2 * M_TO_PS; } else if (x->current->xpos2set) // Only width has been set { xscale = x->current->xpos2 * M_TO_PS; yscale = x->current->xpos2 * M_TO_PS * ((double)data.height / (double)data.width); } else if (x->current->ypos2set) // Only height has been set { xscale = x->current->ypos2 * M_TO_PS * ((double)data.width / (double)data.height); yscale = x->current->ypos2 * M_TO_PS; } else // Neither height nor width has been set; use DPI information to work out how big image should be { xscale = ((double)data.width ) / data.XDPI * 72; yscale = ((double)data.height) / data.YDPI * 72; } // Make it into postscript fprintf(x->epsbuffer, "gsave\n"); fprintf(x->epsbuffer, "%.2f %.2f translate\n", x->current->xpos * M_TO_PS, x->current->ypos * M_TO_PS); fprintf(x->epsbuffer, "%.2f rotate\n", x->current->rotation * 180 / M_PI); fprintf(x->epsbuffer, "%.2f %.2f scale\n", xscale, yscale); // We render image onto a unit square; use scale to make it the size we actually want if (data.colour == BMP_COLOUR_RGB ) fprintf(x->epsbuffer, "/DeviceRGB setcolorspace\n"); // RGB palette else if (data.colour == BMP_COLOUR_GREY) fprintf(x->epsbuffer, "/DeviceGray setcolorspace\n"); // Greyscale palette else if (data.colour == BMP_COLOUR_PALETTE) // Indexed palette { fprintf(x->epsbuffer, "[/Indexed /DeviceRGB %d <~\n", data.pal_len-1); bmp_A85(x->epsbuffer, data.palette, 3*data.pal_len); fprintf(x->epsbuffer, "] setcolorspace\n\n"); } fprintf(x->epsbuffer, "<<\n /ImageType %d\n /Width %d\n /Height %d\n /ImageMatrix [%d 0 0 %d 0 %d]\n", ((!x->current->NoTransparency) && (data.trans!=NULL))?4:1, data.width, data.height, data.width, -data.height, data.height); fprintf(x->epsbuffer, " /DataSource currentfile /ASCII85Decode filter"); // Image data is stored in currentfile, but need to apply filters to decode it if (data.TargetCompression == BMP_ENCODING_DCT ) fprintf(x->epsbuffer, " /DCTDecode filter"); else if (data.TargetCompression == BMP_ENCODING_FLATE) fprintf(x->epsbuffer, " /FlateDecode filter"); fprintf(x->epsbuffer, "\n /BitsPerComponent %d\n /Decode [0 %d%s]\n", (data.colour==BMP_COLOUR_RGB)?(data.depth/3):(data.depth), (data.type==BMP_COLOUR_PALETTE)?((1<<data.depth)-1):1, (data.colour==BMP_COLOUR_RGB)?" 0 1 0 1":""); if ((!x->current->NoTransparency) && (data.trans != NULL)) // If image has transparency, set mask colour { fprintf(x->epsbuffer," /MaskColor ["); if (data.colour == BMP_COLOUR_RGB) fprintf(x->epsbuffer, "%d %d %d]\n",(int)data.trans[0], (int)data.trans[1], (int)data.trans[2]); else fprintf(x->epsbuffer, "%d]\n" ,(int)data.trans[0]); } if (x->current->smooth) fprintf(x->epsbuffer, " /Interpolate true\n"); // If image has smooth flag set, tell postscript interpretter to interpolate image fprintf(x->epsbuffer, ">> image\n"); bmp_A85(x->epsbuffer, data.data, data.data_len); fprintf(x->epsbuffer, "grestore\n"); // Update postscript bounding box r = x->current->rotation; eps_core_BoundingBox(x, x->current->xpos*M_TO_PS , x->current->ypos*M_TO_PS , 0); eps_core_BoundingBox(x, x->current->xpos*M_TO_PS + xscale*cos(r) , x->current->ypos*M_TO_PS + xscale*sin(r) , 0); eps_core_BoundingBox(x, x->current->xpos*M_TO_PS + yscale*-sin(r) , x->current->ypos*M_TO_PS + yscale*cos(r) , 0); eps_core_BoundingBox(x, x->current->xpos*M_TO_PS + xscale*cos(r) + yscale*-sin(r) , x->current->ypos*M_TO_PS + xscale*sin(r) + yscale*cos(r) , 0); // Final newline at end of canvas item fprintf(x->epsbuffer, "\n"); return; }
void bmp_jpegread(FILE *jpeg, bitmap_data *image) { int comps=0, prec=0; long i, j, len, chunk=64*1024; unsigned int width=0, height=0, save; unsigned char *buff, *header, *headp, *headendp, type=0, comp=0, *p; buff = (unsigned char *)lt_malloc(chunk); header = (unsigned char *)lt_malloc(HEADLEN); headp = header; headendp = header + HEADLEN - 8; if (DEBUG) ppl_log("Beginning to decode JPEG image file"); if ((buff == NULL)||(header == NULL)) { ppl_error(ERR_MEMORY, -1, -1,"Out of memory"); return; } if (fread(buff,3,1,jpeg)!=1) { ppl_error(ERR_FILE, -1, -1,"This JPEG image file appears to be corrupted"); return; } i=buff[0]; if (i<0xe0) { ppl_error(ERR_FILE, -1, -1, "In supplied JPEG image, first marker is not APPn. Aborting."); return; } len=buff[1]*256+buff[2]-2; if (fread(buff,len,1,jpeg)!=1) { ppl_error(ERR_FILE, -1, -1,"This JPEG image file appears to be corrupted"); return; } if (DEBUG) { if (!strcmp((char*)buff,"JFIF")&&(i=0xe0)) { sprintf(temp_err_string,"JPEG version %d.%02d",(int)buff[5],(int)buff[6]); ppl_log(temp_err_string); sprintf(temp_err_string,"Thumbnail size %dx%d",(int)buff[12],(int)buff[13]); ppl_log(temp_err_string); sprintf(temp_err_string,"JFIF APP0 entry length 0x%x",(int)len+2); ppl_log(temp_err_string); switch ((int)buff[7]) { case 0: ppl_log("No DPI information available"); break; case 1: sprintf(temp_err_string,"DPI specified as %dx%d dots per inch",(256*((int)buff[ 8])+((int)buff[ 9])), (256*((int)buff[10])+((int)buff[11])) ); ppl_log(temp_err_string); image->XDPI = (256*((int)buff[ 8])+((int)buff[ 9])); image->YDPI = (256*((int)buff[10])+((int)buff[11])); break; case 2: sprintf(temp_err_string,"DPI specified as %dx%d dots per cm" ,(256*((int)buff[ 8])+((int)buff[ 9])), (256*((int)buff[10])+((int)buff[11])) ); ppl_log(temp_err_string); image->XDPI = (256*((int)buff[ 8])+((int)buff[ 9])) * 100 * GSL_CONST_MKSA_INCH; image->YDPI = (256*((int)buff[10])+((int)buff[11])) * 100 * GSL_CONST_MKSA_INCH; break; default: sprintf(temp_err_string,"DPI specified in unrecognised unit number %d",(int)buff[7]); break; } } else if (!strcmp((char*)buff,"Exif")&&(i=0xe1)) { sprintf(temp_err_string,"Exif JPEG file\n"); ppl_log(temp_err_string); sprintf(temp_err_string,"Exif APP1 entry length 0x%x",(int)len+2); ppl_log(temp_err_string); } } *(headp++) = 0xff; *(headp++) = 0xd8; if (DEBUG) { sprintf(temp_err_string, "JFIF APP%d entry discarded", (int)i-0xe0); ppl_log(temp_err_string); } while ((type!=0xda) && fread(buff,1,1,jpeg) && (buff[0]=0xff)) { if (fread(buff,3,1,jpeg)!=1) { ppl_error(ERR_FILE, -1, -1,"This JPEG image file appears to be corrupted"); return; } type = buff[0]; len = buff[1]*256+buff[2]-2; save = 0; if (fread(buff,len,1,jpeg)!=1) { ppl_error(ERR_FILE, -1, -1,"This JPEG image file appears to be corrupted"); return; } if (DEBUG) { sprintf(temp_err_string, "Entry type %x length 0x%x",(int)type,(int)len+2); ppl_log(temp_err_string); } if (((type&0xf0)==0xc0) && (type!=0xc4) && (type!=0xcc)) { if (comp) { ppl_error(ERR_FILE, -1, -1, "Cannot decode JPEG image file: it contains multiple images?"); return; } comp = type; prec = buff[0]; height = 256*buff[1]+buff[2]; width = 256*buff[3]+buff[4]; comps = buff[5]; save = 1; } if ((type==0xfe) && (DEBUG)) { buff[len]=0; sprintf(temp_err_string,"JPEG Comment: %s",buff); ppl_log(temp_err_string); } if ((type==0xe0) && (DEBUG)) { sprintf(temp_err_string,"APP0 Marker: %s",buff); ppl_log(temp_err_string); } if ((type==0xdb)||(type==0xc4)||(type==0xda)||(type==0xdd)) save=1; if (save) { *(headp++) = 0xff; *(headp++) = type; *(headp++) = (len+2)>>8; *(headp++) = (len+2)&0xff; if (headp+len>headendp) { ppl_error(ERR_FILE, -1, -1,"Header storage for JPEG image exhausted"); return; } for (i=0; i<len; i++) *(headp++) = buff[i]; } else if (DEBUG) ppl_log("Discarding section of JPEG image file"); }