File: cm2rem.tcl

package info (click to toggle)
remind 03.03.01-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,096 kB
  • sloc: ansic: 17,507; sh: 3,962; perl: 828; lisp: 343; makefile: 178
file content (358 lines) | stat: -rw-r--r-- 10,259 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/sh
# -*-Mode: TCL;-*-

#--------------------------------------------------------------
#   cm2rem.tcl
#
#   A cheesy Tcl script to convert Sun's "cm" calendar manager
#   files (version 3 only) to Remind format.
#
#   This file is part of REMIND.
#   Copyright (C) 1992-2018 by Dianne Skoll
#
#--------------------------------------------------------------

# the next line restarts using tclsh \
exec tclsh "$0" "$@"

set i 0
foreach month {January February March April May June
    July August September October November December} {
    incr i
    set MonthNum($month) $i
    set FullMonth([string range $month 0 2]) $month
}

#***********************************************************************
# %PROCEDURE: convertParens
# %ARGUMENTS:
#  line -- a line read from a cm file
# %RETURNS:
#  A new line with all ( and ) outside quotes converted to { and }.
#  This cheap trick allows us to use Tcl's built-in list manipulation
#  functions to munge the line.
#***********************************************************************
proc convertParens { line } {
    # Convert all ( and ) to { and } unless they are inside a quoted
    # string
    set out ""
    set len [string length $line]
    set inQuotes 0
    for {set i 0} {$i < $len} {incr i} {
	set char [string range $line $i $i]
	if {$char == "\\" && $inQuotes} {
	    append out $char
	    incr i
	    set char [string range $line $i $i]
	    append out $char
	    continue
	}

	if {$char == "(" && !$inQuotes} {
	    set char \{
	}

	if {$char == ")" && !$inQuotes} {
	    set char \}
	}

	if {$char == "\""} {
	    set inQuotes [expr !$inQuotes]
	}

	append out $char
    }
    return $out
}

#***********************************************************************
# %PROCEDURE: processLine
# %ARGUMENTS:
#  line -- a line read from a cm file
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Processes a single line from the file, possibly writing a reminder
#  in Remind format to stdout
#***********************************************************************
proc processLine { line } {
    global Attributes
    global FullMonth

    catch {unset Attributes}

    # Only convert lines which start with "(add"
    if {[string range $line 0 3] != "(add"} {
	return
    }
    set line [convertParens $line]
    # Convert it to a list.  CAREFUL:  Potential security problem if
    # $line contains something nasty.

    eval set line $line

    set Attributes(body) ""
    foreach {key val} $line {
	switch -exact -- $key {
	    "add" {
		set Attributes(date) $val
	    }
	    "what:" {
		append Attributes(body) $val
	    }
	    "details:" {
		append Attributes(body) $val
	    }
	    "duration:" {
		set Attributes(duration) $val
	    }
	    "period:" {
		set Attributes(period) $val
	    }
	    "ntimes:" {
		set Attributes(ntimes) $val
	    }
	    "attributes:" {
		set Attributes(action) $val
	    }
		
	}
    }

    if {[info exists Attributes(action)]} {
	# Nuke quotes and commas in action
	regsub -all {[,\"]} $Attributes(action) { } Attributes(action)

	# Add spaces to pairs
	regsub -all \}\{  $Attributes(action) \}\ \{ Attributes(action)

	# Add another pair of brackets to make a proper list
	set Attributes(action) "{$Attributes(action)}"

	# Convert to a real Tcl list
	eval set Attributes(action) $Attributes(action)
    }
    # Split out date into month, day, year, time parts
    scan $Attributes(date) "%s%s%s%s%s" wkday month day time year
    set time [string range $time 0 4]
    set Attributes(wkday) $wkday
    set Attributes(month) $FullMonth($month)
    set Attributes(day) $day
    set Attributes(time) $time
    set Attributes(year) $year

    # Convert newlines in body to spaces
    set body $Attributes(body)
    regsub -all "\n" $body " " body

    # TODO: Escape BODY to get rid of [] chars.
    set Attributes(body) $body

    # Convert to Reminder format
    convertReminder
}

#***********************************************************************
# %PROCEDURE: convertReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder to Remind format.
#***********************************************************************
proc convertReminder {} {
    global Attributes
    switch -exact $Attributes(period) {
	single { convertSingleReminder }
	daily  { convertDailyReminder  }
	weekly { convertWeeklyReminder }
	monthly { convertMonthlyReminder }
	yearly { convertYearlyReminder }
	default {
	    puts "\# Unable to convert reminder with period $Attributes(period)"
	    puts "\# Body is: $Attributes(body)"
	}
    }
}

#***********************************************************************
# %PROCEDURE: convertSingleReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder with "single" period to Remind format.
#***********************************************************************
proc convertSingleReminder {} {
    global Attributes
    puts "REM $Attributes(day) $Attributes(month) $Attributes(year) [at][duration]MSG $Attributes(body)"
}

#***********************************************************************
# %PROCEDURE: convertDailyReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder with "daily" period to Remind format.
#***********************************************************************
proc convertDailyReminder {} {
    global Attributes
    set ntimes [expr $Attributes(ntimes) - 1]
    if {$ntimes <= 1} {
	convertSingleReminder
	return
    }
    set until [getUntilDate $Attributes(day) $Attributes(month) $Attributes(year) $ntimes]

    puts "REM $Attributes(day) $Attributes(month) $Attributes(year) *1 [at][duration]UNTIL $until MSG $Attributes(body)"
}

#***********************************************************************
# %PROCEDURE: convertWeeklyReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder with "daily" period to Remind format.
#***********************************************************************
proc convertWeeklyReminder {} {
    global Attributes
    set ntimes [expr $Attributes(ntimes) - 1]
    if {$ntimes <= 1} {
	convertSingleReminder
	return
    }
    set until [getUntilDate $Attributes(day) $Attributes(month) $Attributes(year) [expr $ntimes * 7]]

    puts "REM $Attributes(day) $Attributes(month) $Attributes(year) *7 [at][duration]UNTIL $until MSG $Attributes(body)"
}

#***********************************************************************
# %PROCEDURE: convertMonthlyReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder with "monthly" period to Remind format.
#***********************************************************************
proc convertMonthlyReminder {} {
    global Attributes
    set ntimes [expr $Attributes(ntimes) - 1]
    if {$ntimes <= 1} {
	convertSingleReminder
	return
    }

    # If repetition > 1000, it's infinite
    if {$ntimes > 1000} {
	puts "REM $Attributes(day) [at][duration]MSG $Attributes(body)"
	return
    }

    ### UNTIL date is fudged!
    set until [getUntilDate $Attributes(day) $Attributes(month) $Attributes(year) [expr $ntimes * 30]]

    puts "REM $Attributes(day) [at][duration]UNTIL $until MSG $Attributes(body)"
}

#***********************************************************************
# %PROCEDURE: convertYearlyReminder
# %ARGUMENTS:
#  None -- uses global Attributes variable which must be filled in
# %RETURNS:
#  Nothing
# %DESCRIPTION:
#  Converts a reminder with "yearly" period to Remind format.
#***********************************************************************
proc convertYearlyReminder {} {
    global Attributes

    # No special handling of ntimes et al.
    puts "REM $Attributes(day) $Attributes(month) [at][duration]MSG $Attributes(body)"

}

#***********************************************************************
# %PROCEDURE: at
# %ARGUMENTS:
#  None -- uses Attributes global variable
# %RETURNS:
#  A string providing the correct AT clause for a timed reminder.
#***********************************************************************
proc at {} {
    global Attributes
    if {![info exists Attributes(time)]} {
	return ""
    }
    if {"$Attributes(time)" == ""} {
	return ""
    }

    return "AT $Attributes(time) "
}

#***********************************************************************
# %PROCEDURE: duration
# %ARGUMENTS:
#  None -- uses Attributes global variable
# %RETURNS:
#  A string providing the correct DURATION clause for a timed reminder.
#***********************************************************************
proc duration {} {
    global Attributes
    if {![info exists Attributes(duration)]} {
	return ""
    }
    if {"$Attributes(duration)" == ""} {
	return ""
    }
    set h [expr $Attributes(duration) / 3600]
    set remainder [expr $Attributes(duration) - $h*3600]
    set m [expr $remainder / 60]
    return "DURATION [format "%d:%02d " $h $m]"
}

#***********************************************************************
# %PROCEDURE: getUntilDate
# %ARGUMENTS:
#  day, month, year -- a date
#  days -- number of days to add to date
# %RETURNS:
#  The date which is "days" later than supplied date in a correct UNTIL
#  format.
#***********************************************************************
proc getUntilDate { day month year days } {
    global RemindPipe
    global MonthNum
    set date "'$year/$MonthNum($month)/$day'"
    puts $RemindPipe "MSG \[trigger($date + $days)\]%"
    puts $RemindPipe "flush"
    flush $RemindPipe
    gets $RemindPipe line
    return $line
}

catch {wm withdraw .}
# Start a Remind process to issue reminders
if {[catch {set RemindPipe [open "|remind -" "r+"]} err]} {
    puts stderr "Error: Cannot run Remind: $err"
    exit 1
}

puts $RemindPipe "banner %"
flush $RemindPipe

# Write some blurb
puts "\# Reminder file converted from \"cm\" data by cm2rem.tcl"
puts ""

while {[gets stdin line] >= 0} {
    processLine $line
}
exit 0