File: main.C

package info (click to toggle)
ical 2.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,680 kB
  • ctags: 1,650
  • sloc: cpp: 12,639; tcl: 6,726; makefile: 584; sh: 521; perl: 60; ansic: 27
file content (311 lines) | stat: -rw-r--r-- 7,610 bytes parent folder | download | duplicates (3)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Copyright (c) 1994 by Sanjay Ghemawat */
/*
 * Behaves like Tk main routine if display can be opened, otherwise
 * like the Tcl main routine.  The following control whether or not
 * Tk is used --
 *
 *	$DISPLAY in environment			Use Tk
 *	-display				Use Tk
 *	-list					Do not use Tk
 *	-show					Do not use Tk
 *	-print					Do not use Tk
 *	-nodisplay				Do not use Tk
 *
 * The "-f" flag can be used to pass in an initialization script regardless
 * of whether or not Tk is used.
 *
 * All .tcl files from Tcl/Tk libraries are linked into the executable
 * as well to avoid depending on external files being installed correctly.
 */

#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#include <tk.h>
#include "ical.h"

/* Include various libraries converted to strings. */

#ifdef STANDALONE
static char* tcl_lib_str[] = {
#include "tcl_lib.gen"
0
};

// Need to disable "source" command
static char* tk_lib_str[] = {
"rename source _orig_source",
"proc source {args} {}",
#include "tk_lib.gen"
"rename source {}",
"rename _orig_source source",
0
};

static char* ical_lib_str[] = {
#include "ical_lib.gen"
0
};

static char* tcllib_str[] = {
#include "tcllib.gen"
0
};
#endif

static char* ical_startup[] = {
#include "ical_start.gen"
0
};

static char* psheader_str[] = {
"set ical(psheader) {%!PS-Adobe-",
#include "psheader.gen"
"}",
0
};

static char* ical_doc_str[] = {
"set ical(doc) {",
#include "icaldoc.gen"
"}",
0
};

static char* tcl_doc_str[] = {
"set ical(tcl_interface_doc) {",
#include "tcldoc.gen"
"}",
0
};

#include "bitmaps/left.xbm"
#include "bitmaps/right.xbm"
#include "bitmaps/todo.xbm"
#include "bitmaps/done.xbm"
#include "bitmaps/sleft.xbm"
#include "bitmaps/dleft.xbm"
#include "bitmaps/sright.xbm"
#include "bitmaps/dright.xbm"
#include "bitmaps/ical.xbm"

// Is Tk available?
static int have_tk;

// Was a script specified on the command line?
static int have_script;

static int eval_list(Tcl_Interp*, char** list);
static int app_init(Tcl_Interp*);
extern int Ical_Init(Tcl_Interp*);

int
main(int argc, char* argv[]) {
    // XXX Hacky scanning of argument list to figure out whether
    // or not Tk is needed, and also if a script is specified on the
    // command line.

    have_script = 0;
    have_tk = (getenv("DISPLAY") != 0);

    int i;
    for (i = 1; i < argc; i++) {
	if (strcmp(argv[i], "-display") == 0) {
	    have_tk = 1;
	    continue;
	}
	if (strcmp(argv[i], "-list") == 0) {
	    have_tk = 0;
	    continue;
	}
	if (strcmp(argv[i], "-show") == 0) {
	    have_tk = 0;
	    continue;
	}
	if (strcmp(argv[i], "-print") == 0) {
	    have_tk = 0;
	    continue;
	}
	if (strcmp(argv[i], "-nodisplay") == 0) {
	    have_tk = 0;
	    continue;
	}
	if ((strcmp(argv[i], "-f") == 0) || (strcmp(argv[i], "-file") == 0)) {
	    have_script = 1;
	    continue;
	}
    }

    // Strip out processed "-nodisplay" arguments
    int j = 1;
    for (i = 1; i < argc; i++) {
	if (strcmp(argv[i], "-nodisplay") == 0) continue;
	argv[j++] = argv[i];
    }
    argv[j] = 0;
    argc = j;

    if (!have_tk && have_script) {
	// If a "-f <script>" is present on the command line,
	// strip out the "-f" because tclMain does not understand it.
	for (i = 1; i < argc-1; i++) {
	    if ((strcmp(argv[i],"-f") != 0) && (strcmp(argv[i],"-file") != 0))
		continue;

	    /* Slide the rest of the arguments over */
	    /* (including the NULL in argv[argc].   */

	    for (int j = i+1; j <= argc; j++)
		argv[j-1] = argv[j];
	    argc--;
	    break;
	}
    }


    if (have_tk)
	Tk_Main(argc, argv, app_init);
    else
	Tcl_Main(argc, argv, app_init);

    return 0;
}

#ifdef STANDALONE
extern "C" int MyTcl_Init(Tcl_Interp* tcl) {
    return (eval_list(tcl, tcl_lib_str));
}
#else
#define MyTcl_Init Tcl_Init
#endif

/* XXX We need to replace TkPlatformInit */
#ifdef STANDALONE
extern "C" void TkCreateXEventSource();
extern "C" int TkPlatformInit(Tcl_Interp* tcl) {
    char* libDir = Tcl_GetVar(tcl, "tk_library", TCL_GLOBAL_ONLY);
    if (libDir == NULL) {
	libDir = getenv("TK_LIBRARY");
	if (libDir == NULL) {
	    libDir = "/unknown";
	}
	Tcl_SetVar(tcl, "tk_library", libDir, TCL_GLOBAL_ONLY);
    }

    TkCreateXEventSource();

    return (eval_list(tcl, tk_lib_str));
}
#endif

static int app_init(Tcl_Interp* tcl) {
    if (MyTcl_Init(tcl) != TCL_OK) return TCL_ERROR;
    if (have_tk && (Tk_Init(tcl) != TCL_OK)) return TCL_ERROR;
    if (Ical_Init(tcl) != TCL_OK) return TCL_ERROR;

    if (!have_script) {
	// Perform default initialization
	if (have_tk) {
	    if (Tcl_Eval(tcl, "ical_tk_script") == TCL_ERROR)
		return TCL_ERROR;

	    // Do not bother returning to tkMain because it
	    // will try to read from standard input.

	    Tk_MainLoop();
	    Tcl_Eval(tcl, "exit");
	    exit(1);
	}

	// Default tcl code
	return Tcl_Eval(tcl, "ical_no_tk_script");
    }

    return TCL_OK;
}

// Macro to create a Tk bitmap.  Returns true iff successfull.
#define MAKE_BITMAP(tcl,id,n) \
(Tk_DefineBitmap(tcl,Tk_GetUid(id),n##_bits,n##_width,n##_height) == TCL_OK)

int Ical_Init(Tcl_Interp* tcl) {
    if (have_tk) {
	/* Load necessary Tk support code */
	Tk_Window mainWindow = Tk_MainWindow(tcl);

	if (!MAKE_BITMAP(tcl, "left_arrow",	left))    return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "right_arrow",	right))   return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "todo_box",	todo))    return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "done_box",	done))    return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "single_left",	sleft))   return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "double_left",	dleft))   return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "single_right",	sright))  return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "double_right",	dright))  return TCL_ERROR;
	if (!MAKE_BITMAP(tcl, "ical_icon",	ical))    return TCL_ERROR;
    }

    // Set-up postscript prolog
    if (eval_list(tcl, psheader_str) != TCL_OK)
	return TCL_ERROR;

    // Set-up documentation
    if (eval_list(tcl, ical_doc_str) != TCL_OK)
	return TCL_ERROR;
    if (eval_list(tcl, tcl_doc_str) != TCL_OK)
	return TCL_ERROR;

    // Non-Tk ical commands
    Tcl_CreateCommand(tcl, "calendar",     Cmd_CreateCalendar,	NULL, NULL);
    Tcl_CreateCommand(tcl, "notice",       Cmd_CreateNotice,	NULL, NULL);
    Tcl_CreateCommand(tcl, "appointment",  Cmd_CreateAppt,	NULL, NULL);
    Tcl_CreateCommand(tcl, "date",         Cmd_Date,		NULL, NULL);
    Tcl_CreateCommand(tcl, "ical_time",    Cmd_Time,		NULL, NULL);
    Tcl_CreateCommand(tcl, "de_monthdays", Cmd_MonthDays,	NULL, NULL);
    Tcl_CreateCommand(tcl, "hilite_loop",  Cmd_HiliteLoop,	NULL, NULL);
    Tcl_CreateCommand(tcl, "ical_expand_file_name", Cmd_ExpandFileName, 0, 0);

#ifdef STANDALONE
    // Load tcllib files
    if (eval_list(tcl, tcllib_str) != TCL_OK)
	return TCL_ERROR;

    // Load ical library files
    if (eval_list(tcl, ical_lib_str) != TCL_OK)
	return TCL_ERROR;
#endif

    // Initialize ical stuff
    if (eval_list(tcl, ical_startup) != TCL_OK)
	return TCL_ERROR;

    if (Tcl_Eval(tcl, "ical_init") == TCL_ERROR)
	return TCL_ERROR;

    return TCL_OK;
}

// Concatenate list of lines into one string and "Tcl_Eval" it.
static int eval_list(Tcl_Interp* tcl, char** list) {
    // Get buffer size
    int i;
    int count = 0;
    for (i = 0; list[i] != 0; i++) {
	count += strlen(list[i]);
	count++;			// Space for newline
    }

    // Copy lines into buffer
    int index = 0;
    char* buf = new char[count+1];
    for (i = 0; list[i] != 0; i++) {
	strcpy(buf+index, list[i]);
	index += strlen(list[i]);
	buf[index] = '\n';
	index++;
    }
    buf[index] = '\0';

    int result = Tcl_Eval(tcl, buf);
    delete [] buf;
    return result;
}