File: sendmsg.c

package info (click to toggle)
twin 0.4.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,804 kB
  • ctags: 23,904
  • sloc: ansic: 61,860; cpp: 1,023; makefile: 777; sh: 552; lex: 302; yacc: 231
file content (141 lines) | stat: -rw-r--r-- 3,675 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  sendmsg.c  --  send a message to a twin client
 *
 *  This program is placed in the public domain.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Tw/Tw.h"
#include "Tw/Twerrno.h"
#include "version.h"

byte *argv0;

void Usage(void) {
    fprintf(stderr, "Usage: %s [-msgport=]<MsgPort> [OPTIONS]\n"
	    "Currently known options:\n"
	    " -h, -help               display this help and exit\n"
	    " -V, -version            output version information and exit\n"
	    " -control                send a MSG_USER_CONTROL message (default)\n"
	    " -clientmsg              send a MSG_USER_CLIENTMSG message\n"
	    " [-code=]<Code>          set the message code (default is `open')\n"
	    " [-data=]<Data>          set the message data\n"
	    "Currently known codes for control messages are:\n"
	    " quit (0), restart (1), open (2)\n",
	    argv0);
}

void ShowVersion(void) {
    fputs("twsendmsg " TWIN_VERSION_STR "\n", stdout);
}

TW_DECL_MAGIC(sendmsg_magic);

int main(int argc, char *argv[]) {
    byte *MsgPortName = NULL, *CodeName = NULL, *Data = NULL;
    udat Type = TW_MSG_USER_CONTROL, Code = TW_MSG_CONTROL_OPEN, DataLen = 0;
    tmsgport MsgPort;
    tmsg Msg;
    uldat err;

    argv0 = argv[0];
    
    if (argc == 2) {
	if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-help")) {
	    Usage();
	    return 0;
	} else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "-version")) {
	    ShowVersion();
	    return 0;
	}
    }
    
    if (TwCheckMagic(sendmsg_magic) && TwOpen(NULL)) do {
	
	while (++argv, --argc) {
	    if (!strncmp(*argv, "-msgport=", 9))
		MsgPortName = *argv + 9;
	    else if (!strcmp(*argv, "-control"))
		Type = TW_MSG_USER_CONTROL;
	    else if (!strcmp(*argv, "-clientmsg"))
		Type = TW_MSG_USER_CLIENTMSG;
	    else if (!strncmp(*argv, "-code=", 6))
		CodeName = *argv + 6;
	    else if (!strncmp(*argv, "-data=", 6))
		Data = *argv + 6;
	    else if (!MsgPortName)
		MsgPortName = *argv;
	    else if (!CodeName)
		CodeName = *argv;
	    else if (!Data)
		Data = *argv;
	}
	
	if (CodeName) {
	    if (!strcmp(CodeName, "quit"))
		Code = TW_MSG_CONTROL_QUIT;
	    else if (!strcmp(CodeName, "restart"))
		Code = TW_MSG_CONTROL_RESTART;
	    else if (!strcmp(CodeName, "open"))
		Code = TW_MSG_CONTROL_OPEN;
	    else {
		fprintf(stderr, "%s: argument `%s' not recognized\n"
			"\ttry `%s -help' for usage summary.\n", argv0, CodeName, argv0);
		return 1;
	    }
	}
	
	if (Data)
	    DataLen = strlen(Data);
	
	if (MsgPortName) {
	    if ((MsgPort = TwFindMsgPort(TW_NOID, strlen(MsgPortName), MsgPortName))) {
		if (Type == TW_MSG_USER_CONTROL) {
		    tevent_control EventC;
		    if ((Msg = TwCreateMsg(TW_MSG_USER_CONTROL,
					   DataLen + sizeof(*EventC)))) {
			EventC = &Msg->Event.EventControl;
			EventC->W = TW_NOID;
			EventC->Code = Code;
			EventC->Len = DataLen;
			EventC->X = 0;
			EventC->Y = 0;
			if (DataLen)
			    memcpy(EventC->Data, Data, DataLen);
			if (TwSendMsg(MsgPort, Msg))
			    return 0;
		    }
		} else {
		    tevent_clientmsg EventC;
		    if ((Msg = TwCreateMsg(TW_MSG_USER_CLIENTMSG,
					   DataLen + sizeof(*EventC)))) {
			EventC->W = TW_NOID;
			EventC->Code = Code;
			EventC->Len = DataLen;
			if (DataLen)
			    memcpy(EventC->Data, Data, DataLen);
			if (TwSendMsg(MsgPort, Msg))
			    return 0;
		    }
		}
	    } else {
		fprintf(stderr, "%s: MsgPort `%s' not found on server.\n", argv0, MsgPortName);
		return 1;
	    }
	} else {
	    Usage();
	    return 1;
	}
	
    } while (0);
    
    err = TwErrno;
    fprintf(stderr, "%s: libTw error: %s%s\n", argv0,
	    TwStrError(err), TwStrErrorDetail(err, TwErrnoDetail));
    return 1;
}