2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: rerule.c /main/4 1995/11/09 12:50:23 rswiston $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company
26 * (c) Copyright 1993, 1994 International Business Machines Corp.
27 * (c) Copyright 1993, 1994 Novell, Inc.
28 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
39 NUM_NUM = 0, /* Used in NumsToBuf to tell the proc what */
40 NUM_DAY, /* type of list we are handing it. */
44 static void NumsToBuf(unsigned int *, unsigned int, NumType, char *,
46 static void WeekNumberToString(WeekNumber, char *);
47 static void WeekDayToString(WeekDay, char *);
48 static void ConvertDaily(RepeatEvent *, char *, unsigned int);
49 static void ConvertWeekly(RepeatEvent *, char *, unsigned int);
50 static void ConvertMonthly(RepeatEvent *, char *, unsigned int);
51 static void ConvertYearly(RepeatEvent *, char *, unsigned int);
53 #define RE_DAILY(re) (re->re_data.re_daily)
54 #define RE_WEEKLY(re) (re->re_data.re_weekly)
55 #define RE_MONTHLY(re) (re->re_data.re_monthly)
56 #define RE_YEARLY(re) (re->re_data.re_yearly)
63 char *cmd_buf_tmp = NULL;
65 int subcommand_size = 0;
66 char subcommand[1024]; /* XXX: fixed size */
68 if (!re) return (char *)NULL;
71 switch (re->re_type) {
74 sprintf(subcommand, "M%d #%d",
75 re->re_interval, re->re_duration);
78 ConvertDaily(re, subcommand, 1024);
81 ConvertWeekly(re, subcommand, 1024);
83 case RT_MONTHLY_POSITION:
85 ConvertMonthly(re, subcommand, 1024);
89 ConvertYearly(re, subcommand, 1024);
93 cmd_buf_tmp = cmd_buf;
95 if (cmd_buf) cmd_buf_size = strlen(cmd_buf);
96 if (subcommand) subcommand_size = strlen(subcommand);
98 cmd_buf = (char *)calloc(1, cmd_buf_size + subcommand_size + 2);
101 strcat (cmd_buf, cmd_buf_tmp);
105 strcat (cmd_buf, " ");
106 strcat (cmd_buf, subcommand);
109 memset (subcommand, NULL, 1024);
118 * Takes an array of numbers, converts them back into their string
119 * type (e.g. SU 1W etc) and puts them into a string buffer with end
120 * marks as necessary, seperated by spaces.
125 unsigned int array_size,
128 unsigned int buf_size)
135 for (i = 0; i < array_size; i++) {
137 sprintf(tmp_buf2, " %d", RE_MASK_STOP(array[i]));
138 else if (type == NUM_DAY)
139 WeekDayToString(RE_MASK_STOP(array[i]), tmp_buf2);
140 else if (type == NUM_WEEK)
141 WeekNumberToString(RE_MASK_STOP(array[i]), tmp_buf2);
143 /* Add end mark if needed */
144 if (RE_STOP_IS_SET(array[i])) {
145 sprintf(tmp_buf, "%s$", tmp_buf2);
146 strcat (buffer, tmp_buf);
147 size += strlen(tmp_buf);
149 strcat (buffer, tmp_buf2);
150 size += strlen(tmp_buf2);
153 /* Make sure the size of our buffer does not overflow */
154 if (size > buf_size) {
155 printf ("Error: Internal buffer size exceeded\n");
166 switch (RE_MASK_STOP(day)) {
168 sprintf (buffer, " SU");
171 sprintf (buffer, " MO");
174 sprintf (buffer, " TU");
177 sprintf (buffer, " WE");
180 sprintf (buffer, " TH");
183 sprintf (buffer, " FR");
186 sprintf (buffer, " SA");
190 if (RE_STOP_IS_SET(day))
191 strcat (buffer, "$");
199 switch (RE_MASK_STOP(week)) {
201 sprintf (buffer, " 1+");
204 sprintf (buffer, " 2+");
207 sprintf (buffer, " 3+");
210 sprintf (buffer, " 4+");
213 sprintf (buffer, " 5+");
216 sprintf (buffer, " 1-");
219 sprintf (buffer, " 2-");
222 sprintf (buffer, " 3-");
225 sprintf (buffer, " 4-");
228 sprintf (buffer, " 5-");
232 if (RE_STOP_IS_SET(week))
233 strcat (buffer, "$");
240 unsigned int buf_size)
243 unsigned int size = 0,
247 num_time = RE_DAILY(re)->dd_ntime;
249 sprintf(subcommand, "D%d", re->re_interval);
250 size += strlen(subcommand);
252 NumsToBuf((unsigned int *)RE_DAILY(re)->dd_time, num_time, NUM_NUM,
253 subcommand, buf_size - size);
255 /* Tack on the duration information */
256 sprintf(tmp_buf, " #%d", re->re_duration);
258 size += strlen(tmp_buf);
259 if (size > buf_size) {
260 printf ("Error: Internal buffer size exceeded\n");
263 strcat (subcommand, tmp_buf);
270 unsigned int subcommand_size)
277 num_items = RE_WEEKLY(re)->wd_ndaytime;
279 sprintf(subcommand, "W%d", re->re_interval);
280 size += strlen(subcommand);
282 /* walk through Day/time data (e.g. TU 1200 Th 2000) */
283 for (i = 0; i < num_items; i++) {
285 /* The day: MO TU TH etc. */
286 WeekDayToString(RE_WEEKLY(re)->wd_daytime[i].dt_day, tmp_buf);
287 size += strlen(tmp_buf);
289 /* Make sure the size of our buffer does not overflow */
290 if (size > subcommand_size) {
291 printf ("Error: Internal buffer size exceeded\n");
295 strcat (subcommand, tmp_buf);
297 /* The hours: 1000 1400 etc. */
298 NumsToBuf((unsigned int *)RE_WEEKLY(re)->wd_daytime[i].dt_time,
299 RE_WEEKLY(re)->wd_daytime[i].dt_ntime, NUM_NUM,
300 subcommand, subcommand_size - size);
302 size = strlen(subcommand);
304 /* Tack on the duration information */
305 sprintf(tmp_buf, " #%d", re->re_duration);
307 size += strlen(tmp_buf);
308 if (size > subcommand_size) {
309 printf ("Error: Internal buffer size exceeded\n");
312 strcat (subcommand, tmp_buf);
319 unsigned int subcommand_size)
326 num_items = RE_MONTHLY(re)->md_nitems;
328 if (re->re_type == RT_MONTHLY_POSITION)
329 sprintf(subcommand, "MP%d", re->re_interval);
331 sprintf(subcommand, "MD%d", re->re_interval);
333 size += strlen(subcommand);
335 if (re->re_type == RT_MONTHLY_POSITION) {
337 /* walk through Day/time data (e.g. TU 1200 Th 2000) */
338 for (i = 0; i < num_items; i++) {
340 /* The week: 1+ 3- etc. */
342 (unsigned int*)RE_MONTHLY(re)->md_weektime[i].wdt_week,
343 RE_MONTHLY(re)->md_weektime[i].wdt_nweek, NUM_WEEK,
344 subcommand, subcommand_size - size);
346 size = strlen(subcommand);
348 /* Make sure the size of our buffer does not overflow */
349 if (size > subcommand_size) {
350 printf ("Error: Internal buffer size exceeded\n");
354 /* The day: SU MO TU etc. */
356 (unsigned int *)RE_MONTHLY(re)->md_weektime[i].wdt_day,
357 RE_MONTHLY(re)->md_weektime[i].wdt_nday, NUM_DAY,
358 subcommand, subcommand_size - size);
360 size = strlen(subcommand);
362 /* Make sure the size of our buffer does not overflow */
363 if (size > subcommand_size) {
364 printf ("Error: Internal buffer size exceeded\n");
368 /* The hours: 1000 1400 etc. */
371 RE_MONTHLY(re)->md_weektime[i].wdt_time,
372 RE_MONTHLY(re)->md_weektime[i].wdt_ntime, NUM_NUM,
373 subcommand, subcommand_size - size);
375 size = strlen(subcommand);
377 } else { /* RT_MONTHLY_DAY */
378 /* The days: 1 15 31 etc. */
379 NumsToBuf((unsigned int *)RE_MONTHLY(re)->md_days,
380 num_items, NUM_NUM, subcommand,
381 subcommand_size - size);
383 /* Tack on the duration information */
384 sprintf(tmp_buf, " #%d", re->re_duration);
386 size += strlen(tmp_buf);
387 if (size > subcommand_size) {
388 printf ("Error: Internal buffer size exceeded\n");
391 strcat (subcommand, tmp_buf);
398 unsigned int subcommand_size)
405 num_items = RE_YEARLY(re)->yd_nitems;
407 if (re->re_type == RT_YEARLY_MONTH)
408 sprintf(subcommand, "YM%d", re->re_interval);
410 sprintf(subcommand, "YD%d", re->re_interval);
412 size += strlen(subcommand);
414 /* An array of days or months */
415 NumsToBuf(RE_YEARLY(re)->yd_items,
416 RE_YEARLY(re)->yd_nitems, NUM_NUM,
417 subcommand, subcommand_size - size);
419 /* Tack on the duration information */
420 sprintf(tmp_buf, " #%d", re->re_duration);
422 size += strlen(tmp_buf);
423 if (size > subcommand_size) {
424 printf ("Error: Internal buffer size exceeded\n");
427 strcat (subcommand, tmp_buf);