1 /* $Id: cdklabel.c,v 1.15 2016/12/04 15:22:16 tom Exp $ */
2 
3 #include <cdk_test.h>
4 
5 #ifdef XCURSES
6 char *XCursesProgramName = "cdklabel";
7 #endif
8 
9 #if !defined (HAVE_SLEEP) && defined (_WIN32)	/* Mingw */
10 #define sleep(x) _sleep(x*1000)
11 #endif
12 
13 /*
14  * Declare file local variables.
15  */
16 static const char *FPUsage = "-m Message String | -f filename [-c Command] [-p Pause Character] [-s Sleep] [-X X Position] [-Y Y Position] [-N] [-S]";
17 
18 /*
19  *
20  */
main(int argc,char ** argv)21 int main (int argc, char **argv)
22 {
23    /* *INDENT-EQLS* */
24    CDKSCREEN *cdkScreen         = 0;
25    CDKLABEL *widget             = 0;
26    char *CDK_WIDGET_COLOR       = 0;
27    char *temp                   = 0;
28    chtype *holder               = 0;
29    int messageLines             = -1;
30    char **messageList           = 0;
31    char tempCommand[1000];
32    int j1, j2;
33 
34    CDK_PARAMS params;
35    boolean boxWidget;
36    boolean shadowWidget;
37    char *command;
38    char *filename;
39    char *message;
40    char waitChar = 0;
41    int sleepLength;
42    int xpos;
43    int ypos;
44 
45    CDKparseParams (argc, argv, &params, "c:f:m:p:s:" CDK_MIN_PARAMS);
46 
47    /* *INDENT-EQLS* */
48    xpos         = CDKparamValue (&params, 'X', CENTER);
49    ypos         = CDKparamValue (&params, 'Y', CENTER);
50    boxWidget    = CDKparamValue (&params, 'N', TRUE);
51    shadowWidget = CDKparamValue (&params, 'S', FALSE);
52    sleepLength  = CDKparamValue (&params, 's', 0);
53    command      = CDKparamString (&params, 'c');
54    filename     = CDKparamString (&params, 'f');
55    message      = CDKparamString (&params, 'm');
56 
57    if ((temp = CDKparamString (&params, 'p')) != 0)
58       waitChar = *temp;
59 
60    /* Make sure we have a message to display. */
61    if (message == 0)
62    {
63       /* No message, maybe they provided a file to read. */
64       if (filename != 0)
65       {
66 	 /* Read the file in. */
67 	 messageLines = CDKreadFile (filename, &messageList);
68 
69 	 /* Check if there was an error. */
70 	 if (messageLines == -1)
71 	 {
72 	    fprintf (stderr, "Error: Could not open the file %s\n", filename);
73 	    ExitProgram (CLI_ERROR);
74 	 }
75       }
76       else
77       {
78 	 /* No message, no file, it's an error. */
79 	 fprintf (stderr, "Usage: %s %s\n", argv[0], FPUsage);
80 	 ExitProgram (CLI_ERROR);
81       }
82    }
83    else
84    {
85       /* Split the message up. */
86       messageList = CDKsplitString (message, '\n');
87       messageLines = (int)CDKcountStrings ((CDK_CSTRING2) messageList);
88    }
89 
90    cdkScreen = initCDKScreen (NULL);
91 
92    /* Start color. */
93    initCDKColor ();
94 
95    /* Check if the user wants to set the background of the main screen. */
96    if ((temp = getenv ("CDK_SCREEN_COLOR")) != 0)
97    {
98       holder = char2Chtype (temp, &j1, &j2);
99       wbkgd (cdkScreen->window, holder[0]);
100       wrefresh (cdkScreen->window);
101       freeChtype (holder);
102    }
103 
104    /* Get the widget color background color. */
105    if ((CDK_WIDGET_COLOR = getenv ("CDK_WIDGET_COLOR")) == 0)
106    {
107       CDK_WIDGET_COLOR = 0;
108    }
109 
110    /* Create the label widget. */
111    widget = newCDKLabel (cdkScreen, xpos, ypos,
112 			 (CDK_CSTRING2) messageList, messageLines,
113 			 boxWidget, shadowWidget);
114 
115    /* Make sure we could create the widget. */
116    if (widget == 0)
117    {
118       CDKfreeStrings (messageList);
119 
120       destroyCDKScreen (cdkScreen);
121       endCDK ();
122 
123       fprintf (stderr,
124 	       "Error: Could not create the label. "
125 	       "Is the window too small?\n");
126 
127       ExitProgram (CLI_ERROR);
128    }
129 
130    /* Check if the user wants to set the background of the widget. */
131    setCDKLabelBackgroundColor (widget, CDK_WIDGET_COLOR);
132 
133    /* Draw the widget. */
134    drawCDKLabel (widget, boxWidget);
135 
136    /* If they supplied a command, run it. */
137    if (command != 0)
138    {
139       const char *fmt = "(sh -c %.*s) >/dev/null 2>&1";
140       sprintf (tempCommand, fmt, (int)(sizeof (tempCommand) - strlen (fmt)), command);
141       system (tempCommand);
142    }
143 
144    /* If they supplied a wait character, wait for the user to hit it. */
145    if (waitChar != 0)
146    {
147       waitCDKLabel (widget, waitChar);
148    }
149 
150    /* If they supplied a sleep time, sleep for the given length. */
151    if (sleepLength > 0)
152    {
153       sleep ((unsigned)sleepLength);
154    }
155 
156    CDKfreeStrings (messageList);
157 
158    destroyCDKLabel (widget);
159    destroyCDKScreen (cdkScreen);
160    endCDK ();
161 
162    /* Exit cleanly. */
163    ExitProgram (0);
164 }
165