forked from porridge/xmotd
/
xmotd.c
271 lines (208 loc) · 6.22 KB
/
xmotd.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*$Id: xmotd.c,v 1.20 2003/02/14 00:37:38 elf Exp $*/
/*
* Copyright 1994-97, 2003 Luis Fernandes <elf@ee.ryerson.ca>
*
* Permission to use, copy, hack, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. This application is presented as is
* without any implied or written warranty.
*
*/
/*
* 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 of the License, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <utime.h>
#include <errno.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#ifdef MOTIF
#include <Xm/Text.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/Separator.h>
#ifdef HAVE_HTML
#include <HTML.h>
#endif
#else
#include <X11/Shell.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#ifdef HAVE_HTML
#include <HTML.h>
#endif
#endif
#include "maindefs.h"
#include "main.h"
extern time_t motdChanged();
extern messageptr freeMessage();
extern Pixmap icon_pixmap;
extern char timeStamp[256];
extern messageptr msgslist;/* list of pointers to the motds requested
to be displayed */
extern app_res_t app_res;
extern XtAppContext app_con;
extern XtIntervalId timer;
char *txtbuf; /* file is loaded into this malloc'd pointer */
void
/*ARGSUSED*/
nextMessage(Widget w, caddr_t call_data, caddr_t client_data)
{
char buffer[256];
time_t ftime= time((time_t *)NULL);
extern Widget info, quit;
#ifdef MOTIF
XmString xmstr;
#endif
/* fprintf(stderr,"nextMessage()\n"); */
if (!msgslist)
return;
memset(buffer, 0, 256);
displayMessage(msgslist->file);
updateTimeStamp(getTimeStampName()); /* update the timestamp
whenever we pop-up to
display a message */
if(app_res.showfilename) /*show the filename */
{
sprintf(buffer, "%s (%s)", ctime(&ftime), msgslist->file);
}
else
{
sprintf(buffer, "%s", ctime(&ftime));
}
*strchr(buffer, '\n')=' '; /* junk the \n at the end*/
#ifdef MOTIF
xmstr=XmStringCreateLocalized(buffer);
XtVaSetValues(info, XmNlabelString, xmstr, NULL);
XmStringFree(xmstr);
#else
XtVaSetValues(info, XtNlabel, buffer, NULL);
#endif
/* next time through the loop, we continue at the next message*/
msgslist = freeMessage(msgslist);
/*figure out if we are displaying the last message;
if we are, then change the button-label to
"Dismiss" and re-direct the callback to Quit()*/
if(!msgslist)
{
extern void Quit(Widget w, caddr_t call_data, caddr_t client_data) ;
/* fprintf(stderr, "msgindex=%d, displayed %s (LAST MESSAGE)\n", msgindex, buffer);*/
#ifdef MOTIF
xmstr=XmStringCreateLocalized(LAST_MESSAGE_LABEL);
XtVaSetValues(quit, XmNlabelString, xmstr, NULL);
XmStringFree(xmstr);
XtRemoveAllCallbacks(quit, XmNactivateCallback);
XtAddCallback(quit, XmNactivateCallback,
(XtCallbackProc)Quit, 0);
#else
XtVaSetValues(quit, XtNlabel, LAST_MESSAGE_LABEL, NULL);
XtRemoveAllCallbacks(quit, XtNcallback);
XtAddCallback(quit, XtNcallback,
(XtCallbackProc)Quit, 0);
#endif
if(app_res.pto)
timer=XtAppAddTimeOut(app_con, (unsigned long)(app_res.pto*1000),
(XtTimerCallbackProc)Quit, (caddr_t) NULL);
return;
}
if(app_res.pto)
timer=XtAppAddTimeOut(app_con, (unsigned long)(app_res.pto*1000),
(XtTimerCallbackProc)nextMessage, (caddr_t) NULL);
}
void
revisitMessagesAndDisplay(int numsg)
{
extern int gargc;
extern char **gargv;
extern Widget topLevel, quit;
extern void Quit(Widget w, caddr_t call_data, caddr_t client_data);
/* fprintf(stderr,"revisitMessagesAndDisplay()\n");*/
if(numsg>1)
{
#ifdef MOTIF
XmString xmstr;
xmstr=XmStringCreateLocalized(NEXT_MESSAGE_LABEL);
XtVaSetValues(quit, XmNlabelString, xmstr, NULL);
XmStringFree(xmstr);
XtRemoveAllCallbacks(quit,XmNactivateCallback);
XtAddCallback(quit, XmNactivateCallback,
(XtCallbackProc)nextMessage, 0);
#else
XtVaSetValues(quit, XtNlabel, NEXT_MESSAGE_LABEL, NULL);
XtRemoveAllCallbacks(quit,XtNcallback);
XtAddCallback(quit, XtNcallback,
(XtCallbackProc)nextMessage, 0);
#endif
}
/* find and display 1st msg*/
nextMessage((Widget)NULL, (caddr_t)NULL, (caddr_t)NULL);
XMapRaised(XtDisplay(topLevel), XtWindow(topLevel));
XFlush(XtDisplay(topLevel));
}
int
displayMessage(char *filename)
{
FILE *fp;
struct stat motdstat;
extern Widget text;
/* fprintf(stderr,"displayMessage()\n"); */
#ifdef MOTIF
XmString xmstr;
#endif
/* memset((char *)(&motdstat), 0, sizeof(struct stat));*/
if((fp=fopen(filename,"r"))==NULL) /* Read it in...*/
{
perror(filename);
return(0); /* no file by this name*/
}
stat(filename,&motdstat);
if(txtbuf) free(txtbuf);
txtbuf=(char *)calloc(1, (motdstat.st_size+1)*sizeof(char));
if(!txtbuf)
{
extern XtAppContext app_con;
perror("xmotd");
XtDestroyApplicationContext(app_con);
exit(2);
}
fread(txtbuf,(int)motdstat.st_size,1,fp);
fclose(fp);
#ifdef MOTIF
#ifdef HAVE_HTML
HTMLSetText(text,txtbuf,NULL,NULL,0,NULL,NULL);
#else
XtVaSetValues(text, XmNvalue, txtbuf, NULL);
#endif
#else
#ifdef HAVE_HTML
HTMLSetText(text,txtbuf,NULL,NULL,0,NULL,NULL);
#else
XtVaSetValues(text, XtNstring, txtbuf, NULL);
#endif
#endif
return(1);
}/* displayMessage*/