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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
text.c
Copyright (C) 1999 Ulric Eriksson <ulric@edu.stockholm.se>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Command.h>
#include "../common/common.h"
static Widget topLevel;
static Atom plugin_protocol;
static Window ewin = None;
static int ph = 0;
/* Plugin stuff */
static void exec_cmd_file(FILE *fp)
{
char b[1000];
printf("200 \\.\n"); /* end marker same as in postgres */
while (fgets(b, sizeof b, fp)) {
chomp(b);
printf("%s\n", b);
}
printf("\\.\n");
}
static void exec_cmd(char *p)
{
FILE *fp;
if (*p == '!') { /* read from shell command */
fp = popen(p+1, "r");
if (fp == NULL) {
printf("500 Can't run %s\n", p+1);
return;
}
exec_cmd_file(fp);
pclose(fp);
} else { /* read from text file */
fp = fopen(p, "r");
if (fp == NULL) {
printf("500 Can't read %s\n", p);
return;
}
exec_cmd_file(fp);
fclose(fp);
}
}
static void ewin_cmd(char *p)
{
unsigned long w;
if (sscanf(p, "%lx", &w) != 1) {
printf("500 Where\n");
return;
}
ewin = w;
printf("250 OK\n");
}
static void ph_cmd(char *p)
{
if (sscanf(p, "%d", &ph) != 1) {
printf("500 What\n");
return;
}
printf("250 OK\n");
}
static void what_cmd(char *p)
{
printf("(llpr \"Howdy!\")\n");
}
static void win_cmd(char *p)
{
printf("250 %lx\n", (unsigned long) XtWindow(topLevel));
}
static void quit_cmd(char *p)
{
printf("221 Over and out\n");
exit(0);
}
static struct {
char *verb;
void (*cb) (char *);
} plugin_cmds[] = {
{"EXEC", exec_cmd},
{"EWIN", ewin_cmd},
{"PH", ph_cmd},
{"WHAT", what_cmd},
{"WIN", win_cmd},
{"QUIT", quit_cmd},
{ NULL, NULL }
};
static void read_plugin_cmd(XtPointer client_data, int *fid, XtInputId * id)
{
char b[1024], *p;
int i, n;
if ((n = read(*fid, b, 1020)) == -1)
return;
b[n] = '\0';
if ((p = strchr(b, '\n')) == NULL) {
printf("501 Incomplete command\n");
fflush(stdout);
return;
}
*p = '\0';
for (i = 0; plugin_cmds[i].verb; i++) {
if (!strncmp(b, plugin_cmds[i].verb,
strlen(plugin_cmds[i].verb)))
break;
}
if (plugin_cmds[i].verb) {
(*plugin_cmds[i].cb) (b+strlen(plugin_cmds[i].verb)+1);
} else {
printf("500 What are you talking about\n");
}
fflush(stdout);
}
/* ---
*/
void mainloop(void)
{
XtAppAddInput(XtWidgetToApplicationContext(topLevel),
fileno(stdin), (XtPointer) XtInputReadMask,
read_plugin_cmd, NULL);
printf("220 Text plugin\n");
fflush(stdout);
XtAppMainLoop(XtWidgetToApplicationContext(topLevel));
}
/* This will be useful in the future */
#if 0
static void plugin_callback(Widget w, XtPointer cl, XtPointer ca)
{
XEvent event;
Window target = ewin;
Display *dpy = XtDisplay(topLevel);
if (target == None) {
fprintf(stderr, "No event window set\n");
return;
}
event.xclient.type = ClientMessage;
event.xclient.display = dpy;
event.xclient.message_type = plugin_protocol;
event.xclient.format = 32;
event.xclient.window = target;
event.xclient.data.l[0] = 0; /* protocol version */
event.xclient.data.l[1] = ph; /* plugin handle */
event.xclient.data.l[2] = 0; /* ticket */
XSendEvent(dpy, target, True, NoEventMask, &event);
}
#endif
/* ---
*/
int main(int argc, char **argv)
{
XtAppContext ac;
Display *dpy;
Widget w;
topLevel = XtAppInitialize(&ac, "Text",
NULL, 0, &argc, argv, NULL, NULL, 0);
w = XtVaCreateManagedWidget("text",
labelWidgetClass, topLevel,
XtNlabel, "Text", (char *)0);
/* XtAddCallback(w, XtNcallback, plugin_callback, NULL);
*/
dpy = XtDisplay(topLevel);
plugin_protocol = XInternAtom(dpy, "_PLUGIN_PROTOCOL", False);
XtRealizeWidget(topLevel);
mainloop();
return 0;
}
|