msgtool.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. program msgtool;
  2. Uses ipc;
  3. Type
  4. PMyMsgBuf = ^TMyMsgBuf;
  5. TMyMsgBuf = record
  6. mtype : Longint;
  7. mtext : string[255];
  8. end;
  9. Procedure DoError (Const Msg : string);
  10. begin
  11. Writeln (msg,'returned an error : ',ipcerror);
  12. halt(1);
  13. end;
  14. Procedure SendMessage (Id : Longint;
  15. Var Buf : TMyMsgBuf;
  16. MType : Longint;
  17. Const MText : String);
  18. begin
  19. Writeln ('Sending message.');
  20. Buf.mtype:=mtype;
  21. Buf.Mtext:=mtext;
  22. If not msgsnd(Id,PMsgBuf(@Buf),256,0) then
  23. DoError('msgsnd');
  24. end;
  25. Procedure ReadMessage (ID : Longint;
  26. Var Buf : TMyMsgBuf;
  27. MType : longint);
  28. begin
  29. Writeln ('Reading message.');
  30. Buf.MType:=MType;
  31. If msgrcv(ID,PMSGBuf(@Buf),256,mtype,0) then
  32. Writeln ('Type : ',buf.mtype,' Text : ',buf.mtext)
  33. else
  34. DoError ('msgrcv');
  35. end;
  36. Procedure RemoveQueue ( ID : Longint);
  37. begin
  38. If msgctl (id,IPC_RMID,Nil) then
  39. Writeln ('Removed Queue with id',Id);
  40. end;
  41. Procedure ChangeQueueMode (ID,mode : longint);
  42. Var QueueDS : TMSQid_ds;
  43. begin
  44. If Not msgctl (Id,IPC_STAT,@QueueDS) then
  45. DoError ('msgctl : stat');
  46. Writeln ('Old permissions : ',QueueDS.msg_perm.mode);
  47. QueueDS.msg_perm.mode:=Mode;
  48. if msgctl (ID,IPC_SET,@QueueDS) then
  49. Writeln ('New permissions : ',QueueDS.msg_perm.mode)
  50. else
  51. DoError ('msgctl : IPC_SET');
  52. end;
  53. procedure usage;
  54. begin
  55. Writeln ('Usage : msgtool s(end) <type> <text> (max 255 characters)');
  56. Writeln (' r(eceive) <type>');
  57. Writeln (' d(elete)');
  58. Writeln (' m(ode) <decimal mode>');
  59. halt(1);
  60. end;
  61. Function StrToInt (S : String): longint;
  62. Var M : longint;
  63. C : Integer;
  64. begin
  65. val (S,M,C);
  66. If C<>0 Then DoError ('StrToInt : '+S);
  67. StrToInt:=M;
  68. end;
  69. Var
  70. Key : TKey;
  71. ID : longint;
  72. Buf : TMyMsgBuf;
  73. begin
  74. If Paramcount<1 then Usage;
  75. key :=Ftok('.','M');
  76. ID:=msgget(key,IPC_CREAT or 438);
  77. If ID<0 then DoError ('MsgGet');
  78. Case upCase(Paramstr(1)[1]) of
  79. 'S' : If ParamCount<>3 then
  80. Usage
  81. else
  82. SendMessage (id,Buf,StrToInt(Paramstr(2)),paramstr(3));
  83. 'R' : If ParamCount<>2 then
  84. Usage
  85. else
  86. ReadMessage (id,buf,strtoint(Paramstr(2)));
  87. 'D' : If ParamCount<>1 then
  88. Usage
  89. else
  90. RemoveQueue (ID);
  91. 'M' : If ParamCount<>2 then
  92. Usage
  93. else
  94. ChangeQueueMode (id,strtoint(paramstr(2)));
  95. else
  96. Usage
  97. end;
  98. end.