📄 hsys.c
字号:
return 0; } case CL_Close: /* close the file pointed to by the filehandle */ { unpack_message(buffp, "%w", &fh); DebugPrintF(("CL_Close: fh %d\n", fh)); DevSW_FreePacket(packet); fhreal = hsysGetRealFileHandle(stateptr, fh, NULL); if (fhreal == NULL) err = -1; else { if (fhreal == stdin || fhreal == stdout || fhreal == stderr) { stateptr->last_errno = errno; DebugPrintF(("\tskipping close of std*\n")); err = 0; } else { err = fclose(fhreal); if (err == 0) stateptr->OSptr->FileTable[fh]=NULL; stateptr->last_errno = errno; DebugCheckErr("fclose", TRUE, err, stateptr->last_errno); } } return msgsend(CI_CLIB,"%w%w%w%w%w", CL_Close|HtoT, DebugID, OSInfo1, OSInfo2, err); } case CL_Write: { /* Write(word handle, word nbtotal, word nbytes, bytes data) * return(word nbytes) * WriteX(word nbytes, bytes data) * return(word nbytes) */ unsigned char *rwdata = NULL, *rwhead = NULL; unsigned char *write_source = NULL; char flags; FILE *fhreal; unsigned int ack_reason = CL_Write; /* first ack is for CL_Write */ err = -1; /* err == 0 is fwrite() error indication */ unpack_message(buffp, "%w%w%w", &fh, &nbtotal, &nbytes); DebugPrintF(("CL_Write: fh %d nbtotal %u nbytes %u\n", fh, nbtotal, nbytes)); fhreal = hsysGetRealFileHandle(stateptr, fh, &flags); nbtogo = nbtotal; /* deal with the file handle */ if (fhreal == NULL) err = 0; else { if (flags & READOP) fseek(fhreal,0,SEEK_CUR); stateptr->OSptr->FileFlags[fh] = (flags & BINARY) | WRITEOP; nbtogo -= nbytes; if (nbtogo > 0) { write_source = rwdata = rwhead = (unsigned char *)malloc(nbtotal); if (rwhead == NULL) { fprintf(stderr, "OUT OF MEMORY at line %d in %s\n", __LINE__, __FILE__); return -1; } memcpy(rwdata, buffp+12, nbytes); rwdata += nbytes; } else write_source = buffp+12; } do { /* at least once!! */ if (nbtogo == 0 && err != 0) { /* Do the actual write! */ if (fhreal == stdout || fhreal == stderr) { stateptr->hostif->write(stateptr->hostif->hostosarg, (char *)write_source, nbtotal); } else err = fwrite(write_source, 1, nbtotal, fhreal); stateptr->last_errno = errno; DebugCheckErr("fwrite", TRUE, (err == 0), stateptr->last_errno); } DevSW_FreePacket(packet); if (msgsend(CI_CLIB,"%w%w%w%w%w%w", ack_reason|HtoT, DebugID, OSInfo1, OSInfo2, (err == 0), nbtogo)) { fprintf(stderr, "COULD NOT REPLY at line %d in %s\n", __LINE__, __FILE__); if (rwhead != NULL) free(rwhead); return -1; } if (nbtogo == 0 || err == 0) { DebugPrintF(("\twrite complete - returning\n")); if (rwhead != NULL) free(rwhead); return 0; } else { /* await extension */ ack_reason = CL_WriteX; packet = DevSW_AllocatePacket(Armsd_BufferSize); if (packet == NULL) { fprintf(stderr, "COULD NOT ALLOC PACKET at line %d in %s\n", __LINE__, __FILE__); if (rwhead != NULL) free(rwhead); return -1; } Adp_ChannelRegisterRead(CI_CLIB, NULL, NULL); Adp_ChannelRead(CI_CLIB, &packet); Adp_ChannelRegisterRead(CI_CLIB, (ChannelCallback)HandleSysMessage, stateptr); buffhead = packet->pk_buffer; unpack_message(BUFFERDATA(buffhead), "%w%w%w%w%w", &reason_code, &DebugID, &OSInfo1, &OSInfo2, &nbytes); if (reason_code != (CL_WriteX|TtoH)) { DevSW_FreePacket(packet); free(rwhead); fprintf(stderr, "EXPECTING CL_WriteX GOT %u at line %d in %s\n", reason_code, __LINE__, __FILE__); return -1; } DebugPrintF(("CL_WriteX: nbytes %u\n", nbytes)); memcpy(rwdata, BUFFERDATA(buffhead)+20, nbytes); rwdata += nbytes; nbtogo -= nbytes; } } while (TRUE); /* will return when done */ } case CL_WriteX: /* * NOTE: if we've got here something has gone wrong * CL_WriteX's should all be picked up within the * CL_Write loop, probably best to return an error here * do this for the moment just so we do actually return */ fprintf(stderr, "ERROR: unexpected CL_WriteX message received\n"); return -1; case CL_Read: { /* Read(word handle, word nbtotal) * return(word nbytes, word nbmore, bytes data) */ /* ReadX() * return(word nbytes, word nbmore, bytes data) */ unsigned char *rwdata, *rwhead; int gotlen; unsigned int max_data_in_buffer=Armsd_BufferSize-28; char flags; FILE *fhreal; unsigned int nbleft = 0, reason = CL_Read; err = NoError; unpack_message(buffp, "%w%w", &fh, &nbtotal); DebugPrintF(("CL_Read: fh %d, nbtotal %d: ", fh, nbtotal)); rwdata = rwhead = (unsigned char *)malloc(nbtotal); if (rwdata == NULL) { fprintf(stderr, "OUT OF MEMORY at line %d in %s\n", __LINE__, __FILE__); DevSW_FreePacket(packet); return -1; } /* perform the actual read */ fhreal = hsysGetRealFileHandle(stateptr, fh, &flags); if (fhreal == NULL) { /* bad file handle */ err = -1; nbytes = 0; gotlen = 0; } else { if (flags & WRITEOP) fseek(fhreal,0,SEEK_CUR); stateptr->OSptr->FileFlags[fh] = (flags & BINARY) | WRITEOP; if (isatty_(fhreal)) { /* reading from a tty, so do some nasty stuff, reading into rwdata */ if (angel_hostif->gets(stateptr->hostif->hostosarg, (char *)rwdata, nbtotal) != 0) gotlen = strlen((char *)rwdata); else gotlen = 0; stateptr->last_errno = errno; DebugPrintF(("ttyread %d\n", gotlen)); } else { /* not a tty, reading from a real file */ gotlen = fread(rwdata, 1, nbtotal, fhreal); stateptr->last_errno = errno; DebugCheckErr("fread", FALSE, (gotlen == 0), stateptr->last_errno); DebugPrintF(("(%d)\n", gotlen)); } } nbtogo = gotlen; do { /* at least once */ if ((unsigned int) nbtogo <= max_data_in_buffer) nbytes = nbtogo; else nbytes = max_data_in_buffer; nbtogo -= nbytes; /* last ReadX needs subtle adjustment to returned nbtogo */ if (nbtogo == 0 && err == NoError && reason == CL_ReadX) nbleft = nbtotal - gotlen; else nbleft = nbtogo; count = msgbuild(BUFFERDATA(buffhead), "%w%w%w%w%w%w%w", reason|HtoT, 0, ADP_HandleUnknown, ADP_HandleUnknown, err, nbytes, nbleft); if (err == NoError) { /* copy data into buffptr */ memcpy(BUFFERDATA(buffhead)+28, rwdata, nbytes); rwdata += nbytes; count += nbytes; } DebugPrintF(("\treplying err %d, nbytes %d, nbtogo %d\n", err, nbytes, nbtogo)); packet->pk_length = count; Adp_ChannelWrite(CI_CLIB, packet); if (nbtogo == 0 || err != NoError) { /* done */ free(rwhead); return 0; } else { /* await extension */ reason = CL_ReadX; packet = DevSW_AllocatePacket(Armsd_BufferSize); if (packet == NULL) { fprintf(stderr, "COULD NOT ALLOC PACKET at line %d in %s\n", __LINE__, __FILE__); free(rwhead); return -1; } Adp_ChannelRegisterRead(CI_CLIB, NULL, NULL); Adp_ChannelRead(CI_CLIB, &packet); Adp_ChannelRegisterRead(CI_CLIB, (ChannelCallback)HandleSysMessage, stateptr); buffhead = packet->pk_buffer; unpack_message(BUFFERDATA(buffhead),"%w", &reason_code); if (reason_code != (CL_ReadX|TtoH)) { fprintf(stderr, "EXPECTING CL_ReadX GOT %u at line %d in %s\n", reason_code, __LINE__, __FILE__); DevSW_FreePacket(packet); free(rwdata); return -1; } } } while (TRUE); /* will return above on error or when done */ } case CL_ReadX: /* If we're here something has probably gone wrong */ fprintf(stderr, "ERROR: Got unexpected CL_ReadX message\n"); return -1; case CL_Seek: { unpack_message(buffp, "%w%w", &fh, &posn); DebugPrintF(("CL_Seek: fh %d, posn %ld\n", fh, posn)); DevSW_FreePacket(packet); fhreal = hsysGetRealFileHandle(stateptr, fh, NULL); if (fhreal == NULL) err = -1; else { err = fseek(fhreal, posn, SEEK_SET); stateptr->last_errno = errno; DebugCheckErr("fseek", TRUE, err, stateptr->last_errno); } return msgsend(CI_CLIB, "%w%w%w%w%w", CL_Seek|HtoT, DebugID, OSInfo1, OSInfo2, err); } case CL_Flen: { unpack_message(buffp, "%w", &fh); DebugPrintF(("CL_Flen: fh %d ", fh)); DevSW_FreePacket(packet); fhreal = hsysGetRealFileHandle(stateptr, fh, NULL); if (fhreal == NULL) fl = -1; else { posn = ftell(fhreal); if (fseek(fhreal, 0L, SEEK_END) < 0) { fl=-1; } else { fl = ftell(fhreal); fseek(fhreal, posn, SEEK_SET); } stateptr->last_errno = errno; } DebugPrintF(("returning len %ld\n", fl)); return msgsend(CI_CLIB, "%w%w%w%w%w", CL_Flen|HtoT, DebugID, OSInfo1, OSInfo2, fl); } case CL_IsTTY: { int ttyOrNot; unpack_message(buffp, "%w", &fh); DebugPrintF(("CL_IsTTY: fh %d ", fh)); DevSW_FreePacket(packet); fhreal = hsysGetRealFileHandle(stateptr, fh, NULL); if (fhreal == NULL) ttyOrNot = FALSE; else { ttyOrNot = isatty_(fhreal); stateptr->last_errno = errno; } DebugPrintF(("returning %s\n", ttyOrNot ? "tty (1)" : "not (0)")); return msgsend(CI_CLIB, "%w%w%w%w%w",CL_IsTTY|HtoT, DebugID, OSInfo1, OSInfo2, ttyOrNot); } case CL_TmpNam: { char *name; unsigned int tnamelen, TargetID; unpack_message(buffp, "%w%w", &tnamelen, &TargetID); DebugPrintF(("CL_TmpNam: tnamelen %d TargetID %d: ", tnamelen, TargetID)); DevSW_FreePacket(packet); TargetID = TargetID & 0xFF; if (stateptr->OSptr->TempNames[TargetID] == NULL) { if ((stateptr->OSptr->TempNames[TargetID] = (char *)malloc(L_tmpnam)) == NULL) { fprintf(stderr, "OUT OF MEMORY at line %d in %s\n", __LINE__, __FILE__); return -1; } tmpnam(stateptr->OSptr->TempNames[TargetID]); } name = stateptr->OSptr->TempNames[TargetID]; len = strlen(name) + 1; packet = DevSW_AllocatePacket(Armsd_BufferSize); if (packet == NULL) { fprintf(stderr, "COULD NOT ALLOC PACKET at line %d in %s\n", __LINE__, __FILE__); return -1; } buffhead = packet->pk_buffer; if (len > tnamelen) { DebugPrintF(("TMPNAME TOO LONG!\n")); count = msgbuild(BUFFERDATA(buffhead), "%w%w%w%w%w", CL_TmpNam|HtoT, DebugID, OSInfo1, OSInfo2, -1); } else { DebugPrintF(("returning \"%s\"\n", name)); count = msgbuild(BUFFERDATA(buffhead), "%w%w%w%w%w%w", CL_TmpNam|HtoT, DebugID, OSInfo1, OSInfo2, 0, len); strcpy((char *)BUFFERDATA(buffhead)+count, name); count +=len+1; } packet->pk_length = count; Adp_ChannelWrite(CI_CLIB, packet);/* Send message. */ return 0; } case CL_Unrecognised: DebugPrintF(("CL_Unrecognised!!\n")); return 0; default: fprintf(stderr, "UNRECOGNISED CL code %08x\n", reason_code); break;/* Need some sort of error handling here. *//* A call to CL_Unrecognised should suffice */ } return -1; /* Stop a potential compiler warning */}#ifdef COMPILING_ON_WINDOWS#include <windows.h>extern HWND hwndParent;void panic(const char *format, ...){ char buf[2048]; va_list args; Adp_CloseDevice(); va_start(args, format); vsprintf(buf, format, args); MessageBox(hwndParent, (LPCTSTR)buf, (LPCTSTR)"Fatal Error:", MB_OK); /* SJ - Not the proper way to shutdown the app */ exit(EXIT_FAILURE);/* if (hwndParent != NULL) SendMessage(hwndParent, WM_QUIT, 0, 0);*/ va_end(args);}#elsevoid panic(const char *format, ...){ va_list args; va_start(args, format); fprintf(stderr, "Fatal error: "); vfprintf(stderr, format, args); fprintf(stderr,"\n"); exit(EXIT_FAILURE);}#endif/* EOF hsys.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -