Saturday, February 21, 2009
undefined reference to _yywrap
$flex ch1-02.l
$gcc lex.yy.c -o ch1-02.exe
/cygdrive/d/Profiles/E6097C/LOCALS~1/Temp/ccFsaC7D.o:lex.yy.c:(.text+0x36c): und
efined reference to `_yywrap'
/cygdrive/d/Profiles/E6097C/LOCALS~1/Temp/ccFsaC7D.o:lex.yy.c:(.text+0xac1): und
efined reference to `_yywrap'
collect2: ld returned 1 exit status
solution: compile it with -lfl option.
gcc lex.yy.c -o ch1-02.exe -lfl
- Mihir Patel
Thursday, February 19, 2009
VI Cheat Sheet
How to Exit
:q[uit] | Quit Vim. This fails when changes have been made. |
:q[uit]! | Quit without writing. |
:cq[uit] | Quit always, without writing. |
:wq | Write the current file and exit. |
:wq! | Write the current file and exit always. |
:wq {file} | Write to {file}. Exit if not editing the last |
:wq! {file} | Write to {file} and exit always. |
:[range]wq[!] | [file] Same as above, but only write the lines in [range]. |
ZZ | Write current file, if modified, and exit. |
ZQ | Quit current file and exit (same as ":q!"). |
Editing a File
:e[dit] | Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim. |
:e[dit]! | Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again. |
:e[dit] {file} | Edit {file}. |
:e[dit]! {file} | Edit {file} always. Discard any changes to the current buffer. |
gf | Edit the file whose name is under or after the cursor. Mnemonic: "goto file". |
Inserting Text
a | Append text after the cursor [count] times. |
A | Append text at the end of the line [count] times. |
i | Insert text before the cursor [count] times. |
I | Insert text before the first non-blank in the line [count] times. |
gI | Insert text in column 1 [count] times. |
o | Begin a new line below the cursor and insert text, repeat [count] times. |
O | Begin a new line above the cursor and insert text, repeat [count] times. |
Inserting a file
:r[ead] [name] | Insert the file [name] below the cursor. |
:r[ead] !{cmd} | Execute {cmd} and insert its standard output below the cursor. |
Deleting Text
x | Delete [count] characters under and after the cursor |
X | Delete [count] characters before the cursor |
d{motion} | Delete text that {motion} moves over |
dd | Delete [count] lines |
D | Delete the characters under the cursor until the end of the line |
{Visual}x or
{Visual}d | Delete the highlighted text (for {Visual} see Selecting Text). |
{Visual}CTRL-H or
{Visual} | When in Select mode: Delete the highlighted text |
{Visual}X or
{Visual}D | Delete the highlighted lines |
:[range]d[elete] | Delete [range] lines (default: current line) |
:[range]d[elete] {count} | Delete {count} lines, starting with [range] |
Changing (or Replacing) Text
r{char} | replace the character under the cursor with {char}. |
R | Enter Insert mode, replacing characters rather than inserting |
~ | Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters. |
~{motion} | switch case of {motion} text. |
{Visual}~ | Switch case of highlighted text |
Substituting
:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count] | For each line in [range] replace a match of {pattern} with {string}. |
:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count] | Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add extra flags |
The arguments that you can use for the substitute commands:
[c] Confirm each substitution. Vim positions the cursor on the matching
string. You can type:
'y' to substitute this match
'n' to skip this matchto skip this match
'a' to substitute this and all remaining matches {not in Vi}
'q' to quit substituting {not in Vi}
CTRL-E to scroll the screen up {not in Vi}
CTRL-Y to scroll the screen down {not in Vi}.
[e] When the search pattern fails, do not issue an error message and, in
particular, continue in maps as if no error occurred.
[g] Replace all occurrences in the line. Without this argument,
replacement occurs only for the first occurrence in each line.
[i] Ignore case for the pattern.
[I] Don't ignore case for the pattern.
[p] Print the line containing the last substitute.
Copying and Moving Text
"{a-zA-Z0-9.%#:-"} | Use register {a-zA-Z0-9.%#:-"} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put). |
:reg[isters] | Display the contents of all numbered and named registers. |
:reg[isters] {arg} | Display the contents of the numbered and named registers that are mentioned in {arg}. |
:di[splay] [arg] | Same as :registers. |
["x]y{motion} | Yank {motion} text [into register x]. |
["x]yy | Yank [count] lines [into register x] |
["x]Y | yank [count] lines [into register x] (synonym for yy). |
{Visual}["x]y | Yank the highlighted text [into register x] (for {Visual} see Selecting Text). |
{Visual}["x]Y | Yank the highlighted lines [into register x] |
:[range]y[ank] [x] | Yank [range] lines [into register x]. |
:[range]y[ank] [x] {count} | Yank {count} lines, starting with last line number in [range] (default: current line), [into register x]. |
["x]p | Put the text [from register x] after the cursor [count] times. |
["x]P | Put the text [from register x] before the cursor [count] times. |
["x]gp | Just like "p", but leave the cursor just after the new text. |
["x]gP | Just like "P", but leave the cursor just after the new text. |
:[line]pu[t] [x] | Put the text [from register x] after [line] (default current line). |
:[line]pu[t]! [x] | Put the text [from register x] before [line] (default current line). |
Undo/Redo/Repeat
u | Undo [count] changes. |
:u[ndo] | Undo one change. |
CTRL-R | Redo [count] changes which were undone. |
:red[o] | Redo one change which was undone. |
U | Undo all latest changes on one line. {Vi: while not moved off of it} |
. | Repeat last change, with count replaced with [count]. |
Moving Around
Basic motion commands:
k
h l
j
h or
| [count] characters to the left (exclusive). |
l or
| [count] characters to the right (exclusive). |
k or
CTRL-P | [count] lines upward |
j or
CTRL-J or CTRL-N | [count] lines downward (linewise). |
0 | To the first character of the line (exclusive). |
To the first character of the line (exclusive). | |
^ | To the first non-blank character of the line |
$ or
| To the end of the line and [count - 1] lines downward |
g0 or
g | When lines wrap ('wrap on): To the first character of the screen line (exclusive). Differs from "0" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost character of the current line that is on the screen. Differs from "0" when the first character of the line is not on the screen. |
g^ | When lines wrap ('wrap' on): To the first non-blank character of the screen line (exclusive). Differs from "^" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost non-blank character of the current line that is on the screen. Differs from "^" when the first non-blank character of the line is not on the screen. |
g$ or
g When lines wrap ('wrap' on): To the last character of the screen line and [count - 1] screen lines downward (inclusive). Differs from "$" when a line is wider than the screen. When lines don't wrap ('wrap' off): To the rightmost character of the current line that is visible on the screen. Differs from "$" when the last character of the line is not on the screen or when a count is used. | |
f{char} | To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive). |
F{char} | To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive). |
t{char} | Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive). |
T{char} | Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive). |
; | Repeat latest f, t, F or T [count] times. |
, | Repeat latest f, t, F or T in opposite direction [count] times. |
- | [count] lines upward, on the first non-blank character (linewise). |
+ or
CTRL-M or | [count] lines downward, on the first non-blank character (linewise). |
_ | [count] - 1 lines downward, on the first non-blank character (linewise). |
G | Goto line [count], default last line, on the first non-blank character. |
gg | Goto line [count], default first line, on the first non-blank character. |
w | [count] words forward |
W | [count] WORDS forward |
e | Forward to the end of word [count] |
E | Forward to the end of WORD [count] |
b | [count] words backward |
B | [count] WORDS backward |
ge | Backward to the end of word [count] |
gE | Backward to the end of WORD [count] |
A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs,
A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a word and a WORD.
( | [count] sentences backward |
) | [count] sentences forward |
{ | [count] paragraphs backward |
} | [count] paragraphs forward |
]] | [count] sections forward or to the next '{' in the first column. When used after an operator, then the '}' in the first column. |
][ | [count] sections forward or to the next '}' in the first column |
[[ | [count] sections backward or to the previous '{' in the first column |
[] | [count] sections backward or to the previous '}' in the first column |
Marks
m{a-zA-Z} | Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command). |
m' or
m` | Set the previous context mark. This can be jumped to with the "''" or "``" command (does not move the cursor, this is not a motion command). |
:[range]ma[rk] {a-zA-Z} | Set mark {a-zA-Z} at last line number in [range], column 0. Default is cursor line. |
:[range]k{a-zA-Z} | Same as :mark, but the space before the mark name can be omitted. |
'{a-z} | To the first non-blank character on the line with mark {a-z} (linewise). |
'{A-Z0-9} | To the first non-blank character on the line with mark {A-Z0-9} in the correct file |
`{a-z} | To the mark {a-z} |
`{A-Z0-9} | To the mark {A-Z0-9} in the correct file |
:marks | List all the current marks (not a motion command). |
:marks {arg} | List the marks that are mentioned in {arg} (not a motion command). For example: |
Searching
/{pattern}[/] | Search forward for the [count]'th occurrence of {pattern} |
/{pattern}/{offset} | Search forward for the [count]'th occurrence of {pattern} and go {offset} lines up or down. |
/ | Search forward for the [count]'th latest used pattern |
//{offset} | Search forward for the [count]'th latest used pattern with new. If {offset} is empty no offset is used. |
?{pattern}[?] | Search backward for the [count]'th previous occurrence of {pattern} |
?{pattern}?{offset} | Search backward for the [count]'th previous occurrence of {pattern} and go {offset} lines up or down |
? | Search backward for the [count]'th latest used pattern |
??{offset} | Search backward for the [count]'th latest used pattern with new {offset}. If {offset} is empty no offset is used. |
n | Repeat the latest "/" or "?" [count] times. |
N | Repeat the latest "/" or "?" [count] times in opposite direction. |
Selecting Text (Visual Mode)
To select text, enter visual mode with one of the commands below, and use motion commands to highlight the text you are interested in. Then, use some command on the text.
The operators that can be used are:
~ switch case
d delete
c change
y yank
> shift right
< shift left
! filter through external command
= filter through 'equalprg' option command
gq format lines to 'textwidth' length
v | start Visual mode per character. |
V | start Visual mode linewise. |
exit Visual mode without making any changes |
How to Suspend
CTRL-Z | Suspend Vim, like ":stop". Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character. |
:sus[pend][!] or
:st[op][!] | Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later! |
Vimtutor
vimtutor | Instead of running vim from your shell try running vimtutor instead. This is a built in tuturial for VIM, it is a very usfull and handy tool |
VIM Cheat Sheet
Working with files | |
Vim command | Action |
:e filename | Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt. |
:w filename | Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name. |
:q | Quit Vim. If you have unsaved changes, Vim refuses to exit. |
:q! | Exit Vim without saving changes. |
:wq | Write the file and exit. |
:x | Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file. |
Moving in the file | |
These Vim commands and keys work both in command mode and visual mode. | |
Vim command | Action |
j or Up Arrow | Move the cursor up one line. |
k or Down Arrow | Down one line. |
l or Right Arrow | Right one character. |
h or Left Arrow | Left one character. |
e | To the end of a word. |
E | To the end of a whitespace-delimited word. |
b | To the beginning of a word. |
B | To the beginning of a whitespace-delimited word. |
0 | To the beginning of a line. |
^ | To the first non-whitespace character of a line. |
$ | To the end of a line. |
H | To the first line of the screen. |
M | To the middle line of the screen. |
L | To the the last line of the screen. |
:n | Jump to line number n. For example, to jump to line 42, you'd type :42 |
Inserting and overwriting text | |
Vim command | Action |
i | Insert before cursor. |
I | Insert to the start of the current line. |
a | Append after cursor. |
A | Append to the end of the current line. |
o | Open a new line below and insert. |
O | Open a new line above and insert. |
C | Change the rest of the current line. |
r | Overwrite one character. After overwriting the single character, go back to command mode. |
R | Enter insert mode but replace characters rather than inserting. |
The ESC key | Exit insert/overwrite mode and go back to command mode. |
Deleting text | |
Vim command | Action |
x | Delete characters under the cursor. |
X | Delete characters before the cursor. |
dd or :d | Delete the current line. |
Entering visual mode | |
Vim command | Action |
v | Start highlighting characters. Use the normal movement keys and commands to select text for highlighting. |
V | Start highlighting lines. |
The ESC key | Exit visual mode and return to command mode. |
Editing blocks of text | |
The Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text. | |
Vim command | Action |
~ | Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor. |
> (V) | Shift right. |
< (V) | Shift left. |
c (V) | Change the highlighted text. |
y (V) | Yank the highlighted text. In Winblows terms, "copy the selected text to clipboard." |
d (V) | Delete the highlighted text. In Winblows terms, "cut the selected text to clipboard." |
yy or :y or Y | Yank the current line. You don't need to highlight it first. |
dd or :d | Delete the current line. Again, you don't need to highlight it first. |
p | In Winblows terms, "paste" the contents of the "clipboard". In Vim terms, you "put" the text you yanked or deleted. Put characters after the cursor. Put lines below the current line. |
P | Put characters before the cursor. Put lines above the current line. |
Undo and redo | |
Vim command | Action |
u | Undo the last action. |
U | Undo all the latest changes that were made to the current line. |
Ctrl + r | Redo. |
Search | |
Vim command | Action |
/pattern | Search the file for pattern. |
n | Scan for next search match in the same direction. |
N | Scan for next search match but opposite direction. |
Replace | |
Vim command | Action |
:rs/foo/bar/a | Substitute foo with bar. r determines the range and a determines the arguments. |
The range (r) can be | |
nothing | Work on current line only. |
number | Work on the line whose number you give. |
% | The whole file. |
Arguments (a) can be | |
g | Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line. |
i | Ignore case for the search pattern. |
I | Don't ignore case. |
c | Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution. |
Examples | |
:452s/foo/bar/ | Replace the first occurrence of the word foo with bar on line number 452. |
:s/foo/bar/g | Replace every occurrence of the word foo with bar on current line. |
:%s/foo/bar/g | Replace every occurrence of the word foo with bar in the whole file. |
:%s/foo/bar/gi | The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. |
:%s/foo/bar/gc | Confirm every substitution. |
:%s/foo/bar/c | For each line on the file, replace the first occurrence of foo with bar and confirm every substitution. |
Lex and Yacc Installation with Cygwin
$
bison --version
bison (GNU Bison) 1.875b
$
flex --version
Wednesday, February 18, 2009
Run Your Personal wiki from a USB Stick
You don't have to lease server space or keep your home computer always on to access a personal web server—you can run a web, FTP, and database server straight from a USB drive. A slim web server package called XAMPP fits on a USB stick and can run database-driven webapps like the software that powers Wikipedia, MediaWiki. Almost two years ago you learned how to set up your "personal Wikipedia" on your home web server to capture ideas and track document revisions in a central knowledge repository. Today we'll set up MediaWiki on your flash drive for access on any Windows PC on the go.
A word on security: The XAMPP web server package is intended for web developers to use while building web sites, so by default, the passwords are blank and configuration is not secure. Running server software opens up a port on your computer, which in and of itself is a security risk. Getting this set up requires getting your hands dirty editing configuration files. If you're not comfortable with the risks and implications of this solution but still want your own wiki, I highly recommend the free, hosted, PBWiki. Also check out the nifty TiddlyWiki for a less setup-intensive wiki-on-a-thumb-drive alternative.
Still with me? Let's get your personal wiki running on a thumb drive.
What You'll Need
In order to get your personal Wikipedia running on your thumb drive, you'll need:
- A Windows XP PC with Service Pack 2 or Windows Vista Ultimate
- A thumb drive with at least 150MB free. The bigger your drive, the more data you can store on it so go as big as you can here.
Install and Start XAMPP
Download the XAMPP Lite .EXE installer from here. Double-click on the file to extract it, and choose your flash drive's drive letter as the destination (not a subfolder.) A folder called xampplite
will be created on your thumb drive.
(Note: I chose XAMPP Lite because it offers everything we need to run MediaWiki, and it's smaller in size than XAMPP. But XAMPP is updated more often than XAMPP Lite, and you can install add-ons with full-on XAMPP, so it may be a consideration if you want to do more than just run MediaWiki on your thumb drive.)
Once XAMPPLite is saved to your flash drive, to start it up, double-click on the xampp-control.exe
file in the xampplite
directory. That will launch XAMPP's Control Panel, which lets you start all the services you need.
To run MediaWiki, you need two services: Apache web server, and the MySql database server. Click the Start button next to each of those in the XAMPP Control Panel, as shown.
When you hit the Start button, your system's firewall may ask whether or not it should allow incoming and outgoing connections to the software. Allow it.
Now your portable web server is up and running. To see it in action, in your web browser, visit http://localhost/. Click on your language of choice—by default, XAMPP Lite's English homepage looks like this.
The first thing we want to do is set up a few passwords to secure your servers. Click on the "Security" link on the sidebar (by default, located at http://localhost/security/. You'll see that by default, all of XAMPP's settings are marked as insecure. Click on the link below the security table to "fix" the problems (by default, http://localhost/security/xamppsecurity.php.) On this next page, set your MySql database password for the root user. If you choose, you can set a password for visiting any web page as well, as shown.
To enable the new MySQL password, switch to the XAMPP Control Panel, stop MySql, and start it again.
Install and Configure MediaWiki
Now that your web and database server is up and running, it's time to install the MediaWiki wiki software. Download the latest version of MediaWiki from here (as of this writing, the file name is mediawiki-1.11.1.tar.gz
.) Extract the files (first gunzip, then tar, the free 7-Zip utility can do this for you), and by default they'll be stored in a folder called mediawiki-1.11.1
. Rename this folder simply wiki
and move it to your thumb drive in XAMPP's xampplite\htdocs\
folder. When you're done, if your thumb drive's letter is G:\, the full path to the MediaWiki files would beG:\xampplite\htdocs\wiki\
.
To configure MediaWiki, visit http://localhost/wiki/. Click on the link to configure your wiki. There you can keep all the default settings except:
- You must enter the name of your wiki. Mine is "GinaWiki."
- You must enter a wiki administrator name (by default, WikiSysop) and password.
- Enter your database information. The database name can stay wikidb, but change the DB username to root and enter the password you set up above.
Scroll to the bottom of the page and click on the "Install MediaWiki!" button to save your settings. When the installation is complete, MediaWiki will prompt you to move the LocalSettings.config.php
file up a directory on your thumb drive. Go ahead and do that. You're moving the file fromG:\xampplite\htdocs\wiki\config\LocalSettings.php
toG:\xampplite\htdocs\wiki\LocalSettings.php
.
Finally, visit http://localhost/wiki/ to see your fresh new MediaWiki installation in action, as shown below. Congratulations!
From here you can start editing and adding pages, creating user accounts and even uploading files to your new wiki.
But first, two helpful customizations will make your new wiki easier to use.
- Make your wiki the default start page. When you visit your local server, you might want your wiki's home page to appear first instead of XAMPP's homepage. To do that, open the
index.php
file in XAMPP'shtdocs
directory in a text editor. Change the line that reads:header('Location: '.$uri.'/xampp/');
toheader('Location: '.$uri.'/wiki/');
and save it. Now, when you visit http://localhost/, you'll go directly to your wiki. - Add a custom wiki logo. To add your own logo to the upper left hand side of your wiki install, create a 135x135 pixel image, name it
mywikilogo.png
, and save it toH:\xampplite\htdocs\wiki\skins\common\images
where H: is your thumb drive's letter. Then, open theH:\xampplite\htdocs\wiki\LocalSettings.php
file in a text editor and add the following line:$wgLogo = "/wiki/skins/common/images/mywikilogo.png";
Save the file, and refresh your wiki in the browser. You should now see your custom logo. (Note: the logo doesn't have to be a PNG file; substitute your image's extension, like JPG or GIF.)
Stopping and Starting Your Wiki
When you're finished using your wiki, invoke the XAMPP Control Panel running in your system tray. Click the Stop button next to Apache, then click the Stop button next to MySql. Once the servers have stopped, click the Exit button. You can now eject your thumb drive as usual. Remember: don't try to eject your thumb drive before stopping the services; doing so can blue screen your PC and possibly screw up your wiki and server installation.
To start up your wiki at a new computer, plug in your thumb drive, and double-click on the xampp-control.exe
file to start the Control Panel. From there, start the Apache and MySql servers, then visit http://localhost/ to edit your wiki.
Why a personal Wikipedia?
A wiki is an incredible collaborative tool for groups of people, but it's also a great one-stop shop for your own personal "stuff I want to remember"—like ideas, drafts, lists, and bookmarks. If you haven't edited Wikipedia or just want to see what MediaWiki can do, here's a quick demonstration of what writing a novel draft would be like using the software and its specialWikitext markup.
More Portable Web Servers
XAMPP isn't the only portable web server on the block. Another free portable server package called Server2Go includes Perl 5.8. Thanks, Vijay!
What do you do with your key chain web server? Let us know in the comments.
Ref :-
Gina Trapani, the editor of Lifehacker, likes her web servers tiny and portable. Her weekly feature, Geek to Live, appears every Monday on Lifehacker. Subscribe to the Geek to Live feed to get new installments in your newsreader.