#include	<Xm/Xm.h>
#include	<Xm/Label.h>
#include	<string.h>

void inputCB(Widget label, int *src, XtInputId *id);

int
main(int ac, char *av[])
{
    XtAppContext app;
    Widget top, label;
    Arg shell_arg[] = {
	/* 長い文字列が入力されたときに、
	   ウィンドウサイズを大きくするため */
	{ XmNallowShellResize, True }
    };

    XtSetLanguageProc(NULL, NULL, NULL);
    top = XtAppInitialize(&app, "Sample", NULL, 0, &ac, av, NULL,
			  shell_arg, XtNumber(shell_arg));
    label = XmCreateLabel(top, "Hello", NULL, 0);
    XtManageChild(label);

    /* 標準入力(0)から読み込み可能になったときに呼び出される
       関数を登録する */
    XtAppAddInput(app, 0, (XtPointer)XtInputReadMask,
		  (XtInputCallbackProc)inputCB,
		  (XtPointer)label);
    XtRealizeWidget(top);
    XtAppMainLoop(app);
}

void
inputCB(Widget label, int *src, XtInputId *id)
{
    XmString xms;
    char buf[1024];
    int n;

    /* 標準入力の内容をラベルに設定する */
    n = read(*src, buf, sizeof(buf));
    if (n <= 0)
	return;
    if (n < sizeof(buf))
	buf[n] = '\0';
    else
	buf[sizeof(buf) - 1] = '\0';
    xms = XmStringCreateLocalized(buf);
    XtVaSetValues(label,
		  XmNlabelString, xms,
		  NULL);
    XmStringFree(xms);
}
