make sure previously allocated mem get free()'d if subsequent allocactions fail

This commit is contained in:
marschap
2008-12-14 09:59:34 +00:00
parent 4b11c5aa17
commit 99928e7303
2 changed files with 69 additions and 14 deletions
+68 -14
View File
@@ -1,5 +1,5 @@
/** \file server/screen.c /** \file server/screen.c
* Does screen management * Does screen management.
*/ */
/* This file is part of LCDd, the lcdproc server. /* This file is part of LCDd, the lcdproc server.
@@ -9,6 +9,7 @@
* *
* Copyright (c) 1999, William Ferrell, Scott Scriven * Copyright (c) 1999, William Ferrell, Scott Scriven
* 2003, Joris Robijn * 2003, Joris Robijn
* 2008, Peter Marschall
*/ */
#include <stdlib.h> #include <stdlib.h>
@@ -40,30 +41,39 @@ char *pri_names[] = {
NULL, NULL,
}; };
/** Create a screen.
* \param id Screen id; it's name.
* \param client Client, the screen belongs to.
* \return Pointer to freshly created screen.
*/
Screen * Screen *
screen_create(char *id, Client *client) screen_create(char *id, Client *client)
{ {
Screen *s; Screen *s;
debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])", __FUNCTION__, id, (client?client->sock:-1)); debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])",
__FUNCTION__, id, (client?client->sock:-1));
s = malloc(sizeof(Screen));
if (!s) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
return NULL;
}
if (!id) { if (!id) {
report(RPT_ERR, "%s: Need id string", __FUNCTION__); report(RPT_ERR, "%s: Need id string", __FUNCTION__);
return NULL; return NULL;
} }
/* Client can be NULL for serverscreens and other client-less screens */ /* Client can be NULL for serverscreens and other client-less screens */
s->id = strdup(id); s = malloc(sizeof(Screen));
if (!s->id) { if (s == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__); report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
return NULL; return NULL;
} }
s->id = strdup(id);
if (s->id == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
free(s);
return NULL;
}
s->name = NULL; s->name = NULL;
s->priority = PRI_INFO; s->priority = PRI_INFO;
s->duration = default_duration; s->duration = default_duration;
@@ -82,8 +92,10 @@ screen_create(char *id, Client *client)
s->cursor_y = 1; s->cursor_y = 1;
s->widgetlist = LL_new(); s->widgetlist = LL_new();
if (!s->widgetlist) { if (s->widgetlist == NULL) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__); report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
free(s);
free(s->id);
return NULL; return NULL;
} }
@@ -92,6 +104,12 @@ screen_create(char *id, Client *client)
return s; return s;
} }
/** Destroy a screen.
* \param s Screen to destroy.
* \retval <0 Error; no screen given.
* \retval 0 Success.
*/
int int
screen_destroy(Screen *s) screen_destroy(Screen *s)
{ {
@@ -108,17 +126,30 @@ screen_destroy(Screen *s)
widget_destroy(w); widget_destroy(w);
} }
LL_Destroy(s->widgetlist); LL_Destroy(s->widgetlist);
s->widgetlist = NULL;
if (s->id) if (s->id != NULL) {
free(s->id); free(s->id);
if (s->name) s->id = NULL;
}
if (s->name != NULL) {
free(s->name); free(s->name);
s->name = NULL;
}
free(s); free(s);
s = NULL;
return 0; return 0;
} }
/** Add a widget to a screen.
* \param s Screen to add the widget \c w to.
* \param w Widget to be added to \c s.
* \retval <0 Error.
* \retval 0 Success.
*/
int int
screen_add_widget(Screen *s, Widget *w) screen_add_widget(Screen *s, Widget *w)
{ {
@@ -129,6 +160,13 @@ screen_add_widget(Screen *s, Widget *w)
return 0; return 0;
} }
/** Remove a widget from a screen.
* \param s Screen to remove the widget \c w from.
* \param w Widget to be removed from \c s.
* \retval <0 Error.
* \retval 0 Success.
*/
int int
screen_remove_widget(Screen *s, Widget *w) screen_remove_widget(Screen *s, Widget *w)
{ {
@@ -139,6 +177,12 @@ screen_remove_widget(Screen *s, Widget *w)
return 0; return 0;
} }
/** Find a widget on a screen by its id.
* \param s Screen where to look for the widget.
* \param id Identifier of the widget.
* \return Pointerr to the widget; \c NULL if widget was not found or error.
*/
Widget * Widget *
screen_find_widget(Screen *s, char *id) screen_find_widget(Screen *s, char *id)
{ {
@@ -151,7 +195,7 @@ screen_find_widget(Screen *s, char *id)
debug(RPT_DEBUG, "%s(s=[%.40s], id=\"%.40s\")", __FUNCTION__, s->id, id); debug(RPT_DEBUG, "%s(s=[%.40s], id=\"%.40s\")", __FUNCTION__, s->id, id);
for (w = LL_GetFirst(s->widgetlist); w; w = LL_GetNext(s->widgetlist)) { for (w = LL_GetFirst(s->widgetlist); w != NULL; w = LL_GetNext(s->widgetlist)) {
if (0 == strcmp(w->id, id)) { if (0 == strcmp(w->id, id)) {
debug(RPT_DEBUG, "%s: Found %s", __FUNCTION__, id); debug(RPT_DEBUG, "%s: Found %s", __FUNCTION__, id);
return w; return w;
@@ -159,7 +203,7 @@ screen_find_widget(Screen *s, char *id)
/* Search subscreens recursively */ /* Search subscreens recursively */
if (w->type == WID_FRAME) { if (w->type == WID_FRAME) {
w = widget_search_subs(w, id); w = widget_search_subs(w, id);
if (w) if (w != NULL)
return w; return w;
} }
} }
@@ -167,6 +211,11 @@ screen_find_widget(Screen *s, char *id)
return NULL; return NULL;
} }
/** Convert a priority name to the priority id.
* \param priname Name of the screen priority.
* \return Priority id associatited with \c priname.
*/
Priority Priority
screen_pri_name_to_pri(char *priname) screen_pri_name_to_pri(char *priname)
{ {
@@ -182,6 +231,11 @@ screen_pri_name_to_pri(char *priname)
return pri; return pri;
} }
/** Convert a priority id to the associated name.
* \param pri Priority id.
* \return Priority name associated with \c pri.
*/
char * char *
screen_pri_to_pri_name(Priority pri) screen_pri_to_pri_name(Priority pri)
{ {
+1
View File
@@ -1,4 +1,5 @@
/** \file server/screen.h /** \file server/screen.h
* Public interface to the screen management methods.
*/ */
/* This file is part of LCDd, the lcdproc server. /* This file is part of LCDd, the lcdproc server.