1 /*
2  * Calcurse - text-based organizer
3  *
4  * Copyright (c) 2004-2020 calcurse Development Team <misc@calcurse.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *      - Redistributions of source code must retain the above
12  *        copyright notice, this list of conditions and the
13  *        following disclaimer.
14  *
15  *      - Redistributions in binary form must reproduce the above
16  *        copyright notice, this list of conditions and the
17  *        following disclaimer in the documentation and/or other
18  *        materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Send your feedback or comments to : misc@calcurse.org
33  * Calcurse home page : http://calcurse.org
34  *
35  */
36 
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "calcurse.h"
41 
42 /*
43  * variables to store window size
44  */
45 int col = 0, row = 0;
46 int resize = 0;
47 
48 /* variable to tell if the terminal supports color */
49 unsigned colorize = 0;
50 
51 /* Default background and foreground colors. */
52 int foreground, background;
53 
54 /*
55  * To tell if curses interface was launched already or not (in that case
56  * calcurse is running in command-line mode).
57  * This is useful to konw how to display messages on the screen.
58  */
59 enum ui_mode ui_mode = UI_CMDLINE;
60 
61 /* Don't save anything if this is set. */
62 int read_only = 0;
63 
64 /* Hide import/export message if set. */
65 int quiet = 0;
66 
67 /* Applications can trigger a reload by sending SIGUSR1. */
68 int want_reload = 0;
69 
70 /* Strings describing each input date format. */
71 const char *datefmt_str[DATE_FORMATS];
72 
73 /*
74  * variable to store month lengths
75  */
76 int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
77 
78 /*
79  * variables to store data path names, which are initialized in
80  * io_init()
81  */
82 char *path_ddir = NULL;
83 char *path_cdir = NULL;
84 char *path_todo = NULL;
85 char *path_apts = NULL;
86 char *path_conf = NULL;
87 char *path_notes = NULL;
88 char *path_keys = NULL;
89 char *path_cpid = NULL;
90 char *path_dpid = NULL;
91 char *path_dmon_log = NULL;
92 char *path_hooks = NULL;
93 
94 /* Variable to store global configuration. */
95 struct conf conf;
96 
97 /* Variable to handle pads. */
98 struct pad apad;
99 
100 /* Variable to store notify-bar settings. */
101 struct nbar nbar;
102 
103 /* Variable to store daemon configuration. */
104 struct dmon_conf dmon;
105 
106 /*
107  * Thread id variables for threads that never exit.
108  *
109  * Each variable either carries the identifier of the corresponding thread. If
110  * one of the threads is not running, the corresponding variable is assigned
111  * the identifier of the main thread instead.
112  */
113 pthread_t notify_t_main, io_t_psave, ui_calendar_t_date;
114 
115 /*
116  * Variables init
117  */
vars_init(void)118 void vars_init(void)
119 {
120 	const char *ed, *pg, *mt;
121 
122 	/* Variables for user configuration */
123 	conf.cal_view = CAL_MONTH_VIEW;
124 	conf.todo_view = TODO_HIDE_COMPLETED_VIEW;
125 	conf.multiple_days = 1;
126 	conf.header_line = 1;
127 	conf.event_separator = 1;
128 	conf.day_separator = 1;
129 	conf.empty_appt_line = 1;
130 	strcpy(conf.empty_day, EMPTY_DAY_DEFAULT);
131 	conf.confirm_quit = 1;
132 	conf.confirm_delete = 1;
133 	conf.auto_save = 1;
134 	conf.auto_gc = 0;
135 	conf.periodic_save = 0;
136 	conf.systemevents = 1;
137 	conf.default_panel = CAL;
138 	conf.compact_panels = 0;
139 	strncpy(conf.output_datefmt, "%D", 3);
140 	conf.input_datefmt = 1;
141 	conf.heading_pos = RIGHT;
142 	strcpy(conf.day_heading, DAY_HEADING_DEFAULT);
143 
144 	datefmt_str[0] = _("mm/dd/yyyy");
145 	datefmt_str[1] = _("dd/mm/yyyy");
146 	datefmt_str[2] = _("yyyy/mm/dd");
147 	datefmt_str[3] = _("yyyy-mm-dd");
148 
149 	/* Default external editor and pager */
150 	ed = getenv("CALCURSE_EDITOR");
151 	if (ed == NULL || ed[0] == '\0')
152 		ed = getenv("VISUAL");
153 	if (ed == NULL || ed[0] == '\0')
154 		ed = getenv("EDITOR");
155 	if (ed == NULL || ed[0] == '\0')
156 		ed = DEFAULT_EDITOR;
157 	conf.editor = ed;
158 
159 	pg = getenv("CALCURSE_PAGER");
160 	if (pg == NULL || pg[0] == '\0')
161 		pg = getenv("PAGER");
162 	if (pg == NULL || pg[0] == '\0')
163 		pg = DEFAULT_PAGER;
164 	conf.pager = pg;
165 
166 	mt = getenv("CALCURSE_MERGETOOL");
167 	if (mt == NULL || mt[0] == '\0')
168 		mt = getenv("MERGETOOL");
169 	if (mt == NULL || mt[0] == '\0')
170 		mt = DEFAULT_MERGETOOL;
171 	conf.mergetool = mt;
172 
173 	wins_set_layout(1);
174 
175 	ui_calendar_set_first_day_of_week(MONDAY);
176 
177 	/* Pad structure to scroll text inside the appointment panel */
178 	apad.length = 1;
179 	apad.first_onscreen = 0;
180 
181 	/* Attribute definitions for color and non-color terminals */
182 	custom_init_attr();
183 
184 	/* Start at the current date */
185 	ui_calendar_init_slctd_day();
186 
187 	/* Threads not yet running. */
188 	notify_t_main = io_t_psave = ui_calendar_t_date = pthread_self();
189 }
190