-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorseCommand.c
More file actions
94 lines (82 loc) · 2.69 KB
/
Copy pathmorseCommand.c
File metadata and controls
94 lines (82 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* morseCommand.c --
*
* This file implements the "morse" Tcl command for performing
* morse code <-> alphanumeric conversions.
*
*
* Copyright (c) 2002-2004 Michael Thomas <wart@kobold.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <morse.h>
#include <morseCommand.h>
#include <cipherDebug.h>
#include <string.h>
#include <stdlib.h>
/*
* Usage: morse string
*/
int MorseCmd(ClientData, Tcl_Interp *, int , const char **);
int
MorseCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
{
int inputLength=0;
int convType = TOTEXT;
const char *inputString;
char *result=(char *)NULL;
if (argc != 2) {
Tcl_AppendResult(interp, "Usage: ", *argv, " string", (char *)NULL);
return TCL_ERROR;
}
argv++, argc--;
inputString = *argv;
inputLength = (int)strlen(inputString);
argv++, argc--;
/*
* Determine if we are performing a morse -> text or a text -> morse
* translation based on the characters in the input string. The
* iteration variable in this loop doubles as a counter for the length
* of the input string. Clever, eh?
*
* Assume we are converting to morse code unless we find a completely
* valid string of morse characters that is longer than 1 character.
*/
convType = TOMORSE;
if (inputLength > 1 && MorseValid(inputString)) {
convType = TOTEXT;
}
if (convType == TOTEXT) {
/*
* Text translations from morse code are guaranteed to be shorter
* than the morse code string. If we set our result to have
* the same length as the morese code string then we don't have
* to worry about any buffer overflow.
*/
result = (char *)ckalloc(sizeof(char) * inputLength + 1);
MorseStringToString(inputString, result);
Tcl_SetResult(interp, result, TCL_DYNAMIC);
} else {
Tcl_ResetResult(interp);
/*
* result is created by malloc() in StringToMorse.
*/
result = StringToMorse(inputString);
Tcl_SetResult(interp, result, TCL_VOLATILE);
free(result);
}
return TCL_OK;
}