From d3ba1cf57383182257fb02164fd4bc0fa580fabb Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 5 Mar 2009 21:47:24 +0000 Subject: Fix some compiler warnings. --- lib/sisfileheader.cpp | 220 +++--- plpprint/plpprintd.cc | 1556 +++++++++++++++++++++---------------------- sisinstall/sisinstaller.cpp | 1107 +++++++++++++++--------------- sisinstall/sismain.cpp | 195 +++--- 4 files changed, 1535 insertions(+), 1543 deletions(-) diff --git a/lib/sisfileheader.cpp b/lib/sisfileheader.cpp index e15e54b..5eba9e2 100644 --- a/lib/sisfileheader.cpp +++ b/lib/sisfileheader.cpp @@ -32,137 +32,137 @@ const int OFF_INSTALLATION_DRIVE = 28; SisRC SISFileHeader::compareApp(SISFileHeader* other) { - if (m_uid1 != other->m_uid1) - return SIS_DIFFERENT_APP; - if ((m_major < other->m_major) || - ((m_major == other->m_major) && - (m_minor < other->m_minor))) - return SIS_VER_EARLIER; - if ((m_major == other->m_major) && - (m_minor == other->m_minor) && - (m_variant != other->m_variant)) - return SIS_OTHER_VARIANT; - return SIS_SAME_OR_LATER; + if (m_uid1 != other->m_uid1) + return SIS_DIFFERENT_APP; + if ((m_major < other->m_major) || + ((m_major == other->m_major) && + (m_minor < other->m_minor))) + return SIS_VER_EARLIER; + if ((m_major == other->m_major) && + (m_minor == other->m_minor) && + (m_variant != other->m_variant)) + return SIS_OTHER_VARIANT; + return SIS_SAME_OR_LATER; } SisRC SISFileHeader::fillFrom(uint8_t* buf, int* base, off_t len) { - if (*base + 68 > len) - return SIS_TRUNCATED; - uint8_t* start = buf + *base; - m_buf = buf; - m_uid1 = read32(start); - if (logLevel >= 1) - printf(_("Got uid1 = %08x\n"), m_uid1); - m_uid2 = read32(start + 4); - if (m_uid2 != 0x1000006d) - { - printf(_("Got bad uid2.\n")); - return SIS_CORRUPTED; - } - if (logLevel >= 2) - printf(_("Got uid2 = %08x\n"), m_uid2); - m_uid3 = read32(start + 8); - if (m_uid3 != 0x10000419) - { - printf(_("Got bad uid3.\n")); - return SIS_CORRUPTED; - } - if (logLevel >= 2) - printf(_("Got uid3 = %08x\n"), m_uid3); - m_uid4 = read32(start + 12); -// printf(_("Got uid4 = %08x\n"), m_uid4); - uint16_t crc1 = 0; - for (int i = 0; i < 12; i += 2) - crc1 = updateCrc(crc1, buf[*base + i]); - uint16_t crc2 = 0; - for (int i = 0; i < 12; i += 2) - crc2 = updateCrc(crc2, buf[*base + i + 1]); - if (logLevel >= 2) - printf(_("Got first crc = %08x, wanted %08x\n"), - crc2 << 16 | crc1, m_uid4); - if ((crc2 << 16 | crc1) != m_uid4) - { - printf(_("Got bad crc.\n")); - return SIS_CORRUPTED; - } - m_crc = read16(start + 16); - m_nlangs = read16(start + 18); - if (logLevel >= 2) - printf(_("Got %d languages\n"), m_nlangs); - m_nfiles = read16(start + 20); - if (logLevel >= 2) - printf(_("Got %d files\n"), m_nfiles); - m_nreqs = read16(start + 22); - if (logLevel >= 2) - printf(_("Got %d reqs\n"), m_nreqs); - m_installationLanguage = read16(start + 24); - if (logLevel >= 2) - printf(_("Selected language is %d\n"), m_installationLanguage); - m_installationFiles = read16(start + OFF_NUMBER_OF_FILES); - if (logLevel >= 2) - printf(_("Installed files: %d / %d\n"), m_installationFiles, m_nfiles); - m_installationDrive = read32(start + OFF_INSTALLATION_DRIVE); - if (logLevel >= 2) - printf(_("Installed on drive %c\n"), m_installationDrive); - m_installerVersion = read32(start + 32); - if (logLevel >= 2) - printf(_("Got installer version: %08x\n"), m_installerVersion); - m_options = read16(start + 36); - if (logLevel >= 2) - printf(_("Got options: %04x\n"), m_options); - m_type = read16(start + 38); - if (logLevel >= 2) - printf(_("Got type: %04x\n"), m_type); - m_major = read16(start + 40); - if (logLevel >= 2) - printf(_("Got major: %d\n"), m_major); - m_minor = read16(start + 42); - if (logLevel >= 2) - printf(_("Got minor: %d\n"), m_minor); - m_variant = read32(start + 44); - if (logLevel >= 2) - printf(_("Got variant: %d\n"), m_variant); - m_languagePtr = read32(start + 48); - if (logLevel >= 2) - printf(_("Languages begin at %d\n"), m_languagePtr); + if (*base + 68 > len) + return SIS_TRUNCATED; + uint8_t* start = buf + *base; + m_buf = buf; + m_uid1 = read32(start); + if (logLevel >= 1) + printf(_("Got uid1 = %08x\n"), m_uid1); + m_uid2 = read32(start + 4); + if (m_uid2 != 0x1000006d) + { + printf("%s", _("Got bad uid2.\n")); + return SIS_CORRUPTED; + } + if (logLevel >= 2) + printf(_("Got uid2 = %08x\n"), m_uid2); + m_uid3 = read32(start + 8); + if (m_uid3 != 0x10000419) + { + printf("%s", _("Got bad uid3.\n")); + return SIS_CORRUPTED; + } + if (logLevel >= 2) + printf(_("Got uid3 = %08x\n"), m_uid3); + m_uid4 = read32(start + 12); + if (logLevel >= 2) + printf(_("Got uid4 = %08x\n"), m_uid4); + uint16_t crc1 = 0; + for (int i = 0; i < 12; i += 2) + crc1 = updateCrc(crc1, buf[*base + i]); + uint16_t crc2 = 0; + for (int i = 0; i < 12; i += 2) + crc2 = updateCrc(crc2, buf[*base + i + 1]); + if (logLevel >= 2) + printf(_("Got first crc = %08x, wanted %08x\n"), + crc2 << 16 | crc1, m_uid4); + if ((crc2 << 16 | crc1) != m_uid4) + { + printf("%s", _("Got bad crc.\n")); + return SIS_CORRUPTED; + } + m_crc = read16(start + 16); + m_nlangs = read16(start + 18); + if (logLevel >= 2) + printf(_("Got %d languages\n"), m_nlangs); + m_nfiles = read16(start + 20); + if (logLevel >= 2) + printf(_("Got %d files\n"), m_nfiles); + m_nreqs = read16(start + 22); + if (logLevel >= 2) + printf(_("Got %d reqs\n"), m_nreqs); + m_installationLanguage = read16(start + 24); + if (logLevel >= 2) + printf(_("Selected language is %d\n"), m_installationLanguage); + m_installationFiles = read16(start + OFF_NUMBER_OF_FILES); + if (logLevel >= 2) + printf(_("Installed files: %d / %d\n"), m_installationFiles, m_nfiles); + m_installationDrive = read32(start + OFF_INSTALLATION_DRIVE); + if (logLevel >= 2) + printf(_("Installed on drive %c\n"), m_installationDrive); + m_installerVersion = read32(start + 32); + if (logLevel >= 2) + printf(_("Got installer version: %08x\n"), m_installerVersion); + m_options = read16(start + 36); + if (logLevel >= 2) + printf(_("Got options: %04x\n"), m_options); + m_type = read16(start + 38); + if (logLevel >= 2) + printf(_("Got type: %04x\n"), m_type); + m_major = read16(start + 40); + if (logLevel >= 2) + printf(_("Got major: %d\n"), m_major); + m_minor = read16(start + 42); + if (logLevel >= 2) + printf(_("Got minor: %d\n"), m_minor); + m_variant = read32(start + 44); + if (logLevel >= 2) + printf(_("Got variant: %d\n"), m_variant); + m_languagePtr = read32(start + 48); + if (logLevel >= 2) + printf(_("Languages begin at %d\n"), m_languagePtr); // if (m_languagePtr >= len) // return SIS_TRUNCATED; - m_filesPtr = read32(start + 52); - if (logLevel >= 2) - printf(_("Files begin at %d\n"), m_filesPtr); + m_filesPtr = read32(start + 52); + if (logLevel >= 2) + printf(_("Files begin at %d\n"), m_filesPtr); // if (m_filesPtr >= len) // return SIS_TRUNCATED; - m_reqPtr = read32(start + 56); - if (logLevel >= 2) - printf(_("Requisites begin at %d\n"), m_reqPtr); + m_reqPtr = read32(start + 56); + if (logLevel >= 2) + printf(_("Requisites begin at %d\n"), m_reqPtr); // if (m_reqPtr >= len) // return SIS_TRUNCATED; - m_unknown = read32(start + 60); - m_componentPtr = read32(start + 64); - if (logLevel >= 2) - printf(_("Components begin at %d\n"), m_componentPtr); + m_unknown = read32(start + 60); + m_componentPtr = read32(start + 64); + if (logLevel >= 2) + printf(_("Components begin at %d\n"), m_componentPtr); // if (m_componentPtr >= len) // return SIS_TRUNCATED; - *base += 68; - return SIS_OK; + *base += 68; + return SIS_OK; } void SISFileHeader::setDrive(char drive) { - m_installationDrive = drive; - m_buf[OFF_INSTALLATION_DRIVE] = drive; - m_buf[OFF_INSTALLATION_DRIVE + 1] = - m_buf[OFF_INSTALLATION_DRIVE + 2] = - m_buf[OFF_INSTALLATION_DRIVE + 3] = 0; + m_installationDrive = drive; + m_buf[OFF_INSTALLATION_DRIVE] = drive; + m_buf[OFF_INSTALLATION_DRIVE + 1] = + m_buf[OFF_INSTALLATION_DRIVE + 2] = + m_buf[OFF_INSTALLATION_DRIVE + 3] = 0; } void SISFileHeader::setFiles(int nFiles) { - m_installationFiles = nFiles; - write16(m_buf + OFF_NUMBER_OF_FILES, nFiles); + m_installationFiles = nFiles; + write16(m_buf + OFF_NUMBER_OF_FILES, nFiles); } - diff --git a/plpprint/plpprintd.cc b/plpprint/plpprintd.cc index f96c6b1..feed33f 100644 --- a/plpprint/plpprintd.cc +++ b/plpprint/plpprintd.cc @@ -65,22 +65,22 @@ do { \ int n, size = 100; \ va_list ap; \ if ((p = (char *)malloc(size)) == NULL) \ - return 0; \ + return 0; \ while (1) { \ - /* Try to print in the allocated space. */ \ - va_start(ap, fmt); \ - n = vsnprintf(p, size, fmt, ap); \ - va_end(ap); \ - /* If that worked, return the string. */ \ - if (n > -1 && n < size) \ - break; \ - /* Else try again with more space. */ \ - if (n > -1) /* glibc 2.1 */ \ - size = n+1; /* precisely what is needed */ \ - else /* glibc 2.0 */ \ - size *= 2; /* twice the old size */ \ - if ((p = (char *)realloc(p, size)) == NULL) \ - return 0; \ + /* Try to print in the allocated space. */ \ + va_start(ap, fmt); \ + n = vsnprintf(p, size, fmt, ap); \ + va_end(ap); \ + /* If that worked, return the string. */ \ + if (n > -1 && n < size) \ + break; \ + /* Else try again with more space. */ \ + if (n > -1) /* glibc 2.1 */ \ + size = n+1; /* precisely what is needed */ \ + else /* glibc 2.0 */ \ + size *= 2; /* twice the old size */ \ + if ((p = (char *)realloc(p, size)) == NULL) \ + return 0; \ } \ } while (0) @@ -90,9 +90,9 @@ debuglog(char *fmt, ...) char *buf; alloc_print(buf); if (debug) - cout << buf << endl; + cout << buf << endl; else - syslog(LOG_DEBUG, "%s", buf); + syslog(LOG_DEBUG, "%s", buf); free(buf); return 0; } @@ -103,9 +103,9 @@ errorlog(char *fmt, ...) char *buf; alloc_print(buf); if (debug) - cerr << buf << endl; + cerr << buf << endl; else - syslog(LOG_ERR, "%s", buf); + syslog(LOG_ERR, "%s", buf); free(buf); return 0; } @@ -116,9 +116,9 @@ infolog(char *fmt, ...) char *buf; alloc_print(buf); if (debug) - cout << buf << endl; + cout << buf << endl; else - syslog(LOG_INFO, "%s", buf); + syslog(LOG_INFO, "%s", buf); free(buf); return 0; } @@ -166,109 +166,109 @@ init_fontmap() { fontmap_entry *fe; if ((f = fopen(PKGDATADIR "/fontmap", "r"))) { - char *p; - int bold; - int italic; - char *psifont; - char *psfont; - char *tmp; - char buf[1024]; - while (fgets(buf, sizeof(buf), f)) { - char *bp = buf; - int ne; - if ((p = strchr(buf, '#'))) - *p = '\0'; - if ((p = strchr(buf, '\n'))) - *p = '\0'; + char *p; + int bold; + int italic; + char *psifont; + char *psfont; + char *tmp; + char buf[1024]; + while (fgets(buf, sizeof(buf), f)) { + char *bp = buf; + int ne; + if ((p = strchr(buf, '#'))) + *p = '\0'; + if ((p = strchr(buf, '\n'))) + *p = '\0'; - psifont = strsep(&bp, ":"); - if (!psifont || !(*psifont)) - continue; - tmp = strsep(&bp, ":"); - if (!tmp || !(*tmp) || (sscanf(tmp, "%d", &bold) != 1)) - continue; - tmp = strsep(&bp, ":"); - if (!tmp || !(*tmp) || (sscanf(tmp, "%d", &italic) != 1)) - continue; - psfont = strsep(&bp, ":"); - if (!psfont || !(*psfont)) - continue; - fe = (fontmap_entry *)malloc(sizeof(fontmap_entry)); - if (!fe) - break; - if (!(fe->psifont = strdup(psifont))) { - free(fe); - break; - } - if (!(fe->psfont = strdup(psfont))) { - free(fe->psifont); - free(fe); - break; - } - fe->bold = bold ? true : false; - fe->italic = italic ? true : false; - fe->next = new_fontmap; - new_fontmap = fe; - } - fclose(f); + psifont = strsep(&bp, ":"); + if (!psifont || !(*psifont)) + continue; + tmp = strsep(&bp, ":"); + if (!tmp || !(*tmp) || (sscanf(tmp, "%d", &bold) != 1)) + continue; + tmp = strsep(&bp, ":"); + if (!tmp || !(*tmp) || (sscanf(tmp, "%d", &italic) != 1)) + continue; + psfont = strsep(&bp, ":"); + if (!psfont || !(*psfont)) + continue; + fe = (fontmap_entry *)malloc(sizeof(fontmap_entry)); + if (!fe) + break; + if (!(fe->psifont = strdup(psifont))) { + free(fe); + break; + } + if (!(fe->psfont = strdup(psfont))) { + free(fe->psifont); + free(fe); + break; + } + fe->bold = bold ? true : false; + fe->italic = italic ? true : false; + fe->next = new_fontmap; + new_fontmap = fe; + } + fclose(f); } if (new_fontmap && (new_fontmap->psifont)) - fontmap = new_fontmap; + fontmap = new_fontmap; else { - errorlog("No fontmap found in %s/fontmap, using builtin mapping", - PKGDATADIR); - fe = default_fontmap; - while (fe->psifont) { - fontmap_entry *nfe = (fontmap_entry *)malloc(sizeof(fontmap_entry)); - if (!nfe) - break; - memcpy(nfe, fe, sizeof(fontmap_entry)); - nfe->next = fontmap; - fontmap = nfe; - fe++; - } + errorlog("No fontmap found in %s/fontmap, using builtin mapping", + PKGDATADIR); + fe = default_fontmap; + while (fe->psifont) { + fontmap_entry *nfe = (fontmap_entry *)malloc(sizeof(fontmap_entry)); + if (!nfe) + break; + memcpy(nfe, fe, sizeof(fontmap_entry)); + nfe->next = fontmap; + fontmap = nfe; + fe++; + } } #ifdef DEBUG debuglog("Active Font-Mapping:"); debuglog("%-20s%-7s%-7s%-20s", "Psion", "Bold", "Italic", "PS-Font"); fe = fontmap; while (fe) { - debuglog("%-20s%-7s%-7s%-20s", fe->psifont, - fe->bold ? "true" : "false", - fe->italic ? "true" : "false", - fe->psfont); - fe = fe->next; + debuglog("%-20s%-7s%-7s%-20s", fe->psifont, + fe->bold ? "true" : "false", + fe->italic ? "true" : "false", + fe->psfont); + fe = fe->next; } #endif } static void ps_setfont(FILE *f, const char *fname, bool bold, bool italic, - unsigned long fsize) + unsigned long fsize) { fontmap_entry *fe = fontmap; char *psf = NULL; while (fe) { - if ((!strcmp(fe->psifont, fname)) && - (fe->bold == bold) && - (fe->italic == italic)) { - psf = fe->psfont; - break; - } - fe = fe->next; + if ((!strcmp(fe->psifont, fname)) && + (fe->bold == bold) && + (fe->italic == italic)) { + psf = fe->psfont; + break; + } + fe = fe->next; } if (!psf) { - psf = FALLBACK_FONT; - errorlog("No font mapping for '%s' (%s%s%s); fallback to %s", - fname, (bold) ? "Bold" : "", (italic) ? "Italic" : "", - (bold || italic) ? "" : "Regular", psf); + psf = FALLBACK_FONT; + errorlog("No font mapping for '%s' (%s%s%s); fallback to %s", + fname, (bold) ? "Bold" : "", (italic) ? "Italic" : "", + (bold || italic) ? "" : "Regular", psf); } if (usedfonts.find(psf) == usedfonts.npos) { - usedfonts += "%%+ font "; - usedfonts += psf; - usedfonts += "\n"; + usedfonts += "%%+ font "; + usedfonts += psf; + usedfonts += "\n"; } - fprintf(f, "%d /%s F\n", fsize, psf); + fprintf(f, "%ld /%s F\n", fsize, psf); } static void @@ -276,27 +276,27 @@ ps_escape(string &text) { int pos = 0; while ((pos = text.find_first_of("()", pos)) != text.npos) { - text.insert(pos, "\\"); - pos += 2; + text.insert(pos, "\\"); + pos += 2; } } static void ps_bitmap(FILE *f, unsigned long llx, unsigned long lly, unsigned long urx, - unsigned long ury, const char *buf) + unsigned long ury, const char *buf) { bufferStore out; int width, height; if (decodeBitmap((const unsigned char *)buf, width, height, out)) { - fprintf(f, "%d %d %d %d %d %d I\n", llx, lly, urx, ury, width, height); - const unsigned char *p = (const unsigned char *)out.getString(0); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) - fprintf(f, "%02x", *p++); - fprintf(f, "\n"); - } + fprintf(f, "%ld %ld %ld %ld %d %d I\n", llx, lly, urx, ury, width, height); + const unsigned char *p = (const unsigned char *)out.getString(0); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) + fprintf(f, "%02x", *p++); + fprintf(f, "\n"); + } } else - errorlog("Corrupted bitmap data"); + errorlog("Corrupted bitmap data"); } static void @@ -319,459 +319,459 @@ convertPage(FILE *f, int page, bool last, bufferStore buf) debuglog("Saved page input to %s", dumpname); #endif if (page == 0) { - time_t now = time(NULL); - fputs( - "%!PS-Adobe-3.0\n" - "%%Creator: plpprintd " VERSION "\n" - "%%CreationDate: ", f); - fputs(ctime(&now), f); - fputs( - "%%Pages: (atend)\n" - "%%BoundingBox: (atend)\n" - "%%DocumentNeededResources: (atend)\n" - "%%LanguageLevel: 2\n" - "%%EndComments\n" - "%%BeginProlog\n", f); - char pbuf[1024]; - FILE *pf = fopen(PKGDATADIR "/prolog.ps", "r"); - while (fgets(pbuf, sizeof(pbuf), pf)) - fputs(pbuf, f); - fclose(pf); - fputs( - "%%EndProlog\n" - "%%BeginSetup\n" - "currentpagedevice /PageSize get 1 get /top exch def\n" - "ip 1 1 TH 32 DM RC 1 PS\n" - "%%EndSetup\n", f); - minx = miny = 9999; - maxx = maxy = 0; - usedfonts = ""; + time_t now = time(NULL); + fputs( + "%!PS-Adobe-3.0\n" + "%%Creator: plpprintd " VERSION "\n" + "%%CreationDate: ", f); + fputs(ctime(&now), f); + fputs( + "%%Pages: (atend)\n" + "%%BoundingBox: (atend)\n" + "%%DocumentNeededResources: (atend)\n" + "%%LanguageLevel: 2\n" + "%%EndComments\n" + "%%BeginProlog\n", f); + char pbuf[1024]; + FILE *pf = fopen(PKGDATADIR "/prolog.ps", "r"); + while (fgets(pbuf, sizeof(pbuf), pf)) + fputs(pbuf, f); + fclose(pf); + fputs( + "%%EndProlog\n" + "%%BeginSetup\n" + "currentpagedevice /PageSize get 1 get /top exch def\n" + "ip 1 1 TH 32 DM RC 1 PS\n" + "%%EndSetup\n", f); + minx = miny = 9999; + maxx = maxy = 0; + usedfonts = ""; } fprintf(f, "%%%%Page: %d %d\n", page+1, page+1); while (i < len) { - unsigned char opcode = buf.getByte(i); - switch (opcode) { - case 0x00: { - // Start of section - unsigned long section = buf.getDWord(i+1) & 3; - unsigned long pagenr = buf.getDWord(i+1) >> 2; - fprintf(f, "%% @%d: Section %d, Page %d\n", i, section, pagenr); - fprintf(f, "1 1 TH 32 DM RC 1 PS CC\n"); - // (section & 3) = - // 0 = Header, 1 = Body, 2 = Footer, 3 = Footer - i += 5; - } - break; - case 0x01: { - // End of page - i = len + 1; - } - break; - case 0x03: { - // Set drawing mode - unsigned char drwmode = buf.getByte(i+1); - fprintf(f, "%% @%d: Drawing mode %02x\n", i, drwmode); - switch (drwmode) { - case 0x01: - // ~screen - break; - case 0x02: - // colour ^ screen - break; - case 0x03: - // ~colour ^ screen - break; - case 0x04: - // colour | screen - break; - case 0x05: - // colour | ~screen - break; - case 0x08: - // colour & screen - break; - case 0x09: - // colour & ~screen - break; - case 0x14: - // ~colour | screen - break; - case 0x15: - // ~colour | ~screen - break; - case 0x18: - // ~colour & screen - break; - case 0x19: - // ~colour & ~screen - break; - case 0x20: - // colour - break; - case 0x30: - // ~colour - break; - } - fprintf(f, "%d DM\n", drwmode); - i += 2; - } - break; - case 0x04: { - // Bounding box (clipping rectangle) - left = buf.getDWord(i+1); - top = buf.getDWord(i+5); - right = buf.getDWord(i+9); - bottom = buf.getDWord(i+13); - if (left < minx) - minx = left; - if (right > maxx) - maxx = right; - if (top < miny) - miny = top; - if (bottom > maxy) - maxy = bottom; - i += 17; - fprintf(f, "%% @%d: bbox %d %d %d %d\n", i, left, top, right, - bottom); - fprintf(f, "%d %d %d %d CB\n", left, top, right, bottom); - } - break; - case 0x05: { - // Cancel clipping rect - left = top = right = bottom = 0; - fprintf(f, "%% @%d: Cancel Cliprect\n", i); - fprintf(f, "CC\n"); - i++; - } - break; - case 0x06: { - // ??? - fprintf(f, "%% @%d: U06 %d 0x%08x\n", i, - buf.getByte(i+1), buf.getDWord(i+2)); - i += 6; - } - break; - case 0x07: { - // Font - int namelen; - int ofs; - if (buf.getByte(i+1) & 1) { - namelen = buf.getWord(i+1) >> 3; - ofs = i + 3; - } else { - namelen = buf.getByte(i+1) >> 2; - ofs = i + 2; - } - string fname(buf.getString(ofs), namelen); - ofs += namelen; - int screenfont = buf.getByte(ofs); - int basesize = buf.getWord(ofs+1); - unsigned long style = buf.getDWord(ofs+3); - bool italic = ((style & 1) != 0); - bool bold = ((style & 2) != 0); - unsigned long fontsize = buf.getDWord(ofs+7); - boffset = (long)buf.getDWord(ofs+11); - fprintf(f, "%% @%d: Font '%s' %d %s%s%s\n", i, fname.c_str(), - fontsize, bold ? "Bold" : "", italic ? "Italic" : "", - (bold || italic) ? "" : "Regular"); - ps_setfont(f, fname.c_str(), bold, italic, fontsize); - i = ofs + 15; - } - break; - case 0x08: { - // End Font - fprintf(f, "%% @%d: End Font\n", i); - i++; - } - break; - case 0x09: { - // underline - fprintf(f, "%% @%d: Underline %d\n", i, buf.getByte(i+1)); - fprintf(f, "%d UL\n", buf.getByte(i+1)); - i += 2; - } - break; - case 0x0a: { - // strikethru - fprintf(f, "%% @%d: Strikethru %d\n", i, buf.getByte(i+1)); - fprintf(f, "%d ST\n", buf.getByte(i+1)); - i += 2; - } - break; - case 0x0b: { - // newline - fprintf(f, "%% @%d: Newline %d %d\n", i, buf.getDWord(i+1), - buf.getDWord(i+5)); - i += 9; - } - break; - case 0x0c: { - // cr - fprintf(f, "%% @%d: CR %d %d\n", i, buf.getDWord(i+1), - buf.getDWord(i+5)); - i += 9; - } - break; - case 0x0d: { - // foreground color - fprintf(f, "%% @%d: Foreground %d %d %d\n", i, buf.getByte(i+1), - buf.getByte(i+2), buf.getByte(i+3)); - fprintf(f, "%d %d %d FG\n", buf.getByte(i+1), - buf.getByte(i+2), buf.getByte(i+3)); - i += 4; - } - break; - case 0x0e: { - // Set pen style - unsigned char pstyle = buf.getByte(i+1); - switch (pstyle) { - case 0x00: - // Don't draw - break; - case 0x01: - // Solid - break; - case 0x02: - // Dotted line - break; - case 0x03: - // Dashed line - break; - case 0x04: - // Dash Dot - break; - case 0x05: - // Dash Dot Dot - break; - } - fprintf(f, "%% @%d: Pen Style %d\n", i, pstyle); - fprintf(f, "%d PS\n", pstyle); - i += 2; - } - break; - case 0x0f: { - // Pen thickness x, y - fprintf(f, "%% @%d: Pen thickness %d %d\n", i, buf.getDWord(i+1), - buf.getDWord(i+5)); - fprintf(f, "%d %d TH\n", buf.getDWord(i+1), buf.getDWord(i+5)); - i += 9; - } - break; - case 0x10: { - // background color - fprintf(f, "%% @%d: Background %d %d %d\n", i, buf.getByte(i+1), - buf.getByte(i+2), buf.getByte(i+3)); - fprintf(f, "%d %d %d BG\n", buf.getByte(i+1), - buf.getByte(i+2), buf.getByte(i+3)); - i += 4; - } - break; - case 0x11: { - // Brush style - unsigned char bstyle = buf.getByte(i+1); - switch (bstyle) { - case 0x00: - // No brush - break; - case 0x01: - // Solid brush - break; - case 0x02: - // Patterned brush - break; - case 0x03: - // Vertical hatch brush - break; - case 0x04: - // Diagonal hatch brush (bottom left to top right) - break; - case 0x05: - // Horizontal hatch brush - break; - case 0x06: - // Rev. diagonal hatch brush (top left to bottom right) - break; - case 0x07: - // Square cross hatch (horizontal and vertical) - break; - case 0x08: - // Diamond cross hatch (both diagonals) - break; - } - fprintf(f, "%% @%d: Brush style %d\n", i, bstyle); - fprintf(f, "%d BS\n", bstyle); - i += 2; - } - break; - case 0x17: { - // ??? - fprintf(f, "%% @%d: U17 %d %d\n", i, buf.getDWord(i+1), - buf.getDWord(i+5)); - i += 9; - } - break; - case 0x19: { - // Draw line - fprintf(f, "%% @%d: Line %d %d %d %d\n", i, - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - fprintf(f, "%d %d %d %d L\n", - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - i += 17; - } - break; - case 0x1b: { - // ??? - fprintf(f, "%% @%d: U1b %d %d\n", i, buf.getDWord(i+1), - buf.getDWord(i+5)); - i += 9; - } - break; - case 0x1f: { - // Draw ellipse - fprintf(f, "%% @%d: Ellipse %d %d %d %d\n", i, - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - fprintf(f, "%d %d %d %d E\n", - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - i += 17; - } - break; - case 0x20: { - // Draw rectangle - fprintf(f, "%% @%d: Rectangle %d %d %d %d\n", i, - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - fprintf(f, "%d %d %d %d R\n", - buf.getDWord(i+1), buf.getDWord(i+5), - buf.getDWord(i+9), buf.getDWord(i+13)); - i += 17; - } - break; + unsigned char opcode = buf.getByte(i); + switch (opcode) { + case 0x00: { + // Start of section + unsigned long section = buf.getDWord(i+1) & 3; + unsigned long pagenr = buf.getDWord(i+1) >> 2; + fprintf(f, "%% @%d: Section %ld, Page %ld\n", i, section, pagenr); + fprintf(f, "1 1 TH 32 DM RC 1 PS CC\n"); + // (section & 3) = + // 0 = Header, 1 = Body, 2 = Footer, 3 = Footer + i += 5; + } + break; + case 0x01: { + // End of page + i = len + 1; + } + break; + case 0x03: { + // Set drawing mode + unsigned char drwmode = buf.getByte(i+1); + fprintf(f, "%% @%d: Drawing mode %02x\n", i, drwmode); + switch (drwmode) { + case 0x01: + // ~screen + break; + case 0x02: + // colour ^ screen + break; + case 0x03: + // ~colour ^ screen + break; + case 0x04: + // colour | screen + break; + case 0x05: + // colour | ~screen + break; + case 0x08: + // colour & screen + break; + case 0x09: + // colour & ~screen + break; + case 0x14: + // ~colour | screen + break; + case 0x15: + // ~colour | ~screen + break; + case 0x18: + // ~colour & screen + break; + case 0x19: + // ~colour & ~screen + break; + case 0x20: + // colour + break; + case 0x30: + // ~colour + break; + } + fprintf(f, "%d DM\n", drwmode); + i += 2; + } + break; + case 0x04: { + // Bounding box (clipping rectangle) + left = buf.getDWord(i+1); + top = buf.getDWord(i+5); + right = buf.getDWord(i+9); + bottom = buf.getDWord(i+13); + if (left < minx) + minx = left; + if (right > maxx) + maxx = right; + if (top < miny) + miny = top; + if (bottom > maxy) + maxy = bottom; + i += 17; + fprintf(f, "%% @%d: bbox %ld %ld %ld %ld\n", i, left, top, right, + bottom); + fprintf(f, "%ld %ld %ld %ld CB\n", left, top, right, bottom); + } + break; + case 0x05: { + // Cancel clipping rect + left = top = right = bottom = 0; + fprintf(f, "%% @%d: Cancel Cliprect\n", i); + fprintf(f, "CC\n"); + i++; + } + break; + case 0x06: { + // ??? + fprintf(f, "%% @%d: U06 %d 0x%08x\n", i, + buf.getByte(i+1), buf.getDWord(i+2)); + i += 6; + } + break; + case 0x07: { + // Font + int namelen; + int ofs; + if (buf.getByte(i+1) & 1) { + namelen = buf.getWord(i+1) >> 3; + ofs = i + 3; + } else { + namelen = buf.getByte(i+1) >> 2; + ofs = i + 2; + } + string fname(buf.getString(ofs), namelen); + ofs += namelen; + int screenfont = buf.getByte(ofs); + int basesize = buf.getWord(ofs+1); + unsigned long style = buf.getDWord(ofs+3); + bool italic = ((style & 1) != 0); + bool bold = ((style & 2) != 0); + unsigned long fontsize = buf.getDWord(ofs+7); + boffset = (long)buf.getDWord(ofs+11); + fprintf(f, "%% @%d: Font '%s' %ld %s%s%s\n", i, fname.c_str(), + fontsize, bold ? "Bold" : "", italic ? "Italic" : "", + (bold || italic) ? "" : "Regular"); + ps_setfont(f, fname.c_str(), bold, italic, fontsize); + i = ofs + 15; + } + break; + case 0x08: { + // End Font + fprintf(f, "%% @%d: End Font\n", i); + i++; + } + break; + case 0x09: { + // underline + fprintf(f, "%% @%d: Underline %d\n", i, buf.getByte(i+1)); + fprintf(f, "%d UL\n", buf.getByte(i+1)); + i += 2; + } + break; + case 0x0a: { + // strikethru + fprintf(f, "%% @%d: Strikethru %d\n", i, buf.getByte(i+1)); + fprintf(f, "%d ST\n", buf.getByte(i+1)); + i += 2; + } + break; + case 0x0b: { + // newline + fprintf(f, "%% @%d: Newline %d %d\n", i, buf.getDWord(i+1), + buf.getDWord(i+5)); + i += 9; + } + break; + case 0x0c: { + // cr + fprintf(f, "%% @%d: CR %d %d\n", i, buf.getDWord(i+1), + buf.getDWord(i+5)); + i += 9; + } + break; + case 0x0d: { + // foreground color + fprintf(f, "%% @%d: Foreground %d %d %d\n", i, buf.getByte(i+1), + buf.getByte(i+2), buf.getByte(i+3)); + fprintf(f, "%d %d %d FG\n", buf.getByte(i+1), + buf.getByte(i+2), buf.getByte(i+3)); + i += 4; + } + break; + case 0x0e: { + // Set pen style + unsigned char pstyle = buf.getByte(i+1); + switch (pstyle) { + case 0x00: + // Don't draw + break; + case 0x01: + // Solid + break; + case 0x02: + // Dotted line + break; + case 0x03: + // Dashed line + break; + case 0x04: + // Dash Dot + break; + case 0x05: + // Dash Dot Dot + break; + } + fprintf(f, "%% @%d: Pen Style %d\n", i, pstyle); + fprintf(f, "%d PS\n", pstyle); + i += 2; + } + break; + case 0x0f: { + // Pen thickness x, y + fprintf(f, "%% @%d: Pen thickness %d %d\n", i, buf.getDWord(i+1), + buf.getDWord(i+5)); + fprintf(f, "%d %d TH\n", buf.getDWord(i+1), buf.getDWord(i+5)); + i += 9; + } + break; + case 0x10: { + // background color + fprintf(f, "%% @%d: Background %d %d %d\n", i, buf.getByte(i+1), + buf.getByte(i+2), buf.getByte(i+3)); + fprintf(f, "%d %d %d BG\n", buf.getByte(i+1), + buf.getByte(i+2), buf.getByte(i+3)); + i += 4; + } + break; + case 0x11: { + // Brush style + unsigned char bstyle = buf.getByte(i+1); + switch (bstyle) { + case 0x00: + // No brush + break; + case 0x01: + // Solid brush + break; + case 0x02: + // Patterned brush + break; + case 0x03: + // Vertical hatch brush + break; + case 0x04: + // Diagonal hatch brush (bottom left to top right) + break; + case 0x05: + // Horizontal hatch brush + break; + case 0x06: + // Rev. diagonal hatch brush (top left to bottom right) + break; + case 0x07: + // Square cross hatch (horizontal and vertical) + break; + case 0x08: + // Diamond cross hatch (both diagonals) + break; + } + fprintf(f, "%% @%d: Brush style %d\n", i, bstyle); + fprintf(f, "%d BS\n", bstyle); + i += 2; + } + break; + case 0x17: { + // ??? + fprintf(f, "%% @%d: U17 %d %d\n", i, buf.getDWord(i+1), + buf.getDWord(i+5)); + i += 9; + } + break; + case 0x19: { + // Draw line + fprintf(f, "%% @%d: Line %d %d %d %d\n", i, + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + fprintf(f, "%d %d %d %d L\n", + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + i += 17; + } + break; + case 0x1b: { + // ??? + fprintf(f, "%% @%d: U1b %d %d\n", i, buf.getDWord(i+1), + buf.getDWord(i+5)); + i += 9; + } + break; + case 0x1f: { + // Draw ellipse + fprintf(f, "%% @%d: Ellipse %d %d %d %d\n", i, + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + fprintf(f, "%d %d %d %d E\n", + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + i += 17; + } + break; + case 0x20: { + // Draw rectangle + fprintf(f, "%% @%d: Rectangle %d %d %d %d\n", i, + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + fprintf(f, "%d %d %d %d R\n", + buf.getDWord(i+1), buf.getDWord(i+5), + buf.getDWord(i+9), buf.getDWord(i+13)); + i += 17; + } + break; - case 0x23: { - // Draw polygon - unsigned long count = buf.getDWord(i+1); - int o = i + 5; - fprintf(f, "%% @%d: Polygon (%d segments)\n", i, count); - fprintf(f, "[\n"); - for (int j = 0; j < count; j++) { - fprintf(f, "%d %d\n", buf.getDWord(o), - buf.getDWord(o+4)); - o += 8; - } - unsigned char frule = buf.getByte(o); - fprintf(f, "] %s P\n", frule ? "false" : "true"); - i = o + 1; - } - break; - case 0x25: { - // Draw bitmap - unsigned long llx = buf.getDWord(i+1); - unsigned long lly = buf.getDWord(i+13); - unsigned long urx = buf.getDWord(i+9); - unsigned long ury = buf.getDWord(i+5); - unsigned long blen = buf.getDWord(i+17); - fprintf(f, "%% @%d: Bitmap\n", i); - ps_bitmap(f, llx, lly, urx, ury, buf.getString(i+17)); - i += (17 + blen); - } - break; - case 0x26: { - // Draw bitmap - unsigned long llx = buf.getDWord(i+1); - unsigned long lly = buf.getDWord(i+13); - unsigned long urx = buf.getDWord(i+9); - unsigned long ury = buf.getDWord(i+5); - unsigned long blen = buf.getDWord(i+17); - unsigned long u1 = buf.getDWord(i+17+blen); - unsigned long u2 = buf.getDWord(i+17+blen+4); - unsigned long u3 = buf.getDWord(i+17+blen+8); - unsigned long u4 = buf.getDWord(i+17+blen+12); - fprintf(f, "%% @%d: Bitmap %d %d %d %d\n", i, u1, u2, u3, u4); - ps_bitmap(f, llx, lly, urx, ury, buf.getString(i+17)); - i += (17 + blen + 16); - } - break; - case 0x27: { - // Draw label - int tlen; - int ofs; - if (buf.getByte(i+1) & 1) { - tlen = buf.getWord(i+1) >> 3; - ofs = i + 3; - } else { - tlen = buf.getByte(i+1) >> 2; - ofs = i + 2; - } - string text(buf.getString(ofs), tlen); - ofs += tlen; - ps_escape(text); - fprintf(f, "%% @%d: Text '%s' %d %d\n", i, - text.c_str(), buf.getDWord(ofs), buf.getDWord(ofs+4)); - fprintf(f, "(%s) %d %d 0 0 -1 T\n", text.c_str(), - buf.getDWord(ofs), buf.getDWord(ofs+4) + boffset); - i = ofs + 8; - } - break; - case 0x28: { - // Draw justified text - int tlen; - int ofs; - if (buf.getByte(i+1) & 1) { - tlen = buf.getWord(i+1) >> 3; - ofs = i + 3; - } else { - tlen = buf.getByte(i+1) >> 2; - ofs = i + 2; - } - string text(buf.getString(ofs), tlen); - ofs += tlen; - int left = buf.getDWord(ofs); - int top = buf.getDWord(ofs+4); - int right = buf.getDWord(ofs+8); - int bottom = buf.getDWord(ofs+12); - int baseline = buf.getDWord(ofs+16); - unsigned char align = buf.getByte(ofs+20); - int amargin = buf.getDWord(ofs+21); - fprintf(f, "%% @%d: JText '%s' %d %d %d %d %d %d %d\n", i, - text.c_str(), left, bottom + boffset, top, right, - baseline, align, amargin); - ps_escape(text); - if (align == 2) - right -= amargin; - else - left += amargin; - bottom -= ((bottom - top) / 4); - fprintf(f, "(%s) %d %d %d %d %d T\n", text.c_str(), - left, bottom + boffset, top, right, align); - i = ofs + 25; - } - break; - default: - fprintf(f, "@%d: UNHANDLED OPCODE %02x\n", i, opcode); - debuglog("@%d: UNHANDLED OPCODE %02x", i, opcode); - i++; - break; - } + case 0x23: { + // Draw polygon + unsigned long count = buf.getDWord(i+1); + int o = i + 5; + fprintf(f, "%% @%d: Polygon (%ld segments)\n", i, count); + fprintf(f, "[\n"); + for (int j = 0; j < count; j++) { + fprintf(f, "%d %d\n", buf.getDWord(o), + buf.getDWord(o+4)); + o += 8; + } + unsigned char frule = buf.getByte(o); + fprintf(f, "] %s P\n", frule ? "false" : "true"); + i = o + 1; + } + break; + case 0x25: { + // Draw bitmap + unsigned long llx = buf.getDWord(i+1); + unsigned long lly = buf.getDWord(i+13); + unsigned long urx = buf.getDWord(i+9); + unsigned long ury = buf.getDWord(i+5); + unsigned long blen = buf.getDWord(i+17); + fprintf(f, "%% @%d: Bitmap\n", i); + ps_bitmap(f, llx, lly, urx, ury, buf.getString(i+17)); + i += (17 + blen); + } + break; + case 0x26: { + // Draw bitmap + unsigned long llx = buf.getDWord(i+1); + unsigned long lly = buf.getDWord(i+13); + unsigned long urx = buf.getDWord(i+9); + unsigned long ury = buf.getDWord(i+5); + unsigned long blen = buf.getDWord(i+17); + unsigned long u1 = buf.getDWord(i+17+blen); + unsigned long u2 = buf.getDWord(i+17+blen+4); + unsigned long u3 = buf.getDWord(i+17+blen+8); + unsigned long u4 = buf.getDWord(i+17+blen+12); + fprintf(f, "%% @%d: Bitmap %ld %ld %ld %ld\n", i, u1, u2, u3, u4); + ps_bitmap(f, llx, lly, urx, ury, buf.getString(i+17)); + i += (17 + blen + 16); + } + break; + case 0x27: { + // Draw label + int tlen; + int ofs; + if (buf.getByte(i+1) & 1) { + tlen = buf.getWord(i+1) >> 3; + ofs = i + 3; + } else { + tlen = buf.getByte(i+1) >> 2; + ofs = i + 2; + } + string text(buf.getString(ofs), tlen); + ofs += tlen; + ps_escape(text); + fprintf(f, "%% @%d: Text '%s' %d %d\n", i, + text.c_str(), buf.getDWord(ofs), buf.getDWord(ofs+4)); + fprintf(f, "(%s) %d %ld 0 0 -1 T\n", text.c_str(), + buf.getDWord(ofs), buf.getDWord(ofs+4) + boffset); + i = ofs + 8; + } + break; + case 0x28: { + // Draw justified text + int tlen; + int ofs; + if (buf.getByte(i+1) & 1) { + tlen = buf.getWord(i+1) >> 3; + ofs = i + 3; + } else { + tlen = buf.getByte(i+1) >> 2; + ofs = i + 2; + } + string text(buf.getString(ofs), tlen); + ofs += tlen; + int left = buf.getDWord(ofs); + int top = buf.getDWord(ofs+4); + int right = buf.getDWord(ofs+8); + int bottom = buf.getDWord(ofs+12); + int baseline = buf.getDWord(ofs+16); + unsigned char align = buf.getByte(ofs+20); + int amargin = buf.getDWord(ofs+21); + fprintf(f, "%% @%d: JText '%s' %d %ld %d %d %d %d %d\n", i, + text.c_str(), left, bottom + boffset, top, right, + baseline, align, amargin); + ps_escape(text); + if (align == 2) + right -= amargin; + else + left += amargin; + bottom -= ((bottom - top) / 4); + fprintf(f, "(%s) %d %ld %d %d %d T\n", text.c_str(), + left, bottom + boffset, top, right, align); + i = ofs + 25; + } + break; + default: + fprintf(f, "@%d: UNHANDLED OPCODE %02x\n", i, opcode); + debuglog("@%d: UNHANDLED OPCODE %02x", i, opcode); + i++; + break; + } } fprintf(f, "showpage\n"); if (last) { - fputs( - "%%Trailer\n" - "%%DocumentNeededResources: ", f); - if (usedfonts.empty()) - fputs("none\n", f); - else { - usedfonts.erase(0, 4); - fputs(usedfonts.c_str(), f); - } - fprintf(f, "%%%%Pages: %d\n", page + 1); - fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", - minx / 20, miny / 20, maxx / 20, maxy / 20); - fputs("%%EOF\n", f); + fputs( + "%%Trailer\n" + "%%DocumentNeededResources: ", f); + if (usedfonts.empty()) + fputs("none\n", f); + else { + usedfonts.erase(0, 4); + fputs(usedfonts.c_str(), f); + } + fprintf(f, "%%%%Pages: %d\n", page + 1); + fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", + minx / 20, miny / 20, maxx / 20, maxy / 20); + fputs("%%EOF\n", f); } } @@ -786,143 +786,143 @@ service_loop() { bool jobLoop = true; while (jobLoop) { - bool spoolOpen = false; - bool pageStart = true; - bool cancelled = false; - bool jobEnd; - unsigned long plen; - int pageCount; - bufferStore buf; - bufferStore pageBuf; - int fd; - FILE *f; - unsigned char b; - char *jname = - (char *)malloc(strlen(spooldir) + - strlen(TEMPLATE) + 2); + bool spoolOpen = false; + bool pageStart = true; + bool cancelled = false; + bool jobEnd; + unsigned long plen; + int pageCount; + bufferStore buf; + bufferStore pageBuf; + int fd; + FILE *f; + unsigned char b; + char *jname = + (char *)malloc(strlen(spooldir) + + strlen(TEMPLATE) + 2); - while (jobLoop) { - /* Job loop */ - buf.init(); - switch (wPrt->getData(buf)) { - case rfsv::E_PSI_FILE_DISC: - jobLoop = false; - break; - case rfsv::E_PSI_GEN_NONE: - if ((buf.getLen() == 15) && - (!memcmp(buf.getString(0), fakePage, 15))) { - cancelled = false; - if (spoolOpen) { - fclose(f); - infolog("Cancelled job %s", jname); - unlink(jname); - break; - } - continue; - } - if (!spoolOpen && !cancelled) { - sprintf(jname, "%s/%s", spooldir, TEMPLATE); - if ((fd = mkstemp(jname)) != -1) { - infolog("Receiving new job %s", jname); - spoolOpen = true; - pageStart = true; - pageCount = 0; - } else { - errorlog("Could not create spool file."); - cancelled = true; - wPrt->cancelJob(); - } - f = fdopen(fd, "w"); - plen = 0; - } - b = buf.getByte(0); - if ((b != 0x2a) && (b != 0xff)) { - errorlog("Invalid packet type 0x%02x.", b); - cancelled = true; - wPrt->cancelJob(); - } - jobEnd = (b == 0xff); - if (!cancelled) { - buf.discardFirstBytes(1); - if (pageStart) { - b = buf.getByte(0); - plen = buf.getDWord(1) - 8; - buf.discardFirstBytes(5+8); - pageStart = false; - pageBuf.init(); - } - pageBuf.addBuff(buf); - plen -= buf.getLen(); - if (plen <= 0) { - convertPage(f, pageCount++, jobEnd, pageBuf); - pageBuf.init(); - pageStart = true; - } - } - if (jobEnd) { - if (spoolOpen) - fclose(f); - if (!cancelled) { - if (pageCount > 0) { - if (!printcmd) - infolog("Output stored in %s", jname); - else { - int r; - char cbuf[4096]; + while (jobLoop) { + /* Job loop */ + buf.init(); + switch (wPrt->getData(buf)) { + case rfsv::E_PSI_FILE_DISC: + jobLoop = false; + break; + case rfsv::E_PSI_GEN_NONE: + if ((buf.getLen() == 15) && + (!memcmp(buf.getString(0), fakePage, 15))) { + cancelled = false; + if (spoolOpen) { + fclose(f); + infolog("Cancelled job %s", jname); + unlink(jname); + break; + } + continue; + } + if (!spoolOpen && !cancelled) { + sprintf(jname, "%s/%s", spooldir, TEMPLATE); + if ((fd = mkstemp(jname)) != -1) { + infolog("Receiving new job %s", jname); + spoolOpen = true; + pageStart = true; + pageCount = 0; + } else { + errorlog("Could not create spool file."); + cancelled = true; + wPrt->cancelJob(); + } + f = fdopen(fd, "w"); + plen = 0; + } + b = buf.getByte(0); + if ((b != 0x2a) && (b != 0xff)) { + errorlog("Invalid packet type 0x%02x.", b); + cancelled = true; + wPrt->cancelJob(); + } + jobEnd = (b == 0xff); + if (!cancelled) { + buf.discardFirstBytes(1); + if (pageStart) { + b = buf.getByte(0); + plen = buf.getDWord(1) - 8; + buf.discardFirstBytes(5+8); + pageStart = false; + pageBuf.init(); + } + pageBuf.addBuff(buf); + plen -= buf.getLen(); + if (plen <= 0) { + convertPage(f, pageCount++, jobEnd, pageBuf); + pageBuf.init(); + pageStart = true; + } + } + if (jobEnd) { + if (spoolOpen) + fclose(f); + if (!cancelled) { + if (pageCount > 0) { + if (!printcmd) + infolog("Output stored in %s", jname); + else { + int r; + char cbuf[4096]; - infolog("Spooling %d pages", pageCount); - FILE *pipe = popen(printcmd, "w"); - if (!pipe) { - errorlog("Could not execute %s: %m", - printcmd); - unlink(jname); - } - f = fopen(jname, "r"); - if (!f) { - errorlog("Could not read %s: %m", - jname); - pclose(pipe); - unlink(jname); - } - while ((r = fread(cbuf, 1, - sizeof(cbuf), f)) > 0) - fwrite(cbuf, 1, r, pipe); - pclose(pipe); - fclose(f); - unlink(jname); - } - } - } else - unlink(jname); - spoolOpen = false; - } - break; - } - } - free(jname); + infolog("Spooling %d pages", pageCount); + FILE *pipe = popen(printcmd, "w"); + if (!pipe) { + errorlog("Could not execute %s: %m", + printcmd); + unlink(jname); + } + f = fopen(jname, "r"); + if (!f) { + errorlog("Could not read %s: %m", + jname); + pclose(pipe); + unlink(jname); + } + while ((r = fread(cbuf, 1, + sizeof(cbuf), f)) > 0) + fwrite(cbuf, 1, r, pipe); + pclose(pipe); + fclose(f); + unlink(jname); + } + } + } else + unlink(jname); + spoolOpen = false; + } + break; + } + } + free(jname); } } static void help() { cout << - "Options of plpprintd:\n" - "\n" - " -d, --debug Debugging, do not fork.\n" - " -h, --help Display this text.\n" - " -v, --verbose Increase verbosity.\n" - " -V, --version Print version and exit.\n" - " -p, --port=[HOST:]PORT Connect to port PORT on host HOST.\n" - " -s, --spooldir=DIR Specify spooldir DIR.\n" - " Default: " SPOOLDIR "\n" - " -c, --printcmd=CMD Specify print command.\n" - " Default: " PRINTCMD "\n"; + "Options of plpprintd:\n" + "\n" + " -d, --debug Debugging, do not fork.\n" + " -h, --help Display this text.\n" + " -v, --verbose Increase verbosity.\n" + " -V, --version Print version and exit.\n" + " -p, --port=[HOST:]PORT Connect to port PORT on host HOST.\n" + " -s, --spooldir=DIR Specify spooldir DIR.\n" + " Default: " SPOOLDIR "\n" + " -c, --printcmd=CMD Specify print command.\n" + " Default: " PRINTCMD "\n"; } static void usage() { cerr << "Usage: plpprintd [OPTIONS]" << endl - << "Use --help for more information" << endl; + << "Use --help for more information" << endl; } static struct option opts[] = { @@ -940,29 +940,29 @@ static void parse_destination(const char *arg, const char **host, int *port) { if (!arg) - return; + return; // We don't want to modify argv, therefore copy it first ... char *argcpy = strdup(arg); char *pp = strchr(argcpy, ':'); if (pp) { - // host.domain:400 - // 10.0.0.1:400 - *pp ++= '\0'; - *host = argcpy; + // host.domain:400 + // 10.0.0.1:400 + *pp ++= '\0'; + *host = argcpy; } else { - // 400 - // host.domain - // host - // 10.0.0.1 - if (strchr(argcpy, '.') || !isdigit(argcpy[0])) { - *host = argcpy; - pp = 0L; - } else - pp = argcpy; + // 400 + // host.domain + // host + // 10.0.0.1 + if (strchr(argcpy, '.') || !isdigit(argcpy[0])) { + *host = argcpy; + pp = 0L; + } else + pp = argcpy; } if (pp) - *port = atoi(pp); + *port = atoi(pp); } int @@ -978,100 +978,100 @@ main(int argc, char **argv) struct servent *se = getservbyname("psion", "tcp"); endservent(); if (se != 0L) - sockNum = ntohs(se->s_port); + sockNum = ntohs(se->s_port); while (1) { - c = getopt_long(argc, argv, "dhVvp:s:c:", opts, NULL); - if (c == -1) - break; - switch (c) { - case '?': - usage(); - return -1; - case 'd': - debug = true; - break; - case 'v': - verbose++; - break; - case 'V': - cout << "plpprintd Version " << VERSION << endl; - return 0; - case 'h': - help(); - return 0; - case 'p': - parse_destination(optarg, &host, &sockNum); - break; - case 's': - spooldir = strdup(optarg); - break; - case 'c': - if (!strcmp(optarg, "-")) - printcmd = NULL; - else - printcmd = strdup(optarg); - break; - } + c = getopt_long(argc, argv, "dhVvp:s:c:", opts, NULL); + if (c == -1) + break; + switch (c) { + case '?': + usage(); + return -1; + case 'd': + debug = true; + break; + case 'v': + verbose++; + break; + case 'V': + cout << "plpprintd Version " << VERSION << endl; + return 0; + case 'h': + help(); + return 0; + case 'p': + parse_destination(optarg, &host, &sockNum); + break; + case 's': + spooldir = strdup(optarg); + break; + case 'c': + if (!strcmp(optarg, "-")) + printcmd = NULL; + else + printcmd = strdup(optarg); + break; + } } if (optind < argc) { - usage(); - return -1; + usage(); + return -1; } skt = new ppsocket(); if (!skt->connect(host, sockNum)) { - cout << _("plpprintd: could not connect to ncpd") << endl; - return 1; + cout << _("plpprintd: could not connect to ncpd") << endl; + return 1; } if (!debug) - ret = fork(); + ret = fork(); switch (ret) { - case 0: - /* child */ - setsid(); - chdir("/"); - if (!debug) { - openlog("plpprintd", LOG_PID|LOG_CONS, LOG_DAEMON); - int devnull = - open("/dev/null", O_RDWR, 0); - if (devnull != -1) { - dup2(devnull, STDIN_FILENO); - dup2(devnull, STDOUT_FILENO); - dup2(devnull, STDERR_FILENO); - if (devnull > 2) - close(devnull); - } - } - init_fontmap(); - infolog("started, waiting for requests."); - serviceLoop = true; - while (serviceLoop) { - wPrt = new wprt(skt); - if (wPrt) { - Enum ret; - ret = wPrt->initPrinter(); - if (ret == rfsv::E_PSI_GEN_NONE) - service_loop(); - else { - if (debug) - debuglog("plpprintd: could not connect: %s", - ret.toString().c_str()); - } - delete wPrt; - sleep(1); - } else { - errorlog("plpprintd: Could not create wprt object"); - exit(1); - } - } - break; - case -1: - cerr << "plpprintd: fork failed" << endl; - return 1; - default: - /* parent */ - break; + case 0: + /* child */ + setsid(); + chdir("/"); + if (!debug) { + openlog("plpprintd", LOG_PID|LOG_CONS, LOG_DAEMON); + int devnull = + open("/dev/null", O_RDWR, 0); + if (devnull != -1) { + dup2(devnull, STDIN_FILENO); + dup2(devnull, STDOUT_FILENO); + dup2(devnull, STDERR_FILENO); + if (devnull > 2) + close(devnull); + } + } + init_fontmap(); + infolog("started, waiting for requests."); + serviceLoop = true; + while (serviceLoop) { + wPrt = new wprt(skt); + if (wPrt) { + Enum ret; + ret = wPrt->initPrinter(); + if (ret == rfsv::E_PSI_GEN_NONE) + service_loop(); + else { + if (debug) + debuglog("plpprintd: could not connect: %s", + ret.toString().c_str()); + } + delete wPrt; + sleep(1); + } else { + errorlog("plpprintd: Could not create wprt object"); + exit(1); + } + } + break; + case -1: + cerr << "plpprintd: fork failed" << endl; + return 1; + default: + /* parent */ + break; } return 0; } diff --git a/sisinstall/sisinstaller.cpp b/sisinstall/sisinstaller.cpp index d159496..fdb63f9 100644 --- a/sisinstall/sisinstaller.cpp +++ b/sisinstall/sisinstaller.cpp @@ -16,225 +16,224 @@ static int continueRunning; static int checkAbortHash(void *, u_int32_t) { - if (continueRunning) - { - if (logLevel >= 1) - { - printf("#"); - fflush(stdout); - } - } - return continueRunning; + if (continueRunning) + { + if (logLevel >= 1) + { + printf("#"); + fflush(stdout); + } + } + return continueRunning; } SISInstaller::SISInstaller() { - m_installed = 0; - m_ownInstalled = false; + m_installed = 0; + m_ownInstalled = false; } SISInstaller::~SISInstaller() { - if (m_ownInstalled) - { - SISFileLink* curr = m_installed; - while (curr) - { - delete curr->m_file; - SISFileLink* next = curr->m_next; - delete curr; - curr = next; - } - } + if (m_ownInstalled) + { + SISFileLink* curr = m_installed; + while (curr) + { + delete curr->m_file; + SISFileLink* next = curr->m_next; + delete curr; + curr = next; + } + } } void SISInstaller::createDirs(char* filename) { - char* end = filename + strlen(filename); - while (--end > filename) - { - char ch = *end; - if ((ch == '/') || (ch == '\\')) - { - *end = 0; - if (logLevel >= 1) - fprintf(stderr, "Checking for existance of %s\n", filename); + char* end = filename + strlen(filename); + while (--end > filename) + { + char ch = *end; + if ((ch == '/') || (ch == '\\')) + { + *end = 0; + if (logLevel >= 1) + fprintf(stderr, "Checking for existance of %s\n", filename); // if (!m_psion->dirExists(filename)) - { - if (logLevel >= 1) - fprintf(stderr, "Creating dir %s\n", filename); - Enum res; - res = m_psion->mkdir(filename); - if ((res != rfsv::E_PSI_GEN_NONE) && - (res != rfsv::E_PSI_FILE_EXIST)) - { - fprintf(stderr, " -> Failed: %s\n", (const char*)res); - } - } - *end = ch; - return; - } - } + { + if (logLevel >= 1) + fprintf(stderr, "Creating dir %s\n", filename); + Enum res; + res = m_psion->mkdir(filename); + if ((res != rfsv::E_PSI_GEN_NONE) && + (res != rfsv::E_PSI_FILE_EXIST)) + { + fprintf(stderr, " -> Failed: %s\n", (const char*)res); + } + } + *end = ch; + return; + } + } } void SISInstaller::copyFile(SISFileRecord* fileRecord) { - uint8_t* destptr = fileRecord->getDestPtr(); - if (destptr == 0) - return; - if (destptr[0] == '!') - { - if (m_drive == 0) - selectDrive(); - m_file->setDrive(m_drive); - } - int len = fileRecord->m_destLength; - char dest[256]; - memcpy(dest, destptr, len); - dest[len] = 0; - if (dest[0] == '!') - { - dest[0] = m_drive; - fileRecord->setMainDrive(m_drive); - if (logLevel >= 2) - fprintf(stderr, "Setting drive at index %d to %c\n", - destptr, m_drive); - } - - char msgbuf[1024]; - sprintf(msgbuf, - "Copying %d bytes to %s\n", - fileRecord->m_fileLengths[m_fileNo], dest); - { - printf("%s\n", msgbuf); - } - - copyBuf(fileRecord->getFilePtr(m_fileNo), - fileRecord->m_fileLengths[m_fileNo], - dest); + uint8_t* destptr = fileRecord->getDestPtr(); + if (destptr == 0) + return; + if (destptr[0] == '!') + { + if (m_drive == 0) + selectDrive(); + m_file->setDrive(m_drive); + } + int len = fileRecord->m_destLength; + char dest[256]; + memcpy(dest, destptr, len); + dest[len] = 0; + if (dest[0] == '!') + { + dest[0] = m_drive; + fileRecord->setMainDrive(m_drive); + if (logLevel >= 2) + fprintf(stderr, "Setting main drive to %c\n", m_drive); + } + + char msgbuf[1024]; + sprintf(msgbuf, + "Copying %d bytes to %s\n", + fileRecord->m_fileLengths[m_fileNo], dest); + { + printf("%s\n", msgbuf); + } + + copyBuf(fileRecord->getFilePtr(m_fileNo), + fileRecord->m_fileLengths[m_fileNo], + dest); } void SISInstaller::copyBuf(const uint8_t* buf, int len, char* name) { - createDirs(name); - char srcName[32]; - strcpy(srcName, "/tmp/plptools-sis-XXXXXX"); - int fd = mkstemp(srcName); - if (-1 == fd) - { - fprintf(stderr, - "Couldn't create temp file: %s\n", strerror(errno)); - return; - } - Enum res; - if (logLevel >= 2) - fprintf(stderr, "Storing in %s\n", srcName); - write(fd, buf, len); - close(fd); - continueRunning = 1; - res = m_psion->copyToPsion(srcName, name, NULL, checkAbortHash); - if (res == rfsv::E_PSI_GEN_NONE) - { - if (logLevel >= 1) - fprintf(stderr, " -> Success.\n"); - } - else - { - fprintf(stderr, " -> Fail: %s\n", (const char*)res); - } - unlink(srcName); + createDirs(name); + char srcName[32]; + strcpy(srcName, "/tmp/plptools-sis-XXXXXX"); + int fd = mkstemp(srcName); + if (-1 == fd) + { + fprintf(stderr, + "Couldn't create temp file: %s\n", strerror(errno)); + return; + } + Enum res; + if (logLevel >= 2) + fprintf(stderr, "Storing in %s\n", srcName); + write(fd, buf, len); + close(fd); + continueRunning = 1; + res = m_psion->copyToPsion(srcName, name, NULL, checkAbortHash); + if (res == rfsv::E_PSI_GEN_NONE) + { + if (logLevel >= 1) + fprintf(stderr, " -> Success.\n"); + } + else + { + fprintf(stderr, " -> Fail: %s\n", (const char*)res); + } + unlink(srcName); } int SISInstaller::installFile(SISFileRecord* fileRecord) { - char readbuf[8]; - switch (fileRecord->m_fileType) - { - case 0: - copyFile(fileRecord); - break; - case 1: - - { - printf("Info:\n%.*s\n", - fileRecord->m_fileLengths[m_fileNo], - fileRecord->getFilePtr(m_fileNo)); - switch (fileRecord->m_fileDetails) - { - case 0: - printf("Continue\n"); - fgets(readbuf, sizeof(readbuf), stdin); - break; - case 1: - printf("(Install next file?) [Y]es/No\n"); - fgets(readbuf, sizeof(readbuf), stdin); - if (strchr("Nn", readbuf[0])) - { - return FILE_SKIP; - } - break; - case 2: - printf("(Continue installation?) [Y]es/No\n"); - fgets(readbuf, sizeof(readbuf), stdin); - if (strchr("Nn", readbuf[0])) - { - // Watch out if we have copied any files - // already. - // - return FILE_ABORT; - } - break; - } - } - break; - case 2: - { - if (logLevel >= 1) - fprintf(stderr, "Recursive sis file...\n"); - SISFile sisFile; - uint8_t* buf2 = fileRecord->getFilePtr(m_fileNo); - off_t len = fileRecord->m_fileLengths[m_fileNo]; + char readbuf[8]; + switch (fileRecord->m_fileType) + { + case 0: + copyFile(fileRecord); + break; + case 1: + + { + printf("Info:\n%.*s\n", + fileRecord->m_fileLengths[m_fileNo], + fileRecord->getFilePtr(m_fileNo)); + switch (fileRecord->m_fileDetails) + { + case 0: + printf("Continue\n"); + fgets(readbuf, sizeof(readbuf), stdin); + break; + case 1: + printf("(Install next file?) [Y]es/No\n"); + fgets(readbuf, sizeof(readbuf), stdin); + if (strchr("Nn", readbuf[0])) + { + return FILE_SKIP; + } + break; + case 2: + printf("(Continue installation?) [Y]es/No\n"); + fgets(readbuf, sizeof(readbuf), stdin); + if (strchr("Nn", readbuf[0])) + { + // Watch out if we have copied any files + // already. + // + return FILE_ABORT; + } + break; + } + } + break; + case 2: + { + if (logLevel >= 1) + fprintf(stderr, "Recursive sis file...\n"); + SISFile sisFile; + uint8_t* buf2 = fileRecord->getFilePtr(m_fileNo); + off_t len = fileRecord->m_fileLengths[m_fileNo]; // if (m_lastSisFile < fileptr + len) // m_lastSisFile = fileptr + len; - SisRC rc = sisFile.fillFrom(buf2, len); - if (rc != SIS_OK) - { - fprintf(stderr, - "Could not read contained sis file, rc = %d\n", rc); - break; - } - SISInstaller installer; - installer.setPsion(m_psion); - installer.setInstalled(m_installed); - rc = installer.run(&sisFile, buf2, len, m_file); - if (0 == m_drive) - { - m_drive = sisFile.m_header.m_installationDrive; - m_file->setDrive(m_drive); - if (logLevel >= 1) - fprintf(stderr, - "Updated drive to %c from recursive sis file\n", - m_drive); - } - break; - } - case 3: - if (logLevel >= 1) - fprintf(stderr, "Run %.*s during installation/remove\n", - fileRecord->m_destLength, fileRecord->getDestPtr()); - break; - case 4: - if (logLevel >= 2) - fprintf(stderr, "Running the app will create %.*s\n", - fileRecord->m_destLength, fileRecord->getDestPtr()); - break; - } - return FILE_OK; + SisRC rc = sisFile.fillFrom(buf2, len); + if (rc != SIS_OK) + { + fprintf(stderr, + "Could not read contained sis file, rc = %d\n", rc); + break; + } + SISInstaller installer; + installer.setPsion(m_psion); + installer.setInstalled(m_installed); + rc = installer.run(&sisFile, buf2, len, m_file); + if (0 == m_drive) + { + m_drive = sisFile.m_header.m_installationDrive; + m_file->setDrive(m_drive); + if (logLevel >= 1) + fprintf(stderr, + "Updated drive to %c from recursive sis file\n", + m_drive); + } + break; + } + case 3: + if (logLevel >= 1) + fprintf(stderr, "Run %.*s during installation/remove\n", + fileRecord->m_destLength, fileRecord->getDestPtr()); + break; + case 4: + if (logLevel >= 2) + fprintf(stderr, "Running the app will create %.*s\n", + fileRecord->m_destLength, fileRecord->getDestPtr()); + break; + } + return FILE_OK; } #define SYSTEMINSTALL "c:\\system\\install\\" @@ -242,428 +241,428 @@ SISInstaller::installFile(SISFileRecord* fileRecord) SisRC SISInstaller::loadInstalled() { - PlpDir files; - Enum res; - - if ((res = m_psion->dir(SYSTEMINSTALL, files)) != rfsv::E_PSI_GEN_NONE) - { - return SIS_FAILED; - } - else - { - while (!files.empty()) - { - PlpDirent file = files[0]; - if (logLevel >= 1) - fprintf(stderr, "Loading sis file `%s'\n", file.getName()); - char sisname[256]; - sprintf(sisname, "%s%s", SYSTEMINSTALL, file.getName()); - loadPsionSis(sisname); - files.pop_front(); - } - } + PlpDir files; + Enum res; + + if ((res = m_psion->dir(SYSTEMINSTALL, files)) != rfsv::E_PSI_GEN_NONE) + { + return SIS_FAILED; + } + else + { + while (!files.empty()) + { + PlpDirent file = files[0]; + if (logLevel >= 1) + fprintf(stderr, "Loading sis file `%s'\n", file.getName()); + char sisname[256]; + sprintf(sisname, "%s%s", SYSTEMINSTALL, file.getName()); + loadPsionSis(sisname); + files.pop_front(); + } + } } void SISInstaller::loadPsionSis(const char* name) { - char srcName[32]; - strcpy(srcName, "/tmp/plptools-sis-XXXXXX"); - int fd = mkstemp(srcName); - if (-1 == fd) - { - fprintf(stderr, "Couldn't create temp file: %s\n", strerror(errno)); - return; - } - Enum res; - continueRunning = 1; - if (logLevel >= 2) - fprintf(stderr, "Copying from %s to temp file %s\n", name, srcName); - res = m_psion->copyFromPsion(name, fd, checkAbortHash); - if (res == rfsv::E_PSI_GEN_NONE) - { - off_t fileLen = lseek(fd, 0, SEEK_END); - if (logLevel >= 2) - fprintf(stderr, "Read %d bytes from the Psion file %s\n", - (int)fileLen, name); - lseek(fd, SEEK_SET, 0); - uint8_t* sisbuf = new uint8_t[fileLen]; - int rc = read(fd, sisbuf, fileLen); - if (rc == fileLen) - { - SISFile* sisFile = new SISFile(); - SisRC rc2 = sisFile->fillFrom(sisbuf, fileLen); - if (rc2 == SIS_OK) - { - if (logLevel >= 1) - fprintf(stderr, " Ok.\n"); - SISFileLink* link = new SISFileLink(sisFile); - link->m_next = m_installed; - m_ownInstalled = true; - m_installed = link; - sisFile->ownBuffer(); - } - else - { - delete sisFile; - delete[] sisbuf; - } - } - } - close(fd); - unlink(srcName); + char srcName[32]; + strcpy(srcName, "/tmp/plptools-sis-XXXXXX"); + int fd = mkstemp(srcName); + if (-1 == fd) + { + fprintf(stderr, "Couldn't create temp file: %s\n", strerror(errno)); + return; + } + Enum res; + continueRunning = 1; + if (logLevel >= 2) + fprintf(stderr, "Copying from %s to temp file %s\n", name, srcName); + res = m_psion->copyFromPsion(name, fd, checkAbortHash); + if (res == rfsv::E_PSI_GEN_NONE) + { + off_t fileLen = lseek(fd, 0, SEEK_END); + if (logLevel >= 2) + fprintf(stderr, "Read %d bytes from the Psion file %s\n", + (int)fileLen, name); + lseek(fd, SEEK_SET, 0); + uint8_t* sisbuf = new uint8_t[fileLen]; + int rc = read(fd, sisbuf, fileLen); + if (rc == fileLen) + { + SISFile* sisFile = new SISFile(); + SisRC rc2 = sisFile->fillFrom(sisbuf, fileLen); + if (rc2 == SIS_OK) + { + if (logLevel >= 1) + fprintf(stderr, " Ok.\n"); + SISFileLink* link = new SISFileLink(sisFile); + link->m_next = m_installed; + m_ownInstalled = true; + m_installed = link; + sisFile->ownBuffer(); + } + else + { + delete sisFile; + delete[] sisbuf; + } + } + } + close(fd); + unlink(srcName); } void SISInstaller::removeFile(SISFileRecord* fileRecord) { - int len = fileRecord->m_destLength; - char dest[256]; - memcpy(dest, fileRecord->getDestPtr(), len); - dest[len] = 0; - if (logLevel >= 1) - fprintf(stderr, "Removing file component %s.\n", dest); - m_psion->remove(dest); + int len = fileRecord->m_destLength; + char dest[256]; + memcpy(dest, fileRecord->getDestPtr(), len); + dest[len] = 0; + if (logLevel >= 1) + fprintf(stderr, "Removing file component %s.\n", dest); + m_psion->remove(dest); } SisRC SISInstaller::run(SISFile* file, uint8_t* buf, off_t len) { - return run(file, buf, len, 0); + return run(file, buf, len, 0); } SisRC SISInstaller::run(SISFile* file, uint8_t* buf, off_t len, SISFile* parent) { - int n; - long lang; - m_file = file; - m_buf = buf; - char msgbuf[1024]; - if (parent == 0) - { - n = m_file->m_header.m_nlangs; - if (n == 1) - { - sprintf(msgbuf, - _("You have only one language: %s"), - m_file->getLanguage(0)->m_name); - printf("%s\n", msgbuf); - lang = 0; - } - else - { - printf("Select a language (%d alternatives):\n", n); - for (int i = 0; i < n; ++i) - printf(" %d. %s\n", i, m_file->getLanguage(i)->m_name); - lang = 0; - } - } - else - { + int n; + long lang; + m_file = file; + m_buf = buf; + char msgbuf[1024]; + if (parent == 0) + { + n = m_file->m_header.m_nlangs; + if (n == 1) + { + sprintf(msgbuf, + _("You have only one language: %s"), + m_file->getLanguage(0)->m_name); + printf("%s\n", msgbuf); + lang = 0; + } + else + { + printf("Select a language (%d alternatives):\n", n); + for (int i = 0; i < n; ++i) + printf(" %d. %s\n", i, m_file->getLanguage(i)->m_name); + lang = 0; + } + } + else + { // This needs to check the _name_ of the language, since the // recursive sis file might have a different language list. // For now, defalt to 0. // lang = parent->getLanguage(); - lang = 0; - if (logLevel >= 1) - fprintf(stderr, "Forcing language to %d\n", lang); - } - m_file->setLanguage(lang); - uint8_t* compName = m_file->getName(); - sprintf(msgbuf, _("Installing component: `%s'"), compName); + lang = 0; + if (logLevel >= 1) + fprintf(stderr, "Forcing language to %ld\n", lang); + } + m_file->setLanguage(lang); + uint8_t* compName = m_file->getName(); + sprintf(msgbuf, _("Installing component: `%s'"), compName); printf("%s\n", msgbuf); - // In order to check requisites and previous versions, we need to - // load all sis files from the c:/system/install directory. - // This is the only way to find out if a specific application or - // library has been loaded, since the sis file names could be just - // about anything. - // - if (m_installed == 0) - loadInstalled(); - - // Check Requisites. - // - n = m_file->m_header.m_nreqs; - if (logLevel >= 1) - fprintf(stderr, "Found %d requisites, of some sort.\n", n); - for (int i = 0; i < n; ++i) - { - SISReqRecord* reqRecord = &m_file->m_reqRecords[i]; - if (logLevel >= 1) - fprintf(stderr, - " Check if app with uid %08x exists with version >= %d.%d\n", - reqRecord->m_uid, reqRecord->m_major, reqRecord->m_minor); - } - - // Check previous version. - // - if (logLevel >= 1) - fprintf(stderr, + // In order to check requisites and previous versions, we need to + // load all sis files from the c:/system/install directory. + // This is the only way to find out if a specific application or + // library has been loaded, since the sis file names could be just + // about anything. + // + if (m_installed == 0) + loadInstalled(); + + // Check Requisites. + // + n = m_file->m_header.m_nreqs; + if (logLevel >= 1) + fprintf(stderr, "Found %d requisites, of some sort.\n", n); + for (int i = 0; i < n; ++i) + { + SISReqRecord* reqRecord = &m_file->m_reqRecords[i]; + if (logLevel >= 1) + fprintf(stderr, + " Check if app with uid %08x exists with version >= %d.%d\n", + reqRecord->m_uid, reqRecord->m_major, reqRecord->m_minor); + } + + // Check previous version. + // + if (logLevel >= 1) + fprintf(stderr, "Checking if this app (uid %08x) exists with a version less than %d.%d.\n", - m_file->m_header.m_uid1, - m_file->m_header.m_major, - m_file->m_header.m_minor); - - bool uninstallFirst = false; - SISFileLink* curr = m_installed; - SISFile* oldFile = 0; - while (curr) - { - SISFile* sisFile = curr->m_file; - switch (sisFile->compareApp(m_file)) - { - case SIS_VER_EARLIER: - uninstallFirst = true; - oldFile = sisFile; - break; - - case SIS_SAME_OR_LATER: - // Ask for confirmation. - uninstallFirst = true; - oldFile = sisFile; - break; - - case SIS_OTHER_VARIANT: - // Ask for confirmation. - uninstallFirst = true; - oldFile = sisFile; - break; - } - curr = curr->m_next; - } - - if (uninstallFirst) - { + m_file->m_header.m_uid1, + m_file->m_header.m_major, + m_file->m_header.m_minor); + + bool uninstallFirst = false; + SISFileLink* curr = m_installed; + SISFile* oldFile = 0; + while (curr) + { + SISFile* sisFile = curr->m_file; + switch (sisFile->compareApp(m_file)) + { + case SIS_VER_EARLIER: + uninstallFirst = true; + oldFile = sisFile; + break; + + case SIS_SAME_OR_LATER: + // Ask for confirmation. + uninstallFirst = true; + oldFile = sisFile; + break; + + case SIS_OTHER_VARIANT: + // Ask for confirmation. + uninstallFirst = true; + oldFile = sisFile; + break; + } + curr = curr->m_next; + } + + if (uninstallFirst) + { // printf("You should uninstall the previous version first.\n"); // if (!m_forced) // return SIS_ABORTED; // printf("Forced mode... Installing anyway!\n"); - if (oldFile == 0) - fprintf(stderr, "Already installed, but 0?\n"); - else - { - sprintf(msgbuf, _("Uninstalling the previous version first.")); + if (oldFile == 0) + fprintf(stderr, "Already installed, but 0?\n"); + else + { + sprintf(msgbuf, "%s", _("Uninstalling the previous version first.")); printf("%s\n", msgbuf); - uninstall(oldFile); - } - } - - // Install file components. - // - n = m_file->m_header.m_nfiles; - if (logLevel >= 1) - fprintf(stderr, "Found %d files.\n", n); - m_drive = (parent == 0) ? 0 : parent->m_header.m_installationDrive; - int nCopiedFiles = 0; - m_lastSisFile = 0; - bool skipnext = false; - bool aborted = false; - while (!aborted && (n-- > 0)) - { - SISFileRecord* fileRecord = &m_file->m_fileRecords[n]; - m_fileNo = (fileRecord->m_flags & 1) ? lang : 0; - - if (skipnext) - { - skipnext = false; - } - else - { - switch (installFile(fileRecord)) - { - case FILE_OK: - break; - case FILE_SKIP: - skipnext = true; - break; - case FILE_ABORT: - aborted = true; - break; - } - } - if (!aborted) - nCopiedFiles++; - } - m_file->setFiles(nCopiedFiles); - if (logLevel >= 1) - fprintf(stderr, - "Installed %d files of %d, cutting at offset %d.\n", - m_file->m_header.m_installationFiles, - m_file->m_header.m_nfiles, - m_file->getResidualEnd()); - if (nCopiedFiles == 0) - { - // There is no need to copy any uninstall information to the - // psion, unless we've actually copied anything there. - // - return SIS_ABORTED; - } - - // Copy the updated sis file to the epoc machine. - // - char resname[256]; - int namelen = 0; - while (compName[namelen] != 0) - { - if (compName[namelen] == ' ') - break; - namelen++; - } - sprintf(resname, "C:\\System\\Install\\%.*s.sis", namelen, compName); - if (logLevel >= 1) - fprintf(stderr, "Creating residual sis file %s\n", resname); - copyBuf(buf, m_file->getResidualEnd(), resname); - if (aborted) - return SIS_ABORTED; - return SIS_OK; + uninstall(oldFile); + } + } + + // Install file components. + // + n = m_file->m_header.m_nfiles; + if (logLevel >= 1) + fprintf(stderr, "Found %d files.\n", n); + m_drive = (parent == 0) ? 0 : parent->m_header.m_installationDrive; + int nCopiedFiles = 0; + m_lastSisFile = 0; + bool skipnext = false; + bool aborted = false; + while (!aborted && (n-- > 0)) + { + SISFileRecord* fileRecord = &m_file->m_fileRecords[n]; + m_fileNo = (fileRecord->m_flags & 1) ? lang : 0; + + if (skipnext) + { + skipnext = false; + } + else + { + switch (installFile(fileRecord)) + { + case FILE_OK: + break; + case FILE_SKIP: + skipnext = true; + break; + case FILE_ABORT: + aborted = true; + break; + } + } + if (!aborted) + nCopiedFiles++; + } + m_file->setFiles(nCopiedFiles); + if (logLevel >= 1) + fprintf(stderr, + "Installed %d files of %d, cutting at offset %d.\n", + m_file->m_header.m_installationFiles, + m_file->m_header.m_nfiles, + m_file->getResidualEnd()); + if (nCopiedFiles == 0) + { + // There is no need to copy any uninstall information to the + // psion, unless we've actually copied anything there. + // + return SIS_ABORTED; + } + + // Copy the updated sis file to the epoc machine. + // + char resname[256]; + int namelen = 0; + while (compName[namelen] != 0) + { + if (compName[namelen] == ' ') + break; + namelen++; + } + sprintf(resname, "C:\\System\\Install\\%.*s.sis", namelen, compName); + if (logLevel >= 1) + fprintf(stderr, "Creating residual sis file %s\n", resname); + copyBuf(buf, m_file->getResidualEnd(), resname); + if (aborted) + return SIS_ABORTED; + return SIS_OK; } void SISInstaller::selectDrive() { - u_int32_t devbits = 0; - Enum res; - char drivelist[26]; - int space[26]; - int size[26]; - int ndrives = 0; - if ((res = m_psion->devlist(devbits)) == rfsv::E_PSI_GEN_NONE) - { - for (int i = 0; i < 26; i++) - { - PlpDrive plpdrive; - if (((devbits & 1) != 0) && - (m_psion->devinfo(i + 'A', plpdrive) == rfsv::E_PSI_GEN_NONE)) - { - u_int32_t mediaType = plpdrive.getMediaType(); - if ((mediaType == 3) || (mediaType == 5)) - { - drivelist[ndrives] = 'A' + i; - space[ndrives] = plpdrive.getSpace(); - size[ndrives] = plpdrive.getSize(); - printf("%c: %d bytes free, %d bytes total\n", + u_int32_t devbits = 0; + Enum res; + char drivelist[26]; + int space[26]; + int size[26]; + int ndrives = 0; + if ((res = m_psion->devlist(devbits)) == rfsv::E_PSI_GEN_NONE) + { + for (int i = 0; i < 26; i++) + { + PlpDrive plpdrive; + if (((devbits & 1) != 0) && + (m_psion->devinfo(i + 'A', plpdrive) == rfsv::E_PSI_GEN_NONE)) + { + u_int32_t mediaType = plpdrive.getMediaType(); + if ((mediaType == 3) || (mediaType == 5)) + { + drivelist[ndrives] = 'A' + i; + space[ndrives] = plpdrive.getSpace(); + size[ndrives] = plpdrive.getSize(); + printf("%c: %lld bytes free, %lld bytes total\n", 'A' + i, plpdrive.getSpace(), plpdrive.getSize()); - ++ndrives; - } - } - devbits >>= 1; - } - } - drivelist[ndrives] = 0; - if (ndrives == 0) - { - m_drive = 'C'; - if (logLevel >= 1) - printf("Selecting the only drive %c\n", m_drive); - } - else if (ndrives == 1) - { - m_drive = drivelist[0]; - if (logLevel >= 1) - printf("Selecting the only drive %c\n", m_drive); - } - else - { - - { - printf("Please select a drive\n"); - char ch; - char readbuf[2]; - while (m_drive == 0) - { - fgets(readbuf, 2, stdin); - ch = readbuf[0]; - if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) - { - m_drive = toupper(ch); - if (!strchr(drivelist, m_drive)) - { - m_drive = 0; - printf("Please select a valid drive: %s\n", drivelist); - } - } - } - } - - } + ++ndrives; + } + } + devbits >>= 1; + } + } + drivelist[ndrives] = 0; + if (ndrives == 0) + { + m_drive = 'C'; + if (logLevel >= 1) + printf("Selecting the only drive %c\n", m_drive); + } + else if (ndrives == 1) + { + m_drive = drivelist[0]; + if (logLevel >= 1) + printf("Selecting the only drive %c\n", m_drive); + } + else + { + + { + printf("Please select a drive\n"); + char ch; + char readbuf[2]; + while (m_drive == 0) + { + fgets(readbuf, 2, stdin); + ch = readbuf[0]; + if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) + { + m_drive = toupper(ch); + if (!strchr(drivelist, m_drive)) + { + m_drive = 0; + printf("Please select a valid drive: %s\n", drivelist); + } + } + } + } + + } } void SISInstaller::setPsion(Psion* psion) { - m_psion = psion; + m_psion = psion; } void SISInstaller::uninstall(SISFile* file) { - int n = file->m_header.m_nfiles; - int fileix = n - file->m_header.m_installationFiles; - if (logLevel >= 1) - fprintf(stderr, "Uninstalling %d files, from a total of %d.\n", - file->m_header.m_installationFiles, - file->m_header.m_nfiles); - int lang = file->getLanguage(); - while (fileix < n) - { - SISFileRecord* fileRecord = &file->m_fileRecords[fileix]; - m_fileNo = (fileRecord->m_flags & 1) ? lang : 0; - char drive = file->m_header.m_installationDrive; - fileRecord->setMainDrive(drive); - uninstallFile(fileRecord); - ++fileix; - } + int n = file->m_header.m_nfiles; + int fileix = n - file->m_header.m_installationFiles; + if (logLevel >= 1) + fprintf(stderr, "Uninstalling %d files, from a total of %d.\n", + file->m_header.m_installationFiles, + file->m_header.m_nfiles); + int lang = file->getLanguage(); + while (fileix < n) + { + SISFileRecord* fileRecord = &file->m_fileRecords[fileix]; + m_fileNo = (fileRecord->m_flags & 1) ? lang : 0; + char drive = file->m_header.m_installationDrive; + fileRecord->setMainDrive(drive); + uninstallFile(fileRecord); + ++fileix; + } } void SISInstaller::uninstallFile(SISFileRecord* fileRecord) { - switch (fileRecord->m_fileType) - { - case 0: - case 4: - removeFile(fileRecord); - break; - case 2: - { + switch (fileRecord->m_fileType) + { + case 0: + case 4: + removeFile(fileRecord); + break; + case 2: + { #if 0 - // This is messy... We can't remove the sis component unless - // we've stored the entire component in the residual sis - // file on the target machine. - if (logLevel >= 1) - fprintf(stderr, "Recursive sis file...\n"); - SISFile sisFile; - int fileptr = fileRecord->m_filePtrs[m_fileNo]; - uint8_t* buf2 = m_buf + fileptr; - off_t len = fileRecord->m_fileLengths[m_fileNo]; - if (m_lastSisFile < fileptr + len) - m_lastSisFile = fileptr + len; - SisRC rc = sisFile.fillFrom(buf2, len); - if (rc != SIS_OK) - { - fprintf(stderr, - "Could not read contained sis file, rc = %d\n", rc); - break; - } - SISInstaller installer; - installer.setPsion(m_psion); - installer.setInstalled(m_installed); - rc = installer.run(&sisFile, buf2, len, m_file); - if (0 == m_drive) - { - m_drive = sisFile.m_header.m_installationDrive; - m_file->setDrive(m_drive); - if (logLevel >= 1) - fprintf(stderr, - "Updated drive to %c from recursive sis file\n", - m_drive); - } + // This is messy... We can't remove the sis component unless + // we've stored the entire component in the residual sis + // file on the target machine. + if (logLevel >= 1) + fprintf(stderr, "Recursive sis file...\n"); + SISFile sisFile; + int fileptr = fileRecord->m_filePtrs[m_fileNo]; + uint8_t* buf2 = m_buf + fileptr; + off_t len = fileRecord->m_fileLengths[m_fileNo]; + if (m_lastSisFile < fileptr + len) + m_lastSisFile = fileptr + len; + SisRC rc = sisFile.fillFrom(buf2, len); + if (rc != SIS_OK) + { + fprintf(stderr, + "Could not read contained sis file, rc = %d\n", rc); + break; + } + SISInstaller installer; + installer.setPsion(m_psion); + installer.setInstalled(m_installed); + rc = installer.run(&sisFile, buf2, len, m_file); + if (0 == m_drive) + { + m_drive = sisFile.m_header.m_installationDrive; + m_file->setDrive(m_drive); + if (logLevel >= 1) + fprintf(stderr, + "Updated drive to %c from recursive sis file\n", + m_drive); + } #endif - break; - } - } + break; + } + } } diff --git a/sisinstall/sismain.cpp b/sisinstall/sismain.cpp index 479fbf8..c5f6e05 100644 --- a/sisinstall/sismain.cpp +++ b/sisinstall/sismain.cpp @@ -1,4 +1,3 @@ - #include "sisfile.h" #include "sisinstaller.h" #include "psion.h" @@ -20,122 +19,116 @@ static void error(int line) { - fprintf(stderr, _("Error %d on line %d: %s\n"), errno, line, - strerror(errno)); - exit(1); + fprintf(stderr, _("Error %d on line %d: %s\n"), errno, line, + strerror(errno)); + exit(1); } static struct option opts[] = { - { "help", no_argument, 0, 'h' }, - { "version", no_argument, 0, 'V' }, - { "verbose", required_argument, 0, 'v' }, - { "dry-run", no_argument, 0, 'n' }, - { NULL, 0, 0, 0 }, + { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'V' }, + { "verbose", required_argument, 0, 'v' }, + { "dry-run", no_argument, 0, 'n' }, + { NULL, 0, 0, 0 }, }; void printHelp() { - printf( - _("Usage: sisinstall [OPTIONS]... SISFILE\n" - "\n" - "Supported options:\n" - "\n" - " -h, --help Display this text.\n" - " -V, --version Print version and exit.\n" - " -v, --verbose=LEVEL Set the verbosity level, by default 0.\n" - " -n, --dry-run Just parse the file.\n" - )); + printf("%s", + _("Usage: sisinstall [OPTIONS]... SISFILE\n" + "\n" + "Supported options:\n" + "\n" + " -h, --help Display this text.\n" + " -V, --version Print version and exit.\n" + " -v, --verbose=LEVEL Set the verbosity level, by default 0.\n" + " -n, --dry-run Just parse the file.\n" + )); } int main(int argc, char* argv[]) { - char* filename = 0; - char option; - bool dryrun = false; + char* filename = 0; + char option; + bool dryrun = false; #ifdef LC_ALL - setlocale(LC_ALL, ""); + setlocale(LC_ALL, ""); #endif - textdomain(PACKAGE); + textdomain(PACKAGE); - while (1) - { - option = getopt_long(argc, argv, - "hnv:V" - , opts, NULL); - if (option == -1) - break; - switch (option) - { - case 'h': - case '?': - printHelp(); - exit(0); - case 'v': - logLevel = atoi(optarg); - break; - case 'n': - dryrun = true; - break; - case 'V': - printf(_("sisinstall version 0.1\n")); - exit(0); - } - } - if (optind < argc) - { - filename = argv[optind]; + while (1) + { + option = getopt_long(argc, argv, + "hnv:V" + , opts, NULL); + if (option == -1) + break; + switch (option) + { + case 'h': + case '?': + printHelp(); + exit(0); + case 'v': + logLevel = atoi(optarg); + break; + case 'n': + dryrun = true; + break; + case 'V': + printf("%s", _("sisinstall version 0.1\n")); + exit(0); + } + } + if (optind < argc) + { + filename = argv[optind]; printf(_("Installing sis file %s%s.\n"), filename, dryrun ? _(", not really") : ""); - } - else - { - fprintf(stderr, _("Missing SIS filename\n")); - exit(1); - } - struct stat st; - if (-1 == stat(filename, &st)) - error(__LINE__); - off_t len = st.st_size; - if (logLevel >= 2) - printf(_("File is %d bytes long\n"), len); - uint8_t* buf = new uint8_t[len]; - int fd = open(filename, O_RDONLY); - if (-1 == fd) - error(__LINE__); - if (-1 == read(fd, buf, len)) - error(__LINE__); - close(fd); - Psion* psion; - if (dryrun) - psion = new FakePsion(); - else - psion = new Psion(); - if (!psion->connect()) - { - printf(_("Couldn't connect with the Psion\n")); - } - else - { - createCRCTable(); - SISFile sisFile; - SisRC rc = sisFile.fillFrom(buf, len); - if (rc == SIS_OK) - { -// if (!dryrun) - { - SISInstaller installer; - installer.setPsion(psion); - installer.run(&sisFile, buf, len); - } - } - else - { - printf(_("Could not parse the sis file.\n")); - } - psion->disconnect(); - } + } + else + { + fprintf(stderr, "%s", _("Missing SIS filename\n")); + exit(1); + } + struct stat st; + if (-1 == stat(filename, &st)) + error(__LINE__); + off_t len = st.st_size; + if (logLevel >= 2) + printf(_("File is %d bytes long\n"), len); + uint8_t* buf = new uint8_t[len]; + int fd = open(filename, O_RDONLY); + if (-1 == fd) + error(__LINE__); + if (-1 == read(fd, buf, len)) + error(__LINE__); + close(fd); + Psion* psion; + if (dryrun) + psion = new FakePsion(); + else + psion = new Psion(); + if (!psion->connect()) + { + printf("%s", _("Couldn't connect with the Psion\n")); + } + else + { + createCRCTable(); + SISFile sisFile; + SisRC rc = sisFile.fillFrom(buf, len); + if (rc == SIS_OK) + { + SISInstaller installer; + installer.setPsion(psion); + installer.run(&sisFile, buf, len); + } + else + printf("%s", _("Could not parse the sis file.\n")); + psion->disconnect(); + } - return 0; + return 0; } - -- cgit v1.2.3