1 /* $XConsortium: rerule.c /main/4 1995/11/09 12:50:23 rswiston $ */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994 International Business Machines Corp.
5 * (c) Copyright 1993, 1994 Novell, Inc.
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
17 NUM_NUM = 0, /* Used in NumsToBuf to tell the proc what */
18 NUM_DAY, /* type of list we are handing it. */
22 static void NumsToBuf(unsigned int *, unsigned int, NumType, char *,
24 static void WeekNumberToString(WeekNumber, char *);
25 static void WeekDayToString(WeekDay, char *);
26 static void ConvertDaily(RepeatEvent *, char *, unsigned int);
27 static void ConvertWeekly(RepeatEvent *, char *, unsigned int);
28 static void ConvertMonthly(RepeatEvent *, char *, unsigned int);
29 static void ConvertYearly(RepeatEvent *, char *, unsigned int);
31 #define RE_DAILY(re) (re->re_data.re_daily)
32 #define RE_WEEKLY(re) (re->re_data.re_weekly)
33 #define RE_MONTHLY(re) (re->re_data.re_monthly)
34 #define RE_YEARLY(re) (re->re_data.re_yearly)
41 char *cmd_buf_tmp = NULL;
43 int subcommand_size = 0;
44 char subcommand[1024]; /* XXX: fixed size */
46 if (!re) return (char *)NULL;
49 switch (re->re_type) {
52 sprintf(subcommand, "M%d #%d",
53 re->re_interval, re->re_duration);
56 ConvertDaily(re, subcommand, 1024);
59 ConvertWeekly(re, subcommand, 1024);
61 case RT_MONTHLY_POSITION:
63 ConvertMonthly(re, subcommand, 1024);
67 ConvertYearly(re, subcommand, 1024);
71 cmd_buf_tmp = cmd_buf;
73 if (cmd_buf) cmd_buf_size = strlen(cmd_buf);
74 if (subcommand) subcommand_size = strlen(subcommand);
76 cmd_buf = (char *)calloc(1, cmd_buf_size + subcommand_size + 2);
79 strcat (cmd_buf, cmd_buf_tmp);
83 strcat (cmd_buf, " ");
84 strcat (cmd_buf, subcommand);
87 memset (subcommand, NULL, 1024);
96 * Takes an array of numbers, converts them back into their string
97 * type (e.g. SU 1W etc) and puts them into a string buffer with end
98 * marks as necessary, seperated by spaces.
103 unsigned int array_size,
106 unsigned int buf_size)
113 for (i = 0; i < array_size; i++) {
115 sprintf(tmp_buf2, " %d", RE_MASK_STOP(array[i]));
116 else if (type == NUM_DAY)
117 WeekDayToString(RE_MASK_STOP(array[i]), tmp_buf2);
118 else if (type == NUM_WEEK)
119 WeekNumberToString(RE_MASK_STOP(array[i]), tmp_buf2);
121 /* Add end mark if needed */
122 if (RE_STOP_IS_SET(array[i])) {
123 sprintf(tmp_buf, "%s$", tmp_buf2);
124 strcat (buffer, tmp_buf);
125 size += strlen(tmp_buf);
127 strcat (buffer, tmp_buf2);
128 size += strlen(tmp_buf2);
131 /* Make sure the size of our buffer does not overflow */
132 if (size > buf_size) {
133 printf ("Error: Internal buffer size exceeded\n");
144 switch (RE_MASK_STOP(day)) {
146 sprintf (buffer, " SU");
149 sprintf (buffer, " MO");
152 sprintf (buffer, " TU");
155 sprintf (buffer, " WE");
158 sprintf (buffer, " TH");
161 sprintf (buffer, " FR");
164 sprintf (buffer, " SA");
168 if (RE_STOP_IS_SET(day))
169 strcat (buffer, "$");
177 switch (RE_MASK_STOP(week)) {
179 sprintf (buffer, " 1+");
182 sprintf (buffer, " 2+");
185 sprintf (buffer, " 3+");
188 sprintf (buffer, " 4+");
191 sprintf (buffer, " 5+");
194 sprintf (buffer, " 1-");
197 sprintf (buffer, " 2-");
200 sprintf (buffer, " 3-");
203 sprintf (buffer, " 4-");
206 sprintf (buffer, " 5-");
210 if (RE_STOP_IS_SET(week))
211 strcat (buffer, "$");
218 unsigned int buf_size)
221 unsigned int size = 0,
225 num_time = RE_DAILY(re)->dd_ntime;
227 sprintf(subcommand, "D%d", re->re_interval);
228 size += strlen(subcommand);
230 NumsToBuf((unsigned int *)RE_DAILY(re)->dd_time, num_time, NUM_NUM,
231 subcommand, buf_size - size);
233 /* Tack on the duration information */
234 sprintf(tmp_buf, " #%d", re->re_duration);
236 size += strlen(tmp_buf);
237 if (size > buf_size) {
238 printf ("Error: Internal buffer size exceeded\n");
241 strcat (subcommand, tmp_buf);
248 unsigned int subcommand_size)
255 num_items = RE_WEEKLY(re)->wd_ndaytime;
257 sprintf(subcommand, "W%d", re->re_interval);
258 size += strlen(subcommand);
260 /* walk through Day/time data (e.g. TU 1200 Th 2000) */
261 for (i = 0; i < num_items; i++) {
263 /* The day: MO TU TH etc. */
264 WeekDayToString(RE_WEEKLY(re)->wd_daytime[i].dt_day, tmp_buf);
265 size += strlen(tmp_buf);
267 /* Make sure the size of our buffer does not overflow */
268 if (size > subcommand_size) {
269 printf ("Error: Internal buffer size exceeded\n");
273 strcat (subcommand, tmp_buf);
275 /* The hours: 1000 1400 etc. */
276 NumsToBuf((unsigned int *)RE_WEEKLY(re)->wd_daytime[i].dt_time,
277 RE_WEEKLY(re)->wd_daytime[i].dt_ntime, NUM_NUM,
278 subcommand, subcommand_size - size);
280 size = strlen(subcommand);
282 /* Tack on the duration information */
283 sprintf(tmp_buf, " #%d", re->re_duration);
285 size += strlen(tmp_buf);
286 if (size > subcommand_size) {
287 printf ("Error: Internal buffer size exceeded\n");
290 strcat (subcommand, tmp_buf);
297 unsigned int subcommand_size)
304 num_items = RE_MONTHLY(re)->md_nitems;
306 if (re->re_type == RT_MONTHLY_POSITION)
307 sprintf(subcommand, "MP%d", re->re_interval);
309 sprintf(subcommand, "MD%d", re->re_interval);
311 size += strlen(subcommand);
313 if (re->re_type == RT_MONTHLY_POSITION) {
315 /* walk through Day/time data (e.g. TU 1200 Th 2000) */
316 for (i = 0; i < num_items; i++) {
318 /* The week: 1+ 3- etc. */
320 (unsigned int*)RE_MONTHLY(re)->md_weektime[i].wdt_week,
321 RE_MONTHLY(re)->md_weektime[i].wdt_nweek, NUM_WEEK,
322 subcommand, subcommand_size - size);
324 size = strlen(subcommand);
326 /* Make sure the size of our buffer does not overflow */
327 if (size > subcommand_size) {
328 printf ("Error: Internal buffer size exceeded\n");
332 /* The day: SU MO TU etc. */
334 (unsigned int *)RE_MONTHLY(re)->md_weektime[i].wdt_day,
335 RE_MONTHLY(re)->md_weektime[i].wdt_nday, NUM_DAY,
336 subcommand, subcommand_size - size);
338 size = strlen(subcommand);
340 /* Make sure the size of our buffer does not overflow */
341 if (size > subcommand_size) {
342 printf ("Error: Internal buffer size exceeded\n");
346 /* The hours: 1000 1400 etc. */
349 RE_MONTHLY(re)->md_weektime[i].wdt_time,
350 RE_MONTHLY(re)->md_weektime[i].wdt_ntime, NUM_NUM,
351 subcommand, subcommand_size - size);
353 size = strlen(subcommand);
355 } else { /* RT_MONTHLY_DAY */
356 /* The days: 1 15 31 etc. */
357 NumsToBuf((unsigned int *)RE_MONTHLY(re)->md_days,
358 num_items, NUM_NUM, subcommand,
359 subcommand_size - size);
361 /* Tack on the duration information */
362 sprintf(tmp_buf, " #%d", re->re_duration);
364 size += strlen(tmp_buf);
365 if (size > subcommand_size) {
366 printf ("Error: Internal buffer size exceeded\n");
369 strcat (subcommand, tmp_buf);
376 unsigned int subcommand_size)
383 num_items = RE_YEARLY(re)->yd_nitems;
385 if (re->re_type == RT_YEARLY_MONTH)
386 sprintf(subcommand, "YM%d", re->re_interval);
388 sprintf(subcommand, "YD%d", re->re_interval);
390 size += strlen(subcommand);
392 /* An array of days or months */
393 NumsToBuf(RE_YEARLY(re)->yd_items,
394 RE_YEARLY(re)->yd_nitems, NUM_NUM,
395 subcommand, subcommand_size - size);
397 /* Tack on the duration information */
398 sprintf(tmp_buf, " #%d", re->re_duration);
400 size += strlen(tmp_buf);
401 if (size > subcommand_size) {
402 printf ("Error: Internal buffer size exceeded\n");
405 strcat (subcommand, tmp_buf);