Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swen90006-a2-2019
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jin Peiwen
swen90006-a2-2019
Commits
531eb385
Commit
531eb385
authored
Sep 5, 2019
by
Toby Murray
Browse files
Options
Downloads
Patches
Plain Diff
wip getting closer
parent
edbfbc30
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/passbook.c
+141
-4
141 additions, 4 deletions
src/passbook.c
with
141 additions
and
4 deletions
src/passbook.c
+
141
−
4
View file @
531eb385
...
...
@@ -5,13 +5,19 @@
#include
"debug.h"
/**
The idea is to eventually implement state dumping by writing a series
of commands to a file <file> that we can then reload next time by doing
cat <file> - | ./passbook
**/
const
char
INSTRUCTION_PUT
[]
=
"put"
;
const
char
INSTRUCTION_REM
OVE
[]
=
"rem"
;
const
char
INSTRUCTION_REM
[]
=
"rem"
;
const
char
INSTRUCTION_GET
[]
=
"get"
;
const
char
INSTRUCTION_
MPW
[]
=
"
mpw
"
;
const
char
INSTRUCTION_
SAVE
[]
=
"
save
"
;
typedef
struct
{
char
*
username
;
...
...
@@ -41,6 +47,10 @@ static const node_t * lookup(const node_t *p, const char *url){
return
p
;
// not found
}
static
void
node_print
(
const
node_t
*
p
){
printf
(
"URL: %s, Username: %s, Password: %s
\n
"
,
p
->
url
,
p
->
cred
.
username
,
p
->
cred
.
password
);
}
static
node_t
*
node_new
(
const
char
*
url
,
const
cred_t
cred
){
node_t
*
new
=
malloc
(
sizeof
(
node_t
));
assert
(
new
!=
NULL
&&
"new: malloc failed"
);
...
...
@@ -176,10 +186,131 @@ unsigned int tokenise(char *str, char * toks[], unsigned int toksLen){
/* two extra chars in each line: the newline '\n' and NUL '\0' */
#define INSTRUCTION_LENGTH (MAX_LINE_LENGTH+2)
/* a global instruction buffer */
char
inst
[
INSTRUCTION_LENGTH
];
/* password mapping for each url: initially empty */
node_t
*
map
=
NULL
;
/* a list of node pointers -- the idea is to use these to implement a node
stack that we can then use to traverse the tree iteratively, avoiding
recursion. We need tree traversal e.g. when saving the passbook to a file */
typedef
struct
nodeptr_list
{
const
node_t
*
p
;
struct
nodeptr_list
*
next
;
}
nodeptr_list_t
;
nodeptr_list_t
*
list_push
(
nodeptr_list_t
*
lst
,
const
node_t
*
p
){
nodeptr_list_t
*
n
=
malloc
(
sizeof
(
nodeptr_list_t
));
assert
(
n
!=
NULL
&&
"push: malloc failed"
);
n
->
p
=
p
;
n
->
next
=
lst
;
return
n
;
}
void
list_destroy
(
nodeptr_list_t
*
lst
){
while
(
lst
!=
NULL
){
nodeptr_list_t
*
next
=
lst
->
next
;
free
(
lst
);
lst
=
next
;
}
}
void
list_print
(
nodeptr_list_t
*
lst
){
printf
(
"
\n
Begin list print:
\n
"
);
while
(
lst
!=
NULL
){
node_print
(
lst
->
p
);
lst
=
lst
->
next
;
}
printf
(
"End list print.
\n\n
"
);
}
void
print_all
(
const
node_t
*
p
){
nodeptr_list_t
*
lst
=
NULL
;
if
(
p
!=
NULL
){
lst
=
list_push
(
lst
,
p
);
while
(
lst
!=
NULL
){
// keep recursing left until we can go no further
while
(
p
->
left
!=
NULL
){
lst
=
list_push
(
lst
,
p
->
left
);
p
=
p
->
left
;
}
// pop from the stack to simulate the return
const
node_t
*
q
=
lst
->
p
;
nodeptr_list_t
*
temp
=
lst
;
lst
=
lst
->
next
;
free
(
temp
);
// print the node following the return
node_print
(
q
);
// simulate right recursive call
if
(
q
->
right
!=
NULL
){
lst
=
list_push
(
lst
,
q
->
right
);
p
=
q
->
right
;
}
}
}
}
/* returns 0 on successful execution of the instruction in inst */
static
int
execute
(
void
){
assert
(
0
&&
"not implemented yet"
);
char
*
toks
[
4
];
/* these are pointers to start of different tokens */
const
unsigned
int
numToks
=
tokenise
(
inst
,
toks
,
4
);
debug_printf
(
"got %u tokens
\n
"
,
numToks
);
if
(
numToks
==
0
){
/* blank line */
return
0
;
}
if
(
numToks
<
2
){
return
-
1
;
}
if
(
strcmp
(
toks
[
0
],
INSTRUCTION_GET
)
==
0
){
if
(
numToks
!=
2
){
return
-
1
;
}
debug_printf
(
"Looking up: %s
\n
"
,
toks
[
1
]);
const
node_t
*
p
=
lookup
(
map
,
toks
[
1
]);
if
(
p
!=
NULL
){
node_print
(
p
);
}
else
{
printf
(
"Not found.
\n
"
);
}
}
else
if
(
strcmp
(
toks
[
0
],
INSTRUCTION_REM
)
==
0
){
if
(
numToks
!=
2
){
return
-
1
;
}
debug_printf
(
"Removing: %s
\n
"
,
toks
[
1
]);
map
=
rem
(
map
,
toks
[
1
]);
}
else
if
(
strcmp
(
toks
[
0
],
INSTRUCTION_PUT
)
==
0
){
if
(
numToks
!=
4
){
return
-
1
;
}
cred_t
cred
;
cred
.
username
=
toks
[
2
];
cred
.
password
=
toks
[
3
];
map
=
put
(
map
,
toks
[
1
],
cred
);
}
else
if
(
strcmp
(
toks
[
0
],
INSTRUCTION_SAVE
)
==
0
){
if
(
numToks
!=
2
){
return
-
1
;
}
debug_printf
(
"Saving to: %s
\n
"
,
toks
[
1
]);
print_all
(
map
);
}
else
{
debug_printf
(
"Unrecognised instruction
\n
"
);
return
-
1
;
}
return
0
;
}
...
...
@@ -191,6 +322,7 @@ static int run(void){
int
instructionCount
=
0
;
while
(
1
){
memset
(
inst
,
0
,
sizeof
(
inst
));
char
*
res
=
fgets
(
inst
,
MAX_LINE_LENGTH
+
2
,
f
);
if
(
res
==
NULL
){
if
(
feof
(
f
)){
...
...
@@ -223,8 +355,13 @@ static int run(void){
}
}
instructionCount
++
;
int
r
=
execute
();
if
(
r
!=
0
){
return
r
;
}
}
execute
();
assert
(
0
&&
"Should never reach here"
);
return
0
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment